2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
11 #include <openssl/bio.h>
12 #include <openssl/engine.h>
15 #if OPENSSL_VERSION_NUMBER < 0x10100000L
18 OPENSSL_zalloc(size_t num
)
20 void *ret
= OPENSSL_malloc(num
);
23 (void) memset(ret
, 0, num
);
28 RSA_set0_key(RSA
*r
, BIGNUM
*n
, BIGNUM
*e
, BIGNUM
*d
)
31 * If the fields n and e in r are NULL, the corresponding input
32 * parameters MUST be non-NULL for n and e. d may be
33 * left NULL (in case only the public key is used).
35 if ((r
->n
== NULL
&& n
== NULL
) || (r
->e
== NULL
&& e
== NULL
))
55 RSA_set0_factors(RSA
*r
, BIGNUM
*p
, BIGNUM
*q
)
58 * If the fields p and q in r are NULL, the corresponding input
59 * parameters MUST be non-NULL.
61 if ((r
->p
== NULL
&& p
== NULL
) || (r
->q
== NULL
&& q
== NULL
))
77 RSA_set0_crt_params(RSA
*r
, BIGNUM
*dmp1
, BIGNUM
*dmq1
, BIGNUM
*iqmp
)
80 * If the fields dmp1, dmq1 and iqmp in r are NULL, the
81 * corresponding input parameters MUST be non-NULL.
83 if ((r
->dmp1
== NULL
&& dmp1
== NULL
) ||
84 (r
->dmq1
== NULL
&& dmq1
== NULL
) ||
85 (r
->iqmp
== NULL
&& iqmp
== NULL
))
105 RSA_get0_key(const RSA
*r
, const BIGNUM
**n
, const BIGNUM
**e
, const BIGNUM
**d
)
116 RSA_get0_factors(const RSA
*r
, const BIGNUM
**p
, const BIGNUM
**q
)
125 RSA_get0_crt_params(const RSA
*r
, const BIGNUM
**dmp1
, const BIGNUM
**dmq1
,
137 DSA_get0_pqg(const DSA
*d
, const BIGNUM
**p
, const BIGNUM
**q
,
149 DSA_set0_pqg(DSA
*d
, BIGNUM
*p
, BIGNUM
*q
, BIGNUM
*g
)
152 * If the fields p, q and g in d are NULL, the corresponding input
153 * parameters MUST be non-NULL.
155 if ((d
->p
== NULL
&& p
== NULL
) || (d
->q
== NULL
&& q
== NULL
) ||
156 (d
->g
== NULL
&& g
== NULL
))
176 DSA_get0_key(const DSA
*d
, const BIGNUM
**pub_key
, const BIGNUM
**priv_key
)
179 *pub_key
= d
->pub_key
;
180 if (priv_key
!= NULL
)
181 *priv_key
= d
->priv_key
;
185 DSA_set0_key(DSA
*d
, BIGNUM
*pub_key
, BIGNUM
*priv_key
)
188 * If the field pub_key in d is NULL, the corresponding input
189 * parameters MUST be non-NULL. The priv_key field may
192 if (d
->pub_key
== NULL
&& pub_key
== NULL
)
195 if (pub_key
!= NULL
) {
197 d
->pub_key
= pub_key
;
199 if (priv_key
!= NULL
) {
200 BN_free(d
->priv_key
);
201 d
->priv_key
= priv_key
;
208 DSA_SIG_get0(const DSA_SIG
*sig
, const BIGNUM
**pr
, const BIGNUM
**ps
)
217 DSA_SIG_set0(DSA_SIG
*sig
, BIGNUM
*r
, BIGNUM
*s
)
219 if (r
== NULL
|| s
== NULL
)
221 BN_clear_free(sig
->r
);
222 BN_clear_free(sig
->s
);
229 EVP_PKEY_get0_DSA(EVP_PKEY
*pkey
)
231 if (pkey
->type
!= EVP_PKEY_DSA
)
233 return (pkey
->pkey
.dsa
);
237 ECDSA_SIG_get0(const ECDSA_SIG
*sig
, const BIGNUM
**pr
, const BIGNUM
**ps
)
246 ECDSA_SIG_set0(ECDSA_SIG
*sig
, BIGNUM
*r
, BIGNUM
*s
)
248 if (r
== NULL
|| s
== NULL
)
250 BN_clear_free(sig
->r
);
251 BN_clear_free(sig
->s
);
258 DH_get0_pqg(const DH
*dh
, const BIGNUM
**p
, const BIGNUM
**q
, const BIGNUM
**g
)
269 DH_set0_pqg(DH
*dh
, BIGNUM
*p
, BIGNUM
*q
, BIGNUM
*g
)
272 * If the fields p and g in d are NULL, the corresponding input
273 * parameters MUST be non-NULL. q may remain NULL.
275 if ((dh
->p
== NULL
&& p
== NULL
) || (dh
->g
== NULL
&& g
== NULL
))
292 dh
->length
= BN_num_bits(q
);
299 DH_get0_key(const DH
*dh
, const BIGNUM
**pub_key
, const BIGNUM
**priv_key
)
302 *pub_key
= dh
->pub_key
;
303 if (priv_key
!= NULL
)
304 *priv_key
= dh
->priv_key
;
308 DH_set0_key(DH
*dh
, BIGNUM
*pub_key
, BIGNUM
*priv_key
)
311 * If the field pub_key in dh is NULL, the corresponding input
312 * parameters MUST be non-NULL. The priv_key field may
315 if (dh
->pub_key
== NULL
&& pub_key
== NULL
)
318 if (pub_key
!= NULL
) {
319 BN_free(dh
->pub_key
);
320 dh
->pub_key
= pub_key
;
322 if (priv_key
!= NULL
) {
323 BN_free(dh
->priv_key
);
324 dh
->priv_key
= priv_key
;
331 DH_set_length(DH
*dh
, long length
)
337 const unsigned char *
338 EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX
*ctx
)
344 EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX
*ctx
)
352 return (OPENSSL_zalloc(sizeof (EVP_MD_CTX
)));
356 EVP_MD_CTX_free(EVP_MD_CTX
*ctx
)
358 (void) EVP_MD_CTX_cleanup(ctx
);
363 RSA_meth_dup(const RSA_METHOD
*meth
)
367 ret
= OPENSSL_malloc(sizeof (RSA_METHOD
));
370 (void) memcpy(ret
, meth
, sizeof (*meth
));
371 ret
->name
= OPENSSL_strdup(meth
->name
);
372 if (ret
->name
== NULL
) {
382 RSA_meth_set1_name(RSA_METHOD
*meth
, const char *name
)
386 tmpname
= OPENSSL_strdup(name
);
387 if (tmpname
== NULL
) {
391 OPENSSL_free((char *)meth
->name
);
392 meth
->name
= tmpname
;
398 RSA_meth_set_priv_enc(RSA_METHOD
*meth
,
399 int (*priv_enc
) (int flen
, const unsigned char *from
,
400 unsigned char *to
, RSA
*rsa
, int padding
))
402 meth
->rsa_priv_enc
= priv_enc
;
407 RSA_meth_set_priv_dec(RSA_METHOD
*meth
,
408 int (*priv_dec
) (int flen
, const unsigned char *from
,
409 unsigned char *to
, RSA
*rsa
, int padding
))
411 meth
->rsa_priv_dec
= priv_dec
;
416 RSA_meth_set_finish(RSA_METHOD
*meth
, int (*finish
) (RSA
*rsa
))
418 meth
->finish
= finish
;
423 RSA_meth_free(RSA_METHOD
*meth
)
426 OPENSSL_free((char *)meth
->name
);
432 RSA_bits(const RSA
*r
)
434 return (BN_num_bits(r
->n
));
438 EVP_PKEY_get0_RSA(EVP_PKEY
*pkey
)
440 if (pkey
->type
!= EVP_PKEY_RSA
) {
443 return (pkey
->pkey
.rsa
);
446 #endif /* OPENSSL_VERSION_NUMBER */