2016-05-18 02:51:34 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2002-12-08 13:24:31 +08:00
|
|
|
*
|
2016-05-18 02:51:34 +08:00
|
|
|
* 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
|
2002-12-08 13:24:31 +08:00
|
|
|
*/
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* NB: This file contains deprecated functions (compatibility wrappers to the
|
|
|
|
* "new" versions).
|
|
|
|
*/
|
2002-12-08 13:24:31 +08:00
|
|
|
|
2016-01-05 12:00:33 +08:00
|
|
|
#include <openssl/opensslconf.h>
|
|
|
|
#if OPENSSL_API_COMPAT >= 0x00908000L
|
2016-02-01 02:08:23 +08:00
|
|
|
NON_EMPTY_TRANSLATION_UNIT
|
2003-10-29 12:06:50 +08:00
|
|
|
|
2004-04-26 23:31:35 +08:00
|
|
|
#else
|
|
|
|
|
2016-02-01 02:08:23 +08:00
|
|
|
# include <stdio.h>
|
|
|
|
# include <time.h>
|
|
|
|
# include "internal/cryptlib.h"
|
|
|
|
# include <openssl/bn.h>
|
|
|
|
# include <openssl/rsa.h>
|
|
|
|
|
2002-12-08 13:24:31 +08:00
|
|
|
RSA *RSA_generate_key(int bits, unsigned long e_value,
|
2015-01-22 11:40:55 +08:00
|
|
|
void (*callback) (int, int, void *), void *cb_arg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
BN_GENCB *cb = BN_GENCB_new();
|
|
|
|
RSA *rsa = RSA_new();
|
|
|
|
BIGNUM *e = BN_new();
|
2002-12-08 13:24:31 +08:00
|
|
|
|
2015-10-30 19:12:26 +08:00
|
|
|
if (cb == NULL || rsa == NULL || e == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
2004-04-26 23:31:35 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* The problem is when building with 8, 16, or 32 BN_ULONG, unsigned long
|
|
|
|
* can be larger
|
|
|
|
*/
|
|
|
|
for (i = 0; i < (int)sizeof(unsigned long) * 8; i++) {
|
|
|
|
if (e_value & (1UL << i))
|
|
|
|
if (BN_set_bit(e, i) == 0)
|
|
|
|
goto err;
|
|
|
|
}
|
2002-12-08 13:24:31 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_GENCB_set_old(cb, callback, cb_arg);
|
2002-12-08 13:24:31 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (RSA_generate_key_ex(rsa, bits, e, cb)) {
|
|
|
|
BN_free(e);
|
|
|
|
BN_GENCB_free(cb);
|
|
|
|
return rsa;
|
|
|
|
}
|
|
|
|
err:
|
2015-05-01 09:37:06 +08:00
|
|
|
BN_free(e);
|
2015-03-24 22:17:37 +08:00
|
|
|
RSA_free(rsa);
|
2015-05-01 09:37:06 +08:00
|
|
|
BN_GENCB_free(cb);
|
2015-01-22 11:40:55 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2003-10-29 12:06:50 +08:00
|
|
|
#endif
|