os/ossrv/ssl/tsrc/topenssl/src/x509.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* apps/x509.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
sl@0
    59
#include <assert.h>
sl@0
    60
#include <stdio.h>
sl@0
    61
#include <stdlib.h>
sl@0
    62
#include <string.h>
sl@0
    63
#ifdef OPENSSL_NO_STDIO
sl@0
    64
#define APPS_WIN16
sl@0
    65
#endif
sl@0
    66
#include "apps.h"
sl@0
    67
#include <openssl/bio.h>
sl@0
    68
#include <openssl/asn1.h>
sl@0
    69
#include <openssl/err.h>
sl@0
    70
#include <openssl/bn.h>
sl@0
    71
#include <openssl/evp.h>
sl@0
    72
#include <openssl/x509.h>
sl@0
    73
#include <openssl/x509v3.h>
sl@0
    74
#include <openssl/objects.h>
sl@0
    75
#include <openssl/pem.h>
sl@0
    76
#ifndef OPENSSL_NO_RSA
sl@0
    77
#include <openssl/rsa.h>
sl@0
    78
#endif
sl@0
    79
#ifndef OPENSSL_NO_DSA
sl@0
    80
#include <openssl/dsa.h>
sl@0
    81
#endif
sl@0
    82
sl@0
    83
#undef PROG
sl@0
    84
#define PROG x509_main
sl@0
    85
sl@0
    86
#undef POSTFIX
sl@0
    87
#define	POSTFIX	".srl"
sl@0
    88
#define DEF_DAYS	30
sl@0
    89
sl@0
    90
static const char *x509_usage[]={
sl@0
    91
"usage: x509 args\n",
sl@0
    92
" -inform arg     - input format - default PEM (one of DER, NET or PEM)\n",
sl@0
    93
" -outform arg    - output format - default PEM (one of DER, NET or PEM)\n",
sl@0
    94
" -keyform arg    - private key format - default PEM\n",
sl@0
    95
" -CAform arg     - CA format - default PEM\n",
sl@0
    96
" -CAkeyform arg  - CA key format - default PEM\n",
sl@0
    97
" -in arg         - input file - default stdin\n",
sl@0
    98
" -out arg        - output file - default stdout\n",
sl@0
    99
" -passin arg     - private key password source\n",
sl@0
   100
" -serial         - print serial number value\n",
sl@0
   101
" -subject_hash   - print subject hash value\n",
sl@0
   102
" -issuer_hash    - print issuer hash value\n",
sl@0
   103
" -hash           - synonym for -subject_hash\n",
sl@0
   104
" -subject        - print subject DN\n",
sl@0
   105
" -issuer         - print issuer DN\n",
sl@0
   106
" -email          - print email address(es)\n",
sl@0
   107
" -startdate      - notBefore field\n",
sl@0
   108
" -enddate        - notAfter field\n",
sl@0
   109
" -purpose        - print out certificate purposes\n",
sl@0
   110
" -dates          - both Before and After dates\n",
sl@0
   111
" -modulus        - print the RSA key modulus\n",
sl@0
   112
" -pubkey         - output the public key\n",
sl@0
   113
" -fingerprint    - print the certificate fingerprint\n",
sl@0
   114
" -alias          - output certificate alias\n",
sl@0
   115
" -noout          - no certificate output\n",
sl@0
   116
" -ocspid         - print OCSP hash values for the subject name and public key\n",
sl@0
   117
" -trustout       - output a \"trusted\" certificate\n",
sl@0
   118
" -clrtrust       - clear all trusted purposes\n",
sl@0
   119
" -clrreject      - clear all rejected purposes\n",
sl@0
   120
" -addtrust arg   - trust certificate for a given purpose\n",
sl@0
   121
" -addreject arg  - reject certificate for a given purpose\n",
sl@0
   122
" -setalias arg   - set certificate alias\n",
sl@0
   123
" -days arg       - How long till expiry of a signed certificate - def 30 days\n",
sl@0
   124
" -checkend arg   - check whether the cert expires in the next arg seconds\n",
sl@0
   125
"                   exit 1 if so, 0 if not\n",
sl@0
   126
" -signkey arg    - self sign cert with arg\n",
sl@0
   127
" -x509toreq      - output a certification request object\n",
sl@0
   128
" -req            - input is a certificate request, sign and output.\n",
sl@0
   129
" -CA arg         - set the CA certificate, must be PEM format.\n",
sl@0
   130
" -CAkey arg      - set the CA key, must be PEM format\n",
sl@0
   131
"                   missing, it is assumed to be in the CA file.\n",
sl@0
   132
" -CAcreateserial - create serial number file if it does not exist\n",
sl@0
   133
" -CAserial arg   - serial file\n",
sl@0
   134
" -set_serial     - serial number to use\n",
sl@0
   135
" -text           - print the certificate in text form\n",
sl@0
   136
" -C              - print out C code forms\n",
sl@0
   137
" -md2/-md5/-sha1/-mdc2 - digest to use\n",
sl@0
   138
" -extfile        - configuration file with X509V3 extensions to add\n",
sl@0
   139
" -extensions     - section from config file with X509V3 extensions to add\n",
sl@0
   140
" -clrext         - delete extensions before signing and input certificate\n",
sl@0
   141
" -nameopt arg    - various certificate name options\n",
sl@0
   142
#ifndef OPENSSL_NO_ENGINE
sl@0
   143
" -engine e       - use engine e, possibly a hardware device.\n",
sl@0
   144
#endif
sl@0
   145
" -certopt arg    - various certificate text options\n",
sl@0
   146
NULL
sl@0
   147
};
sl@0
   148
sl@0
   149
static int MS_CALLBACK callb(int ok, X509_STORE_CTX *ctx);
sl@0
   150
static int sign (X509 *x, EVP_PKEY *pkey,int days,int clrext, const EVP_MD *digest,
sl@0
   151
						CONF *conf, char *section);
sl@0
   152
static int x509_certify (X509_STORE *ctx,char *CAfile,const EVP_MD *digest,
sl@0
   153
			 X509 *x,X509 *xca,EVP_PKEY *pkey,char *serial,
sl@0
   154
			 int create,int days, int clrext, CONF *conf, char *section,
sl@0
   155
						ASN1_INTEGER *sno);
sl@0
   156
static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt);
sl@0
   157
static int reqfile=0;
sl@0
   158
sl@0
   159
sl@0
   160
int MAIN(int, char **);
sl@0
   161
sl@0
   162
