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 %{
11 #ifdef LIBRESSL_VERSION_NUMBER
12 #include <string.h>
13 # define OPENSSL_zalloc(num) \
14         CRYPTO_zalloc(num, __FILE__, __LINE__)
15 
16 static void *CRYPTO_zalloc(size_t num, const char *file, int line)
17 {
18       void *ret = CRYPTO_malloc(num, file, line);
19       if (ret != NULL)
20               memset(ret, 0, num);
21       return ret;
22 }
23 #endif
24 
25 #if OPENSSL_VERSION_NUMBER < 0x10100000L
26 
27 #include <string.h>
28 #include <openssl/engine.h>
29 
30 # define OPENSSL_zalloc(num) \
31         CRYPTO_zalloc(num, __FILE__, __LINE__)
32 
33 static void *CRYPTO_zalloc(size_t num, const char *file, int line)
34 {
35       void *ret = CRYPTO_malloc(num, file, line);
36       if (ret != NULL)
37               memset(ret, 0, num);
38       return ret;
39 }
40 
41 #include <openssl/bn.h>
42 
43 #ifndef BN_F_BN_GENCB_NEW
44 # define BN_F_BN_GENCB_NEW       143
45 #endif
46 
47 # define BN_GENCB_get_arg(gencb) ((gencb)->arg)
48 
49 BN_GENCB *BN_GENCB_new(void)
50 {
51     BN_GENCB *ret;
52 
53     if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
54         BNerr(BN_F_BN_GENCB_NEW, ERR_R_MALLOC_FAILURE);
55         return (NULL);
56     }
57 
58     return ret;
59 }
60 
61 void BN_GENCB_free(BN_GENCB *cb)
62 {
63     if (cb == NULL)
64         return;
65     OPENSSL_free(cb);
66 }
67 
68 
69 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
70 {
71     /* If the fields n and e in r are NULL, the corresponding input
72      * parameters MUST be non-NULL for n and e.  d may be
73      * left NULL (in case only the public key is used).
74      */
75     if ((r->n == NULL && n == NULL)
76         || (r->e == NULL && e == NULL))
77         return 0;
78 
79     if (n != NULL) {
80         BN_free(r->n);
81         r->n = n;
82     }
83     if (e != NULL) {
84         BN_free(r->e);
85         r->e = e;
86     }
87     if (d != NULL) {
88         BN_free(r->d);
89         r->d = d;
90     }
91 
92     return 1;
93 }
94 
95 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
96 {
97     /* If the fields p and q in r are NULL, the corresponding input
98      * parameters MUST be non-NULL.
99      */
100     if ((r->p == NULL && p == NULL)
101         || (r->q == NULL && q == NULL))
102         return 0;
103 
104     if (p != NULL) {
105         BN_free(r->p);
106         r->p = p;
107     }
108     if (q != NULL) {
109         BN_free(r->q);
110         r->q = q;
111     }
112 
113     return 1;
114 }
115 
116 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
117 {
118     /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
119      * parameters MUST be non-NULL.
120      */
121     if ((r->dmp1 == NULL && dmp1 == NULL)
122         || (r->dmq1 == NULL && dmq1 == NULL)
123         || (r->iqmp == NULL && iqmp == NULL))
124         return 0;
125 
126     if (dmp1 != NULL) {
127         BN_free(r->dmp1);
128         r->dmp1 = dmp1;
129     }
130     if (dmq1 != NULL) {
131         BN_free(r->dmq1);
132         r->dmq1 = dmq1;
133     }
134     if (iqmp != NULL) {
135         BN_free(r->iqmp);
136         r->iqmp = iqmp;
137     }
138 
139     return 1;
140 }
141 
142 void RSA_get0_key(const RSA *r,
143                   const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
144 {
145     if (n != NULL)
146         *n = r->n;
147     if (e != NULL)
148         *e = r->e;
149     if (d != NULL)
150         *d = r->d;
151 }
152 
153 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
154 {
155     if (p != NULL)
156         *p = r->p;
157     if (q != NULL)
158         *q = r->q;
159 }
160 
161 void RSA_get0_crt_params(const RSA *r,
162                          const BIGNUM **dmp1, const BIGNUM **dmq1,
163                          const BIGNUM **iqmp)
164 {
165     if (dmp1 != NULL)
166         *dmp1 = r->dmp1;
167     if (dmq1 != NULL)
168         *dmq1 = r->dmq1;
169     if (iqmp != NULL)
170         *iqmp = r->iqmp;
171 }
172 
173 void DSA_get0_pqg(const DSA *d,
174                   const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
175 {
176     if (p != NULL)
177         *p = d->p;
178     if (q != NULL)
179         *q = d->q;
180     if (g != NULL)
181         *g = d->g;
182 }
183 
184 int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
185 {
186     /* If the fields p, q and g in d are NULL, the corresponding input
187      * parameters MUST be non-NULL.
188      */
189     if ((d->p == NULL && p == NULL)
190         || (d->q == NULL && q == NULL)
191         || (d->g == NULL && g == NULL))
192         return 0;
193 
194     if (p != NULL) {
195         BN_free(d->p);
196         d->p = p;
197     }
198     if (q != NULL) {
199         BN_free(d->q);
200         d->q = q;
201     }
202     if (g != NULL) {
203         BN_free(d->g);
204         d->g = g;
205     }
206 
207     return 1;
208 }
209 
210 void DSA_get0_key(const DSA *d,
211                   const BIGNUM **pub_key, const BIGNUM **priv_key)
212 {
213     if (pub_key != NULL)
214         *pub_key = d->pub_key;
215     if (priv_key != NULL)
216         *priv_key = d->priv_key;
217 }
218 
219 int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
220 {
221     /* If the field pub_key in d is NULL, the corresponding input
222      * parameters MUST be non-NULL.  The priv_key field may
223      * be left NULL.
224      */
225     if (d->pub_key == NULL && pub_key == NULL)
226         return 0;
227 
228     if (pub_key != NULL) {
229         BN_free(d->pub_key);
230         d->pub_key = pub_key;
231     }
232     if (priv_key != NULL) {
233         BN_free(d->priv_key);
234         d->priv_key = priv_key;
235     }
236 
237     return 1;
238 }
239 
240 void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
241 {
242     if (pr != NULL)
243         *pr = sig->r;
244     if (ps != NULL)
245         *ps = sig->s;
246 }
247 
248 int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
249 {
250     if (r == NULL || s == NULL)
251         return 0;
252     BN_clear_free(sig->r);
253     BN_clear_free(sig->s);
254     sig->r = r;
255     sig->s = s;
256     return 1;
257 }
258 
259 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
260 {
261     if (pr != NULL)
262         *pr = sig->r;
263     if (ps != NULL)
264         *ps = sig->s;
265 }
266 
267 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
268 {
269     if (r == NULL || s == NULL)
270         return 0;
271     BN_clear_free(sig->r);
272     BN_clear_free(sig->s);
273     sig->r = r;
274     sig->s = s;
275     return 1;
276 }
277 
278 void DH_get0_pqg(const DH *dh,
279                  const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
280 {
281     if (p != NULL)
282         *p = dh->p;
283     if (q != NULL)
284         *q = dh->q;
285     if (g != NULL)
286         *g = dh->g;
287 }
288 
289 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
290 {
291     /* If the fields p and g in d are NULL, the corresponding input
292      * parameters MUST be non-NULL.  q may remain NULL.
293      */
294     if ((dh->p == NULL && p == NULL)
295         || (dh->g == NULL && g == NULL))
296         return 0;
297 
298     if (p != NULL) {
299         BN_free(dh->p);
300         dh->p = p;
301     }
302     if (q != NULL) {
303         BN_free(dh->q);
304         dh->q = q;
305     }
306     if (g != NULL) {
307         BN_free(dh->g);
308         dh->g = g;
309     }
310 
311     if (q != NULL) {
312         dh->length = BN_num_bits(q);
313     }
314 
315     return 1;
316 }
317 
318 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
319 {
320     if (pub_key != NULL)
321         *pub_key = dh->pub_key;
322     if (priv_key != NULL)
323         *priv_key = dh->priv_key;
324 }
325 
326 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
327 {
328     /* If the field pub_key in dh is NULL, the corresponding input
329      * parameters MUST be non-NULL.  The priv_key field may
330      * be left NULL.
331      */
332     if (dh->pub_key == NULL && pub_key == NULL)
333         return 0;
334 
335     if (pub_key != NULL) {
336         BN_free(dh->pub_key);
337         dh->pub_key = pub_key;
338     }
339     if (priv_key != NULL) {
340         BN_free(dh->priv_key);
341         dh->priv_key = priv_key;
342     }
343 
344     return 1;
345 }
346 
347 int DH_set_length(DH *dh, long length)
348 {
349     dh->length = length;
350     return 1;
351 }
352 
353 const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx)
354 {
355     return ctx->iv;
356 }
357 
358 unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx)
359 {
360     return ctx->iv;
361 }
362 
363 EVP_MD_CTX *EVP_MD_CTX_new(void)
364 {
365     return OPENSSL_zalloc(sizeof(EVP_MD_CTX));
366 }
367 
368 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
369 {
370     EVP_MD_CTX_cleanup(ctx);
371     OPENSSL_free(ctx);
372 }
373 
374 int RSA_size(const RSA* rsa) {
375     /* BIGNUM* n = NULL;
376     RSA_get0_key(rsa, n, NULL, NULL); */
377     return BN_num_bytes(rsa->n);
378 }
379 
380 RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth)
381 {
382     RSA_METHOD *ret;
383 
384     ret = OPENSSL_malloc(sizeof(RSA_METHOD));
385 
386     if (ret != NULL) {
387         memcpy(ret, meth, sizeof(*meth));
388         ret->name = OPENSSL_strdup(meth->name);
389         if (ret->name == NULL) {
390             OPENSSL_free(ret);
391             return NULL;
392         }
393     }
394 
395     return ret;
396 }
397 
398 int RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
399 {
400     char *tmpname;
401 
402     tmpname = OPENSSL_strdup(name);
403     if (tmpname == NULL) {
404         return 0;
405     }
406 
407     OPENSSL_free((char *)meth->name);
408     meth->name = tmpname;
409 
410     return 1;
411 }
412 
413 int RSA_meth_set_priv_enc(RSA_METHOD *meth,
414                           int (*priv_enc) (int flen, const unsigned char *from,
415                                            unsigned char *to, RSA *rsa,
416                                            int padding))
417 {
418     meth->rsa_priv_enc = priv_enc;
419     return 1;
420 }
421 
422 int RSA_meth_set_priv_dec(RSA_METHOD *meth,
423                           int (*priv_dec) (int flen, const unsigned char *from,
424                                            unsigned char *to, RSA *rsa,
425                                            int padding))
426 {
427     meth->rsa_priv_dec = priv_dec;
428     return 1;
429 }
430 
431 int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa))
432 {
433     meth->finish = finish;
434     return 1;
435 }
436 
437 void RSA_meth_free(RSA_METHOD *meth)
438 {
439     if (meth != NULL) {
440         OPENSSL_free((char *)meth->name);
441         OPENSSL_free(meth);
442     }
443 }
444 
445 int RSA_bits(const RSA *r)
446 {
447     return (BN_num_bits(r->n));
448 }
449 
450 RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
451 {
452     if (pkey->type != EVP_PKEY_RSA) {
453         return NULL;
454     }
455     return pkey->pkey.rsa;
456 }
457 
458 int X509_NAME_get0_der(X509_NAME *nm, const unsigned char **pder,
459                        size_t *pderlen)
460 {
461     /* Make sure encoding is valid */
462     if (i2d_X509_NAME(nm, NULL) <= 0)
463         return 0;
464     if (pder != NULL)
465         *pder = (unsigned char *)nm->bytes->data;
466     if (pderlen != NULL)
467         *pderlen = nm->bytes->length;
468     return 1;
469 }
470 
471 #endif /* OPENSSL_VERSION_NUMBER */
472 %}
473