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