int MAIN(int argc, char **argv)
sl@0
   163
	{
sl@0
   164
	ENGINE *e = NULL;
sl@0
   165
	int ret=1;
sl@0
   166
	X509_REQ *req=NULL;
sl@0
   167
	X509 *x=NULL,*xca=NULL;
sl@0
   168
	ASN1_OBJECT *objtmp;
sl@0
   169
	EVP_PKEY *Upkey=NULL,*CApkey=NULL;
sl@0
   170
	ASN1_INTEGER *sno = NULL;
sl@0
   171
	int i,num,badops=0;
sl@0
   172
	BIO *out=NULL;
sl@0
   173
	BIO *STDout=NULL;
sl@0
   174
	STACK_OF(ASN1_OBJECT) *trust = NULL, *reject = NULL;
sl@0
   175
	int informat,outformat,keyformat,CAformat,CAkeyformat;
sl@0
   176
	char *infile=NULL,*outfile=NULL,*keyfile=NULL,*CAfile=NULL;
sl@0
   177
	char *CAkeyfile=NULL,*CAserial=NULL;
sl@0
   178
	char *alias=NULL;
sl@0
   179
	int text=0,serial=0,subject=0,issuer=0,startdate=0,enddate=0;
sl@0
   180
	int next_serial=0;
sl@0
   181
	int subject_hash=0,issuer_hash=0,ocspid=0;
sl@0
   182
	int noout=0,sign_flag=0,CA_flag=0,CA_createserial=0,email=0;
sl@0
   183
	int trustout=0,clrtrust=0,clrreject=0,aliasout=0,clrext=0;
sl@0
   184
	int C=0;
sl@0
   185
	int x509req=0,days=DEF_DAYS,modulus=0,pubkey=0;
sl@0
   186
	int pprint = 0;
sl@0
   187
	const char **pp;
sl@0
   188
	X509_STORE *ctx=NULL;
sl@0
   189
	X509_REQ *rq=NULL;
sl@0
   190
	int fingerprint=0;
sl@0
   191
	char buf[256];
sl@0
   192
	const EVP_MD *md_alg,*digest=EVP_sha1();
sl@0
   193
	CONF *extconf = NULL;
sl@0
   194
	char *extsect = NULL, *extfile = NULL, *passin = NULL, *passargin = NULL;
sl@0
   195
	int need_rand = 0;
sl@0
   196
	int checkend=0,checkoffset=0;
sl@0
   197
	unsigned long nmflag = 0, certflag = 0;
sl@0
   198
#ifndef OPENSSL_NO_ENGINE
sl@0
   199
	char *engine=NULL;
sl@0
   200
#endif
sl@0
   201
sl@0
   202
	reqfile=0;
sl@0
   203
sl@0
   204
	apps_startup();
sl@0
   205
sl@0
   206
	if (bio_err == NULL)
sl@0
   207
sl@0
   208
		bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
sl@0
   209
	if (!load_config(bio_err, NULL))
sl@0
   210
		goto end;
sl@0
   211
	STDout=BIO_new_fp(stdout,BIO_NOCLOSE);
sl@0
   212
#ifdef OPENSSL_SYS_VMS
sl@0
   213
	{
sl@0
   214
	BIO *tmpbio = BIO_new(BIO_f_linebuffer());
sl@0
   215
	STDout = BIO_push(tmpbio, STDout);
sl@0
   216
	}
sl@0
   217
#endif
sl@0
   218
sl@0
   219
	informat=FORMAT_PEM;
sl@0
   220
	outformat=FORMAT_PEM;
sl@0
   221
	keyformat=FORMAT_PEM;
sl@0
   222
	CAformat=FORMAT_PEM;
sl@0
   223
	CAkeyformat=FORMAT_PEM;
sl@0
   224
sl@0
   225
	ctx=X509_STORE_new();
sl@0
   226
	if (ctx == NULL) goto end;
sl@0
   227
	X509_STORE_set_verify_cb_func(ctx,callb);
sl@0
   228
sl@0
   229
	argc--;
sl@0
   230
	argv++;
sl@0
   231
	num=0;
sl@0
   232
	while (argc >= 1)
sl@0
   233
		{
sl@0
   234
		if 	(strcmp(*argv,"-inform") == 0)
sl@0
   235
			{
sl@0
   236
			if (--argc < 1) goto bad;
sl@0
   237
			informat=str2fmt(*(++argv));
sl@0
   238
			}
sl@0
   239
		else if (strcmp(*argv,"-outform") == 0)
sl@0
   240
			{
sl@0
   241
			if (--argc < 1) goto bad;
sl@0
   242
			outformat=str2fmt(*(++argv));
sl@0
   243
			}
sl@0
   244
		else if (strcmp(*argv,"-keyform") == 0)
sl@0
   245
			{
sl@0
   246
			if (--argc < 1) goto bad;
sl@0
   247
			keyformat=str2fmt(*(++argv));
sl@0
   248
			}
sl@0
   249
		else if (strcmp(*argv,"-req") == 0)
sl@0
   250
			{
sl@0
   251
			reqfile=1;
sl@0
   252
			need_rand = 1;
sl@0
   253
			}
sl@0
   254
		else if (strcmp(*argv,"-CAform") == 0)
sl@0
   255
			{
sl@0
   256
			if (--argc < 1) goto bad;
sl@0
   257
			CAformat=str2fmt(*(++argv));
sl@0
   258
			}
sl@0
   259
		else if (strcmp(*argv,"-CAkeyform") == 0)
sl@0
   260
			{
sl@0
   261
			if (--argc < 1) goto bad;
sl@0
   262
			CAkeyformat=str2fmt(*(++argv));
sl@0
   263
			}
sl@0
   264
		else if (strcmp(*argv,"-days") == 0)
sl@0
   265
			{
sl@0
   266
			if (--argc < 1) goto bad;
sl@0
   267
			days=atoi(*(++argv));
sl@0
   268
			if (days == 0)
sl@0
   269
				{
sl@0
   270
				BIO_printf(STDout,"bad number of days\n");
sl@0
   271
				goto bad;
sl@0
   272
				}
sl@0
   273
			}
sl@0
   274
		else if (strcmp(*argv,"-passin") == 0)
sl@0
   275
			{
sl@0
   276
			if (--argc < 1) goto bad;
sl@0
   277
			passargin= *(++argv);
sl@0
   278
			}
sl@0
   279
		else if (strcmp(*argv,"-extfile") == 0)
sl@0
   280
			{
sl@0
   281
			if (--argc < 1) goto bad;
sl@0
   282
			extfile= *(++argv);
sl@0
   283
			}
sl@0
   284
		else if (strcmp(*argv,"-extensions") == 0)
sl@0
   285
			{
sl@0
   286
			if (--argc < 1) goto bad;
sl@0
   287
			extsect= *(++argv);
sl@0
   288
			}
sl@0
   289
		else if (strcmp(*argv,"-in") == 0)
sl@0
   290
			{
sl@0
   291
			if (--argc < 1) goto bad;
sl@0
   292
			infile= *(++argv);
sl@0
   293
			}
sl@0
   294
		else if (strcmp(*argv,"-out") == 0)
sl@0
   295
			{
sl@0
   296
			if (--argc < 1) goto bad;
sl@0
   297
			outfile= *(++argv);
sl@0
   298
			}
sl@0
   299
		else if (strcmp(*argv,"-signkey") == 0)
sl@0
   300
			{
sl@0
   301
			if (--argc < 1) goto bad;
sl@0
   302
			keyfile= *(++argv);
sl@0
   303
			sign_flag= ++num;
sl@0
   304
			need_rand = 1;
sl@0
   305
			}
sl@0
   306
		else if (strcmp(*argv,"-CA") == 0)
sl@0
   307
			{
sl@0
   308
			if (--argc < 1) goto bad;
sl@0
   309
			CAfile= *(++argv);
sl@0
   310
			CA_flag= ++num;
sl@0
   311
			need_rand = 1;
sl@0
   312
			}
sl@0
   313
		else if (strcmp(*argv,"-CAkey") == 0)
sl@0
   314
			{
sl@0
   315
			if (--argc < 1) goto bad;
sl@0
   316
			CAkeyfile= *(++argv);
sl@0
   317
			}
sl@0
   318
		else if (strcmp(*argv,"-CAserial") == 0)
sl@0
   319
			{
sl@0
   320
			if (--argc < 1) goto bad;
sl@0
   321
			CAserial= *(++argv);
sl@0
   322
			}
sl@0
   323
		else if (strcmp(*argv,"-set_serial") == 0)
sl@0
   324
			{
sl@0
   325
			if (--argc < 1) goto bad;
sl@0
   326
			if (!(sno = s2i_ASN1_INTEGER(NULL, *(++argv))))
sl@0
   327
				goto bad;
sl@0
   328
			}
sl@0
   329
		else if (strcmp(*argv,"-addtrust") == 0)
sl@0
   330
			{
sl@0
   331
			if (--argc < 1) goto bad;
sl@0
   332
			if (!(objtmp = OBJ_txt2obj(*(++argv), 0)))
sl@0
   333
				{
sl@0
   334
				BIO_printf(bio_err,
sl@0
   335
					"Invalid trust object value %s\n", *argv);
sl@0
   336
				goto bad;
sl@0
   337
				}
sl@0
   338
			if (!trust) trust = sk_ASN1_OBJECT_new_null();
sl@0
   339
			sk_ASN1_OBJECT_push(trust, objtmp);
sl@0
   340
			trustout = 1;
sl@0
   341
			}
sl@0
   342
		else if (strcmp(*argv,"-addreject") == 0)
sl@0
   343
			{
sl@0
   344
			if (--argc < 1) goto bad;
sl@0
   345
			if (!(objtmp = OBJ_txt2obj(*(++argv), 0)))
sl@0
   346
				{
sl@0
   347
				BIO_printf(bio_err,
sl@0
   348
					"Invalid reject object value %s\n", *argv);
sl@0
   349
				goto bad;
sl@0
   350
				}
sl@0
   351
			if (!reject) reject = sk_ASN1_OBJECT_new_null();
sl@0
   352
			sk_ASN1_OBJECT_push(reject, objtmp);
sl@0
   353
			trustout = 1;
sl@0
   354
			}
sl@0
   355
		else if (strcmp(*argv,"-setalias") == 0)
sl@0
   356
			{
sl@0
   357
			if (--argc < 1) goto bad;
sl@0
   358
			alias= *(++argv);
sl@0
   359
			trustout = 1;
sl@0
   360
			}
sl@0
   361
		else if (strcmp(*argv,"-certopt") == 0)
sl@0
   362
			{
sl@0
   363
			if (--argc < 1) goto bad;
sl@0
   364
			if (!set_cert_ex(&certflag, *(++argv))) goto bad;
sl@0
   365
			}
sl@0
   366
		else if (strcmp(*argv,"-nameopt") == 0)
sl@0
   367
			{
sl@0
   368
			if (--argc < 1) goto bad;
sl@0
   369
			if (!set_name_ex(&nmflag, *(++argv))) goto bad;
sl@0
   370
			}
sl@0
   371
#ifndef OPENSSL_NO_ENGINE
sl@0
   372
		else if (strcmp(*argv,"-engine") == 0)
sl@0
   373
			{
sl@0
   374
			if (--argc < 1) goto bad;
sl@0
   375
			engine= *(++argv);
sl@0
   376
			}
sl@0
   377
#endif
sl@0
   378
		else if (strcmp(*argv,"-C") == 0)
sl@0
   379
			C= ++num;
sl@0
   380
		else if (strcmp(*argv,"-email") == 0)
sl@0
   381
			email= ++num;
sl@0
   382
		else if (strcmp(*argv,"-serial") == 0)
sl@0
   383
			serial= ++num;
sl@0
   384
		else if (strcmp(*argv,"-next_serial") == 0)
sl@0
   385
			next_serial= ++num;
sl@0
   386
		else if (strcmp(*argv,"-modulus") == 0)
sl@0
   387
			modulus= ++num;
sl@0
   388
		else if (strcmp(*argv,"-pubkey") == 0)
sl@0
   389
			pubkey= ++num;
sl@0
   390
		else if (strcmp(*argv,"-x509toreq") == 0)
sl@0
   391
			x509req= ++num;
sl@0
   392
		else if (strcmp(*argv,"-text") == 0)
sl@0
   393
			text= ++num;
sl@0
   394
		else if (strcmp(*argv,"-hash") == 0
sl@0
   395
			|| strcmp(*argv,"-subject_hash") == 0)
sl@0
   396
			subject_hash= ++num;
sl@0
   397
		else if (strcmp(*argv,"-issuer_hash") == 0)
sl@0
   398
			issuer_hash= ++num;
sl@0
   399
		else if (strcmp(*argv,"-subject") == 0)
sl@0
   400
			subject= ++num;
sl@0
   401
		else if (strcmp(*argv,"-issuer") == 0)
sl@0
   402
			issuer= ++num;
sl@0
   403
		else if (strcmp(*argv,"-fingerprint") == 0)
sl@0
   404
			fingerprint= ++num;
sl@0
   405
		else if (strcmp(*argv,"-dates") == 0)
sl@0
   406
			{
sl@0
   407
			startdate= ++num;
sl@0
   408
			enddate= ++num;
sl@0
   409
			}
sl@0
   410
		else if (strcmp(*argv,"-purpose") == 0)
sl@0
   411
			pprint= ++num;
sl@0
   412
		else if (strcmp(*argv,"-startdate") == 0)
sl@0
   413
			startdate= ++num;
sl@0
   414
		else if (strcmp(*argv,"-enddate") == 0)
sl@0
   415
			enddate= ++num;
sl@0
   416
		else if (strcmp(*argv,"-checkend") == 0)
sl@0
   417
			{
sl@0
   418
			if (--argc < 1) goto bad;
sl@0
   419
			checkoffset=atoi(*(++argv));
sl@0
   420
			checkend=1;
sl@0
   421
			}
sl@0
   422
		else if (strcmp(*argv,"-noout") == 0)
sl@0
   423
			noout= ++num;
sl@0
   424
		else if (strcmp(*argv,"-trustout") == 0)
sl@0
   425
			trustout= 1;
sl@0
   426
		else if (strcmp(*argv,"-clrtrust") == 0)
sl@0
   427
			clrtrust= ++num;
sl@0
   428
		else if (strcmp(*argv,"-clrreject") == 0)
sl@0
   429
			clrreject= ++num;
sl@0
   430
		else if (strcmp(*argv,"-alias") == 0)
sl@0
   431
			aliasout= ++num;
sl@0
   432
		else if (strcmp(*argv,"-CAcreateserial") == 0)
sl@0
   433
			CA_createserial= ++num;
sl@0
   434
		else if (strcmp(*argv,"-clrext") == 0)
sl@0
   435
			clrext = 1;
sl@0
   436
#if 1 /* stay backwards-compatible with 0.9.5; this should go away soon */
sl@0
   437
		else if (strcmp(*argv,"-crlext") == 0)
sl@0
   438
			{
sl@0
   439
			BIO_printf(bio_err,"use -clrext instead of -crlext\n");
sl@0
   440
			clrext = 1;
sl@0
   441
			}
sl@0
   442
#endif
sl@0
   443
		else if (strcmp(*argv,"-ocspid") == 0)
sl@0
   444
			ocspid= ++num;
sl@0
   445
		else if ((md_alg=EVP_get_digestbyname(*argv + 1)))
sl@0
   446
			{
sl@0
   447
			/* ok */
sl@0
   448
			digest=md_alg;
sl@0
   449
			}
sl@0
   450
		else
sl@0
   451
			{
sl@0
   452
			BIO_printf(bio_err,"unknown option %s\n",*argv);
sl@0
   453
			badops=1;
sl@0
   454
			break;
sl@0
   455
			}
sl@0
   456
		argc--;
sl@0
   457
		argv++;
sl@0
   458
		}
sl@0
   459
sl@0
   460
	if (badops)
sl@0
   461
		{
sl@0
   462
bad:
sl@0
   463
		for (pp=x509_usage; (*pp != NULL); pp++)
sl@0
   464
			BIO_printf(bio_err,"%s",*pp);
sl@0
   465
		goto end;
sl@0
   466
		}
sl@0
   467
sl@0
   468
#ifndef OPENSSL_NO_ENGINE
sl@0
   469
        e = setup_engine(bio_err, engine, 0);
sl@0
   470
#endif
sl@0
   471
sl@0
   472
	if (need_rand)
sl@0
   473
		app_RAND_load_file(NULL, bio_err, 0);
sl@0
   474
sl@0
   475
	ERR_load_crypto_strings();
sl@0
   476
sl@0
   477
	if (!app_passwd(bio_err, passargin, NULL, &passin, NULL))
sl@0
   478
		{
sl@0
   479
		BIO_printf(bio_err, "Error getting password\n");
sl@0
   480
		goto end;
sl@0
   481
		}
sl@0
   482
sl@0
   483
	if (!X509_STORE_set_default_paths(ctx))
sl@0
   484
		{
sl@0
   485
		ERR_print_errors(bio_err);
sl@0
   486
		goto end;
sl@0
   487
		}
sl@0
   488
sl@0
   489
	if ((CAkeyfile == NULL) && (CA_flag) && (CAformat == FORMAT_PEM))
sl@0
   490
		{ CAkeyfile=CAfile; }
sl@0
   491
	else if ((CA_flag) && (CAkeyfile == NULL))
sl@0
   492
		{
sl@0
   493
		BIO_printf(bio_err,"need to specify a CAkey if using the CA command\n");
sl@0
   494
		goto end;
sl@0
   495
		}
sl@0
   496
sl@0
   497
	if (extfile)
sl@0
   498
		{
sl@0
   499
		long errorline = -1;
sl@0
   500
		X509V3_CTX ctx2;
sl@0
   501
		extconf = NCONF_new(NULL);
sl@0
   502
		if (!NCONF_load(extconf, extfile,&errorline))
sl@0
   503
			{
sl@0
   504
			if (errorline <= 0)
sl@0
   505
				BIO_printf(bio_err,
sl@0
   506
					"error loading the config file '%s'\n",
sl@0
   507
								extfile);
sl@0
   508
                	else
sl@0
   509
                        	BIO_printf(bio_err,
sl@0
   510
				       "error on line %ld of config file '%s'\n"
sl@0
   511
							,errorline,extfile);
sl@0
   512
			goto end;
sl@0
   513
			}
sl@0
   514
		if (!extsect)
sl@0
   515
			{
sl@0
   516
			extsect = NCONF_get_string(extconf, "default", "extensions");
sl@0
   517
			if (!extsect)
sl@0
   518
				{
sl@0
   519
				ERR_clear_error();
sl@0
   520
				extsect = "default";
sl@0
   521
				}
sl@0
   522
			}
sl@0
   523
		X509V3_set_ctx_test(&ctx2);
sl@0
   524
		X509V3_set_nconf(&ctx2, extconf);
sl@0
   525
		if (!X509V3_EXT_add_nconf(extconf, &ctx2, extsect, NULL))
sl@0
   526
			{
sl@0
   527
			BIO_printf(bio_err,
sl@0
   528
				"Error Loading extension section %s\n",
sl@0
   529
								 extsect);
sl@0
   530
			ERR_print_errors(bio_err);
sl@0
   531
			goto end;
sl@0
   532
			}
sl@0
   533
		}
sl@0
   534
sl@0
   535
sl@0
   536
	if (reqfile)
sl@0
   537
		{
sl@0
   538
		EVP_PKEY *pkey;
sl@0
   539
		X509_CINF *ci;
sl@0
   540
		BIO *in;
sl@0
   541
sl@0
   542
		if (!sign_flag && !CA_flag)
sl@0
   543
			{
sl@0
   544
			BIO_printf(bio_err,"We need a private key to sign with\n");
sl@0
   545
			goto end;
sl@0
   546
			}
sl@0
   547
		in=BIO_new(BIO_s_file());
sl@0
   548
		if (in == NULL)
sl@0
   549
			{
sl@0
   550
			ERR_print_errors(bio_err);
sl@0
   551
			goto end;
sl@0
   552
			}
sl@0
   553
sl@0
   554
		if (infile == NULL)
sl@0
   555
			BIO_set_fp(in,stdin,BIO_NOCLOSE|BIO_FP_TEXT);
sl@0
   556
		else
sl@0
   557
			{
sl@0
   558
			if (BIO_read_filename(in,infile) <= 0)
sl@0
   559
				{
sl@0
   560
				perror(infile);
sl@0
   561
				BIO_free(in);
sl@0
   562
				goto end;
sl@0
   563
				}
sl@0
   564
			}
sl@0
   565
		req=PEM_read_bio_X509_REQ(in,NULL,NULL,NULL);
sl@0
   566
		BIO_free(in);
sl@0
   567
sl@0
   568
		if (req == NULL)
sl@0
   569
			{
sl@0
   570
			ERR_print_errors(bio_err);
sl@0
   571
			goto end;
sl@0
   572
			}
sl@0
   573
sl@0
   574
		if (	(req->req_info == NULL) ||
sl@0
   575
			(req->req_info->pubkey == NULL) ||
sl@0
   576
			(req->req_info->pubkey->public_key == NULL) ||
sl@0
   577
			(req->req_info->pubkey->public_key->data == NULL))
sl@0
   578
			{
sl@0
   579
			BIO_printf(bio_err,"The certificate request appears to corrupted\n");
sl@0
   580
			BIO_printf(bio_err,"It does not contain a public key\n");
sl@0
   581
			goto end;
sl@0
   582
			}
sl@0
   583
		if ((pkey=X509_REQ_get_pubkey(req)) == NULL)
sl@0
   584
	                {
sl@0
   585
	                BIO_printf(bio_err,"error unpacking public key\n");
sl@0
   586
	                goto end;
sl@0
   587
	                }
sl@0
   588
		i=X509_REQ_verify(req,pkey);
sl@0
   589
		EVP_PKEY_free(pkey);
sl@0
   590
		if (i < 0)
sl@0
   591
			{
sl@0
   592
			BIO_printf(bio_err,"Signature verification error\n");
sl@0
   593
			ERR_print_errors(bio_err);
sl@0
   594
			goto end;
sl@0
   595
			}
sl@0
   596
	        if (i == 0)
sl@0
   597
			{
sl@0
   598
			BIO_printf(bio_err,"Signature did not match the certificate request\n");
sl@0
   599
			goto end;
sl@0
   600
			}
sl@0
   601
		else
sl@0
   602
			BIO_printf(bio_err,"Signature ok\n");
sl@0
   603
sl@0
   604
		print_name(bio_err, "subject=", X509_REQ_get_subject_name(req), nmflag);
sl@0
   605
sl@0
   606
		if ((x=X509_new()) == NULL) goto end;
sl@0
   607
		ci=x->cert_info;
sl@0
   608
sl@0
   609
		if (sno == NULL)
sl@0
   610
			{
sl@0
   611
			sno = ASN1_INTEGER_new();
sl@0
   612
			if (!sno || !rand_serial(NULL, sno))
sl@0
   613
				goto end;
sl@0
   614
			if (!X509_set_serialNumber(x, sno)) 
sl@0
   615
				goto end;
sl@0
   616
			ASN1_INTEGER_free(sno);
sl@0
   617
			sno = NULL;
sl@0
   618
			}
sl@0
   619
		else if (!X509_set_serialNumber(x, sno)) 
sl@0
   620
			goto end;
sl@0
   621
sl@0
   622
		if (!X509_set_issuer_name(x,req->req_info->subject)) goto end;
sl@0
   623
		if (!X509_set_subject_name(x,req->req_info->subject)) goto end;
sl@0
   624
sl@0
   625
		X509_gmtime_adj(X509_get_notBefore(x),0);
sl@0
   626
	        X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days);
sl@0
   627
sl@0
   628
		pkey = X509_REQ_get_pubkey(req);
sl@0
   629
		X509_set_pubkey(x,pkey);
sl@0
   630
		EVP_PKEY_free(pkey);
sl@0
   631
		}
