os/ossrv/ssl/libcrypto/src/crypto/asn1/t_req.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* crypto/asn1/t_req.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 <stdio.h>
sl@0
    60
#include "cryptlib.h"
sl@0
    61
#include <openssl/buffer.h>
sl@0
    62
#include <openssl/bn.h>
sl@0
    63
#include <openssl/objects.h>
sl@0
    64
#include <openssl/x509.h>
sl@0
    65
#include <openssl/x509v3.h>
sl@0
    66
#ifndef OPENSSL_NO_RSA
sl@0
    67
#include <openssl/rsa.h>
sl@0
    68
#endif
sl@0
    69
#ifndef OPENSSL_NO_DSA
sl@0
    70
#include <openssl/dsa.h>
sl@0
    71
#endif
sl@0
    72
sl@0
    73
#ifndef OPENSSL_NO_FP_API
sl@0
    74
EXPORT_C int X509_REQ_print_fp(FILE *fp, X509_REQ *x)
sl@0
    75
        {
sl@0
    76
        BIO *b;
sl@0
    77
        int ret;
sl@0
    78
sl@0
    79
        if ((b=BIO_new(BIO_s_file())) == NULL)
sl@0
    80
		{
sl@0
    81
		X509err(X509_F_X509_REQ_PRINT_FP,ERR_R_BUF_LIB);
sl@0
    82
                return(0);
sl@0
    83
		}
sl@0
    84
        BIO_set_fp(b,fp,BIO_NOCLOSE);
sl@0
    85
        ret=X509_REQ_print(b, x);
sl@0
    86
        BIO_free(b);
sl@0
    87
        return(ret);
sl@0
    88
        }
sl@0
    89
#endif
sl@0
    90
sl@0
    91
