xref: /illumos-gate/usr/src/lib/libkmf/plugins/kmf_openssl/common/compat.c (revision 553e44ce7e541a9e1404b1ead92be0753668710a)
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright 2018 RackTop Systems.
4  *
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  */
10 
11 #include <string.h>
12 #include <openssl/bio.h>
13 #include <openssl/engine.h>
14 #include "compat.h"
15 
16 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
17 
18 static void *
OPENSSL_zalloc(size_t num)19 OPENSSL_zalloc(size_t num)
20 {
21 	void *ret = OPENSSL_malloc(num);
22 
23 	if (ret != NULL)
24 		(void) memset(ret, 0, num);
25 	return (ret);
26 }
27 
28 int
RSA_set0_key(RSA * r,BIGNUM * n,BIGNUM * e,BIGNUM * d)29 RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
30 {
31 	/*
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).
35 	 */
36 	if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
37 		return (0);
38 
39 	if (n != NULL) {
40 		BN_free(r->n);
41 		r->n = n;
42 	}
43 	if (e != NULL) {
44 		BN_free(r->e);
45 		r->e = e;
46 	}
47 	if (d != NULL) {
48 		BN_free(r->d);
49 		r->d = d;
50 	}
51 
52 	return (1);
53 }
54 
55 int
RSA_set0_factors(RSA * r,BIGNUM * p,BIGNUM * q)56 RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
57 {
58 	/*
59 	 * If the fields p and q in r are NULL, the corresponding input
60 	 * parameters MUST be non-NULL.
61 	 */
62 	if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL))
63 		return (0);
64 
65 	if (p != NULL) {
66 		BN_free(r->p);
67 		r->p = p;
68 	}
69 	if (q != NULL) {
70 		BN_free(r->q);
71 		r->q = q;
72 	}
73 
74 	return (1);
75 }
76 
77 int
RSA_set0_crt_params(RSA * r,BIGNUM * dmp1,BIGNUM * dmq1,BIGNUM * iqmp)78 RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
79 {
80 	/*
81 	 * If the fields dmp1, dmq1 and iqmp in r are NULL, the
82 	 * corresponding input parameters MUST be non-NULL.
83 	 */
84 	if ((r->dmp1 == NULL && dmp1 == NULL) ||
85 	    (r->dmq1 == NULL && dmq1 == NULL) ||
86 	    (r->iqmp == NULL && iqmp == NULL))
87 		return (0);
88 
89 	if (dmp1 != NULL) {
90 		BN_free(r->dmp1);
91 		r->dmp1 = dmp1;
92 	}
93 	if (dmq1 != NULL) {
94 		BN_free(r->dmq1);
95 		r->dmq1 = dmq1;
96 	}
97 	if (iqmp != NULL) {
98 		BN_free(r->iqmp);
99 		r->iqmp = iqmp;
100 	}
101 
102 	return (1);
103 }
104 
105 void
RSA_get0_key(const RSA * r,const BIGNUM ** n,const BIGNUM ** e,const BIGNUM ** d)106 RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
107 {
108 	if (n != NULL)
109 		*n = r->n;
110 	if (e != NULL)
111 		*e = r->e;
112 	if (d != NULL)
113 		*d = r->d;
114 }
115 
116 void
RSA_get0_factors(const RSA * r,const BIGNUM ** p,const BIGNUM ** q)117 RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
118 {
119 	if (p != NULL)
120 		*p = r->p;
121 	if (q != NULL)
122 		*q = r->q;
123 }
124 
125 void
RSA_get0_crt_params(const RSA * r,const BIGNUM ** dmp1,const BIGNUM ** dmq1,const BIGNUM ** iqmp)126 RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
127     const BIGNUM **iqmp)
128 {
129 	if (dmp1 != NULL)
130 		*dmp1 = r->dmp1;
131 	if (dmq1 != NULL)
132 		*dmq1 = r->dmq1;
133 	if (iqmp != NULL)
134 		*iqmp = r->iqmp;
135 }
136 
137 void
DSA_get0_pqg(const DSA * d,const BIGNUM ** p,const BIGNUM ** q,const BIGNUM ** g)138 DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q,
139     const BIGNUM **g)
140 {
141 	if (p != NULL)
142 		*p = d->p;
143 	if (q != NULL)
144 		*q = d->q;
145 	if (g != NULL)
146 		*g = d->g;
147 }
148 
149 int
DSA_set0_pqg(DSA * d,BIGNUM * p,BIGNUM * q,BIGNUM * g)150 DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
151 {
152 	/*
153 	 * If the fields p, q and g in d are NULL, the corresponding input
154 	 * parameters MUST be non-NULL.
155 	 */
156 	if ((d->p == NULL && p == NULL) || (d->q == NULL && q == NULL) ||
157 	    (d->g == NULL && g == NULL))
158 		return (0);
159 
160 	if (p != NULL) {
161 		BN_free(d->p);
162 		d->p = p;
163 	}
164 	if (q != NULL) {
165 		BN_free(d->q);
166 		d->q = q;
167 	}
168 	if (g != NULL) {
169 		BN_free(d->g);
170 		d->g = g;
171 	}
172 
173 	return (1);
174 }
175 
176 void
DSA_get0_key(const DSA * d,const BIGNUM ** pub_key,const BIGNUM ** priv_key)177 DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
178 {
179 	if (pub_key != NULL)
180 		*pub_key = d->pub_key;
181 	if (priv_key != NULL)
182 		*priv_key = d->priv_key;
183 }
184 
185 int
DSA_set0_key(DSA * d,BIGNUM * pub_key,BIGNUM * priv_key)186 DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
187 {
188 	/*
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.
192 	 */
193 	if (d->pub_key == NULL && pub_key == NULL)
194 		return (0);
195 
196 	if (pub_key != NULL) {
197 		BN_free(d->pub_key);
198 		d->pub_key = pub_key;
199 	}
200 	if (priv_key != NULL) {
201 		BN_free(d->priv_key);
202 		d->priv_key = priv_key;
203 	}
204 
205 	return (1);
206 }
207 
208 void
DSA_SIG_get0(const DSA_SIG * sig,const BIGNUM ** pr,const BIGNUM ** ps)209 DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
210 {
211 	if (pr != NULL)
212 		*pr = sig->r;
213 	if (ps != NULL)
214 		*ps = sig->s;
215 }
216 
217 int
DSA_SIG_set0(DSA_SIG * sig,BIGNUM * r,BIGNUM * s)218 DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
219 {
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);
227 }
228 
229 DSA *
EVP_PKEY_get0_DSA(EVP_PKEY * pkey)230 EVP_PKEY_get0_DSA(EVP_PKEY *pkey)
231 {
232 	if (pkey->type != EVP_PKEY_DSA)
233 		return (NULL);
234 	return (pkey->pkey.dsa);
235 }
236 
237 void
ECDSA_SIG_get0(const ECDSA_SIG * sig,const BIGNUM ** pr,const BIGNUM ** ps)238 ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
239 {
240 	if (pr != NULL)
241 		*pr = sig->r;
242 	if (ps != NULL)
243 		*ps = sig->s;
244 }
245 
246 int
ECDSA_SIG_set0(ECDSA_SIG * sig,BIGNUM * r,BIGNUM * s)247 ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
248 {
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);
256 }
257 
258 void
DH_get0_pqg(const DH * dh,const BIGNUM ** p,const BIGNUM ** q,const BIGNUM ** g)259 DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
260 {
261 	if (p != NULL)
262 		*p = dh->p;
263 	if (q != NULL)
264 		*q = dh->q;
265 	if (g != NULL)
266 		*g = dh->g;
267 }
268 
269 int
DH_set0_pqg(DH * dh,BIGNUM * p,BIGNUM * q,BIGNUM * g)270 DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
271 {
272 	/*
273 	 * If the fields p and g in d are NULL, the corresponding input
274 	 * parameters MUST be non-NULL.  q may remain NULL.
275 	 */
276 	if ((dh->p == NULL && p == NULL) || (dh->g == NULL && g == NULL))
277 		return (0);
278 
279 	if (p != NULL) {
280 		BN_free(dh->p);
281 		dh->p = p;
282 	}
283 	if (q != NULL) {
284 		BN_free(dh->q);
285 		dh->q = q;
286 	}
287 	if (g != NULL) {
288 		BN_free(dh->g);
289 		dh->g = g;
290 	}
291 
292 	if (q != NULL) {
293 		dh->length = BN_num_bits(q);
294 	}
295 
296 	return (1);
297 }
298 
299 void
DH_get0_key(const DH * dh,const BIGNUM ** pub_key,const BIGNUM ** priv_key)300 DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
301 {
302 	if (pub_key != NULL)
303 		*pub_key = dh->pub_key;
304 	if (priv_key != NULL)
305 		*priv_key = dh->priv_key;
306 }
307 
308 int
DH_set0_key(DH * dh,BIGNUM * pub_key,BIGNUM * priv_key)309 DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
310 {
311 	/*
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.
315 	 */
316 	if (dh->pub_key == NULL && pub_key == NULL)
317 		return (0);
318 
319 	if (pub_key != NULL) {
320 		BN_free(dh->pub_key);
321 		dh->pub_key = pub_key;
322 	}
323 	if (priv_key != NULL) {
324 		BN_free(dh->priv_key);
325 		dh->priv_key = priv_key;
326 	}
327 
328 	return (1);
329 }
330 
331 int
DH_set_length(DH * dh,long length)332 DH_set_length(DH *dh, long length)
333 {
334 	dh->length = length;
335 	return (1);
336 }
337 
338 const unsigned char *
EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX * ctx)339 EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
340 {
341 	return (ctx->iv);
342 }
343 
344 unsigned char *
EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX * ctx)345 EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
346 {
347 	return (ctx->iv);
348 }
349 
350 EVP_MD_CTX *
EVP_MD_CTX_new(void)351 EVP_MD_CTX_new(void)
352 {
353 	return (OPENSSL_zalloc(sizeof (EVP_MD_CTX)));
354 }
355 
356 void
EVP_MD_CTX_free(EVP_MD_CTX * ctx)357 EVP_MD_CTX_free(EVP_MD_CTX *ctx)
358 {
359 	(void) EVP_MD_CTX_cleanup(ctx);
360 	OPENSSL_free(ctx);
361 }
362 
363 RSA_METHOD *
RSA_meth_dup(const RSA_METHOD * meth)364 RSA_meth_dup(const RSA_METHOD *meth)
365 {
366 	RSA_METHOD *ret;
367 
368 	ret = OPENSSL_malloc(sizeof (RSA_METHOD));
369 
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);
376 		}
377 	}
378 
379 	return (ret);
380 }
381 
382 int
RSA_meth_set1_name(RSA_METHOD * meth,const char * name)383 RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
384 {
385 	char *tmpname;
386 
387 	tmpname = OPENSSL_strdup(name);
388 	if (tmpname == NULL) {
389 		return (0);
390 	}
391 
392 	OPENSSL_free((char *)meth->name);
393 	meth->name = tmpname;
394 
395 	return (1);
396 }
397 
398 int
RSA_meth_set_priv_enc(RSA_METHOD * meth,int (* priv_enc)(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding))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))
402 {
403 	meth->rsa_priv_enc = priv_enc;
404 	return (1);
405 }
406 
407 int
RSA_meth_set_priv_dec(RSA_METHOD * meth,int (* priv_dec)(int flen,const unsigned char * from,unsigned char * to,RSA * rsa,int padding))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))
411 {
412 	meth->rsa_priv_dec = priv_dec;
413 	return (1);
414 }
415 
416 int
RSA_meth_set_finish(RSA_METHOD * meth,int (* finish)(RSA * rsa))417 RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa))
418 {
419 	meth->finish = finish;
420 	return (1);
421 }
422 
423 void
RSA_meth_free(RSA_METHOD * meth)424 RSA_meth_free(RSA_METHOD *meth)
425 {
426 	if (meth != NULL) {
427 		OPENSSL_free((char *)meth->name);
428 		OPENSSL_free(meth);
429 	}
430 }
431 
432 int
RSA_bits(const RSA * r)433 RSA_bits(const RSA *r)
434 {
435 	return (BN_num_bits(r->n));
436 }
437 
438 RSA *
EVP_PKEY_get0_RSA(EVP_PKEY * pkey)439 EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
440 {
441 	if (pkey->type != EVP_PKEY_RSA) {
442 		return (NULL);
443 	}
444 	return (pkey->pkey.rsa);
445 }
446 
447 #endif /* OPENSSL_VERSION_NUMBER || LIBRESSL_VERSION_NUMBER */
448