1 /*
2 * Copyright 2002-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 <openssl/bn.h>
11 #include <openssl/dh.h>
12
13
14 struct wrapped_callback {
15 void (*callback)(int, int, void *);
16 void *arg;
17 };
18
19 // callback_wrapper converts an “old” style generation callback to the newer
20 // |BN_GENCB| form.
callback_wrapper(int event,int n,BN_GENCB * gencb)21 static int callback_wrapper(int event, int n, BN_GENCB *gencb) {
22 struct wrapped_callback *wrapped = (struct wrapped_callback *) gencb->arg;
23 wrapped->callback(event, n, wrapped->arg);
24 return 1;
25 }
26
DH_generate_parameters(int prime_len,int generator,void (* callback)(int,int,void *),void * cb_arg)27 DH *DH_generate_parameters(int prime_len, int generator,
28 void (*callback)(int, int, void *), void *cb_arg) {
29 if (prime_len < 0 || generator < 0) {
30 return NULL;
31 }
32
33 DH *ret = DH_new();
34 if (ret == NULL) {
35 return NULL;
36 }
37
38 BN_GENCB gencb_storage;
39 BN_GENCB *cb = NULL;
40
41 struct wrapped_callback wrapped;
42
43 if (callback != NULL) {
44 wrapped.callback = callback;
45 wrapped.arg = cb_arg;
46
47 cb = &gencb_storage;
48 BN_GENCB_set(cb, callback_wrapper, &wrapped);
49 }
50
51 if (!DH_generate_parameters_ex(ret, prime_len, generator, cb)) {
52 goto err;
53 }
54
55 return ret;
56
57 err:
58 DH_free(ret);
59 return NULL;
60 }
61