EXPORT_C int X509_REQ_print_ex(BIO *bp, X509_REQ *x, unsigned long nmflags, unsigned long cflag)
sl@0
    92
	{
sl@0
    93
	unsigned long l;
sl@0
    94
	int i;
sl@0
    95
	const char *neg;
sl@0
    96
	X509_REQ_INFO *ri;
sl@0
    97
	EVP_PKEY *pkey;
sl@0
    98
	STACK_OF(X509_ATTRIBUTE) *sk;
sl@0
    99
	STACK_OF(X509_EXTENSION) *exts;
sl@0
   100
	char mlch = ' ';
sl@0
   101
	int nmindent = 0;
sl@0
   102
sl@0
   103
	if((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
sl@0
   104
		mlch = '\n';
sl@0
   105
		nmindent = 12;
sl@0
   106
	}
sl@0
   107
sl@0
   108
	if(nmflags == X509_FLAG_COMPAT)
sl@0
   109
		nmindent = 16;
sl@0
   110
sl@0
   111
sl@0
   112
	ri=x->req_info;
sl@0
   113
	if(!(cflag & X509_FLAG_NO_HEADER))
sl@0
   114
		{
sl@0
   115
		if (BIO_write(bp,"Certificate Request:\n",21) <= 0) goto err;
sl@0
   116
		if (BIO_write(bp,"    Data:\n",10) <= 0) goto err;
sl@0
   117
		}
sl@0
   118
	if(!(cflag & X509_FLAG_NO_VERSION))
sl@0
   119
		{
sl@0
   120
		neg=(ri->version->type == V_ASN1_NEG_INTEGER)?"-":"";
sl@0
   121
		l=0;
sl@0
   122
		for (i=0; i<ri->version->length; i++)
sl@0
   123
			{ l<<=8; l+=ri->version->data[i]; }
sl@0
   124
		if(BIO_printf(bp,"%8sVersion: %s%lu (%s0x%lx)\n","",neg,l,neg,
sl@0
   125
			      l) <= 0)
sl@0
   126
		    goto err;
sl@0
   127
		}
sl@0
   128
        if(!(cflag & X509_FLAG_NO_SUBJECT))
sl@0
   129
                {
sl@0
   130
                if (BIO_printf(bp,"        Subject:%c",mlch) <= 0) goto err;
sl@0
   131
                if (X509_NAME_print_ex(bp,ri->subject,nmindent, nmflags) < 0) goto err;
sl@0
   132
                if (BIO_write(bp,"\n",1) <= 0) goto err;
sl@0
   133
                }
sl@0
   134
	if(!(cflag & X509_FLAG_NO_PUBKEY))
sl@0
   135
		{
sl@0
   136
		if (BIO_write(bp,"        Subject Public Key Info:\n",33) <= 0)
sl@0
   137
			goto err;
sl@0
   138
		if (BIO_printf(bp,"%12sPublic Key Algorithm: ","") <= 0)
sl@0
   139
			goto err;
sl@0
   140
		if (i2a_ASN1_OBJECT(bp, ri->pubkey->algor->algorithm) <= 0)
sl@0
   141
			goto err;
sl@0
   142
		if (BIO_puts(bp, "\n") <= 0)
sl@0
   143
			goto err;
sl@0
   144
sl@0
   145
		pkey=X509_REQ_get_pubkey(x);
sl@0
   146
		if (pkey == NULL)
sl@0
   147
			{
sl@0
   148
			BIO_printf(bp,"%12sUnable to load Public Key\n","");
sl@0
   149
			ERR_print_errors(bp);
sl@0
   150
			}
sl@0
   151
		else
sl@0
   152
#ifndef OPENSSL_NO_RSA
sl@0
   153
		if (pkey->type == EVP_PKEY_RSA)
sl@0
   154
			{
sl@0
   155
			BIO_printf(bp,"%12sRSA Public Key: (%d bit)\n","",
sl@0
   156
			BN_num_bits(pkey->pkey.rsa->n));
sl@0
   157
			RSA_print(bp,pkey->pkey.rsa,16);
sl@0
   158
			}
sl@0
   159
		else
sl@0
   160
#endif
sl@0
   161
#ifndef OPENSSL_NO_DSA
sl@0
   162
		if (pkey->type == EVP_PKEY_DSA)
sl@0
   163
			{
sl@0
   164
			BIO_printf(bp,"%12sDSA Public Key:\n","");
sl@0
   165
			DSA_print(bp,pkey->pkey.dsa,16);
sl@0
   166
			}
sl@0
   167
		else
sl@0
   168
#endif
sl@0
   169
#ifndef OPENSSL_NO_EC
sl@0
   170
		if (pkey->type == EVP_PKEY_EC)
sl@0
   171
		{
sl@0
   172
			BIO_printf(bp, "%12sEC Public Key: \n","");
sl@0
   173
			EC_KEY_print(bp, pkey->pkey.ec, 16);
sl@0
   174
		}
sl@0
   175
	else
sl@0
   176
#endif
sl@0
   177
			BIO_printf(bp,"%12sUnknown Public Key:\n","");
sl@0
   178
sl@0
   179
		EVP_PKEY_free(pkey);
sl@0
   180
		}
sl@0
   181
sl@0
   182
	if(!(cflag & X509_FLAG_NO_ATTRIBUTES))
sl@0
   183
		{
sl@0
   184
		/* may not be */
sl@0
   185
		if(BIO_printf(bp,"%8sAttributes:\n","") <= 0)
sl@0
   186
		    goto err;
sl@0
   187
sl@0
   188
		sk=x->req_info->attributes;
sl@0
   189
		if (sk_X509_ATTRIBUTE_num(sk) == 0)
sl@0
   190
			{
sl@0
   191
			if(BIO_printf(bp,"%12sa0:00\n","") <= 0)
sl@0
   192
			    goto err;
sl@0
   193
			}
sl@0
   194
		else
sl@0
   195
			{
sl@0
   196
			for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++)
sl@0
   197
				{
sl@0
   198
				ASN1_TYPE *at;
sl@0
   199
				X509_ATTRIBUTE *a;
sl@0
   200
				ASN1_BIT_STRING *bs=NULL;
sl@0
   201
				ASN1_TYPE *t;
sl@0
   202
				int j,type=0,count=1,ii=0;
sl@0
   203
sl@0
   204
				a=sk_X509_ATTRIBUTE_value(sk,i);
sl@0
   205
				if(X509_REQ_extension_nid(OBJ_obj2nid(a->object)))
sl@0
   206
									continue;
sl@0
   207
				if(BIO_printf(bp,"%12s","") <= 0)
sl@0
   208
				    goto err;
sl@0
   209
				if ((j=i2a_ASN1_OBJECT(bp,a->object)) > 0)
sl@0
   210
				{
sl@0
   211
				if (a->single)
sl@0
   212
					{
sl@0
   213
					t=a->value.single;
sl@0
   214
					type=t->type;
sl@0
   215
					bs=t->value.bit_string;
sl@0
   216
					}
sl@0
   217
				else
sl@0
   218
					{
sl@0
   219
					ii=0;
sl@0
   220
					count=sk_ASN1_TYPE_num(a->value.set);
sl@0
   221
get_next:
sl@0
   222
					at=sk_ASN1_TYPE_value(a->value.set,ii);
sl@0
   223
					type=at->type;
sl@0
   224
					bs=at->value.asn1_string;
sl@0
   225
					}
sl@0
   226
				}
sl@0
   227
				for (j=25-j; j>0; j--)
sl@0
   228
					if (BIO_write(bp," ",1) != 1) goto err;
sl@0
   229
				if (BIO_puts(bp,":") <= 0) goto err;
sl@0
   230
				if (	(type == V_ASN1_PRINTABLESTRING) ||
sl@0
   231
					(type == V_ASN1_T61STRING) ||
sl@0
   232
					(type == V_ASN1_IA5STRING))
sl@0
   233
					{
sl@0
   234
					if (BIO_write(bp,(char *)bs->data,bs->length)
sl@0
   235
						!= bs->length)
sl@0
   236
						goto err;
sl@0
   237
					BIO_puts(bp,"\n");
sl@0
   238
					}
sl@0
   239
				else
sl@0
   240
					{
sl@0
   241
					BIO_puts(bp,"unable to print attribute\n");
sl@0
   242
					}
sl@0
   243
				if (++ii < count) goto get_next;
sl@0
   244
				}
sl@0
   245
			}
sl@0
   246
		}
sl@0
   247
	if(!(cflag & X509_FLAG_NO_EXTENSIONS))
sl@0
   248
		{
sl@0
   249
		exts = X509_REQ_get_extensions(x);
sl@0
   250
		if(exts)
sl@0
   251
			{
sl@0
   252
			BIO_printf(bp,"%8sRequested Extensions:\n","");
sl@0
   253
			for (i=0; i<sk_X509_EXTENSION_num(exts); i++)
sl@0
   254
				{
sl@0
   255
				ASN1_OBJECT *obj;
sl@0
   256
				X509_EXTENSION *ex;
sl@0
   257
				int j;
sl@0
   258
				ex=sk_X509_EXTENSION_value(exts, i);
sl@0
   259
				if (BIO_printf(bp,"%12s","") <= 0) goto err;
sl@0
   260
				obj=X509_EXTENSION_get_object(ex);
sl@0
   261
				i2a_ASN1_OBJECT(bp,obj);
sl@0
   262
				j=X509_EXTENSION_get_critical(ex);
sl@0
   263
				if (BIO_printf(bp,": %s\n",j?"critical":"") <= 0)
sl@0
   264
					goto err;
sl@0
   265
				if(!X509V3_EXT_print(bp, ex, cflag, 16))
sl@0
   266
					{
sl@0
   267
					BIO_printf(bp, "%16s", "");
sl@0
   268
					M_ASN1_OCTET_STRING_print(bp,ex->value);
sl@0
   269
					}
sl@0
   270
				if (BIO_write(bp,"\n",1) <= 0) goto err;
sl@0
   271
				}
sl@0
   272
			sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
sl@0
   273
			}
sl@0
   274
		}
sl@0
   275
sl@0
   276
	if(!(cflag & X509_FLAG_NO_SIGDUMP))
sl@0
   277
		{
sl@0
   278
		if(!X509_signature_print(bp, x->sig_alg, x->signature)) goto err;
sl@0
   279
		}
sl@0
   280
sl@0
   281
	return(1);
sl@0
   282
err:
sl@0
   283
	X509err(X509_F_X509_REQ_PRINT_EX,ERR_R_BUF_LIB);
sl@0
   284
	return(0);
sl@0
   285
	}
sl@0
   286
sl@0
   287
EXPORT_C int X509_REQ_print(BIO *bp, X509_REQ *x)
sl@0
   288
	{
sl@0
   289
	return X509_REQ_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
sl@0
   290
	}