]>
Commit | Line | Data |
---|---|---|
846e33c7 | 1 | /* |
2234212c | 2 | * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
846e33c7 RS |
4 | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
d02b48c6 | 13 | #include "apps.h" |
ec577822 BM |
14 | #include <openssl/bio.h> |
15 | #include <openssl/asn1.h> | |
16 | #include <openssl/err.h> | |
17 | #include <openssl/bn.h> | |
18 | #include <openssl/evp.h> | |
19 | #include <openssl/x509.h> | |
20 | #include <openssl/x509v3.h> | |
21 | #include <openssl/objects.h> | |
22 | #include <openssl/pem.h> | |
3eeaab4b | 23 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 24 | # include <openssl/rsa.h> |
3eeaab4b NL |
25 | #endif |
26 | #ifndef OPENSSL_NO_DSA | |
0f113f3e | 27 | # include <openssl/dsa.h> |
3eeaab4b | 28 | #endif |
d02b48c6 | 29 | |
d02b48c6 | 30 | #undef POSTFIX |
0f113f3e MC |
31 | #define POSTFIX ".srl" |
32 | #define DEF_DAYS 30 | |
33 | ||
6d23cf97 | 34 | static int callb(int ok, X509_STORE_CTX *ctx); |
0f113f3e | 35 | static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, |
4a60bb18 TS |
36 | const EVP_MD *digest, CONF *conf, const char *section, |
37 | int preserve_dates); | |
cc696296 | 38 | static int x509_certify(X509_STORE *ctx, const char *CAfile, const EVP_MD *digest, |
0f113f3e | 39 | X509 *x, X509 *xca, EVP_PKEY *pkey, |
cc696296 | 40 | STACK_OF(OPENSSL_STRING) *sigopts, const char *serialfile, |
0f113f3e | 41 | int create, int days, int clrext, CONF *conf, |
4a60bb18 TS |
42 | const char *section, ASN1_INTEGER *sno, int reqfile, |
43 | int preserve_dates); | |
d4cec6a1 | 44 | static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt); |
7e1b7485 | 45 | |
7e1b7485 RS |
46 | typedef enum OPTION_choice { |
47 | OPT_ERR = -1, OPT_EOF = 0, OPT_HELP, | |
48 | OPT_INFORM, OPT_OUTFORM, OPT_KEYFORM, OPT_REQ, OPT_CAFORM, | |
49 | OPT_CAKEYFORM, OPT_SIGOPT, OPT_DAYS, OPT_PASSIN, OPT_EXTFILE, | |
50 | OPT_EXTENSIONS, OPT_IN, OPT_OUT, OPT_SIGNKEY, OPT_CA, | |
51 | OPT_CAKEY, OPT_CASERIAL, OPT_SET_SERIAL, OPT_FORCE_PUBKEY, | |
52 | OPT_ADDTRUST, OPT_ADDREJECT, OPT_SETALIAS, OPT_CERTOPT, OPT_NAMEOPT, | |
53 | OPT_C, OPT_EMAIL, OPT_OCSP_URI, OPT_SERIAL, OPT_NEXT_SERIAL, | |
54 | OPT_MODULUS, OPT_PUBKEY, OPT_X509TOREQ, OPT_TEXT, OPT_HASH, | |
55 | OPT_ISSUER_HASH, OPT_SUBJECT, OPT_ISSUER, OPT_FINGERPRINT, OPT_DATES, | |
56 | OPT_PURPOSE, OPT_STARTDATE, OPT_ENDDATE, OPT_CHECKEND, OPT_CHECKHOST, | |
57 | OPT_CHECKEMAIL, OPT_CHECKIP, OPT_NOOUT, OPT_TRUSTOUT, OPT_CLRTRUST, | |
58 | OPT_CLRREJECT, OPT_ALIAS, OPT_CACREATESERIAL, OPT_CLREXT, OPT_OCSPID, | |
7e1b7485 RS |
59 | OPT_SUBJECT_HASH_OLD, |
60 | OPT_ISSUER_HASH_OLD, | |
4a60bb18 | 61 | OPT_BADSIG, OPT_MD, OPT_ENGINE, OPT_NOCERT, OPT_PRESERVE_DATES |
7e1b7485 RS |
62 | } OPTION_CHOICE; |
63 | ||
44c83ebd | 64 | const OPTIONS x509_options[] = { |
7e1b7485 RS |
65 | {"help", OPT_HELP, '-', "Display this summary"}, |
66 | {"inform", OPT_INFORM, 'f', | |
67 | "Input format - default PEM (one of DER, NET or PEM)"}, | |
68 | {"in", OPT_IN, '<', "Input file - default stdin"}, | |
69 | {"outform", OPT_OUTFORM, 'f', | |
70 | "Output format - default PEM (one of DER, NET or PEM)"}, | |
71 | {"out", OPT_OUT, '>', "Output file - default stdout"}, | |
72 | {"keyform", OPT_KEYFORM, 'F', "Private key format - default PEM"}, | |
16e1b281 | 73 | {"passin", OPT_PASSIN, 's', "Private key password/pass-phrase source"}, |
7e1b7485 RS |
74 | {"serial", OPT_SERIAL, '-', "Print serial number value"}, |
75 | {"subject_hash", OPT_HASH, '-', "Print subject hash value"}, | |
76 | {"issuer_hash", OPT_ISSUER_HASH, '-', "Print issuer hash value"}, | |
7e1b7485 RS |
77 | {"hash", OPT_HASH, '-', "Synonym for -subject_hash"}, |
78 | {"subject", OPT_SUBJECT, '-', "Print subject DN"}, | |
79 | {"issuer", OPT_ISSUER, '-', "Print issuer DN"}, | |
80 | {"email", OPT_EMAIL, '-', "Print email address(es)"}, | |
81 | {"startdate", OPT_STARTDATE, '-', "Set notBefore field"}, | |
82 | {"enddate", OPT_ENDDATE, '-', "Set notAfter field"}, | |
83 | {"purpose", OPT_PURPOSE, '-', "Print out certificate purposes"}, | |
84 | {"dates", OPT_DATES, '-', "Both Before and After dates"}, | |
85 | {"modulus", OPT_MODULUS, '-', "Print the RSA key modulus"}, | |
86 | {"pubkey", OPT_PUBKEY, '-', "Output the public key"}, | |
87 | {"fingerprint", OPT_FINGERPRINT, '-', | |
88 | "Print the certificate fingerprint"}, | |
89 | {"alias", OPT_ALIAS, '-', "Output certificate alias"}, | |
90 | {"noout", OPT_NOOUT, '-', "No output, just status"}, | |
91 | {"nocert", OPT_NOCERT, '-', "No certificate output"}, | |
92 | {"ocspid", OPT_OCSPID, '-', | |
93 | "Print OCSP hash values for the subject name and public key"}, | |
94 | {"ocsp_uri", OPT_OCSP_URI, '-', "Print OCSP Responder URL(s)"}, | |
95 | {"trustout", OPT_TRUSTOUT, '-', "Output a trusted certificate"}, | |
96 | {"clrtrust", OPT_CLRTRUST, '-', "Clear all trusted purposes"}, | |
12d56b29 | 97 | {"clrext", OPT_CLREXT, '-', "Clear all certificate extensions"}, |
7e1b7485 RS |
98 | {"addtrust", OPT_ADDTRUST, 's', "Trust certificate for a given purpose"}, |
99 | {"addreject", OPT_ADDREJECT, 's', | |
100 | "Reject certificate for a given purpose"}, | |
101 | {"setalias", OPT_SETALIAS, 's', "Set certificate alias"}, | |
102 | {"days", OPT_DAYS, 'n', | |
103 | "How long till expiry of a signed certificate - def 30 days"}, | |
56087077 | 104 | {"checkend", OPT_CHECKEND, 'M', |
7e1b7485 RS |
105 | "Check whether the cert expires in the next arg seconds"}, |
106 | {OPT_MORE_STR, 1, 1, "Exit 1 if so, 0 if not"}, | |
107 | {"signkey", OPT_SIGNKEY, '<', "Self sign cert with arg"}, | |
108 | {"x509toreq", OPT_X509TOREQ, '-', | |
109 | "Output a certification request object"}, | |
110 | {"req", OPT_REQ, '-', "Input is a certificate request, sign and output"}, | |
111 | {"CA", OPT_CA, '<', "Set the CA certificate, must be PEM format"}, | |
dd958974 | 112 | {"CAkey", OPT_CAKEY, 's', |
7e1b7485 RS |
113 | "The CA key, must be PEM format; if not in CAfile"}, |
114 | {"CAcreateserial", OPT_CACREATESERIAL, '-', | |
115 | "Create serial number file if it does not exist"}, | |
5c4e3a4e | 116 | {"CAserial", OPT_CASERIAL, 's', "Serial file"}, |
7e1b7485 RS |
117 | {"set_serial", OPT_SET_SERIAL, 's', "Serial number to use"}, |
118 | {"text", OPT_TEXT, '-', "Print the certificate in text form"}, | |
119 | {"C", OPT_C, '-', "Print out C code forms"}, | |
120 | {"extfile", OPT_EXTFILE, '<', "File with X509V3 extensions to add"}, | |
121 | {"extensions", OPT_EXTENSIONS, 's', "Section from config file to use"}, | |
122 | {"nameopt", OPT_NAMEOPT, 's', "Various certificate name options"}, | |
123 | {"certopt", OPT_CERTOPT, 's', "Various certificate text options"}, | |
124 | {"checkhost", OPT_CHECKHOST, 's', "Check certificate matches host"}, | |
125 | {"checkemail", OPT_CHECKEMAIL, 's', "Check certificate matches email"}, | |
126 | {"checkip", OPT_CHECKIP, 's', "Check certificate matches ipaddr"}, | |
127 | {"CAform", OPT_CAFORM, 'F', "CA format - default PEM"}, | |
128 | {"CAkeyform", OPT_CAKEYFORM, 'F', "CA key format - default PEM"}, | |
16e1b281 | 129 | {"sigopt", OPT_SIGOPT, 's', "Signature parameter in n:v form"}, |
12d56b29 F |
130 | {"force_pubkey", OPT_FORCE_PUBKEY, '<', "Force the Key to put inside certificate"}, |
131 | {"next_serial", OPT_NEXT_SERIAL, '-', "Increment current certificate serial number"}, | |
132 | {"clrreject", OPT_CLRREJECT, '-', | |
133 | "Clears all the prohibited or rejected uses of the certificate"}, | |
16e1b281 | 134 | {"badsig", OPT_BADSIG, '-', "Corrupt last byte of certificate signature (for test)"}, |
7e1b7485 | 135 | {"", OPT_MD, '-', "Any supported digest"}, |
9c3bcfa0 RS |
136 | #ifndef OPENSSL_NO_MD5 |
137 | {"subject_hash_old", OPT_SUBJECT_HASH_OLD, '-', | |
138 | "Print old-style (MD5) issuer hash value"}, | |
139 | {"issuer_hash_old", OPT_ISSUER_HASH_OLD, '-', | |
140 | "Print old-style (MD5) subject hash value"}, | |
7e1b7485 | 141 | #endif |
9c3bcfa0 RS |
142 | #ifndef OPENSSL_NO_ENGINE |
143 | {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"}, | |
7e1b7485 | 144 | #endif |
4a60bb18 | 145 | {"preserve_dates", OPT_PRESERVE_DATES, '-', "preserve existing dates when signing"}, |
7e1b7485 RS |
146 | {NULL} |
147 | }; | |
667ac4ec | 148 | |
7e1b7485 | 149 | int x509_main(int argc, char **argv) |
0f113f3e | 150 | { |
0f113f3e | 151 | ASN1_INTEGER *sno = NULL; |
f83b85fb | 152 | ASN1_OBJECT *objtmp = NULL; |
0f113f3e | 153 | BIO *out = NULL; |
7e1b7485 RS |
154 | CONF *extconf = NULL; |
155 | EVP_PKEY *Upkey = NULL, *CApkey = NULL, *fkey = NULL; | |
0f113f3e | 156 | STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL; |
7e1b7485 RS |
157 | STACK_OF(OPENSSL_STRING) *sigopts = NULL; |
158 | X509 *x = NULL, *xca = NULL; | |
159 | X509_REQ *req = NULL, *rq = NULL; | |
160 | X509_STORE *ctx = NULL; | |
161 | const EVP_MD *digest = NULL; | |
162 | char *CAkeyfile = NULL, *CAserial = NULL, *fkeyfile = NULL, *alias = NULL; | |
163 | char *checkhost = NULL, *checkemail = NULL, *checkip = NULL; | |
164 | char *extsect = NULL, *extfile = NULL, *passin = NULL, *passinarg = NULL; | |
0f113f3e | 165 | char *infile = NULL, *outfile = NULL, *keyfile = NULL, *CAfile = NULL; |
b5c4209b | 166 | char *prog; |
333b070e RS |
167 | int x509req = 0, days = DEF_DAYS, modulus = 0, pubkey = 0, pprint = 0; |
168 | int C = 0, CAformat = FORMAT_PEM, CAkeyformat = FORMAT_PEM; | |
169 | int fingerprint = 0, reqfile = 0, need_rand = 0, checkend = 0; | |
7e1b7485 RS |
170 | int informat = FORMAT_PEM, outformat = FORMAT_PEM, keyformat = FORMAT_PEM; |
171 | int next_serial = 0, subject_hash = 0, issuer_hash = 0, ocspid = 0; | |
172 | int noout = 0, sign_flag = 0, CA_flag = 0, CA_createserial = 0, email = 0; | |
173 | int ocsp_uri = 0, trustout = 0, clrtrust = 0, clrreject = 0, aliasout = 0; | |
174 | int ret = 1, i, num = 0, badsig = 0, clrext = 0, nocert = 0; | |
333b070e | 175 | int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0; |
56087077 VD |
176 | int enddate = 0; |
177 | time_t checkoffset = 0; | |
b5c4209b | 178 | unsigned long certflag = 0; |
4a60bb18 | 179 | int preserve_dates = 0; |
7e1b7485 | 180 | OPTION_CHOICE o; |
7e1b7485 | 181 | ENGINE *e = NULL; |
7e1b7485 RS |
182 | #ifndef OPENSSL_NO_MD5 |
183 | int subject_hash_old = 0, issuer_hash_old = 0; | |
645749ef | 184 | #endif |
d02b48c6 | 185 | |
0f113f3e MC |
186 | ctx = X509_STORE_new(); |
187 | if (ctx == NULL) | |
188 | goto end; | |
189 | X509_STORE_set_verify_cb(ctx, callb); | |
190 | ||
7e1b7485 RS |
191 | prog = opt_init(argc, argv, x509_options); |
192 | while ((o = opt_next()) != OPT_EOF) { | |
193 | switch (o) { | |
194 | case OPT_EOF: | |
195 | case OPT_ERR: | |
196 | opthelp: | |
197 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
198 | goto end; | |
199 | case OPT_HELP: | |
200 | opt_help(x509_options); | |
201 | ret = 0; | |
202 | goto end; | |
203 | case OPT_INFORM: | |
204 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat)) | |
205 | goto opthelp; | |
206 | break; | |
207 | case OPT_IN: | |
208 | infile = opt_arg(); | |
209 | break; | |
210 | case OPT_OUTFORM: | |
211 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat)) | |
212 | goto opthelp; | |
213 | break; | |
214 | case OPT_KEYFORM: | |
215 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat)) | |
216 | goto opthelp; | |
217 | break; | |
218 | case OPT_CAFORM: | |
219 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAformat)) | |
220 | goto opthelp; | |
221 | break; | |
222 | case OPT_CAKEYFORM: | |
dd958974 | 223 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &CAkeyformat)) |
7e1b7485 RS |
224 | goto opthelp; |
225 | break; | |
226 | case OPT_OUT: | |
227 | outfile = opt_arg(); | |
228 | break; | |
229 | case OPT_REQ: | |
230 | reqfile = need_rand = 1; | |
231 | break; | |
232 | ||
233 | case OPT_SIGOPT: | |
0f113f3e MC |
234 | if (!sigopts) |
235 | sigopts = sk_OPENSSL_STRING_new_null(); | |
7e1b7485 RS |
236 | if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg())) |
237 | goto opthelp; | |
238 | break; | |
7e1b7485 | 239 | case OPT_DAYS: |
4a60bb18 TS |
240 | if (preserve_dates) |
241 | goto opthelp; | |
7e1b7485 RS |
242 | days = atoi(opt_arg()); |
243 | break; | |
244 | case OPT_PASSIN: | |
245 | passinarg = opt_arg(); | |
246 | break; | |
247 | case OPT_EXTFILE: | |
248 | extfile = opt_arg(); | |
249 | break; | |
250 | case OPT_EXTENSIONS: | |
251 | extsect = opt_arg(); | |
252 | break; | |
253 | case OPT_SIGNKEY: | |
254 | keyfile = opt_arg(); | |
0f113f3e MC |
255 | sign_flag = ++num; |
256 | need_rand = 1; | |
7e1b7485 RS |
257 | break; |
258 | case OPT_CA: | |
259 | CAfile = opt_arg(); | |
0f113f3e MC |
260 | CA_flag = ++num; |
261 | need_rand = 1; | |
7e1b7485 RS |
262 | break; |
263 | case OPT_CAKEY: | |
264 | CAkeyfile = opt_arg(); | |
265 | break; | |
266 | case OPT_CASERIAL: | |
267 | CAserial = opt_arg(); | |
268 | break; | |
269 | case OPT_SET_SERIAL: | |
08f6ae5b | 270 | if (sno != NULL) { |
efba7787 | 271 | BIO_printf(bio_err, "Serial number supplied twice\n"); |
08f6ae5b MC |
272 | goto opthelp; |
273 | } | |
7e1b7485 RS |
274 | if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL) |
275 | goto opthelp; | |
276 | break; | |
277 | case OPT_FORCE_PUBKEY: | |
278 | fkeyfile = opt_arg(); | |
279 | break; | |
280 | case OPT_ADDTRUST: | |
281 | if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) { | |
282 | BIO_printf(bio_err, | |
283 | "%s: Invalid trust object value %s\n", | |
284 | prog, opt_arg()); | |
285 | goto opthelp; | |
0f113f3e | 286 | } |
7e1b7485 RS |
287 | if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL) |
288 | goto end; | |
0f113f3e | 289 | sk_ASN1_OBJECT_push(trust, objtmp); |
f83b85fb | 290 | objtmp = NULL; |
0f113f3e | 291 | trustout = 1; |
7e1b7485 RS |
292 | break; |
293 | case OPT_ADDREJECT: | |
294 | if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) { | |
0f113f3e | 295 | BIO_printf(bio_err, |
7e1b7485 RS |
296 | "%s: Invalid reject object value %s\n", |
297 | prog, opt_arg()); | |
298 | goto opthelp; | |
0f113f3e | 299 | } |
7e1b7485 RS |
300 | if (reject == NULL |
301 | && (reject = sk_ASN1_OBJECT_new_null()) == NULL) | |
302 | goto end; | |
0f113f3e | 303 | sk_ASN1_OBJECT_push(reject, objtmp); |
f83b85fb | 304 | objtmp = NULL; |
0f113f3e | 305 | trustout = 1; |
7e1b7485 RS |
306 | break; |
307 | case OPT_SETALIAS: | |
308 | alias = opt_arg(); | |
0f113f3e | 309 | trustout = 1; |
7e1b7485 RS |
310 | break; |
311 | case OPT_CERTOPT: | |
312 | if (!set_cert_ex(&certflag, opt_arg())) | |
313 | goto opthelp; | |
314 | break; | |
315 | case OPT_NAMEOPT: | |
b5c4209b | 316 | if (!set_nameopt(opt_arg())) |
7e1b7485 RS |
317 | goto opthelp; |
318 | break; | |
319 | case OPT_ENGINE: | |
333b070e | 320 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
321 | break; |
322 | case OPT_C: | |
0f113f3e | 323 | C = ++num; |
7e1b7485 RS |
324 | break; |
325 | case OPT_EMAIL: | |
0f113f3e | 326 | email = ++num; |
7e1b7485 RS |
327 | break; |
328 | case OPT_OCSP_URI: | |
0f113f3e | 329 | ocsp_uri = ++num; |
7e1b7485 RS |
330 | break; |
331 | case OPT_SERIAL: | |
0f113f3e | 332 | serial = ++num; |
7e1b7485 RS |
333 | break; |
334 | case OPT_NEXT_SERIAL: | |
0f113f3e | 335 | next_serial = ++num; |
7e1b7485 RS |
336 | break; |
337 | case OPT_MODULUS: | |
0f113f3e | 338 | modulus = ++num; |
7e1b7485 RS |
339 | break; |
340 | case OPT_PUBKEY: | |
0f113f3e | 341 | pubkey = ++num; |
7e1b7485 RS |
342 | break; |
343 | case OPT_X509TOREQ: | |
0f113f3e | 344 | x509req = ++num; |
7e1b7485 RS |
345 | break; |
346 | case OPT_TEXT: | |
0f113f3e | 347 | text = ++num; |
7e1b7485 RS |
348 | break; |
349 | case OPT_SUBJECT: | |
0f113f3e | 350 | subject = ++num; |
7e1b7485 RS |
351 | break; |
352 | case OPT_ISSUER: | |
0f113f3e | 353 | issuer = ++num; |
7e1b7485 RS |
354 | break; |
355 | case OPT_FINGERPRINT: | |
0f113f3e | 356 | fingerprint = ++num; |
7e1b7485 RS |
357 | break; |
358 | case OPT_HASH: | |
359 | subject_hash = ++num; | |
360 | break; | |
361 | case OPT_ISSUER_HASH: | |
362 | issuer_hash = ++num; | |
363 | break; | |
364 | case OPT_PURPOSE: | |
0f113f3e | 365 | pprint = ++num; |
7e1b7485 RS |
366 | break; |
367 | case OPT_STARTDATE: | |
0f113f3e | 368 | startdate = ++num; |
7e1b7485 RS |
369 | break; |
370 | case OPT_ENDDATE: | |
0f113f3e | 371 | enddate = ++num; |
7e1b7485 RS |
372 | break; |
373 | case OPT_NOOUT: | |
0f113f3e | 374 | noout = ++num; |
7e1b7485 RS |
375 | break; |
376 | case OPT_NOCERT: | |
377 | nocert = 1; | |
378 | break; | |
379 | case OPT_TRUSTOUT: | |
0f113f3e | 380 | trustout = 1; |
7e1b7485 RS |
381 | break; |
382 | case OPT_CLRTRUST: | |
0f113f3e | 383 | clrtrust = ++num; |
7e1b7485 RS |
384 | break; |
385 | case OPT_CLRREJECT: | |
0f113f3e | 386 | clrreject = ++num; |
7e1b7485 RS |
387 | break; |
388 | case OPT_ALIAS: | |
0f113f3e | 389 | aliasout = ++num; |
7e1b7485 RS |
390 | break; |
391 | case OPT_CACREATESERIAL: | |
0f113f3e | 392 | CA_createserial = ++num; |
7e1b7485 RS |
393 | break; |
394 | case OPT_CLREXT: | |
0f113f3e | 395 | clrext = 1; |
7e1b7485 RS |
396 | break; |
397 | case OPT_OCSPID: | |
0f113f3e | 398 | ocspid = ++num; |
7e1b7485 RS |
399 | break; |
400 | case OPT_BADSIG: | |
0f113f3e | 401 | badsig = 1; |
0f113f3e | 402 | break; |
7e1b7485 RS |
403 | #ifndef OPENSSL_NO_MD5 |
404 | case OPT_SUBJECT_HASH_OLD: | |
405 | subject_hash_old = ++num; | |
406 | break; | |
407 | case OPT_ISSUER_HASH_OLD: | |
408 | issuer_hash_old = ++num; | |
409 | break; | |
9c3bcfa0 RS |
410 | #else |
411 | case OPT_SUBJECT_HASH_OLD: | |
412 | case OPT_ISSUER_HASH_OLD: | |
413 | break; | |
7e1b7485 RS |
414 | #endif |
415 | case OPT_DATES: | |
416 | startdate = ++num; | |
417 | enddate = ++num; | |
418 | break; | |
419 | case OPT_CHECKEND: | |
7e1b7485 | 420 | checkend = 1; |
33254e1c RL |
421 | { |
422 | intmax_t temp = 0; | |
423 | if (!opt_imax(opt_arg(), &temp)) | |
424 | goto opthelp; | |
425 | checkoffset = (time_t)temp; | |
426 | if ((intmax_t)checkoffset != temp) { | |
427 | BIO_printf(bio_err, "%s: checkend time out of range %s\n", | |
428 | prog, opt_arg()); | |
429 | goto opthelp; | |
430 | } | |
56087077 | 431 | } |
7e1b7485 RS |
432 | break; |
433 | case OPT_CHECKHOST: | |
434 | checkhost = opt_arg(); | |
435 | break; | |
436 | case OPT_CHECKEMAIL: | |
437 | checkemail = opt_arg(); | |
438 | break; | |
439 | case OPT_CHECKIP: | |
440 | checkip = opt_arg(); | |
441 | break; | |
4a60bb18 TS |
442 | case OPT_PRESERVE_DATES: |
443 | if (days != DEF_DAYS) | |
444 | goto opthelp; | |
445 | preserve_dates = 1; | |
446 | break; | |
7e1b7485 RS |
447 | case OPT_MD: |
448 | if (!opt_md(opt_unknown(), &digest)) | |
449 | goto opthelp; | |
0f113f3e | 450 | } |
7e1b7485 RS |
451 | } |
452 | argc = opt_num_rest(); | |
453 | argv = opt_rest(); | |
454 | if (argc != 0) { | |
455 | BIO_printf(bio_err, "%s: Unknown parameter %s\n", prog, argv[0]); | |
456 | goto opthelp; | |
0f113f3e MC |
457 | } |
458 | ||
bdd58d98 | 459 | out = bio_open_default(outfile, 'w', outformat); |
7e1b7485 | 460 | if (out == NULL) |
0f113f3e | 461 | goto end; |
7e1b7485 | 462 | |
0f113f3e | 463 | if (need_rand) |
7e1b7485 | 464 | app_RAND_load_file(NULL, 0); |
0f113f3e | 465 | |
7e1b7485 | 466 | if (!app_passwd(passinarg, NULL, &passin, NULL)) { |
0f113f3e MC |
467 | BIO_printf(bio_err, "Error getting password\n"); |
468 | goto end; | |
469 | } | |
470 | ||
471 | if (!X509_STORE_set_default_paths(ctx)) { | |
472 | ERR_print_errors(bio_err); | |
473 | goto end; | |
474 | } | |
475 | ||
2234212c | 476 | if (fkeyfile != NULL) { |
7e1b7485 | 477 | fkey = load_pubkey(fkeyfile, keyformat, 0, NULL, e, "Forced key"); |
0f113f3e MC |
478 | if (fkey == NULL) |
479 | goto end; | |
480 | } | |
481 | ||
482 | if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM)) { | |
483 | CAkeyfile = CAfile; | |
484 | } else if ((CA_flag) && (CAkeyfile == NULL)) { | |
485 | BIO_printf(bio_err, | |
486 | "need to specify a CAkey if using the CA command\n"); | |
487 | goto end; | |
488 | } | |
489 | ||
2234212c | 490 | if (extfile != NULL) { |
0f113f3e | 491 | X509V3_CTX ctx2; |
cc01d217 | 492 | if ((extconf = app_load_config(extfile)) == NULL) |
0f113f3e | 493 | goto end; |
2234212c | 494 | if (extsect == NULL) { |
0f113f3e | 495 | extsect = NCONF_get_string(extconf, "default", "extensions"); |
2234212c | 496 | if (extsect == NULL) { |
0f113f3e MC |
497 | ERR_clear_error(); |
498 | extsect = "default"; | |
499 | } | |
500 | } | |
501 | X509V3_set_ctx_test(&ctx2); | |
502 | X509V3_set_nconf(&ctx2, extconf); | |
503 | if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) { | |
504 | BIO_printf(bio_err, | |
505 | "Error Loading extension section %s\n", extsect); | |
506 | ERR_print_errors(bio_err); | |
507 | goto end; | |
508 | } | |
509 | } | |
510 | ||
511 | if (reqfile) { | |
512 | EVP_PKEY *pkey; | |
513 | BIO *in; | |
514 | ||
515 | if (!sign_flag && !CA_flag) { | |
516 | BIO_printf(bio_err, "We need a private key to sign with\n"); | |
517 | goto end; | |
518 | } | |
bdd58d98 | 519 | in = bio_open_default(infile, 'r', informat); |
7e1b7485 | 520 | if (in == NULL) |
0f113f3e | 521 | goto end; |
0f113f3e MC |
522 | req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL); |
523 | BIO_free(in); | |
524 | ||
525 | if (req == NULL) { | |
526 | ERR_print_errors(bio_err); | |
527 | goto end; | |
528 | } | |
529 | ||
c5137473 | 530 | if ((pkey = X509_REQ_get0_pubkey(req)) == NULL) { |
0f113f3e MC |
531 | BIO_printf(bio_err, "error unpacking public key\n"); |
532 | goto end; | |
533 | } | |
534 | i = X509_REQ_verify(req, pkey); | |
0f113f3e MC |
535 | if (i < 0) { |
536 | BIO_printf(bio_err, "Signature verification error\n"); | |
537 | ERR_print_errors(bio_err); | |
538 | goto end; | |
539 | } | |
540 | if (i == 0) { | |
541 | BIO_printf(bio_err, | |
542 | "Signature did not match the certificate request\n"); | |
543 | goto end; | |
2234212c | 544 | } else { |
0f113f3e | 545 | BIO_printf(bio_err, "Signature ok\n"); |
2234212c | 546 | } |
0f113f3e MC |
547 | |
548 | print_name(bio_err, "subject=", X509_REQ_get_subject_name(req), | |
b5c4209b | 549 | get_nameopt()); |
0f113f3e MC |
550 | |
551 | if ((x = X509_new()) == NULL) | |
552 | goto end; | |
553 | ||
554 | if (sno == NULL) { | |
555 | sno = ASN1_INTEGER_new(); | |
96487cdd | 556 | if (sno == NULL || !rand_serial(NULL, sno)) |
0f113f3e MC |
557 | goto end; |
558 | if (!X509_set_serialNumber(x, sno)) | |
559 | goto end; | |
560 | ASN1_INTEGER_free(sno); | |
561 | sno = NULL; | |
2234212c | 562 | } else if (!X509_set_serialNumber(x, sno)) { |
0f113f3e | 563 | goto end; |
2234212c | 564 | } |
0f113f3e | 565 | |
124055a9 | 566 | if (!X509_set_issuer_name(x, X509_REQ_get_subject_name(req))) |
0f113f3e | 567 | goto end; |
124055a9 | 568 | if (!X509_set_subject_name(x, X509_REQ_get_subject_name(req))) |
0f113f3e | 569 | goto end; |
dc047d31 DSH |
570 | if (!set_cert_times(x, NULL, NULL, days)) |
571 | goto end; | |
0f113f3e | 572 | |
2234212c | 573 | if (fkey != NULL) { |
0f113f3e | 574 | X509_set_pubkey(x, fkey); |
2234212c | 575 | } else { |
c5137473 | 576 | pkey = X509_REQ_get0_pubkey(req); |
0f113f3e | 577 | X509_set_pubkey(x, pkey); |
0f113f3e | 578 | } |
2234212c | 579 | } else { |
a773b52a | 580 | x = load_cert(infile, informat, "Certificate"); |
2234212c | 581 | } |
0f113f3e MC |
582 | |
583 | if (x == NULL) | |
584 | goto end; | |
585 | if (CA_flag) { | |
a773b52a | 586 | xca = load_cert(CAfile, CAformat, "CA Certificate"); |
0f113f3e MC |
587 | if (xca == NULL) |
588 | goto end; | |
589 | } | |
590 | ||
591 | if (!noout || text || next_serial) { | |
592 | OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3"); | |
593 | ||
0f113f3e MC |
594 | } |
595 | ||
596 | if (alias) | |
597 | X509_alias_set1(x, (unsigned char *)alias, -1); | |
598 | ||
599 | if (clrtrust) | |
600 | X509_trust_clear(x); | |
601 | if (clrreject) | |
602 | X509_reject_clear(x); | |
603 | ||
2234212c | 604 | if (trust != NULL) { |
0f113f3e MC |
605 | for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) { |
606 | objtmp = sk_ASN1_OBJECT_value(trust, i); | |
607 | X509_add1_trust_object(x, objtmp); | |
608 | } | |
f83b85fb | 609 | objtmp = NULL; |
0f113f3e MC |
610 | } |
611 | ||
2234212c | 612 | if (reject != NULL) { |
0f113f3e MC |
613 | for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) { |
614 | objtmp = sk_ASN1_OBJECT_value(reject, i); | |
615 | X509_add1_reject_object(x, objtmp); | |
616 | } | |
f83b85fb | 617 | objtmp = NULL; |
0f113f3e MC |
618 | } |
619 | ||
0f022f5a | 620 | if (badsig) { |
8adc1cb8 DSH |
621 | const ASN1_BIT_STRING *signature; |
622 | ||
0f022f5a | 623 | X509_get0_signature(&signature, NULL, x); |
a0754084 | 624 | corrupt_signature(signature); |
0f022f5a DSH |
625 | } |
626 | ||
0f113f3e MC |
627 | if (num) { |
628 | for (i = 1; i <= num; i++) { | |
629 | if (issuer == i) { | |
b5c4209b | 630 | print_name(out, "issuer=", X509_get_issuer_name(x), get_nameopt()); |
0f113f3e | 631 | } else if (subject == i) { |
fb0303f3 | 632 | print_name(out, "subject=", |
b5c4209b | 633 | X509_get_subject_name(x), get_nameopt()); |
0f113f3e | 634 | } else if (serial == i) { |
7e1b7485 RS |
635 | BIO_printf(out, "serial="); |
636 | i2a_ASN1_INTEGER(out, X509_get_serialNumber(x)); | |
637 | BIO_printf(out, "\n"); | |
0f113f3e | 638 | } else if (next_serial == i) { |
12d56b29 F |
639 | ASN1_INTEGER *ser = X509_get_serialNumber(x); |
640 | BIGNUM *bnser = ASN1_INTEGER_to_BN(ser, NULL); | |
641 | ||
0f113f3e MC |
642 | if (!bnser) |
643 | goto end; | |
644 | if (!BN_add_word(bnser, 1)) | |
645 | goto end; | |
646 | ser = BN_to_ASN1_INTEGER(bnser, NULL); | |
647 | if (!ser) | |
648 | goto end; | |
649 | BN_free(bnser); | |
650 | i2a_ASN1_INTEGER(out, ser); | |
651 | ASN1_INTEGER_free(ser); | |
652 | BIO_puts(out, "\n"); | |
653 | } else if ((email == i) || (ocsp_uri == i)) { | |
654 | int j; | |
655 | STACK_OF(OPENSSL_STRING) *emlst; | |
656 | if (email == i) | |
657 | emlst = X509_get1_email(x); | |
658 | else | |
659 | emlst = X509_get1_ocsp(x); | |
660 | for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++) | |
7e1b7485 | 661 | BIO_printf(out, "%s\n", |
0f113f3e MC |
662 | sk_OPENSSL_STRING_value(emlst, j)); |
663 | X509_email_free(emlst); | |
664 | } else if (aliasout == i) { | |
665 | unsigned char *alstr; | |
666 | alstr = X509_alias_get0(x, NULL); | |
667 | if (alstr) | |
7e1b7485 | 668 | BIO_printf(out, "%s\n", alstr); |
0f113f3e | 669 | else |
7e1b7485 | 670 | BIO_puts(out, "<No Alias>\n"); |
0f113f3e | 671 | } else if (subject_hash == i) { |
7e1b7485 | 672 | BIO_printf(out, "%08lx\n", X509_subject_name_hash(x)); |
0f113f3e | 673 | } |
0e0c6821 | 674 | #ifndef OPENSSL_NO_MD5 |
0f113f3e | 675 | else if (subject_hash_old == i) { |
7e1b7485 | 676 | BIO_printf(out, "%08lx\n", X509_subject_name_hash_old(x)); |
0f113f3e | 677 | } |
0e0c6821 | 678 | #endif |
0f113f3e | 679 | else if (issuer_hash == i) { |
7e1b7485 | 680 | BIO_printf(out, "%08lx\n", X509_issuer_name_hash(x)); |
0f113f3e | 681 | } |
0e0c6821 | 682 | #ifndef OPENSSL_NO_MD5 |
0f113f3e | 683 | else if (issuer_hash_old == i) { |
7e1b7485 | 684 | BIO_printf(out, "%08lx\n", X509_issuer_name_hash_old(x)); |
0f113f3e | 685 | } |
0e0c6821 | 686 | #endif |
0f113f3e MC |
687 | else if (pprint == i) { |
688 | X509_PURPOSE *ptmp; | |
689 | int j; | |
7e1b7485 | 690 | BIO_printf(out, "Certificate purposes:\n"); |
0f113f3e MC |
691 | for (j = 0; j < X509_PURPOSE_get_count(); j++) { |
692 | ptmp = X509_PURPOSE_get0(j); | |
7e1b7485 | 693 | purpose_print(out, x, ptmp); |
0f113f3e MC |
694 | } |
695 | } else if (modulus == i) { | |
696 | EVP_PKEY *pkey; | |
697 | ||
c01ff880 | 698 | pkey = X509_get0_pubkey(x); |
0f113f3e MC |
699 | if (pkey == NULL) { |
700 | BIO_printf(bio_err, "Modulus=unavailable\n"); | |
701 | ERR_print_errors(bio_err); | |
702 | goto end; | |
703 | } | |
7e1b7485 | 704 | BIO_printf(out, "Modulus="); |
cf1b7d96 | 705 | #ifndef OPENSSL_NO_RSA |
9862e9aa | 706 | if (EVP_PKEY_id(pkey) == EVP_PKEY_RSA) { |
2ac6115d | 707 | const BIGNUM *n; |
9862e9aa RL |
708 | RSA_get0_key(EVP_PKEY_get0_RSA(pkey), &n, NULL, NULL); |
709 | BN_print(out, n); | |
710 | } else | |
7be304ac | 711 | #endif |
cf1b7d96 | 712 | #ifndef OPENSSL_NO_DSA |
6e9fa57c | 713 | if (EVP_PKEY_id(pkey) == EVP_PKEY_DSA) { |
2ac6115d | 714 | const BIGNUM *dsapub = NULL; |
6e9fa57c MC |
715 | DSA_get0_key(EVP_PKEY_get0_DSA(pkey), &dsapub, NULL); |
716 | BN_print(out, dsapub); | |
717 | } else | |
7be304ac | 718 | #endif |
6e9fa57c | 719 | { |
7e1b7485 | 720 | BIO_printf(out, "Wrong Algorithm type"); |
6e9fa57c | 721 | } |
7e1b7485 | 722 | BIO_printf(out, "\n"); |
0f113f3e MC |
723 | } else if (pubkey == i) { |
724 | EVP_PKEY *pkey; | |
725 | ||
c01ff880 | 726 | pkey = X509_get0_pubkey(x); |
0f113f3e MC |
727 | if (pkey == NULL) { |
728 | BIO_printf(bio_err, "Error getting public key\n"); | |
729 | ERR_print_errors(bio_err); | |
730 | goto end; | |
731 | } | |
7e1b7485 | 732 | PEM_write_bio_PUBKEY(out, pkey); |
0f113f3e MC |
733 | } else if (C == i) { |
734 | unsigned char *d; | |
735 | char *m; | |
7e1b7485 | 736 | int len; |
0f113f3e | 737 | |
b5c4209b DB |
738 | print_name(out, "/*\n" |
739 | " * Subject: ", X509_get_subject_name(x), get_nameopt()); | |
740 | print_name(out, " * Issuer: ", X509_get_issuer_name(x), get_nameopt()); | |
741 | BIO_puts(out, " */\n"); | |
0f113f3e | 742 | |
7e1b7485 | 743 | len = i2d_X509(x, NULL); |
68dc6824 | 744 | m = app_malloc(len, "x509 name buffer"); |
0f113f3e | 745 | d = (unsigned char *)m; |
7e1b7485 RS |
746 | len = i2d_X509_NAME(X509_get_subject_name(x), &d); |
747 | print_array(out, "the_subject_name", len, (unsigned char *)m); | |
0f113f3e | 748 | d = (unsigned char *)m; |
7e1b7485 RS |
749 | len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &d); |
750 | print_array(out, "the_public_key", len, (unsigned char *)m); | |
0f113f3e | 751 | d = (unsigned char *)m; |
7e1b7485 RS |
752 | len = i2d_X509(x, &d); |
753 | print_array(out, "the_certificate", len, (unsigned char *)m); | |
0f113f3e MC |
754 | OPENSSL_free(m); |
755 | } else if (text == i) { | |
b5c4209b | 756 | X509_print_ex(out, x, get_nameopt(), certflag); |
0f113f3e | 757 | } else if (startdate == i) { |
7e1b7485 | 758 | BIO_puts(out, "notBefore="); |
568ce3a5 | 759 | ASN1_TIME_print(out, X509_get0_notBefore(x)); |
7e1b7485 | 760 | BIO_puts(out, "\n"); |
0f113f3e | 761 | } else if (enddate == i) { |
7e1b7485 | 762 | BIO_puts(out, "notAfter="); |
568ce3a5 | 763 | ASN1_TIME_print(out, X509_get0_notAfter(x)); |
7e1b7485 | 764 | BIO_puts(out, "\n"); |
0f113f3e MC |
765 | } else if (fingerprint == i) { |
766 | int j; | |
767 | unsigned int n; | |
768 | unsigned char md[EVP_MAX_MD_SIZE]; | |
769 | const EVP_MD *fdig = digest; | |
770 | ||
2234212c | 771 | if (fdig == NULL) |
0f113f3e MC |
772 | fdig = EVP_sha1(); |
773 | ||
774 | if (!X509_digest(x, fdig, md, &n)) { | |
775 | BIO_printf(bio_err, "out of memory\n"); | |
776 | goto end; | |
777 | } | |
7e1b7485 | 778 | BIO_printf(out, "%s Fingerprint=", |
0f113f3e MC |
779 | OBJ_nid2sn(EVP_MD_type(fdig))); |
780 | for (j = 0; j < (int)n; j++) { | |
7e1b7485 | 781 | BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n) |
0f113f3e MC |
782 | ? '\n' : ':'); |
783 | } | |
784 | } | |
785 | ||
786 | /* should be in the library */ | |
787 | else if ((sign_flag == i) && (x509req == 0)) { | |
788 | BIO_printf(bio_err, "Getting Private key\n"); | |
789 | if (Upkey == NULL) { | |
7e1b7485 | 790 | Upkey = load_key(keyfile, keyformat, 0, |
0f113f3e MC |
791 | passin, e, "Private key"); |
792 | if (Upkey == NULL) | |
793 | goto end; | |
794 | } | |
795 | ||
796 | assert(need_rand); | |
4a60bb18 | 797 | if (!sign(x, Upkey, days, clrext, digest, extconf, extsect, preserve_dates)) |
0f113f3e MC |
798 | goto end; |
799 | } else if (CA_flag == i) { | |
800 | BIO_printf(bio_err, "Getting CA Private Key\n"); | |
801 | if (CAkeyfile != NULL) { | |
7e1b7485 | 802 | CApkey = load_key(CAkeyfile, CAkeyformat, |
0f113f3e MC |
803 | 0, passin, e, "CA Private Key"); |
804 | if (CApkey == NULL) | |
805 | goto end; | |
806 | } | |
807 | ||
808 | assert(need_rand); | |
809 | if (!x509_certify(ctx, CAfile, digest, x, xca, | |
810 | CApkey, sigopts, | |
811 | CAserial, CA_createserial, days, clrext, | |
4a60bb18 | 812 | extconf, extsect, sno, reqfile, preserve_dates)) |
0f113f3e MC |
813 | goto end; |
814 | } else if (x509req == i) { | |
815 | EVP_PKEY *pk; | |
816 | ||
817 | BIO_printf(bio_err, "Getting request Private Key\n"); | |
818 | if (keyfile == NULL) { | |
819 | BIO_printf(bio_err, "no request key file specified\n"); | |
820 | goto end; | |
821 | } else { | |
7e1b7485 | 822 | pk = load_key(keyfile, keyformat, 0, |
0f113f3e MC |
823 | passin, e, "request key"); |
824 | if (pk == NULL) | |
825 | goto end; | |
826 | } | |
827 | ||
828 | BIO_printf(bio_err, "Generating certificate request\n"); | |
829 | ||
830 | rq = X509_to_X509_REQ(x, pk, digest); | |
831 | EVP_PKEY_free(pk); | |
832 | if (rq == NULL) { | |
833 | ERR_print_errors(bio_err); | |
834 | goto end; | |
835 | } | |
836 | if (!noout) { | |
b5c4209b | 837 | X509_REQ_print_ex(out, rq, get_nameopt(), X509_FLAG_COMPAT); |
0f113f3e MC |
838 | PEM_write_bio_X509_REQ(out, rq); |
839 | } | |
840 | noout = 1; | |
841 | } else if (ocspid == i) { | |
842 | X509_ocspid_print(out, x); | |
843 | } | |
844 | } | |
845 | } | |
846 | ||
847 | if (checkend) { | |
848 | time_t tcheck = time(NULL) + checkoffset; | |
849 | ||
568ce3a5 | 850 | if (X509_cmp_time(X509_get0_notAfter(x), &tcheck) < 0) { |
0f113f3e MC |
851 | BIO_printf(out, "Certificate will expire\n"); |
852 | ret = 1; | |
853 | } else { | |
854 | BIO_printf(out, "Certificate will not expire\n"); | |
855 | ret = 0; | |
856 | } | |
857 | goto end; | |
858 | } | |
859 | ||
7e1b7485 | 860 | print_cert_checks(out, x, checkhost, checkemail, checkip); |
0f113f3e | 861 | |
7e1b7485 | 862 | if (noout || nocert) { |
0f113f3e MC |
863 | ret = 0; |
864 | goto end; | |
865 | } | |
866 | ||
2234212c | 867 | if (outformat == FORMAT_ASN1) { |
0f113f3e | 868 | i = i2d_X509_bio(out, x); |
2234212c | 869 | } else if (outformat == FORMAT_PEM) { |
0f113f3e MC |
870 | if (trustout) |
871 | i = PEM_write_bio_X509_AUX(out, x); | |
872 | else | |
873 | i = PEM_write_bio_X509(out, x); | |
0f113f3e MC |
874 | } else { |
875 | BIO_printf(bio_err, "bad output format specified for outfile\n"); | |
876 | goto end; | |
877 | } | |
878 | if (!i) { | |
879 | BIO_printf(bio_err, "unable to write certificate\n"); | |
880 | ERR_print_errors(bio_err); | |
881 | goto end; | |
882 | } | |
883 | ret = 0; | |
884 | end: | |
885 | if (need_rand) | |
7e1b7485 | 886 | app_RAND_write_file(NULL); |
0f113f3e MC |
887 | NCONF_free(extconf); |
888 | BIO_free_all(out); | |
0f113f3e MC |
889 | X509_STORE_free(ctx); |
890 | X509_REQ_free(req); | |
891 | X509_free(x); | |
892 | X509_free(xca); | |
893 | EVP_PKEY_free(Upkey); | |
894 | EVP_PKEY_free(CApkey); | |
895 | EVP_PKEY_free(fkey); | |
25aaa98a | 896 | sk_OPENSSL_STRING_free(sigopts); |
0f113f3e MC |
897 | X509_REQ_free(rq); |
898 | ASN1_INTEGER_free(sno); | |
899 | sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free); | |
900 | sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free); | |
f83b85fb | 901 | ASN1_OBJECT_free(objtmp); |
dd1abd44 | 902 | release_engine(e); |
b548a1f1 | 903 | OPENSSL_free(passin); |
eee95522 | 904 | return ret; |
0f113f3e MC |
905 | } |
906 | ||
eee95522 P |
907 | static ASN1_INTEGER *x509_load_serial(const char *CAfile, |
908 | const char *serialfile, int create) | |
0f113f3e | 909 | { |
eee95522 | 910 | char *buf = NULL; |
0f113f3e MC |
911 | ASN1_INTEGER *bs = NULL; |
912 | BIGNUM *serial = NULL; | |
0f113f3e | 913 | |
0f113f3e | 914 | if (serialfile == NULL) { |
eee95522 P |
915 | const char *p = strchr(CAfile, '.'); |
916 | size_t len = p != NULL ? (size_t)(p - CAfile) : strlen(CAfile); | |
917 | ||
918 | buf = app_malloc(len + sizeof(POSTFIX), "serial# buffer"); | |
919 | memcpy(buf, CAfile, len); | |
920 | memcpy(buf + len, POSTFIX, sizeof(POSTFIX)); | |
921 | serialfile = buf; | |
2234212c | 922 | } |
0f113f3e | 923 | |
eee95522 | 924 | serial = load_serial(serialfile, create, NULL); |
0f113f3e MC |
925 | if (serial == NULL) |
926 | goto end; | |
927 | ||
928 | if (!BN_add_word(serial, 1)) { | |
929 | BIO_printf(bio_err, "add_word failure\n"); | |
930 | goto end; | |
931 | } | |
932 | ||
eee95522 | 933 | if (!save_serial(serialfile, NULL, serial, &bs)) |
0f113f3e | 934 | goto end; |
acba75c5 | 935 | |
f85b68cd | 936 | end: |
b548a1f1 | 937 | OPENSSL_free(buf); |
0f113f3e MC |
938 | BN_free(serial); |
939 | return bs; | |
940 | } | |
acba75c5 | 941 | |
cc696296 | 942 | static int x509_certify(X509_STORE *ctx, const char *CAfile, const EVP_MD *digest, |
0f113f3e MC |
943 | X509 *x, X509 *xca, EVP_PKEY *pkey, |
944 | STACK_OF(OPENSSL_STRING) *sigopts, | |
cc696296 F |
945 | const char *serialfile, int create, |
946 | int days, int clrext, CONF *conf, const char *section, | |
4a60bb18 | 947 | ASN1_INTEGER *sno, int reqfile, int preserve_dates) |
0f113f3e MC |
948 | { |
949 | int ret = 0; | |
950 | ASN1_INTEGER *bs = NULL; | |
f0e0fd51 | 951 | X509_STORE_CTX *xsc = NULL; |
0f113f3e MC |
952 | EVP_PKEY *upkey; |
953 | ||
c01ff880 | 954 | upkey = X509_get0_pubkey(xca); |
c223c4a9 MC |
955 | if (upkey == NULL) { |
956 | BIO_printf(bio_err, "Error obtaining CA X509 public key\n"); | |
957 | goto end; | |
958 | } | |
0f113f3e | 959 | EVP_PKEY_copy_parameters(upkey, pkey); |
0f113f3e | 960 | |
f0e0fd51 RS |
961 | xsc = X509_STORE_CTX_new(); |
962 | if (xsc == NULL || !X509_STORE_CTX_init(xsc, ctx, x, NULL)) { | |
0f113f3e MC |
963 | BIO_printf(bio_err, "Error initialising X509 store\n"); |
964 | goto end; | |
965 | } | |
966 | if (sno) | |
967 | bs = sno; | |
75ebbd9a | 968 | else if ((bs = x509_load_serial(CAfile, serialfile, create)) == NULL) |
0f113f3e MC |
969 | goto end; |
970 | ||
0f113f3e MC |
971 | /* |
972 | * NOTE: this certificate can/should be self signed, unless it was a | |
973 | * certificate request in which case it is not. | |
974 | */ | |
f0e0fd51 RS |
975 | X509_STORE_CTX_set_cert(xsc, x); |
976 | X509_STORE_CTX_set_flags(xsc, X509_V_FLAG_CHECK_SS_SIGNATURE); | |
977 | if (!reqfile && X509_verify_cert(xsc) <= 0) | |
0f113f3e MC |
978 | goto end; |
979 | ||
980 | if (!X509_check_private_key(xca, pkey)) { | |
981 | BIO_printf(bio_err, | |
982 | "CA certificate and CA private key do not match\n"); | |
983 | goto end; | |
984 | } | |
985 | ||
986 | if (!X509_set_issuer_name(x, X509_get_subject_name(xca))) | |
987 | goto end; | |
988 | if (!X509_set_serialNumber(x, bs)) | |
989 | goto end; | |
990 | ||
4a60bb18 | 991 | if (!preserve_dates && !set_cert_times(x, NULL, NULL, days)) |
0f113f3e MC |
992 | goto end; |
993 | ||
994 | if (clrext) { | |
995 | while (X509_get_ext_count(x) > 0) | |
996 | X509_delete_ext(x, 0); | |
997 | } | |
998 | ||
2234212c | 999 | if (conf != NULL) { |
0f113f3e | 1000 | X509V3_CTX ctx2; |
0f113f3e | 1001 | X509_set_version(x, 2); /* version 3 certificate */ |
0f113f3e MC |
1002 | X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0); |
1003 | X509V3_set_nconf(&ctx2, conf); | |
1004 | if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x)) | |
1005 | goto end; | |
1006 | } | |
1007 | ||
7e1b7485 | 1008 | if (!do_X509_sign(x, pkey, digest, sigopts)) |
0f113f3e MC |
1009 | goto end; |
1010 | ret = 1; | |
1011 | end: | |
f0e0fd51 | 1012 | X509_STORE_CTX_free(xsc); |
0f113f3e MC |
1013 | if (!ret) |
1014 | ERR_print_errors(bio_err); | |
1015 | if (!sno) | |
1016 | ASN1_INTEGER_free(bs); | |
1017 | return ret; | |
1018 | } | |
d02b48c6 | 1019 | |
6d23cf97 | 1020 | static int callb(int ok, X509_STORE_CTX *ctx) |
0f113f3e MC |
1021 | { |
1022 | int err; | |
1023 | X509 *err_cert; | |
1024 | ||
1025 | /* | |
1026 | * it is ok to use a self signed certificate This case will catch both | |
1027 | * the initial ok == 0 and the final ok == 1 calls to this function | |
1028 | */ | |
1029 | err = X509_STORE_CTX_get_error(ctx); | |
1030 | if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) | |
1031 | return 1; | |
1032 | ||
1033 | /* | |
1034 | * BAD we should have gotten an error. Normally if everything worked | |
1035 | * X509_STORE_CTX_get_error(ctx) will still be set to | |
1036 | * DEPTH_ZERO_SELF_.... | |
1037 | */ | |
1038 | if (ok) { | |
1039 | BIO_printf(bio_err, | |
1040 | "error with certificate to be certified - should be self signed\n"); | |
1041 | return 0; | |
1042 | } else { | |
1043 | err_cert = X509_STORE_CTX_get_current_cert(ctx); | |
1044 | print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0); | |
1045 | BIO_printf(bio_err, | |
1046 | "error with certificate - error %d at depth %d\n%s\n", err, | |
1047 | X509_STORE_CTX_get_error_depth(ctx), | |
1048 | X509_verify_cert_error_string(err)); | |
1049 | return 1; | |
1050 | } | |
1051 | } | |
d02b48c6 | 1052 | |
d02b48c6 | 1053 | /* self sign */ |
0f113f3e | 1054 | static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, |
4a60bb18 TS |
1055 | const EVP_MD *digest, CONF *conf, const char *section, |
1056 | int preserve_dates) | |
0f113f3e MC |
1057 | { |
1058 | ||
0f113f3e MC |
1059 | if (!X509_set_issuer_name(x, X509_get_subject_name(x))) |
1060 | goto err; | |
4a60bb18 | 1061 | if (!preserve_dates && !set_cert_times(x, NULL, NULL, days)) |
0f113f3e | 1062 | goto err; |
0f113f3e MC |
1063 | if (!X509_set_pubkey(x, pkey)) |
1064 | goto err; | |
1065 | if (clrext) { | |
1066 | while (X509_get_ext_count(x) > 0) | |
1067 | X509_delete_ext(x, 0); | |
1068 | } | |
2234212c | 1069 | if (conf != NULL) { |
0f113f3e | 1070 | X509V3_CTX ctx; |
0f113f3e | 1071 | X509_set_version(x, 2); /* version 3 certificate */ |
0f113f3e MC |
1072 | X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0); |
1073 | X509V3_set_nconf(&ctx, conf); | |
1074 | if (!X509V3_EXT_add_nconf(conf, &ctx, section, x)) | |
1075 | goto err; | |
1076 | } | |
1077 | if (!X509_sign(x, pkey, digest)) | |
1078 | goto err; | |
1079 | return 1; | |
1080 | err: | |
1081 | ERR_print_errors(bio_err); | |
1082 | return 0; | |
1083 | } | |
673b102c | 1084 | |
d4cec6a1 | 1085 | static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt) |
673b102c | 1086 | { |
0f113f3e | 1087 | int id, i, idret; |
82643254 | 1088 | const char *pname; |
0f113f3e MC |
1089 | id = X509_PURPOSE_get_id(pt); |
1090 | pname = X509_PURPOSE_get0_name(pt); | |
1091 | for (i = 0; i < 2; i++) { | |
1092 | idret = X509_check_purpose(cert, id, i); | |
1093 | BIO_printf(bio, "%s%s : ", pname, i ? " CA" : ""); | |
1094 | if (idret == 1) | |
1095 | BIO_printf(bio, "Yes\n"); | |
1096 | else if (idret == 0) | |
1097 | BIO_printf(bio, "No\n"); | |
1098 | else | |
1099 | BIO_printf(bio, "Yes (WARNING code=%d)\n", idret); | |
1100 | } | |
1101 | return 1; | |
673b102c | 1102 | } |