sl@0
   632
	else
sl@0
   633
		x=load_cert(bio_err,infile,informat,NULL,e,"Certificate");
sl@0
   634
sl@0
   635
	if (x == NULL) goto end;
sl@0
   636
	if (CA_flag)
sl@0
   637
		{
sl@0
   638
		xca=load_cert(bio_err,CAfile,CAformat,NULL,e,"CA Certificate");
sl@0
   639
		if (xca == NULL) goto end;
sl@0
   640
		}
sl@0
   641
sl@0
   642
	if (!noout || text || next_serial)
sl@0
   643
		{
sl@0
   644
		OBJ_create("2.99999.3",
sl@0
   645
			"SET.ex3","SET x509v3 extension 3");
sl@0
   646
sl@0
   647
		out=BIO_new(BIO_s_file());
sl@0
   648
		if (out == NULL)
sl@0
   649
			{
sl@0
   650
			ERR_print_errors(bio_err);
sl@0
   651
			goto end;
sl@0
   652
			}
sl@0
   653
		if (outfile == NULL)
sl@0
   654
			{
sl@0
   655
			BIO_set_fp(out,stdout,BIO_NOCLOSE);
sl@0
   656
#ifdef OPENSSL_SYS_VMS
sl@0
   657
			{
sl@0
   658
			BIO *tmpbio = BIO_new(BIO_f_linebuffer());
sl@0
   659
			out = BIO_push(tmpbio, out);
sl@0
   660
			}
sl@0
   661
#endif
sl@0
   662
			}
sl@0
   663
		else
sl@0
   664
			{
sl@0
   665
			if (BIO_write_filename(out,outfile) <= 0)
sl@0
   666
				{
sl@0
   667
				perror(outfile);
sl@0
   668
				goto end;
sl@0
   669
				}
sl@0
   670
			}
sl@0
   671
		}
