Debian Codegraph @

src/lib/crypto/OSSLComp.cpp

raw
1 /*
2 * Copyright (c) 2016 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 OSSLUtil.cpp
29
30 Adding OpenSSL forward-compatible code as suggested by OpenSSL
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "OSSLComp.h"
35 #include <openssl/opensslv.h>
36
37 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
38
39 /*
40 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
41 *
42 * Licensed under the OpenSSL license (the "License"). You may not use
43 * this file except in compliance with the License. You can obtain a copy
44 * in the file LICENSE in the source distribution or at
45 * https://www.openssl.org/source/license.html
46 */
47
48 #include <openssl/crypto.h>
49 #include <openssl/evp.h>
50 #include <openssl/hmac.h>
51 #include <openssl/dh.h>
52 #include <openssl/dsa.h>
53 #ifdef WITH_ECC
54 #include <openssl/ecdsa.h>
55 #endif
56 #include <openssl/rsa.h>
57
58 #include <string.h>
59
60 // EVP digest routines
61 EVP_MD_CTX *EVP_MD_CTX_new(void)
62 {
63 EVP_MD_CTX *ctx = (EVP_MD_CTX*)OPENSSL_malloc(sizeof *ctx);
64
65 if (ctx)
66 EVP_MD_CTX_init(ctx);
67
68 return ctx;
69 }
70
71 void EVP_MD_CTX_free(EVP_MD_CTX *ctx)
72 {
73 if (ctx)
74 {
75 EVP_MD_CTX_cleanup(ctx);
76 OPENSSL_free(ctx);
77 }
78 }
79
80 // HMAC routines
81 HMAC_CTX *HMAC_CTX_new(void)
82 {
83 HMAC_CTX *ctx = (HMAC_CTX*)OPENSSL_malloc(sizeof(*ctx));
84 if (ctx == NULL) return NULL;
85
86 HMAC_CTX_init(ctx);
87
88 return ctx;
89 }
90
91 void HMAC_CTX_free(HMAC_CTX *ctx)
92 {
93 if (ctx == NULL) return;
94
95 HMAC_CTX_cleanup(ctx);
96 OPENSSL_free(ctx);
97 }
98
99 // DH routines
100 void DH_get0_pqg(const DH *dh,
101 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
102 {
103 if (p != NULL)
104 *p = dh->p;
105 if (q != NULL)
106 *q = dh->q;
107 if (g != NULL)
108 *g = dh->g;
109 }
110
111 int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
112 {
113 /* If the fields p and g in d are NULL, the corresponding input
114 * parameters MUST be non-NULL. q may remain NULL.
115 */
116 if ((dh->p == NULL && p == NULL)
117 || (dh->g == NULL && g == NULL))
118 return 0;
119
120 if (p != NULL)
121 {
122 BN_free(dh->p);
123 dh->p = p;
124 }
125 if (q != NULL)
126 {
127 BN_free(dh->q);
128 dh->q = q;
129 }
130 if (g != NULL)
131 {
132 BN_free(dh->g);
133 dh->g = g;
134 }
135
136 if (q != NULL)
137 {
138 dh->length = BN_num_bits(q);
139 }
140
141 return 1;
142 }
143
144 void DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
145 {
146 if (pub_key != NULL)
147 *pub_key = dh->pub_key;
148 if (priv_key != NULL)
149 *priv_key = dh->priv_key;
150 }
151
152 int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
153 {
154 /* If the field pub_key in dh is NULL, the corresponding input
155 * parameters MUST be non-NULL. The priv_key field may
156 * be left NULL.
157 */
158 if (dh->pub_key == NULL && pub_key == NULL)
159 return 0;
160
161 if (pub_key != NULL)
162 {
163 BN_free(dh->pub_key);
164 dh->pub_key = pub_key;
165 }
166 if (priv_key != NULL)
167 {
168 BN_free(dh->priv_key);
169 dh->priv_key = priv_key;
170 }
171
172 return 1;
173 }
174
175 long DH_get_length(const DH *dh)
176 {
177 return dh->length;
178 }
179
180 int DH_set_length(DH *dh, long length)
181 {
182 dh->length = length;
183
184 return 1;
185 }
186
187 // DSA routines
188 void DSA_get0_pqg(const DSA *d,
189 const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
190 {
191 if (p != NULL)
192 *p = d->p;
193 if (q != NULL)
194 *q = d->q;
195 if (g != NULL)
196 *g = d->g;
197 }
198
199 int DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
200 {
201 /* If the fields p, q and g in d are NULL, the corresponding input
202 * parameters MUST be non-NULL.
203 */
204 if ((d->p == NULL && p == NULL)
205 || (d->q == NULL && q == NULL)
206 || (d->g == NULL && g == NULL))
207 return 0;
208
209 if (p != NULL)
210 {
211 BN_free(d->p);
212 d->p = p;
213 }
214 if (q != NULL)
215 {
216 BN_free(d->q);
217 d->q = q;
218 }
219 if (g != NULL)
220 {
221 BN_free(d->g);
222 d->g = g;
223 }
224
225 return 1;
226 }
227
228 void DSA_get0_key(const DSA *d,
229 const BIGNUM **pub_key, const BIGNUM **priv_key)
230 {
231 if (pub_key != NULL)
232 *pub_key = d->pub_key;
233 if (priv_key != NULL)
234 *priv_key = d->priv_key;
235 }
236
237 int DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
238 {
239 /* If the field pub_key in d is NULL, the corresponding input
240 * parameters MUST be non-NULL. The priv_key field may
241 * be left NULL.
242 */
243 if (d->pub_key == NULL && pub_key == NULL)
244 return 0;
245
246 if (pub_key != NULL)
247 {
248 BN_free(d->pub_key);
249 d->pub_key = pub_key;
250 }
251 if (priv_key != NULL)
252 {
253 BN_free(d->priv_key);
254 d->priv_key = priv_key;
255 }
256
257 return 1;
258 }
259
260 // DSA_SIG routines
261 void DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
262 {
263 if (pr != NULL)
264 *pr = sig->r;
265 if (ps != NULL)
266 *ps = sig->s;
267 }
268
269 int DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
270 {
271 if (r == NULL || s == NULL)
272 return 0;
273 BN_clear_free(sig->r);
274 BN_clear_free(sig->s);
275 sig->r = r;
276 sig->s = s;
277 return 1;
278 }
279
280 // ECDSA_SIG routines
281 #ifdef WITH_ECC
282 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
283 {
284 if (pr != NULL)
285 *pr = sig->r;
286 if (ps != NULL)
287 *ps = sig->s;
288 }
289
290 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
291 {
292 if (r == NULL || s == NULL)
293 return 0;
294 BN_clear_free(sig->r);
295 BN_clear_free(sig->s);
296 sig->r = r;
297 sig->s = s;
298
299 return 1;
300 }
301 #endif
302
303 int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
304 {
305 /* If the fields n and e in r are NULL, the corresponding input
306 * parameters MUST be non-NULL for n and e. d may be
307 * left NULL (in case only the public key is used).
308 */
309 if ((r->n == NULL && n == NULL)
310 || (r->e == NULL && e == NULL))
311 return 0;
312
313 if (n != NULL)
314 {
315 BN_free(r->n);
316 r->n = n;
317 }
318 if (e != NULL)
319 {
320 BN_free(r->e);
321 r->e = e;
322 }
323 if (d != NULL)
324 {
325 BN_free(r->d);
326 r->d = d;
327 }
328
329 return 1;
330 }
331
332 int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
333 {
334 /* If the fields p and q in r are NULL, the corresponding input
335 * parameters MUST be non-NULL.
336 */
337 if ((r->p == NULL && p == NULL)
338 || (r->q == NULL && q == NULL))
339 return 0;
340
341 if (p != NULL)
342 {
343 BN_free(r->p);
344 r->p = p;
345 }
346 if (q != NULL)
347 {
348 BN_free(r->q);
349 r->q = q;
350 }
351
352 return 1;
353 }
354
355 int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
356 {
357 /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
358 * parameters MUST be non-NULL.
359 */
360 if ((r->dmp1 == NULL && dmp1 == NULL)
361 || (r->dmq1 == NULL && dmq1 == NULL)
362 || (r->iqmp == NULL && iqmp == NULL))
363 return 0;
364
365 if (dmp1 != NULL)
366 {
367 BN_free(r->dmp1);
368 r->dmp1 = dmp1;
369 }
370 if (dmq1 != NULL)
371 {
372 BN_free(r->dmq1);
373 r->dmq1 = dmq1;
374 }
375 if (iqmp != NULL)
376 {
377 BN_free(r->iqmp);
378 r->iqmp = iqmp;
379 }
380
381 return 1;
382 }
383
384 void RSA_get0_key(const RSA *r,
385 const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
386 {
387 if (n != NULL)
388 *n = r->n;
389 if (e != NULL)
390 *e = r->e;
391 if (d != NULL)
392 *d = r->d;
393 }
394
395 void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
396 {
397 if (p != NULL)
398 *p = r->p;
399 if (q != NULL)
400 *q = r->q;
401 }
402
403 void RSA_get0_crt_params(const RSA *r,
404 const BIGNUM **dmp1, const BIGNUM **dmq1,
405 const BIGNUM **iqmp)
406 {
407 if (dmp1 != NULL)
408 *dmp1 = r->dmp1;
409 if (dmq1 != NULL)
410 *dmq1 = r->dmq1;
411 if (iqmp != NULL)
412 *iqmp = r->iqmp;
413 }
414
415 #endif