Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 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 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 58 | #include <assert.h> |
| 59 | #include <limits.h> |
| 60 | #include <string.h> |
| 61 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 62 | #include <openssl/bn.h> |
| 63 | #include <openssl/bytestring.h> |
| 64 | #include <openssl/err.h> |
| 65 | #include <openssl/mem.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 66 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 67 | #include "../fipsmodule/rsa/internal.h" |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 68 | #include "../bytestring/internal.h" |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 69 | #include "../internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 70 | |
| 71 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 72 | static int parse_integer_buggy(CBS *cbs, BIGNUM **out, int buggy) { |
| 73 | assert(*out == NULL); |
| 74 | *out = BN_new(); |
| 75 | if (*out == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 76 | return 0; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 77 | } |
| 78 | if (buggy) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 79 | return BN_parse_asn1_unsigned_buggy(cbs, *out); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 80 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 81 | return BN_parse_asn1_unsigned(cbs, *out); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static int parse_integer(CBS *cbs, BIGNUM **out) { |
| 85 | return parse_integer_buggy(cbs, out, 0 /* not buggy */); |
| 86 | } |
| 87 | |
| 88 | static int marshal_integer(CBB *cbb, BIGNUM *bn) { |
| 89 | if (bn == NULL) { |
| 90 | /* An RSA object may be missing some components. */ |
| 91 | OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING); |
| 92 | return 0; |
| 93 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 94 | return BN_marshal_asn1(cbb, bn); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static RSA *parse_public_key(CBS *cbs, int buggy) { |
| 98 | RSA *ret = RSA_new(); |
| 99 | if (ret == NULL) { |
| 100 | return NULL; |
| 101 | } |
| 102 | CBS child; |
| 103 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
| 104 | !parse_integer_buggy(&child, &ret->n, buggy) || |
| 105 | !parse_integer(&child, &ret->e) || |
| 106 | CBS_len(&child) != 0) { |
| 107 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
| 108 | RSA_free(ret); |
| 109 | return NULL; |
| 110 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 111 | |
| 112 | if (!BN_is_odd(ret->e) || |
| 113 | BN_num_bits(ret->e) < 2) { |
| 114 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS); |
| 115 | RSA_free(ret); |
| 116 | return NULL; |
| 117 | } |
| 118 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | RSA *RSA_parse_public_key(CBS *cbs) { |
| 123 | return parse_public_key(cbs, 0 /* not buggy */); |
| 124 | } |
| 125 | |
| 126 | RSA *RSA_parse_public_key_buggy(CBS *cbs) { |
| 127 | /* Estonian IDs issued between September 2014 to September 2015 are |
| 128 | * broken. See https://crbug.com/532048 and https://crbug.com/534766. |
| 129 | * |
| 130 | * TODO(davidben): Remove this code and callers in March 2016. */ |
| 131 | return parse_public_key(cbs, 1 /* buggy */); |
| 132 | } |
| 133 | |
| 134 | RSA *RSA_public_key_from_bytes(const uint8_t *in, size_t in_len) { |
| 135 | CBS cbs; |
| 136 | CBS_init(&cbs, in, in_len); |
| 137 | RSA *ret = RSA_parse_public_key(&cbs); |
| 138 | if (ret == NULL || CBS_len(&cbs) != 0) { |
| 139 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
| 140 | RSA_free(ret); |
| 141 | return NULL; |
| 142 | } |
| 143 | return ret; |
| 144 | } |
| 145 | |
| 146 | int RSA_marshal_public_key(CBB *cbb, const RSA *rsa) { |
| 147 | CBB child; |
| 148 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
| 149 | !marshal_integer(&child, rsa->n) || |
| 150 | !marshal_integer(&child, rsa->e) || |
| 151 | !CBB_flush(cbb)) { |
| 152 | OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); |
| 153 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 154 | } |
| 155 | return 1; |
| 156 | } |
| 157 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 158 | int RSA_public_key_to_bytes(uint8_t **out_bytes, size_t *out_len, |
| 159 | const RSA *rsa) { |
| 160 | CBB cbb; |
| 161 | CBB_zero(&cbb); |
| 162 | if (!CBB_init(&cbb, 0) || |
| 163 | !RSA_marshal_public_key(&cbb, rsa) || |
| 164 | !CBB_finish(&cbb, out_bytes, out_len)) { |
| 165 | OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); |
| 166 | CBB_cleanup(&cbb); |
| 167 | return 0; |
| 168 | } |
| 169 | return 1; |
| 170 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 171 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 172 | /* kVersionTwoPrime is the value of the version field for a two-prime |
| 173 | * RSAPrivateKey structure (RFC 3447). */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 174 | static const uint64_t kVersionTwoPrime = 0; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 175 | |
| 176 | RSA *RSA_parse_private_key(CBS *cbs) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 177 | RSA *ret = RSA_new(); |
| 178 | if (ret == NULL) { |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | CBS child; |
| 183 | uint64_t version; |
| 184 | if (!CBS_get_asn1(cbs, &child, CBS_ASN1_SEQUENCE) || |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 185 | !CBS_get_asn1_uint64(&child, &version)) { |
| 186 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
| 187 | goto err; |
| 188 | } |
| 189 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 190 | if (version != kVersionTwoPrime) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 191 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_VERSION); |
| 192 | goto err; |
| 193 | } |
| 194 | |
| 195 | if (!parse_integer(&child, &ret->n) || |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 196 | !parse_integer(&child, &ret->e) || |
| 197 | !parse_integer(&child, &ret->d) || |
| 198 | !parse_integer(&child, &ret->p) || |
| 199 | !parse_integer(&child, &ret->q) || |
| 200 | !parse_integer(&child, &ret->dmp1) || |
| 201 | !parse_integer(&child, &ret->dmq1) || |
| 202 | !parse_integer(&child, &ret->iqmp)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 203 | goto err; |
| 204 | } |
| 205 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 206 | if (CBS_len(&child) != 0) { |
| 207 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
| 208 | goto err; |
| 209 | } |
| 210 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 211 | if (!RSA_check_key(ret)) { |
| 212 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS); |
| 213 | goto err; |
| 214 | } |
| 215 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 216 | return ret; |
| 217 | |
| 218 | err: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 219 | RSA_free(ret); |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | RSA *RSA_private_key_from_bytes(const uint8_t *in, size_t in_len) { |
| 224 | CBS cbs; |
| 225 | CBS_init(&cbs, in, in_len); |
| 226 | RSA *ret = RSA_parse_private_key(&cbs); |
| 227 | if (ret == NULL || CBS_len(&cbs) != 0) { |
| 228 | OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_ENCODING); |
| 229 | RSA_free(ret); |
| 230 | return NULL; |
| 231 | } |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | int RSA_marshal_private_key(CBB *cbb, const RSA *rsa) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 236 | CBB child; |
| 237 | if (!CBB_add_asn1(cbb, &child, CBS_ASN1_SEQUENCE) || |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 238 | !CBB_add_asn1_uint64(&child, kVersionTwoPrime) || |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 239 | !marshal_integer(&child, rsa->n) || |
| 240 | !marshal_integer(&child, rsa->e) || |
| 241 | !marshal_integer(&child, rsa->d) || |
| 242 | !marshal_integer(&child, rsa->p) || |
| 243 | !marshal_integer(&child, rsa->q) || |
| 244 | !marshal_integer(&child, rsa->dmp1) || |
| 245 | !marshal_integer(&child, rsa->dmq1) || |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 246 | !marshal_integer(&child, rsa->iqmp) || |
| 247 | !CBB_flush(cbb)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 248 | OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); |
| 249 | return 0; |
| 250 | } |
| 251 | return 1; |
| 252 | } |
| 253 | |
| 254 | int RSA_private_key_to_bytes(uint8_t **out_bytes, size_t *out_len, |
| 255 | const RSA *rsa) { |
| 256 | CBB cbb; |
| 257 | CBB_zero(&cbb); |
| 258 | if (!CBB_init(&cbb, 0) || |
| 259 | !RSA_marshal_private_key(&cbb, rsa) || |
| 260 | !CBB_finish(&cbb, out_bytes, out_len)) { |
| 261 | OPENSSL_PUT_ERROR(RSA, RSA_R_ENCODE_ERROR); |
| 262 | CBB_cleanup(&cbb); |
| 263 | return 0; |
| 264 | } |
| 265 | return 1; |
| 266 | } |
| 267 | |
| 268 | RSA *d2i_RSAPublicKey(RSA **out, const uint8_t **inp, long len) { |
| 269 | if (len < 0) { |
| 270 | return NULL; |
| 271 | } |
| 272 | CBS cbs; |
| 273 | CBS_init(&cbs, *inp, (size_t)len); |
| 274 | RSA *ret = RSA_parse_public_key(&cbs); |
| 275 | if (ret == NULL) { |
| 276 | return NULL; |
| 277 | } |
| 278 | if (out != NULL) { |
| 279 | RSA_free(*out); |
| 280 | *out = ret; |
| 281 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 282 | *inp = CBS_data(&cbs); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | int i2d_RSAPublicKey(const RSA *in, uint8_t **outp) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 287 | CBB cbb; |
| 288 | if (!CBB_init(&cbb, 0) || |
| 289 | !RSA_marshal_public_key(&cbb, in)) { |
| 290 | CBB_cleanup(&cbb); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 291 | return -1; |
| 292 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 293 | return CBB_finish_i2d(&cbb, outp); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | RSA *d2i_RSAPrivateKey(RSA **out, const uint8_t **inp, long len) { |
| 297 | if (len < 0) { |
| 298 | return NULL; |
| 299 | } |
| 300 | CBS cbs; |
| 301 | CBS_init(&cbs, *inp, (size_t)len); |
| 302 | RSA *ret = RSA_parse_private_key(&cbs); |
| 303 | if (ret == NULL) { |
| 304 | return NULL; |
| 305 | } |
| 306 | if (out != NULL) { |
| 307 | RSA_free(*out); |
| 308 | *out = ret; |
| 309 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 310 | *inp = CBS_data(&cbs); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | int i2d_RSAPrivateKey(const RSA *in, uint8_t **outp) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 315 | CBB cbb; |
| 316 | if (!CBB_init(&cbb, 0) || |
| 317 | !RSA_marshal_private_key(&cbb, in)) { |
| 318 | CBB_cleanup(&cbb); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 319 | return -1; |
| 320 | } |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 321 | return CBB_finish_i2d(&cbb, outp); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 322 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 323 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 324 | RSA *RSAPublicKey_dup(const RSA *rsa) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 325 | uint8_t *der; |
| 326 | size_t der_len; |
| 327 | if (!RSA_public_key_to_bytes(&der, &der_len, rsa)) { |
| 328 | return NULL; |
| 329 | } |
| 330 | RSA *ret = RSA_public_key_from_bytes(der, der_len); |
| 331 | OPENSSL_free(der); |
| 332 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | RSA *RSAPrivateKey_dup(const RSA *rsa) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 336 | uint8_t *der; |
| 337 | size_t der_len; |
| 338 | if (!RSA_private_key_to_bytes(&der, &der_len, rsa)) { |
| 339 | return NULL; |
| 340 | } |
| 341 | RSA *ret = RSA_private_key_from_bytes(der, der_len); |
| 342 | OPENSSL_free(der); |
| 343 | return ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 344 | } |