1*0Sstevel@tonic-gate /* crypto/asn1/x_pubkey.c */ 2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3*0Sstevel@tonic-gate * All rights reserved. 4*0Sstevel@tonic-gate * 5*0Sstevel@tonic-gate * This package is an SSL implementation written 6*0Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com). 7*0Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as 10*0Sstevel@tonic-gate * the following conditions are aheared to. The following conditions 11*0Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA, 12*0Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13*0Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms 14*0Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15*0Sstevel@tonic-gate * 16*0Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in 17*0Sstevel@tonic-gate * the code are not to be removed. 18*0Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution 19*0Sstevel@tonic-gate * as the author of the parts of the library used. 20*0Sstevel@tonic-gate * This can be in the form of a textual message at program startup or 21*0Sstevel@tonic-gate * in documentation (online or textual) provided with the package. 22*0Sstevel@tonic-gate * 23*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 24*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 25*0Sstevel@tonic-gate * are met: 26*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright 27*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 28*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 29*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 30*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 31*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 32*0Sstevel@tonic-gate * must display the following acknowledgement: 33*0Sstevel@tonic-gate * "This product includes cryptographic software written by 34*0Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)" 35*0Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library 36*0Sstevel@tonic-gate * being used are not cryptographic related :-). 37*0Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from 38*0Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement: 39*0Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40*0Sstevel@tonic-gate * 41*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51*0Sstevel@tonic-gate * SUCH DAMAGE. 52*0Sstevel@tonic-gate * 53*0Sstevel@tonic-gate * The licence and distribution terms for any publically available version or 54*0Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be 55*0Sstevel@tonic-gate * copied and put under another distribution licence 56*0Sstevel@tonic-gate * [including the GNU Public Licence.] 57*0Sstevel@tonic-gate */ 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate #include <stdio.h> 60*0Sstevel@tonic-gate #include "cryptlib.h" 61*0Sstevel@tonic-gate #include <openssl/asn1t.h> 62*0Sstevel@tonic-gate #include <openssl/x509.h> 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate /* Minor tweak to operation: free up EVP_PKEY */ 65*0Sstevel@tonic-gate static int pubkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it) 66*0Sstevel@tonic-gate { 67*0Sstevel@tonic-gate if(operation == ASN1_OP_FREE_POST) { 68*0Sstevel@tonic-gate X509_PUBKEY *pubkey = (X509_PUBKEY *)*pval; 69*0Sstevel@tonic-gate EVP_PKEY_free(pubkey->pkey); 70*0Sstevel@tonic-gate } 71*0Sstevel@tonic-gate return 1; 72*0Sstevel@tonic-gate } 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gate ASN1_SEQUENCE_cb(X509_PUBKEY, pubkey_cb) = { 75*0Sstevel@tonic-gate ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR), 76*0Sstevel@tonic-gate ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING) 77*0Sstevel@tonic-gate } ASN1_SEQUENCE_END_cb(X509_PUBKEY, X509_PUBKEY) 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate IMPLEMENT_ASN1_FUNCTIONS(X509_PUBKEY) 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey) 82*0Sstevel@tonic-gate { 83*0Sstevel@tonic-gate int ok=0; 84*0Sstevel@tonic-gate X509_PUBKEY *pk; 85*0Sstevel@tonic-gate X509_ALGOR *a; 86*0Sstevel@tonic-gate ASN1_OBJECT *o; 87*0Sstevel@tonic-gate unsigned char *s,*p = NULL; 88*0Sstevel@tonic-gate int i; 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate if (x == NULL) return(0); 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate if ((pk=X509_PUBKEY_new()) == NULL) goto err; 93*0Sstevel@tonic-gate a=pk->algor; 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate /* set the algorithm id */ 96*0Sstevel@tonic-gate if ((o=OBJ_nid2obj(pkey->type)) == NULL) goto err; 97*0Sstevel@tonic-gate ASN1_OBJECT_free(a->algorithm); 98*0Sstevel@tonic-gate a->algorithm=o; 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate /* Set the parameter list */ 101*0Sstevel@tonic-gate if (!pkey->save_parameters || (pkey->type == EVP_PKEY_RSA)) 102*0Sstevel@tonic-gate { 103*0Sstevel@tonic-gate if ((a->parameter == NULL) || 104*0Sstevel@tonic-gate (a->parameter->type != V_ASN1_NULL)) 105*0Sstevel@tonic-gate { 106*0Sstevel@tonic-gate ASN1_TYPE_free(a->parameter); 107*0Sstevel@tonic-gate a->parameter=ASN1_TYPE_new(); 108*0Sstevel@tonic-gate a->parameter->type=V_ASN1_NULL; 109*0Sstevel@tonic-gate } 110*0Sstevel@tonic-gate } 111*0Sstevel@tonic-gate else 112*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 113*0Sstevel@tonic-gate if (pkey->type == EVP_PKEY_DSA) 114*0Sstevel@tonic-gate { 115*0Sstevel@tonic-gate unsigned char *pp; 116*0Sstevel@tonic-gate DSA *dsa; 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate dsa=pkey->pkey.dsa; 119*0Sstevel@tonic-gate dsa->write_params=0; 120*0Sstevel@tonic-gate ASN1_TYPE_free(a->parameter); 121*0Sstevel@tonic-gate i=i2d_DSAparams(dsa,NULL); 122*0Sstevel@tonic-gate if ((p=(unsigned char *)OPENSSL_malloc(i)) == NULL) goto err; 123*0Sstevel@tonic-gate pp=p; 124*0Sstevel@tonic-gate i2d_DSAparams(dsa,&pp); 125*0Sstevel@tonic-gate a->parameter=ASN1_TYPE_new(); 126*0Sstevel@tonic-gate a->parameter->type=V_ASN1_SEQUENCE; 127*0Sstevel@tonic-gate a->parameter->value.sequence=ASN1_STRING_new(); 128*0Sstevel@tonic-gate ASN1_STRING_set(a->parameter->value.sequence,p,i); 129*0Sstevel@tonic-gate OPENSSL_free(p); 130*0Sstevel@tonic-gate } 131*0Sstevel@tonic-gate else 132*0Sstevel@tonic-gate #endif 133*0Sstevel@tonic-gate { 134*0Sstevel@tonic-gate X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM); 135*0Sstevel@tonic-gate goto err; 136*0Sstevel@tonic-gate } 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate if ((i=i2d_PublicKey(pkey,NULL)) <= 0) goto err; 139*0Sstevel@tonic-gate if ((s=(unsigned char *)OPENSSL_malloc(i+1)) == NULL) 140*0Sstevel@tonic-gate { 141*0Sstevel@tonic-gate X509err(X509_F_X509_PUBKEY_SET,ERR_R_MALLOC_FAILURE); 142*0Sstevel@tonic-gate goto err; 143*0Sstevel@tonic-gate } 144*0Sstevel@tonic-gate p=s; 145*0Sstevel@tonic-gate i2d_PublicKey(pkey,&p); 146*0Sstevel@tonic-gate if (!M_ASN1_BIT_STRING_set(pk->public_key,s,i)) goto err; 147*0Sstevel@tonic-gate /* Set number of unused bits to zero */ 148*0Sstevel@tonic-gate pk->public_key->flags&= ~(ASN1_STRING_FLAG_BITS_LEFT|0x07); 149*0Sstevel@tonic-gate pk->public_key->flags|=ASN1_STRING_FLAG_BITS_LEFT; 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate OPENSSL_free(s); 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate #if 0 154*0Sstevel@tonic-gate CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY); 155*0Sstevel@tonic-gate pk->pkey=pkey; 156*0Sstevel@tonic-gate #endif 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate if (*x != NULL) 159*0Sstevel@tonic-gate X509_PUBKEY_free(*x); 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate *x=pk; 162*0Sstevel@tonic-gate pk=NULL; 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gate ok=1; 165*0Sstevel@tonic-gate err: 166*0Sstevel@tonic-gate if (pk != NULL) X509_PUBKEY_free(pk); 167*0Sstevel@tonic-gate return(ok); 168*0Sstevel@tonic-gate } 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate EVP_PKEY *X509_PUBKEY_get(X509_PUBKEY *key) 171*0Sstevel@tonic-gate { 172*0Sstevel@tonic-gate EVP_PKEY *ret=NULL; 173*0Sstevel@tonic-gate long j; 174*0Sstevel@tonic-gate int type; 175*0Sstevel@tonic-gate unsigned char *p; 176*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 177*0Sstevel@tonic-gate const unsigned char *cp; 178*0Sstevel@tonic-gate X509_ALGOR *a; 179*0Sstevel@tonic-gate #endif 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate if (key == NULL) goto err; 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate if (key->pkey != NULL) 184*0Sstevel@tonic-gate { 185*0Sstevel@tonic-gate CRYPTO_add(&key->pkey->references,1,CRYPTO_LOCK_EVP_PKEY); 186*0Sstevel@tonic-gate return(key->pkey); 187*0Sstevel@tonic-gate } 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate if (key->public_key == NULL) goto err; 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate type=OBJ_obj2nid(key->algor->algorithm); 192*0Sstevel@tonic-gate p=key->public_key->data; 193*0Sstevel@tonic-gate j=key->public_key->length; 194*0Sstevel@tonic-gate if ((ret=d2i_PublicKey(type,NULL,&p,(long)j)) == NULL) 195*0Sstevel@tonic-gate { 196*0Sstevel@tonic-gate X509err(X509_F_X509_PUBKEY_GET,X509_R_ERR_ASN1_LIB); 197*0Sstevel@tonic-gate goto err; 198*0Sstevel@tonic-gate } 199*0Sstevel@tonic-gate ret->save_parameters=0; 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 202*0Sstevel@tonic-gate a=key->algor; 203*0Sstevel@tonic-gate if (ret->type == EVP_PKEY_DSA) 204*0Sstevel@tonic-gate { 205*0Sstevel@tonic-gate if (a->parameter && (a->parameter->type == V_ASN1_SEQUENCE)) 206*0Sstevel@tonic-gate { 207*0Sstevel@tonic-gate ret->pkey.dsa->write_params=0; 208*0Sstevel@tonic-gate cp=p=a->parameter->value.sequence->data; 209*0Sstevel@tonic-gate j=a->parameter->value.sequence->length; 210*0Sstevel@tonic-gate if (!d2i_DSAparams(&ret->pkey.dsa,&cp,(long)j)) 211*0Sstevel@tonic-gate goto err; 212*0Sstevel@tonic-gate } 213*0Sstevel@tonic-gate ret->save_parameters=1; 214*0Sstevel@tonic-gate } 215*0Sstevel@tonic-gate #endif 216*0Sstevel@tonic-gate key->pkey=ret; 217*0Sstevel@tonic-gate CRYPTO_add(&ret->references,1,CRYPTO_LOCK_EVP_PKEY); 218*0Sstevel@tonic-gate return(ret); 219*0Sstevel@tonic-gate err: 220*0Sstevel@tonic-gate if (ret != NULL) 221*0Sstevel@tonic-gate EVP_PKEY_free(ret); 222*0Sstevel@tonic-gate return(NULL); 223*0Sstevel@tonic-gate } 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate /* Now two pseudo ASN1 routines that take an EVP_PKEY structure 226*0Sstevel@tonic-gate * and encode or decode as X509_PUBKEY 227*0Sstevel@tonic-gate */ 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate EVP_PKEY *d2i_PUBKEY(EVP_PKEY **a, unsigned char **pp, 230*0Sstevel@tonic-gate long length) 231*0Sstevel@tonic-gate { 232*0Sstevel@tonic-gate X509_PUBKEY *xpk; 233*0Sstevel@tonic-gate EVP_PKEY *pktmp; 234*0Sstevel@tonic-gate xpk = d2i_X509_PUBKEY(NULL, pp, length); 235*0Sstevel@tonic-gate if(!xpk) return NULL; 236*0Sstevel@tonic-gate pktmp = X509_PUBKEY_get(xpk); 237*0Sstevel@tonic-gate X509_PUBKEY_free(xpk); 238*0Sstevel@tonic-gate if(!pktmp) return NULL; 239*0Sstevel@tonic-gate if(a) { 240*0Sstevel@tonic-gate EVP_PKEY_free(*a); 241*0Sstevel@tonic-gate *a = pktmp; 242*0Sstevel@tonic-gate } 243*0Sstevel@tonic-gate return pktmp; 244*0Sstevel@tonic-gate } 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate int i2d_PUBKEY(EVP_PKEY *a, unsigned char **pp) 247*0Sstevel@tonic-gate { 248*0Sstevel@tonic-gate X509_PUBKEY *xpk=NULL; 249*0Sstevel@tonic-gate int ret; 250*0Sstevel@tonic-gate if(!a) return 0; 251*0Sstevel@tonic-gate if(!X509_PUBKEY_set(&xpk, a)) return 0; 252*0Sstevel@tonic-gate ret = i2d_X509_PUBKEY(xpk, pp); 253*0Sstevel@tonic-gate X509_PUBKEY_free(xpk); 254*0Sstevel@tonic-gate return ret; 255*0Sstevel@tonic-gate } 256*0Sstevel@tonic-gate 257*0Sstevel@tonic-gate /* The following are equivalents but which return RSA and DSA 258*0Sstevel@tonic-gate * keys 259*0Sstevel@tonic-gate */ 260*0Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 261*0Sstevel@tonic-gate RSA *d2i_RSA_PUBKEY(RSA **a, unsigned char **pp, 262*0Sstevel@tonic-gate long length) 263*0Sstevel@tonic-gate { 264*0Sstevel@tonic-gate EVP_PKEY *pkey; 265*0Sstevel@tonic-gate RSA *key; 266*0Sstevel@tonic-gate unsigned char *q; 267*0Sstevel@tonic-gate q = *pp; 268*0Sstevel@tonic-gate pkey = d2i_PUBKEY(NULL, &q, length); 269*0Sstevel@tonic-gate if(!pkey) return NULL; 270*0Sstevel@tonic-gate key = EVP_PKEY_get1_RSA(pkey); 271*0Sstevel@tonic-gate EVP_PKEY_free(pkey); 272*0Sstevel@tonic-gate if(!key) return NULL; 273*0Sstevel@tonic-gate *pp = q; 274*0Sstevel@tonic-gate if(a) { 275*0Sstevel@tonic-gate RSA_free(*a); 276*0Sstevel@tonic-gate *a = key; 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate return key; 279*0Sstevel@tonic-gate } 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gate int i2d_RSA_PUBKEY(RSA *a, unsigned char **pp) 282*0Sstevel@tonic-gate { 283*0Sstevel@tonic-gate EVP_PKEY *pktmp; 284*0Sstevel@tonic-gate int ret; 285*0Sstevel@tonic-gate if(!a) return 0; 286*0Sstevel@tonic-gate pktmp = EVP_PKEY_new(); 287*0Sstevel@tonic-gate if(!pktmp) { 288*0Sstevel@tonic-gate ASN1err(ASN1_F_I2D_RSA_PUBKEY, ERR_R_MALLOC_FAILURE); 289*0Sstevel@tonic-gate return 0; 290*0Sstevel@tonic-gate } 291*0Sstevel@tonic-gate EVP_PKEY_set1_RSA(pktmp, a); 292*0Sstevel@tonic-gate ret = i2d_PUBKEY(pktmp, pp); 293*0Sstevel@tonic-gate EVP_PKEY_free(pktmp); 294*0Sstevel@tonic-gate return ret; 295*0Sstevel@tonic-gate } 296*0Sstevel@tonic-gate #endif 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 299*0Sstevel@tonic-gate DSA *d2i_DSA_PUBKEY(DSA **a, unsigned char **pp, 300*0Sstevel@tonic-gate long length) 301*0Sstevel@tonic-gate { 302*0Sstevel@tonic-gate EVP_PKEY *pkey; 303*0Sstevel@tonic-gate DSA *key; 304*0Sstevel@tonic-gate unsigned char *q; 305*0Sstevel@tonic-gate q = *pp; 306*0Sstevel@tonic-gate pkey = d2i_PUBKEY(NULL, &q, length); 307*0Sstevel@tonic-gate if(!pkey) return NULL; 308*0Sstevel@tonic-gate key = EVP_PKEY_get1_DSA(pkey); 309*0Sstevel@tonic-gate EVP_PKEY_free(pkey); 310*0Sstevel@tonic-gate if(!key) return NULL; 311*0Sstevel@tonic-gate *pp = q; 312*0Sstevel@tonic-gate if(a) { 313*0Sstevel@tonic-gate DSA_free(*a); 314*0Sstevel@tonic-gate *a = key; 315*0Sstevel@tonic-gate } 316*0Sstevel@tonic-gate return key; 317*0Sstevel@tonic-gate } 318*0Sstevel@tonic-gate 319*0Sstevel@tonic-gate int i2d_DSA_PUBKEY(DSA *a, unsigned char **pp) 320*0Sstevel@tonic-gate { 321*0Sstevel@tonic-gate EVP_PKEY *pktmp; 322*0Sstevel@tonic-gate int ret; 323*0Sstevel@tonic-gate if(!a) return 0; 324*0Sstevel@tonic-gate pktmp = EVP_PKEY_new(); 325*0Sstevel@tonic-gate if(!pktmp) { 326*0Sstevel@tonic-gate ASN1err(ASN1_F_I2D_DSA_PUBKEY, ERR_R_MALLOC_FAILURE); 327*0Sstevel@tonic-gate return 0; 328*0Sstevel@tonic-gate } 329*0Sstevel@tonic-gate EVP_PKEY_set1_DSA(pktmp, a); 330*0Sstevel@tonic-gate ret = i2d_PUBKEY(pktmp, pp); 331*0Sstevel@tonic-gate EVP_PKEY_free(pktmp); 332*0Sstevel@tonic-gate return ret; 333*0Sstevel@tonic-gate } 334*0Sstevel@tonic-gate #endif 335