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