2 * Copyright 2019 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 "serializer_local.h"
26 static OSSL_OP_serializer_newctx_fn rsa_pub_newctx;
27 static OSSL_OP_serializer_freectx_fn rsa_pub_freectx;
28 static OSSL_OP_serializer_serialize_data_fn rsa_pub_der_data;
29 static OSSL_OP_serializer_serialize_object_fn rsa_pub_der;
30 static OSSL_OP_serializer_serialize_data_fn rsa_pub_pem_data;
31 static OSSL_OP_serializer_serialize_object_fn rsa_pub_pem;
33 static OSSL_OP_serializer_serialize_data_fn rsa_pub_print_data;
34 static OSSL_OP_serializer_serialize_object_fn rsa_pub_print;
36 /* Public key : context */
39 * There's no specific implementation context, so we use the provider context
41 static void *rsa_pub_newctx(void *provctx)
46 static void rsa_pub_freectx(void *ctx)
50 /* Public key : DER */
51 static int rsa_pub_der_data(void *ctx, const OSSL_PARAM params[], BIO *out,
52 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
54 OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
55 OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
56 OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
59 if (rsa_import != NULL) {
63 if ((rsa = rsa_new(ctx)) != NULL
64 && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
65 && rsa_pub_der(ctx, rsa, out, cb, cbarg))
72 static int rsa_pub_der(void *ctx, void *rsa, BIO *out,
73 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
75 return i2d_RSA_PUBKEY_bio(out, rsa);
78 /* Public key : PEM */
79 static int rsa_pub_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
80 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
82 OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
83 OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
84 OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
87 if (rsa_import != NULL) {
91 if ((rsa = rsa_new(ctx)) != NULL
92 && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
93 && rsa_pub_pem(ctx, rsa, out, cb, cbarg))
100 static int rsa_pub_pem(void *ctx, void *rsa, BIO *out,
101 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
103 return PEM_write_bio_RSA_PUBKEY(out, rsa);
106 static int rsa_pub_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
107 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
109 OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
110 OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
111 OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
114 if (rsa_import != NULL) {
118 if ((rsa = rsa_new(ctx)) != NULL
119 && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
120 && rsa_pub_print(ctx, rsa, out, cb, cbarg))
127 static int rsa_pub_print(void *ctx, void *rsa, BIO *out,
128 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
130 return ossl_prov_print_rsa(out, rsa, 0);
133 const OSSL_DISPATCH rsa_pub_der_serializer_functions[] = {
134 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
135 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
136 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pub_der_data },
137 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_der },
141 const OSSL_DISPATCH rsa_pub_pem_serializer_functions[] = {
142 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
143 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
144 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pub_pem_data },
145 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_pem },
149 const OSSL_DISPATCH rsa_pub_text_serializer_functions[] = {
150 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
151 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
152 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_print },
153 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
154 (void (*)(void))rsa_pub_print_data },