1219820Sjeff/* crypto/engine/eng_pkey.c */
2219820Sjeff/* ====================================================================
3219820Sjeff * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
4219820Sjeff *
5219820Sjeff * Redistribution and use in source and binary forms, with or without
6219820Sjeff * modification, are permitted provided that the following conditions
7219820Sjeff * are met:
8219820Sjeff *
9219820Sjeff * 1. Redistributions of source code must retain the above copyright
10219820Sjeff *    notice, this list of conditions and the following disclaimer.
11219820Sjeff *
12219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
13219820Sjeff *    notice, this list of conditions and the following disclaimer in
14219820Sjeff *    the documentation and/or other materials provided with the
15219820Sjeff *    distribution.
16219820Sjeff *
17219820Sjeff * 3. All advertising materials mentioning features or use of this
18219820Sjeff *    software must display the following acknowledgment:
19219820Sjeff *    "This product includes software developed by the OpenSSL Project
20219820Sjeff *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21219820Sjeff *
22219820Sjeff * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23219820Sjeff *    endorse or promote products derived from this software without
24219820Sjeff *    prior written permission. For written permission, please contact
25219820Sjeff *    licensing@OpenSSL.org.
26219820Sjeff *
27219820Sjeff * 5. Products derived from this software may not be called "OpenSSL"
28219820Sjeff *    nor may "OpenSSL" appear in their names without prior written
29219820Sjeff *    permission of the OpenSSL Project.
30219820Sjeff *
31219820Sjeff * 6. Redistributions of any form whatsoever must retain the following
32219820Sjeff *    acknowledgment:
33219820Sjeff *    "This product includes software developed by the OpenSSL Project
34219820Sjeff *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35219820Sjeff *
36219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37219820Sjeff * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38219820Sjeff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39219820Sjeff * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40219820Sjeff * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41219820Sjeff * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43219820Sjeff * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44219820Sjeff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45219820Sjeff * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46219820Sjeff * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47219820Sjeff * OF THE POSSIBILITY OF SUCH DAMAGE.
48219820Sjeff * ====================================================================
49219820Sjeff *
50219820Sjeff * This product includes cryptographic software written by Eric Young
51219820Sjeff * (eay@cryptsoft.com).  This product includes software written by Tim
52219820Sjeff * Hudson (tjh@cryptsoft.com).
53219820Sjeff *
54219820Sjeff */
55219820Sjeff
56219820Sjeff#include "eng_int.h"
57219820Sjeff
58219820Sjeff/* Basic get/set stuff */
59219820Sjeff
60219820Sjeffint ENGINE_set_load_privkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpriv_f)
61219820Sjeff	{
62219820Sjeff	e->load_privkey = loadpriv_f;
63219820Sjeff	return 1;
64219820Sjeff	}
65219820Sjeff
66219820Sjeffint ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f)
67219820Sjeff	{
68219820Sjeff	e->load_pubkey = loadpub_f;
69219820Sjeff	return 1;
70219820Sjeff	}
71219820Sjeff
72219820Sjeffint ENGINE_set_load_ssl_client_cert_function(ENGINE *e,
73219820Sjeff				ENGINE_SSL_CLIENT_CERT_PTR loadssl_f)
74219820Sjeff	{
75219820Sjeff	e->load_ssl_client_cert = loadssl_f;
76219820Sjeff	return 1;
77219820Sjeff	}
78219820Sjeff
79219820SjeffENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e)
80219820Sjeff	{
81219820Sjeff	return e->load_privkey;
82219820Sjeff	}
83219820Sjeff
84219820SjeffENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e)
85219820Sjeff	{
86219820Sjeff	return e->load_pubkey;
87219820Sjeff	}
88219820Sjeff
89219820SjeffENGINE_SSL_CLIENT_CERT_PTR ENGINE_get_ssl_client_cert_function(const ENGINE *e)
90219820Sjeff	{
91219820Sjeff	return e->load_ssl_client_cert;
92219820Sjeff	}
93219820Sjeff
94219820Sjeff/* API functions to load public/private keys */
95219820Sjeff
96219820SjeffEVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
97219820Sjeff	UI_METHOD *ui_method, void *callback_data)
98219820Sjeff	{
99219820Sjeff	EVP_PKEY *pkey;
100219820Sjeff
101219820Sjeff	if(e == NULL)
102219820Sjeff		{
103219820Sjeff		ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
104219820Sjeff			ERR_R_PASSED_NULL_PARAMETER);
105219820Sjeff		return 0;
106219820Sjeff		}
107219820Sjeff	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
108219820Sjeff	if(e->funct_ref == 0)
109219820Sjeff		{
110219820Sjeff		CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
111219820Sjeff		ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
112219820Sjeff			ENGINE_R_NOT_INITIALISED);
113219820Sjeff		return 0;
114219820Sjeff		}
115219820Sjeff	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
116219820Sjeff	if (!e->load_privkey)
117219820Sjeff		{
118219820Sjeff		ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
119219820Sjeff			ENGINE_R_NO_LOAD_FUNCTION);
120219820Sjeff		return 0;
121219820Sjeff		}
122219820Sjeff	pkey = e->load_privkey(e, key_id, ui_method, callback_data);
123219820Sjeff	if (!pkey)
124219820Sjeff		{
125219820Sjeff		ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
126219820Sjeff			ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
127219820Sjeff		return 0;
128219820Sjeff		}
129219820Sjeff	return pkey;
130219820Sjeff	}
131219820Sjeff
132219820SjeffEVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
133219820Sjeff	UI_METHOD *ui_method, void *callback_data)
134219820Sjeff	{
135219820Sjeff	EVP_PKEY *pkey;
136219820Sjeff
137219820Sjeff	if(e == NULL)
138219820Sjeff		{
139219820Sjeff		ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
140219820Sjeff			ERR_R_PASSED_NULL_PARAMETER);
141219820Sjeff		return 0;
142219820Sjeff		}
143219820Sjeff	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
144219820Sjeff	if(e->funct_ref == 0)
145219820Sjeff		{
146219820Sjeff		CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
147219820Sjeff		ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
148219820Sjeff			ENGINE_R_NOT_INITIALISED);
149219820Sjeff		return 0;
150219820Sjeff		}
151219820Sjeff	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
152219820Sjeff	if (!e->load_pubkey)
153219820Sjeff		{
154219820Sjeff		ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
155219820Sjeff			ENGINE_R_NO_LOAD_FUNCTION);
156219820Sjeff		return 0;
157219820Sjeff		}
158219820Sjeff	pkey = e->load_pubkey(e, key_id, ui_method, callback_data);
159219820Sjeff	if (!pkey)
160219820Sjeff		{
161219820Sjeff		ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
162219820Sjeff			ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
163219820Sjeff		return 0;
164219820Sjeff		}
165219820Sjeff	return pkey;
166219820Sjeff	}
167219820Sjeff
168219820Sjeffint ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s,
169219820Sjeff	STACK_OF(X509_NAME) *ca_dn, X509 **pcert, EVP_PKEY **ppkey,
170219820Sjeff	STACK_OF(X509) **pother, UI_METHOD *ui_method, void *callback_data)
171219820Sjeff	{
172219820Sjeff
173219820Sjeff	if(e == NULL)
174219820Sjeff		{
175219820Sjeff		ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
176219820Sjeff			ERR_R_PASSED_NULL_PARAMETER);
177219820Sjeff		return 0;
178219820Sjeff		}
179219820Sjeff	CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
180219820Sjeff	if(e->funct_ref == 0)
181219820Sjeff		{
182219820Sjeff		CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
183219820Sjeff		ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
184219820Sjeff			ENGINE_R_NOT_INITIALISED);
185219820Sjeff		return 0;
186219820Sjeff		}
187219820Sjeff	CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
188219820Sjeff	if (!e->load_ssl_client_cert)
189219820Sjeff		{
190219820Sjeff		ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
191219820Sjeff			ENGINE_R_NO_LOAD_FUNCTION);
192219820Sjeff		return 0;
193219820Sjeff		}
194219820Sjeff	return e->load_ssl_client_cert(e, s, ca_dn, pcert, ppkey, pother,
195219820Sjeff					ui_method, callback_data);
196219820Sjeff	}
197219820Sjeff