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