xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/crypto/evp/p_rsa_asn1.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 /* Written by Dr Stephen N Henson ([email protected]) for the OpenSSL
2  * project 2006.
3  */
4 /* ====================================================================
5  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    [email protected].
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * ([email protected]).  This product includes software written by Tim
54  * Hudson ([email protected]). */
55 
56 #include <openssl/evp.h>
57 
58 #include <openssl/bn.h>
59 #include <openssl/bytestring.h>
60 #include <openssl/digest.h>
61 #include <openssl/err.h>
62 #include <openssl/mem.h>
63 #include <openssl/rsa.h>
64 
65 #include "../fipsmodule/rsa/internal.h"
66 #include "internal.h"
67 
68 
rsa_pub_encode(CBB * out,const EVP_PKEY * key)69 static int rsa_pub_encode(CBB *out, const EVP_PKEY *key) {
70   // See RFC 3279, section 2.3.1.
71   const RSA *rsa = key->pkey;
72   CBB spki, algorithm, oid, null, key_bitstring;
73   if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
74       !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
75       !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
76       !CBB_add_bytes(&oid, rsa_asn1_meth.oid, rsa_asn1_meth.oid_len) ||
77       !CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL) ||
78       !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
79       !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
80       !RSA_marshal_public_key(&key_bitstring, rsa) ||
81       !CBB_flush(out)) {
82     OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
83     return 0;
84   }
85 
86   return 1;
87 }
88 
rsa_pub_decode(EVP_PKEY * out,CBS * params,CBS * key)89 static int rsa_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
90   // See RFC 3279, section 2.3.1.
91 
92   // The parameters must be NULL.
93   CBS null;
94   if (!CBS_get_asn1(params, &null, CBS_ASN1_NULL) ||
95       CBS_len(&null) != 0 ||
96       CBS_len(params) != 0) {
97     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
98     return 0;
99   }
100 
101   RSA *rsa = RSA_parse_public_key(key);
102   if (rsa == NULL || CBS_len(key) != 0) {
103     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
104     RSA_free(rsa);
105     return 0;
106   }
107 
108   EVP_PKEY_assign_RSA(out, rsa);
109   return 1;
110 }
111 
rsa_pub_cmp(const EVP_PKEY * a,const EVP_PKEY * b)112 static int rsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
113   const RSA *a_rsa = a->pkey;
114   const RSA *b_rsa = b->pkey;
115   return BN_cmp(RSA_get0_n(b_rsa), RSA_get0_n(a_rsa)) == 0 &&
116          BN_cmp(RSA_get0_e(b_rsa), RSA_get0_e(a_rsa)) == 0;
117 }
118 
rsa_priv_encode(CBB * out,const EVP_PKEY * key)119 static int rsa_priv_encode(CBB *out, const EVP_PKEY *key) {
120   const RSA *rsa = key->pkey;
121   CBB pkcs8, algorithm, oid, null, private_key;
122   if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
123       !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
124       !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
125       !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
126       !CBB_add_bytes(&oid, rsa_asn1_meth.oid, rsa_asn1_meth.oid_len) ||
127       !CBB_add_asn1(&algorithm, &null, CBS_ASN1_NULL) ||
128       !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
129       !RSA_marshal_private_key(&private_key, rsa) ||
130       !CBB_flush(out)) {
131     OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
132     return 0;
133   }
134 
135   return 1;
136 }
137 
rsa_priv_decode(EVP_PKEY * out,CBS * params,CBS * key)138 static int rsa_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
139   // Per RFC 3447, A.1, the parameters have type NULL.
140   CBS null;
141   if (!CBS_get_asn1(params, &null, CBS_ASN1_NULL) ||
142       CBS_len(&null) != 0 ||
143       CBS_len(params) != 0) {
144     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
145     return 0;
146   }
147 
148   RSA *rsa = RSA_parse_private_key(key);
149   if (rsa == NULL || CBS_len(key) != 0) {
150     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
151     RSA_free(rsa);
152     return 0;
153   }
154 
155   EVP_PKEY_assign_RSA(out, rsa);
156   return 1;
157 }
158 
rsa_opaque(const EVP_PKEY * pkey)159 static int rsa_opaque(const EVP_PKEY *pkey) {
160   const RSA *rsa = pkey->pkey;
161   return RSA_is_opaque(rsa);
162 }
163 
int_rsa_size(const EVP_PKEY * pkey)164 static int int_rsa_size(const EVP_PKEY *pkey) {
165   const RSA *rsa = pkey->pkey;
166   return RSA_size(rsa);
167 }
168 
rsa_bits(const EVP_PKEY * pkey)169 static int rsa_bits(const EVP_PKEY *pkey) {
170   const RSA *rsa = pkey->pkey;
171   return RSA_bits(rsa);
172 }
173 
int_rsa_free(EVP_PKEY * pkey)174 static void int_rsa_free(EVP_PKEY *pkey) {
175   RSA_free(pkey->pkey);
176   pkey->pkey = NULL;
177 }
178 
179 const EVP_PKEY_ASN1_METHOD rsa_asn1_meth = {
180     EVP_PKEY_RSA,
181     // 1.2.840.113549.1.1.1
182     {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01},
183     9,
184 
185     &rsa_pkey_meth,
186 
187     rsa_pub_decode,
188     rsa_pub_encode,
189     rsa_pub_cmp,
190 
191     rsa_priv_decode,
192     rsa_priv_encode,
193 
194     /*set_priv_raw=*/NULL,
195     /*set_pub_raw=*/NULL,
196     /*get_priv_raw=*/NULL,
197     /*get_pub_raw=*/NULL,
198     /*set1_tls_encodedpoint=*/NULL,
199     /*get1_tls_encodedpoint=*/NULL,
200 
201     rsa_opaque,
202 
203     int_rsa_size,
204     rsa_bits,
205 
206     0,
207     0,
208     0,
209 
210     int_rsa_free,
211 };
212 
EVP_PKEY_set1_RSA(EVP_PKEY * pkey,RSA * key)213 int EVP_PKEY_set1_RSA(EVP_PKEY *pkey, RSA *key) {
214   if (EVP_PKEY_assign_RSA(pkey, key)) {
215     RSA_up_ref(key);
216     return 1;
217   }
218   return 0;
219 }
220 
EVP_PKEY_assign_RSA(EVP_PKEY * pkey,RSA * key)221 int EVP_PKEY_assign_RSA(EVP_PKEY *pkey, RSA *key) {
222   evp_pkey_set_method(pkey, &rsa_asn1_meth);
223   pkey->pkey = key;
224   return key != NULL;
225 }
226 
EVP_PKEY_get0_RSA(const EVP_PKEY * pkey)227 RSA *EVP_PKEY_get0_RSA(const EVP_PKEY *pkey) {
228   if (pkey->type != EVP_PKEY_RSA) {
229     OPENSSL_PUT_ERROR(EVP, EVP_R_EXPECTING_AN_RSA_KEY);
230     return NULL;
231   }
232   return pkey->pkey;
233 }
234 
EVP_PKEY_get1_RSA(const EVP_PKEY * pkey)235 RSA *EVP_PKEY_get1_RSA(const EVP_PKEY *pkey) {
236   RSA *rsa = EVP_PKEY_get0_RSA(pkey);
237   if (rsa != NULL) {
238     RSA_up_ref(rsa);
239   }
240   return rsa;
241 }
242