sl@0: /* crypto/evp/p_lib.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #ifndef OPENSSL_NO_RSA sl@0: #include sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: #include sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: #include sl@0: #endif sl@0: sl@0: static void EVP_PKEY_free_it(EVP_PKEY *x); sl@0: sl@0: EXPORT_C int EVP_PKEY_bits(EVP_PKEY *pkey) sl@0: { sl@0: if (0) sl@0: return 0; sl@0: #ifndef OPENSSL_NO_RSA sl@0: else if (pkey->type == EVP_PKEY_RSA) sl@0: return(BN_num_bits(pkey->pkey.rsa->n)); sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: else if (pkey->type == EVP_PKEY_DSA) sl@0: return(BN_num_bits(pkey->pkey.dsa->p)); sl@0: #endif sl@0: #ifndef OPENSSL_NO_EC sl@0: else if (pkey->type == EVP_PKEY_EC) sl@0: { sl@0: BIGNUM *order = BN_new(); sl@0: const EC_GROUP *group; sl@0: int ret; sl@0: sl@0: if (!order) sl@0: { sl@0: ERR_clear_error(); sl@0: return 0; sl@0: } sl@0: group = EC_KEY_get0_group(pkey->pkey.ec); sl@0: if (!EC_GROUP_get_order(group, order, NULL)) sl@0: { sl@0: ERR_clear_error(); sl@0: return 0; sl@0: } sl@0: sl@0: ret = BN_num_bits(order); sl@0: BN_free(order); sl@0: return ret; sl@0: } sl@0: #endif sl@0: return(0); sl@0: } sl@0: sl@0: EXPORT_C int EVP_PKEY_size(EVP_PKEY *pkey) sl@0: { sl@0: if (pkey == NULL) sl@0: return(0); sl@0: #ifndef OPENSSL_NO_RSA sl@0: if (pkey->type == EVP_PKEY_RSA) sl@0: return(RSA_size(pkey->pkey.rsa)); sl@0: else sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (pkey->type == EVP_PKEY_DSA) sl@0: return(DSA_size(pkey->pkey.dsa)); sl@0: #endif sl@0: #ifndef OPENSSL_NO_ECDSA sl@0: if (pkey->type == EVP_PKEY_EC) sl@0: return(ECDSA_size(pkey->pkey.ec)); sl@0: #endif sl@0: sl@0: return(0); sl@0: } sl@0: sl@0: EXPORT_C int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode) sl@0: { sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (pkey->type == EVP_PKEY_DSA) sl@0: { sl@0: int ret=pkey->save_parameters; sl@0: sl@0: if (mode >= 0) sl@0: pkey->save_parameters=mode; sl@0: return(ret); sl@0: } sl@0: #endif sl@0: #ifndef OPENSSL_NO_EC sl@0: if (pkey->type == EVP_PKEY_EC) sl@0: { sl@0: int ret = pkey->save_parameters; sl@0: sl@0: if (mode >= 0) sl@0: pkey->save_parameters = mode; sl@0: return(ret); sl@0: } sl@0: #endif sl@0: return(0); sl@0: } sl@0: sl@0: EXPORT_C int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) sl@0: { sl@0: if (to->type != from->type) sl@0: { sl@0: EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_DIFFERENT_KEY_TYPES); sl@0: goto err; sl@0: } sl@0: sl@0: if (EVP_PKEY_missing_parameters(from)) sl@0: { sl@0: EVPerr(EVP_F_EVP_PKEY_COPY_PARAMETERS,EVP_R_MISSING_PARAMETERS); sl@0: goto err; sl@0: } sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (to->type == EVP_PKEY_DSA) sl@0: { sl@0: BIGNUM *a; sl@0: sl@0: if ((a=BN_dup(from->pkey.dsa->p)) == NULL) goto err; sl@0: if (to->pkey.dsa->p != NULL) BN_free(to->pkey.dsa->p); sl@0: to->pkey.dsa->p=a; sl@0: sl@0: if ((a=BN_dup(from->pkey.dsa->q)) == NULL) goto err; sl@0: if (to->pkey.dsa->q != NULL) BN_free(to->pkey.dsa->q); sl@0: to->pkey.dsa->q=a; sl@0: sl@0: if ((a=BN_dup(from->pkey.dsa->g)) == NULL) goto err; sl@0: if (to->pkey.dsa->g != NULL) BN_free(to->pkey.dsa->g); sl@0: to->pkey.dsa->g=a; sl@0: } sl@0: #endif sl@0: #ifndef OPENSSL_NO_EC sl@0: if (to->type == EVP_PKEY_EC) sl@0: { sl@0: EC_GROUP *group = EC_GROUP_dup(EC_KEY_get0_group(from->pkey.ec)); sl@0: if (group == NULL) sl@0: goto err; sl@0: if (EC_KEY_set_group(to->pkey.ec, group) == 0) sl@0: goto err; sl@0: EC_GROUP_free(group); sl@0: } sl@0: #endif sl@0: return(1); sl@0: err: sl@0: return(0); sl@0: } sl@0: sl@0: EXPORT_C int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey) sl@0: { sl@0: #ifndef OPENSSL_NO_DSA sl@0: if (pkey->type == EVP_PKEY_DSA) sl@0: { sl@0: DSA *dsa; sl@0: sl@0: dsa=pkey->pkey.dsa; sl@0: if ((dsa->p == NULL) || (dsa->q == NULL) || (dsa->g == NULL)) sl@0: return(1); sl@0: } sl@0: #endif sl@0: #ifndef OPENSSL_NO_EC sl@0: if (pkey->type == EVP_PKEY_EC) sl@0: { sl@0: if (EC_KEY_get0_group(pkey->pkey.ec) == NULL) sl@0: return(1); sl@0: } sl@0: #endif sl@0: sl@0: return(0); sl@0: } sl@0: sl@0: EXPORT_C int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) sl@0: { sl@0: #ifndef OPENSSL_NO_DSA sl@0: if ((a->type == EVP_PKEY_DSA) && (b->type == EVP_PKEY_DSA)) sl@0: { sl@0: if ( BN_cmp(a->pkey.dsa->p,b->pkey.dsa->p) || sl@0: BN_cmp(a->pkey.dsa->q,b->pkey.dsa->q) || sl@0: BN_cmp(a->pkey.dsa->g,b->pkey.dsa->g)) sl@0: return(0); sl@0: else sl@0: return(1); sl@0: } sl@0: #endif sl@0: #ifndef OPENSSL_NO_EC sl@0: if (a->type == EVP_PKEY_EC && b->type == EVP_PKEY_EC) sl@0: { sl@0: const EC_GROUP *group_a = EC_KEY_get0_group(a->pkey.ec), sl@0: *group_b = EC_KEY_get0_group(b->pkey.ec); sl@0: if (EC_GROUP_cmp(group_a, group_b, NULL)) sl@0: return 0; sl@0: else sl@0: return 1; sl@0: } sl@0: #endif sl@0: return(-1); sl@0: } sl@0: sl@0: EXPORT_C int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b) sl@0: { sl@0: if (a->type != b->type) sl@0: return -1; sl@0: sl@0: if (EVP_PKEY_cmp_parameters(a, b) == 0) sl@0: return 0; sl@0: sl@0: switch (a->type) sl@0: { sl@0: #ifndef OPENSSL_NO_RSA sl@0: case EVP_PKEY_RSA: sl@0: if (BN_cmp(b->pkey.rsa->n,a->pkey.rsa->n) != 0 sl@0: || BN_cmp(b->pkey.rsa->e,a->pkey.rsa->e) != 0) sl@0: return 0; sl@0: break; sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: case EVP_PKEY_DSA: sl@0: if (BN_cmp(b->pkey.dsa->pub_key,a->pkey.dsa->pub_key) != 0) sl@0: return 0; sl@0: break; sl@0: #endif sl@0: #ifndef OPENSSL_NO_EC sl@0: case EVP_PKEY_EC: sl@0: { sl@0: int r; sl@0: const EC_GROUP *group = EC_KEY_get0_group(b->pkey.ec); sl@0: const EC_POINT *pa = EC_KEY_get0_public_key(a->pkey.ec), sl@0: *pb = EC_KEY_get0_public_key(b->pkey.ec); sl@0: r = EC_POINT_cmp(group, pa, pb, NULL); sl@0: if (r != 0) sl@0: { sl@0: if (r == 1) sl@0: return 0; sl@0: else sl@0: return -2; sl@0: } sl@0: } sl@0: break; sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: case EVP_PKEY_DH: sl@0: return -2; sl@0: #endif sl@0: default: sl@0: return -2; sl@0: } sl@0: sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C EVP_PKEY *EVP_PKEY_new(void) sl@0: { sl@0: EVP_PKEY *ret; sl@0: sl@0: ret=(EVP_PKEY *)OPENSSL_malloc(sizeof(EVP_PKEY)); sl@0: if (ret == NULL) sl@0: { sl@0: EVPerr(EVP_F_EVP_PKEY_NEW,ERR_R_MALLOC_FAILURE); sl@0: return(NULL); sl@0: } sl@0: ret->type=EVP_PKEY_NONE; sl@0: ret->references=1; sl@0: ret->pkey.ptr=NULL; sl@0: ret->attributes=NULL; sl@0: ret->save_parameters=1; sl@0: return(ret); sl@0: } sl@0: sl@0: EXPORT_C int EVP_PKEY_assign(EVP_PKEY *pkey, int type, char *key) sl@0: { sl@0: if (pkey == NULL) return(0); sl@0: if (pkey->pkey.ptr != NULL) sl@0: EVP_PKEY_free_it(pkey); sl@0: pkey->type=EVP_PKEY_type(type); sl@0: pkey->save_type=type; sl@0: pkey->pkey.ptr=key; sl@0: return(key != NULL); sl@0: } sl@0: sl@0: #ifndef OPENSSL_NO_RSA sl@0: EXPORT_C int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) sl@0: { sl@0: int ret = EVP_PKEY_assign_RSA(pkey, key); sl@0: if(ret) sl@0: RSA_up_ref(key); sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C RSA *EVP_PKEY_get1_RSA(EVP_PKEY *pkey) sl@0: { sl@0: if(pkey->type != EVP_PKEY_RSA) { sl@0: EVPerr(EVP_F_EVP_PKEY_GET1_RSA, EVP_R_EXPECTING_AN_RSA_KEY); sl@0: return NULL; sl@0: } sl@0: RSA_up_ref(pkey->pkey.rsa); sl@0: return pkey->pkey.rsa; sl@0: } sl@0: #endif sl@0: sl@0: #ifndef OPENSSL_NO_DSA sl@0: EXPORT_C int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key) sl@0: { sl@0: int ret = EVP_PKEY_assign_DSA(pkey, key); sl@0: if(ret) sl@0: DSA_up_ref(key); sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey) sl@0: { sl@0: if(pkey->type != EVP_PKEY_DSA) { sl@0: EVPerr(EVP_F_EVP_PKEY_GET1_DSA, EVP_R_EXPECTING_A_DSA_KEY); sl@0: return NULL; sl@0: } sl@0: DSA_up_ref(pkey->pkey.dsa); sl@0: return pkey->pkey.dsa; sl@0: } sl@0: #endif sl@0: sl@0: #ifndef OPENSSL_NO_EC sl@0: sl@0: EXPORT_C int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) sl@0: { sl@0: int ret = EVP_PKEY_assign_EC_KEY(pkey,key); sl@0: if (ret) sl@0: EC_KEY_up_ref(key); sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey) sl@0: { sl@0: if (pkey->type != EVP_PKEY_EC) sl@0: { sl@0: EVPerr(EVP_F_EVP_PKEY_GET1_EC_KEY, EVP_R_EXPECTING_A_EC_KEY); sl@0: return NULL; sl@0: } sl@0: EC_KEY_up_ref(pkey->pkey.ec); sl@0: return pkey->pkey.ec; sl@0: } sl@0: #endif sl@0: sl@0: sl@0: #ifndef OPENSSL_NO_DH sl@0: sl@0: EXPORT_C int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key) sl@0: { sl@0: int ret = EVP_PKEY_assign_DH(pkey, key); sl@0: if(ret) sl@0: DH_up_ref(key); sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey) sl@0: { sl@0: if(pkey->type != EVP_PKEY_DH) { sl@0: EVPerr(EVP_F_EVP_PKEY_GET1_DH, EVP_R_EXPECTING_A_DH_KEY); sl@0: return NULL; sl@0: } sl@0: DH_up_ref(pkey->pkey.dh); sl@0: return pkey->pkey.dh; sl@0: } sl@0: #endif sl@0: sl@0: EXPORT_C int EVP_PKEY_type(int type) sl@0: { sl@0: switch (type) sl@0: { sl@0: case EVP_PKEY_RSA: sl@0: case EVP_PKEY_RSA2: sl@0: return(EVP_PKEY_RSA); sl@0: case EVP_PKEY_DSA: sl@0: case EVP_PKEY_DSA1: sl@0: case EVP_PKEY_DSA2: sl@0: case EVP_PKEY_DSA3: sl@0: case EVP_PKEY_DSA4: sl@0: return(EVP_PKEY_DSA); sl@0: case EVP_PKEY_DH: sl@0: return(EVP_PKEY_DH); sl@0: case EVP_PKEY_EC: sl@0: return(EVP_PKEY_EC); sl@0: default: sl@0: return(NID_undef); sl@0: } sl@0: } sl@0: sl@0: EXPORT_C void EVP_PKEY_free(EVP_PKEY *x) sl@0: { sl@0: int i; sl@0: sl@0: if (x == NULL) return; sl@0: sl@0: i=CRYPTO_add(&x->references,-1,CRYPTO_LOCK_EVP_PKEY); sl@0: #ifdef REF_PRINT sl@0: REF_PRINT("EVP_PKEY",x); sl@0: #endif sl@0: if (i > 0) return; sl@0: #ifdef REF_CHECK sl@0: if (i < 0) sl@0: { sl@0: fprintf(stderr,"EVP_PKEY_free, bad reference count\n"); sl@0: abort(); sl@0: } sl@0: #endif sl@0: EVP_PKEY_free_it(x); sl@0: if (x->attributes) sl@0: sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free); sl@0: OPENSSL_free(x); sl@0: } sl@0: sl@0: static void EVP_PKEY_free_it(EVP_PKEY *x) sl@0: { sl@0: switch (x->type) sl@0: { sl@0: #ifndef OPENSSL_NO_RSA sl@0: case EVP_PKEY_RSA: sl@0: case EVP_PKEY_RSA2: sl@0: RSA_free(x->pkey.rsa); sl@0: break; sl@0: #endif sl@0: #ifndef OPENSSL_NO_DSA sl@0: case EVP_PKEY_DSA: sl@0: case EVP_PKEY_DSA2: sl@0: case EVP_PKEY_DSA3: sl@0: case EVP_PKEY_DSA4: sl@0: DSA_free(x->pkey.dsa); sl@0: break; sl@0: #endif sl@0: #ifndef OPENSSL_NO_EC sl@0: case EVP_PKEY_EC: sl@0: EC_KEY_free(x->pkey.ec); sl@0: break; sl@0: #endif sl@0: #ifndef OPENSSL_NO_DH sl@0: case EVP_PKEY_DH: sl@0: DH_free(x->pkey.dh); sl@0: break; sl@0: #endif sl@0: } sl@0: } sl@0: