grpc 1.10.0.pre1 → 1.10.0.pre2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +3 -3
  3. data/src/core/ext/filters/max_age/max_age_filter.cc +3 -0
  4. data/src/core/lib/surface/version.cc +1 -1
  5. data/src/ruby/lib/grpc/version.rb +1 -1
  6. data/third_party/boringssl/crypto/fipsmodule/aes/aes.c +1100 -0
  7. data/third_party/boringssl/crypto/fipsmodule/aes/key_wrap.c +138 -0
  8. data/third_party/boringssl/crypto/fipsmodule/aes/mode_wrappers.c +112 -0
  9. data/third_party/boringssl/crypto/fipsmodule/bn/add.c +375 -0
  10. data/third_party/boringssl/crypto/fipsmodule/bn/asm/x86_64-gcc.c +537 -0
  11. data/third_party/boringssl/crypto/fipsmodule/bn/bn.c +370 -0
  12. data/third_party/boringssl/crypto/fipsmodule/bn/bytes.c +269 -0
  13. data/third_party/boringssl/crypto/fipsmodule/bn/cmp.c +239 -0
  14. data/third_party/boringssl/crypto/fipsmodule/bn/ctx.c +303 -0
  15. data/third_party/boringssl/crypto/fipsmodule/bn/div.c +726 -0
  16. data/third_party/boringssl/crypto/fipsmodule/bn/exponentiation.c +1233 -0
  17. data/third_party/boringssl/crypto/fipsmodule/bn/gcd.c +627 -0
  18. data/third_party/boringssl/crypto/fipsmodule/bn/generic.c +715 -0
  19. data/third_party/boringssl/crypto/fipsmodule/bn/jacobi.c +146 -0
  20. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery.c +430 -0
  21. data/third_party/boringssl/crypto/fipsmodule/bn/montgomery_inv.c +207 -0
  22. data/third_party/boringssl/crypto/fipsmodule/bn/mul.c +869 -0
  23. data/third_party/boringssl/crypto/fipsmodule/bn/prime.c +894 -0
  24. data/third_party/boringssl/crypto/fipsmodule/bn/random.c +283 -0
  25. data/third_party/boringssl/crypto/fipsmodule/bn/rsaz_exp.c +254 -0
  26. data/third_party/boringssl/crypto/fipsmodule/bn/shift.c +307 -0
  27. data/third_party/boringssl/crypto/fipsmodule/bn/sqrt.c +502 -0
  28. data/third_party/boringssl/crypto/fipsmodule/cipher/aead.c +284 -0
  29. data/third_party/boringssl/crypto/fipsmodule/cipher/cipher.c +613 -0
  30. data/third_party/boringssl/crypto/fipsmodule/cipher/e_aes.c +1437 -0
  31. data/third_party/boringssl/crypto/fipsmodule/cipher/e_des.c +233 -0
  32. data/third_party/boringssl/crypto/fipsmodule/des/des.c +785 -0
  33. data/third_party/boringssl/crypto/fipsmodule/digest/digest.c +256 -0
  34. data/third_party/boringssl/crypto/fipsmodule/digest/digests.c +280 -0
  35. data/third_party/boringssl/crypto/fipsmodule/ec/ec.c +842 -0
  36. data/third_party/boringssl/crypto/fipsmodule/ec/ec_key.c +517 -0
  37. data/third_party/boringssl/crypto/fipsmodule/ec/ec_montgomery.c +304 -0
  38. data/third_party/boringssl/crypto/fipsmodule/ec/oct.c +404 -0
  39. data/third_party/boringssl/crypto/fipsmodule/ec/p224-64.c +1165 -0
  40. data/third_party/boringssl/crypto/fipsmodule/ec/p256-64.c +1708 -0
  41. data/third_party/boringssl/crypto/fipsmodule/ec/p256-x86_64.c +561 -0
  42. data/third_party/boringssl/crypto/fipsmodule/ec/simple.c +1113 -0
  43. data/third_party/boringssl/crypto/fipsmodule/ec/util-64.c +109 -0
  44. data/third_party/boringssl/crypto/fipsmodule/ec/wnaf.c +456 -0
  45. data/third_party/boringssl/crypto/fipsmodule/ecdsa/ecdsa.c +444 -0
  46. data/third_party/boringssl/crypto/fipsmodule/hmac/hmac.c +228 -0
  47. data/third_party/boringssl/crypto/fipsmodule/md4/md4.c +254 -0
  48. data/third_party/boringssl/crypto/fipsmodule/md5/md5.c +298 -0
  49. data/third_party/boringssl/crypto/fipsmodule/modes/cbc.c +212 -0
  50. data/third_party/boringssl/crypto/fipsmodule/modes/cfb.c +230 -0
  51. data/third_party/boringssl/crypto/fipsmodule/modes/ctr.c +219 -0
  52. data/third_party/boringssl/crypto/fipsmodule/modes/gcm.c +1074 -0
  53. data/third_party/boringssl/crypto/fipsmodule/modes/ofb.c +95 -0
  54. data/third_party/boringssl/crypto/fipsmodule/modes/polyval.c +91 -0
  55. data/third_party/boringssl/crypto/fipsmodule/rand/ctrdrbg.c +200 -0
  56. data/third_party/boringssl/crypto/fipsmodule/rand/rand.c +358 -0
  57. data/third_party/boringssl/crypto/fipsmodule/rand/urandom.c +302 -0
  58. data/third_party/boringssl/crypto/fipsmodule/rsa/blinding.c +263 -0
  59. data/third_party/boringssl/crypto/fipsmodule/rsa/padding.c +692 -0
  60. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa.c +855 -0
  61. data/third_party/boringssl/crypto/fipsmodule/rsa/rsa_impl.c +1061 -0
  62. data/third_party/boringssl/crypto/fipsmodule/sha/sha1-altivec.c +361 -0
  63. data/third_party/boringssl/crypto/fipsmodule/sha/sha1.c +375 -0
  64. data/third_party/boringssl/crypto/fipsmodule/sha/sha256.c +337 -0
  65. data/third_party/boringssl/crypto/fipsmodule/sha/sha512.c +608 -0
  66. metadata +62 -2
