6429 SMB domain join doesn't work with libreSSL
[unleashed.git] / usr / src / lib / libkmf / plugins / kmf_openssl / common / compat.c
blob0a17b32a6ae48de90a6966e3b9d272a64bb35fd2
1 /*
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
9 */
11 #include <string.h>
12 #include <openssl/bio.h>
13 #include <openssl/engine.h>
14 #include "compat.h"
16 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
18 static void *
19 OPENSSL_zalloc(size_t num)
21 void *ret = OPENSSL_malloc(num);
23 if (ret != NULL)
24 (void) memset(ret, 0, num);
25 return (ret);
28 int
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))
37 return (0);
39 if (n != NULL) {
40 BN_free(r->n);
41 r->n = n;
43 if (e != NULL) {
44 BN_free(r->e);
45 r->e = e;
47 if (d != NULL) {
48 BN_free(r->d);
49 r->d = d;
52 return (1);
55 int
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))
63 return (0);
65 if (p != NULL) {
66 BN_free(r->p);
67 r->p = p;
69 if (q != NULL) {
70 BN_free(r->q);
71 r->q = q;
74 return (1);
77 int
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))
87 return (0);
89 if (dmp1 != NULL) {
90 BN_free(r->dmp1);
91 r->dmp1 = dmp1;
93 if (dmq1 != NULL) {
94 BN_free(r->dmq1);
95 r->dmq1 = dmq1;
97 if (iqmp != NULL) {
98 BN_free(r->iqmp);
99 r->iqmp = iqmp;
102 return (1);
105 void
106 RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
108 if (n != NULL)
109 *n = r->n;
110 if (e != NULL)
111 *e = r->e;
112 if (d != NULL)
113 *d = r->d;
116 void
117 RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
119 if (p != NULL)
120 *p = r->p;
121 if (q != NULL)
122 *q = r->q;
125 void
126 RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
127 const BIGNUM **iqmp)
129 if (dmp1 != NULL)
130 *dmp1 = r->dmp1;
131 if (dmq1 != NULL)
132 *dmq1 = r->dmq1;
133 if (iqmp != NULL)
134 *iqmp = r->iqmp;
137 void
138 DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q,
139 const BIGNUM **g)
141 if (p != NULL)
142 *p = d->p;
143 if (q != NULL)
144 *q = d->q;
145 if (g != NULL)
146 *g = d->g;
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))
158 return (0);
160 if (p != NULL) {
161 BN_free(d->p);
162 d->p = p;
164 if (q != NULL) {
165 BN_free(d->q);
166 d->q = q;
168 if (g != NULL) {
169 BN_free(d->g);
170 d->g = g;
173 return (1);
176 void
177 DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
179 if (pub_key != NULL)
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
191 * be left NULL.
193 if (d->pub_key == NULL && pub_key == NULL)
194 return (0);
196 if (pub_key != NULL) {
197 BN_free(d->pub_key);
198 d->pub_key = pub_key;
200 if (priv_key != NULL) {
201 BN_free(d->priv_key);
202 d->priv_key = priv_key;
205 return (1);
208 void
209 DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
211 if (pr != NULL)
212 *pr = sig->r;
213 if (ps != NULL)
214 *ps = sig->s;
218 DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
220 if (r == NULL || s == NULL)
221 return (0);
222 BN_clear_free(sig->r);
223 BN_clear_free(sig->s);
224 sig->r = r;
225 sig->s = s;
226 return (1);
229 DSA *
230 EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
232 if (pkey->type != EVP_PKEY_DSA)
233 return (NULL);
234 return (pkey->pkey.dsa);
237 void
238 ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
240 if (pr != NULL)
241 *pr = sig->r;
242 if (ps != NULL)
243 *ps = sig->s;
247 ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
249 if (r == NULL || s == NULL)
250 return (0);
251 BN_clear_free(sig->r);
252 BN_clear_free(sig->s);
253 sig->r = r;
254 sig->s = s;
255 return (1);
258 void
259 DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
261 if (p != NULL)
262 *p = dh->p;
263 if (q != NULL)
264 *q = dh->q;
265 if (g != NULL)
266 *g = dh->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))
277 return (0);
279 if (p != NULL) {
280 BN_free(dh->p);
281 dh->p = p;
283 if (q != NULL) {
284 BN_free(dh->q);
285 dh->q = q;
287 if (g != NULL) {
288 BN_free(dh->g);
289 dh->g = g;
292 if (q != NULL) {
293 dh->length = BN_num_bits(q);
296 return (1);
299 void
300 DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
302 if (pub_key != NULL)
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
314 * be left NULL.
316 if (dh->pub_key == NULL && pub_key == NULL)
317 return (0);
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;
328 return (1);
332 DH_set_length(DH *dh, long length)
334 dh->length = length;
335 return (1);
338 const unsigned char *
339 EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
341 return (ctx->iv);
344 unsigned char *
345 EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
347 return (ctx->iv);
350 EVP_MD_CTX *
351 EVP_MD_CTX_new(void)
353 return (OPENSSL_zalloc(sizeof (EVP_MD_CTX)));
356 void
357 EVP_MD_CTX_free(EVP_MD_CTX *ctx)
359 (void) EVP_MD_CTX_cleanup(ctx);
360 OPENSSL_free(ctx);
363 RSA_METHOD *
364 RSA_meth_dup(const RSA_METHOD *meth)
366 RSA_METHOD *ret;
368 ret = OPENSSL_malloc(sizeof (RSA_METHOD));
370 if (ret != NULL) {
371 (void) memcpy(ret, meth, sizeof (*meth));
372 ret->name = OPENSSL_strdup(meth->name);
373 if (ret->name == NULL) {
374 OPENSSL_free(ret);
375 return (NULL);
379 return (ret);
383 RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
385 char *tmpname;
387 tmpname = OPENSSL_strdup(name);
388 if (tmpname == NULL) {
389 return (0);
392 OPENSSL_free((char *)meth->name);
393 meth->name = tmpname;
395 return (1);
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;
404 return (1);
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;
413 return (1);
417 RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa))
419 meth->finish = finish;
420 return (1);
423 void
424 RSA_meth_free(RSA_METHOD *meth)
426 if (meth != NULL) {
427 OPENSSL_free((char *)meth->name);
428 OPENSSL_free(meth);
433 RSA_bits(const RSA *r)
435 return (BN_num_bits(r->n));
438 RSA *
439 EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
441 if (pkey->type != EVP_PKEY_RSA) {
442 return (NULL);
444 return (pkey->pkey.rsa);
447 #endif /* OPENSSL_VERSION_NUMBER || LIBRESSL_VERSION_NUMBER */