X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fopenssl%2Frsa.c;h=543262e4bb5ab016908c1ec9d4b17cedac6ad88a;hb=2a37712b0d3d5c441424cf1fac6c95f7c76cc709;hp=9c1f4984ba1f4fbbddf0715643ea37a4a10fa7f4;hpb=09e000ba54fd4a4ffe3e5c15ee7aeadac35d6996;p=tinc diff --git a/src/openssl/rsa.c b/src/openssl/rsa.c index 9c1f4984..543262e4 100644 --- a/src/openssl/rsa.c +++ b/src/openssl/rsa.c @@ -1,6 +1,6 @@ /* rsa.c -- RSA key handling - Copyright (C) 2007-2013 Guus Sliepen + Copyright (C) 2007-2021 Guus Sliepen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,8 +21,9 @@ #include #include +#include -#define __TINC_RSA_INTERNAL__ +#define TINC_RSA_INTERNAL typedef RSA rsa_t; #include "../logger.h" @@ -31,28 +32,46 @@ typedef RSA rsa_t; // Set RSA keys 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((size_t)BN_hex2bn(&bn_n, n) != strlen(n) || (size_t)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((size_t)BN_hex2bn(&bn_n, n) != strlen(n) || (size_t)BN_hex2bn(&bn_e, e) != strlen(e) || (size_t)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; + if(!rsa) { + return NULL; } + RSA_set0_key(rsa, bn_n, bn_e, bn_d); + return rsa; } @@ -66,8 +85,9 @@ rsa_t *rsa_read_pem_public_key(FILE *fp) { rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL); } - if(!rsa) + if(!rsa) { logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read RSA public key: %s", ERR_error_string(ERR_get_error(), NULL)); + } return rsa; } @@ -75,27 +95,30 @@ rsa_t *rsa_read_pem_public_key(FILE *fp) { rsa_t *rsa_read_pem_private_key(FILE *fp) { rsa_t *rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL); - if(!rsa) + if(!rsa) { logger(DEBUG_ALWAYS, LOG_ERR, "Unable to read RSA private key: %s", ERR_error_string(ERR_get_error(), NULL)); + } return rsa; } -size_t rsa_size(rsa_t *rsa) { +size_t rsa_size(const rsa_t *rsa) { return RSA_size(rsa); } bool rsa_public_encrypt(rsa_t *rsa, void *in, size_t len, void *out) { - if(RSA_public_encrypt(len, in, out, rsa, RSA_NO_PADDING) == len) + if((size_t)RSA_public_encrypt(len, in, out, rsa, RSA_NO_PADDING) == len) { return true; + } logger(DEBUG_ALWAYS, LOG_ERR, "Unable to perform RSA encryption: %s", ERR_error_string(ERR_get_error(), NULL)); return false; } bool rsa_private_decrypt(rsa_t *rsa, void *in, size_t len, void *out) { - if(RSA_private_decrypt(len, in, out, rsa, RSA_NO_PADDING) == len) + if((size_t)RSA_private_decrypt(len, in, out, rsa, RSA_NO_PADDING) == len) { return true; + } logger(DEBUG_ALWAYS, LOG_ERR, "Unable to perform RSA decryption: %s", ERR_error_string(ERR_get_error(), NULL)); return false; @@ -106,6 +129,7 @@ bool rsa_active(rsa_t *rsa) { } void rsa_free(rsa_t *rsa) { - if(rsa) + if(rsa) { RSA_free(rsa); + } }