Added internal functions for easy getting and setting all RSA parameters.
[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
332     return 1;
333 }
334
335 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
336 {
337     /* If the fields p and q in r are NULL, the corresponding input
338      * parameters MUST be non-NULL.
339      */
340     if ((r->p == NULL && p == NULL)
341         || (r->q == NULL && q == NULL))
342         return 0;
343
344     if (p != NULL) {
345         BN_clear_free(r->p);
346         r->p = p;
347         BN_set_flags(r->p, BN_FLG_CONSTTIME);
348     }
349     if (q != NULL) {
350         BN_clear_free(r->q);
351         r->q = q;
352         BN_set_flags(r->q, BN_FLG_CONSTTIME);
353     }
354
355     return 1;
356 }
357
358 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
359 {
360     /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
361      * parameters MUST be non-NULL.
362      */
363     if ((r->dmp1 == NULL && dmp1 == NULL)
364         || (r->dmq1 == NULL && dmq1 == NULL)
365         || (r->iqmp == NULL && iqmp == NULL))
366         return 0;
367
368     if (dmp1 != NULL) {
369         BN_clear_free(r->dmp1);
370         r->dmp1 = dmp1;
371         BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
372     }
373     if (dmq1 != NULL) {
374         BN_clear_free(r->dmq1);
375         r->dmq1 = dmq1;
376         BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
377     }
378     if (iqmp != NULL) {
379         BN_clear_free(r->iqmp);
380         r->iqmp = iqmp;
381         BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
382     }
383
384     return 1;
385 }
386
387 /*
388  * Is it better to export RSA_PRIME_INFO structure
389  * and related functions to let user pass a triplet?
390  */
391 int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
392                                 BIGNUM *coeffs[], int pnum)
393 {
394     STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
395     RSA_PRIME_INFO *pinfo;
396     int i;
397
398     if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
399         return 0;
400
401     prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
402     if (prime_infos == NULL)
403         return 0;
404
405     if (r->prime_infos != NULL)
406         old = r->prime_infos;
407
408     for (i = 0; i < pnum; i++) {
409         pinfo = rsa_multip_info_new();
410         if (pinfo == NULL)
411             goto err;
412         if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
413             BN_clear_free(pinfo->r);
414             BN_clear_free(pinfo->d);
415             BN_clear_free(pinfo->t);
416             pinfo->r = primes[i];
417             pinfo->d = exps[i];
418             pinfo->t = coeffs[i];
419             BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
420             BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
421             BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
422         } else {
423             rsa_multip_info_free(pinfo);
424             goto err;
425         }
426         (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
427     }
428
429     r->prime_infos = prime_infos;
430
431     if (!rsa_multip_calc_product(r)) {
432         r->prime_infos = old;
433         goto err;
434     }
435
436     if (old != NULL) {
437         /*
438          * This is hard to deal with, since the old infos could
439          * also be set by this function and r, d, t should not
440          * be freed in that case. So currently, stay consistent
441          * with other *set0* functions: just free it...
442          */
443         sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
444     }
445
446     r->version = RSA_ASN1_VERSION_MULTI;
447
448     return 1;
449  err:
450     /* r, d, t should not be freed */
451     sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
452     return 0;
453 }
454
455 void RSA_get0_key(const RSA *r,
456                   const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
457 {
458     if (n != NULL)
459         *n = r->n;
460     if (e != NULL)
461         *e = r->e;
462     if (d != NULL)
463         *d = r->d;
464 }
465
466 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
467 {
468     if (p != NULL)
469         *p = r->p;
470     if (q != NULL)
471         *q = r->q;
472 }
473
474 int RSA_get_multi_prime_extra_count(const RSA *r)
475 {
476     int pnum;
477
478     pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
479     if (pnum <= 0)
480         pnum = 0;
481     return pnum;
482 }
483
484 int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
485 {
486     int pnum, i;
487     RSA_PRIME_INFO *pinfo;
488
489     if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
490         return 0;
491
492     /*
493      * return other primes
494      * it's caller's responsibility to allocate oth_primes[pnum]
495      */
496     for (i = 0; i < pnum; i++) {
497         pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
498         primes[i] = pinfo->r;
499     }
500
501     return 1;
502 }
503
504 void RSA_get0_crt_params(const RSA *r,
505                          const BIGNUM **dmp1, const BIGNUM **dmq1,
506                          const BIGNUM **iqmp)
507 {
508     if (dmp1 != NULL)
509         *dmp1 = r->dmp1;
510     if (dmq1 != NULL)
511         *dmq1 = r->dmq1;
512     if (iqmp != NULL)
513         *iqmp = r->iqmp;
514 }
515
516 int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
517                                     const BIGNUM *coeffs[])
518 {
519     int pnum;
520
521     if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
522         return 0;
523
524     /* return other primes */
525     if (exps != NULL || coeffs != NULL) {
526         RSA_PRIME_INFO *pinfo;
527         int i;
528
529         /* it's the user's job to guarantee the buffer length */
530         for (i = 0; i < pnum; i++) {
531             pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
532             if (exps != NULL)
533                 exps[i] = pinfo->d;
534             if (coeffs != NULL)
535                 coeffs[i] = pinfo->t;
536         }
537     }
538
539     return 1;
540 }
541
542 const BIGNUM *RSA_get0_n(const RSA *r)
543 {
544     return r->n;
545 }
546
547 const BIGNUM *RSA_get0_e(const RSA *r)
548 {
549     return r->e;
550 }
551
552 const BIGNUM *RSA_get0_d(const RSA *r)
553 {
554     return r->d;
555 }
556
557 const BIGNUM *RSA_get0_p(const RSA *r)
558 {
559     return r->p;
560 }
561
562 const BIGNUM *RSA_get0_q(const RSA *r)
563 {
564     return r->q;
565 }
566
567 const BIGNUM *RSA_get0_dmp1(const RSA *r)
568 {
569     return r->dmp1;
570 }
571
572 const BIGNUM *RSA_get0_dmq1(const RSA *r)
573 {
574     return r->dmq1;
575 }
576
577 const BIGNUM *RSA_get0_iqmp(const RSA *r)
578 {
579     return r->iqmp;
580 }
581
582 void RSA_clear_flags(RSA *r, int flags)
583 {
584     r->flags &= ~flags;
585 }
586
587 int RSA_test_flags(const RSA *r, int flags)
588 {
589     return r->flags & flags;
590 }
591
592 void RSA_set_flags(RSA *r, int flags)
593 {
594     r->flags |= flags;
595 }
596
597 int RSA_get_version(RSA *r)
598 {
599     /* { two-prime(0), multi(1) } */
600     return r->version;
601 }
602
603 ENGINE *RSA_get0_engine(const RSA *r)
604 {
605     return r->engine;
606 }
607
608 int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
609 {
610     /* If key type not RSA or RSA-PSS return error */
611     if (ctx != NULL && ctx->pmeth != NULL
612         && ctx->pmeth->pkey_id != EVP_PKEY_RSA
613         && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
614         return -1;
615      return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
616 }
617
618 DEFINE_STACK_OF(BIGNUM)
619
620 int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
621                         const STACK_OF(BIGNUM) *exps,
622                         const STACK_OF(BIGNUM) *coeffs)
623 {
624     STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
625     int pnum;
626
627     if (primes == NULL || exps == NULL || coeffs == NULL)
628         return 0;
629
630     pnum = sk_BIGNUM_num(primes);
631     if (pnum < 2
632         || pnum != sk_BIGNUM_num(exps)
633         || pnum != sk_BIGNUM_num(coeffs) + 1)
634         return 0;
635
636     if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
637                           sk_BIGNUM_value(primes, 1))
638         || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
639                                 sk_BIGNUM_value(exps, 1),
640                                 sk_BIGNUM_value(coeffs, 0)))
641         return 0;
642
643     old_infos = r->prime_infos;
644
645     if (pnum > 2) {
646         int i;
647
648         prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
649         if (prime_infos == NULL)
650             return 0;
651
652         for (i = 2; i < pnum; i++) {
653             BIGNUM *prime = sk_BIGNUM_value(primes, i);
654             BIGNUM *exp = sk_BIGNUM_value(exps, i);
655             BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
656             RSA_PRIME_INFO *pinfo = NULL;
657
658             if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
659                 goto err;
660
661             /* Using rsa_multip_info_new() is wasteful, so allocate directly */
662             if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
663                 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
664                 goto err;
665             }
666
667             pinfo->r = prime;
668             pinfo->d = exp;
669             pinfo->t = coeff;
670             BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
671             BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
672             BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
673             (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
674         }
675
676         r->prime_infos = prime_infos;
677
678         if (!rsa_multip_calc_product(r)) {
679             r->prime_infos = old_infos;
680             goto err;
681         }
682     }
683
684     if (old_infos != NULL) {
685         /*
686          * This is hard to deal with, since the old infos could
687          * also be set by this function and r, d, t should not
688          * be freed in that case. So currently, stay consistent
689          * with other *set0* functions: just free it...
690          */
691         sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
692     }
693
694     r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
695     r->dirty_cnt++;
696
697     return 1;
698  err:
699     /* r, d, t should not be freed */
700     sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
701     return 0;
702 }
703
704 DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
705
706 int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
707                         STACK_OF(BIGNUM_const) *exps,
708                         STACK_OF(BIGNUM_const) *coeffs)
709 {
710     RSA_PRIME_INFO *pinfo;
711     int i, pnum;
712
713     if (r == NULL)
714         return 0;
715
716     pnum = RSA_get_multi_prime_extra_count(r);
717
718     sk_BIGNUM_const_push(primes, RSA_get0_p(r));
719     sk_BIGNUM_const_push(primes, RSA_get0_q(r));
720     sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
721     sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
722     sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
723     for (i = 0; i < pnum; i++) {
724         pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
725         sk_BIGNUM_const_push(primes, pinfo->r);
726         sk_BIGNUM_const_push(exps, pinfo->d);
727         sk_BIGNUM_const_push(coeffs, pinfo->t);
728     }
729
730     return 1;
731 }