sl@0
   672
sl@0
   673
	if (alias) X509_alias_set1(x, (unsigned char *)alias, -1);
sl@0
   674
sl@0
   675
	if (clrtrust) X509_trust_clear(x);
sl@0
   676
	if (clrreject) X509_reject_clear(x);
sl@0
   677
sl@0
   678
	if (trust)
sl@0
   679
		{
sl@0
   680
		for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++)
sl@0
   681
			{
sl@0
   682
			objtmp = sk_ASN1_OBJECT_value(trust, i);
sl@0
   683
			X509_add1_trust_object(x, objtmp);
sl@0
   684
			}
sl@0
   685
		}
sl@0
   686
sl@0
   687
	if (reject)
sl@0
   688
		{
sl@0
   689
		for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++)
sl@0
   690
			{
sl@0
   691
			objtmp = sk_ASN1_OBJECT_value(reject, i);
sl@0
   692
			X509_add1_reject_object(x, objtmp);
sl@0
   693
			}
sl@0
   694
		}
sl@0
   695
sl@0
   696
	if (num)
sl@0
   697
		{
sl@0
   698
		for (i=1; i<=num; i++)
sl@0
   699
			{
sl@0
   700
			if (issuer == i)
sl@0
   701
				{
sl@0
   702
				print_name(STDout, "issuer= ",
sl@0
   703
					X509_get_issuer_name(x), nmflag);
sl@0
   704
				}
sl@0
   705
			else if (subject == i) 
sl@0
   706
				{
sl@0
   707
				print_name(STDout, "subject= ",
sl@0
   708
					X509_get_subject_name(x), nmflag);
sl@0
   709
				}
sl@0
   710
			else if (serial == i)
sl@0
   711
				{
sl@0
   712
				BIO_printf(STDout,"serial=");
sl@0
   713
				i2a_ASN1_INTEGER(STDout,
sl@0
   714
					X509_get_serialNumber(x));
sl@0
   715
				BIO_printf(STDout,"\n");
sl@0
   716
				}
sl@0
   717
			else if (next_serial == i)
sl@0
   718
				{
sl@0
   719
				BIGNUM *bnser;
sl@0
   720
				ASN1_INTEGER *ser;
sl@0
   721
				ser = X509_get_serialNumber(x);
sl@0
   722
				bnser = ASN1_INTEGER_to_BN(ser, NULL);
sl@0
   723
				if (!bnser)
sl@0
   724
					goto end;
sl@0
   725
				if (!BN_add_word(bnser, 1))
sl@0
   726
					goto end;
sl@0
   727
				ser = BN_to_ASN1_INTEGER(bnser, NULL);
sl@0
   728
				if (!ser)
sl@0
   729
					goto end;
sl@0
   730
				BN_free(bnser);
sl@0
   731
				i2a_ASN1_INTEGER(out, ser);
sl@0
   732
				ASN1_INTEGER_free(ser);
sl@0
   733
				BIO_puts(out, "\n");
sl@0
   734
				}
sl@0
   735
			else if (email == i) 
sl@0
   736
				{
sl@0
   737
				int j;
sl@0
   738
				STACK *emlst;
sl@0
   739
				emlst = X509_get1_email(x);
sl@0
   740
				for (j = 0; j < sk_num(emlst); j++)
sl@0
   741
					BIO_printf(STDout, "%s\n", sk_value(emlst, j));
sl@0
   742
				X509_email_free(emlst);
sl@0
   743
				}
sl@0
   744
			else if (aliasout == i)
sl@0
   745
				{
sl@0
   746
				unsigned char *alstr;
sl@0
   747
				alstr = X509_alias_get0(x, NULL);
sl@0
   748
				if (alstr) BIO_printf(STDout,"%s\n", alstr);
sl@0
   749
				else BIO_puts(STDout,"<No Alias>\n");
sl@0
   750
				}
sl@0
   751
			else if (subject_hash == i)
sl@0
   752
				{
sl@0
   753
				BIO_printf(STDout,"%08lx\n",X509_subject_name_hash(x));
sl@0
   754
				}
sl@0
   755
			else if (issuer_hash == i)
sl@0
   756
				{
sl@0
   757
				BIO_printf(STDout,"%08lx\n",X509_issuer_name_hash(x));
sl@0
   758
				}
sl@0
   759
			else if (pprint == i)
sl@0
   760
				{
sl@0
   761
				X509_PURPOSE *ptmp;
sl@0
   762
				int j;
sl@0
   763
				BIO_printf(STDout, "Certificate purposes:\n");
sl@0
   764
				for (j = 0; j < X509_PURPOSE_get_count(); j++)
sl@0
   765
					{
sl@0
   766
					ptmp = X509_PURPOSE_get0(j);
sl@0
   767
					purpose_print(STDout, x, ptmp);
sl@0
   768
					}
sl@0
   769
				}
sl@0
   770
			else
sl@0
   771
				if (modulus == i)
sl@0
   772
				{
sl@0
   773
				EVP_PKEY *pkey;
sl@0
   774
sl@0
   775
				pkey=X509_get_pubkey(x);
sl@0
   776
				if (pkey == NULL)
sl@0
   777
					{
sl@0
   778
					BIO_printf(bio_err,"Modulus=unavailable\n");
sl@0
   779
					ERR_print_errors(bio_err);
sl@0
   780
					goto end;
sl@0
   781
					}
sl@0
   782
				BIO_printf(STDout,"Modulus=");
sl@0
   783
#ifndef OPENSSL_NO_RSA
sl@0
   784
				if (pkey->type == EVP_PKEY_RSA)
sl@0
   785
					BN_print(STDout,pkey->pkey.rsa->n);
sl@0
   786
				else
sl@0
   787
#endif
sl@0
   788
#ifndef OPENSSL_NO_DSA
sl@0
   789
				if (pkey->type == EVP_PKEY_DSA)
sl@0
   790
					BN_print(STDout,pkey->pkey.dsa->pub_key);
sl@0
   791
				else
sl@0
   792
#endif
sl@0
   793
					BIO_printf(STDout,"Wrong Algorithm type");
sl@0
   794
				BIO_printf(STDout,"\n");
sl@0
   795
				EVP_PKEY_free(pkey);
sl@0
   796
				}
sl@0
   797
			else
sl@0
   798
				if (pubkey == i)
sl@0
   799
				{
sl@0
   800
				EVP_PKEY *pkey;
sl@0
   801
sl@0
   802
				pkey=X509_get_pubkey(x);
sl@0
   803
				if (pkey == NULL)
sl@0
   804
					{
sl@0
   805
					BIO_printf(bio_err,"Error getting public key\n");
sl@0
   806
					ERR_print_errors(bio_err);
sl@0
   807
					goto end;
sl@0
   808
					}
sl@0
   809
				PEM_write_bio_PUBKEY(STDout, pkey);
sl@0
   810
				EVP_PKEY_free(pkey);
sl@0
   811
				}
sl@0
   812
			else
sl@0
   813
				if (C == i)
sl@0
   814
				{
sl@0
   815
				unsigned char *d;
sl@0
   816
				char *m;
sl@0
   817
				int y,z;
sl@0
   818
sl@0
   819
				X509_NAME_oneline(X509_get_subject_name(x),
sl@0
   820
					buf,sizeof buf);
sl@0
   821
				BIO_printf(STDout,"/* subject:%s */\n",buf);
sl@0
   822
				m=X509_NAME_oneline(
sl@0
   823
					X509_get_issuer_name(x),buf,
sl@0
   824
					sizeof buf);
sl@0
   825
				BIO_printf(STDout,"/* issuer :%s */\n",buf);
sl@0
   826
sl@0
   827
				z=i2d_X509(x,NULL);
sl@0
   828
				m=OPENSSL_malloc(z);
sl@0
   829
sl@0
   830
				d=(unsigned char *)m;
sl@0
   831
				z=i2d_X509_NAME(X509_get_subject_name(x),&d);
sl@0
   832
				BIO_printf(STDout,"unsigned char XXX_subject_name[%d]={\n",z);
sl@0
   833
				d=(unsigned char *)m;
sl@0
   834
				for (y=0; y<z; y++)
sl@0
   835
					{
sl@0
   836
					BIO_printf(STDout,"0x%02X,",d[y]);
sl@0
   837
					if ((y & 0x0f) == 0x0f) BIO_printf(STDout,"\n");
sl@0
   838
					}
sl@0
   839
				if (y%16 != 0) BIO_printf(STDout,"\n");
sl@0
   840
				BIO_printf(STDout,"};\n");
sl@0
   841
sl@0
   842
				z=i2d_X509_PUBKEY(X509_get_X509_PUBKEY(x),&d);
sl@0
   843
				BIO_printf(STDout,"unsigned char XXX_public_key[%d]={\n",z);
sl@0
   844
				d=(unsigned char *)m;
sl@0
   845
				for (y=0; y<z; y++)
sl@0
   846
					{
sl@0
   847
					BIO_printf(STDout,"0x%02X,",d[y]);
sl@0
   848
					if ((y & 0x0f) == 0x0f)
sl@0
   849
						BIO_printf(STDout,"\n");
sl@0
   850
					}
sl@0
   851
				if (y%16 != 0) BIO_printf(STDout,"\n");
sl@0
   852
				BIO_printf(STDout,"};\n");
sl@0
   853
sl@0
   854
				z=i2d_X509(x,&d);
sl@0
   855
				BIO_printf(STDout,"unsigned char XXX_certificate[%d]={\n",z);
sl@0
   856
				d=(unsigned char *)m;
sl@0
   857
				for (y=0; y<z; y++)
sl@0
   858
					{
sl@0
   859
					BIO_printf(STDout,"0x%02X,",d[y]);
sl@0
   860
					if ((y & 0x0f) == 0x0f)
sl@0
   861
						BIO_printf(STDout,"\n");
sl@0
   862
					}
sl@0
   863
				if (y%16 != 0) BIO_printf(STDout,"\n");
sl@0
   864
				BIO_printf(STDout,"};\n");
sl@0
   865
sl@0
   866
				OPENSSL_free(m);
sl@0
   867
				}
sl@0
   868
			else if (text == i)
sl@0
   869
				{
sl@0
   870
				X509_print_ex(out,x,nmflag, certflag);
sl@0
   871
				}
sl@0
   872
			else if (startdate == i)
sl@0
   873
				{
sl@0
   874
				BIO_puts(STDout,"notBefore=");
sl@0
   875
				ASN1_TIME_print(STDout,X509_get_notBefore(x));
sl@0
   876
				BIO_puts(STDout,"\n");
sl@0
   877
				}
sl@0
   878
			else if (enddate == i)
sl@0
   879
				{
sl@0
   880
				BIO_puts(STDout,"notAfter=");
sl@0
   881
				ASN1_TIME_print(STDout,X509_get_notAfter(x));
sl@0
   882
				BIO_puts(STDout,"\n");
sl@0
   883
				}
sl@0
   884
			else if (fingerprint == i)
sl@0
   885
				{
sl@0
   886
				int j;
sl@0
   887
				unsigned int n;
sl@0
   888
				unsigned char md[EVP_MAX_MD_SIZE];
sl@0
   889
sl@0
   890
				if (!X509_digest(x,digest,md,&n))
sl@0
   891
					{
sl@0
   892
					BIO_printf(bio_err,"out of memory\n");
sl@0
   893
					goto end;
sl@0
   894
					}
sl@0
   895
				BIO_printf(STDout,"%s Fingerprint=",
sl@0
   896
						OBJ_nid2sn(EVP_MD_type(digest)));
sl@0
   897
				for (j=0; j<(int)n; j++)
sl@0
   898
					{
sl@0
   899
					BIO_printf(STDout,"%02X%c",md[j],
sl@0
   900
						(j+1 == (int)n)
sl@0
   901
						?'\n':':');
sl@0
   902
					}
sl@0
   903
				}
sl@0
   904
sl@0
   905
			/* should be in the library */
sl@0
   906
			else if ((sign_flag == i) && (x509req == 0))
sl@0
   907
				{
sl@0
   908
				BIO_printf(bio_err,"Getting Private key\n");
sl@0
   909
				if (Upkey == NULL)
sl@0
   910
					{
sl@0
   911
					Upkey=load_key(bio_err,
sl@0
   912
						keyfile, keyformat, 0,
sl@0
   913
						passin, e, "Private key");
sl@0
   914
					if (Upkey == NULL) goto end;
sl@0
   915
					}
sl@0
   916
#ifndef OPENSSL_NO_DSA
sl@0
   917
		                if (Upkey->type == EVP_PKEY_DSA)
sl@0
   918
		                        digest=EVP_dss1();
sl@0
   919
#endif
sl@0
   920
#ifndef OPENSSL_NO_ECDSA
sl@0
   921
				if (Upkey->type == EVP_PKEY_EC)
sl@0
   922
					digest=EVP_ecdsa();
sl@0
   923
#endif
sl@0
   924
sl@0
   925
				assert(need_rand);
sl@0
   926
				if (!sign(x,Upkey,days,clrext,digest,
sl@0
   927
						 extconf, extsect)) goto end;
sl@0
   928
				}
sl@0
   929
			else if (CA_flag == i)
sl@0
   930
				{
sl@0
   931
				BIO_printf(bio_err,"Getting CA Private Key\n");
sl@0
   932
				if (CAkeyfile != NULL)
sl@0
   933
					{
sl@0
   934
					CApkey=load_key(bio_err,
sl@0
   935
						CAkeyfile, CAkeyformat,
sl@0
   936
						0, passin, e,
sl@0
   937
						"CA Private Key");
sl@0
   938
					if (CApkey == NULL) goto end;
sl@0
   939
					}
sl@0
   940
#ifndef OPENSSL_NO_DSA
sl@0
   941
		                if (CApkey->type == EVP_PKEY_DSA)
sl@0
   942
		                        digest=EVP_dss1();
sl@0
   943
#endif
sl@0
   944
#ifndef OPENSSL_NO_ECDSA
sl@0
   945
				if (CApkey->type == EVP_PKEY_EC)
sl@0
   946
					digest = EVP_ecdsa();
sl@0
   947
#endif
sl@0
   948
				
sl@0
   949
				assert(need_rand);
sl@0
   950
				if (!x509_certify(ctx,CAfile,digest,x,xca,
sl@0
   951
					CApkey, CAserial,CA_createserial,days, clrext,
sl@0
   952
					extconf, extsect, sno))
sl@0
   953
					goto end;
sl@0
   954
				}
sl@0
   955
			else if (x509req == i)
sl@0
   956
				{
sl@0
   957
				EVP_PKEY *pk;
sl@0
   958
sl@0
   959
				BIO_printf(bio_err,"Getting request Private Key\n");
sl@0
   960
				if (keyfile == NULL)
sl@0
   961
					{
sl@0
   962
					BIO_printf(bio_err,"no request key file specified\n");
sl@0
   963
					goto end;
sl@0
   964
					}
sl@0
   965
				else
sl@0
   966
					{
sl@0
   967
					pk=load_key(bio_err,
sl@0
   968
						keyfile, FORMAT_PEM, 0,
sl@0
   969
						passin, e, "request key");
sl@0
   970
					if (pk == NULL) goto end;
sl@0
   971
					}
sl@0
   972
sl@0
   973
				BIO_printf(bio_err,"Generating certificate request\n");
sl@0
   974
sl@0
   975
#ifndef OPENSSL_NO_DSA
sl@0
   976
		                if (pk->type == EVP_PKEY_DSA)
sl@0
   977
		                        digest=EVP_dss1();
sl@0
   978
#endif
sl@0
   979
#ifndef OPENSSL_NO_ECDSA
sl@0
   980
				if (pk->type == EVP_PKEY_EC)
sl@0
   981
					digest=EVP_ecdsa();
sl@0
   982
#endif
sl@0
   983
sl@0
   984
				rq=X509_to_X509_REQ(x,pk,digest);
sl@0
   985
				EVP_PKEY_free(pk);
sl@0
   986
				if (rq == NULL)
sl@0
   987
					{
sl@0
   988
					ERR_print_errors(bio_err);
sl@0
   989
					goto end;
sl@0
   990
					}
sl@0
   991
				if (!noout)
sl@0
   992
					{
sl@0
   993
					X509_REQ_print(out,rq);
sl@0
   994
					PEM_write_bio_X509_REQ(out,rq);
sl@0
   995
					}
sl@0
   996
				noout=1;
sl@0
   997
				}
sl@0
   998
			else if (ocspid == i)
sl@0
   999
				{
sl@0
  1000
				X509_ocspid_print(out, x);
sl@0
  1001
				}
sl@0
  1002
			}
sl@0
  1003
		}
