rsa_asn1.c
Go to the documentation of this file.
1 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
2  * project 2000.
3  */
4 /* ====================================================================
5  * Copyright (c) 2000-2005 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  * licensing@OpenSSL.org.
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  * (eay@cryptsoft.com). This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com). */
55 
56 #include <openssl/rsa.h>
57 
58 #include <assert.h>
59 #include <limits.h>
60 #include <string.h>
61 
62 #include <openssl/bn.h>
63 #include <openssl/bytestring.h>
64 #include <openssl/err.h>
65 #include <openssl/mem.h>
66 
67 #include "../fipsmodule/rsa/internal.h"
68 #include "../bytestring/internal.h"
69 #include "../internal.h"
70 
71 
72 static int parse_integer(CBS *cbs, BIGNUM **out) {
73  assert(*out == NULL);
74  *out = BN_new();
75  if (*out == NULL) {
76  return 0;
77  }
78  return BN_parse_asn1_unsigned(cbs, *out);
79 }
80 
81 static int marshal_integer(CBB *cbb, BIGNUM *bn) {
82  if (bn == NULL) {
83  // An RSA object may be missing some components.
85  return 0;
86  }
87  return BN_marshal_asn1(cbb, bn);
88 }
89 
91  RSA *ret = RSA_new();
92  if (ret == NULL) {
93  return NULL;
94  }
95  CBS child;
97  !parse_integer(&child, &ret->n) ||
98  !parse_integer(&child, &ret->e) ||
99  CBS_len(&child) != 0) {
101  RSA_free(ret);
102  return NULL;
103  }
104 
105  if (!RSA_check_key(ret)) {
107  RSA_free(ret);
108  return NULL;
109  }
110 
111  return ret;
112 }
113 
114 RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len) {
115  CBS cbs;
116  CBS_init(&cbs, in, in_len);
118  if (ret == NULL || CBS_len(&cbs) != 0) {
120  RSA_free(ret);
121  return NULL;
122  }
123  return ret;
124 }
125 
126 int RSA_marshal_public_key(CBB *cbb, const RSA *rsa) {
127  CBB child;
128  if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
129  !marshal_integer(&child, rsa->n) ||
130  !marshal_integer(&child, rsa->e) ||
131  !CBB_flush(cbb)) {
133  return 0;
134  }
135  return 1;
136 }
137 
138 int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
139  const RSA *rsa) {
140  CBB cbb;
141  CBB_zero(&cbb);
142  if (!CBB_init(&cbb, 0) ||
143  !RSA_marshal_public_key(&cbb, rsa) ||
144  !CBB_finish(&cbb, out_bytes, out_len)) {
146  CBB_cleanup(&cbb);
147  return 0;
148  }
149  return 1;
150 }
151 
152 // kVersionTwoPrime is the value of the version field for a two-prime
153 // RSAPrivateKey structure (RFC 3447).
154 static const uint64_t kVersionTwoPrime = 0;
155 
157  RSA *ret = RSA_new();
158  if (ret == NULL) {
159  return NULL;
160  }
161 
162  CBS child;
167  goto err;
168  }
169 
170  if (version != kVersionTwoPrime) {
172  goto err;
173  }
174 
175  if (!parse_integer(&child, &ret->n) ||
176  !parse_integer(&child, &ret->e) ||
177  !parse_integer(&child, &ret->d) ||
178  !parse_integer(&child, &ret->p) ||
179  !parse_integer(&child, &ret->q) ||
180  !parse_integer(&child, &ret->dmp1) ||
181  !parse_integer(&child, &ret->dmq1) ||
182  !parse_integer(&child, &ret->iqmp)) {
183  goto err;
184  }
185 
186  if (CBS_len(&child) != 0) {
188  goto err;
189  }
190 
191  if (!RSA_check_key(ret)) {
193  goto err;
194  }
195 
196  return ret;
197 
198 err:
199  RSA_free(ret);
200  return NULL;
201 }
202 
203 RSA *RSA_private_key_from_bytes(const uint8_t *in, size_t in_len) {
204  CBS cbs;
205  CBS_init(&cbs, in, in_len);
207  if (ret == NULL || CBS_len(&cbs) != 0) {
209  RSA_free(ret);
210  return NULL;
211  }
212  return ret;
213 }
214 
215 int RSA_marshal_private_key(CBB *cbb, const RSA *rsa) {
216  CBB child;
217  if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) ||
219  !marshal_integer(&child, rsa->n) ||
220  !marshal_integer(&child, rsa->e) ||
221  !marshal_integer(&child, rsa->d) ||
222  !marshal_integer(&child, rsa->p) ||
223  !marshal_integer(&child, rsa->q) ||
224  !marshal_integer(&child, rsa->dmp1) ||
225  !marshal_integer(&child, rsa->dmq1) ||
226  !marshal_integer(&child, rsa->iqmp) ||
227  !CBB_flush(cbb)) {
229  return 0;
230  }
231  return 1;
232 }
233 
234 int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len,
235  const RSA *rsa) {
236  CBB cbb;
237  CBB_zero(&cbb);
238  if (!CBB_init(&cbb, 0) ||
239  !RSA_marshal_private_key(&cbb, rsa) ||
240  !CBB_finish(&cbb, out_bytes, out_len)) {
242  CBB_cleanup(&cbb);
243  return 0;
244  }
245  return 1;
246 }
247 
248 RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len) {
249  if (len < 0) {
250  return NULL;
251  }
252  CBS cbs;
253  CBS_init(&cbs, *inp, (size_t)len);
255  if (ret == NULL) {
256  return NULL;
257  }
258  if (out != NULL) {
259  RSA_free(*out);
260  *out = ret;
261  }
262  *inp = CBS_data(&cbs);
263  return ret;
264 }
265 
266 int i2d_RSAPublicKey(const RSA *in, uint8_t **outp) {
267  CBB cbb;
268  if (!CBB_init(&cbb, 0) ||
269  !RSA_marshal_public_key(&cbb, in)) {
270  CBB_cleanup(&cbb);
271  return -1;
272  }
273  return CBB_finish_i2d(&cbb, outp);
274 }
275 
276 RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len) {
277  if (len < 0) {
278  return NULL;
279  }
280  CBS cbs;
281  CBS_init(&cbs, *inp, (size_t)len);
283  if (ret == NULL) {
284  return NULL;
285  }
286  if (out != NULL) {
287  RSA_free(*out);
288  *out = ret;
289  }
290  *inp = CBS_data(&cbs);
291  return ret;
292 }
293 
294 int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp) {
295  CBB cbb;
296  if (!CBB_init(&cbb, 0) ||
297  !RSA_marshal_private_key(&cbb, in)) {
298  CBB_cleanup(&cbb);
299  return -1;
300  }
301  return CBB_finish_i2d(&cbb, outp);
302 }
303 
304 RSA *RSAPublicKey_dup(const RSA *rsa) {
305  uint8_t *der;
306  size_t der_len;
307  if (!RSA_public_key_to_bytes(&der, &der_len, rsa)) {
308  return NULL;
309  }
310  RSA *ret = RSA_public_key_from_bytes(der, der_len);
311  OPENSSL_free(der);
312  return ret;
313 }
314 
315 RSA *RSAPrivateKey_dup(const RSA *rsa) {
316  uint8_t *der;
317  size_t der_len;
318  if (!RSA_private_key_to_bytes(&der, &der_len, rsa)) {
319  return NULL;
320  }
321  RSA *ret = RSA_private_key_from_bytes(der, der_len);
322  OPENSSL_free(der);
323  return ret;
324 }
CBS_get_asn1_uint64
#define CBS_get_asn1_uint64
Definition: boringssl_prefix_symbols.h:1066
RSA_R_VALUE_MISSING
#define RSA_R_VALUE_MISSING
Definition: rsa.h:849
bn.h
CBB_flush
#define CBB_flush
Definition: boringssl_prefix_symbols.h:1045
CBB_init
#define CBB_init
Definition: boringssl_prefix_symbols.h:1047
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
RSA_R_ENCODE_ERROR
#define RSA_R_ENCODE_ERROR
Definition: rsa.h:826
cbs_st
Definition: bytestring.h:39
CBB_cleanup
#define CBB_cleanup
Definition: boringssl_prefix_symbols.h:1039
rsa_st::d
BIGNUM * d
Definition: rsa.h:740
CBB_zero
#define CBB_zero
Definition: boringssl_prefix_symbols.h:1051
CBS_data
#define CBS_data
Definition: boringssl_prefix_symbols.h:1057
i2d_RSAPrivateKey
int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp)
Definition: rsa_asn1.c:294
regen-readme.inp
inp
Definition: regen-readme.py:11
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
string.h
error_ref_leak.err
err
Definition: error_ref_leak.py:35
CBS_len
#define CBS_len
Definition: boringssl_prefix_symbols.h:1089
rsa_st::dmq1
BIGNUM * dmq1
Definition: rsa.h:744
d2i_RSAPrivateKey
RSA * d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len)
Definition: rsa_asn1.c:276
CBS_get_asn1
#define CBS_get_asn1
Definition: boringssl_prefix_symbols.h:1061
version
Definition: version.py:1
RSA_marshal_private_key
int RSA_marshal_private_key(CBB *cbb, const RSA *rsa)
Definition: rsa_asn1.c:215
cbs
const CBS * cbs
Definition: third_party/boringssl-with-bazel/src/crypto/trust_token/internal.h:107
CBS_init
#define CBS_init
Definition: boringssl_prefix_symbols.h:1085
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
RSA_public_key_from_bytes
RSA * RSA_public_key_from_bytes(const uint8_t *in, size_t in_len)
Definition: rsa_asn1.c:114
bytestring.h
RSA_private_key_to_bytes
int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len, const RSA *rsa)
Definition: rsa_asn1.c:234
RSA_R_BAD_VERSION
#define RSA_R_BAD_VERSION
Definition: rsa.h:811
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
RSA_parse_private_key
RSA * RSA_parse_private_key(CBS *cbs)
Definition: rsa_asn1.c:156
RSA_private_key_from_bytes
RSA * RSA_private_key_from_bytes(const uint8_t *in, size_t in_len)
Definition: rsa_asn1.c:203
RSA_free
#define RSA_free
Definition: boringssl_prefix_symbols.h:2090
rsa_st::p
BIGNUM * p
Definition: rsa.h:741
rsa_st::e
BIGNUM * e
Definition: rsa.h:739
CBB_finish
#define CBB_finish
Definition: boringssl_prefix_symbols.h:1043
rsa_st::dmp1
BIGNUM * dmp1
Definition: rsa.h:743
conf.version
string version
Definition: doc/python/sphinx/conf.py:36
RSA_parse_public_key
RSA * RSA_parse_public_key(CBS *cbs)
Definition: rsa_asn1.c:90
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
CBB_add_asn1
#define CBB_add_asn1
Definition: boringssl_prefix_symbols.h:1019
err.h
rsa.h
RSA_marshal_public_key
int RSA_marshal_public_key(CBB *cbb, const RSA *rsa)
Definition: rsa_asn1.c:126
googletest-filter-unittest.child
child
Definition: bloaty/third_party/googletest/googletest/test/googletest-filter-unittest.py:62
RSA_R_BAD_RSA_PARAMETERS
#define RSA_R_BAD_RSA_PARAMETERS
Definition: rsa.h:809
marshal_integer
static int marshal_integer(CBB *cbb, BIGNUM *bn)
Definition: rsa_asn1.c:81
RSA_new
#define RSA_new
Definition: boringssl_prefix_symbols.h:2110
parse_integer
static int parse_integer(CBS *cbs, BIGNUM **out)
Definition: rsa_asn1.c:72
rsa_st::q
BIGNUM * q
Definition: rsa.h:742
i2d_RSAPublicKey
int i2d_RSAPublicKey(const RSA *in, uint8_t **outp)
Definition: rsa_asn1.c:266
rsa_st::iqmp
BIGNUM * iqmp
Definition: rsa.h:745
RSA_R_BAD_ENCODING
#define RSA_R_BAD_ENCODING
Definition: rsa.h:805
bignum_st
Definition: bn.h:957
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
RSA_check_key
#define RSA_check_key
Definition: boringssl_prefix_symbols.h:2085
CBB_finish_i2d
#define CBB_finish_i2d
Definition: boringssl_prefix_symbols.h:1044
RSAPublicKey_dup
RSA * RSAPublicKey_dup(const RSA *rsa)
Definition: rsa_asn1.c:304
kVersionTwoPrime
static const uint64_t kVersionTwoPrime
Definition: rsa_asn1.c:154
RSA_public_key_to_bytes
int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len, const RSA *rsa)
Definition: rsa_asn1.c:138
BN_parse_asn1_unsigned
#define BN_parse_asn1_unsigned
Definition: boringssl_prefix_symbols.h:978
mem.h
rsa_st
Definition: rsa.h:732
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
BN_marshal_asn1
#define BN_marshal_asn1
Definition: boringssl_prefix_symbols.h:944
CBS_ASN1_SEQUENCE
#define CBS_ASN1_SEQUENCE
Definition: bytestring.h:214
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
RSAPrivateKey_dup
RSA * RSAPrivateKey_dup(const RSA *rsa)
Definition: rsa_asn1.c:315
BN_new
#define BN_new
Definition: boringssl_prefix_symbols.h:971
rsa_st::n
BIGNUM * n
Definition: rsa.h:738
d2i_RSAPublicKey
RSA * d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len)
Definition: rsa_asn1.c:248
CBB_add_asn1_uint64
#define CBB_add_asn1_uint64
Definition: boringssl_prefix_symbols.h:1024
cbb_st
Definition: bytestring.h:375


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:07