63 #ifdef OPENSSL_NO_STDIO
76 #ifndef OPENSSL_NO_RSA
79 #ifndef OPENSSL_NO_DSA
84 #define PROG x509_main
87 #define POSTFIX ".srl"
90 static const char *x509_usage[]={
92 " -inform arg - input format - default PEM (one of DER, NET or PEM)\n",
93 " -outform arg - output format - default PEM (one of DER, NET or PEM)\n",
94 " -keyform arg - private key format - default PEM\n",
95 " -CAform arg - CA format - default PEM\n",
96 " -CAkeyform arg - CA key format - default PEM\n",
97 " -in arg - input file - default stdin\n",
98 " -out arg - output file - default stdout\n",
99 " -passin arg - private key password source\n",
100 " -serial - print serial number value\n",
101 " -subject_hash - print subject hash value\n",
102 #ifndef OPENSSL_NO_MD5
103 " -subject_hash_old - print old-style (MD5) subject hash value\n",
105 " -issuer_hash - print issuer hash value\n",
106 #ifndef OPENSSL_NO_MD5
107 " -issuer_hash_old - print old-style (MD5) issuer hash value\n",
109 " -hash - synonym for -subject_hash\n",
110 " -subject - print subject DN\n",
111 " -issuer - print issuer DN\n",
112 " -email - print email address(es)\n",
113 " -startdate - notBefore field\n",
114 " -enddate - notAfter field\n",
115 " -purpose - print out certificate purposes\n",
116 " -dates - both Before and After dates\n",
117 " -modulus - print the RSA key modulus\n",
118 " -pubkey - output the public key\n",
119 " -fingerprint - print the certificate fingerprint\n",
120 " -alias - output certificate alias\n",
121 " -noout - no certificate output\n",
122 " -ocspid - print OCSP hash values for the subject name and public key\n",
123 " -ocsp_uri - print OCSP Responder URL(s)\n",
124 " -trustout - output a \"trusted\" certificate\n",
125 " -clrtrust - clear all trusted purposes\n",
126 " -clrreject - clear all rejected purposes\n",
127 " -addtrust arg - trust certificate for a given purpose\n",
128 " -addreject arg - reject certificate for a given purpose\n",
129 " -setalias arg - set certificate alias\n",
130 " -days arg - How long till expiry of a signed certificate - def 30 days\n",
131 " -checkend arg - check whether the cert expires in the next arg seconds\n",
132 " exit 1 if so, 0 if not\n",
133 " -signkey arg - self sign cert with arg\n",
134 " -x509toreq - output a certification request object\n",
135 " -req - input is a certificate request, sign and output.\n",
136 " -CA arg - set the CA certificate, must be PEM format.\n",
137 " -CAkey arg - set the CA key, must be PEM format\n",
138 " missing, it is assumed to be in the CA file.\n",
139 " -CAcreateserial - create serial number file if it does not exist\n",
140 " -CAserial arg - serial file\n",
141 " -set_serial - serial number to use\n",
142 " -text - print the certificate in text form\n",
143 " -C - print out C code forms\n",
144 " -md2/-md5/-sha1/-mdc2 - digest to use\n",
145 " -extfile - configuration file with X509V3 extensions to add\n",
146 " -extensions - section from config file with X509V3 extensions to add\n",
147 " -clrext - delete extensions before signing and input certificate\n",
148 " -nameopt arg - various certificate name options\n",
149 #ifndef OPENSSL_NO_ENGINE
150 " -engine e - use engine e, possibly a hardware device.\n",
152 " -certopt arg - various certificate text options\n",
158 CONF *conf,
char *section);
162 char *serial,
int create ,
int days,
int clrext,
165 static int reqfile=0;
167 int MAIN(
int,
char **);
169 int MAIN(
int argc,
char **argv)
174 X509 *x=NULL,*xca=NULL;
183 int informat,outformat,keyformat,CAformat,CAkeyformat;
184 char *infile=NULL,*outfile=NULL,*keyfile=NULL,*CAfile=NULL;
185 char *CAkeyfile=NULL,*CAserial=NULL;
187 int text=0,serial=0,subject=0,issuer=0,startdate=0,enddate=0;
189 int subject_hash=0,issuer_hash=0,ocspid=0;
190 #ifndef OPENSSL_NO_MD5
191 int subject_hash_old=0,issuer_hash_old=0;
193 int noout=0,sign_flag=0,CA_flag=0,CA_createserial=0,email=0;
195 int trustout=0,clrtrust=0,clrreject=0,aliasout=0,clrext=0;
197 int x509req=0,days=
DEF_DAYS,modulus=0,pubkey=0;
204 const EVP_MD *md_alg,*digest=NULL;
205 CONF *extconf = NULL;
206 char *extsect = NULL, *extfile = NULL, *passin = NULL, *passargin = NULL;
208 int checkend=0,checkoffset=0;
209 unsigned long nmflag = 0, certflag = 0;
210 #ifndef OPENSSL_NO_ENGINE
224 #ifdef OPENSSL_SYS_VMS
238 if (ctx == NULL)
goto end;
246 if (strcmp(*argv,
"-inform") == 0)
248 if (--argc < 1)
goto bad;
251 else if (strcmp(*argv,
"-outform") == 0)
253 if (--argc < 1)
goto bad;
256 else if (strcmp(*argv,
"-keyform") == 0)
258 if (--argc < 1)
goto bad;
261 else if (strcmp(*argv,
"-req") == 0)
266 else if (strcmp(*argv,
"-CAform") == 0)
268 if (--argc < 1)
goto bad;
271 else if (strcmp(*argv,
"-CAkeyform") == 0)
273 if (--argc < 1)
goto bad;
274 CAkeyformat=
str2fmt(*(++argv));
276 else if (strcmp(*argv,
"-sigopt") == 0)
285 else if (strcmp(*argv,
"-days") == 0)
287 if (--argc < 1)
goto bad;
288 days=atoi(*(++argv));
295 else if (strcmp(*argv,
"-passin") == 0)
297 if (--argc < 1)
goto bad;
298 passargin= *(++argv);
300 else if (strcmp(*argv,
"-extfile") == 0)
302 if (--argc < 1)
goto bad;
305 else if (strcmp(*argv,
"-extensions") == 0)
307 if (--argc < 1)
goto bad;
310 else if (strcmp(*argv,
"-in") == 0)
312 if (--argc < 1)
goto bad;
315 else if (strcmp(*argv,
"-out") == 0)
317 if (--argc < 1)
goto bad;
320 else if (strcmp(*argv,
"-signkey") == 0)
322 if (--argc < 1)
goto bad;
327 else if (strcmp(*argv,
"-CA") == 0)
329 if (--argc < 1)
goto bad;
334 else if (strcmp(*argv,
"-CAkey") == 0)
336 if (--argc < 1)
goto bad;
337 CAkeyfile= *(++argv);
339 else if (strcmp(*argv,
"-CAserial") == 0)
341 if (--argc < 1)
goto bad;
344 else if (strcmp(*argv,
"-set_serial") == 0)
346 if (--argc < 1)
goto bad;
350 else if (strcmp(*argv,
"-addtrust") == 0)
352 if (--argc < 1)
goto bad;
356 "Invalid trust object value %s\n", *argv);
363 else if (strcmp(*argv,
"-addreject") == 0)
365 if (--argc < 1)
goto bad;
369 "Invalid reject object value %s\n", *argv);
376 else if (strcmp(*argv,
"-setalias") == 0)
378 if (--argc < 1)
goto bad;
382 else if (strcmp(*argv,
"-certopt") == 0)
384 if (--argc < 1)
goto bad;
387 else if (strcmp(*argv,
"-nameopt") == 0)
389 if (--argc < 1)
goto bad;
392 #ifndef OPENSSL_NO_ENGINE
393 else if (strcmp(*argv,
"-engine") == 0)
395 if (--argc < 1)
goto bad;
399 else if (strcmp(*argv,
"-C") == 0)
401 else if (strcmp(*argv,
"-email") == 0)
403 else if (strcmp(*argv,
"-ocsp_uri") == 0)
405 else if (strcmp(*argv,
"-serial") == 0)
407 else if (strcmp(*argv,
"-next_serial") == 0)
409 else if (strcmp(*argv,
"-modulus") == 0)
411 else if (strcmp(*argv,
"-pubkey") == 0)
413 else if (strcmp(*argv,
"-x509toreq") == 0)
415 else if (strcmp(*argv,
"-text") == 0)
417 else if (strcmp(*argv,
"-hash") == 0
418 || strcmp(*argv,
"-subject_hash") == 0)
420 #ifndef OPENSSL_NO_MD5
421 else if (strcmp(*argv,
"-subject_hash_old") == 0)
422 subject_hash_old= ++num;
424 else if (strcmp(*argv,
"-issuer_hash") == 0)
426 #ifndef OPENSSL_NO_MD5
427 else if (strcmp(*argv,
"-issuer_hash_old") == 0)
428 issuer_hash_old= ++num;
430 else if (strcmp(*argv,
"-subject") == 0)
432 else if (strcmp(*argv,
"-issuer") == 0)
434 else if (strcmp(*argv,
"-fingerprint") == 0)
436 else if (strcmp(*argv,
"-dates") == 0)
441 else if (strcmp(*argv,
"-purpose") == 0)
443 else if (strcmp(*argv,
"-startdate") == 0)
445 else if (strcmp(*argv,
"-enddate") == 0)
447 else if (strcmp(*argv,
"-checkend") == 0)
449 if (--argc < 1)
goto bad;
450 checkoffset=atoi(*(++argv));
453 else if (strcmp(*argv,
"-noout") == 0)
455 else if (strcmp(*argv,
"-trustout") == 0)
457 else if (strcmp(*argv,
"-clrtrust") == 0)
459 else if (strcmp(*argv,
"-clrreject") == 0)
461 else if (strcmp(*argv,
"-alias") == 0)
463 else if (strcmp(*argv,
"-CAcreateserial") == 0)
464 CA_createserial= ++
num;
465 else if (strcmp(*argv,
"-clrext") == 0)
468 else if (strcmp(*argv,
"-crlext") == 0)
474 else if (strcmp(*argv,
"-ocspid") == 0)
494 for (pp=x509_usage; (*pp != NULL); pp++)
499 #ifndef OPENSSL_NO_ENGINE
520 if ((CAkeyfile == NULL) && (CA_flag) && (CAformat ==
FORMAT_PEM))
521 { CAkeyfile=CAfile; }
522 else if ((CA_flag) && (CAkeyfile == NULL))
537 "error loading the config file '%s'\n",
541 "error on line %ld of config file '%s'\n"
559 "Error Loading extension section %s\n",
572 if (!sign_flag && !CA_flag)
595 req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL);
636 if ((x=X509_new()) == NULL)
goto end;
640 sno = ASN1_INTEGER_new();
645 ASN1_INTEGER_free(sno);
664 if (x == NULL)
goto end;
668 if (xca == NULL)
goto end;
671 if (!noout || text || next_serial)
674 "SET.ex3",
"SET x509v3 extension 3");
685 #ifdef OPENSSL_SYS_VMS
727 for (i=1; i<=
num; i++)
734 else if (subject == i)
739 else if (serial == i)
746 else if (next_serial == i)
761 ASN1_INTEGER_free(ser);
764 else if ((email == i) || (ocsp_uri == i))
769 emlst = X509_get1_email(x);
771 emlst = X509_get1_ocsp(x);
777 else if (aliasout == i)
779 unsigned char *alstr;
782 else BIO_puts(STDout,
"<No Alias>\n");
784 else if (subject_hash == i)
788 #ifndef OPENSSL_NO_MD5
789 else if (subject_hash_old == i)
794 else if (issuer_hash == i)
798 #ifndef OPENSSL_NO_MD5
799 else if (issuer_hash_old == i)
804 else if (pprint == i)
808 BIO_printf(STDout,
"Certificate purposes:\n");
812 purpose_print(STDout, x, ptmp);
828 #ifndef OPENSSL_NO_RSA
833 #ifndef OPENSSL_NO_DSA
854 PEM_write_bio_PUBKEY(STDout, pkey);
875 d=(
unsigned char *)m;
877 BIO_printf(STDout,
"unsigned char XXX_subject_name[%d]={\n",z);
878 d=(
unsigned char *)m;
882 if ((y & 0x0f) == 0x0f)
BIO_printf(STDout,
"\n");
888 BIO_printf(STDout,
"unsigned char XXX_public_key[%d]={\n",z);
889 d=(
unsigned char *)m;
893 if ((y & 0x0f) == 0x0f)
900 BIO_printf(STDout,
"unsigned char XXX_certificate[%d]={\n",z);
901 d=(
unsigned char *)m;
905 if ((y & 0x0f) == 0x0f)
917 else if (startdate == i)
923 else if (enddate == i)
929 else if (fingerprint == i)
934 const EVP_MD *fdig = digest;
946 for (j=0; j<(int)n; j++)
955 else if ((sign_flag == i) && (x509req == 0))
961 keyfile, keyformat, 0,
962 passin, e,
"Private key");
963 if (Upkey == NULL)
goto end;
967 if (!sign(x,Upkey,days,clrext,digest,
968 extconf, extsect))
goto end;
970 else if (CA_flag == i)
973 if (CAkeyfile != NULL)
976 CAkeyfile, CAkeyformat,
979 if (CApkey == NULL)
goto end;
983 if (!x509_certify(ctx,CAfile,digest,x,xca,
985 CAserial,CA_createserial,days, clrext,
986 extconf, extsect, sno))
989 else if (x509req == i)
1002 keyfile, keyformat, 0,
1003 passin, e,
"request key");
1004 if (pk == NULL)
goto end;
1019 PEM_write_bio_X509_REQ(out,rq);
1023 else if (ocspid == i)
1032 time_t tcheck=time(NULL) + checkoffset;
1041 BIO_printf(out,
"Certificate will not expire\n");
1057 if (trustout) i=PEM_write_bio_X509_AUX(out,x);
1058 else i=PEM_write_bio_X509(out,x);
1099 ASN1_INTEGER_free(sno);
1107 static ASN1_INTEGER *x509_load_serial(
char *CAfile,
char *serialfile,
int create)
1109 char *buf = NULL, *
p;
1114 len = ((serialfile == NULL)
1115 ?(strlen(CAfile)+strlen(
POSTFIX)+1)
1116 :(strlen(serialfile)))+1;
1119 if (serialfile == NULL)
1122 for (p=buf; *
p; p++)
1134 if (serial == NULL)
goto end;
1139 if (!
save_serial(buf, NULL, serial, &bs))
goto end;
1147 static int x509_certify(
X509_STORE *ctx,
char *CAfile,
const EVP_MD *digest,
1150 char *serialfile,
int create,
1151 int days,
int clrext,
CONF *conf,
char *section,
1169 else if (!(bs = x509_load_serial(CAfile, serialfile, create)))
1218 if (!sno) ASN1_INTEGER_free(bs);
1239 BIO_printf(
bio_err,
"error with certificate to be certified - should be self signed\n");
1255 CONF *conf,
char *section)
1288 if (!
X509_sign(x,pkey,digest))
goto err;
1301 for (i = 0; i < 2; i++)
1304 BIO_printf(bio,
"%s%s : ", pname, i ?
" CA" :
"");
1306 else if (idret == 0)
BIO_printf(bio,
"No\n");
1307 else BIO_printf(bio,
"Yes (WARNING code=%d)\n", idret);