| /* |
| * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved. |
| * |
| * Licensed under the OpenSSL license (the "License"). You may not use |
| * this file except in compliance with the License. You can obtain a copy |
| * in the file LICENSE in the source distribution or at |
| * https://www.openssl.org/source/license.html |
| */ |
| |
| #include <openssl/bn.h> |
| #include <openssl/dh.h> |
| |
| |
| struct wrapped_callback { |
| void (*callback)(int, int, void *); |
| void *arg; |
| }; |
| |
| // callback_wrapper converts an “old” style generation callback to the newer |
| // |BN_GENCB| form. |
| static int callback_wrapper(int event, int n, BN_GENCB *gencb) { |
| struct wrapped_callback *wrapped = (struct wrapped_callback *) gencb->arg; |
| wrapped->callback(event, n, wrapped->arg); |
| return 1; |
| } |
| |
| DH *DH_generate_parameters(int prime_len, int generator, |
| void (*callback)(int, int, void *), void *cb_arg) { |
| if (prime_len < 0 || generator < 0) { |
| return NULL; |
| } |
| |
| DH *ret = DH_new(); |
| if (ret == NULL) { |
| return NULL; |
| } |
| |
| BN_GENCB gencb_storage; |
| BN_GENCB *cb = NULL; |
| |
| struct wrapped_callback wrapped; |
| |
| if (callback != NULL) { |
| wrapped.callback = callback; |
| wrapped.arg = cb_arg; |
| |
| cb = &gencb_storage; |
| BN_GENCB_set(cb, callback_wrapper, &wrapped); |
| } |
| |
| if (!DH_generate_parameters_ex(ret, prime_len, generator, cb)) { |
| goto err; |
| } |
| |
| return ret; |
| |
| err: |
| DH_free(ret); |
| return NULL; |
| } |