xref: /freebsd/crypto/openssl/crypto/engine/eng_pkey.c (revision db522d3ae42d8f706499b4b4bc97836292ab180b)
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 
605c87c606SMark Murray int ENGINE_set_load_privkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpriv_f)
615c87c606SMark Murray 	{
625c87c606SMark Murray 	e->load_privkey = loadpriv_f;
635c87c606SMark Murray 	return 1;
645c87c606SMark Murray 	}
655c87c606SMark Murray 
665c87c606SMark Murray int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f)
675c87c606SMark Murray 	{
685c87c606SMark Murray 	e->load_pubkey = loadpub_f;
695c87c606SMark Murray 	return 1;
705c87c606SMark Murray 	}
715c87c606SMark Murray 
72db522d3aSSimon L. B. Nielsen int ENGINE_set_load_ssl_client_cert_function(ENGINE *e,
73db522d3aSSimon L. B. Nielsen 				ENGINE_SSL_CLIENT_CERT_PTR loadssl_f)
74db522d3aSSimon L. B. Nielsen 	{
75db522d3aSSimon L. B. Nielsen 	e->load_ssl_client_cert = loadssl_f;
76db522d3aSSimon L. B. Nielsen 	return 1;
77db522d3aSSimon L. B. Nielsen 	}
78db522d3aSSimon L. B. Nielsen 
795c87c606SMark Murray ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e)
805c87c606SMark Murray 	{
815c87c606SMark Murray 	return e->load_privkey;
825c87c606SMark Murray 	}
835c87c606SMark Murray 
845c87c606SMark Murray ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e)
855c87c606SMark Murray 	{
865c87c606SMark Murray 	return e->load_pubkey;
875c87c606SMark Murray 	}
885c87c606SMark Murray 
89db522d3aSSimon L. B. Nielsen ENGINE_SSL_CLIENT_CERT_PTR ENGINE_get_ssl_client_cert_function(const ENGINE *e)
90db522d3aSSimon L. B. Nielsen 	{
91db522d3aSSimon L. B. Nielsen 	return e->load_ssl_client_cert;
92db522d3aSSimon L. B. Nielsen 	}
93db522d3aSSimon L. B. Nielsen 
945c87c606SMark Murray /* API functions to load public/private keys */
955c87c606SMark Murray 
965c87c606SMark Murray EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
975c87c606SMark Murray 	UI_METHOD *ui_method, void *callback_data)
985c87c606SMark Murray 	{
995c87c606SMark Murray 	EVP_PKEY *pkey;
1005c87c606SMark Murray 
1015c87c606SMark Murray 	if(e == NULL)
1025c87c606SMark Murray 		{
1035c87c606SMark Murray 		ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
1045c87c606SMark Murray 			ERR_R_PASSED_NULL_PARAMETER);
1055c87c606SMark Murray 		return 0;
1065c87c606SMark Murray 		}
1075c87c606SMark Murray 	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
1085c87c606SMark Murray 	if(e->funct_ref == 0)
1095c87c606SMark Murray 		{
1105c87c606SMark Murray 		CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1115c87c606SMark Murray 		ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
1125c87c606SMark Murray 			ENGINE_R_NOT_INITIALISED);
1135c87c606SMark Murray 		return 0;
1145c87c606SMark Murray 		}
1155c87c606SMark Murray 	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1165c87c606SMark Murray 	if (!e->load_privkey)
1175c87c606SMark Murray 		{
1185c87c606SMark Murray 		ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
1195c87c606SMark Murray 			ENGINE_R_NO_LOAD_FUNCTION);
1205c87c606SMark Murray 		return 0;
1215c87c606SMark Murray 		}
1225c87c606SMark Murray 	pkey = e->load_privkey(e, key_id, ui_method, callback_data);
1235c87c606SMark Murray 	if (!pkey)
1245c87c606SMark Murray 		{
1255c87c606SMark Murray 		ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
1265c87c606SMark Murray 			ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
1275c87c606SMark Murray 		return 0;
1285c87c606SMark Murray 		}
1295c87c606SMark Murray 	return pkey;
1305c87c606SMark Murray 	}
1315c87c606SMark Murray 
1325c87c606SMark Murray EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
1335c87c606SMark Murray 	UI_METHOD *ui_method, void *callback_data)
1345c87c606SMark Murray 	{
1355c87c606SMark Murray 	EVP_PKEY *pkey;
1365c87c606SMark Murray 
1375c87c606SMark Murray 	if(e == NULL)
1385c87c606SMark Murray 		{
1395c87c606SMark Murray 		ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
1405c87c606SMark Murray 			ERR_R_PASSED_NULL_PARAMETER);
1415c87c606SMark Murray 		return 0;
1425c87c606SMark Murray 		}
1435c87c606SMark Murray 	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
1445c87c606SMark Murray 	if(e->funct_ref == 0)
1455c87c606SMark Murray 		{
1465c87c606SMark Murray 		CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1475c87c606SMark Murray 		ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
1485c87c606SMark Murray 			ENGINE_R_NOT_INITIALISED);
1495c87c606SMark Murray 		return 0;
1505c87c606SMark Murray 		}
1515c87c606SMark Murray 	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1525c87c606SMark Murray 	if (!e->load_pubkey)
1535c87c606SMark Murray 		{
1545c87c606SMark Murray 		ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
1555c87c606SMark Murray 			ENGINE_R_NO_LOAD_FUNCTION);
1565c87c606SMark Murray 		return 0;
1575c87c606SMark Murray 		}
1585c87c606SMark Murray 	pkey = e->load_pubkey(e, key_id, ui_method, callback_data);
1595c87c606SMark Murray 	if (!pkey)
1605c87c606SMark Murray 		{
1615c87c606SMark Murray 		ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
1625c87c606SMark Murray 			ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
1635c87c606SMark Murray 		return 0;
1645c87c606SMark Murray 		}
1655c87c606SMark Murray 	return pkey;
1665c87c606SMark Murray 	}
167db522d3aSSimon L. B. Nielsen 
168db522d3aSSimon L. B. Nielsen int ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s,
169db522d3aSSimon L. B. Nielsen 	STACK_OF(X509_NAME) *ca_dn, X509 **pcert, EVP_PKEY **ppkey,
170db522d3aSSimon L. B. Nielsen 	STACK_OF(X509) **pother, UI_METHOD *ui_method, void *callback_data)
171db522d3aSSimon L. B. Nielsen 	{
172db522d3aSSimon L. B. Nielsen 
173db522d3aSSimon L. B. Nielsen 	if(e == NULL)
174db522d3aSSimon L. B. Nielsen 		{
175db522d3aSSimon L. B. Nielsen 		ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
176db522d3aSSimon L. B. Nielsen 			ERR_R_PASSED_NULL_PARAMETER);
177db522d3aSSimon L. B. Nielsen 		return 0;
178db522d3aSSimon L. B. Nielsen 		}
179db522d3aSSimon L. B. Nielsen 	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
180db522d3aSSimon L. B. Nielsen 	if(e->funct_ref == 0)
181db522d3aSSimon L. B. Nielsen 		{
182db522d3aSSimon L. B. Nielsen 		CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
183db522d3aSSimon L. B. Nielsen 		ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
184db522d3aSSimon L. B. Nielsen 			ENGINE_R_NOT_INITIALISED);
185db522d3aSSimon L. B. Nielsen 		return 0;
186db522d3aSSimon L. B. Nielsen 		}
187db522d3aSSimon L. B. Nielsen 	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
188db522d3aSSimon L. B. Nielsen 	if (!e->load_ssl_client_cert)
189db522d3aSSimon L. B. Nielsen 		{
190db522d3aSSimon L. B. Nielsen 		ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
191db522d3aSSimon L. B. Nielsen 			ENGINE_R_NO_LOAD_FUNCTION);
192db522d3aSSimon L. B. Nielsen 		return 0;
193db522d3aSSimon L. B. Nielsen 		}
194db522d3aSSimon L. B. Nielsen 	return e->load_ssl_client_cert(e, s, ca_dn, pcert, ppkey, pother,
195db522d3aSSimon L. B. Nielsen 					ui_method, callback_data);
196db522d3aSSimon L. B. Nielsen 	}
197