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