2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2018 RackTop Systems.
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
12 #include <openssl/bio.h>
13 #include <openssl/engine.h>
16 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
19 OPENSSL_zalloc(size_t num
)
21 void *ret
= OPENSSL_malloc(num
);
24 (void) memset(ret
, 0, num
);
29 RSA_set0_key(RSA
*r
, BIGNUM
*n
, BIGNUM
*e
, BIGNUM
*d
)
32 * If the fields n and e in r are NULL, the corresponding input
33 * parameters MUST be non-NULL for n and e. d may be
34 * left NULL (in case only the public key is used).
36 if ((r
->n
== NULL
&& n
== NULL
) || (r
->e
== NULL
&& e
== NULL
))
56 RSA_set0_factors(RSA
*r
, BIGNUM
*p
, BIGNUM
*q
)
59 * If the fields p and q in r are NULL, the corresponding input
60 * parameters MUST be non-NULL.
62 if ((r
->p
== NULL
&& p
== NULL
) || (r
->q
== NULL
&& q
== NULL
))
78 RSA_set0_crt_params(RSA
*r
, BIGNUM
*dmp1
, BIGNUM
*dmq1
, BIGNUM
*iqmp
)
81 * If the fields dmp1, dmq1 and iqmp in r are NULL, the
82 * corresponding input parameters MUST be non-NULL.
84 if ((r
->dmp1
== NULL
&& dmp1
== NULL
) ||
85 (r
->dmq1
== NULL
&& dmq1
== NULL
) ||
86 (r
->iqmp
== NULL
&& iqmp
== NULL
))
106 RSA_get0_key(const RSA
*r
, const BIGNUM
**n
, const BIGNUM
**e
, const BIGNUM
**d
)
117 RSA_get0_factors(const RSA
*r
, const BIGNUM
**p
, const BIGNUM
**q
)
126 RSA_get0_crt_params(const RSA
*r
, const BIGNUM
**dmp1
, const BIGNUM
**dmq1
,
138 DSA_get0_pqg(const DSA
*d
, const BIGNUM
**p
, const BIGNUM
**q
,
150 DSA_set0_pqg(DSA
*d
, BIGNUM
*p
, BIGNUM
*q
, BIGNUM
*g
)
153 * If the fields p, q and g in d are NULL, the corresponding input
154 * parameters MUST be non-NULL.
156 if ((d
->p
== NULL
&& p
== NULL
) || (d
->q
== NULL
&& q
== NULL
) ||
157 (d
->g
== NULL
&& g
== NULL
))
177 DSA_get0_key(const DSA
*d
, const BIGNUM
**pub_key
, const BIGNUM
**priv_key
)
180 *pub_key
= d
->pub_key
;
181 if (priv_key
!= NULL
)
182 *priv_key
= d
->priv_key
;
186 DSA_set0_key(DSA
*d
, BIGNUM
*pub_key
, BIGNUM
*priv_key
)
189 * If the field pub_key in d is NULL, the corresponding input
190 * parameters MUST be non-NULL. The priv_key field may
193 if (d
->pub_key
== NULL
&& pub_key
== NULL
)
196 if (pub_key
!= NULL
) {
198 d
->pub_key
= pub_key
;
200 if (priv_key
!= NULL
) {
201 BN_free(d
->priv_key
);
202 d
->priv_key
= priv_key
;
209 DSA_SIG_get0(const DSA_SIG
*sig
, const BIGNUM
**pr
, const BIGNUM
**ps
)
218 DSA_SIG_set0(DSA_SIG
*sig
, BIGNUM
*r
, BIGNUM
*s
)
220 if (r
== NULL
|| s
== NULL
)
222 BN_clear_free(sig
->r
);
223 BN_clear_free(sig
->s
);
230 EVP_PKEY_get0_DSA(EVP_PKEY
*pkey
)
232 if (pkey
->type
!= EVP_PKEY_DSA
)
234 return (pkey
->pkey
.dsa
);
238 ECDSA_SIG_get0(const ECDSA_SIG
*sig
, const BIGNUM
**pr
, const BIGNUM
**ps
)
247 ECDSA_SIG_set0(ECDSA_SIG
*sig
, BIGNUM
*r
, BIGNUM
*s
)
249 if (r
== NULL
|| s
== NULL
)
251 BN_clear_free(sig
->r
);
252 BN_clear_free(sig
->s
);
259 DH_get0_pqg(const DH
*dh
, const BIGNUM
**p
, const BIGNUM
**q
, const BIGNUM
**g
)
270 DH_set0_pqg(DH
*dh
, BIGNUM
*p
, BIGNUM
*q
, BIGNUM
*g
)
273 * If the fields p and g in d are NULL, the corresponding input
274 * parameters MUST be non-NULL. q may remain NULL.
276 if ((dh
->p
== NULL
&& p
== NULL
) || (dh
->g
== NULL
&& g
== NULL
))
293 dh
->length
= BN_num_bits(q
);
300 DH_get0_key(const DH
*dh
, const BIGNUM
**pub_key
, const BIGNUM
**priv_key
)
303 *pub_key
= dh
->pub_key
;
304 if (priv_key
!= NULL
)
305 *priv_key
= dh
->priv_key
;
309 DH_set0_key(DH
*dh
, BIGNUM
*pub_key
, BIGNUM
*priv_key
)
312 * If the field pub_key in dh is NULL, the corresponding input
313 * parameters MUST be non-NULL. The priv_key field may
316 if (dh
->pub_key
== NULL
&& pub_key
== NULL
)
319 if (pub_key
!= NULL
) {
320 BN_free(dh
->pub_key
);
321 dh
->pub_key
= pub_key
;
323 if (priv_key
!= NULL
) {
324 BN_free(dh
->priv_key
);
325 dh
->priv_key
= priv_key
;
332 DH_set_length(DH
*dh
, long length
)
338 const unsigned char *
339 EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX
*ctx
)
345 EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX
*ctx
)
353 return (OPENSSL_zalloc(sizeof (EVP_MD_CTX
)));
357 EVP_MD_CTX_free(EVP_MD_CTX
*ctx
)
359 (void) EVP_MD_CTX_cleanup(ctx
);
364 RSA_meth_dup(const RSA_METHOD
*meth
)
368 ret
= OPENSSL_malloc(sizeof (RSA_METHOD
));
371 (void) memcpy(ret
, meth
, sizeof (*meth
));
372 ret
->name
= OPENSSL_strdup(meth
->name
);
373 if (ret
->name
== NULL
) {
383 RSA_meth_set1_name(RSA_METHOD
*meth
, const char *name
)
387 tmpname
= OPENSSL_strdup(name
);
388 if (tmpname
== NULL
) {
392 OPENSSL_free((char *)meth
->name
);
393 meth
->name
= tmpname
;
399 RSA_meth_set_priv_enc(RSA_METHOD
*meth
,
400 int (*priv_enc
) (int flen
, const unsigned char *from
,
401 unsigned char *to
, RSA
*rsa
, int padding
))
403 meth
->rsa_priv_enc
= priv_enc
;
408 RSA_meth_set_priv_dec(RSA_METHOD
*meth
,
409 int (*priv_dec
) (int flen
, const unsigned char *from
,
410 unsigned char *to
, RSA
*rsa
, int padding
))
412 meth
->rsa_priv_dec
= priv_dec
;
417 RSA_meth_set_finish(RSA_METHOD
*meth
, int (*finish
) (RSA
*rsa
))
419 meth
->finish
= finish
;
424 RSA_meth_free(RSA_METHOD
*meth
)
427 OPENSSL_free((char *)meth
->name
);
433 RSA_bits(const RSA
*r
)
435 return (BN_num_bits(r
->n
));
439 EVP_PKEY_get0_RSA(EVP_PKEY
*pkey
)
441 if (pkey
->type
!= EVP_PKEY_RSA
) {
444 return (pkey
->pkey
.rsa
);
447 #endif /* OPENSSL_VERSION_NUMBER || LIBRESSL_VERSION_NUMBER */