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
10 #include <openssl/core_numbers.h>
11 #include <openssl/pem.h>
12 #include <openssl/rsa.h>
13 #include <openssl/types.h>
14 #include <openssl/params.h>
16 #include "prov/implementations.h"
17 #include "prov/providercommonerr.h"
18 #include "serializer_local.h"
20 static OSSL_OP_serializer_newctx_fn rsa_pub_newctx;
21 static OSSL_OP_serializer_freectx_fn rsa_pub_freectx;
22 static OSSL_OP_serializer_serialize_data_fn rsa_pub_der_data;
23 static OSSL_OP_serializer_serialize_object_fn rsa_pub_der;
24 static OSSL_OP_serializer_serialize_data_fn rsa_pub_pem_data;
25 static OSSL_OP_serializer_serialize_object_fn rsa_pub_pem;
27 static OSSL_OP_serializer_serialize_data_fn rsa_pub_print_data;
28 static OSSL_OP_serializer_serialize_object_fn rsa_pub_print;
30 /* Public key : context */
33 * There's no specific implementation context, so we use the provider context
35 static void *rsa_pub_newctx(void *provctx)
40 static void rsa_pub_freectx(void *ctx)
44 /* Public key : DER */
45 static int rsa_pub_der_data(void *ctx, const OSSL_PARAM params[], BIO *out,
46 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
48 OSSL_OP_keymgmt_importkey_fn *rsa_importkey =
49 ossl_prov_get_rsa_importkey();
52 if (rsa_importkey != NULL) {
53 RSA *rsa = rsa_importkey(ctx, params); /* ctx == provctx */
55 ok = rsa_pub_der(ctx, rsa, out, cb, cbarg);
61 static int rsa_pub_der(void *ctx, void *rsa, BIO *out,
62 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
64 return i2d_RSA_PUBKEY_bio(out, rsa);
67 /* Public key : PEM */
68 static int rsa_pub_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
69 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
71 OSSL_OP_keymgmt_importkey_fn *rsa_importkey =
72 ossl_prov_get_rsa_importkey();
75 if (rsa_importkey != NULL) {
76 RSA *rsa = rsa_importkey(ctx, params); /* ctx == provctx */
78 ok = rsa_pub_pem(ctx, rsa, out, cb, cbarg);
84 static int rsa_pub_pem(void *ctx, void *rsa, BIO *out,
85 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
87 return PEM_write_bio_RSA_PUBKEY(out, rsa);
90 static int rsa_pub_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
91 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
93 OSSL_OP_keymgmt_importkey_fn *rsa_importkey =
94 ossl_prov_get_rsa_importkey();
97 if (rsa_importkey != NULL) {
98 RSA *rsa = rsa_importkey(ctx, params); /* ctx == provctx */
100 ok = rsa_pub_print(ctx, rsa, out, cb, cbarg);
106 static int rsa_pub_print(void *ctx, void *rsa, BIO *out,
107 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
109 return ossl_prov_print_rsa(out, rsa, 0);
112 const OSSL_DISPATCH rsa_pub_der_serializer_functions[] = {
113 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
114 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
115 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pub_der_data },
116 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_der },
120 const OSSL_DISPATCH rsa_pub_pem_serializer_functions[] = {
121 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
122 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
123 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pub_pem_data },
124 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_pem },
128 const OSSL_DISPATCH rsa_pub_text_serializer_functions[] = {
129 { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
130 { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
131 { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_print },
132 { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
133 (void (*)(void))rsa_pub_print_data },