1109998Smarkm/* crypto/engine/eng_pkey.c */
2109998Smarkm/* ====================================================================
3109998Smarkm * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
4109998Smarkm *
5109998Smarkm * Redistribution and use in source and binary forms, with or without
6109998Smarkm * modification, are permitted provided that the following conditions
7109998Smarkm * are met:
8109998Smarkm *
9109998Smarkm * 1. Redistributions of source code must retain the above copyright
10280304Sjkim *    notice, this list of conditions and the following disclaimer.
11109998Smarkm *
12109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
13109998Smarkm *    notice, this list of conditions and the following disclaimer in
14109998Smarkm *    the documentation and/or other materials provided with the
15109998Smarkm *    distribution.
16109998Smarkm *
17109998Smarkm * 3. All advertising materials mentioning features or use of this
18109998Smarkm *    software must display the following acknowledgment:
19109998Smarkm *    "This product includes software developed by the OpenSSL Project
20109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21109998Smarkm *
22109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23109998Smarkm *    endorse or promote products derived from this software without
24109998Smarkm *    prior written permission. For written permission, please contact
25109998Smarkm *    licensing@OpenSSL.org.
26109998Smarkm *
27109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
28109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
29109998Smarkm *    permission of the OpenSSL Project.
30109998Smarkm *
31109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
32109998Smarkm *    acknowledgment:
33109998Smarkm *    "This product includes software developed by the OpenSSL Project
34109998Smarkm *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35109998Smarkm *
36109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
48109998Smarkm * ====================================================================
49109998Smarkm *
50109998Smarkm * This product includes cryptographic software written by Eric Young
51109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
52109998Smarkm * Hudson (tjh@cryptsoft.com).
53109998Smarkm *
54109998Smarkm */
55109998Smarkm
56109998Smarkm#include "eng_int.h"
57109998Smarkm
58109998Smarkm/* Basic get/set stuff */
59109998Smarkm
60280304Sjkimint ENGINE_set_load_privkey_function(ENGINE *e,
61280304Sjkim                                     ENGINE_LOAD_KEY_PTR loadpriv_f)
62280304Sjkim{
63280304Sjkim    e->load_privkey = loadpriv_f;
64280304Sjkim    return 1;
65280304Sjkim}
66109998Smarkm
67109998Smarkmint ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f)
68280304Sjkim{
69280304Sjkim    e->load_pubkey = loadpub_f;
70280304Sjkim    return 1;
71280304Sjkim}
72109998Smarkm
73194206Ssimonint ENGINE_set_load_ssl_client_cert_function(ENGINE *e,
74280304Sjkim                                             ENGINE_SSL_CLIENT_CERT_PTR
75280304Sjkim                                             loadssl_f)
76280304Sjkim{
77280304Sjkim    e->load_ssl_client_cert = loadssl_f;
78280304Sjkim    return 1;
79280304Sjkim}
80194206Ssimon
81109998SmarkmENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e)
82280304Sjkim{
83280304Sjkim    return e->load_privkey;
84280304Sjkim}
85109998Smarkm
86109998SmarkmENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e)
87280304Sjkim{
88280304Sjkim    return e->load_pubkey;
89280304Sjkim}
90109998Smarkm
91280304SjkimENGINE_SSL_CLIENT_CERT_PTR ENGINE_get_ssl_client_cert_function(const ENGINE
92280304Sjkim                                                               *e)
93280304Sjkim{
94280304Sjkim    return e->load_ssl_client_cert;
95280304Sjkim}
96194206Ssimon
97109998Smarkm/* API functions to load public/private keys */
98109998Smarkm
99109998SmarkmEVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
100280304Sjkim                                  UI_METHOD *ui_method, void *callback_data)
101280304Sjkim{
102280304Sjkim    EVP_PKEY *pkey;
103109998Smarkm
104280304Sjkim    if (e == NULL) {
105280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
106280304Sjkim                  ERR_R_PASSED_NULL_PARAMETER);
107280304Sjkim        return 0;
108280304Sjkim    }
109280304Sjkim    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
110280304Sjkim    if (e->funct_ref == 0) {
111280304Sjkim        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
112280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY, ENGINE_R_NOT_INITIALISED);
113280304Sjkim        return 0;
114280304Sjkim    }
115280304Sjkim    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
116280304Sjkim    if (!e->load_privkey) {
117280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
118280304Sjkim                  ENGINE_R_NO_LOAD_FUNCTION);
119280304Sjkim        return 0;
120280304Sjkim    }
121280304Sjkim    pkey = e->load_privkey(e, key_id, ui_method, callback_data);
122280304Sjkim    if (!pkey) {
123280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_LOAD_PRIVATE_KEY,
124280304Sjkim                  ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
125280304Sjkim        return 0;
126280304Sjkim    }
127280304Sjkim    return pkey;
128280304Sjkim}
129109998Smarkm
130109998SmarkmEVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
131280304Sjkim                                 UI_METHOD *ui_method, void *callback_data)
132280304Sjkim{
133280304Sjkim    EVP_PKEY *pkey;
134109998Smarkm
135280304Sjkim    if (e == NULL) {
136280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
137280304Sjkim                  ERR_R_PASSED_NULL_PARAMETER);
138280304Sjkim        return 0;
139280304Sjkim    }
140280304Sjkim    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
141280304Sjkim    if (e->funct_ref == 0) {
142280304Sjkim        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
143280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY, ENGINE_R_NOT_INITIALISED);
144280304Sjkim        return 0;
145280304Sjkim    }
146280304Sjkim    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
147280304Sjkim    if (!e->load_pubkey) {
148280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY, ENGINE_R_NO_LOAD_FUNCTION);
149280304Sjkim        return 0;
150280304Sjkim    }
151280304Sjkim    pkey = e->load_pubkey(e, key_id, ui_method, callback_data);
152280304Sjkim    if (!pkey) {
153280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_LOAD_PUBLIC_KEY,
154280304Sjkim                  ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
155280304Sjkim        return 0;
156280304Sjkim    }
157280304Sjkim    return pkey;
158280304Sjkim}
159194206Ssimon
160194206Ssimonint ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s,
161280304Sjkim                                STACK_OF(X509_NAME) *ca_dn, X509 **pcert,
162280304Sjkim                                EVP_PKEY **ppkey, STACK_OF(X509) **pother,
163280304Sjkim                                UI_METHOD *ui_method, void *callback_data)
164280304Sjkim{
165194206Ssimon
166280304Sjkim    if (e == NULL) {
167280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
168280304Sjkim                  ERR_R_PASSED_NULL_PARAMETER);
169280304Sjkim        return 0;
170280304Sjkim    }
171280304Sjkim    CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
172280304Sjkim    if (e->funct_ref == 0) {
173280304Sjkim        CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
174280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
175280304Sjkim                  ENGINE_R_NOT_INITIALISED);
176280304Sjkim        return 0;
177280304Sjkim    }
178280304Sjkim    CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
179280304Sjkim    if (!e->load_ssl_client_cert) {
180280304Sjkim        ENGINEerr(ENGINE_F_ENGINE_LOAD_SSL_CLIENT_CERT,
181280304Sjkim                  ENGINE_R_NO_LOAD_FUNCTION);
182280304Sjkim        return 0;
183280304Sjkim    }
184280304Sjkim    return e->load_ssl_client_cert(e, s, ca_dn, pcert, ppkey, pother,
185280304Sjkim                                   ui_method, callback_data);
186280304Sjkim}
187