xref: /freebsd/crypto/openssl/crypto/engine/eng_pkey.c (revision b077aed33b7b6aefca7b17ddb250cf521f938613)
1e71b7053SJung-uk Kim /*
2*b077aed3SPierre Pronchery  * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
35c87c606SMark Murray  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5e71b7053SJung-uk Kim  * this file except in compliance with the License.  You can obtain a copy
6e71b7053SJung-uk Kim  * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim  * https://www.openssl.org/source/license.html
85c87c606SMark Murray  */
95c87c606SMark Murray 
10*b077aed3SPierre Pronchery /* We need to use some engine deprecated APIs */
11*b077aed3SPierre Pronchery #define OPENSSL_SUPPRESS_DEPRECATED
12*b077aed3SPierre Pronchery 
1317f01e99SJung-uk Kim #include "eng_local.h"
145c87c606SMark Murray 
155c87c606SMark Murray /* Basic get/set stuff */
165c87c606SMark Murray 
176f9291ceSJung-uk Kim int ENGINE_set_load_privkey_function(ENGINE *e,
186f9291ceSJung-uk Kim                                      ENGINE_LOAD_KEY_PTR loadpriv_f)
195c87c606SMark Murray {
205c87c606SMark Murray     e->load_privkey = loadpriv_f;
215c87c606SMark Murray     return 1;
225c87c606SMark Murray }
235c87c606SMark Murray 
245c87c606SMark Murray int ENGINE_set_load_pubkey_function(ENGINE *e, ENGINE_LOAD_KEY_PTR loadpub_f)
255c87c606SMark Murray {
265c87c606SMark Murray     e->load_pubkey = loadpub_f;
275c87c606SMark Murray     return 1;
285c87c606SMark Murray }
295c87c606SMark Murray 
30db522d3aSSimon L. B. Nielsen int ENGINE_set_load_ssl_client_cert_function(ENGINE *e,
316f9291ceSJung-uk Kim                                              ENGINE_SSL_CLIENT_CERT_PTR
326f9291ceSJung-uk Kim                                              loadssl_f)
33db522d3aSSimon L. B. Nielsen {
34db522d3aSSimon L. B. Nielsen     e->load_ssl_client_cert = loadssl_f;
35db522d3aSSimon L. B. Nielsen     return 1;
36db522d3aSSimon L. B. Nielsen }
37db522d3aSSimon L. B. Nielsen 
385c87c606SMark Murray ENGINE_LOAD_KEY_PTR ENGINE_get_load_privkey_function(const ENGINE *e)
395c87c606SMark Murray {
405c87c606SMark Murray     return e->load_privkey;
415c87c606SMark Murray }
425c87c606SMark Murray 
435c87c606SMark Murray ENGINE_LOAD_KEY_PTR ENGINE_get_load_pubkey_function(const ENGINE *e)
445c87c606SMark Murray {
455c87c606SMark Murray     return e->load_pubkey;
465c87c606SMark Murray }
475c87c606SMark Murray 
486f9291ceSJung-uk Kim ENGINE_SSL_CLIENT_CERT_PTR ENGINE_get_ssl_client_cert_function(const ENGINE
496f9291ceSJung-uk Kim                                                                *e)
50db522d3aSSimon L. B. Nielsen {
51db522d3aSSimon L. B. Nielsen     return e->load_ssl_client_cert;
52db522d3aSSimon L. B. Nielsen }
53db522d3aSSimon L. B. Nielsen 
545c87c606SMark Murray /* API functions to load public/private keys */
555c87c606SMark Murray 
565c87c606SMark Murray EVP_PKEY *ENGINE_load_private_key(ENGINE *e, const char *key_id,
575c87c606SMark Murray                                   UI_METHOD *ui_method, void *callback_data)
585c87c606SMark Murray {
595c87c606SMark Murray     EVP_PKEY *pkey;
605c87c606SMark Murray 
616f9291ceSJung-uk Kim     if (e == NULL) {
62*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
63*b077aed3SPierre Pronchery         return NULL;
645c87c606SMark Murray     }
65*b077aed3SPierre Pronchery     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
66*b077aed3SPierre Pronchery         return NULL;
676f9291ceSJung-uk Kim     if (e->funct_ref == 0) {
68e71b7053SJung-uk Kim         CRYPTO_THREAD_unlock(global_engine_lock);
69*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NOT_INITIALISED);
70*b077aed3SPierre Pronchery         return NULL;
715c87c606SMark Murray     }
72e71b7053SJung-uk Kim     CRYPTO_THREAD_unlock(global_engine_lock);
736f9291ceSJung-uk Kim     if (!e->load_privkey) {
74*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_LOAD_FUNCTION);
75*b077aed3SPierre Pronchery         return NULL;
765c87c606SMark Murray     }
775c87c606SMark Murray     pkey = e->load_privkey(e, key_id, ui_method, callback_data);
78*b077aed3SPierre Pronchery     if (pkey == NULL) {
79*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
80*b077aed3SPierre Pronchery         return NULL;
815c87c606SMark Murray     }
825c87c606SMark Murray     return pkey;
835c87c606SMark Murray }
845c87c606SMark Murray 
855c87c606SMark Murray EVP_PKEY *ENGINE_load_public_key(ENGINE *e, const char *key_id,
865c87c606SMark Murray                                  UI_METHOD *ui_method, void *callback_data)
875c87c606SMark Murray {
885c87c606SMark Murray     EVP_PKEY *pkey;
895c87c606SMark Murray 
906f9291ceSJung-uk Kim     if (e == NULL) {
91*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
92*b077aed3SPierre Pronchery         return NULL;
935c87c606SMark Murray     }
94*b077aed3SPierre Pronchery     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
95*b077aed3SPierre Pronchery         return NULL;
966f9291ceSJung-uk Kim     if (e->funct_ref == 0) {
97e71b7053SJung-uk Kim         CRYPTO_THREAD_unlock(global_engine_lock);
98*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NOT_INITIALISED);
99*b077aed3SPierre Pronchery         return NULL;
1005c87c606SMark Murray     }
101e71b7053SJung-uk Kim     CRYPTO_THREAD_unlock(global_engine_lock);
1026f9291ceSJung-uk Kim     if (!e->load_pubkey) {
103*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_LOAD_FUNCTION);
104*b077aed3SPierre Pronchery         return NULL;
1055c87c606SMark Murray     }
1065c87c606SMark Murray     pkey = e->load_pubkey(e, key_id, ui_method, callback_data);
107*b077aed3SPierre Pronchery     if (pkey == NULL) {
108*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
109*b077aed3SPierre Pronchery         return NULL;
1105c87c606SMark Murray     }
1115c87c606SMark Murray     return pkey;
1125c87c606SMark Murray }
113db522d3aSSimon L. B. Nielsen 
114db522d3aSSimon L. B. Nielsen int ENGINE_load_ssl_client_cert(ENGINE *e, SSL *s,
1156f9291ceSJung-uk Kim                                 STACK_OF(X509_NAME) *ca_dn, X509 **pcert,
1166f9291ceSJung-uk Kim                                 EVP_PKEY **ppkey, STACK_OF(X509) **pother,
1176f9291ceSJung-uk Kim                                 UI_METHOD *ui_method, void *callback_data)
118db522d3aSSimon L. B. Nielsen {
119db522d3aSSimon L. B. Nielsen 
1206f9291ceSJung-uk Kim     if (e == NULL) {
121*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_ENGINE, ERR_R_PASSED_NULL_PARAMETER);
122db522d3aSSimon L. B. Nielsen         return 0;
123db522d3aSSimon L. B. Nielsen     }
124*b077aed3SPierre Pronchery     if (!CRYPTO_THREAD_write_lock(global_engine_lock))
125*b077aed3SPierre Pronchery         return 0;
1266f9291ceSJung-uk Kim     if (e->funct_ref == 0) {
127e71b7053SJung-uk Kim         CRYPTO_THREAD_unlock(global_engine_lock);
128*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NOT_INITIALISED);
129db522d3aSSimon L. B. Nielsen         return 0;
130db522d3aSSimon L. B. Nielsen     }
131e71b7053SJung-uk Kim     CRYPTO_THREAD_unlock(global_engine_lock);
1326f9291ceSJung-uk Kim     if (!e->load_ssl_client_cert) {
133*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_ENGINE, ENGINE_R_NO_LOAD_FUNCTION);
134db522d3aSSimon L. B. Nielsen         return 0;
135db522d3aSSimon L. B. Nielsen     }
136db522d3aSSimon L. B. Nielsen     return e->load_ssl_client_cert(e, s, ca_dn, pcert, ppkey, pother,
137db522d3aSSimon L. B. Nielsen                                    ui_method, callback_data);
138db522d3aSSimon L. B. Nielsen }
139