1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery *
4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5*b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy
6*b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery */
9*b077aed3SPierre Pronchery
10*b077aed3SPierre Pronchery /*
11*b077aed3SPierre Pronchery * Low level APIs are deprecated for public use, but still ok for internal use.
12*b077aed3SPierre Pronchery */
13*b077aed3SPierre Pronchery #include "internal/deprecated.h"
14*b077aed3SPierre Pronchery
15*b077aed3SPierre Pronchery #include <string.h>
16*b077aed3SPierre Pronchery #include <openssl/core.h>
17*b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
18*b077aed3SPierre Pronchery #include <openssl/core_names.h>
19*b077aed3SPierre Pronchery #include <openssl/params.h>
20*b077aed3SPierre Pronchery #include <openssl/err.h>
21*b077aed3SPierre Pronchery #include <openssl/pem.h> /* Functions for writing MSBLOB and PVK */
22*b077aed3SPierre Pronchery #include <openssl/dsa.h>
23*b077aed3SPierre Pronchery #include "internal/passphrase.h"
24*b077aed3SPierre Pronchery #include "crypto/rsa.h"
25*b077aed3SPierre Pronchery #include "prov/implementations.h"
26*b077aed3SPierre Pronchery #include "prov/bio.h"
27*b077aed3SPierre Pronchery #include "prov/provider_ctx.h"
28*b077aed3SPierre Pronchery #include "endecoder_local.h"
29*b077aed3SPierre Pronchery
30*b077aed3SPierre Pronchery struct key2ms_ctx_st {
31*b077aed3SPierre Pronchery PROV_CTX *provctx;
32*b077aed3SPierre Pronchery
33*b077aed3SPierre Pronchery int pvk_encr_level;
34*b077aed3SPierre Pronchery
35*b077aed3SPierre Pronchery struct ossl_passphrase_data_st pwdata;
36*b077aed3SPierre Pronchery };
37*b077aed3SPierre Pronchery
write_msblob(struct key2ms_ctx_st * ctx,OSSL_CORE_BIO * cout,EVP_PKEY * pkey,int ispub)38*b077aed3SPierre Pronchery static int write_msblob(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout,
39*b077aed3SPierre Pronchery EVP_PKEY *pkey, int ispub)
40*b077aed3SPierre Pronchery {
41*b077aed3SPierre Pronchery BIO *out = ossl_bio_new_from_core_bio(ctx->provctx, cout);
42*b077aed3SPierre Pronchery int ret;
43*b077aed3SPierre Pronchery
44*b077aed3SPierre Pronchery if (out == NULL)
45*b077aed3SPierre Pronchery return 0;
46*b077aed3SPierre Pronchery ret = ispub ? i2b_PublicKey_bio(out, pkey) : i2b_PrivateKey_bio(out, pkey);
47*b077aed3SPierre Pronchery
48*b077aed3SPierre Pronchery BIO_free(out);
49*b077aed3SPierre Pronchery return ret;
50*b077aed3SPierre Pronchery }
51*b077aed3SPierre Pronchery
write_pvk(struct key2ms_ctx_st * ctx,OSSL_CORE_BIO * cout,EVP_PKEY * pkey)52*b077aed3SPierre Pronchery static int write_pvk(struct key2ms_ctx_st *ctx, OSSL_CORE_BIO *cout,
53*b077aed3SPierre Pronchery EVP_PKEY *pkey)
54*b077aed3SPierre Pronchery {
55*b077aed3SPierre Pronchery BIO *out = NULL;
56*b077aed3SPierre Pronchery int ret;
57*b077aed3SPierre Pronchery OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
58*b077aed3SPierre Pronchery
59*b077aed3SPierre Pronchery out = ossl_bio_new_from_core_bio(ctx->provctx, cout);
60*b077aed3SPierre Pronchery if (out == NULL)
61*b077aed3SPierre Pronchery return 0;
62*b077aed3SPierre Pronchery ret = i2b_PVK_bio_ex(out, pkey, ctx->pvk_encr_level,
63*b077aed3SPierre Pronchery ossl_pw_pvk_password, &ctx->pwdata, libctx, NULL);
64*b077aed3SPierre Pronchery BIO_free(out);
65*b077aed3SPierre Pronchery return ret;
66*b077aed3SPierre Pronchery }
67*b077aed3SPierre Pronchery
68*b077aed3SPierre Pronchery static OSSL_FUNC_encoder_freectx_fn key2ms_freectx;
69*b077aed3SPierre Pronchery static OSSL_FUNC_encoder_does_selection_fn key2ms_does_selection;
70*b077aed3SPierre Pronchery
key2ms_newctx(void * provctx)71*b077aed3SPierre Pronchery static struct key2ms_ctx_st *key2ms_newctx(void *provctx)
72*b077aed3SPierre Pronchery {
73*b077aed3SPierre Pronchery struct key2ms_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
74*b077aed3SPierre Pronchery
75*b077aed3SPierre Pronchery if (ctx != NULL) {
76*b077aed3SPierre Pronchery ctx->provctx = provctx;
77*b077aed3SPierre Pronchery /* This is the strongest encryption level */
78*b077aed3SPierre Pronchery ctx->pvk_encr_level = 2;
79*b077aed3SPierre Pronchery }
80*b077aed3SPierre Pronchery return ctx;
81*b077aed3SPierre Pronchery }
82*b077aed3SPierre Pronchery
key2ms_freectx(void * vctx)83*b077aed3SPierre Pronchery static void key2ms_freectx(void *vctx)
84*b077aed3SPierre Pronchery {
85*b077aed3SPierre Pronchery struct key2ms_ctx_st *ctx = vctx;
86*b077aed3SPierre Pronchery
87*b077aed3SPierre Pronchery ossl_pw_clear_passphrase_data(&ctx->pwdata);
88*b077aed3SPierre Pronchery OPENSSL_free(ctx);
89*b077aed3SPierre Pronchery }
90*b077aed3SPierre Pronchery
key2pvk_settable_ctx_params(ossl_unused void * provctx)91*b077aed3SPierre Pronchery static const OSSL_PARAM *key2pvk_settable_ctx_params(ossl_unused void *provctx)
92*b077aed3SPierre Pronchery {
93*b077aed3SPierre Pronchery static const OSSL_PARAM settables[] = {
94*b077aed3SPierre Pronchery OSSL_PARAM_int(OSSL_ENCODER_PARAM_ENCRYPT_LEVEL, NULL),
95*b077aed3SPierre Pronchery OSSL_PARAM_END,
96*b077aed3SPierre Pronchery };
97*b077aed3SPierre Pronchery
98*b077aed3SPierre Pronchery return settables;
99*b077aed3SPierre Pronchery }
100*b077aed3SPierre Pronchery
key2pvk_set_ctx_params(void * vctx,const OSSL_PARAM params[])101*b077aed3SPierre Pronchery static int key2pvk_set_ctx_params(void *vctx, const OSSL_PARAM params[])
102*b077aed3SPierre Pronchery {
103*b077aed3SPierre Pronchery struct key2ms_ctx_st *ctx = vctx;
104*b077aed3SPierre Pronchery const OSSL_PARAM *p;
105*b077aed3SPierre Pronchery
106*b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_ENCRYPT_LEVEL);
107*b077aed3SPierre Pronchery if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->pvk_encr_level))
108*b077aed3SPierre Pronchery return 0;
109*b077aed3SPierre Pronchery return 1;
110*b077aed3SPierre Pronchery }
111*b077aed3SPierre Pronchery
key2ms_does_selection(void * vctx,int selection)112*b077aed3SPierre Pronchery static int key2ms_does_selection(void *vctx, int selection)
113*b077aed3SPierre Pronchery {
114*b077aed3SPierre Pronchery return (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0;
115*b077aed3SPierre Pronchery }
116*b077aed3SPierre Pronchery
117*b077aed3SPierre Pronchery /*
118*b077aed3SPierre Pronchery * The real EVP_PKEY_set1_TYPE() functions take a non-const key, while the key
119*b077aed3SPierre Pronchery * pointer in the encode function is a const pointer. We violate the constness
120*b077aed3SPierre Pronchery * knowingly, since we know that the key comes from the same provider, is never
121*b077aed3SPierre Pronchery * actually const, and the implied reference count change is safe.
122*b077aed3SPierre Pronchery *
123*b077aed3SPierre Pronchery * EVP_PKEY_assign() can't be used, because there's no way to clear the internal
124*b077aed3SPierre Pronchery * key using that function without freeing the existing internal key.
125*b077aed3SPierre Pronchery */
126*b077aed3SPierre Pronchery typedef int evp_pkey_set1_fn(EVP_PKEY *, const void *key);
127*b077aed3SPierre Pronchery
key2msblob_encode(void * vctx,const void * key,int selection,OSSL_CORE_BIO * cout,evp_pkey_set1_fn * set1_key,OSSL_PASSPHRASE_CALLBACK * pw_cb,void * pw_cbarg)128*b077aed3SPierre Pronchery static int key2msblob_encode(void *vctx, const void *key, int selection,
129*b077aed3SPierre Pronchery OSSL_CORE_BIO *cout, evp_pkey_set1_fn *set1_key,
130*b077aed3SPierre Pronchery OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
131*b077aed3SPierre Pronchery {
132*b077aed3SPierre Pronchery struct key2ms_ctx_st *ctx = vctx;
133*b077aed3SPierre Pronchery int ispub = -1;
134*b077aed3SPierre Pronchery EVP_PKEY *pkey = NULL;
135*b077aed3SPierre Pronchery int ok = 0;
136*b077aed3SPierre Pronchery
137*b077aed3SPierre Pronchery if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
138*b077aed3SPierre Pronchery ispub = 0;
139*b077aed3SPierre Pronchery else if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
140*b077aed3SPierre Pronchery ispub = 1;
141*b077aed3SPierre Pronchery else
142*b077aed3SPierre Pronchery return 0; /* Error */
143*b077aed3SPierre Pronchery
144*b077aed3SPierre Pronchery if ((pkey = EVP_PKEY_new()) != NULL && set1_key(pkey, key))
145*b077aed3SPierre Pronchery ok = write_msblob(ctx, cout, pkey, ispub);
146*b077aed3SPierre Pronchery EVP_PKEY_free(pkey);
147*b077aed3SPierre Pronchery return ok;
148*b077aed3SPierre Pronchery }
149*b077aed3SPierre Pronchery
key2pvk_encode(void * vctx,const void * key,int selection,OSSL_CORE_BIO * cout,evp_pkey_set1_fn * set1_key,OSSL_PASSPHRASE_CALLBACK * pw_cb,void * pw_cbarg)150*b077aed3SPierre Pronchery static int key2pvk_encode(void *vctx, const void *key, int selection,
151*b077aed3SPierre Pronchery OSSL_CORE_BIO *cout, evp_pkey_set1_fn *set1_key,
152*b077aed3SPierre Pronchery OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
153*b077aed3SPierre Pronchery {
154*b077aed3SPierre Pronchery struct key2ms_ctx_st *ctx = vctx;
155*b077aed3SPierre Pronchery EVP_PKEY *pkey = NULL;
156*b077aed3SPierre Pronchery int ok = 0;
157*b077aed3SPierre Pronchery
158*b077aed3SPierre Pronchery if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0)
159*b077aed3SPierre Pronchery return 0; /* Error */
160*b077aed3SPierre Pronchery
161*b077aed3SPierre Pronchery if ((pkey = EVP_PKEY_new()) != NULL && set1_key(pkey, key)
162*b077aed3SPierre Pronchery && (pw_cb == NULL
163*b077aed3SPierre Pronchery || ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, pw_cb, pw_cbarg)))
164*b077aed3SPierre Pronchery ok = write_pvk(ctx, cout, pkey);
165*b077aed3SPierre Pronchery EVP_PKEY_free(pkey);
166*b077aed3SPierre Pronchery return ok;
167*b077aed3SPierre Pronchery }
168*b077aed3SPierre Pronchery
169*b077aed3SPierre Pronchery #define dsa_set1 (evp_pkey_set1_fn *)EVP_PKEY_set1_DSA
170*b077aed3SPierre Pronchery #define rsa_set1 (evp_pkey_set1_fn *)EVP_PKEY_set1_RSA
171*b077aed3SPierre Pronchery
172*b077aed3SPierre Pronchery #define msblob_set_params
173*b077aed3SPierre Pronchery #define pvk_set_params \
174*b077aed3SPierre Pronchery { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, \
175*b077aed3SPierre Pronchery (void (*)(void))key2pvk_settable_ctx_params }, \
176*b077aed3SPierre Pronchery { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, \
177*b077aed3SPierre Pronchery (void (*)(void))key2pvk_set_ctx_params },
178*b077aed3SPierre Pronchery
179*b077aed3SPierre Pronchery #define MAKE_MS_ENCODER(impl, output, type) \
180*b077aed3SPierre Pronchery static OSSL_FUNC_encoder_import_object_fn \
181*b077aed3SPierre Pronchery impl##2##output##_import_object; \
182*b077aed3SPierre Pronchery static OSSL_FUNC_encoder_free_object_fn impl##2##output##_free_object; \
183*b077aed3SPierre Pronchery static OSSL_FUNC_encoder_encode_fn impl##2##output##_encode; \
184*b077aed3SPierre Pronchery \
185*b077aed3SPierre Pronchery static void * \
186*b077aed3SPierre Pronchery impl##2##output##_import_object(void *ctx, int selection, \
187*b077aed3SPierre Pronchery const OSSL_PARAM params[]) \
188*b077aed3SPierre Pronchery { \
189*b077aed3SPierre Pronchery return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
190*b077aed3SPierre Pronchery ctx, selection, params); \
191*b077aed3SPierre Pronchery } \
192*b077aed3SPierre Pronchery static void impl##2##output##_free_object(void *key) \
193*b077aed3SPierre Pronchery { \
194*b077aed3SPierre Pronchery ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
195*b077aed3SPierre Pronchery } \
196*b077aed3SPierre Pronchery static int impl##2##output##_encode(void *vctx, OSSL_CORE_BIO *cout, \
197*b077aed3SPierre Pronchery const void *key, \
198*b077aed3SPierre Pronchery const OSSL_PARAM key_abstract[], \
199*b077aed3SPierre Pronchery int selection, \
200*b077aed3SPierre Pronchery OSSL_PASSPHRASE_CALLBACK *cb, \
201*b077aed3SPierre Pronchery void *cbarg) \
202*b077aed3SPierre Pronchery { \
203*b077aed3SPierre Pronchery /* We don't deal with abstract objects */ \
204*b077aed3SPierre Pronchery if (key_abstract != NULL) { \
205*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
206*b077aed3SPierre Pronchery return 0; \
207*b077aed3SPierre Pronchery } \
208*b077aed3SPierre Pronchery return key2##output##_encode(vctx, key, selection, cout, type##_set1, \
209*b077aed3SPierre Pronchery cb, cbarg); \
210*b077aed3SPierre Pronchery } \
211*b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_##impl##_to_##output##_encoder_functions[] = { \
212*b077aed3SPierre Pronchery { OSSL_FUNC_ENCODER_NEWCTX, \
213*b077aed3SPierre Pronchery (void (*)(void))key2ms_newctx }, \
214*b077aed3SPierre Pronchery { OSSL_FUNC_ENCODER_FREECTX, \
215*b077aed3SPierre Pronchery (void (*)(void))key2ms_freectx }, \
216*b077aed3SPierre Pronchery output##_set_params \
217*b077aed3SPierre Pronchery { OSSL_FUNC_ENCODER_DOES_SELECTION, \
218*b077aed3SPierre Pronchery (void (*)(void))key2ms_does_selection }, \
219*b077aed3SPierre Pronchery { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
220*b077aed3SPierre Pronchery (void (*)(void))impl##2##output##_import_object }, \
221*b077aed3SPierre Pronchery { OSSL_FUNC_ENCODER_FREE_OBJECT, \
222*b077aed3SPierre Pronchery (void (*)(void))impl##2##output##_free_object }, \
223*b077aed3SPierre Pronchery { OSSL_FUNC_ENCODER_ENCODE, \
224*b077aed3SPierre Pronchery (void (*)(void))impl##2##output##_encode }, \
225*b077aed3SPierre Pronchery { 0, NULL } \
226*b077aed3SPierre Pronchery }
227*b077aed3SPierre Pronchery
228*b077aed3SPierre Pronchery #ifndef OPENSSL_NO_DSA
229*b077aed3SPierre Pronchery MAKE_MS_ENCODER(dsa, pvk, dsa);
230*b077aed3SPierre Pronchery MAKE_MS_ENCODER(dsa, msblob, dsa);
231*b077aed3SPierre Pronchery #endif
232*b077aed3SPierre Pronchery
233*b077aed3SPierre Pronchery MAKE_MS_ENCODER(rsa, pvk, rsa);
234*b077aed3SPierre Pronchery MAKE_MS_ENCODER(rsa, msblob, rsa);
235