Adapt existing SERIALIZER implementations to the redesigned interface
[oweals/openssl.git] / providers / implementations / serializers / serializer_rsa_pub.c
1 /*
2  * Copyright 2019 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 #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>
15 #include "prov/bio.h"
16 #include "prov/implementations.h"
17 #include "prov/providercommonerr.h"
18 #include "serializer_local.h"
19
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;
26
27 static OSSL_OP_serializer_serialize_data_fn rsa_pub_print_data;
28 static OSSL_OP_serializer_serialize_object_fn rsa_pub_print;
29
30 /* Public key : context */
31
32 /*
33  * There's no specific implementation context, so we use the provider context
34  */
35 static void *rsa_pub_newctx(void *provctx)
36 {
37     return provctx;
38 }
39
40 static void rsa_pub_freectx(void *ctx)
41 {
42 }
43
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)
47 {
48     OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
49     OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
50     OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
51     int ok = 0;
52
53     if (rsa_import != NULL) {
54         RSA *rsa;
55
56         /* ctx == provctx */
57         if ((rsa = rsa_new(ctx)) != NULL
58             && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
59             && rsa_pub_der(ctx, rsa, out, cb, cbarg))
60             ok = 1;
61         rsa_free(rsa);
62     }
63     return ok;
64 }
65
66 static int rsa_pub_der(void *ctx, void *rsa, BIO *out,
67                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
68 {
69     return i2d_RSA_PUBKEY_bio(out, rsa);
70 }
71
72 /* Public key : PEM */
73 static int rsa_pub_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
74                             OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
75 {
76     OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
77     OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
78     OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
79     int ok = 0;
80
81     if (rsa_import != NULL) {
82         RSA *rsa;
83
84         /* ctx == provctx */
85         if ((rsa = rsa_new(ctx)) != NULL
86             && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
87             && rsa_pub_pem(ctx, rsa, out, cb, cbarg))
88             ok = 1;
89         rsa_free(rsa);
90     }
91     return ok;
92 }
93
94 static int rsa_pub_pem(void *ctx, void *rsa, BIO *out,
95                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
96 {
97     return PEM_write_bio_RSA_PUBKEY(out, rsa);
98 }
99
100 static int rsa_pub_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
101                               OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
102 {
103     OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
104     OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
105     OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
106     int ok = 0;
107
108     if (rsa_import != NULL) {
109         RSA *rsa;
110
111         /* ctx == provctx */
112         if ((rsa = rsa_new(ctx)) != NULL
113             && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
114             && rsa_pub_print(ctx, rsa, out, cb, cbarg))
115             ok = 1;
116         rsa_free(rsa);
117     }
118     return ok;
119 }
120
121 static int rsa_pub_print(void *ctx, void *rsa, BIO *out,
122                          OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
123 {
124     return ossl_prov_print_rsa(out, rsa, 0);
125 }
126
127 const OSSL_DISPATCH rsa_pub_der_serializer_functions[] = {
128     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
129     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
130     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pub_der_data },
131     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_der },
132     { 0, NULL }
133 };
134
135 const OSSL_DISPATCH rsa_pub_pem_serializer_functions[] = {
136     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
137     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
138     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pub_pem_data },
139     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_pem },
140     { 0, NULL }
141 };
142
143 const OSSL_DISPATCH rsa_pub_text_serializer_functions[] = {
144     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
145     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
146     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_print },
147     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
148       (void (*)(void))rsa_pub_print_data },
149     { 0, NULL }
150 };