Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenSSL 1.1 compatibility #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 54 additions & 9 deletions crypto/rsa_pem_openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,71 @@
#include "meta.h"
#include "rsa_pem.h"

#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

TGLC_WRAPPER_ASSOC(rsa,RSA)

// TODO: Refactor crucial struct-identity into its own header.
TGLC_WRAPPER_ASSOC(bn,BIGNUM)

TGLC_rsa *TGLC_rsa_new (unsigned long e, int n_bytes, const unsigned char *n) {
RSA *ret = RSA_new ();
ret->e = unwrap_bn (TGLC_bn_new ());
TGLC_bn_set_word (wrap_bn (ret->e), e);
ret->n = unwrap_bn (TGLC_bn_bin2bn (n, n_bytes, NULL));
TGLC_bn* e_tglcbn = TGLC_bn_new ();
TGLC_bn_set_word (e_tglcbn, e);
RSA_set0_key(ret, unwrap_bn (TGLC_bn_bin2bn (n, n_bytes, NULL)), unwrap_bn(e_tglcbn), NULL);
return wrap_rsa (ret);
}

#define RSA_GETTER(M) \
TGLC_bn *TGLC_rsa_ ## M (TGLC_rsa *key) { \
return wrap_bn (unwrap_rsa (key)->M); \
} \
TGLC_bn *TGLC_rsa_n (TGLC_rsa * key) {
const BIGNUM *n;
RSA_get0_key( unwrap_rsa(key), &n, NULL, NULL);
return wrap_bn(n);
}

RSA_GETTER(n);
RSA_GETTER(e);
TGLC_bn *TGLC_rsa_e (TGLC_rsa * key) {
const BIGNUM *e;
RSA_get0_key( unwrap_rsa(key), NULL, &e, NULL);
return wrap_bn(e);
}

void TGLC_rsa_free (TGLC_rsa *p) {
RSA_free (unwrap_rsa (p));
Expand Down