t_req.c revision 55714
155714Skris/* crypto/asn1/t_req.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 855714Skris * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1555714Skris * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 2255714Skris * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4055714Skris * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 5255714Skris * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include "cryptlib.h" 6155714Skris#include <openssl/buffer.h> 6255714Skris#include <openssl/bn.h> 6355714Skris#include <openssl/objects.h> 6455714Skris#include <openssl/x509.h> 6555714Skris 6655714Skris#ifndef NO_FP_API 6755714Skrisint X509_REQ_print_fp(FILE *fp, X509_REQ *x) 6855714Skris { 6955714Skris BIO *b; 7055714Skris int ret; 7155714Skris 7255714Skris if ((b=BIO_new(BIO_s_file())) == NULL) 7355714Skris { 7455714Skris X509err(X509_F_X509_REQ_PRINT_FP,ERR_R_BUF_LIB); 7555714Skris return(0); 7655714Skris } 7755714Skris BIO_set_fp(b,fp,BIO_NOCLOSE); 7855714Skris ret=X509_REQ_print(b, x); 7955714Skris BIO_free(b); 8055714Skris return(ret); 8155714Skris } 8255714Skris#endif 8355714Skris 8455714Skrisint X509_REQ_print(BIO *bp, X509_REQ *x) 8555714Skris { 8655714Skris unsigned long l; 8755714Skris int i,n; 8855714Skris char *s; 8955714Skris const char *neg; 9055714Skris X509_REQ_INFO *ri; 9155714Skris EVP_PKEY *pkey; 9255714Skris STACK_OF(X509_ATTRIBUTE) *sk; 9355714Skris char str[128]; 9455714Skris 9555714Skris ri=x->req_info; 9655714Skris sprintf(str,"Certificate Request:\n"); 9755714Skris if (BIO_puts(bp,str) <= 0) goto err; 9855714Skris sprintf(str,"%4sData:\n",""); 9955714Skris if (BIO_puts(bp,str) <= 0) goto err; 10055714Skris 10155714Skris neg=(ri->version->type == V_ASN1_NEG_INTEGER)?"-":""; 10255714Skris l=0; 10355714Skris for (i=0; i<ri->version->length; i++) 10455714Skris { l<<=8; l+=ri->version->data[i]; } 10555714Skris sprintf(str,"%8sVersion: %s%lu (%s0x%lx)\n","",neg,l,neg,l); 10655714Skris if (BIO_puts(bp,str) <= 0) goto err; 10755714Skris sprintf(str,"%8sSubject: ",""); 10855714Skris if (BIO_puts(bp,str) <= 0) goto err; 10955714Skris 11055714Skris X509_NAME_print(bp,ri->subject,16); 11155714Skris sprintf(str,"\n%8sSubject Public Key Info:\n",""); 11255714Skris if (BIO_puts(bp,str) <= 0) goto err; 11355714Skris i=OBJ_obj2nid(ri->pubkey->algor->algorithm); 11455714Skris sprintf(str,"%12sPublic Key Algorithm: %s\n","", 11555714Skris (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)); 11655714Skris if (BIO_puts(bp,str) <= 0) goto err; 11755714Skris 11855714Skris pkey=X509_REQ_get_pubkey(x); 11955714Skris#ifndef NO_RSA 12055714Skris if (pkey->type == EVP_PKEY_RSA) 12155714Skris { 12255714Skris BIO_printf(bp,"%12sRSA Public Key: (%d bit)\n","", 12355714Skris BN_num_bits(pkey->pkey.rsa->n)); 12455714Skris RSA_print(bp,pkey->pkey.rsa,16); 12555714Skris } 12655714Skris else 12755714Skris#endif 12855714Skris#ifndef NO_DSA 12955714Skris if (pkey->type == EVP_PKEY_DSA) 13055714Skris { 13155714Skris BIO_printf(bp,"%12sDSA Public Key:\n",""); 13255714Skris DSA_print(bp,pkey->pkey.dsa,16); 13355714Skris } 13455714Skris else 13555714Skris#endif 13655714Skris BIO_printf(bp,"%12sUnknown Public Key:\n",""); 13755714Skris 13855714Skris EVP_PKEY_free(pkey); 13955714Skris 14055714Skris /* may not be */ 14155714Skris sprintf(str,"%8sAttributes:\n",""); 14255714Skris if (BIO_puts(bp,str) <= 0) goto err; 14355714Skris 14455714Skris sk=x->req_info->attributes; 14555714Skris if ((sk == NULL) || (sk_X509_ATTRIBUTE_num(sk) == 0)) 14655714Skris { 14755714Skris if (!x->req_info->req_kludge) 14855714Skris { 14955714Skris sprintf(str,"%12sa0:00\n",""); 15055714Skris if (BIO_puts(bp,str) <= 0) goto err; 15155714Skris } 15255714Skris } 15355714Skris else 15455714Skris { 15555714Skris for (i=0; i<sk_X509_ATTRIBUTE_num(sk); i++) 15655714Skris { 15755714Skris ASN1_TYPE *at; 15855714Skris X509_ATTRIBUTE *a; 15955714Skris ASN1_BIT_STRING *bs=NULL; 16055714Skris ASN1_TYPE *t; 16155714Skris int j,type=0,count=1,ii=0; 16255714Skris 16355714Skris a=sk_X509_ATTRIBUTE_value(sk,i); 16455714Skris sprintf(str,"%12s",""); 16555714Skris if (BIO_puts(bp,str) <= 0) goto err; 16655714Skris if ((j=i2a_ASN1_OBJECT(bp,a->object)) > 0) 16755714Skris { 16855714Skris if (a->set) 16955714Skris { 17055714Skris ii=0; 17155714Skris count=sk_ASN1_TYPE_num(a->value.set); 17255714Skrisget_next: 17355714Skris at=sk_ASN1_TYPE_value(a->value.set,ii); 17455714Skris type=at->type; 17555714Skris bs=at->value.asn1_string; 17655714Skris } 17755714Skris else 17855714Skris { 17955714Skris t=a->value.single; 18055714Skris type=t->type; 18155714Skris bs=t->value.bit_string; 18255714Skris } 18355714Skris } 18455714Skris for (j=25-j; j>0; j--) 18555714Skris if (BIO_write(bp," ",1) != 1) goto err; 18655714Skris if (BIO_puts(bp,":") <= 0) goto err; 18755714Skris if ( (type == V_ASN1_PRINTABLESTRING) || 18855714Skris (type == V_ASN1_T61STRING) || 18955714Skris (type == V_ASN1_IA5STRING)) 19055714Skris { 19155714Skris if (BIO_write(bp,(char *)bs->data,bs->length) 19255714Skris != bs->length) 19355714Skris goto err; 19455714Skris BIO_puts(bp,"\n"); 19555714Skris } 19655714Skris else 19755714Skris { 19855714Skris BIO_puts(bp,"unable to print attribute\n"); 19955714Skris } 20055714Skris if (++ii < count) goto get_next; 20155714Skris } 20255714Skris } 20355714Skris 20455714Skris i=OBJ_obj2nid(x->sig_alg->algorithm); 20555714Skris sprintf(str,"%4sSignature Algorithm: %s","", 20655714Skris (i == NID_undef)?"UNKNOWN":OBJ_nid2ln(i)); 20755714Skris if (BIO_puts(bp,str) <= 0) goto err; 20855714Skris 20955714Skris n=x->signature->length; 21055714Skris s=(char *)x->signature->data; 21155714Skris for (i=0; i<n; i++) 21255714Skris { 21355714Skris if ((i%18) == 0) 21455714Skris { 21555714Skris sprintf(str,"\n%8s",""); 21655714Skris if (BIO_puts(bp,str) <= 0) goto err; 21755714Skris } 21855714Skris sprintf(str,"%02x%s",(unsigned char)s[i],((i+1) == n)?"":":"); 21955714Skris if (BIO_puts(bp,str) <= 0) goto err; 22055714Skris } 22155714Skris if (BIO_puts(bp,"\n") <= 0) goto err; 22255714Skris return(1); 22355714Skriserr: 22455714Skris X509err(X509_F_X509_REQ_PRINT,ERR_R_BUF_LIB); 22555714Skris return(0); 22655714Skris } 227