2 * Copyright 2019-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 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
16 #include <openssl/core_numbers.h>
17 #include <openssl/pem.h>
18 #include <openssl/rsa.h>
19 #include <openssl/types.h>
20 #include <openssl/params.h>
22 #include "prov/implementations.h"
23 #include "prov/providercommonerr.h"
24 #include "prov/provider_ctx.h"
25 #include "serializer_local.h"
27 static OSSL_OP_serializer_newctx_fn rsa_pub_newctx;
28 static OSSL_OP_serializer_freectx_fn rsa_pub_freectx;
29 static OSSL_OP_serializer_serialize_data_fn rsa_pub_der_data;
30 static OSSL_OP_serializer_serialize_object_fn rsa_pub_der;
31 static OSSL_OP_serializer_serialize_data_fn rsa_pub_pem_data;
32 static OSSL_OP_serializer_serialize_object_fn rsa_pub_pem;
34 static OSSL_OP_serializer_serialize_data_fn rsa_pub_print_data;
35 static OSSL_OP_serializer_serialize_object_fn rsa_pub_print;
37 /* Public key : context */
40 * There's no specific implementation context, so we use the provider context
42 static void *rsa_pub_newctx(void *provctx)
47 static void rsa_pub_freectx(void *ctx)
51 /* Public key : DER */
52 static int rsa_pub_der_data(void *ctx, const OSSL_PARAM params[],
54 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
56 OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
57 OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
58 OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
61 if (rsa_import != NULL) {
65 if ((rsa = rsa_new(ctx)) != NULL
66 && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
67 && rsa_pub_der(ctx, rsa, out, cb, cbarg))
74 static int rsa_pub_der(void *ctx, void *rsa, OSSL_CORE_BIO *cout,
75 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
77 BIO *out = bio_new_from_core_bio(ctx, cout);
83 ret = ossl_prov_write_pub_der_from_obj(out, rsa,
84 ossl_prov_rsa_type_to_evp(rsa),
85 ossl_prov_prepare_rsa_params,
86 (i2d_of_void *)i2d_RSAPublicKey);
92 /* Public key : PEM */
93 static int rsa_pub_pem_data(void *ctx, const OSSL_PARAM params[],
95 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
97 OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
98 OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
99 OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
102 if (rsa_import != NULL) {
106 if ((rsa = rsa_new(ctx)) != NULL
107 && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
108 && rsa_pub_pem(ctx, rsa, out, cb, cbarg))
115 static int rsa_pub_pem(void *ctx, void *rsa, OSSL_CORE_BIO *cout,
116 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
118 BIO *out = bio_new_from_core_bio(ctx, cout);
124 ret = ossl_prov_write_pub_pem_from_obj(out, rsa,
125 ossl_prov_rsa_type_to_evp(rsa),
126 ossl_prov_prepare_rsa_params,
127 (i2d_of_void *)i2d_RSAPublicKey);
133 static int rsa_pub_print_data(void *ctx, const OSSL_PARAM params[],
135 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
137 OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
138 OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
139 OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
142 if (rsa_import != NULL) {
146 if ((rsa = rsa_new(ctx)) != NULL
147 && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
148 && rsa_pub_print(ctx, rsa, out, cb, cbarg))
155 static int rsa_pub_print(void *ctx, void *rsa, OSSL_CORE_BIO *cout,
156 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
158 BIO *out = bio_new_from_core_bio(ctx, cout);
164 ret = ossl_prov_print_rsa(out, rsa, 0);
170 const OSSL_DISPATCH rsa_pub_der_serializer_functions[] = {
171 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
172 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
173 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pub_der_data },
174 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_der },
178 const OSSL_DISPATCH rsa_pub_pem_serializer_functions[] = {
179 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
180 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
181 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pub_pem_data },
182 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_pem },
186 const OSSL_DISPATCH rsa_pub_text_serializer_functions[] = {
187 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
188 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
189 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_print },
190 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
191 (void (*)(void))rsa_pub_print_data },