15c87c606SMark Murray /* crypto/engine/eng_pkey.c */ 25c87c606SMark Murray /* ==================================================================== 35c87c606SMark Murray * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. 45c87c606SMark Murray * 55c87c606SMark Murray * Redistribution and use in source and binary forms, with or without 65c87c606SMark Murray * modification, are permitted provided that the following conditions 75c87c606SMark Murray * are met: 85c87c606SMark Murray * 95c87c606SMark Murray * 1. Redistributions of source code must retain the above copyright 105c87c606SMark Murray * notice, this list of conditions and the following disclaimer. 115c87c606SMark Murray * 125c87c606SMark Murray * 2. Redistributions in binary form must reproduce the above copyright 135c87c606SMark Murray * notice, this list of conditions and the following disclaimer in 145c87c606SMark Murray * the documentation and/or other materials provided with the 155c87c606SMark Murray * distribution. 165c87c606SMark Murray * 175c87c606SMark Murray * 3. All advertising materials mentioning features or use of this 185c87c606SMark Murray * software must display the following acknowledgment: 195c87c606SMark Murray * "This product includes software developed by the OpenSSL Project 205c87c606SMark Murray * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 215c87c606SMark Murray * 225c87c606SMark Murray * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 235c87c606SMark Murray * endorse or promote products derived from this software without 245c87c606SMark Murray * prior written permission. For written permission, please contact 255c87c606SMark Murray * licensing@OpenSSL.org. 265c87c606SMark Murray * 275c87c606SMark Murray * 5. Products derived from this software may not be called "OpenSSL" 285c87c606SMark Murray * nor may "OpenSSL" appear in their names without prior written 295c87c606SMark Murray * permission of the OpenSSL Project. 305c87c606SMark Murray * 315c87c606SMark Murray * 6. Redistributions of any form whatsoever must retain the following 325c87c606SMark Murray * acknowledgment: 335c87c606SMark Murray * "This product includes software developed by the OpenSSL Project 345c87c606SMark Murray * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 355c87c606SMark Murray * 365c87c606SMark Murray * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 375c87c606SMark Murray * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 385c87c606SMark Murray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 395c87c606SMark Murray * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 405c87c606SMark Murray * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 415c87c606SMark Murray * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 425c87c606SMark Murray * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 435c87c606SMark Murray * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 445c87c606SMark Murray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 455c87c606SMark Murray * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 465c87c606SMark Murray * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 475c87c606SMark Murray * OF THE POSSIBILITY OF SUCH DAMAGE. 485c87c606SMark Murray * ==================================================================== 495c87c606SMark Murray * 505c87c606SMark Murray * This product includes cryptographic software written by Eric Young 515c87c606SMark Murray * (eay@cryptsoft.com). This product includes software written by Tim 525c87c606SMark Murray * Hudson (tjh@cryptsoft.com). 535c87c606SMark Murray * 545c87c606SMark Murray */ 555c87c606SMark Murray 565c87c606SMark Murray #include "eng_int.h" 575c87c606SMark Murray 585c87c606SMark Murray /* Basic get/set stuff */ 595c87c606SMark Murray 60*6f9291ceSJung-uk Kim int ENGINE_set_load_privkey_function(ENGINE *e, 61*6f9291ceSJung-uk Kim ENGINE_LOAD_KEY_PTR loadpriv_f) 625c87c606SMark Murray { 635c87c606SMark Murray e->load_privkey = loadpriv_f; 645c87c606SMark Murray return 1; 655c87c606SMark Murray } 665c87c606SMark Murray 675c87c606SMark Murray int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f) 685c87c606SMark Murray { 695c87c606SMark Murray e->load_pubkey = loadpub_f; 705c87c606SMark Murray return 1; 715c87c606SMark Murray } 725c87c606SMark Murray 73db522d3aSSimon L. B. Nielsen int ENGINE_set_load_ssl_client_cert_function(ENGINE *e, 74*6f9291ceSJung-uk Kim ENGINE_SSL_CLIENT_CERT_PTR 75*6f9291ceSJung-uk Kim loadssl_f) 76db522d3aSSimon L. B. Nielsen { 77db522d3aSSimon L. B. Nielsen e->load_ssl_client_cert = loadssl_f; 78db522d3aSSimon L. B. Nielsen return 1; 79db522d3aSSimon L. B. Nielsen } 80db522d3aSSimon L. B. Nielsen 815c87c606SMark Murray ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e) 825c87c606SMark Murray { 835c87c606SMark Murray return e->load_privkey; 845c87c606SMark Murray } 855c87c606SMark Murray 865c87c606SMark Murray ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e) 875c87c606SMark Murray { 885c87c606SMark Murray return e->load_pubkey; 895c87c606SMark Murray } 905c87c606SMark Murray 91*6f9291ceSJung-uk Kim ENGINE_SSL_CLIENT_CERT_PTR ENGINE_get_ssl_client_cert_function(const ENGINE 92*6f9291ceSJung-uk Kim *e) 93db522d3aSSimon L. B. Nielsen { 94db522d3aSSimon L. B. Nielsen return e->load_ssl_client_cert; 95db522d3aSSimon L. B. Nielsen } 96db522d3aSSimon L. B. Nielsen 975c87c606SMark Murray /* API functions to load public/private keys */ 985c87c606SMark Murray 995c87c606SMark Murray EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id, 1005c87c606SMark Murray UI_METHOD *ui_method, void *callback_data) 1015c87c606SMark Murray { 1025c87c606SMark Murray EVP_PKEY *pkey; 1035c87c606SMark Murray 104*6f9291ceSJung-uk Kim if (e == NULL) { 1055c87c606SMark Murray ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY, 1065c87c606SMark Murray ERR_R_PASSED_NULL_PARAMETER); 1075c87c606SMark Murray return 0; 1085c87c606SMark Murray } 1095c87c606SMark Murray CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 110*6f9291ceSJung-uk Kim if (e->funct_ref == 0) { 1115c87c606SMark Murray CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 112*6f9291ceSJung-uk Kim ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY, ENGINE_R_NOT_INITIALISED); 1135c87c606SMark Murray return 0; 1145c87c606SMark Murray } 1155c87c606SMark Murray CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 116*6f9291ceSJung-uk Kim if (!e->load_privkey) { 1175c87c606SMark Murray ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY, 1185c87c606SMark Murray ENGINE_R_NO_LOAD_FUNCTION); 1195c87c606SMark Murray return 0; 1205c87c606SMark Murray } 1215c87c606SMark Murray pkey = e->load_privkey(e, key_id, ui_method, callback_data); 122*6f9291ceSJung-uk Kim if (!pkey) { 1235c87c606SMark Murray ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY, 1245c87c606SMark Murray ENGINE_R_FAILED_LOADING_PRIVATE_KEY); 1255c87c606SMark Murray return 0; 1265c87c606SMark Murray } 1275c87c606SMark Murray return pkey; 1285c87c606SMark Murray } 1295c87c606SMark Murray 1305c87c606SMark Murray EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id, 1315c87c606SMark Murray UI_METHOD *ui_method, void *callback_data) 1325c87c606SMark Murray { 1335c87c606SMark Murray EVP_PKEY *pkey; 1345c87c606SMark Murray 135*6f9291ceSJung-uk Kim if (e == NULL) { 1365c87c606SMark Murray ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY, 1375c87c606SMark Murray ERR_R_PASSED_NULL_PARAMETER); 1385c87c606SMark Murray return 0; 1395c87c606SMark Murray } 1405c87c606SMark Murray CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 141*6f9291ceSJung-uk Kim if (e->funct_ref == 0) { 1425c87c606SMark Murray CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 143*6f9291ceSJung-uk Kim ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY, ENGINE_R_NOT_INITIALISED); 1445c87c606SMark Murray return 0; 1455c87c606SMark Murray } 1465c87c606SMark Murray CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 147*6f9291ceSJung-uk Kim if (!e->load_pubkey) { 148*6f9291ceSJung-uk Kim ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY, ENGINE_R_NO_LOAD_FUNCTION); 1495c87c606SMark Murray return 0; 1505c87c606SMark Murray } 1515c87c606SMark Murray pkey = e->load_pubkey(e, key_id, ui_method, callback_data); 152*6f9291ceSJung-uk Kim if (!pkey) { 1535c87c606SMark Murray ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY, 1545c87c606SMark Murray ENGINE_R_FAILED_LOADING_PUBLIC_KEY); 1555c87c606SMark Murray return 0; 1565c87c606SMark Murray } 1575c87c606SMark Murray return pkey; 1585c87c606SMark Murray } 159db522d3aSSimon L. B. Nielsen 160db522d3aSSimon L. B. Nielsen int ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s, 161*6f9291ceSJung-uk Kim STACK_OF(X509_NAME) *ca_dn, X509 **pcert, 162*6f9291ceSJung-uk Kim EVP_PKEY **ppkey, STACK_OF(X509) **pother, 163*6f9291ceSJung-uk Kim UI_METHOD *ui_method, void *callback_data) 164db522d3aSSimon L. B. Nielsen { 165db522d3aSSimon L. B. Nielsen 166*6f9291ceSJung-uk Kim if (e == NULL) { 167db522d3aSSimon L. B. Nielsen ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT, 168db522d3aSSimon L. B. Nielsen ERR_R_PASSED_NULL_PARAMETER); 169db522d3aSSimon L. B. Nielsen return 0; 170db522d3aSSimon L. B. Nielsen } 171db522d3aSSimon L. B. Nielsen CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); 172*6f9291ceSJung-uk Kim if (e->funct_ref == 0) { 173db522d3aSSimon L. B. Nielsen CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 174db522d3aSSimon L. B. Nielsen ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT, 175db522d3aSSimon L. B. Nielsen ENGINE_R_NOT_INITIALISED); 176db522d3aSSimon L. B. Nielsen return 0; 177db522d3aSSimon L. B. Nielsen } 178db522d3aSSimon L. B. Nielsen CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); 179*6f9291ceSJung-uk Kim if (!e->load_ssl_client_cert) { 180db522d3aSSimon L. B. Nielsen ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT, 181db522d3aSSimon L. B. Nielsen ENGINE_R_NO_LOAD_FUNCTION); 182db522d3aSSimon L. B. Nielsen return 0; 183db522d3aSSimon L. B. Nielsen } 184db522d3aSSimon L. B. Nielsen return e->load_ssl_client_cert(e, s, ca_dn, pcert, ppkey, pother, 185db522d3aSSimon L. B. Nielsen ui_method, callback_data); 186db522d3aSSimon L. B. Nielsen } 187