10Sstevel@tonic-gate /* crypto/evp/p_lib.c */ 20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 30Sstevel@tonic-gate * All rights reserved. 40Sstevel@tonic-gate * 50Sstevel@tonic-gate * This package is an SSL implementation written 60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com). 70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as 100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions 110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA, 120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation 130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms 140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com). 150Sstevel@tonic-gate * 160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in 170Sstevel@tonic-gate * the code are not to be removed. 180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution 190Sstevel@tonic-gate * as the author of the parts of the library used. 200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or 210Sstevel@tonic-gate * in documentation (online or textual) provided with the package. 220Sstevel@tonic-gate * 230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 240Sstevel@tonic-gate * modification, are permitted provided that the following conditions 250Sstevel@tonic-gate * are met: 260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright 270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 320Sstevel@tonic-gate * must display the following acknowledgement: 330Sstevel@tonic-gate * "This product includes cryptographic software written by 340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)" 350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library 360Sstevel@tonic-gate * being used are not cryptographic related :-). 370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from 380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement: 390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 400Sstevel@tonic-gate * 410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 510Sstevel@tonic-gate * SUCH DAMAGE. 520Sstevel@tonic-gate * 530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or 540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be 550Sstevel@tonic-gate * copied and put under another distribution licence 560Sstevel@tonic-gate * [including the GNU Public Licence.] 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate 590Sstevel@tonic-gate #include <stdio.h> 600Sstevel@tonic-gate #include "cryptlib.h" 61*2139Sjp161948 #include <openssl/bn.h> 62*2139Sjp161948 #include <openssl/err.h> 630Sstevel@tonic-gate #include <openssl/objects.h> 640Sstevel@tonic-gate #include <openssl/evp.h> 650Sstevel@tonic-gate #include <openssl/asn1_mac.h> 660Sstevel@tonic-gate #include <openssl/x509.h> 67*2139Sjp161948 #ifndef OPENSSL_NO_RSA 68*2139Sjp161948 #include <openssl/rsa.h> 69*2139Sjp161948 #endif 70*2139Sjp161948 #ifndef OPENSSL_NO_DSA 71*2139Sjp161948 #include <openssl/dsa.h> 72*2139Sjp161948 #endif 73*2139Sjp161948 #ifndef OPENSSL_NO_DH 74*2139Sjp161948 #include <openssl/dh.h> 75*2139Sjp161948 #endif 760Sstevel@tonic-gate 770Sstevel@tonic-gate static void EVP_PKEY_free_it(EVP_PKEY *x); 780Sstevel@tonic-gate 790Sstevel@tonic-gate int EVP_PKEY_bits(EVP_PKEY *pkey) 800Sstevel@tonic-gate { 81*2139Sjp161948 if (0) 82*2139Sjp161948 return 0; 830Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 84*2139Sjp161948 else if (pkey->type == EVP_PKEY_RSA) 850Sstevel@tonic-gate return(BN_num_bits(pkey->pkey.rsa->n)); 860Sstevel@tonic-gate #endif 870Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 88*2139Sjp161948 else if (pkey->type == EVP_PKEY_DSA) 890Sstevel@tonic-gate return(BN_num_bits(pkey->pkey.dsa->p)); 900Sstevel@tonic-gate #endif 91*2139Sjp161948 #ifndef OPENSSL_NO_EC 92*2139Sjp161948 else if (pkey->type == EVP_PKEY_EC) 93*2139Sjp161948 { 94*2139Sjp161948 BIGNUM *order = BN_new(); 95*2139Sjp161948 const EC_GROUP *group; 96*2139Sjp161948 int ret; 97*2139Sjp161948 98*2139Sjp161948 if (!order) 99*2139Sjp161948 { 100*2139Sjp161948 ERR_clear_error(); 101*2139Sjp161948 return 0; 102*2139Sjp161948 } 103*2139Sjp161948 group = EC_KEY_get0_group(pkey->pkey.ec); 104*2139Sjp161948 if (!EC_GROUP_get_order(group, order, NULL)) 105*2139Sjp161948 { 106*2139Sjp161948 ERR_clear_error(); 107*2139Sjp161948 return 0; 108*2139Sjp161948 } 109*2139Sjp161948 110*2139Sjp161948 ret = BN_num_bits(order); 111*2139Sjp161948 BN_free(order); 112*2139Sjp161948 return ret; 113*2139Sjp161948 } 114*2139Sjp161948 #endif 1150Sstevel@tonic-gate return(0); 1160Sstevel@tonic-gate } 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate int EVP_PKEY_size(EVP_PKEY *pkey) 1190Sstevel@tonic-gate { 1200Sstevel@tonic-gate if (pkey == NULL) 1210Sstevel@tonic-gate return(0); 1220Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 1230Sstevel@tonic-gate if (pkey->type == EVP_PKEY_RSA) 1240Sstevel@tonic-gate return(RSA_size(pkey->pkey.rsa)); 1250Sstevel@tonic-gate else 1260Sstevel@tonic-gate #endif 1270Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 1280Sstevel@tonic-gate if (pkey->type == EVP_PKEY_DSA) 1290Sstevel@tonic-gate return(DSA_size(pkey->pkey.dsa)); 1300Sstevel@tonic-gate #endif 131*2139Sjp161948 #ifndef OPENSSL_NO_ECDSA 132*2139Sjp161948 if (pkey->type == EVP_PKEY_EC) 133*2139Sjp161948 return(ECDSA_size(pkey->pkey.ec)); 134*2139Sjp161948 #endif 135*2139Sjp161948 1360Sstevel@tonic-gate return(0); 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode) 1400Sstevel@tonic-gate { 1410Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 1420Sstevel@tonic-gate if (pkey->type == EVP_PKEY_DSA) 1430Sstevel@tonic-gate { 1440Sstevel@tonic-gate int ret=pkey->save_parameters; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate if (mode >= 0) 1470Sstevel@tonic-gate pkey->save_parameters=mode; 1480Sstevel@tonic-gate return(ret); 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate #endif 151*2139Sjp161948 #ifndef OPENSSL_NO_EC 152*2139Sjp161948 if (pkey->type == EVP_PKEY_EC) 153*2139Sjp161948 { 154*2139Sjp161948 int ret = pkey->save_parameters; 155*2139Sjp161948 156*2139Sjp161948 if (mode >= 0) 157*2139Sjp161948 pkey->save_parameters = mode; 158*2139Sjp161948 return(ret); 159*2139Sjp161948 } 160*2139Sjp161948 #endif 1610Sstevel@tonic-gate return(0); 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate 164*2139Sjp161948 int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) 1650Sstevel@tonic-gate { 1660Sstevel@tonic-gate if (to->type != from->type) 1670Sstevel@tonic-gate { 1680Sstevel@tonic-gate EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_DIFFERENT_KEY_TYPES); 1690Sstevel@tonic-gate goto err; 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate if (EVP_PKEY_missing_parameters(from)) 1730Sstevel@tonic-gate { 1740Sstevel@tonic-gate EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_MISSING_PARAMETERS); 1750Sstevel@tonic-gate goto err; 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 1780Sstevel@tonic-gate if (to->type == EVP_PKEY_DSA) 1790Sstevel@tonic-gate { 1800Sstevel@tonic-gate BIGNUM *a; 1810Sstevel@tonic-gate 1820Sstevel@tonic-gate if ((a=BN_dup(from->pkey.dsa->p)) == NULL) goto err; 1830Sstevel@tonic-gate if (to->pkey.dsa->p != NULL) BN_free(to->pkey.dsa->p); 1840Sstevel@tonic-gate to->pkey.dsa->p=a; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate if ((a=BN_dup(from->pkey.dsa->q)) == NULL) goto err; 1870Sstevel@tonic-gate if (to->pkey.dsa->q != NULL) BN_free(to->pkey.dsa->q); 1880Sstevel@tonic-gate to->pkey.dsa->q=a; 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate if ((a=BN_dup(from->pkey.dsa->g)) == NULL) goto err; 1910Sstevel@tonic-gate if (to->pkey.dsa->g != NULL) BN_free(to->pkey.dsa->g); 1920Sstevel@tonic-gate to->pkey.dsa->g=a; 1930Sstevel@tonic-gate } 1940Sstevel@tonic-gate #endif 195*2139Sjp161948 #ifndef OPENSSL_NO_EC 196*2139Sjp161948 if (to->type == EVP_PKEY_EC) 197*2139Sjp161948 { 198*2139Sjp161948 EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec)); 199*2139Sjp161948 if (group == NULL) 200*2139Sjp161948 goto err; 201*2139Sjp161948 if (EC_KEY_set_group(to->pkey.ec, group) == 0) 202*2139Sjp161948 goto err; 203*2139Sjp161948 EC_GROUP_free(group); 204*2139Sjp161948 } 205*2139Sjp161948 #endif 2060Sstevel@tonic-gate return(1); 2070Sstevel@tonic-gate err: 2080Sstevel@tonic-gate return(0); 2090Sstevel@tonic-gate } 2100Sstevel@tonic-gate 211*2139Sjp161948 int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) 2120Sstevel@tonic-gate { 2130Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 2140Sstevel@tonic-gate if (pkey->type == EVP_PKEY_DSA) 2150Sstevel@tonic-gate { 2160Sstevel@tonic-gate DSA *dsa; 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate dsa=pkey->pkey.dsa; 2190Sstevel@tonic-gate if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL)) 2200Sstevel@tonic-gate return(1); 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate #endif 223*2139Sjp161948 #ifndef OPENSSL_NO_EC 224*2139Sjp161948 if (pkey->type == EVP_PKEY_EC) 225*2139Sjp161948 { 226*2139Sjp161948 if (EC_KEY_get0_group(pkey->pkey.ec) == NULL) 227*2139Sjp161948 return(1); 228*2139Sjp161948 } 229*2139Sjp161948 #endif 230*2139Sjp161948 2310Sstevel@tonic-gate return(0); 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate 234*2139Sjp161948 int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) 2350Sstevel@tonic-gate { 2360Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 2370Sstevel@tonic-gate if ((a->type == EVP_PKEY_DSA) && (b->type == EVP_PKEY_DSA)) 2380Sstevel@tonic-gate { 2390Sstevel@tonic-gate if ( BN_cmp(a->pkey.dsa->p,b->pkey.dsa->p) || 2400Sstevel@tonic-gate BN_cmp(a->pkey.dsa->q,b->pkey.dsa->q) || 2410Sstevel@tonic-gate BN_cmp(a->pkey.dsa->g,b->pkey.dsa->g)) 2420Sstevel@tonic-gate return(0); 2430Sstevel@tonic-gate else 2440Sstevel@tonic-gate return(1); 2450Sstevel@tonic-gate } 2460Sstevel@tonic-gate #endif 247*2139Sjp161948 #ifndef OPENSSL_NO_EC 248*2139Sjp161948 if (a->type == EVP_PKEY_EC && b->type == EVP_PKEY_EC) 249*2139Sjp161948 { 250*2139Sjp161948 const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec), 251*2139Sjp161948 *group_b = EC_KEY_get0_group(b->pkey.ec); 252*2139Sjp161948 if (EC_GROUP_cmp(group_a, group_b, NULL)) 253*2139Sjp161948 return 0; 254*2139Sjp161948 else 255*2139Sjp161948 return 1; 256*2139Sjp161948 } 257*2139Sjp161948 #endif 2580Sstevel@tonic-gate return(-1); 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate 261*2139Sjp161948 int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) 262*2139Sjp161948 { 263*2139Sjp161948 if (a->type != b->type) 264*2139Sjp161948 return -1; 265*2139Sjp161948 266*2139Sjp161948 if (EVP_PKEY_cmp_parameters(a, b) == 0) 267*2139Sjp161948 return 0; 268*2139Sjp161948 269*2139Sjp161948 switch (a->type) 270*2139Sjp161948 { 271*2139Sjp161948 #ifndef OPENSSL_NO_RSA 272*2139Sjp161948 case EVP_PKEY_RSA: 273*2139Sjp161948 if (BN_cmp(b->pkey.rsa->n,a->pkey.rsa->n) != 0 274*2139Sjp161948 || BN_cmp(b->pkey.rsa->e,a->pkey.rsa->e) != 0) 275*2139Sjp161948 return 0; 276*2139Sjp161948 break; 277*2139Sjp161948 #endif 278*2139Sjp161948 #ifndef OPENSSL_NO_DSA 279*2139Sjp161948 case EVP_PKEY_DSA: 280*2139Sjp161948 if (BN_cmp(b->pkey.dsa->pub_key,a->pkey.dsa->pub_key) != 0) 281*2139Sjp161948 return 0; 282*2139Sjp161948 break; 283*2139Sjp161948 #endif 284*2139Sjp161948 #ifndef OPENSSL_NO_EC 285*2139Sjp161948 case EVP_PKEY_EC: 286*2139Sjp161948 { 287*2139Sjp161948 int r; 288*2139Sjp161948 const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec); 289*2139Sjp161948 const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec), 290*2139Sjp161948 *pb = EC_KEY_get0_public_key(b->pkey.ec); 291*2139Sjp161948 r = EC_POINT_cmp(group, pa, pb, NULL); 292*2139Sjp161948 if (r != 0) 293*2139Sjp161948 { 294*2139Sjp161948 if (r == 1) 295*2139Sjp161948 return 0; 296*2139Sjp161948 else 297*2139Sjp161948 return -2; 298*2139Sjp161948 } 299*2139Sjp161948 } 300*2139Sjp161948 break; 301*2139Sjp161948 #endif 302*2139Sjp161948 #ifndef OPENSSL_NO_DH 303*2139Sjp161948 case EVP_PKEY_DH: 304*2139Sjp161948 return -2; 305*2139Sjp161948 #endif 306*2139Sjp161948 default: 307*2139Sjp161948 return -2; 308*2139Sjp161948 } 309*2139Sjp161948 310*2139Sjp161948 return 1; 311*2139Sjp161948 } 312*2139Sjp161948 3130Sstevel@tonic-gate EVP_PKEY *EVP_PKEY_new(void) 3140Sstevel@tonic-gate { 3150Sstevel@tonic-gate EVP_PKEY *ret; 3160Sstevel@tonic-gate 3170Sstevel@tonic-gate ret=(EVP_PKEY *)OPENSSL_malloc(sizeof(EVP_PKEY)); 3180Sstevel@tonic-gate if (ret == NULL) 3190Sstevel@tonic-gate { 3200Sstevel@tonic-gate EVPerr(EVP_F_EVP_PKEY_NEW,ERR_R_MALLOC_FAILURE); 3210Sstevel@tonic-gate return(NULL); 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate ret->type=EVP_PKEY_NONE; 3240Sstevel@tonic-gate ret->references=1; 3250Sstevel@tonic-gate ret->pkey.ptr=NULL; 3260Sstevel@tonic-gate ret->attributes=NULL; 3270Sstevel@tonic-gate ret->save_parameters=1; 3280Sstevel@tonic-gate return(ret); 3290Sstevel@tonic-gate } 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate int EVP_PKEY_assign(EVP_PKEY *pkey, int type, char *key) 3320Sstevel@tonic-gate { 3330Sstevel@tonic-gate if (pkey == NULL) return(0); 3340Sstevel@tonic-gate if (pkey->pkey.ptr != NULL) 3350Sstevel@tonic-gate EVP_PKEY_free_it(pkey); 3360Sstevel@tonic-gate pkey->type=EVP_PKEY_type(type); 3370Sstevel@tonic-gate pkey->save_type=type; 3380Sstevel@tonic-gate pkey->pkey.ptr=key; 3390Sstevel@tonic-gate return(key != NULL); 3400Sstevel@tonic-gate } 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 3430Sstevel@tonic-gate int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) 3440Sstevel@tonic-gate { 3450Sstevel@tonic-gate int ret = EVP_PKEY_assign_RSA(pkey, key); 3460Sstevel@tonic-gate if(ret) 3470Sstevel@tonic-gate RSA_up_ref(key); 3480Sstevel@tonic-gate return ret; 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) 3520Sstevel@tonic-gate { 3530Sstevel@tonic-gate if(pkey->type != EVP_PKEY_RSA) { 3540Sstevel@tonic-gate EVPerr(EVP_F_EVP_PKEY_GET1_RSA, EVP_R_EXPECTING_AN_RSA_KEY); 3550Sstevel@tonic-gate return NULL; 3560Sstevel@tonic-gate } 3570Sstevel@tonic-gate RSA_up_ref(pkey->pkey.rsa); 3580Sstevel@tonic-gate return pkey->pkey.rsa; 3590Sstevel@tonic-gate } 3600Sstevel@tonic-gate #endif 3610Sstevel@tonic-gate 3620Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 3630Sstevel@tonic-gate int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) 3640Sstevel@tonic-gate { 3650Sstevel@tonic-gate int ret = EVP_PKEY_assign_DSA(pkey, key); 3660Sstevel@tonic-gate if(ret) 3670Sstevel@tonic-gate DSA_up_ref(key); 3680Sstevel@tonic-gate return ret; 3690Sstevel@tonic-gate } 3700Sstevel@tonic-gate 3710Sstevel@tonic-gate DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) 3720Sstevel@tonic-gate { 3730Sstevel@tonic-gate if(pkey->type != EVP_PKEY_DSA) { 3740Sstevel@tonic-gate EVPerr(EVP_F_EVP_PKEY_GET1_DSA, EVP_R_EXPECTING_A_DSA_KEY); 3750Sstevel@tonic-gate return NULL; 3760Sstevel@tonic-gate } 3770Sstevel@tonic-gate DSA_up_ref(pkey->pkey.dsa); 3780Sstevel@tonic-gate return pkey->pkey.dsa; 3790Sstevel@tonic-gate } 3800Sstevel@tonic-gate #endif 3810Sstevel@tonic-gate 382*2139Sjp161948 #ifndef OPENSSL_NO_EC 383*2139Sjp161948 384*2139Sjp161948 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) 385*2139Sjp161948 { 386*2139Sjp161948 int ret = EVP_PKEY_assign_EC_KEY(pkey,key); 387*2139Sjp161948 if (ret) 388*2139Sjp161948 EC_KEY_up_ref(key); 389*2139Sjp161948 return ret; 390*2139Sjp161948 } 391*2139Sjp161948 392*2139Sjp161948 EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) 393*2139Sjp161948 { 394*2139Sjp161948 if (pkey->type != EVP_PKEY_EC) 395*2139Sjp161948 { 396*2139Sjp161948 EVPerr(EVP_F_EVP_PKEY_GET1_EC_KEY, EVP_R_EXPECTING_A_EC_KEY); 397*2139Sjp161948 return NULL; 398*2139Sjp161948 } 399*2139Sjp161948 EC_KEY_up_ref(pkey->pkey.ec); 400*2139Sjp161948 return pkey->pkey.ec; 401*2139Sjp161948 } 402*2139Sjp161948 #endif 403*2139Sjp161948 404*2139Sjp161948 4050Sstevel@tonic-gate #ifndef OPENSSL_NO_DH 4060Sstevel@tonic-gate 4070Sstevel@tonic-gate int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) 4080Sstevel@tonic-gate { 4090Sstevel@tonic-gate int ret = EVP_PKEY_assign_DH(pkey, key); 4100Sstevel@tonic-gate if(ret) 4110Sstevel@tonic-gate DH_up_ref(key); 4120Sstevel@tonic-gate return ret; 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey) 4160Sstevel@tonic-gate { 4170Sstevel@tonic-gate if(pkey->type != EVP_PKEY_DH) { 4180Sstevel@tonic-gate EVPerr(EVP_F_EVP_PKEY_GET1_DH, EVP_R_EXPECTING_A_DH_KEY); 4190Sstevel@tonic-gate return NULL; 4200Sstevel@tonic-gate } 4210Sstevel@tonic-gate DH_up_ref(pkey->pkey.dh); 4220Sstevel@tonic-gate return pkey->pkey.dh; 4230Sstevel@tonic-gate } 4240Sstevel@tonic-gate #endif 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate int EVP_PKEY_type(int type) 4270Sstevel@tonic-gate { 4280Sstevel@tonic-gate switch (type) 4290Sstevel@tonic-gate { 4300Sstevel@tonic-gate case EVP_PKEY_RSA: 4310Sstevel@tonic-gate case EVP_PKEY_RSA2: 4320Sstevel@tonic-gate return(EVP_PKEY_RSA); 4330Sstevel@tonic-gate case EVP_PKEY_DSA: 4340Sstevel@tonic-gate case EVP_PKEY_DSA1: 4350Sstevel@tonic-gate case EVP_PKEY_DSA2: 4360Sstevel@tonic-gate case EVP_PKEY_DSA3: 4370Sstevel@tonic-gate case EVP_PKEY_DSA4: 4380Sstevel@tonic-gate return(EVP_PKEY_DSA); 4390Sstevel@tonic-gate case EVP_PKEY_DH: 4400Sstevel@tonic-gate return(EVP_PKEY_DH); 441*2139Sjp161948 case EVP_PKEY_EC: 442*2139Sjp161948 return(EVP_PKEY_EC); 4430Sstevel@tonic-gate default: 4440Sstevel@tonic-gate return(NID_undef); 4450Sstevel@tonic-gate } 4460Sstevel@tonic-gate } 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate void EVP_PKEY_free(EVP_PKEY *x) 4490Sstevel@tonic-gate { 4500Sstevel@tonic-gate int i; 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate if (x == NULL) return; 4530Sstevel@tonic-gate 4540Sstevel@tonic-gate i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_EVP_PKEY); 4550Sstevel@tonic-gate #ifdef REF_PRINT 4560Sstevel@tonic-gate REF_PRINT("EVP_PKEY",x); 4570Sstevel@tonic-gate #endif 4580Sstevel@tonic-gate if (i > 0) return; 4590Sstevel@tonic-gate #ifdef REF_CHECK 4600Sstevel@tonic-gate if (i < 0) 4610Sstevel@tonic-gate { 4620Sstevel@tonic-gate fprintf(stderr,"EVP_PKEY_free, bad reference count\n"); 4630Sstevel@tonic-gate abort(); 4640Sstevel@tonic-gate } 4650Sstevel@tonic-gate #endif 4660Sstevel@tonic-gate EVP_PKEY_free_it(x); 467*2139Sjp161948 if (x->attributes) 468*2139Sjp161948 sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free); 4690Sstevel@tonic-gate OPENSSL_free(x); 4700Sstevel@tonic-gate } 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate static void EVP_PKEY_free_it(EVP_PKEY *x) 4730Sstevel@tonic-gate { 4740Sstevel@tonic-gate switch (x->type) 4750Sstevel@tonic-gate { 4760Sstevel@tonic-gate #ifndef OPENSSL_NO_RSA 4770Sstevel@tonic-gate case EVP_PKEY_RSA: 4780Sstevel@tonic-gate case EVP_PKEY_RSA2: 4790Sstevel@tonic-gate RSA_free(x->pkey.rsa); 4800Sstevel@tonic-gate break; 4810Sstevel@tonic-gate #endif 4820Sstevel@tonic-gate #ifndef OPENSSL_NO_DSA 4830Sstevel@tonic-gate case EVP_PKEY_DSA: 4840Sstevel@tonic-gate case EVP_PKEY_DSA2: 4850Sstevel@tonic-gate case EVP_PKEY_DSA3: 4860Sstevel@tonic-gate case EVP_PKEY_DSA4: 4870Sstevel@tonic-gate DSA_free(x->pkey.dsa); 4880Sstevel@tonic-gate break; 4890Sstevel@tonic-gate #endif 490*2139Sjp161948 #ifndef OPENSSL_NO_EC 491*2139Sjp161948 case EVP_PKEY_EC: 492*2139Sjp161948 EC_KEY_free(x->pkey.ec); 493*2139Sjp161948 break; 494*2139Sjp161948 #endif 4950Sstevel@tonic-gate #ifndef OPENSSL_NO_DH 4960Sstevel@tonic-gate case EVP_PKEY_DH: 4970Sstevel@tonic-gate DH_free(x->pkey.dh); 4980Sstevel@tonic-gate break; 4990Sstevel@tonic-gate #endif 5000Sstevel@tonic-gate } 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate 503