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/dsa.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
DSA_generate_parameters(int bits,uint8_t * seed_in,int seed_len,int * counter_ret,unsigned long * h_ret,void (* callback)(int,int,void *),void * cb_arg)27 DSA *DSA_generate_parameters(int bits, uint8_t *seed_in, int seed_len,
28 int *counter_ret, unsigned long *h_ret,
29 void (*callback)(int, int, void *), void *cb_arg) {
30 if (bits < 0 || seed_len < 0) {
31 return NULL;
32 }
33
34 DSA *ret = DSA_new();
35 if (ret == NULL) {
36 return NULL;
37 }
38
39 BN_GENCB gencb_storage;
40 BN_GENCB *cb = NULL;
41
42 struct wrapped_callback wrapped;
43
44 if (callback != NULL) {
45 wrapped.callback = callback;
46 wrapped.arg = cb_arg;
47
48 cb = &gencb_storage;
49 BN_GENCB_set(cb, callback_wrapper, &wrapped);
50 }
51
52 if (!DSA_generate_parameters_ex(ret, bits, seed_in, seed_len, counter_ret,
53 h_ret, cb)) {
54 goto err;
55 }
56
57 return ret;
58
59 err:
60 DSA_free(ret);
61 return NULL;
62 }
63