1238384Sjkim/* ==================================================================== 2238384Sjkim * Copyright (c) 2006 The OpenSSL Project. All rights reserved. 3238384Sjkim * 4238384Sjkim * Redistribution and use in source and binary forms, with or without 5238384Sjkim * modification, are permitted provided that the following conditions 6238384Sjkim * are met: 7238384Sjkim * 8238384Sjkim * 1. Redistributions of source code must retain the above copyright 9280304Sjkim * notice, this list of conditions and the following disclaimer. 10238384Sjkim * 11238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright 12238384Sjkim * notice, this list of conditions and the following disclaimer in 13238384Sjkim * the documentation and/or other materials provided with the 14238384Sjkim * distribution. 15238384Sjkim * 16238384Sjkim * 3. All advertising materials mentioning features or use of this 17238384Sjkim * software must display the following acknowledgment: 18238384Sjkim * "This product includes software developed by the OpenSSL Project 19238384Sjkim * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 20238384Sjkim * 21238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 22238384Sjkim * endorse or promote products derived from this software without 23238384Sjkim * prior written permission. For written permission, please contact 24238384Sjkim * licensing@OpenSSL.org. 25238384Sjkim * 26238384Sjkim * 5. Products derived from this software may not be called "OpenSSL" 27238384Sjkim * nor may "OpenSSL" appear in their names without prior written 28238384Sjkim * permission of the OpenSSL Project. 29238384Sjkim * 30238384Sjkim * 6. Redistributions of any form whatsoever must retain the following 31238384Sjkim * acknowledgment: 32238384Sjkim * "This product includes software developed by the OpenSSL Project 33238384Sjkim * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 34238384Sjkim * 35238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 36238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 37238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 38238384Sjkim * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 39238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 41238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 42238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 43238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 44238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 46238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE. 47238384Sjkim * ==================================================================== 48238384Sjkim * 49238384Sjkim * This product includes cryptographic software written by Eric Young 50238384Sjkim * (eay@cryptsoft.com). This product includes software written by Tim 51238384Sjkim * Hudson (tjh@cryptsoft.com). 52238384Sjkim * 53238384Sjkim */ 54238384Sjkim 55238384Sjkim#include "eng_int.h" 56238384Sjkim#include "asn1_locl.h" 57238384Sjkim#include <openssl/evp.h> 58238384Sjkim 59280304Sjkim/* 60280304Sjkim * If this symbol is defined then ENGINE_get_pkey_asn1_meth_engine(), the 61238384Sjkim * function that is used by EVP to hook in pkey_asn1_meth code and cache 62238384Sjkim * defaults (etc), will display brief debugging summaries to stderr with the 63280304Sjkim * 'nid'. 64280304Sjkim */ 65238384Sjkim/* #define ENGINE_PKEY_ASN1_METH_DEBUG */ 66238384Sjkim 67238384Sjkimstatic ENGINE_TABLE *pkey_asn1_meth_table = NULL; 68238384Sjkim 69238384Sjkimvoid ENGINE_unregister_pkey_asn1_meths(ENGINE *e) 70280304Sjkim{ 71280304Sjkim engine_table_unregister(&pkey_asn1_meth_table, e); 72280304Sjkim} 73238384Sjkim 74238384Sjkimstatic void engine_unregister_all_pkey_asn1_meths(void) 75280304Sjkim{ 76280304Sjkim engine_table_cleanup(&pkey_asn1_meth_table); 77280304Sjkim} 78238384Sjkim 79238384Sjkimint ENGINE_register_pkey_asn1_meths(ENGINE *e) 80280304Sjkim{ 81280304Sjkim if (e->pkey_asn1_meths) { 82280304Sjkim const int *nids; 83280304Sjkim int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0); 84280304Sjkim if (num_nids > 0) 85280304Sjkim return engine_table_register(&pkey_asn1_meth_table, 86280304Sjkim engine_unregister_all_pkey_asn1_meths, 87280304Sjkim e, nids, num_nids, 0); 88280304Sjkim } 89280304Sjkim return 1; 90280304Sjkim} 91238384Sjkim 92238384Sjkimvoid ENGINE_register_all_pkey_asn1_meths(void) 93280304Sjkim{ 94280304Sjkim ENGINE *e; 95238384Sjkim 96280304Sjkim for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e)) 97280304Sjkim ENGINE_register_pkey_asn1_meths(e); 98280304Sjkim} 99238384Sjkim 100238384Sjkimint ENGINE_set_default_pkey_asn1_meths(ENGINE *e) 101280304Sjkim{ 102280304Sjkim if (e->pkey_asn1_meths) { 103280304Sjkim const int *nids; 104280304Sjkim int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0); 105280304Sjkim if (num_nids > 0) 106280304Sjkim return engine_table_register(&pkey_asn1_meth_table, 107280304Sjkim engine_unregister_all_pkey_asn1_meths, 108280304Sjkim e, nids, num_nids, 1); 109280304Sjkim } 110280304Sjkim return 1; 111280304Sjkim} 112238384Sjkim 113280304Sjkim/* 114280304Sjkim * Exposed API function to get a functional reference from the implementation 115238384Sjkim * table (ie. try to get a functional reference from the tabled structural 116280304Sjkim * references) for a given pkey_asn1_meth 'nid' 117280304Sjkim */ 118238384SjkimENGINE *ENGINE_get_pkey_asn1_meth_engine(int nid) 119280304Sjkim{ 120280304Sjkim return engine_table_select(&pkey_asn1_meth_table, nid); 121280304Sjkim} 122238384Sjkim 123280304Sjkim/* 124280304Sjkim * Obtains a pkey_asn1_meth implementation from an ENGINE functional 125280304Sjkim * reference 126280304Sjkim */ 127238384Sjkimconst EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth(ENGINE *e, int nid) 128280304Sjkim{ 129280304Sjkim EVP_PKEY_ASN1_METHOD *ret; 130280304Sjkim ENGINE_PKEY_ASN1_METHS_PTR fn = ENGINE_get_pkey_asn1_meths(e); 131280304Sjkim if (!fn || !fn(e, &ret, NULL, nid)) { 132280304Sjkim ENGINEerr(ENGINE_F_ENGINE_GET_PKEY_ASN1_METH, 133280304Sjkim ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD); 134280304Sjkim return NULL; 135280304Sjkim } 136280304Sjkim return ret; 137280304Sjkim} 138238384Sjkim 139238384Sjkim/* Gets the pkey_asn1_meth callback from an ENGINE structure */ 140238384SjkimENGINE_PKEY_ASN1_METHS_PTR ENGINE_get_pkey_asn1_meths(const ENGINE *e) 141280304Sjkim{ 142280304Sjkim return e->pkey_asn1_meths; 143280304Sjkim} 144238384Sjkim 145238384Sjkim/* Sets the pkey_asn1_meth callback in an ENGINE structure */ 146238384Sjkimint ENGINE_set_pkey_asn1_meths(ENGINE *e, ENGINE_PKEY_ASN1_METHS_PTR f) 147280304Sjkim{ 148280304Sjkim e->pkey_asn1_meths = f; 149280304Sjkim return 1; 150280304Sjkim} 151238384Sjkim 152280304Sjkim/* 153280304Sjkim * Internal function to free up EVP_PKEY_ASN1_METHOD structures before an 154238384Sjkim * ENGINE is destroyed 155238384Sjkim */ 156238384Sjkim 157238384Sjkimvoid engine_pkey_asn1_meths_free(ENGINE *e) 158280304Sjkim{ 159280304Sjkim int i; 160280304Sjkim EVP_PKEY_ASN1_METHOD *pkm; 161280304Sjkim if (e->pkey_asn1_meths) { 162280304Sjkim const int *pknids; 163280304Sjkim int npknids; 164280304Sjkim npknids = e->pkey_asn1_meths(e, NULL, &pknids, 0); 165280304Sjkim for (i = 0; i < npknids; i++) { 166280304Sjkim if (e->pkey_asn1_meths(e, &pkm, NULL, pknids[i])) { 167280304Sjkim EVP_PKEY_asn1_free(pkm); 168280304Sjkim } 169280304Sjkim } 170280304Sjkim } 171280304Sjkim} 172238384Sjkim 173280304Sjkim/* 174280304Sjkim * Find a method based on a string. This does a linear search through all 175280304Sjkim * implemented algorithms. This is OK in practice because only a small number 176280304Sjkim * of algorithms are likely to be implemented in an engine and it is not used 177280304Sjkim * for speed critical operations. 178238384Sjkim */ 179238384Sjkim 180238384Sjkimconst EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth_str(ENGINE *e, 181280304Sjkim const char *str, 182280304Sjkim int len) 183280304Sjkim{ 184280304Sjkim int i, nidcount; 185280304Sjkim const int *nids; 186280304Sjkim EVP_PKEY_ASN1_METHOD *ameth; 187280304Sjkim if (!e->pkey_asn1_meths) 188280304Sjkim return NULL; 189280304Sjkim if (len == -1) 190280304Sjkim len = strlen(str); 191280304Sjkim nidcount = e->pkey_asn1_meths(e, NULL, &nids, 0); 192280304Sjkim for (i = 0; i < nidcount; i++) { 193280304Sjkim e->pkey_asn1_meths(e, &ameth, NULL, nids[i]); 194280304Sjkim if (((int)strlen(ameth->pem_str) == len) && 195280304Sjkim !strncasecmp(ameth->pem_str, str, len)) 196280304Sjkim return ameth; 197280304Sjkim } 198280304Sjkim return NULL; 199280304Sjkim} 200238384Sjkim 201280304Sjkimtypedef struct { 202280304Sjkim ENGINE *e; 203280304Sjkim const EVP_PKEY_ASN1_METHOD *ameth; 204280304Sjkim const char *str; 205280304Sjkim int len; 206280304Sjkim} ENGINE_FIND_STR; 207238384Sjkim 208238384Sjkimstatic void look_str_cb(int nid, STACK_OF(ENGINE) *sk, ENGINE *def, void *arg) 209280304Sjkim{ 210280304Sjkim ENGINE_FIND_STR *lk = arg; 211280304Sjkim int i; 212280304Sjkim if (lk->ameth) 213280304Sjkim return; 214280304Sjkim for (i = 0; i < sk_ENGINE_num(sk); i++) { 215280304Sjkim ENGINE *e = sk_ENGINE_value(sk, i); 216280304Sjkim EVP_PKEY_ASN1_METHOD *ameth; 217280304Sjkim e->pkey_asn1_meths(e, &ameth, NULL, nid); 218280304Sjkim if (((int)strlen(ameth->pem_str) == lk->len) && 219280304Sjkim !strncasecmp(ameth->pem_str, lk->str, lk->len)) { 220280304Sjkim lk->e = e; 221280304Sjkim lk->ameth = ameth; 222280304Sjkim return; 223280304Sjkim } 224280304Sjkim } 225280304Sjkim} 226238384Sjkim 227238384Sjkimconst EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe, 228280304Sjkim const char *str, 229280304Sjkim int len) 230280304Sjkim{ 231280304Sjkim ENGINE_FIND_STR fstr; 232280304Sjkim fstr.e = NULL; 233280304Sjkim fstr.ameth = NULL; 234280304Sjkim fstr.str = str; 235280304Sjkim fstr.len = len; 236280304Sjkim CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 237280304Sjkim engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr); 238280304Sjkim /* If found obtain a structural reference to engine */ 239280304Sjkim if (fstr.e) { 240280304Sjkim fstr.e->struct_ref++; 241280304Sjkim engine_ref_debug(fstr.e, 0, 1) 242280304Sjkim } 243280304Sjkim *pe = fstr.e; 244280304Sjkim CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 245280304Sjkim return fstr.ameth; 246280304Sjkim} 247