353b9d872568d09d1e09a89326ccba9a03020fb6
[openssl.git] / crypto / rsa / rsa_lib.c
1 /*
2  * Copyright 1995-2018 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 #include <stdio.h>
11 #include <openssl/crypto.h>
12 #include "internal/cryptlib.h"
13 #include "internal/refcount.h"
14 #include "crypto/bn.h"
15 #include <openssl/engine.h>
16 #include <openssl/evp.h>
17 #include "crypto/evp.h"
18 #include "crypto/rsa.h"
19 #include "rsa_local.h"
20
21 RSA *RSA_new(void)
22 {
23     return RSA_new_method(NULL);
24 }
25
26 const RSA_METHOD *RSA_get_method(const RSA *rsa)
27 {
28     return rsa->meth;
29 }
30
31 int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
32 {
33     /*
34      * NB: The caller is specifically setting a method, so it's not up to us
35      * to deal with which ENGINE it comes from.
36      */
37     const RSA_METHOD *mtmp;
38     mtmp = rsa->meth;
39     if (mtmp->finish)
40         mtmp->finish(rsa);
41 #ifndef OPENSSL_NO_ENGINE
42     ENGINE_finish(rsa->engine);
43     rsa->engine = NULL;
44 #endif
45     rsa->meth = meth;
46     if (meth->init)
47         meth->init(rsa);
48     return 1;
49 }
50
51 RSA *RSA_new_method(ENGINE *engine)
52 {
53     RSA *ret = OPENSSL_zalloc(sizeof(*ret));
54
55     if (ret == NULL) {
56         RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
57         return NULL;
58     }
59
60     ret->references = 1;
61     ret->lock = CRYPTO_THREAD_lock_new();
62     if (ret->lock == NULL) {
63         RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
64         OPENSSL_free(ret);
65         return NULL;
66     }
67
68     ret->meth = RSA_get_default_method();
69 #ifndef OPENSSL_NO_ENGINE
70     ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
71     if (engine) {
72         if (!ENGINE_init(engine)) {
73             RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
74             goto err;
75         }
76         ret->engine = engine;
77     } else {
78         ret->engine = ENGINE_get_default_RSA();
79     }
80     if (ret->engine) {
81         ret->meth = ENGINE_get_RSA(ret->engine);
82         if (ret->meth == NULL) {
83             RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
84             goto err;
85         }
86     }
87 #endif
88
89     ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
90     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
91         goto err;
92     }
93
94     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
95         RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
96         goto err;
97     }
98
99     return ret;
100
101  err:
102     RSA_free(ret);
103     return NULL;
104 }
105
106 void RSA_free(RSA *r)
107 {
108     int i;
109
110     if (r == NULL)
111         return;
112
113     CRYPTO_DOWN_REF(&r->references, &i, r->lock);
114     REF_PRINT_COUNT("RSA", r);
115     if (i > 0)
116         return;
117     REF_ASSERT_ISNT(i < 0);
118
119     if (r->meth != NULL && r->meth->finish != NULL)
120         r->meth->finish(r);
121 #ifndef OPENSSL_NO_ENGINE
122     ENGINE_finish(r->engine);
123 #endif
124
125     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
126
127     CRYPTO_THREAD_lock_free(r->lock);
128
129     BN_free(r->n);
130     BN_free(r->e);
131     BN_clear_free(r->d);
132     BN_clear_free(r->p);
133     BN_clear_free(r->q);
134     BN_clear_free(r->dmp1);
135     BN_clear_free(r->dmq1);
136     BN_clear_free(r->iqmp);
137     RSA_PSS_PARAMS_free(r->pss);
138     sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
139     BN_BLINDING_free(r->blinding);
140     BN_BLINDING_free(r->mt_blinding);
141     OPENSSL_free(r->bignum_data);
142     OPENSSL_free(r);
143 }
144
145 int RSA_up_ref(RSA *r)
146 {
147     int i;
148
149     if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
150         return 0;
151
152     REF_PRINT_COUNT("RSA", r);
153     REF_ASSERT_ISNT(i < 2);
154     return i > 1 ? 1 : 0;
155 }
156
157 int RSA_set_ex_data(RSA *r, int idx, void *arg)
158 {
159     return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
160 }
161
162 void *RSA_get_ex_data(const RSA *r, int idx)
163 {
164     return CRYPTO_get_ex_data(&r->ex_data, idx);
165 }
166
167 /*
168  * Define a scaling constant for our fixed point arithmetic.
169  * This value must be a power of two because the base two logarithm code
170  * makes this assumption.  The exponent must also be a multiple of three so
171  * that the scale factor has an exact cube root.  Finally, the scale factor
172  * should not be so large that a multiplication of two scaled numbers
173  * overflows a 64 bit unsigned integer.
174  */
175 static const unsigned int scale = 1 << 18;
176 static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
177
178 /* Define some constants, none exceed 32 bits */
179 static const unsigned int log_2  = 0x02c5c8;    /* scale * log(2) */
180 static const unsigned int log_e  = 0x05c551;    /* scale * log2(M_E) */
181 static const unsigned int c1_923 = 0x07b126;    /* scale * 1.923 */
182 static const unsigned int c4_690 = 0x12c28f;    /* scale * 4.690 */
183
184 /*
185  * Multiply two scaled integers together and rescale the result.
186  */
187 static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
188 {
189     return a * b / scale;
190 }
191
192 /*
193  * Calculate the cube root of a 64 bit scaled integer.
194  * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
195  * integer, this is not guaranteed after scaling, so this function has a
196  * 64 bit return.  This uses the shifting nth root algorithm with some
197  * algebraic simplifications.
198  */
199 static uint64_t icbrt64(uint64_t x)
200 {
201     uint64_t r = 0;
202     uint64_t b;
203     int s;
204
205     for (s = 63; s >= 0; s -= 3) {
206         r <<= 1;
207         b = 3 * r * (r + 1) + 1;
208         if ((x >> s) >= b) {
209             x -= b << s;
210             r++;
211         }
212     }
213     return r * cbrt_scale;
214 }
215
216 /*
217  * Calculate the natural logarithm of a 64 bit scaled integer.
218  * This is done by calculating a base two logarithm and scaling.
219  * The maximum logarithm (base 2) is 64 and this reduces base e, so
220  * a 32 bit result should not overflow.  The argument passed must be
221  * greater than unity so we don't need to handle negative results.
222  */
223 static uint32_t ilog_e(uint64_t v)
224 {
225     uint32_t i, r = 0;
226
227     /*
228      * Scale down the value into the range 1 .. 2.
229      *
230      * If fractional numbers need to be processed, another loop needs
231      * to go here that checks v < scale and if so multiplies it by 2 and
232      * reduces r by scale.  This also means making r signed.
233      */
234     while (v >= 2 * scale) {
235         v >>= 1;
236         r += scale;
237     }
238     for (i = scale / 2; i != 0; i /= 2) {
239         v = mul2(v, v);
240         if (v >= 2 * scale) {
241             v >>= 1;
242             r += i;
243         }
244     }
245     r = (r * (uint64_t)scale) / log_e;
246     return r;
247 }
248
249 /*
250  * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
251  * Modulus Lengths.
252  *
253  * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
254  *           \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
255  * The two cube roots are merged together here.
256  */
257 uint16_t rsa_compute_security_bits(int n)
258 {
259     uint64_t x;
260     uint32_t lx;
261     uint16_t y;
262
263     /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
264     switch (n) {
265     case 2048:
266         return 112;
267     case 3072:
268         return 128;
269     case 4096:
270         return 152;
271     case 6144:
272         return 176;
273     case 8192:
274         return 200;
275     }
276     /*
277      * The first incorrect result (i.e. not accurate or off by one low) occurs
278      * for n = 699668.  The true value here is 1200.  Instead of using this n
279      * as the check threshold, the smallest n such that the correct result is
280      * 1200 is used instead.
281      */
282     if (n >= 687737)
283         return 1200;
284     if (n < 8)
285         return 0;
286
287     x = n * (uint64_t)log_2;
288     lx = ilog_e(x);
289     y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
290                    / log_2);
291     return (y + 4) & ~7;
292 }
293
294 int RSA_security_bits(const RSA *rsa)
295 {
296     int bits = BN_num_bits(rsa->n);
297
298     if (rsa->version == RSA_ASN1_VERSION_MULTI) {
299         /* This ought to mean that we have private key at hand. */
300         int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
301
302         if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
303             return 0;
304     }
305     return rsa_compute_security_bits(bits);
306 }
307
308 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
309 {
310     /* If the fields n and e in r are NULL, the corresponding input
311      * parameters MUST be non-NULL for n and e.  d may be
312      * left NULL (in case only the public key is used).
313      */
314     if ((r->n == NULL && n == NULL)
315         || (r->e == NULL && e == NULL))
316         return 0;
317
318     if (n != NULL) {
319         BN_free(r->n);
320         r->n = n;
321     }
322     if (e != NULL) {
323         BN_free(r->e);
324         r->e = e;
325     }
326     if (d != NULL) {
327         BN_clear_free(r->d);
328         r->d = d;
329         BN_set_flags(r->d, BN_FLG_CONSTTIME);
330     }
331     r->dirty_cnt++;
332
333     return 1;
334 }
335
336 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
337 {
338     /* If the fields p and q in r are NULL, the corresponding input
339      * parameters MUST be non-NULL.
340      */
341     if ((r->p == NULL && p == NULL)
342         || (r->q == NULL && q == NULL))
343         return 0;
344
345     if (p != NULL) {
346         BN_clear_free(r->p);
347         r->p = p;
348         BN_set_flags(r->p, BN_FLG_CONSTTIME);
349     }
350     if (q != NULL) {
351         BN_clear_free(r->q);
352         r->q = q;
353         BN_set_flags(r->q, BN_FLG_CONSTTIME);
354     }
355     r->dirty_cnt++;
356
357     return 1;
358 }
359
360 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
361 {
362     /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
363      * parameters MUST be non-NULL.
364      */
365     if ((r->dmp1 == NULL && dmp1 == NULL)
366         || (r->dmq1 == NULL && dmq1 == NULL)
367         || (r->iqmp == NULL && iqmp == NULL))
368         return 0;
369
370     if (dmp1 != NULL) {
371         BN_clear_free(r->dmp1);
372         r->dmp1 = dmp1;
373         BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
374     }
375     if (dmq1 != NULL) {
376         BN_clear_free(r->dmq1);
377         r->dmq1 = dmq1;
378         BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
379     }
380     if (iqmp != NULL) {
381         BN_clear_free(r->iqmp);
382         r->iqmp = iqmp;
383         BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
384     }
385     r->dirty_cnt++;
386
387     return 1;
388 }
389
390 /*
391  * Is it better to export RSA_PRIME_INFO structure
392  * and related functions to let user pass a triplet?
393  */
394 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
395                                 BIGNUM *coeffs[], int pnum)
396 {
397     STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
398     RSA_PRIME_INFO *pinfo;
399     int i;
400
401     if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
402         return 0;
403
404     prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
405     if (prime_infos == NULL)
406         return 0;
407
408     if (r->prime_infos != NULL)
409         old = r->prime_infos;
410
411     for (i = 0; i < pnum; i++) {
412         pinfo = rsa_multip_info_new();
413         if (pinfo == NULL)
414             goto err;
415         if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
416             BN_clear_free(pinfo->r);
417             BN_clear_free(pinfo->d);
418             BN_clear_free(pinfo->t);
419             pinfo->r = primes[i];
420             pinfo->d = exps[i];
421             pinfo->t = coeffs[i];
422             BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
423             BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
424             BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
425         } else {
426             rsa_multip_info_free(pinfo);
427             goto err;
428         }
429         (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
430     }
431
432     r->prime_infos = prime_infos;
433
434     if (!rsa_multip_calc_product(r)) {
435         r->prime_infos = old;
436         goto err;
437     }
438
439     if (old != NULL) {
440         /*
441          * This is hard to deal with, since the old infos could
442          * also be set by this function and r, d, t should not
443          * be freed in that case. So currently, stay consistent
444          * with other *set0* functions: just free it...
445          */
446         sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
447     }
448
449     r->version = RSA_ASN1_VERSION_MULTI;
450     r->dirty_cnt++;
451
452     return 1;
453  err:
454     /* r, d, t should not be freed */
455     sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
456     return 0;
457 }
458
459 void RSA_get0_key(const RSA *r,
460                   const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
461 {
462     if (n != NULL)
463         *n = r->n;
464     if (e != NULL)
465         *e = r->e;
466     if (d != NULL)
467         *d = r->d;
468 }
469
470 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
471 {
472     if (p != NULL)
473         *p = r->p;
474     if (q != NULL)
475         *q = r->q;
476 }
477
478 int RSA_get_multi_prime_extra_count(const RSA *r)
479 {
480     int pnum;
481
482     pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
483     if (pnum <= 0)
484         pnum = 0;
485     return pnum;
486 }
487
488 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
489 {
490     int pnum, i;
491     RSA_PRIME_INFO *pinfo;
492
493     if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
494         return 0;
495
496     /*
497      * return other primes
498      * it's caller's responsibility to allocate oth_primes[pnum]
499      */
500     for (i = 0; i < pnum; i++) {
501         pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
502         primes[i] = pinfo->r;
503     }
504
505     return 1;
506 }
507
508 void RSA_get0_crt_params(const RSA *r,
509                          const BIGNUM **dmp1, const BIGNUM **dmq1,
510                          const BIGNUM **iqmp)
511 {
512     if (dmp1 != NULL)
513         *dmp1 = r->dmp1;
514     if (dmq1 != NULL)
515         *dmq1 = r->dmq1;
516     if (iqmp != NULL)
517         *iqmp = r->iqmp;
518 }
519
520 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
521                                     const BIGNUM *coeffs[])
522 {
523     int pnum;
524
525     if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
526         return 0;
527
528     /* return other primes */
529     if (exps != NULL || coeffs != NULL) {
530         RSA_PRIME_INFO *pinfo;
531         int i;
532
533         /* it's the user's job to guarantee the buffer length */
534         for (i = 0; i < pnum; i++) {
535             pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
536             if (exps != NULL)
537                 exps[i] = pinfo->d;
538             if (coeffs != NULL)
539                 coeffs[i] = pinfo->t;
540         }
541     }
542
543     return 1;
544 }
545
546 const BIGNUM *RSA_get0_n(const RSA *r)
547 {
548     return r->n;
549 }
550
551 const BIGNUM *RSA_get0_e(const RSA *r)
552 {
553     return r->e;
554 }
555
556 const BIGNUM *RSA_get0_d(const RSA *r)
557 {
558     return r->d;
559 }
560
561 const BIGNUM *RSA_get0_p(const RSA *r)
562 {
563     return r->p;
564 }
565
566 const BIGNUM *RSA_get0_q(const RSA *r)
567 {
568     return r->q;
569 }
570
571 const BIGNUM *RSA_get0_dmp1(const RSA *r)
572 {
573     return r->dmp1;
574 }
575
576 const BIGNUM *RSA_get0_dmq1(const RSA *r)
577 {
578     return r->dmq1;
579 }
580
581 const BIGNUM *RSA_get0_iqmp(const RSA *r)
582 {
583     return r->iqmp;
584 }
585
586 void RSA_clear_flags(RSA *r, int flags)
587 {
588     r->flags &= ~flags;
589 }
590
591 int RSA_test_flags(const RSA *r, int flags)
592 {
593     return r->flags & flags;
594 }
595
596 void RSA_set_flags(RSA *r, int flags)
597 {
598     r->flags |= flags;
599 }
600
601 int RSA_get_version(RSA *r)
602 {
603     /* { two-prime(0), multi(1) } */
604     return r->version;
605 }
606
607 ENGINE *RSA_get0_engine(const RSA *r)
608 {
609     return r->engine;
610 }
611
612 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
613 {
614     /* If key type not RSA or RSA-PSS return error */
615     if (ctx != NULL && ctx->pmeth != NULL
616         && ctx->pmeth->pkey_id != EVP_PKEY_RSA
617         && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
618         return -1;
619      return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
620 }
621
622 DEFINE_STACK_OF(BIGNUM)
623
624 int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
625                         const STACK_OF(BIGNUM) *exps,
626                         const STACK_OF(BIGNUM) *coeffs)
627 {
628     STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
629     int pnum;
630
631     if (primes == NULL || exps == NULL || coeffs == NULL)
632         return 0;
633
634     pnum = sk_BIGNUM_num(primes);
635     if (pnum < 2
636         || pnum != sk_BIGNUM_num(exps)
637         || pnum != sk_BIGNUM_num(coeffs) + 1)
638         return 0;
639
640     if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
641                           sk_BIGNUM_value(primes, 1))
642         || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
643                                 sk_BIGNUM_value(exps, 1),
644                                 sk_BIGNUM_value(coeffs, 0)))
645         return 0;
646
647     old_infos = r->prime_infos;
648
649     if (pnum > 2) {
650         int i;
651
652         prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
653         if (prime_infos == NULL)
654             return 0;
655
656         for (i = 2; i < pnum; i++) {
657             BIGNUM *prime = sk_BIGNUM_value(primes, i);
658             BIGNUM *exp = sk_BIGNUM_value(exps, i);
659             BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
660             RSA_PRIME_INFO *pinfo = NULL;
661
662             if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
663                 goto err;
664
665             /* Using rsa_multip_info_new() is wasteful, so allocate directly */
666             if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
667                 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
668                 goto err;
669             }
670
671             pinfo->r = prime;
672             pinfo->d = exp;
673             pinfo->t = coeff;
674             BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
675             BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
676             BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
677             (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
678         }
679
680         r->prime_infos = prime_infos;
681
682         if (!rsa_multip_calc_product(r)) {
683             r->prime_infos = old_infos;
684             goto err;
685         }
686     }
687
688     if (old_infos != NULL) {
689         /*
690          * This is hard to deal with, since the old infos could
691          * also be set by this function and r, d, t should not
692          * be freed in that case. So currently, stay consistent
693          * with other *set0* functions: just free it...
694          */
695         sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
696     }
697
698     r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
699     r->dirty_cnt++;
700
701     return 1;
702  err:
703     /* r, d, t should not be freed */
704     sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
705     return 0;
706 }
707
708 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
709
710 int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
711                         STACK_OF(BIGNUM_const) *exps,
712                         STACK_OF(BIGNUM_const) *coeffs)
713 {
714     RSA_PRIME_INFO *pinfo;
715     int i, pnum;
716
717     if (r == NULL)
718         return 0;
719
720     pnum = RSA_get_multi_prime_extra_count(r);
721
722     sk_BIGNUM_const_push(primes, RSA_get0_p(r));
723     sk_BIGNUM_const_push(primes, RSA_get0_q(r));
724     sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
725     sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
726     sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
727     for (i = 0; i < pnum; i++) {
728         pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
729         sk_BIGNUM_const_push(primes, pinfo->r);
730         sk_BIGNUM_const_push(exps, pinfo->d);
731         sk_BIGNUM_const_push(coeffs, pinfo->t);
732     }
733
734     return 1;
735 }