| 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/dh.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. |
| 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 |
| 32 | DH *DH_generate_parameters(int prime_len, int generator, |
| 33 | void (*callback)(int, int, void *), void *cb_arg) { |
| 34 | if (prime_len < 0 || generator < 0) { |
| 35 | return NULL; |
| 36 | } |
| 37 |
| 38 | DH *ret = DH_new(); |
| 39 | if (ret == NULL) { |
| 40 | return NULL; |
| 41 | } |
| 42 |
| 43 | BN_GENCB gencb_storage; |
| 44 | BN_GENCB *cb = NULL; |
| 45 |
| 46 | struct wrapped_callback wrapped; |
| 47 |
| 48 | if (callback != NULL) { |
| 49 | wrapped.callback = callback; |
| 50 | wrapped.arg = cb_arg; |
| 51 |
| 52 | cb = &gencb_storage; |
| 53 | BN_GENCB_set(cb, callback_wrapper, &wrapped); |
| 54 | } |
| 55 |
| 56 | if (!DH_generate_parameters_ex(ret, prime_len, generator, cb)) { |
| 57 | goto err; |
| 58 | } |
| 59 |
| 60 | return ret; |
| 61 |
| 62 | err: |
| 63 | DH_free(ret); |
| 64 | return NULL; |
| 65 | } |
| 66 |