X-Git-Url: https://www.tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fopenssl%2Frsa.c;h=3543df3050f3d7decaa7ee2575a6c8e831ce426f;hb=5822f817aa802c2c5a83e9d99a8ae78cb822799b;hp=20bfb65a072b2926e26a1e21191c32dcd3c0aa2b;hpb=9b9230a0a79c670b86f54fadd2807b864ff9d91f;p=tinc diff --git a/src/openssl/rsa.c b/src/openssl/rsa.c index 20bfb65a..3543df30 100644 --- a/src/openssl/rsa.c +++ b/src/openssl/rsa.c @@ -22,7 +22,7 @@ #include #include -#define __TINC_RSA_INTERNAL__ +#define TINC_RSA_INTERNAL typedef RSA rsa_t; #include "../logger.h" @@ -30,28 +30,51 @@ typedef RSA rsa_t; // Set RSA keys +#ifndef HAVE_RSA_SET0_KEY +int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) { + BN_free(r->n); r->n = n; + BN_free(r->e); r->e = e; + BN_free(r->d); r->d = d; + return 1; +} +#endif + rsa_t *rsa_set_hex_public_key(char *n, char *e) { - rsa_t *rsa = RSA_new(); - if(!rsa) - return NULL; + BIGNUM *bn_n = NULL; + BIGNUM *bn_e = NULL; - if(BN_hex2bn(&rsa->n, n) != strlen(n) || BN_hex2bn(&rsa->e, e) != strlen(e)) { - RSA_free(rsa); + if(BN_hex2bn(&bn_n, n) != strlen(n) || BN_hex2bn(&bn_e, e) != strlen(e)) { + BN_free(bn_e); + BN_free(bn_n); return false; } + rsa_t *rsa = RSA_new(); + if(!rsa) + return NULL; + + RSA_set0_key(rsa, bn_n, bn_e, NULL); + return rsa; } rsa_t *rsa_set_hex_private_key(char *n, char *e, char *d) { + BIGNUM *bn_n = NULL; + BIGNUM *bn_e = NULL; + BIGNUM *bn_d = NULL; + + if(BN_hex2bn(&bn_n, n) != strlen(n) || BN_hex2bn(&bn_e, e) != strlen(e) || BN_hex2bn(&bn_d, d) != strlen(d)) { + BN_free(bn_d); + BN_free(bn_e); + BN_free(bn_n); + return false; + } + rsa_t *rsa = RSA_new(); if(!rsa) return NULL; - if(BN_hex2bn(&rsa->n, n) != strlen(n) || BN_hex2bn(&rsa->e, e) != strlen(e) || BN_hex2bn(&rsa->d, d) != strlen(d)) { - RSA_free(rsa); - return false; - } + RSA_set0_key(rsa, bn_n, bn_e, bn_d); return rsa; } @@ -61,8 +84,10 @@ rsa_t *rsa_set_hex_private_key(char *n, char *e, char *d) { rsa_t *rsa_read_pem_public_key(FILE *fp) { rsa_t *rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL); - if(!rsa) + if(!rsa) { + rewind(fp); rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL); + } if(!rsa) logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read RSA public key: %s", ERR_error_string(ERR_get_error(), NULL));