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