1 // Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <openssl/bn.h>
16 #include <openssl/dsa.h>
17 
18 
19 struct wrapped_callback {
20   void (*callback)(int, int, void *);
21   void *arg;
22 };
23 
24 // callback_wrapper converts an “old” style generation callback to the newer
25 // |BN_GENCB| form.
callback_wrapper(int event,int n,BN_GENCB * gencb)26 static int callback_wrapper(int event, int n, BN_GENCB *gencb) {
27   struct wrapped_callback *wrapped = (struct wrapped_callback *) gencb->arg;
28   wrapped->callback(event, n, wrapped->arg);
29   return 1;
30 }
31 
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)32 DSA *DSA_generate_parameters(int bits, uint8_t *seed_in, int seed_len,
33                              int *counter_ret, unsigned long *h_ret,
34                              void (*callback)(int, int, void *), void *cb_arg) {
35   if (bits < 0 || seed_len < 0) {
36       return nullptr;
37   }
38 
39   bssl::UniquePtr<DSA> ret(DSA_new());
40   if (ret == nullptr) {
41     return nullptr;
42   }
43 
44   BN_GENCB gencb_storage;
45   BN_GENCB *cb = nullptr;
46   struct wrapped_callback wrapped;
47   if (callback != nullptr) {
48     wrapped.callback = callback;
49     wrapped.arg = cb_arg;
50 
51     cb = &gencb_storage;
52     BN_GENCB_set(cb, callback_wrapper, &wrapped);
53   }
54 
55   if (!DSA_generate_parameters_ex(ret.get(), bits, seed_in, seed_len,
56                                   counter_ret, h_ret, cb)) {
57     return nullptr;
58   }
59 
60   return ret.release();
61 }
62