sl@0
  1004
sl@0
  1005
	if (checkend)
sl@0
  1006
		{
sl@0
  1007
		time_t tcheck=time(NULL) + checkoffset;
sl@0
  1008
sl@0
  1009
		if (X509_cmp_time(X509_get_notAfter(x), &tcheck) < 0)
sl@0
  1010
			{
sl@0
  1011
			BIO_printf(out,"Certificate will expire\n");
sl@0
  1012
			ret=1;
sl@0
  1013
			}
sl@0
  1014
		else
sl@0
  1015
			{
sl@0
  1016
			BIO_printf(out,"Certificate will not expire\n");
sl@0
  1017
			ret=0;
sl@0
  1018
			}
sl@0
  1019
		goto end;
sl@0
  1020
		}
sl@0
  1021
sl@0
  1022
	if (noout)
sl@0
  1023
		{
sl@0
  1024
		ret=0;
sl@0
  1025
		goto end;
sl@0
  1026
		}
sl@0
  1027
sl@0
  1028
	if 	(outformat == FORMAT_ASN1)
sl@0
  1029
		i=i2d_X509_bio(out,x);
sl@0
  1030
	else if (outformat == FORMAT_PEM)
sl@0
  1031
		{
sl@0
  1032
		if (trustout) i=PEM_write_bio_X509_AUX(out,x);
sl@0
  1033
		else i=PEM_write_bio_X509(out,x);
sl@0
  1034
		}