@@ -0,0 +1,855 @@
1
+ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
+ * All rights reserved.
3
+ *
4
+ * This package is an SSL implementation written
5
+ * by Eric Young (eay@cryptsoft.com).
6
+ * The implementation was written so as to conform with Netscapes SSL.
7
+ *
8
+ * This library is free for commercial and non-commercial use as long as
9
+ * the following conditions are aheared to. The following conditions
10
+ * apply to all code found in this distribution, be it the RC4, RSA,
11
+ * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12
+ * included with this distribution is covered by the same copyright terms
13
+ * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
+ *
15
+ * Copyright remains Eric Young's, and as such any Copyright notices in
16
+ * the code are not to be removed.
17
+ * If this package is used in a product, Eric Young should be given attribution
18
+ * as the author of the parts of the library used.
19
+ * This can be in the form of a textual message at program startup or
20
+ * in documentation (online or textual) provided with the package.
21
+ *
22
+ * Redistribution and use in source and binary forms, with or without
23
+ * modification, are permitted provided that the following conditions
24
+ * are met:
25
+ * 1. Redistributions of source code must retain the copyright
26
+ * notice, this list of conditions and the following disclaimer.
27
+ * 2. Redistributions in binary form must reproduce the above copyright
28
+ * notice, this list of conditions and the following disclaimer in the
29
+ * documentation and/or other materials provided with the distribution.
30
+ * 3. All advertising materials mentioning features or use of this software
31
+ * must display the following acknowledgement:
32
+ * "This product includes cryptographic software written by
33
+ * Eric Young (eay@cryptsoft.com)"
34
+ * The word 'cryptographic' can be left out if the rouines from the library
35
+ * being used are not cryptographic related :-).
36
+ * 4. If you include any Windows specific code (or a derivative thereof) from
37
+ * the apps directory (application code) you must include an acknowledgement:
38
+ * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
+ *
40
+ * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
+ * SUCH DAMAGE.
51
+ *
52
+ * The licence and distribution terms for any publically available version or
53
+ * derivative of this code cannot be changed. i.e. this code cannot simply be
54
+ * copied and put under another distribution licence
55
+ * [including the GNU Public Licence.] */
56
+
57
+ #include <openssl/rsa.h>
58
+
59
+ #include <limits.h>
60
+ #include <string.h>
61
+
62
+ #include <openssl/bn.h>
63
+ #include <openssl/digest.h>
64
+ #include <openssl/engine.h>
65
+ #include <openssl/err.h>
66
+ #include <openssl/ex_data.h>
67
+ #include <openssl/md5.h>
68
+ #include <openssl/mem.h>
69
+ #include <openssl/nid.h>
70
+ #include <openssl/sha.h>
71
+ #include <openssl/thread.h>
72
+
73
+ #include "../bn/internal.h"
74
+ #include "../delocate.h"
75
+ #include "../../internal.h"
76
+ #include "internal.h"
77
+
78
+
79
+ DEFINE_STATIC_EX_DATA_CLASS(g_rsa_ex_data_class);
80
+
81
+ RSA *RSA_new(void) { return RSA_new_method(NULL); }
82
+
83
+ RSA *RSA_new_method(const ENGINE *engine) {
84
+ RSA *rsa = OPENSSL_malloc(sizeof(RSA));
85
+ if (rsa == NULL) {
86
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
87
+ return NULL;
88
+ }
89
+
90
+ OPENSSL_memset(rsa, 0, sizeof(RSA));
91
+
92
+ if (engine) {
93
+ rsa->meth = ENGINE_get_RSA_method(engine);
94
+ }
95
+
96
+ if (rsa->meth == NULL) {
97
+ rsa->meth = (RSA_METHOD *) RSA_default_method();
98
+ }
99
+ METHOD_ref(rsa->meth);
100
+
101
+ rsa->references = 1;
102
+ rsa->flags = rsa->meth->flags;
103
+ CRYPTO_MUTEX_init(&rsa->lock);
104
+ CRYPTO_new_ex_data(&rsa->ex_data);
105
+
106
+ if (rsa->meth->init && !rsa->meth->init(rsa)) {
107
+ CRYPTO_free_ex_data(g_rsa_ex_data_class_bss_get(), rsa, &rsa->ex_data);
108
+ CRYPTO_MUTEX_cleanup(&rsa->lock);
109
+ METHOD_unref(rsa->meth);
110
+ OPENSSL_free(rsa);
111
+ return NULL;
112
+ }
113
+
114
+ return rsa;
115
+ }
116
+
117
+ void RSA_free(RSA *rsa) {
118
+ unsigned u;
119
+
120
+ if (rsa == NULL) {
121
+ return;
122
+ }
123
+
124
+ if (!CRYPTO_refcount_dec_and_test_zero(&rsa->references)) {
125
+ return;
126
+ }
127
+
128
+ if (rsa->meth->finish) {
129
+ rsa->meth->finish(rsa);
130
+ }
131
+ METHOD_unref(rsa->meth);
132
+
133
+ CRYPTO_free_ex_data(g_rsa_ex_data_class_bss_get(), rsa, &rsa->ex_data);
134
+
135
+ BN_clear_free(rsa->n);
136
+ BN_clear_free(rsa->e);
137
+ BN_clear_free(rsa->d);
138
+ BN_clear_free(rsa->p);
139
+ BN_clear_free(rsa->q);
140
+ BN_clear_free(rsa->dmp1);
141
+ BN_clear_free(rsa->dmq1);
142
+ BN_clear_free(rsa->iqmp);
143
+ BN_MONT_CTX_free(rsa->mont_n);
144
+ BN_MONT_CTX_free(rsa->mont_p);
145
+ BN_MONT_CTX_free(rsa->mont_q);
146
+ for (u = 0; u < rsa->num_blindings; u++) {
147
+ BN_BLINDING_free(rsa->blindings[u]);
148
+ }
149
+ OPENSSL_free(rsa->blindings);
150
+ OPENSSL_free(rsa->blindings_inuse);
151
+ CRYPTO_MUTEX_cleanup(&rsa->lock);
152
+ OPENSSL_free(rsa);
153
+ }
154
+
155
+ int RSA_up_ref(RSA *rsa) {
156
+ CRYPTO_refcount_inc(&rsa->references);
157
+ return 1;
158
+ }
159
+
160
+ void RSA_get0_key(const RSA *rsa, const BIGNUM **out_n, const BIGNUM **out_e,
161
+ const BIGNUM **out_d) {
162
+ if (out_n != NULL) {
163
+ *out_n = rsa->n;
164
+ }
165
+ if (out_e != NULL) {
166
+ *out_e = rsa->e;
167
+ }
168
+ if (out_d != NULL) {
169
+ *out_d = rsa->d;
170
+ }
171
+ }
172
+
173
+ void RSA_get0_factors(const RSA *rsa, const BIGNUM **out_p,
174
+ const BIGNUM **out_q) {
175
+ if (out_p != NULL) {
176
+ *out_p = rsa->p;
177
+ }
178
+ if (out_q != NULL) {
179
+ *out_q = rsa->q;
180
+ }
181
+ }
182
+
183
+ void RSA_get0_crt_params(const RSA *rsa, const BIGNUM **out_dmp1,
184
+ const BIGNUM **out_dmq1, const BIGNUM **out_iqmp) {
185
+ if (out_dmp1 != NULL) {
186
+ *out_dmp1 = rsa->dmp1;
187
+ }
188
+ if (out_dmq1 != NULL) {
189
+ *out_dmq1 = rsa->dmq1;
190
+ }
191
+ if (out_iqmp != NULL) {
192
+ *out_iqmp = rsa->iqmp;
193
+ }
194
+ }
195
+
196
+ int RSA_set0_key(RSA *rsa, BIGNUM *n, BIGNUM *e, BIGNUM *d) {
197
+ if ((rsa->n == NULL && n == NULL) ||
198
+ (rsa->e == NULL && e == NULL)) {
199
+ return 0;
200
+ }
201
+
202
+ if (n != NULL) {
203
+ BN_free(rsa->n);
204
+ rsa->n = n;
205
+ }
206
+ if (e != NULL) {
207
+ BN_free(rsa->e);
208
+ rsa->e = e;
209
+ }
210
+ if (d != NULL) {
211
+ BN_free(rsa->d);
212
+ rsa->d = d;
213
+ }
214
+
215
+ return 1;
216
+ }
217
+
218
+ int RSA_set0_factors(RSA *rsa, BIGNUM *p, BIGNUM *q) {
219
+ if ((rsa->p == NULL && p == NULL) ||
220
+ (rsa->q == NULL && q == NULL)) {
221
+ return 0;
222
+ }
223
+
224
+ if (p != NULL) {
225
+ BN_free(rsa->p);
226
+ rsa->p = p;
227
+ }
228
+ if (q != NULL) {
229
+ BN_free(rsa->q);
230
+ rsa->q = q;
231
+ }
232
+
233
+ return 1;
234
+ }
235
+
236
+ int RSA_set0_crt_params(RSA *rsa, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) {
237
+ if ((rsa->dmp1 == NULL && dmp1 == NULL) ||
238
+ (rsa->dmq1 == NULL && dmq1 == NULL) ||
239
+ (rsa->iqmp == NULL && iqmp == NULL)) {
240
+ return 0;
241
+ }
242
+
243
+ if (dmp1 != NULL) {
244
+ BN_free(rsa->dmp1);
245
+ rsa->dmp1 = dmp1;
246
+ }
247
+ if (dmq1 != NULL) {
248
+ BN_free(rsa->dmq1);
249
+ rsa->dmq1 = dmq1;
250
+ }
251
+ if (iqmp != NULL) {
252
+ BN_free(rsa->iqmp);
253
+ rsa->iqmp = iqmp;
254
+ }
255
+
256
+ return 1;
257
+ }
258
+
259
+ int RSA_public_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
260
+ int padding) {
261
+ size_t out_len;
262
+
263
+ if (!RSA_encrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
264
+ return -1;
265
+ }
266
+
267
+ if (out_len > INT_MAX) {
268
+ OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
269
+ return -1;
270
+ }
271
+ return out_len;
272
+ }
273
+
274
+ int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
275
+ const uint8_t *in, size_t in_len, int padding) {
276
+ if (rsa->meth->sign_raw) {
277
+ return rsa->meth->sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
278
+ }
279
+
280
+ return rsa_default_sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
281
+ }
282
+
283
+ int RSA_private_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
284
+ int padding) {
285
+ size_t out_len;
286
+
287
+ if (!RSA_sign_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
288
+ return -1;
289
+ }
290
+
291
+ if (out_len > INT_MAX) {
292
+ OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
293
+ return -1;
294
+ }
295
+ return out_len;
296
+ }
297
+
298
+ int RSA_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
299
+ const uint8_t *in, size_t in_len, int padding) {
300
+ if (rsa->meth->decrypt) {
301
+ return rsa->meth->decrypt(rsa, out_len, out, max_out, in, in_len, padding);
302
+ }
303
+
304
+ return rsa_default_decrypt(rsa, out_len, out, max_out, in, in_len, padding);
305
+ }
306
+
307
+ int RSA_private_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
308
+ int padding) {
309
+ size_t out_len;
310
+
311
+ if (!RSA_decrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
312
+ return -1;
313
+ }
314
+
315
+ if (out_len > INT_MAX) {
316
+ OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
317
+ return -1;
318
+ }
319
+ return out_len;
320
+ }
321
+
322
+ int RSA_public_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
323
+ int padding) {
324
+ size_t out_len;
325
+
326
+ if (!RSA_verify_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
327
+ return -1;
328
+ }
329
+
330
+ if (out_len > INT_MAX) {
331
+ OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
332
+ return -1;
333
+ }
334
+ return out_len;
335
+ }
336
+
337
+ unsigned RSA_size(const RSA *rsa) {
338
+ if (rsa->meth->size) {
339
+ return rsa->meth->size(rsa);
340
+ }
341
+
342
+ return rsa_default_size(rsa);
343
+ }
344
+
345
+ int RSA_is_opaque(const RSA *rsa) {
346
+ return rsa->meth && (rsa->meth->flags & RSA_FLAG_OPAQUE);
347
+ }
348
+
349
+ int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
350
+ CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
351
+ int index;
352
+ if (!CRYPTO_get_ex_new_index(g_rsa_ex_data_class_bss_get(), &index, argl,
353
+ argp, free_func)) {
354
+ return -1;
355
+ }
356
+ return index;
357
+ }
358
+
359
+ int RSA_set_ex_data(RSA *rsa, int idx, void *arg) {
360
+ return CRYPTO_set_ex_data(&rsa->ex_data, idx, arg);
361
+ }
362
+
363
+ void *RSA_get_ex_data(const RSA *rsa, int idx) {
364
+ return CRYPTO_get_ex_data(&rsa->ex_data, idx);
365
+ }
366
+
367
+ // SSL_SIG_LENGTH is the size of an SSL/TLS (prior to TLS 1.2) signature: it's
368
+ // the length of an MD5 and SHA1 hash.
369
+ static const unsigned SSL_SIG_LENGTH = 36;
370
+
371
+ // pkcs1_sig_prefix contains the ASN.1, DER encoded prefix for a hash that is
372
+ // to be signed with PKCS#1.
373
+ struct pkcs1_sig_prefix {
374
+ // nid identifies the hash function.
375
+ int nid;
376
+ // hash_len is the expected length of the hash function.
377
+ uint8_t hash_len;
378
+ // len is the number of bytes of |bytes| which are valid.
379
+ uint8_t len;
380
+ // bytes contains the DER bytes.
381
+ uint8_t bytes[19];
382
+ };
383
+
384
+ // kPKCS1SigPrefixes contains the ASN.1 prefixes for PKCS#1 signatures with
385
+ // different hash functions.
386
+ static const struct pkcs1_sig_prefix kPKCS1SigPrefixes[] = {
387
+ {
388
+ NID_md5,
389
+ MD5_DIGEST_LENGTH,
390
+ 18,
391
+ {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
392
+ 0x02, 0x05, 0x05, 0x00, 0x04, 0x10},
393
+ },
394
+ {
395
+ NID_sha1,
396
+ SHA_DIGEST_LENGTH,
397
+ 15,
398
+ {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
399
+ 0x00, 0x04, 0x14},
400
+ },
401
+ {
402
+ NID_sha224,
403
+ SHA224_DIGEST_LENGTH,
404
+ 19,
405
+ {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
406
+ 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c},
407
+ },
408
+ {
409
+ NID_sha256,
410
+ SHA256_DIGEST_LENGTH,
411
+ 19,
412
+ {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
413
+ 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20},
414
+ },
415
+ {
416
+ NID_sha384,
417
+ SHA384_DIGEST_LENGTH,
418
+ 19,
419
+ {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
420
+ 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30},
421
+ },
422
+ {
423
+ NID_sha512,
424
+ SHA512_DIGEST_LENGTH,
425
+ 19,
426
+ {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
427
+ 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40},
428
+ },
429
+ {
430
+ NID_undef, 0, 0, {0},
431
+ },
432
+ };
433
+
434
+ int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len,
435
+ int *is_alloced, int hash_nid, const uint8_t *msg,
436
+ size_t msg_len) {
437
+ unsigned i;
438
+
439
+ if (hash_nid == NID_md5_sha1) {
440
+ // Special case: SSL signature, just check the length.
441
+ if (msg_len != SSL_SIG_LENGTH) {
442
+ OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
443
+ return 0;
444
+ }
445
+
446
+ *out_msg = (uint8_t*) msg;
447
+ *out_msg_len = SSL_SIG_LENGTH;
448
+ *is_alloced = 0;
449
+ return 1;
450
+ }
451
+
452
+ for (i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) {
453
+ const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i];
454
+ if (sig_prefix->nid != hash_nid) {
455
+ continue;
456
+ }
457
+
458
+ if (msg_len != sig_prefix->hash_len) {
459
+ OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
460
+ return 0;
461
+ }
462
+
463
+ const uint8_t* prefix = sig_prefix->bytes;
464
+ unsigned prefix_len = sig_prefix->len;
465
+ unsigned signed_msg_len;
466
+ uint8_t *signed_msg;
467
+
468
+ signed_msg_len = prefix_len + msg_len;
469
+ if (signed_msg_len < prefix_len) {
470
+ OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_LONG);
471
+ return 0;
472
+ }
473
+
474
+ signed_msg = OPENSSL_malloc(signed_msg_len);
475
+ if (!signed_msg) {
476
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
477
+ return 0;
478
+ }
479
+
480
+ OPENSSL_memcpy(signed_msg, prefix, prefix_len);
481
+ OPENSSL_memcpy(signed_msg + prefix_len, msg, msg_len);
482
+
483
+ *out_msg = signed_msg;
484
+ *out_msg_len = signed_msg_len;
485
+ *is_alloced = 1;
486
+
487
+ return 1;
488
+ }
489
+
490
+ OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE);
491
+ return 0;
492
+ }
493
+
494
+ int RSA_sign(int hash_nid, const uint8_t *in, unsigned in_len, uint8_t *out,
495
+ unsigned *out_len, RSA *rsa) {
496
+ const unsigned rsa_size = RSA_size(rsa);
497
+ int ret = 0;
498
+ uint8_t *signed_msg = NULL;
499
+ size_t signed_msg_len = 0;
500
+ int signed_msg_is_alloced = 0;
501
+ size_t size_t_out_len;
502
+
503
+ if (rsa->meth->sign) {
504
+ return rsa->meth->sign(hash_nid, in, in_len, out, out_len, rsa);
505
+ }
506
+
507
+ if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
508
+ &signed_msg_is_alloced, hash_nid, in, in_len) ||
509
+ !RSA_sign_raw(rsa, &size_t_out_len, out, rsa_size, signed_msg,
510
+ signed_msg_len, RSA_PKCS1_PADDING)) {
511
+ goto err;
512
+ }
513
+
514
+ *out_len = size_t_out_len;
515
+ ret = 1;
516
+
517
+ err:
518
+ if (signed_msg_is_alloced) {
519
+ OPENSSL_free(signed_msg);
520
+ }
521
+ return ret;
522
+ }
523
+
524
+ int RSA_sign_pss_mgf1(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
525
+ const uint8_t *in, size_t in_len, const EVP_MD *md,
526
+ const EVP_MD *mgf1_md, int salt_len) {
527
+ if (in_len != EVP_MD_size(md)) {
528
+ OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
529
+ return 0;
530
+ }
531
+
532
+ size_t padded_len = RSA_size(rsa);
533
+ uint8_t *padded = OPENSSL_malloc(padded_len);
534
+ if (padded == NULL) {
535
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
536
+ return 0;
537
+ }
538
+
539
+ int ret =
540
+ RSA_padding_add_PKCS1_PSS_mgf1(rsa, padded, in, md, mgf1_md, salt_len) &&
541
+ RSA_sign_raw(rsa, out_len, out, max_out, padded, padded_len,
542
+ RSA_NO_PADDING);
543
+ OPENSSL_free(padded);
544
+ return ret;
545
+ }
546
+
547
+ int RSA_verify(int hash_nid, const uint8_t *msg, size_t msg_len,
548
+ const uint8_t *sig, size_t sig_len, RSA *rsa) {
549
+ if (rsa->n == NULL || rsa->e == NULL) {
550
+ OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
551
+ return 0;
552
+ }
553
+
554
+ const size_t rsa_size = RSA_size(rsa);
555
+ uint8_t *buf = NULL;
556
+ int ret = 0;
557
+ uint8_t *signed_msg = NULL;
558
+ size_t signed_msg_len = 0, len;
559
+ int signed_msg_is_alloced = 0;
560
+
561
+ if (hash_nid == NID_md5_sha1 && msg_len != SSL_SIG_LENGTH) {
562
+ OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
563
+ return 0;
564
+ }
565
+
566
+ buf = OPENSSL_malloc(rsa_size);
567
+ if (!buf) {
568
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
569
+ return 0;
570
+ }
571
+
572
+ if (!RSA_verify_raw(rsa, &len, buf, rsa_size, sig, sig_len,
573
+ RSA_PKCS1_PADDING)) {
574
+ goto out;
575
+ }
576
+
577
+ if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
578
+ &signed_msg_is_alloced, hash_nid, msg, msg_len)) {
579
+ goto out;
580
+ }
581
+
582
+ // Check that no other information follows the hash value (FIPS 186-4 Section
583
+ // 5.5) and it matches the expected hash.
584
+ if (len != signed_msg_len || OPENSSL_memcmp(buf, signed_msg, len) != 0) {
585
+ OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE);
586
+ goto out;
587
+ }
588
+
589
+ ret = 1;
590
+
591
+ out:
592
+ OPENSSL_free(buf);
593
+ if (signed_msg_is_alloced) {
594
+ OPENSSL_free(signed_msg);
595
+ }
596
+ return ret;
597
+ }
598
+
599
+ int RSA_verify_pss_mgf1(RSA *rsa, const uint8_t *msg, size_t msg_len,
600
+ const EVP_MD *md, const EVP_MD *mgf1_md, int salt_len,
601
+ const uint8_t *sig, size_t sig_len) {
602
+ if (msg_len != EVP_MD_size(md)) {
603
+ OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
604
+ return 0;
605
+ }
606
+
607
+ size_t em_len = RSA_size(rsa);
608
+ uint8_t *em = OPENSSL_malloc(em_len);
609
+ if (em == NULL) {
610
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
611
+ return 0;
612
+ }
613
+
614
+ int ret = 0;
615
+ if (!RSA_verify_raw(rsa, &em_len, em, em_len, sig, sig_len, RSA_NO_PADDING)) {
616
+ goto err;
617
+ }
618
+
619
+ if (em_len != RSA_size(rsa)) {
620
+ OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
621
+ goto err;
622
+ }
623
+
624
+ ret = RSA_verify_PKCS1_PSS_mgf1(rsa, msg, md, mgf1_md, em, salt_len);
625
+
626
+ err:
627
+ OPENSSL_free(em);
628
+ return ret;
629
+ }
630
+
631
+ int RSA_check_key(const RSA *key) {
632
+ BIGNUM n, pm1, qm1, lcm, gcd, de, dmp1, dmq1, iqmp_times_q;
633
+ BN_CTX *ctx;
634
+ int ok = 0, has_crt_values;
635
+
636
+ if (RSA_is_opaque(key)) {
637
+ // Opaque keys can't be checked.
638
+ return 1;
639
+ }
640
+
641
+ if ((key->p != NULL) != (key->q != NULL)) {
642
+ OPENSSL_PUT_ERROR(RSA, RSA_R_ONLY_ONE_OF_P_Q_GIVEN);
643
+ return 0;
644
+ }
645
+
646
+ if (!key->n || !key->e) {
647
+ OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
648
+ return 0;
649
+ }
650
+
651
+ if (!key->d || !key->p) {
652
+ // For a public key, or without p and q, there's nothing that can be
653
+ // checked.
654
+ return 1;
655
+ }
656
+
657
+ ctx = BN_CTX_new();
658
+ if (ctx == NULL) {
659
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
660
+ return 0;
661
+ }
662
+
663
+ BN_init(&n);
664
+ BN_init(&pm1);
665
+ BN_init(&qm1);
666
+ BN_init(&lcm);
667
+ BN_init(&gcd);
668
+ BN_init(&de);
669
+ BN_init(&dmp1);
670
+ BN_init(&dmq1);
671
+ BN_init(&iqmp_times_q);
672
+
673
+ if (!BN_mul(&n, key->p, key->q, ctx) ||
674
+ // lcm = lcm(p, q)
675
+ !BN_sub(&pm1, key->p, BN_value_one()) ||
676
+ !BN_sub(&qm1, key->q, BN_value_one()) ||
677
+ !BN_mul(&lcm, &pm1, &qm1, ctx) ||
678
+ !BN_gcd(&gcd, &pm1, &qm1, ctx)) {
679
+ OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
680
+ goto out;
681
+ }
682
+
683
+ if (!BN_div(&lcm, NULL, &lcm, &gcd, ctx) ||
684
+ !BN_gcd(&gcd, &pm1, &qm1, ctx) ||
685
+ // de = d*e mod lcm(p, q).
686
+ !BN_mod_mul(&de, key->d, key->e, &lcm, ctx)) {
687
+ OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
688
+ goto out;
689
+ }
690
+
691
+ if (BN_cmp(&n, key->n) != 0) {
692
+ OPENSSL_PUT_ERROR(RSA, RSA_R_N_NOT_EQUAL_P_Q);
693
+ goto out;
694
+ }
695
+
696
+ if (!BN_is_one(&de)) {
697
+ OPENSSL_PUT_ERROR(RSA, RSA_R_D_E_NOT_CONGRUENT_TO_1);
698
+ goto out;
699
+ }
700
+
701
+ has_crt_values = key->dmp1 != NULL;
702
+ if (has_crt_values != (key->dmq1 != NULL) ||
703
+ has_crt_values != (key->iqmp != NULL)) {
704
+ OPENSSL_PUT_ERROR(RSA, RSA_R_INCONSISTENT_SET_OF_CRT_VALUES);
705
+ goto out;
706
+ }
707
+
708
+ if (has_crt_values) {
709
+ if (// dmp1 = d mod (p-1)
710
+ !BN_mod(&dmp1, key->d, &pm1, ctx) ||
711
+ // dmq1 = d mod (q-1)
712
+ !BN_mod(&dmq1, key->d, &qm1, ctx) ||
713
+ // iqmp = q^-1 mod p
714
+ !BN_mod_mul(&iqmp_times_q, key->iqmp, key->q, key->p, ctx)) {
715
+ OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
716
+ goto out;
717
+ }
718
+
719
+ if (BN_cmp(&dmp1, key->dmp1) != 0 ||
720
+ BN_cmp(&dmq1, key->dmq1) != 0 ||
721
+ BN_cmp(key->iqmp, key->p) >= 0 ||
722
+ !BN_is_one(&iqmp_times_q)) {
723
+ OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_VALUES_INCORRECT);
724
+ goto out;
725
+ }
726
+ }
727
+
728
+ ok = 1;
729
+
730
+ out:
731
+ BN_free(&n);
732
+ BN_free(&pm1);
733
+ BN_free(&qm1);
734
+ BN_free(&lcm);
735
+ BN_free(&gcd);
736
+ BN_free(&de);
737
+ BN_free(&dmp1);
738
+ BN_free(&dmq1);
739
+ BN_free(&iqmp_times_q);
740
+ BN_CTX_free(ctx);
741
+
742
+ return ok;
743
+ }
744
+
745
+
746
+ // This is the product of the 132 smallest odd primes, from 3 to 751.
747
+ static const BN_ULONG kSmallFactorsLimbs[] = {
748
+ TOBN(0xc4309333, 0x3ef4e3e1), TOBN(0x71161eb6, 0xcd2d655f),
749
+ TOBN(0x95e2238c, 0x0bf94862), TOBN(0x3eb233d3, 0x24f7912b),
750
+ TOBN(0x6b55514b, 0xbf26c483), TOBN(0x0a84d817, 0x5a144871),
751
+ TOBN(0x77d12fee, 0x9b82210a), TOBN(0xdb5b93c2, 0x97f050b3),
752
+ TOBN(0x4acad6b9, 0x4d6c026b), TOBN(0xeb7751f3, 0x54aec893),
753
+ TOBN(0xdba53368, 0x36bc85c4), TOBN(0xd85a1b28, 0x7f5ec78e),
754
+ TOBN(0x2eb072d8, 0x6b322244), TOBN(0xbba51112, 0x5e2b3aea),
755
+ TOBN(0x36ed1a6c, 0x0e2486bf), TOBN(0x5f270460, 0xec0c5727),
756
+ 0x000017b1
757
+ };
758
+
759
+ DEFINE_LOCAL_DATA(BIGNUM, g_small_factors) {
760
+ out->d = (BN_ULONG *) kSmallFactorsLimbs;
761
+ out->top = OPENSSL_ARRAY_SIZE(kSmallFactorsLimbs);
762
+ out->dmax = out->top;
763
+ out->neg = 0;
764
+ out->flags = BN_FLG_STATIC_DATA;
765
+ }
766
+
767
+ int RSA_check_fips(RSA *key) {
768
+ if (RSA_is_opaque(key)) {
769
+ // Opaque keys can't be checked.
770
+ OPENSSL_PUT_ERROR(RSA, RSA_R_PUBLIC_KEY_VALIDATION_FAILED);
771
+ return 0;
772
+ }
773
+
774
+ if (!RSA_check_key(key)) {
775
+ return 0;
776
+ }
777
+
778
+ BN_CTX *ctx = BN_CTX_new();
779
+ if (ctx == NULL) {
780
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
781
+ return 0;
782
+ }
783
+
784
+ BIGNUM small_gcd;
785
+ BN_init(&small_gcd);
786
+
787
+ int ret = 1;
788
+
789
+ // Perform partial public key validation of RSA keys (SP 800-89 5.3.3).
790
+ enum bn_primality_result_t primality_result;
791
+ if (BN_num_bits(key->e) <= 16 ||
792
+ BN_num_bits(key->e) > 256 ||
793
+ !BN_is_odd(key->n) ||
794
+ !BN_is_odd(key->e) ||
795
+ !BN_gcd(&small_gcd, key->n, g_small_factors(), ctx) ||
796
+ !BN_is_one(&small_gcd) ||
797
+ !BN_enhanced_miller_rabin_primality_test(&primality_result, key->n,
798
+ BN_prime_checks, ctx, NULL) ||
799
+ primality_result != bn_non_prime_power_composite) {
800
+ OPENSSL_PUT_ERROR(RSA, RSA_R_PUBLIC_KEY_VALIDATION_FAILED);
801
+ ret = 0;
802
+ }
803
+
804
+ BN_free(&small_gcd);
805
+ BN_CTX_free(ctx);
806
+
807
+ if (!ret || key->d == NULL || key->p == NULL) {
808
+ // On a failure or on only a public key, there's nothing else can be
809
+ // checked.
810
+ return ret;
811
+ }
812
+
813
+ // FIPS pairwise consistency test (FIPS 140-2 4.9.2). Per FIPS 140-2 IG,
814
+ // section 9.9, it is not known whether |rsa| will be used for signing or
815
+ // encryption, so either pair-wise consistency self-test is acceptable. We
816
+ // perform a signing test.
817
+ uint8_t data[32] = {0};
818
+ unsigned sig_len = RSA_size(key);
819
+ uint8_t *sig = OPENSSL_malloc(sig_len);
820
+ if (sig == NULL) {
821
+ OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
822
+ return 0;
823
+ }
824
+
825
+ if (!RSA_sign(NID_sha256, data, sizeof(data), sig, &sig_len, key)) {
826
+ OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
827
+ ret = 0;
828
+ goto cleanup;
829
+ }
830
+ #if defined(BORINGSSL_FIPS_BREAK_RSA_PWCT)
831
+ data[0] = ~data[0];
832
+ #endif
833
+ if (!RSA_verify(NID_sha256, data, sizeof(data), sig, sig_len, key)) {
834
+ OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
835
+ ret = 0;
836
+ }
837
+
838
+ cleanup:
839
+ OPENSSL_free(sig);
840
+
841
+ return ret;
842
+ }
843
+
844
+ int RSA_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
845
+ size_t len) {
846
+ if (rsa->meth->private_transform) {
847
+ return rsa->meth->private_transform(rsa, out, in, len);
848
+ }
849
+
850
+ return rsa_default_private_transform(rsa, out, in, len);
851
+ }
852
+
853
+ int RSA_blinding_on(RSA *rsa, BN_CTX *ctx) {
854
+ return 1;
855
+ }