]>
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"}, | |
155 | {"checkend", OPT_CHECKEND, 'p', | |
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 RS |
227 | int text = 0, serial = 0, subject = 0, issuer = 0, startdate = 0; |
228 | int checkoffset = 0, enddate = 0; | |
0f113f3e | 229 | unsigned long nmflag = 0, certflag = 0; |
f1cece55 | 230 | char nmflag_set = 0; |
7e1b7485 | 231 | OPTION_CHOICE o; |
7e1b7485 | 232 | ENGINE *e = NULL; |
7e1b7485 RS |
233 | #ifndef OPENSSL_NO_MD5 |
234 | int subject_hash_old = 0, issuer_hash_old = 0; | |
645749ef | 235 | #endif |
d02b48c6 | 236 | |
0f113f3e MC |
237 | ctx = X509_STORE_new(); |
238 | if (ctx == NULL) | |
239 | goto end; | |
240 | X509_STORE_set_verify_cb(ctx, callb); | |
241 | ||
7e1b7485 RS |
242 | prog = opt_init(argc, argv, x509_options); |
243 | while ((o = opt_next()) != OPT_EOF) { | |
244 | switch (o) { | |
245 | case OPT_EOF: | |
246 | case OPT_ERR: | |
247 | opthelp: | |
248 | BIO_printf(bio_err, "%s: Use -help for summary.\n", prog); | |
249 | goto end; | |
250 | case OPT_HELP: | |
251 | opt_help(x509_options); | |
252 | ret = 0; | |
253 | goto end; | |
254 | case OPT_INFORM: | |
255 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &informat)) | |
256 | goto opthelp; | |
257 | break; | |
258 | case OPT_IN: | |
259 | infile = opt_arg(); | |
260 | break; | |
261 | case OPT_OUTFORM: | |
262 | if (!opt_format(opt_arg(), OPT_FMT_ANY, &outformat)) | |
263 | goto opthelp; | |
264 | break; | |
265 | case OPT_KEYFORM: | |
266 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &keyformat)) | |
267 | goto opthelp; | |
268 | break; | |
269 | case OPT_CAFORM: | |
270 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAformat)) | |
271 | goto opthelp; | |
272 | break; | |
273 | case OPT_CAKEYFORM: | |
274 | if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &CAkeyformat)) | |
275 | goto opthelp; | |
276 | break; | |
277 | case OPT_OUT: | |
278 | outfile = opt_arg(); | |
279 | break; | |
280 | case OPT_REQ: | |
281 | reqfile = need_rand = 1; | |
282 | break; | |
283 | ||
284 | case OPT_SIGOPT: | |
0f113f3e MC |
285 | if (!sigopts) |
286 | sigopts = sk_OPENSSL_STRING_new_null(); | |
7e1b7485 RS |
287 | if (!sigopts || !sk_OPENSSL_STRING_push(sigopts, opt_arg())) |
288 | goto opthelp; | |
289 | break; | |
7e1b7485 | 290 | case OPT_FORCE_VERSION: |
9c3bcfa0 | 291 | #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL |
7e1b7485 | 292 | force_version = atoi(opt_arg()) - 1; |
df316fd4 | 293 | #endif |
9c3bcfa0 | 294 | break; |
7e1b7485 RS |
295 | case OPT_DAYS: |
296 | days = atoi(opt_arg()); | |
297 | break; | |
298 | case OPT_PASSIN: | |
299 | passinarg = opt_arg(); | |
300 | break; | |
301 | case OPT_EXTFILE: | |
302 | extfile = opt_arg(); | |
303 | break; | |
304 | case OPT_EXTENSIONS: | |
305 | extsect = opt_arg(); | |
306 | break; | |
307 | case OPT_SIGNKEY: | |
308 | keyfile = opt_arg(); | |
0f113f3e MC |
309 | sign_flag = ++num; |
310 | need_rand = 1; | |
7e1b7485 RS |
311 | break; |
312 | case OPT_CA: | |
313 | CAfile = opt_arg(); | |
0f113f3e MC |
314 | CA_flag = ++num; |
315 | need_rand = 1; | |
7e1b7485 RS |
316 | break; |
317 | case OPT_CAKEY: | |
318 | CAkeyfile = opt_arg(); | |
319 | break; | |
320 | case OPT_CASERIAL: | |
321 | CAserial = opt_arg(); | |
322 | break; | |
323 | case OPT_SET_SERIAL: | |
324 | if ((sno = s2i_ASN1_INTEGER(NULL, opt_arg())) == NULL) | |
325 | goto opthelp; | |
326 | break; | |
327 | case OPT_FORCE_PUBKEY: | |
328 | fkeyfile = opt_arg(); | |
329 | break; | |
330 | case OPT_ADDTRUST: | |
331 | if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) { | |
332 | BIO_printf(bio_err, | |
333 | "%s: Invalid trust object value %s\n", | |
334 | prog, opt_arg()); | |
335 | goto opthelp; | |
0f113f3e | 336 | } |
7e1b7485 RS |
337 | if (trust == NULL && (trust = sk_ASN1_OBJECT_new_null()) == NULL) |
338 | goto end; | |
0f113f3e MC |
339 | sk_ASN1_OBJECT_push(trust, objtmp); |
340 | trustout = 1; | |
7e1b7485 RS |
341 | break; |
342 | case OPT_ADDREJECT: | |
343 | if ((objtmp = OBJ_txt2obj(opt_arg(), 0)) == NULL) { | |
0f113f3e | 344 | BIO_printf(bio_err, |
7e1b7485 RS |
345 | "%s: Invalid reject object value %s\n", |
346 | prog, opt_arg()); | |
347 | goto opthelp; | |
0f113f3e | 348 | } |
7e1b7485 RS |
349 | if (reject == NULL |
350 | && (reject = sk_ASN1_OBJECT_new_null()) == NULL) | |
351 | goto end; | |
0f113f3e MC |
352 | sk_ASN1_OBJECT_push(reject, objtmp); |
353 | trustout = 1; | |
7e1b7485 RS |
354 | break; |
355 | case OPT_SETALIAS: | |
356 | alias = opt_arg(); | |
0f113f3e | 357 | trustout = 1; |
7e1b7485 RS |
358 | break; |
359 | case OPT_CERTOPT: | |
360 | if (!set_cert_ex(&certflag, opt_arg())) | |
361 | goto opthelp; | |
362 | break; | |
363 | case OPT_NAMEOPT: | |
f1cece55 | 364 | nmflag_set = 1; |
7e1b7485 RS |
365 | if (!set_name_ex(&nmflag, opt_arg())) |
366 | goto opthelp; | |
367 | break; | |
368 | case OPT_ENGINE: | |
333b070e | 369 | e = setup_engine(opt_arg(), 0); |
7e1b7485 RS |
370 | break; |
371 | case OPT_C: | |
0f113f3e | 372 | C = ++num; |
7e1b7485 RS |
373 | break; |
374 | case OPT_EMAIL: | |
0f113f3e | 375 | email = ++num; |
7e1b7485 RS |
376 | break; |
377 | case OPT_OCSP_URI: | |
0f113f3e | 378 | ocsp_uri = ++num; |
7e1b7485 RS |
379 | break; |
380 | case OPT_SERIAL: | |
0f113f3e | 381 | serial = ++num; |
7e1b7485 RS |
382 | break; |
383 | case OPT_NEXT_SERIAL: | |
0f113f3e | 384 | next_serial = ++num; |
7e1b7485 RS |
385 | break; |
386 | case OPT_MODULUS: | |
0f113f3e | 387 | modulus = ++num; |
7e1b7485 RS |
388 | break; |
389 | case OPT_PUBKEY: | |
0f113f3e | 390 | pubkey = ++num; |
7e1b7485 RS |
391 | break; |
392 | case OPT_X509TOREQ: | |
0f113f3e | 393 | x509req = ++num; |
7e1b7485 RS |
394 | break; |
395 | case OPT_TEXT: | |
0f113f3e | 396 | text = ++num; |
7e1b7485 RS |
397 | break; |
398 | case OPT_SUBJECT: | |
0f113f3e | 399 | subject = ++num; |
7e1b7485 RS |
400 | break; |
401 | case OPT_ISSUER: | |
0f113f3e | 402 | issuer = ++num; |
7e1b7485 RS |
403 | break; |
404 | case OPT_FINGERPRINT: | |
0f113f3e | 405 | fingerprint = ++num; |
7e1b7485 RS |
406 | break; |
407 | case OPT_HASH: | |
408 | subject_hash = ++num; | |
409 | break; | |
410 | case OPT_ISSUER_HASH: | |
411 | issuer_hash = ++num; | |
412 | break; | |
413 | case OPT_PURPOSE: | |
0f113f3e | 414 | pprint = ++num; |
7e1b7485 RS |
415 | break; |
416 | case OPT_STARTDATE: | |
0f113f3e | 417 | startdate = ++num; |
7e1b7485 RS |
418 | break; |
419 | case OPT_ENDDATE: | |
0f113f3e | 420 | enddate = ++num; |
7e1b7485 RS |
421 | break; |
422 | case OPT_NOOUT: | |
0f113f3e | 423 | noout = ++num; |
7e1b7485 RS |
424 | break; |
425 | case OPT_NOCERT: | |
426 | nocert = 1; | |
427 | break; | |
428 | case OPT_TRUSTOUT: | |
0f113f3e | 429 | trustout = 1; |
7e1b7485 RS |
430 | break; |
431 | case OPT_CLRTRUST: | |
0f113f3e | 432 | clrtrust = ++num; |
7e1b7485 RS |
433 | break; |
434 | case OPT_CLRREJECT: | |
0f113f3e | 435 | clrreject = ++num; |
7e1b7485 RS |
436 | break; |
437 | case OPT_ALIAS: | |
0f113f3e | 438 | aliasout = ++num; |
7e1b7485 RS |
439 | break; |
440 | case OPT_CACREATESERIAL: | |
0f113f3e | 441 | CA_createserial = ++num; |
7e1b7485 RS |
442 | break; |
443 | case OPT_CLREXT: | |
0f113f3e | 444 | clrext = 1; |
7e1b7485 RS |
445 | break; |
446 | case OPT_OCSPID: | |
0f113f3e | 447 | ocspid = ++num; |
7e1b7485 RS |
448 | break; |
449 | case OPT_BADSIG: | |
0f113f3e | 450 | badsig = 1; |
0f113f3e | 451 | break; |
7e1b7485 RS |
452 | #ifndef OPENSSL_NO_MD5 |
453 | case OPT_SUBJECT_HASH_OLD: | |
454 | subject_hash_old = ++num; | |
455 | break; | |
456 | case OPT_ISSUER_HASH_OLD: | |
457 | issuer_hash_old = ++num; | |
458 | break; | |
9c3bcfa0 RS |
459 | #else |
460 | case OPT_SUBJECT_HASH_OLD: | |
461 | case OPT_ISSUER_HASH_OLD: | |
462 | break; | |
7e1b7485 RS |
463 | #endif |
464 | case OPT_DATES: | |
465 | startdate = ++num; | |
466 | enddate = ++num; | |
467 | break; | |
468 | case OPT_CHECKEND: | |
469 | checkoffset = atoi(opt_arg()); | |
470 | checkend = 1; | |
471 | break; | |
472 | case OPT_CHECKHOST: | |
473 | checkhost = opt_arg(); | |
474 | break; | |
475 | case OPT_CHECKEMAIL: | |
476 | checkemail = opt_arg(); | |
477 | break; | |
478 | case OPT_CHECKIP: | |
479 | checkip = opt_arg(); | |
480 | break; | |
481 | case OPT_MD: | |
482 | if (!opt_md(opt_unknown(), &digest)) | |
483 | goto opthelp; | |
0f113f3e | 484 | } |
7e1b7485 RS |
485 | } |
486 | argc = opt_num_rest(); | |
487 | argv = opt_rest(); | |
488 | if (argc != 0) { | |
489 | BIO_printf(bio_err, "%s: Unknown parameter %s\n", prog, argv[0]); | |
490 | goto opthelp; | |
0f113f3e MC |
491 | } |
492 | ||
f1cece55 RL |
493 | if (!nmflag_set) |
494 | nmflag = XN_FLAG_ONELINE; | |
495 | ||
bdd58d98 | 496 | out = bio_open_default(outfile, 'w', outformat); |
7e1b7485 | 497 | if (out == NULL) |
0f113f3e | 498 | goto end; |
7e1b7485 | 499 | |
0f113f3e | 500 | if (need_rand) |
7e1b7485 | 501 | app_RAND_load_file(NULL, 0); |
0f113f3e | 502 | |
7e1b7485 | 503 | if (!app_passwd(passinarg, NULL, &passin, NULL)) { |
0f113f3e MC |
504 | BIO_printf(bio_err, "Error getting password\n"); |
505 | goto end; | |
506 | } | |
507 | ||
508 | if (!X509_STORE_set_default_paths(ctx)) { | |
509 | ERR_print_errors(bio_err); | |
510 | goto end; | |
511 | } | |
512 | ||
513 | if (fkeyfile) { | |
7e1b7485 | 514 | fkey = load_pubkey(fkeyfile, keyformat, 0, NULL, e, "Forced key"); |
0f113f3e MC |
515 | if (fkey == NULL) |
516 | goto end; | |
517 | } | |
518 | ||
519 | if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM)) { | |
520 | CAkeyfile = CAfile; | |
521 | } else if ((CA_flag) && (CAkeyfile == NULL)) { | |
522 | BIO_printf(bio_err, | |
523 | "need to specify a CAkey if using the CA command\n"); | |
524 | goto end; | |
525 | } | |
526 | ||
527 | if (extfile) { | |
0f113f3e | 528 | X509V3_CTX ctx2; |
cc01d217 | 529 | if ((extconf = app_load_config(extfile)) == NULL) |
0f113f3e | 530 | goto end; |
0f113f3e MC |
531 | if (!extsect) { |
532 | extsect = NCONF_get_string(extconf, "default", "extensions"); | |
533 | if (!extsect) { | |
534 | ERR_clear_error(); | |
535 | extsect = "default"; | |
536 | } | |
537 | } | |
538 | X509V3_set_ctx_test(&ctx2); | |
539 | X509V3_set_nconf(&ctx2, extconf); | |
540 | if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL)) { | |
541 | BIO_printf(bio_err, | |
542 | "Error Loading extension section %s\n", extsect); | |
543 | ERR_print_errors(bio_err); | |
544 | goto end; | |
545 | } | |
546 | } | |
547 | ||
548 | if (reqfile) { | |
549 | EVP_PKEY *pkey; | |
550 | BIO *in; | |
551 | ||
552 | if (!sign_flag && !CA_flag) { | |
553 | BIO_printf(bio_err, "We need a private key to sign with\n"); | |
554 | goto end; | |
555 | } | |
bdd58d98 | 556 | in = bio_open_default(infile, 'r', informat); |
7e1b7485 | 557 | if (in == NULL) |
0f113f3e | 558 | goto end; |
0f113f3e MC |
559 | req = PEM_read_bio_X509_REQ(in, NULL, NULL, NULL); |
560 | BIO_free(in); | |
561 | ||
562 | if (req == NULL) { | |
563 | ERR_print_errors(bio_err); | |
564 | goto end; | |
565 | } | |
566 | ||
0f113f3e MC |
567 | if ((pkey = X509_REQ_get_pubkey(req)) == NULL) { |
568 | BIO_printf(bio_err, "error unpacking public key\n"); | |
569 | goto end; | |
570 | } | |
571 | i = X509_REQ_verify(req, pkey); | |
572 | EVP_PKEY_free(pkey); | |
573 | if (i < 0) { | |
574 | BIO_printf(bio_err, "Signature verification error\n"); | |
575 | ERR_print_errors(bio_err); | |
576 | goto end; | |
577 | } | |
578 | if (i == 0) { | |
579 | BIO_printf(bio_err, | |
580 | "Signature did not match the certificate request\n"); | |
581 | goto end; | |
582 | } else | |
583 | BIO_printf(bio_err, "Signature ok\n"); | |
584 | ||
585 | print_name(bio_err, "subject=", X509_REQ_get_subject_name(req), | |
586 | nmflag); | |
587 | ||
588 | if ((x = X509_new()) == NULL) | |
589 | goto end; | |
590 | ||
591 | if (sno == NULL) { | |
592 | sno = ASN1_INTEGER_new(); | |
593 | if (!sno || !rand_serial(NULL, sno)) | |
594 | goto end; | |
595 | if (!X509_set_serialNumber(x, sno)) | |
596 | goto end; | |
597 | ASN1_INTEGER_free(sno); | |
598 | sno = NULL; | |
599 | } else if (!X509_set_serialNumber(x, sno)) | |
600 | goto end; | |
601 | ||
124055a9 | 602 | if (!X509_set_issuer_name(x, X509_REQ_get_subject_name(req))) |
0f113f3e | 603 | goto end; |
124055a9 | 604 | if (!X509_set_subject_name(x, X509_REQ_get_subject_name(req))) |
0f113f3e MC |
605 | goto end; |
606 | ||
607 | X509_gmtime_adj(X509_get_notBefore(x), 0); | |
608 | X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL); | |
609 | if (fkey) | |
610 | X509_set_pubkey(x, fkey); | |
611 | else { | |
612 | pkey = X509_REQ_get_pubkey(req); | |
613 | X509_set_pubkey(x, pkey); | |
614 | EVP_PKEY_free(pkey); | |
615 | } | |
616 | } else | |
7e1b7485 | 617 | x = load_cert(infile, informat, NULL, e, "Certificate"); |
0f113f3e MC |
618 | |
619 | if (x == NULL) | |
620 | goto end; | |
621 | if (CA_flag) { | |
7e1b7485 | 622 | xca = load_cert(CAfile, CAformat, NULL, e, "CA Certificate"); |
0f113f3e MC |
623 | if (xca == NULL) |
624 | goto end; | |
625 | } | |
626 | ||
627 | if (!noout || text || next_serial) { | |
628 | OBJ_create("2.99999.3", "SET.ex3", "SET x509v3 extension 3"); | |
629 | ||
0f113f3e MC |
630 | } |
631 | ||
632 | if (alias) | |
633 | X509_alias_set1(x, (unsigned char *)alias, -1); | |
634 | ||
635 | if (clrtrust) | |
636 | X509_trust_clear(x); | |
637 | if (clrreject) | |
638 | X509_reject_clear(x); | |
639 | ||
640 | if (trust) { | |
641 | for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) { | |
642 | objtmp = sk_ASN1_OBJECT_value(trust, i); | |
643 | X509_add1_trust_object(x, objtmp); | |
644 | } | |
645 | } | |
646 | ||
647 | if (reject) { | |
648 | for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) { | |
649 | objtmp = sk_ASN1_OBJECT_value(reject, i); | |
650 | X509_add1_reject_object(x, objtmp); | |
651 | } | |
652 | } | |
653 | ||
654 | if (num) { | |
655 | for (i = 1; i <= num; i++) { | |
656 | if (issuer == i) { | |
7e1b7485 | 657 | print_name(out, "issuer= ", X509_get_issuer_name(x), nmflag); |
0f113f3e | 658 | } else if (subject == i) { |
7e1b7485 | 659 | print_name(out, "subject= ", |
0f113f3e MC |
660 | X509_get_subject_name(x), nmflag); |
661 | } else if (serial == i) { | |
7e1b7485 RS |
662 | BIO_printf(out, "serial="); |
663 | i2a_ASN1_INTEGER(out, X509_get_serialNumber(x)); | |
664 | BIO_printf(out, "\n"); | |
0f113f3e MC |
665 | } else if (next_serial == i) { |
666 | BIGNUM *bnser; | |
667 | ASN1_INTEGER *ser; | |
668 | ser = X509_get_serialNumber(x); | |
669 | bnser = ASN1_INTEGER_to_BN(ser, NULL); | |
670 | if (!bnser) | |
671 | goto end; | |
672 | if (!BN_add_word(bnser, 1)) | |
673 | goto end; | |
674 | ser = BN_to_ASN1_INTEGER(bnser, NULL); | |
675 | if (!ser) | |
676 | goto end; | |
677 | BN_free(bnser); | |
678 | i2a_ASN1_INTEGER(out, ser); | |
679 | ASN1_INTEGER_free(ser); | |
680 | BIO_puts(out, "\n"); | |
681 | } else if ((email == i) || (ocsp_uri == i)) { | |
682 | int j; | |
683 | STACK_OF(OPENSSL_STRING) *emlst; | |
684 | if (email == i) | |
685 | emlst = X509_get1_email(x); | |
686 | else | |
687 | emlst = X509_get1_ocsp(x); | |
688 | for (j = 0; j < sk_OPENSSL_STRING_num(emlst); j++) | |
7e1b7485 | 689 | BIO_printf(out, "%s\n", |
0f113f3e MC |
690 | sk_OPENSSL_STRING_value(emlst, j)); |
691 | X509_email_free(emlst); | |
692 | } else if (aliasout == i) { | |
693 | unsigned char *alstr; | |
694 | alstr = X509_alias_get0(x, NULL); | |
695 | if (alstr) | |
7e1b7485 | 696 | BIO_printf(out, "%s\n", alstr); |
0f113f3e | 697 | else |
7e1b7485 | 698 | BIO_puts(out, "<No Alias>\n"); |
0f113f3e | 699 | } else if (subject_hash == i) { |
7e1b7485 | 700 | BIO_printf(out, "%08lx\n", X509_subject_name_hash(x)); |
0f113f3e | 701 | } |
0e0c6821 | 702 | #ifndef OPENSSL_NO_MD5 |
0f113f3e | 703 | else if (subject_hash_old == i) { |
7e1b7485 | 704 | BIO_printf(out, "%08lx\n", X509_subject_name_hash_old(x)); |
0f113f3e | 705 | } |
0e0c6821 | 706 | #endif |
0f113f3e | 707 | else if (issuer_hash == i) { |
7e1b7485 | 708 | BIO_printf(out, "%08lx\n", X509_issuer_name_hash(x)); |
0f113f3e | 709 | } |
0e0c6821 | 710 | #ifndef OPENSSL_NO_MD5 |
0f113f3e | 711 | else if (issuer_hash_old == i) { |
7e1b7485 | 712 | BIO_printf(out, "%08lx\n", X509_issuer_name_hash_old(x)); |
0f113f3e | 713 | } |
0e0c6821 | 714 | #endif |
0f113f3e MC |
715 | else if (pprint == i) { |
716 | X509_PURPOSE *ptmp; | |
717 | int j; | |
7e1b7485 | 718 | BIO_printf(out, "Certificate purposes:\n"); |
0f113f3e MC |
719 | for (j = 0; j < X509_PURPOSE_get_count(); j++) { |
720 | ptmp = X509_PURPOSE_get0(j); | |
7e1b7485 | 721 | purpose_print(out, x, ptmp); |
0f113f3e MC |
722 | } |
723 | } else if (modulus == i) { | |
724 | EVP_PKEY *pkey; | |
725 | ||
726 | pkey = X509_get_pubkey(x); | |
727 | if (pkey == NULL) { | |
728 | BIO_printf(bio_err, "Modulus=unavailable\n"); | |
729 | ERR_print_errors(bio_err); | |
730 | goto end; | |
731 | } | |
7e1b7485 | 732 | BIO_printf(out, "Modulus="); |
cf1b7d96 | 733 | #ifndef OPENSSL_NO_RSA |
0f113f3e | 734 | if (pkey->type == EVP_PKEY_RSA) |
7e1b7485 | 735 | BN_print(out, pkey->pkey.rsa->n); |
0f113f3e | 736 | else |
7be304ac | 737 | #endif |
cf1b7d96 | 738 | #ifndef OPENSSL_NO_DSA |
0f113f3e | 739 | if (pkey->type == EVP_PKEY_DSA) |
7e1b7485 | 740 | BN_print(out, pkey->pkey.dsa->pub_key); |
0f113f3e | 741 | else |
7be304ac | 742 | #endif |
7e1b7485 RS |
743 | BIO_printf(out, "Wrong Algorithm type"); |
744 | BIO_printf(out, "\n"); | |
0f113f3e MC |
745 | EVP_PKEY_free(pkey); |
746 | } else if (pubkey == i) { | |
747 | EVP_PKEY *pkey; | |
748 | ||
749 | pkey = X509_get_pubkey(x); | |
750 | if (pkey == NULL) { | |
751 | BIO_printf(bio_err, "Error getting public key\n"); | |
752 | ERR_print_errors(bio_err); | |
753 | goto end; | |
754 | } | |
7e1b7485 | 755 | PEM_write_bio_PUBKEY(out, pkey); |
0f113f3e MC |
756 | EVP_PKEY_free(pkey); |
757 | } else if (C == i) { | |
758 | unsigned char *d; | |
759 | char *m; | |
7e1b7485 | 760 | int len; |
0f113f3e MC |
761 | |
762 | X509_NAME_oneline(X509_get_subject_name(x), buf, sizeof buf); | |
7e1b7485 RS |
763 | BIO_printf(out, "/*\n" |
764 | " * Subject: %s\n", buf); | |
765 | ||
766 | m = X509_NAME_oneline(X509_get_issuer_name(x), buf, sizeof buf); | |
767 | BIO_printf(out, " * Issuer: %s\n" | |
768 | " */\n", buf); | |
0f113f3e | 769 | |
7e1b7485 | 770 | len = i2d_X509(x, NULL); |
68dc6824 | 771 | m = app_malloc(len, "x509 name buffer"); |
0f113f3e | 772 | d = (unsigned char *)m; |
7e1b7485 RS |
773 | len = i2d_X509_NAME(X509_get_subject_name(x), &d); |
774 | print_array(out, "the_subject_name", len, (unsigned char *)m); | |
0f113f3e | 775 | d = (unsigned char *)m; |
7e1b7485 RS |
776 | len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x), &d); |
777 | print_array(out, "the_public_key", len, (unsigned char *)m); | |
0f113f3e | 778 | d = (unsigned char *)m; |
7e1b7485 RS |
779 | len = i2d_X509(x, &d); |
780 | print_array(out, "the_certificate", len, (unsigned char *)m); | |
0f113f3e MC |
781 | OPENSSL_free(m); |
782 | } else if (text == i) { | |
7e1b7485 | 783 | X509_print_ex(out, x, nmflag, certflag); |
0f113f3e | 784 | } else if (startdate == i) { |
7e1b7485 RS |
785 | BIO_puts(out, "notBefore="); |
786 | ASN1_TIME_print(out, X509_get_notBefore(x)); | |
787 | BIO_puts(out, "\n"); | |
0f113f3e | 788 | } else if (enddate == i) { |
7e1b7485 RS |
789 | BIO_puts(out, "notAfter="); |
790 | ASN1_TIME_print(out, X509_get_notAfter(x)); | |
791 | BIO_puts(out, "\n"); | |
0f113f3e MC |
792 | } else if (fingerprint == i) { |
793 | int j; | |
794 | unsigned int n; | |
795 | unsigned char md[EVP_MAX_MD_SIZE]; | |
796 | const EVP_MD *fdig = digest; | |
797 | ||
798 | if (!fdig) | |
799 | fdig = EVP_sha1(); | |
800 | ||
801 | if (!X509_digest(x, fdig, md, &n)) { | |
802 | BIO_printf(bio_err, "out of memory\n"); | |
803 | goto end; | |
804 | } | |
7e1b7485 | 805 | BIO_printf(out, "%s Fingerprint=", |
0f113f3e MC |
806 | OBJ_nid2sn(EVP_MD_type(fdig))); |
807 | for (j = 0; j < (int)n; j++) { | |
7e1b7485 | 808 | BIO_printf(out, "%02X%c", md[j], (j + 1 == (int)n) |
0f113f3e MC |
809 | ? '\n' : ':'); |
810 | } | |
811 | } | |
812 | ||
813 | /* should be in the library */ | |
814 | else if ((sign_flag == i) && (x509req == 0)) { | |
815 | BIO_printf(bio_err, "Getting Private key\n"); | |
816 | if (Upkey == NULL) { | |
7e1b7485 | 817 | Upkey = load_key(keyfile, keyformat, 0, |
0f113f3e MC |
818 | passin, e, "Private key"); |
819 | if (Upkey == NULL) | |
820 | goto end; | |
821 | } | |
822 | ||
823 | assert(need_rand); | |
824 | if (!sign(x, Upkey, days, clrext, digest, extconf, extsect)) | |
825 | goto end; | |
826 | } else if (CA_flag == i) { | |
827 | BIO_printf(bio_err, "Getting CA Private Key\n"); | |
828 | if (CAkeyfile != NULL) { | |
7e1b7485 | 829 | CApkey = load_key(CAkeyfile, CAkeyformat, |
0f113f3e MC |
830 | 0, passin, e, "CA Private Key"); |
831 | if (CApkey == NULL) | |
832 | goto end; | |
833 | } | |
834 | ||
835 | assert(need_rand); | |
836 | if (!x509_certify(ctx, CAfile, digest, x, xca, | |
837 | CApkey, sigopts, | |
838 | CAserial, CA_createserial, days, clrext, | |
7e1b7485 | 839 | extconf, extsect, sno, reqfile)) |
0f113f3e MC |
840 | goto end; |
841 | } else if (x509req == i) { | |
842 | EVP_PKEY *pk; | |
843 | ||
844 | BIO_printf(bio_err, "Getting request Private Key\n"); | |
845 | if (keyfile == NULL) { | |
846 | BIO_printf(bio_err, "no request key file specified\n"); | |
847 | goto end; | |
848 | } else { | |
7e1b7485 | 849 | pk = load_key(keyfile, keyformat, 0, |
0f113f3e MC |
850 | passin, e, "request key"); |
851 | if (pk == NULL) | |
852 | goto end; | |
853 | } | |
854 | ||
855 | BIO_printf(bio_err, "Generating certificate request\n"); | |
856 | ||
857 | rq = X509_to_X509_REQ(x, pk, digest); | |
858 | EVP_PKEY_free(pk); | |
859 | if (rq == NULL) { | |
860 | ERR_print_errors(bio_err); | |
861 | goto end; | |
862 | } | |
863 | if (!noout) { | |
864 | X509_REQ_print(out, rq); | |
865 | PEM_write_bio_X509_REQ(out, rq); | |
866 | } | |
867 | noout = 1; | |
868 | } else if (ocspid == i) { | |
869 | X509_ocspid_print(out, x); | |
870 | } | |
871 | } | |
872 | } | |
873 | ||
874 | if (checkend) { | |
875 | time_t tcheck = time(NULL) + checkoffset; | |
876 | ||
877 | if (X509_cmp_time(X509_get_notAfter(x), &tcheck) < 0) { | |
878 | BIO_printf(out, "Certificate will expire\n"); | |
879 | ret = 1; | |
880 | } else { | |
881 | BIO_printf(out, "Certificate will not expire\n"); | |
882 | ret = 0; | |
883 | } | |
884 | goto end; | |
885 | } | |
886 | ||
7e1b7485 | 887 | print_cert_checks(out, x, checkhost, checkemail, checkip); |
0f113f3e | 888 | |
7e1b7485 | 889 | if (noout || nocert) { |
0f113f3e MC |
890 | ret = 0; |
891 | goto end; | |
892 | } | |
893 | ||
a8d8e06b DSH |
894 | if (badsig) { |
895 | ASN1_BIT_STRING *signature; | |
896 | unsigned char *s; | |
897 | X509_get0_signature(&signature, NULL, x); | |
898 | s = ASN1_STRING_data(signature); | |
899 | s[ASN1_STRING_length(signature) - 1] ^= 0x1; | |
900 | } | |
0f113f3e MC |
901 | |
902 | if (outformat == FORMAT_ASN1) | |
903 | i = i2d_X509_bio(out, x); | |
904 | else if (outformat == FORMAT_PEM) { | |
905 | if (trustout) | |
906 | i = PEM_write_bio_X509_AUX(out, x); | |
907 | else | |
908 | i = PEM_write_bio_X509(out, x); | |
0f113f3e MC |
909 | } else { |
910 | BIO_printf(bio_err, "bad output format specified for outfile\n"); | |
911 | goto end; | |
912 | } | |
913 | if (!i) { | |
914 | BIO_printf(bio_err, "unable to write certificate\n"); | |
915 | ERR_print_errors(bio_err); | |
916 | goto end; | |
917 | } | |
918 | ret = 0; | |
919 | end: | |
920 | if (need_rand) | |
7e1b7485 | 921 | app_RAND_write_file(NULL); |
0f113f3e MC |
922 | OBJ_cleanup(); |
923 | NCONF_free(extconf); | |
924 | BIO_free_all(out); | |
0f113f3e MC |
925 | X509_STORE_free(ctx); |
926 | X509_REQ_free(req); | |
927 | X509_free(x); | |
928 | X509_free(xca); | |
929 | EVP_PKEY_free(Upkey); | |
930 | EVP_PKEY_free(CApkey); | |
931 | EVP_PKEY_free(fkey); | |
25aaa98a | 932 | sk_OPENSSL_STRING_free(sigopts); |
0f113f3e MC |
933 | X509_REQ_free(rq); |
934 | ASN1_INTEGER_free(sno); | |
935 | sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free); | |
936 | sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free); | |
b548a1f1 | 937 | OPENSSL_free(passin); |
7e1b7485 | 938 | return (ret); |
0f113f3e MC |
939 | } |
940 | ||
941 | static ASN1_INTEGER *x509_load_serial(char *CAfile, char *serialfile, | |
942 | int create) | |
943 | { | |
944 | char *buf = NULL, *p; | |
945 | ASN1_INTEGER *bs = NULL; | |
946 | BIGNUM *serial = NULL; | |
947 | size_t len; | |
948 | ||
949 | len = ((serialfile == NULL) | |
950 | ? (strlen(CAfile) + strlen(POSTFIX) + 1) | |
951 | : (strlen(serialfile))) + 1; | |
68dc6824 | 952 | buf = app_malloc(len, "serial# buffer"); |
0f113f3e MC |
953 | if (serialfile == NULL) { |
954 | BUF_strlcpy(buf, CAfile, len); | |
955 | for (p = buf; *p; p++) | |
956 | if (*p == '.') { | |
957 | *p = '\0'; | |
958 | break; | |
959 | } | |
960 | BUF_strlcat(buf, POSTFIX, len); | |
961 | } else | |
962 | BUF_strlcpy(buf, serialfile, len); | |
963 | ||
964 | serial = load_serial(buf, create, NULL); | |
965 | if (serial == NULL) | |
966 | goto end; | |
967 | ||
968 | if (!BN_add_word(serial, 1)) { | |
969 | BIO_printf(bio_err, "add_word failure\n"); | |
970 | goto end; | |
971 | } | |
972 | ||
973 | if (!save_serial(buf, NULL, serial, &bs)) | |
974 | goto end; | |
acba75c5 | 975 | |
f85b68cd | 976 | end: |
b548a1f1 | 977 | OPENSSL_free(buf); |
0f113f3e MC |
978 | BN_free(serial); |
979 | return bs; | |
980 | } | |
acba75c5 DSH |
981 | |
982 | static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest, | |
0f113f3e MC |
983 | X509 *x, X509 *xca, EVP_PKEY *pkey, |
984 | STACK_OF(OPENSSL_STRING) *sigopts, | |
985 | char *serialfile, int create, | |
986 | int days, int clrext, CONF *conf, char *section, | |
7e1b7485 | 987 | ASN1_INTEGER *sno, int reqfile) |
0f113f3e MC |
988 | { |
989 | int ret = 0; | |
990 | ASN1_INTEGER *bs = NULL; | |
991 | X509_STORE_CTX xsc; | |
992 | EVP_PKEY *upkey; | |
993 | ||
994 | upkey = X509_get_pubkey(xca); | |
995 | EVP_PKEY_copy_parameters(upkey, pkey); | |
996 | EVP_PKEY_free(upkey); | |
997 | ||
998 | if (!X509_STORE_CTX_init(&xsc, ctx, x, NULL)) { | |
999 | BIO_printf(bio_err, "Error initialising X509 store\n"); | |
1000 | goto end; | |
1001 | } | |
1002 | if (sno) | |
1003 | bs = sno; | |
75ebbd9a | 1004 | else if ((bs = x509_load_serial(CAfile, serialfile, create)) == NULL) |
0f113f3e MC |
1005 | goto end; |
1006 | ||
0f113f3e MC |
1007 | /* |
1008 | * NOTE: this certificate can/should be self signed, unless it was a | |
1009 | * certificate request in which case it is not. | |
1010 | */ | |
1011 | X509_STORE_CTX_set_cert(&xsc, x); | |
1012 | X509_STORE_CTX_set_flags(&xsc, X509_V_FLAG_CHECK_SS_SIGNATURE); | |
1013 | if (!reqfile && X509_verify_cert(&xsc) <= 0) | |
1014 | goto end; | |
1015 | ||
1016 | if (!X509_check_private_key(xca, pkey)) { | |
1017 | BIO_printf(bio_err, | |
1018 | "CA certificate and CA private key do not match\n"); | |
1019 | goto end; | |
1020 | } | |
1021 | ||
1022 | if (!X509_set_issuer_name(x, X509_get_subject_name(xca))) | |
1023 | goto end; | |
1024 | if (!X509_set_serialNumber(x, bs)) | |
1025 | goto end; | |
1026 | ||
1027 | if (X509_gmtime_adj(X509_get_notBefore(x), 0L) == NULL) | |
1028 | goto end; | |
1029 | ||
1030 | /* hardwired expired */ | |
1031 | if (X509_time_adj_ex(X509_get_notAfter(x), days, 0, NULL) == NULL) | |
1032 | goto end; | |
1033 | ||
1034 | if (clrext) { | |
1035 | while (X509_get_ext_count(x) > 0) | |
1036 | X509_delete_ext(x, 0); | |
1037 | } | |
1038 | ||
1039 | if (conf) { | |
1040 | X509V3_CTX ctx2; | |
df316fd4 | 1041 | #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL |
0f113f3e | 1042 | X509_set_version(x, force_version); |
df316fd4 | 1043 | #else |
0f113f3e | 1044 | X509_set_version(x, 2); /* version 3 certificate */ |
df316fd4 | 1045 | #endif |
0f113f3e MC |
1046 | X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0); |
1047 | X509V3_set_nconf(&ctx2, conf); | |
1048 | if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x)) | |
1049 | goto end; | |
1050 | } | |
1051 | ||
7e1b7485 | 1052 | if (!do_X509_sign(x, pkey, digest, sigopts)) |
0f113f3e MC |
1053 | goto end; |
1054 | ret = 1; | |
1055 | end: | |
1056 | X509_STORE_CTX_cleanup(&xsc); | |
1057 | if (!ret) | |
1058 | ERR_print_errors(bio_err); | |
1059 | if (!sno) | |
1060 | ASN1_INTEGER_free(bs); | |
1061 | return ret; | |
1062 | } | |
d02b48c6 | 1063 | |
6d23cf97 | 1064 | static int callb(int ok, X509_STORE_CTX *ctx) |
0f113f3e MC |
1065 | { |
1066 | int err; | |
1067 | X509 *err_cert; | |
1068 | ||
1069 | /* | |
1070 | * it is ok to use a self signed certificate This case will catch both | |
1071 | * the initial ok == 0 and the final ok == 1 calls to this function | |
1072 | */ | |
1073 | err = X509_STORE_CTX_get_error(ctx); | |
1074 | if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT) | |
1075 | return 1; | |
1076 | ||
1077 | /* | |
1078 | * BAD we should have gotten an error. Normally if everything worked | |
1079 | * X509_STORE_CTX_get_error(ctx) will still be set to | |
1080 | * DEPTH_ZERO_SELF_.... | |
1081 | */ | |
1082 | if (ok) { | |
1083 | BIO_printf(bio_err, | |
1084 | "error with certificate to be certified - should be self signed\n"); | |
1085 | return 0; | |
1086 | } else { | |
1087 | err_cert = X509_STORE_CTX_get_current_cert(ctx); | |
1088 | print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0); | |
1089 | BIO_printf(bio_err, | |
1090 | "error with certificate - error %d at depth %d\n%s\n", err, | |
1091 | X509_STORE_CTX_get_error_depth(ctx), | |
1092 | X509_verify_cert_error_string(err)); | |
1093 | return 1; | |
1094 | } | |
1095 | } | |
d02b48c6 | 1096 | |
d02b48c6 | 1097 | /* self sign */ |
0f113f3e MC |
1098 | static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, |
1099 | const EVP_MD *digest, CONF *conf, char *section) | |
1100 | { | |
1101 | ||
0f113f3e MC |
1102 | if (!X509_set_issuer_name(x, X509_get_subject_name(x))) |
1103 | goto err; | |
1104 | if (X509_gmtime_adj(X509_get_notBefore(x), 0) == NULL) | |
1105 | goto err; | |
1106 | ||
0f113f3e MC |
1107 | if (X509_gmtime_adj(X509_get_notAfter(x), (long)60 * 60 * 24 * days) == |
1108 | NULL) | |
1109 | goto err; | |
1110 | ||
1111 | if (!X509_set_pubkey(x, pkey)) | |
1112 | goto err; | |
1113 | if (clrext) { | |
1114 | while (X509_get_ext_count(x) > 0) | |
1115 | X509_delete_ext(x, 0); | |
1116 | } | |
1117 | if (conf) { | |
1118 | X509V3_CTX ctx; | |
df316fd4 | 1119 | #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL |
0f113f3e | 1120 | X509_set_version(x, force_version); |
df316fd4 | 1121 | #else |
0f113f3e | 1122 | X509_set_version(x, 2); /* version 3 certificate */ |
df316fd4 | 1123 | #endif |
0f113f3e MC |
1124 | X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0); |
1125 | X509V3_set_nconf(&ctx, conf); | |
1126 | if (!X509V3_EXT_add_nconf(conf, &ctx, section, x)) | |
1127 | goto err; | |
1128 | } | |
1129 | if (!X509_sign(x, pkey, digest)) | |
1130 | goto err; | |
1131 | return 1; | |
1132 | err: | |
1133 | ERR_print_errors(bio_err); | |
1134 | return 0; | |
1135 | } | |
673b102c | 1136 | |
d4cec6a1 | 1137 | static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt) |
673b102c | 1138 | { |
0f113f3e MC |
1139 | int id, i, idret; |
1140 | char *pname; | |
1141 | id = X509_PURPOSE_get_id(pt); | |
1142 | pname = X509_PURPOSE_get0_name(pt); | |
1143 | for (i = 0; i < 2; i++) { | |
1144 | idret = X509_check_purpose(cert, id, i); | |
1145 | BIO_printf(bio, "%s%s : ", pname, i ? " CA" : ""); | |
1146 | if (idret == 1) | |
1147 | BIO_printf(bio, "Yes\n"); | |
1148 | else if (idret == 0) | |
1149 | BIO_printf(bio, "No\n"); | |
1150 | else | |
1151 | BIO_printf(bio, "Yes (WARNING code=%d)\n", idret); | |
1152 | } | |
1153 | return 1; | |
673b102c | 1154 | } |