sl@0
  1035
	else if (outformat == FORMAT_NETSCAPE)
sl@0
  1036
		{
sl@0
  1037
		ASN1_HEADER ah;
sl@0
  1038
		ASN1_OCTET_STRING os;
sl@0
  1039
sl@0
  1040
		os.data=(unsigned char *)NETSCAPE_CERT_HDR;
sl@0
  1041
		os.length=strlen(NETSCAPE_CERT_HDR);
sl@0
  1042
		ah.header= &os;
sl@0
  1043
		ah.data=(char *)x;
sl@0
  1044
		ah.meth=X509_asn1_meth();
sl@0
  1045
sl@0
  1046
		i=ASN1_i2d_bio_of(ASN1_HEADER,i2d_ASN1_HEADER,out,&ah);
sl@0
  1047
		}
sl@0
  1048
	else	{
sl@0
  1049
		BIO_printf(bio_err,"bad output format specified for outfile\n");
sl@0
  1050
		goto end;
sl@0
  1051
		}
sl@0
  1052
	if (!i)
sl@0
  1053
		{
sl@0
  1054
		BIO_printf(bio_err,"unable to write certificate\n");
sl@0
  1055
		ERR_print_errors(bio_err);
sl@0
  1056
		goto end;
sl@0
  1057
		}
