diff options
Diffstat (limited to 'src/analysis/db/keymgn.c')
-rw-r--r-- | src/analysis/db/keymgn.c | 121 |
1 files changed, 117 insertions, 4 deletions
diff --git a/src/analysis/db/keymgn.c b/src/analysis/db/keymgn.c index bcd8d28..bce2ce8 100644 --- a/src/analysis/db/keymgn.c +++ b/src/analysis/db/keymgn.c @@ -24,12 +24,14 @@ #include "keymgn.h" +#include <assert.h> #include <glib.h> #include <malloc.h> #include <stdio.h> #include <unistd.h> +#include <openssl/err.h> #include <openssl/evp.h> -#include <openssl/rsa.h> +#include <openssl/pem.h> #include <i18n.h> @@ -79,7 +81,7 @@ bool ensure_user_has_rsa_keys(void) result = generate_user_rsa_keys(priv, pub); if (!result) - fprintf(stderr, _("Unable to create new user RSA key pair.")); + fprintf(stderr, _("Unable to create new user RSA key pair.\n")); } @@ -110,7 +112,6 @@ static bool generate_user_rsa_keys(const char *priv, const char *pub) EVP_PKEY_CTX *ctx; /* Contexte de génération */ int ret; /* Bilan d'un appel */ EVP_PKEY *pair; /* Paire de clefs RSA générée */ - char *filename; /* Chemin d'accès */ FILE *stream; /* Flux ouvert en écriture */ result = false; @@ -125,7 +126,7 @@ static bool generate_user_rsa_keys(const char *priv, const char *pub) ret = EVP_PKEY_keygen_init(ctx); if (ret != 1) goto euhrk_exit; - ret = EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, 2048); + ret = EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, RSA_USED_SIZE * 8); if (ret != 1) goto euhrk_exit; ret = EVP_PKEY_keygen(ctx, &pair); @@ -164,3 +165,115 @@ static bool generate_user_rsa_keys(const char *priv, const char *pub) return result; } + + +/****************************************************************************** +* * +* Paramètres : filename = chemin d'accès à la clef à charger. * +* private = nature de la clef visée. * +* * +* Description : Charge une clef RSA à partir d'un fichier PEM. * +* * +* Retour : Clef RSA ou NULL en cas de soucis. * +* * +* Remarques : - * +* * +******************************************************************************/ + +RSA *load_rsa_key(const char *filename, bool private) +{ + RSA *result; /* Clef à retourner */ + FILE *stream; /* Flux ouvert en lecture */ + int bits; /* Taille de la clef en bits */ + + result = NULL; + + stream = fopen(filename, "r"); + if (stream == NULL) goto lrk_exit; + + if (private) + result = PEM_read_RSAPrivateKey(stream, &result, NULL, NULL); + else + result = PEM_read_RSA_PUBKEY(stream, &result, NULL, NULL); + + fclose(stream); + + if (result == NULL) + fprintf(stderr, _("Unable to read the RSA key from '%s'.\n"), filename); + + else + { + bits = RSA_size(result); + + if (bits != RSA_USED_SIZE) + { + fprintf(stderr, _("Wrong RSA key size for %s: expected %d, got %d.\n"), filename, RSA_USED_SIZE, bits); + RSA_free(result); + result = NULL; + } + + } + + lrk_exit: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : key = clef RSA à utiliser. * +* hash = empreinte à signer. * +* sig = signature calculée. * +* * +* Description : Signe une empreinte MD5 à l'aide d'une clef RSA. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool sign_md5_hash(RSA *key, const unsigned char *hash, unsigned char *sig) +{ + int ret; /* Bilan de l'opération */ + unsigned int siglen; /* Taille de la signature */ + + siglen = RSA_USED_SIZE; + + ret = RSA_sign(NID_md5, hash, 16, sig, &siglen, key); + + assert(siglen == RSA_USED_SIZE); + + if (ret != 1) + fprintf(stderr, "Unable to sign hash (error=%lu).\n", ERR_get_error()); + + return (ret == 1); + +} + + +/****************************************************************************** +* * +* Paramètres : key = clef RSA à utiliser. * +* hash = empreinte à signer. * +* sig = signature calculée. * +* * +* Description : Vérifie la signature d'une empreinte MD5 avec une clef RSA. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool verify_md5_hash(RSA *key, const unsigned char *hash, unsigned char *sig) +{ + int ret; /* Bilan de l'opération */ + + ret = RSA_verify(NID_md5, hash, 16, sig, RSA_USED_SIZE, key); + + return (ret == 1); + +} |