2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
18 #include <openssl/core_dispatch.h>
19 #include <openssl/core_names.h>
20 #include <openssl/core_object.h>
21 #include <openssl/crypto.h>
22 #include <openssl/params.h>
23 #include <openssl/pem.h> /* For public PVK functions */
24 #include <openssl/x509.h>
25 #include "internal/pem.h" /* For internal PVK and "blob" headers */
26 #include "internal/passphrase.h"
28 #include "prov/implementations.h"
29 #include "endecoder_local.h"
31 #ifndef OPENSSL_NO_DSA
32 static EVP_PKEY *read_msblob(PROV_CTX *provctx, OSSL_CORE_BIO *cin, int *ispub)
34 BIO *in = bio_new_from_core_bio(provctx, cin);
35 EVP_PKEY *pkey = ossl_b2i_bio(in, ispub);
41 # ifndef OPENSSL_NO_RC4
42 static EVP_PKEY *read_pvk(PROV_CTX *provctx, OSSL_CORE_BIO *cin,
43 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
46 EVP_PKEY *pkey = NULL;
47 struct ossl_passphrase_data_st pwdata;
49 memset(&pwdata, 0, sizeof(pwdata));
50 if (!ossl_pw_set_ossl_passphrase_cb(&pwdata, pw_cb, pw_cbarg))
53 in = bio_new_from_core_bio(provctx, cin);
54 pkey = b2i_PVK_bio(in, ossl_pw_pem_password, &pwdata);
62 static OSSL_FUNC_decoder_freectx_fn ms2key_freectx;
63 static OSSL_FUNC_decoder_gettable_params_fn ms2key_gettable_params;
64 static OSSL_FUNC_decoder_get_params_fn msblob2key_get_params;
65 #ifndef OPENSSL_NO_RC4
66 static OSSL_FUNC_decoder_get_params_fn pvk2key_get_params;
68 static OSSL_FUNC_decoder_decode_fn msblob2key_decode;
69 #ifndef OPENSSL_NO_RC4
70 static OSSL_FUNC_decoder_decode_fn pvk2key_decode;
72 static OSSL_FUNC_decoder_export_object_fn ms2key_export_object;
74 typedef void *(extract_key_fn)(EVP_PKEY *);
75 typedef void (free_key_fn)(void *);
76 struct keytype_desc_st {
77 int type; /* EVP key type */
78 const char *name; /* Keytype */
79 const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
82 * These must be the correct EVP_PKEY_get1_{TYPE}() and {TYPE}_free()
83 * function for the key.
85 extract_key_fn *extract_key;
86 free_key_fn *free_key;
90 * Context used for DER to key decoding.
92 struct ms2key_ctx_st {
94 const struct keytype_desc_st *desc;
97 static struct ms2key_ctx_st *
98 ms2key_newctx(void *provctx, const struct keytype_desc_st *desc)
100 struct ms2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
103 ctx->provctx = provctx;
109 static void ms2key_freectx(void *vctx)
111 struct ms2key_ctx_st *ctx = vctx;
116 static const OSSL_PARAM *ms2key_gettable_params(ossl_unused void *provctx)
118 static const OSSL_PARAM gettables[] = {
119 { OSSL_DECODER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
126 static int msblob2key_get_params(OSSL_PARAM params[])
130 p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_TYPE);
131 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "MSBLOB"))
137 #ifndef OPENSSL_NO_RC4
138 static int pvk2key_get_params(OSSL_PARAM params[])
142 p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_TYPE);
143 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "PVK"))
150 static int ms2key_post(struct ms2key_ctx_st *ctx, EVP_PKEY *pkey,
151 OSSL_CALLBACK *data_cb, void *data_cbarg)
158 * Tear out the low-level key pointer from the pkey,
159 * but only if it matches the expected key type.
161 * TODO(3.0): The check should be done with EVP_PKEY_is_a(), but
162 * as long as we still have #legacy internal keys, it's safer to
163 * use the type numbers in side the provider.
165 if (EVP_PKEY_id(pkey) == ctx->desc->type)
166 key = ctx->desc->extract_key(pkey);
170 OSSL_PARAM params[4];
171 int object_type = OSSL_OBJECT_PKEY;
174 OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
176 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
177 (char *)ctx->desc->name, 0);
178 /* The address of the key becomes the octet string */
180 OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
182 params[3] = OSSL_PARAM_construct_end();
184 ok = data_cb(params, data_cbarg);
186 ctx->desc->free_key(key);
191 static int msblob2key_decode(void *vctx, OSSL_CORE_BIO *cin,
192 OSSL_CALLBACK *data_cb, void *data_cbarg,
193 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
195 struct ms2key_ctx_st *ctx = vctx;
197 EVP_PKEY *pkey = read_msblob(ctx->provctx, cin, &ispub);
198 int ok = ms2key_post(ctx, pkey, data_cb, data_cbarg);
204 #ifndef OPENSSL_NO_RC4
205 static int pvk2key_decode(void *vctx, OSSL_CORE_BIO *cin,
206 OSSL_CALLBACK *data_cb, void *data_cbarg,
207 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
209 struct ms2key_ctx_st *ctx = vctx;
210 EVP_PKEY *pkey = read_pvk(ctx->provctx, cin, pw_cb, pw_cbarg);
211 int ok = ms2key_post(ctx, pkey, data_cb, data_cbarg);
218 static int ms2key_export_object(void *vctx,
219 const void *reference, size_t reference_sz,
220 OSSL_CALLBACK *export_cb, void *export_cbarg)
222 struct ms2key_ctx_st *ctx = vctx;
223 OSSL_FUNC_keymgmt_export_fn *export =
224 ossl_prov_get_keymgmt_export(ctx->desc->fns);
227 if (reference_sz == sizeof(keydata) && export != NULL) {
228 /* The contents of the reference is the address to our object */
229 keydata = *(void **)reference;
231 return export(keydata, OSSL_KEYMGMT_SELECT_ALL,
232 export_cb, export_cbarg);
237 #define IMPLEMENT_TYPE(KEYTYPEstr, KEYTYPE, keytype, extract, free) \
238 static const struct keytype_desc_st keytype##_desc; \
239 static OSSL_FUNC_decoder_newctx_fn ms2##keytype##_newctx; \
240 static void *ms2##keytype##_newctx(void *provctx) \
242 return ms2key_newctx(provctx, &keytype##_desc); \
244 static const struct keytype_desc_st keytype##_desc = \
245 { EVP_PKEY_##KEYTYPE, KEYTYPEstr, keytype##_keymgmt_functions, \
246 (extract_key_fn *)extract, \
247 (free_key_fn *)free }
249 #define IMPLEMENT_MS(mstype, keytype) \
250 const OSSL_DISPATCH \
251 mstype##_to_##keytype##_decoder_functions[] = { \
252 { OSSL_FUNC_DECODER_NEWCTX, \
253 (void (*)(void))ms2##keytype##_newctx }, \
254 { OSSL_FUNC_DECODER_FREECTX, \
255 (void (*)(void))ms2key_freectx }, \
256 { OSSL_FUNC_DECODER_GETTABLE_PARAMS, \
257 (void (*)(void))ms2key_gettable_params }, \
258 { OSSL_FUNC_DECODER_GET_PARAMS, \
259 (void (*)(void))mstype##2key_get_params }, \
260 { OSSL_FUNC_DECODER_DECODE, \
261 (void (*)(void))mstype##2key_decode }, \
262 { OSSL_FUNC_DECODER_EXPORT_OBJECT, \
263 (void (*)(void))ms2key_export_object }, \
267 #ifndef OPENSSL_NO_DSA
268 IMPLEMENT_TYPE("DSA", DSA, dsa, EVP_PKEY_get1_DSA, DSA_free);
269 IMPLEMENT_MS(msblob, dsa);
270 # ifndef OPENSSL_NO_RC4
271 IMPLEMENT_MS(pvk, dsa);
274 IMPLEMENT_TYPE("RSA", RSA, rsa, EVP_PKEY_get1_RSA, RSA_free);
275 IMPLEMENT_MS(msblob, rsa);
276 #ifndef OPENSSL_NO_RC4
277 IMPLEMENT_MS(pvk, rsa);