sl@0
  1058
	ret=0;
sl@0
  1059
end:
sl@0
  1060
	if (need_rand)
sl@0
  1061
		app_RAND_write_file(NULL, bio_err);
sl@0
  1062
	OBJ_cleanup();
sl@0
  1063
	NCONF_free(extconf);
sl@0
  1064
	BIO_free_all(out);
sl@0
  1065
	BIO_free_all(STDout);
sl@0
  1066
	X509_STORE_free(ctx);
sl@0
  1067
	X509_REQ_free(req);
sl@0
  1068
	X509_free(x);
sl@0
  1069
	X509_free(xca);
sl@0
  1070
	EVP_PKEY_free(Upkey);
sl@0
  1071
	EVP_PKEY_free(CApkey);
sl@0
  1072
	X509_REQ_free(rq);
sl@0
  1073
	ASN1_INTEGER_free(sno);
sl@0
  1074
	sk_ASN1_OBJECT_pop_free(trust, ASN1_OBJECT_free);
sl@0
  1075
	sk_ASN1_OBJECT_pop_free(reject, ASN1_OBJECT_free);
sl@0
  1076
	if (passin) OPENSSL_free(passin);
sl@0
  1077
	apps_shutdown();
sl@0
  1078
	OPENSSL_EXIT(ret);
sl@0
  1079
	}
sl@0
  1080
sl@0
  1081
static ASN1_INTEGER *x509_load_serial(char *CAfile, char *serialfile, int create)
sl@0
  1082
	{
sl@0
  1083
	char *buf = NULL, *p;
sl@0
  1084
	ASN1_INTEGER *bs = NULL;
sl@0
  1085
	BIGNUM *serial = NULL;
sl@0
  1086
	size_t len;
sl@0
  1087
sl@0
  1088
	len = ((serialfile == NULL)
sl@0
  1089
		?(strlen(CAfile)+strlen(POSTFIX)+1)
sl@0
  1090
		:(strlen(serialfile)))+1;
sl@0
  1091
	buf=OPENSSL_malloc(len);
sl@0
  1092
	if (buf == NULL) { BIO_printf(bio_err,"out of mem\n"); goto end; }
sl@0
  1093
	if (serialfile == NULL)
sl@0
  1094
		{
sl@0
  1095
		BUF_strlcpy(buf,CAfile,len);
sl@0
  1096
		for (p=buf; *p; p++)
sl@0
  1097
			if (*p == '.')
sl@0
  1098
				{
sl@0
  1099
				*p='\0';
sl@0
  1100
				break;
sl@0
  1101
				}
sl@0
  1102
		BUF_strlcat(buf,POSTFIX,len);
sl@0
  1103
		}
sl@0
  1104
	else
sl@0
  1105
		BUF_strlcpy(buf,serialfile,len);
sl@0
  1106
sl@0
  1107
	serial = load_serial(buf, create, NULL);
sl@0
  1108
	if (serial == NULL) goto end;
sl@0
  1109
sl@0
  1110
	if (!BN_add_word(serial,1))
sl@0
  1111
		{ BIO_printf(bio_err,"add_word failure\n"); goto end; }
sl@0
  1112
sl@0
  1113
	if (!save_serial(buf, NULL, serial, &bs)) goto end;
sl@0
  1114
sl@0
  1115
 end:
sl@0
  1116
	if (buf) OPENSSL_free(buf);
sl@0
  1117
	BN_free(serial);
sl@0
  1118
	return bs;
sl@0
  1119
	}
sl@0
  1120
sl@0
  1121
static int x509_certify(X509_STORE *ctx, char *CAfile, const EVP_MD *digest,
sl@0
  1122
	     X509 *x, X509 *xca, EVP_PKEY *pkey, char *serialfile, int create,
sl@0
  1123
	     int days, int clrext, CONF *conf, char *section, ASN1_INTEGER *sno)
