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