Line 0
Link Here
|
|
|
1 |
--- src/libstrongswan/plugins/openssl/openssl_compat.c.orig 2017-04-25 20:15:52 UTC |
2 |
+++ src/libstrongswan/plugins/openssl/openssl_compat.c |
3 |
@@ -0,0 +1,419 @@ |
4 |
+/* |
5 |
+ * Copyright (C) 2008-2016 Tobias Brunner |
6 |
+ * Copyright (C) 2008 Martin Willi |
7 |
+ * HSR Hochschule fuer Technik Rapperswil |
8 |
+ * Copyright (C) 2017 Bernard Spil |
9 |
+ * |
10 |
+ * This program is free software; you can redistribute it and/or modify it |
11 |
+ * under the terms of the GNU General Public License as published by the |
12 |
+ * Free Software Foundation; either version 2 of the License, or (at your |
13 |
+ * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. |
14 |
+ * |
15 |
+ * This program is distributed in the hope that it will be useful, but |
16 |
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
17 |
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
18 |
+ * for more details. |
19 |
+ */ |
20 |
+ |
21 |
+#include "openssl_compat.h" |
22 |
+ |
23 |
+#if OPENSSL_VERSION_NUMBER < 0x10100000L |
24 |
+ |
25 |
+#include <string.h> |
26 |
+#include <openssl/engine.h> |
27 |
+#include <openssl/hmac.h> |
28 |
+ |
29 |
+static void *OPENSSL_zalloc(size_t num) |
30 |
+{ |
31 |
+ void *ret = OPENSSL_malloc(num); |
32 |
+ |
33 |
+ if (ret != NULL) |
34 |
+ memset(ret, 0, num); |
35 |
+ return ret; |
36 |
+} |
37 |
+ |
38 |
+int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) |
39 |
+{ |
40 |
+ /* If the fields n and e in r are NULL, the corresponding input |
41 |
+ * parameters MUST be non-NULL for n and e. d may be |
42 |
+ * left NULL (in case only the public key is used). |
43 |
+ */ |
44 |
+ if ((r->n == NULL && n == NULL) |
45 |
+ || (r->e == NULL && e == NULL)) |
46 |
+ return 0; |
47 |
+ |
48 |
+ if (n != NULL) { |
49 |
+ BN_free(r->n); |
50 |
+ r->n = n; |
51 |
+ } |
52 |
+ if (e != NULL) { |
53 |
+ BN_free(r->e); |
54 |
+ r->e = e; |
55 |
+ } |
56 |
+ if (d != NULL) { |
57 |
+ BN_free(r->d); |
58 |
+ r->d = d; |
59 |
+ } |
60 |
+ |
61 |
+ return 1; |
62 |
+} |
63 |
+ |
64 |
+int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) |
65 |
+{ |
66 |
+ /* If the fields p and q in r are NULL, the corresponding input |
67 |
+ * parameters MUST be non-NULL. |
68 |
+ */ |
69 |
+ if ((r->p == NULL && p == NULL) |
70 |
+ || (r->q == NULL && q == NULL)) |
71 |
+ return 0; |
72 |
+ |
73 |
+ if (p != NULL) { |
74 |
+ BN_free(r->p); |
75 |
+ r->p = p; |
76 |
+ } |
77 |
+ if (q != NULL) { |
78 |
+ BN_free(r->q); |
79 |
+ r->q = q; |
80 |
+ } |
81 |
+ |
82 |
+ return 1; |
83 |
+} |
84 |
+ |
85 |
+int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) |
86 |
+{ |
87 |
+ /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input |
88 |
+ * parameters MUST be non-NULL. |
89 |
+ */ |
90 |
+ if ((r->dmp1 == NULL && dmp1 == NULL) |
91 |
+ || (r->dmq1 == NULL && dmq1 == NULL) |
92 |
+ || (r->iqmp == NULL && iqmp == NULL)) |
93 |
+ return 0; |
94 |
+ |
95 |
+ if (dmp1 != NULL) { |
96 |
+ BN_free(r->dmp1); |
97 |
+ r->dmp1 = dmp1; |
98 |
+ } |
99 |
+ if (dmq1 != NULL) { |
100 |
+ BN_free(r->dmq1); |
101 |
+ r->dmq1 = dmq1; |
102 |
+ } |
103 |
+ if (iqmp != NULL) { |
104 |
+ BN_free(r->iqmp); |
105 |
+ r->iqmp = iqmp; |
106 |
+ } |
107 |
+ |
108 |
+ return 1; |
109 |
+} |
110 |
+ |
111 |
+void RSA_get0_key(const RSA *r, |
112 |
+ const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) |
113 |
+{ |
114 |
+ if (n != NULL) |
115 |
+ *n = r->n; |
116 |
+ if (e != NULL) |
117 |
+ *e = r->e; |
118 |
+ if (d != NULL) |
119 |
+ *d = r->d; |
120 |
+} |
121 |
+ |
122 |
+void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) |
123 |
+{ |
124 |
+ if (p != NULL) |
125 |
+ *p = r->p; |
126 |
+ if (q != NULL) |
127 |
+ *q = r->q; |
128 |
+} |
129 |
+ |
130 |
+void RSA_get0_crt_params(const RSA *r, |
131 |
+ const BIGNUM **dmp1, const BIGNUM **dmq1, |
132 |
+ const BIGNUM **iqmp) |
133 |
+{ |
134 |
+ if (dmp1 != NULL) |
135 |
+ *dmp1 = r->dmp1; |
136 |
+ if (dmq1 != NULL) |
137 |
+ *dmq1 = r->dmq1; |
138 |
+ if (iqmp != NULL) |
139 |
+ *iqmp = r->iqmp; |
140 |
+} |
141 |
+ |
142 |
+void DSA_get0_pqg(const DSA *d, |
143 |
+ const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) |
144 |
+{ |
145 |
+ if (p != NULL) |
146 |
+ *p = d->p; |
147 |
+ if (q != NULL) |
148 |
+ *q = d->q; |
149 |
+ if (g != NULL) |
150 |
+ *g = d->g; |
151 |
+} |
152 |
+ |
153 |
+int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) |
154 |
+{ |
155 |
+ /* If the fields p, q and g in d are NULL, the corresponding input |
156 |
+ * parameters MUST be non-NULL. |
157 |
+ */ |
158 |
+ if ((d->p == NULL && p == NULL) |
159 |
+ || (d->q == NULL && q == NULL) |
160 |
+ || (d->g == NULL && g == NULL)) |
161 |
+ return 0; |
162 |
+ |
163 |
+ if (p != NULL) { |
164 |
+ BN_free(d->p); |
165 |
+ d->p = p; |
166 |
+ } |
167 |
+ if (q != NULL) { |
168 |
+ BN_free(d->q); |
169 |
+ d->q = q; |
170 |
+ } |
171 |
+ if (g != NULL) { |
172 |
+ BN_free(d->g); |
173 |
+ d->g = g; |
174 |
+ } |
175 |
+ |
176 |
+ return 1; |
177 |
+} |
178 |
+ |
179 |
+void DSA_get0_key(const DSA *d, |
180 |
+ const BIGNUM **pub_key, const BIGNUM **priv_key) |
181 |
+{ |
182 |
+ if (pub_key != NULL) |
183 |
+ *pub_key = d->pub_key; |
184 |
+ if (priv_key != NULL) |
185 |
+ *priv_key = d->priv_key; |
186 |
+} |
187 |
+ |
188 |
+int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key) |
189 |
+{ |
190 |
+ /* If the field pub_key in d is NULL, the corresponding input |
191 |
+ * parameters MUST be non-NULL. The priv_key field may |
192 |
+ * be left NULL. |
193 |
+ */ |
194 |
+ if (d->pub_key == NULL && pub_key == NULL) |
195 |
+ return 0; |
196 |
+ |
197 |
+ if (pub_key != NULL) { |
198 |
+ BN_free(d->pub_key); |
199 |
+ d->pub_key = pub_key; |
200 |
+ } |
201 |
+ if (priv_key != NULL) { |
202 |
+ BN_free(d->priv_key); |
203 |
+ d->priv_key = priv_key; |
204 |
+ } |
205 |
+ |
206 |
+ return 1; |
207 |
+} |
208 |
+ |
209 |
+void 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 |
+{ |
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 |
+void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) |
229 |
+{ |
230 |
+ if (pr != NULL) |
231 |
+ *pr = sig->r; |
232 |
+ if (ps != NULL) |
233 |
+ *ps = sig->s; |
234 |
+} |
235 |
+ |
236 |
+int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) |
237 |
+{ |
238 |
+ if (r == NULL || s == NULL) |
239 |
+ return 0; |
240 |
+ BN_clear_free(sig->r); |
241 |
+ BN_clear_free(sig->s); |
242 |
+ sig->r = r; |
243 |
+ sig->s = s; |
244 |
+ return 1; |
245 |
+} |
246 |
+ |
247 |
+void DH_get0_pqg(const DH *dh, |
248 |
+ const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) |
249 |
+{ |
250 |
+ if (p != NULL) |
251 |
+ *p = dh->p; |
252 |
+ if (q != NULL) |
253 |
+ *q = dh->q; |
254 |
+ if (g != NULL) |
255 |
+ *g = dh->g; |
256 |
+} |
257 |
+ |
258 |
+int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) |
259 |
+{ |
260 |
+ /* If the fields p and g in d are NULL, the corresponding input |
261 |
+ * parameters MUST be non-NULL. q may remain NULL. |
262 |
+ */ |
263 |
+ if ((dh->p == NULL && p == NULL) |
264 |
+ || (dh->g == NULL && g == NULL)) |
265 |
+ return 0; |
266 |
+ |
267 |
+ if (p != NULL) { |
268 |
+ BN_free(dh->p); |
269 |
+ dh->p = p; |
270 |
+ } |
271 |
+ if (q != NULL) { |
272 |
+ BN_free(dh->q); |
273 |
+ dh->q = q; |
274 |
+ } |
275 |
+ if (g != NULL) { |
276 |
+ BN_free(dh->g); |
277 |
+ dh->g = g; |
278 |
+ } |
279 |
+ |
280 |
+ if (q != NULL) { |
281 |
+ dh->length = BN_num_bits(q); |
282 |
+ } |
283 |
+ |
284 |
+ return 1; |
285 |
+} |
286 |
+ |
287 |
+void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key) |
288 |
+{ |
289 |
+ if (pub_key != NULL) |
290 |
+ *pub_key = dh->pub_key; |
291 |
+ if (priv_key != NULL) |
292 |
+ *priv_key = dh->priv_key; |
293 |
+} |
294 |
+ |
295 |
+int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) |
296 |
+{ |
297 |
+ /* If the field pub_key in dh is NULL, the corresponding input |
298 |
+ * parameters MUST be non-NULL. The priv_key field may |
299 |
+ * be left NULL. |
300 |
+ */ |
301 |
+ if (dh->pub_key == NULL && pub_key == NULL) |
302 |
+ return 0; |
303 |
+ |
304 |
+ if (pub_key != NULL) { |
305 |
+ BN_free(dh->pub_key); |
306 |
+ dh->pub_key = pub_key; |
307 |
+ } |
308 |
+ if (priv_key != NULL) { |
309 |
+ BN_free(dh->priv_key); |
310 |
+ dh->priv_key = priv_key; |
311 |
+ } |
312 |
+ |
313 |
+ return 1; |
314 |
+} |
315 |
+ |
316 |
+int DH_set_length(DH *dh, long length) |
317 |
+{ |
318 |
+ dh->length = length; |
319 |
+ return 1; |
320 |
+} |
321 |
+ |
322 |
+const unsigned char *EVP_CIPHER_CTX_iv(const EVP_CIPHER_CTX *ctx) |
323 |
+{ |
324 |
+ return ctx->iv; |
325 |
+} |
326 |
+ |
327 |
+unsigned char *EVP_CIPHER_CTX_iv_noconst(EVP_CIPHER_CTX *ctx) |
328 |
+{ |
329 |
+ return ctx->iv; |
330 |
+} |
331 |
+ |
332 |
+EVP_MD_CTX *EVP_MD_CTX_new(void) |
333 |
+{ |
334 |
+ return OPENSSL_zalloc(sizeof(EVP_MD_CTX)); |
335 |
+} |
336 |
+ |
337 |
+void EVP_MD_CTX_free(EVP_MD_CTX *ctx) |
338 |
+{ |
339 |
+ EVP_MD_CTX_cleanup(ctx); |
340 |
+ OPENSSL_free(ctx); |
341 |
+} |
342 |
+ |
343 |
+RSA_METHOD *RSA_meth_dup(const RSA_METHOD *meth) |
344 |
+{ |
345 |
+ RSA_METHOD *ret; |
346 |
+ |
347 |
+ ret = OPENSSL_malloc(sizeof(RSA_METHOD)); |
348 |
+ |
349 |
+ if (ret != NULL) { |
350 |
+ memcpy(ret, meth, sizeof(*meth)); |
351 |
+ ret->name = OPENSSL_strdup(meth->name); |
352 |
+ if (ret->name == NULL) { |
353 |
+ OPENSSL_free(ret); |
354 |
+ return NULL; |
355 |
+ } |
356 |
+ } |
357 |
+ |
358 |
+ return ret; |
359 |
+} |
360 |
+ |
361 |
+int RSA_meth_set1_name(RSA_METHOD *meth, const char *name) |
362 |
+{ |
363 |
+ char *tmpname; |
364 |
+ |
365 |
+ tmpname = OPENSSL_strdup(name); |
366 |
+ if (tmpname == NULL) { |
367 |
+ return 0; |
368 |
+ } |
369 |
+ |
370 |
+ OPENSSL_free((char *)meth->name); |
371 |
+ meth->name = tmpname; |
372 |
+ |
373 |
+ return 1; |
374 |
+} |
375 |
+ |
376 |
+int RSA_meth_set_priv_enc(RSA_METHOD *meth, |
377 |
+ int (*priv_enc) (int flen, const unsigned char *from, |
378 |
+ unsigned char *to, RSA *rsa, |
379 |
+ int padding)) |
380 |
+{ |
381 |
+ meth->rsa_priv_enc = priv_enc; |
382 |
+ return 1; |
383 |
+} |
384 |
+ |
385 |
+int RSA_meth_set_priv_dec(RSA_METHOD *meth, |
386 |
+ int (*priv_dec) (int flen, const unsigned char *from, |
387 |
+ unsigned char *to, RSA *rsa, |
388 |
+ int padding)) |
389 |
+{ |
390 |
+ meth->rsa_priv_dec = priv_dec; |
391 |
+ return 1; |
392 |
+} |
393 |
+ |
394 |
+int RSA_meth_set_finish(RSA_METHOD *meth, int (*finish) (RSA *rsa)) |
395 |
+{ |
396 |
+ meth->finish = finish; |
397 |
+ return 1; |
398 |
+} |
399 |
+ |
400 |
+void RSA_meth_free(RSA_METHOD *meth) |
401 |
+{ |
402 |
+ if (meth != NULL) { |
403 |
+ OPENSSL_free((char *)meth->name); |
404 |
+ OPENSSL_free(meth); |
405 |
+ } |
406 |
+} |
407 |
+ |
408 |
+int RSA_bits(const RSA *r) |
409 |
+{ |
410 |
+ return (BN_num_bits(r->n)); |
411 |
+} |
412 |
+ |
413 |
+RSA *EVP_PKEY_get0_RSA(EVP_PKEY *pkey) |
414 |
+{ |
415 |
+ if (pkey->type != EVP_PKEY_RSA) { |
416 |
+ return NULL; |
417 |
+ } |
418 |
+ return pkey->pkey.rsa; |
419 |
+} |
420 |
+ |
421 |
+#endif |
422 |
+ |