sl@0
  1124
	{
sl@0
  1125
	int ret=0;
sl@0
  1126
	ASN1_INTEGER *bs=NULL;
sl@0
  1127
	X509_STORE_CTX xsc;
sl@0
  1128
	EVP_PKEY *upkey;
sl@0
  1129
sl@0
  1130
	upkey = X509_get_pubkey(xca);
sl@0
  1131
	EVP_PKEY_copy_parameters(upkey,pkey);
sl@0
  1132
	EVP_PKEY_free(upkey);
sl@0
  1133
sl@0
  1134
	if(!X509_STORE_CTX_init(&xsc,ctx,x,NULL))
sl@0
  1135
		{
sl@0
  1136
		BIO_printf(bio_err,"Error initialising X509 store\n");
sl@0
  1137
		goto end;
sl@0
  1138
		}
sl@0
  1139
	if (sno) bs = sno;
sl@0
  1140
	else if (!(bs = x509_load_serial(CAfile, serialfile, create)))
sl@0
  1141
		goto end;
sl@0
  1142
sl@0
  1143
/*	if (!X509_STORE_add_cert(ctx,x)) goto end;*/
sl@0
  1144
sl@0
  1145
	/* NOTE: this certificate can/should be self signed, unless it was
sl@0
  1146
	 * a certificate request in which case it is not. */
sl@0
  1147
	X509_STORE_CTX_set_cert(&xsc,x);
sl@0
  1148
	if (!reqfile && !X509_verify_cert(&xsc))
sl@0
  1149
		goto end;
sl@0
  1150
sl@0
  1151
	if (!X509_check_private_key(xca,pkey))
sl@0
  1152
		{
sl@0
  1153
		BIO_printf(bio_err,"CA certificate and CA private key do not match\n");
sl@0
  1154
		goto end;
sl@0
  1155
		}
sl@0
  1156
sl@0
  1157
	if (!X509_set_issuer_name(x,X509_get_subject_name(xca))) goto end;
sl@0
  1158
	if (!X509_set_serialNumber(x,bs)) goto end;
sl@0
  1159
sl@0
  1160
	if (X509_gmtime_adj(X509_get_notBefore(x),0L) == NULL)
sl@0
  1161
		goto end;
sl@0
  1162
sl@0
  1163
	/* hardwired expired */
sl@0
  1164
	if (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)
sl@0
  1165
		goto end;
sl@0
  1166
sl@0
  1167
	if (clrext)
sl@0
  1168
		{
sl@0
  1169
		while (X509_get_ext_count(x) > 0) X509_delete_ext(x, 0);
sl@0
  1170
		}
sl@0
  1171
sl@0
  1172
	if (conf)
sl@0
  1173
		{
sl@0
  1174
		X509V3_CTX ctx2;
sl@0
  1175
		X509_set_version(x,2); /* version 3 certificate */
sl@0
  1176
                X509V3_set_ctx(&ctx2, xca, x, NULL, NULL, 0);
sl@0
  1177
                X509V3_set_nconf(&ctx2, conf);
sl@0
  1178
                if (!X509V3_EXT_add_nconf(conf, &ctx2, section, x)) goto end;
sl@0
  1179
		}
sl@0
  1180
sl@0
  1181
	if (!X509_sign(x,pkey,digest)) goto end;
sl@0
  1182
	ret=1;
sl@0
  1183
end:
sl@0
  1184
	X509_STORE_CTX_cleanup(&xsc);
sl@0
  1185
	if (!ret)
sl@0
  1186
		ERR_print_errors(bio_err);
sl@0
  1187
	if (!sno) ASN1_INTEGER_free(bs);
sl@0
  1188
	return ret;
sl@0
  1189
	}
sl@0
  1190
sl@0
  1191
static int MS_CALLBACK callb(int ok, X509_STORE_CTX *ctx)
sl@0
  1192
	{
sl@0
  1193
	int err;
sl@0
  1194
	X509 *err_cert;
sl@0
  1195
sl@0
  1196
	/* it is ok to use a self signed certificate
sl@0
  1197
	 * This case will catch both the initial ok == 0 and the
sl@0
  1198
	 * final ok == 1 calls to this function */
sl@0
  1199
	err=X509_STORE_CTX_get_error(ctx);
sl@0
  1200
	if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
sl@0
  1201
		return 1;
sl@0
  1202
sl@0
  1203
	/* BAD we should have gotten an error.  Normally if everything
sl@0
  1204
	 * worked X509_STORE_CTX_get_error(ctx) will still be set to
sl@0
  1205
	 * DEPTH_ZERO_SELF_.... */
sl@0
  1206
	if (ok)
sl@0
  1207
		{
sl@0
  1208
		BIO_printf(bio_err,"error with certificate to be certified - should be self signed\n");
sl@0
  1209
		return 0;
sl@0
  1210
		}
sl@0
  1211
	else
sl@0
  1212
		{
sl@0
  1213
		err_cert=X509_STORE_CTX_get_current_cert(ctx);
sl@0
  1214
		print_name(bio_err, NULL, X509_get_subject_name(err_cert),0);
sl@0
  1215
		BIO_printf(bio_err,"error with certificate - error %d at depth %d\n%s\n",
sl@0
  1216
			err,X509_STORE_CTX_get_error_depth(ctx),
sl@0
  1217
			X509_verify_cert_error_string(err));
sl@0
  1218
		return 1;
sl@0
  1219
		}
sl@0
  1220
	}
sl@0
  1221
sl@0
  1222
/* self sign */
sl@0
  1223
static int sign(X509 *x, EVP_PKEY *pkey, int days, int clrext, const EVP_MD *digest, 
sl@0
  1224
						CONF *conf, char *section)
sl@0
  1225
	{
sl@0
  1226
sl@0
  1227
	EVP_PKEY *pktmp;
sl@0
  1228
sl@0
  1229
	pktmp = X509_get_pubkey(x);
sl@0
  1230
	EVP_PKEY_copy_parameters(pktmp,pkey);
sl@0
  1231
	EVP_PKEY_save_parameters(pktmp,1);
sl@0
  1232
	EVP_PKEY_free(pktmp);
sl@0
  1233
sl@0
  1234
	if (!X509_set_issuer_name(x,X509_get_subject_name(x))) goto err;
sl@0
  1235
	if (X509_gmtime_adj(X509_get_notBefore(x),0) == NULL) goto err;
sl@0
  1236
sl@0
  1237
	/* Lets just make it 12:00am GMT, Jan 1 1970 */
sl@0
  1238
	/* memcpy(x->cert_info->validity->notBefore,"700101120000Z",13); */
sl@0
  1239
	/* 28 days to be certified */
sl@0
  1240
sl@0
  1241
	if (X509_gmtime_adj(X509_get_notAfter(x),(long)60*60*24*days) == NULL)
sl@0
  1242
		goto err;
sl@0
  1243
sl@0
  1244
	if (!X509_set_pubkey(x,pkey)) goto err;
sl@0
  1245
	if (clrext)
sl@0
  1246
		{
sl@0
  1247
		while (X509_get_ext_count(x) > 0) X509_delete_ext(x, 0);
sl@0
  1248
		}
sl@0
  1249
	if (conf)
sl@0
  1250
		{
sl@0
  1251
		X509V3_CTX ctx;
sl@0
  1252
		X509_set_version(x,2); /* version 3 certificate */
sl@0
  1253
                X509V3_set_ctx(&ctx, x, x, NULL, NULL, 0);
sl@0
  1254
                X509V3_set_nconf(&ctx, conf);
sl@0
  1255
                if (!X509V3_EXT_add_nconf(conf, &ctx, section, x)) goto err;
sl@0
  1256
		}
sl@0
  1257
	if (!X509_sign(x,pkey,digest)) goto err;
sl@0
  1258
	return 1;
sl@0
  1259
err:
sl@0
  1260
	ERR_print_errors(bio_err);
sl@0
  1261
	return 0;
sl@0
  1262
	}
sl@0
  1263
sl@0
  1264
static int purpose_print(BIO *bio, X509 *cert, X509_PURPOSE *pt)
sl@0
  1265
{
sl@0
  1266
	int id, i, idret;
sl@0
  1267
	char *pname;
sl@0
  1268
	id = X509_PURPOSE_get_id(pt);
sl@0
  1269
	pname = X509_PURPOSE_get0_name(pt);
sl@0
  1270
	for (i = 0; i < 2; i++)
sl@0
  1271
		{
sl@0
  1272
		idret = X509_check_purpose(cert, id, i);
sl@0
  1273
		BIO_printf(bio, "%s%s : ", pname, i ? " CA" : ""); 
sl@0
  1274
		if (idret == 1) BIO_printf(bio, "Yes\n");
sl@0
  1275
		else if (idret == 0) BIO_printf(bio, "No\n");
sl@0
  1276
		else BIO_printf(bio, "Yes (WARNING code=%d)\n", idret);
sl@0
  1277
		}
sl@0
  1278
	return 1;
sl@0
  1279
}