Skip to content

Commit

Permalink
Merge pull request #973 from Ant-lib/master
Browse files Browse the repository at this point in the history
Fix build with openssl-1.1.0
  • Loading branch information
killing committed Nov 21, 2017
2 parents f659e11 + 6c94659 commit 10feb60
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/ccnet-init.cpp
Expand Up @@ -136,7 +136,7 @@ int make_config_dir(const char *ccnet_dir)

int create_ccnet_config (const char *ccnet_dir)
{
SSLeay_add_all_algorithms();
OpenSSL_add_all_algorithms();

g_assert (RAND_status() == 1);

Expand Down
57 changes: 51 additions & 6 deletions src/utils/rsa.cpp
Expand Up @@ -7,9 +7,51 @@
#include <cstring>

#include "rsa.h"
#include "utils.h"

namespace {

/* Forward compatibility functions if libssl < 1.1.0. */

#if OPENSSL_VERSION_NUMBER < 0x10100000L

int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
{
/* If the fields n and e in r are NULL, the corresponding input
* parameters MUST be non-NULL for n and e. d may be
* left NULL (in case only the public key is used).
*/
if ((r->n == NULL && n == NULL)
|| (r->e == NULL && e == NULL))
return 0;
if (n != NULL) {
BN_free(r->n);
r->n = n;
}
if (e != NULL) {
BN_free(r->e);
r->e = e;
}
if (d != NULL) {
BN_free(r->d);
r->d = d;
}
return 1;
}

void RSA_get0_key(const RSA *r,
const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
{
if (n != NULL)
*n = r->n;
if (e != NULL)
*e = r->e;
if (d != NULL)
*d = r->d;
}

#endif

int calculate_sha1 (unsigned char *sha1, const char *msg)
{
SHA_CTX c;
Expand Down Expand Up @@ -41,18 +83,20 @@ GString* public_key_to_gstring(const RSA *rsa)
GString *buf = g_string_new(NULL);
unsigned char *temp;
char *coded;
const BIGNUM *n, *e;

gsize len = BN_num_bytes(rsa->n);
RSA_get0_key (rsa, &n, &e, NULL);
gsize len = BN_num_bytes(n);
temp = (unsigned char *)malloc(len);
BN_bn2bin(rsa->n, temp);
BN_bn2bin(n, temp);
coded = g_base64_encode(temp, len);
g_string_append (buf, coded);
g_string_append_c (buf, ' ');
g_free(coded);

len = BN_num_bytes(rsa->e);
len = BN_num_bytes(e);
temp = (unsigned char*)realloc(temp, len);
BN_bn2bin(rsa->e, temp);
BN_bn2bin(e, temp);
coded = g_base64_encode(temp, len);
g_string_append (buf, coded);
g_free(coded);
Expand Down Expand Up @@ -106,9 +150,10 @@ RSA*
private_key_to_pub(RSA *priv)
{
RSA *pub = RSA_new();
const BIGNUM *n, *e;

pub->n = BN_dup(priv->n);
pub->e = BN_dup(priv->e);
RSA_get0_key (priv, &n, &e, NULL);
RSA_set0_key (pub, BN_dup(n), BN_dup(e), NULL);

return pub;
}
Expand Down

0 comments on commit 10feb60

Please sign in to comment.