8982 Support building with OpenSSL 1.1
[unleashed.git] / usr / src / lib / libkmf / plugins / kmf_openssl / common / compat.c
blobb64064cb7b6e2597f5a3c5242af281cf7d145983
1 /*
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
8 */
10 #include <string.h>
11 #include <openssl/bio.h>
12 #include <openssl/engine.h>
13 #include "compat.h"
15 #if OPENSSL_VERSION_NUMBER < 0x10100000L
17 static void *
18 OPENSSL_zalloc(size_t num)
20 void *ret = OPENSSL_malloc(num);
22 if (ret != NULL)
23 (void) memset(ret, 0, num);
24 return (ret);
27 int
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))
36 return (0);
38 if (n != NULL) {
39 BN_free(r->n);
40 r->n = n;
42 if (e != NULL) {
43 BN_free(r->e);
44 r->e = e;
46 if (d != NULL) {
47 BN_free(r->d);
48 r->d = d;
51 return (1);
54 int
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))
62 return (0);
64 if (p != NULL) {
65 BN_free(r->p);
66 r->p = p;
68 if (q != NULL) {
69 BN_free(r->q);
70 r->q = q;
73 return (1);
76 int
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))
86 return (0);
88 if (dmp1 != NULL) {
89 BN_free(r->dmp1);
90 r->dmp1 = dmp1;
92 if (dmq1 != NULL) {
93 BN_free(r->dmq1);
94 r->dmq1 = dmq1;
96 if (iqmp != NULL) {
97 BN_free(r->iqmp);
98 r->iqmp = iqmp;
101 return (1);
104 void
105 RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
107 if (n != NULL)
108 *n = r->n;
109 if (e != NULL)
110 *e = r->e;
111 if (d != NULL)
112 *d = r->d;
115 void
116 RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
118 if (p != NULL)
119 *p = r->p;
120 if (q != NULL)
121 *q = r->q;
124 void
125 RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
126 const BIGNUM **iqmp)
128 if (dmp1 != NULL)
129 *dmp1 = r->dmp1;
130 if (dmq1 != NULL)
131 *dmq1 = r->dmq1;
132 if (iqmp != NULL)
133 *iqmp = r->iqmp;
136 void
137 DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q,
138 const BIGNUM **g)
140 if (p != NULL)
141 *p = d->p;
142 if (q != NULL)
143 *q = d->q;
144 if (g != NULL)
145 *g = d->g;
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))
157 return (0);
159 if (p != NULL) {
160 BN_free(d->p);
161 d->p = p;
163 if (q != NULL) {
164 BN_free(d->q);
165 d->q = q;
167 if (g != NULL) {
168 BN_free(d->g);
169 d->g = g;
172 return (1);
175 void
176 DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
178 if (pub_key != NULL)
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
190 * be left NULL.
192 if (d->pub_key == NULL && pub_key == NULL)
193 return (0);
195 if (pub_key != NULL) {
196 BN_free(d->pub_key);
197 d->pub_key = pub_key;
199 if (priv_key != NULL) {
200 BN_free(d->priv_key);
201 d->priv_key = priv_key;
204 return (1);
207 void
208 DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
210 if (pr != NULL)
211 *pr = sig->r;
212 if (ps != NULL)
213 *ps = sig->s;
217 DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
219 if (r == NULL || s == NULL)
220 return (0);
221 BN_clear_free(sig->r);
222 BN_clear_free(sig->s);
223 sig->r = r;
224 sig->s = s;
225 return (1);
228 DSA *
229 EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
231 if (pkey->type != EVP_PKEY_DSA)
232 return (NULL);
233 return (pkey->pkey.dsa);
236 void
237 ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
239 if (pr != NULL)
240 *pr = sig->r;
241 if (ps != NULL)
242 *ps = sig->s;
246 ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
248 if (r == NULL || s == NULL)
249 return (0);
250 BN_clear_free(sig->r);
251 BN_clear_free(sig->s);
252 sig->r = r;
253 sig->s = s;
254 return (1);
257 void
258 DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
260 if (p != NULL)
261 *p = dh->p;
262 if (q != NULL)
263 *q = dh->q;
264 if (g != NULL)
265 *g = dh->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))
276 return (0);
278 if (p != NULL) {
279 BN_free(dh->p);
280 dh->p = p;
282 if (q != NULL) {
283 BN_free(dh->q);
284 dh->q = q;
286 if (g != NULL) {
287 BN_free(dh->g);
288 dh->g = g;
291 if (q != NULL) {
292 dh->length = BN_num_bits(q);
295 return (1);
298 void
299 DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
301 if (pub_key != NULL)
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
313 * be left NULL.
315 if (dh->pub_key == NULL && pub_key == NULL)
316 return (0);
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;
327 return (1);
331 DH_set_length(DH *dh, long length)
333 dh->length = length;
334 return (1);
337 const unsigned char *
338 EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
340 return (ctx->iv);
343 unsigned char *
344 EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
346 return (ctx->iv);
349 EVP_MD_CTX *
350 EVP_MD_CTX_new(void)
352 return (OPENSSL_zalloc(sizeof (EVP_MD_CTX)));
355 void
356 EVP_MD_CTX_free(EVP_MD_CTX *ctx)
358 (void) EVP_MD_CTX_cleanup(ctx);
359 OPENSSL_free(ctx);
362 RSA_METHOD *
363 RSA_meth_dup(const RSA_METHOD *meth)
365 RSA_METHOD *ret;
367 ret = OPENSSL_malloc(sizeof (RSA_METHOD));
369 if (ret != NULL) {
370 (void) memcpy(ret, meth, sizeof (*meth));
371 ret->name = OPENSSL_strdup(meth->name);
372 if (ret->name == NULL) {
373 OPENSSL_free(ret);
374 return (NULL);
378 return (ret);
382 RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
384 char *tmpname;
386 tmpname = OPENSSL_strdup(name);
387 if (tmpname == NULL) {
388 return (0);
391 OPENSSL_free((char *)meth->name);
392 meth->name = tmpname;
394 return (1);
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;
403 return (1);
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;
412 return (1);
416 RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa))
418 meth->finish = finish;
419 return (1);
422 void
423 RSA_meth_free(RSA_METHOD *meth)
425 if (meth != NULL) {
426 OPENSSL_free((char *)meth->name);
427 OPENSSL_free(meth);
432 RSA_bits(const RSA *r)
434 return (BN_num_bits(r->n));
437 RSA *
438 EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
440 if (pkey->type != EVP_PKEY_RSA) {
441 return (NULL);
443 return (pkey->pkey.rsa);
446 #endif /* OPENSSL_VERSION_NUMBER */