PROV & SERIALIZER: Adapt the RSA serializers for PSS-parameters
[openssl.git] / providers / implementations / serializers / serializer_rsa_pub.c
1 /*
2  * Copyright 2019-2020 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  * RSA low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15
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>
21 #include "prov/bio.h"
22 #include "prov/implementations.h"
23 #include "prov/providercommonerr.h"
24 #include "serializer_local.h"
25
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;
32
33 static OSSL_OP_serializer_serialize_data_fn rsa_pub_print_data;
34 static OSSL_OP_serializer_serialize_object_fn rsa_pub_print;
35
36 /* Public key : context */
37
38 /*
39  * There's no specific implementation context, so we use the provider context
40  */
41 static void *rsa_pub_newctx(void *provctx)
42 {
43     return provctx;
44 }
45
46 static void rsa_pub_freectx(void *ctx)
47 {
48 }
49
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)
53 {
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();
57     int ok = 0;
58
59     if (rsa_import != NULL) {
60         RSA *rsa;
61
62         /* ctx == provctx */
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))
66             ok = 1;
67         rsa_free(rsa);
68     }
69     return ok;
70 }
71
72 static int rsa_pub_der(void *ctx, void *rsa, BIO *out,
73                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
74 {
75     return ossl_prov_write_pub_der_from_obj(out, rsa,
76                                             ossl_prov_rsa_type_to_evp(rsa),
77                                             ossl_prov_prepare_rsa_params,
78                                             (i2d_of_void *)i2d_RSAPublicKey);
79 }
80
81 /* Public key : PEM */
82 static int rsa_pub_pem_data(void *ctx, const OSSL_PARAM params[], BIO *out,
83                             OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
84 {
85     OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
86     OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
87     OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
88     int ok = 0;
89
90     if (rsa_import != NULL) {
91         RSA *rsa;
92
93         /* ctx == provctx */
94         if ((rsa = rsa_new(ctx)) != NULL
95             && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
96             && rsa_pub_pem(ctx, rsa, out, cb, cbarg))
97             ok = 1;
98         rsa_free(rsa);
99     }
100     return ok;
101 }
102
103 static int rsa_pub_pem(void *ctx, void *rsa, BIO *out,
104                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
105 {
106     return ossl_prov_write_pub_pem_from_obj(out, rsa,
107                                             ossl_prov_rsa_type_to_evp(rsa),
108                                             ossl_prov_prepare_rsa_params,
109                                             (i2d_of_void *)i2d_RSAPublicKey);
110 }
111
112 static int rsa_pub_print_data(void *ctx, const OSSL_PARAM params[], BIO *out,
113                               OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
114 {
115     OSSL_OP_keymgmt_new_fn *rsa_new = ossl_prov_get_keymgmt_rsa_new();
116     OSSL_OP_keymgmt_free_fn *rsa_free = ossl_prov_get_keymgmt_rsa_free();
117     OSSL_OP_keymgmt_import_fn *rsa_import = ossl_prov_get_keymgmt_rsa_import();
118     int ok = 0;
119
120     if (rsa_import != NULL) {
121         RSA *rsa;
122
123         /* ctx == provctx */
124         if ((rsa = rsa_new(ctx)) != NULL
125             && rsa_import(rsa, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
126             && rsa_pub_print(ctx, rsa, out, cb, cbarg))
127             ok = 1;
128         rsa_free(rsa);
129     }
130     return ok;
131 }
132
133 static int rsa_pub_print(void *ctx, void *rsa, BIO *out,
134                          OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
135 {
136     return ossl_prov_print_rsa(out, rsa, 0);
137 }
138
139 const OSSL_DISPATCH rsa_pub_der_serializer_functions[] = {
140     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
141     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
142     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pub_der_data },
143     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_der },
144     { 0, NULL }
145 };
146
147 const OSSL_DISPATCH rsa_pub_pem_serializer_functions[] = {
148     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
149     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
150     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))rsa_pub_pem_data },
151     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_pem },
152     { 0, NULL }
153 };
154
155 const OSSL_DISPATCH rsa_pub_text_serializer_functions[] = {
156     { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))rsa_pub_newctx },
157     { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))rsa_pub_freectx },
158     { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))rsa_pub_print },
159     { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
160       (void (*)(void))rsa_pub_print_data },
161     { 0, NULL }
162 };