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