summaryrefslogtreecommitdiff
path: root/src/analysis/db/keymgn.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/analysis/db/keymgn.c')
-rw-r--r--src/analysis/db/keymgn.c429
1 files changed, 429 insertions, 0 deletions
diff --git a/src/analysis/db/keymgn.c b/src/analysis/db/keymgn.c
index 9c35289..e1afdcf 100644
--- a/src/analysis/db/keymgn.c
+++ b/src/analysis/db/keymgn.c
@@ -24,6 +24,435 @@
#include "keymgn.h"
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+#include "../../common/extstr.h"
+#include "../../common/pathname.h"
+#include "../../common/xdg.h"
+#include "../../core/params.h"
+
+
+
+/* Mémorise en mémoire la définition d'une identité courante. */
+static void store_identity(const x509_entries *, bool);
+
+/* Fournit le répertoire de travail pour les certifications. */
+static char *get_cert_working_directory(const char *, const char *);
+
+
+
+/******************************************************************************
+* *
+* Paramètres : client = précise la nature de l'identité à représenter. *
+* entries = éléments d'identité à définir en mémoire. [OUT] *
+* *
+* Description : Charge en mémoire la définition de l'identité courante. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void load_identity(bool client, x509_entries *entries)
+{
+ GGenConfig *config; /* Configuration à manipuler */
+ const char *path; /* Accès à la configuration */
+ char *saved; /* Valeur sauvegardée */
+ char *username; /* Dénomination affichée */
+ char *lang; /* Langage de l'utilisateur */
+ size_t length; /* Taille de valeur récupérée */
+ size_t first; /* Indice de première majuscule*/
+ size_t i; /* Boucle de parcours */
+
+ memset(entries, 0, sizeof(x509_entries));
+
+ /* Chargement à partir des sauvegardes */
+
+ config = get_main_configuration();
+
+ path = (client ? MPK_IDENTITY_CLIENT_C : MPK_IDENTITY_SERVER_C);
+
+ if (g_generic_config_get_value(get_main_configuration(), path, &saved))
+ entries->country = (saved != NULL ? strdup(saved) : NULL);
+
+ path = (client ? MPK_IDENTITY_CLIENT_ST : MPK_IDENTITY_SERVER_ST);
+
+ if (g_generic_config_get_value(get_main_configuration(), path, &saved))
+ entries->state = (saved != NULL ? strdup(saved) : NULL);
+
+ path = (client ? MPK_IDENTITY_CLIENT_L : MPK_IDENTITY_SERVER_L);
+
+ if (g_generic_config_get_value(get_main_configuration(), path, &saved))
+ entries->locality = (saved != NULL ? strdup(saved) : NULL);
+
+ path = (client ? MPK_IDENTITY_CLIENT_O : MPK_IDENTITY_SERVER_O);
+
+ if (g_generic_config_get_value(get_main_configuration(), path, &saved))
+ entries->organisation = (saved != NULL ? strdup(saved) : NULL);
+
+ path = (client ? MPK_IDENTITY_CLIENT_OU : MPK_IDENTITY_SERVER_OU);
+
+ if (g_generic_config_get_value(get_main_configuration(), path, &saved))
+ entries->organisational_unit = (saved != NULL ? strdup(saved) : NULL);
+
+ path = (client ? MPK_IDENTITY_CLIENT_CN : MPK_IDENTITY_SERVER_CN);
+
+ if (g_generic_config_get_value(get_main_configuration(), path, &saved))
+ entries->common_name = (saved != NULL ? strdup(saved) : NULL);
+
+ /* Si les valeurs n'étaient pas définies... */
+
+ if (are_x509_entries_empty(entries))
+ {
+ /* Identification de l'utilisateur */
+
+ username = getenv("USERNAME");
+
+ if (username == NULL)
+ username = getenv("USER");
+
+ if (username == NULL)
+ username = getenv("LOGNAME");
+
+ if (username != NULL)
+ entries->common_name = strdup(username);
+ else
+ entries->common_name = strdup("???");
+
+ /* Identification du pays */
+
+ lang = getenv("LANG");
+
+ if (lang != NULL)
+ {
+ length = strlen(lang);
+
+ first = length;
+
+ for (i = 0; i < length; i++)
+ {
+ if (isupper(lang[i]))
+ {
+ if (first == length)
+ first = i;
+ }
+
+ else if (first != length)
+ {
+ entries->country = strndup(&lang[first], i - first);
+ break;
+ }
+
+ }
+
+ }
+
+ if (entries->country == NULL)
+ entries->country = strdup("??");
+
+ }
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : entries = éléments d'identité à conserver en mémoire. *
+* client = précise la nature de l'identité représentée. *
+* *
+* Description : Mémorise en mémoire la définition d'une identité courante. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static void store_identity(const x509_entries *entries, bool client)
+{
+ GGenConfig *config; /* Configuration à manipuler */
+ const char *path; /* Accès à la configuration */
+
+ config = get_main_configuration();
+
+ path = (client ? MPK_IDENTITY_CLIENT_C : MPK_IDENTITY_SERVER_C);
+
+ g_generic_config_set_value(config, path, entries->country);
+
+ path = (client ? MPK_IDENTITY_CLIENT_ST : MPK_IDENTITY_SERVER_ST);
+
+ g_generic_config_set_value(config, path, entries->state);
+
+ path = (client ? MPK_IDENTITY_CLIENT_L : MPK_IDENTITY_SERVER_L);
+
+ g_generic_config_set_value(config, path, entries->locality);
+
+ path = (client ? MPK_IDENTITY_CLIENT_O : MPK_IDENTITY_SERVER_O);
+
+ g_generic_config_set_value(config, path, entries->organisation);
+
+ path = (client ? MPK_IDENTITY_CLIENT_OU : MPK_IDENTITY_SERVER_OU);
+
+ g_generic_config_set_value(config, path, entries->organisational_unit);
+
+ path = (client ? MPK_IDENTITY_CLIENT_CN : MPK_IDENTITY_SERVER_CN);
+
+ g_generic_config_set_value(config, path, entries->common_name);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : type = type de certificat à gérer. *
+* name = dénomination du serveur visé. *
+* *
+* Description : Fournit le répertoire de travail pour les certifications. *
+* *
+* Retour : Définition d'emplacement à libérer de la mémoire après usage.*
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static char *get_cert_working_directory(const char *type, const char *name)
+{
+ char *result; /* Chemin à retourner */
+ char *suffix; /* Fin de la destination */
+
+ suffix = strdup("chrysalide");
+ suffix = stradd(suffix, G_DIR_SEPARATOR_S);
+ suffix = stradd(suffix, type);
+ suffix = stradd(suffix, G_DIR_SEPARATOR_S);
+ suffix = stradd(suffix, name);
+ suffix = stradd(suffix, G_DIR_SEPARATOR_S);
+
+ result = get_xdg_config_dir(suffix);
+
+ free(suffix);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : entries = éléments d'identité à utiliser pour l'opération. *
+* *
+* Description : Définit les certificats utilisés pour les échanges internes. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool register_standalone_certs(const x509_entries *entries)
+{
+ bool result; /* Bilan de l'opération */
+ char *working; /* Répertoire par le client */
+ char *csr; /* Requête de signature */
+ char *cert; /* Certificat signé en sortie */
+
+ /* Certificats côtés serveur */
+
+ result = register_server_cert("standalone", entries);
+
+ if (result)
+ {
+ /* Demande de signature */
+
+ result = make_client_sign_request("standalone", entries);
+
+ /* Signature */
+
+ if (result)
+ {
+ working = get_cert_working_directory("clients", "standalone");
+
+ csr = build_absolute_filename(working, "client-csr.pem");
+ cert = build_absolute_filename(working, "client-cert.pem");
+
+ result = sign_client_request("standalone", csr, cert);
+
+ free(csr);
+ free(cert);
+
+ free(working);
+
+ }
+
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : name = dénomination du serveur visé. *
+* entries = éléments d'identité à utiliser pour l'opération. *
+* *
+* Description : Définit les certificats utilisés pour par un serveur. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool register_server_cert(const char *name, const x509_entries *entries)
+{
+ bool result; /* Bilan de l'opération */
+ char *suffix; /* Fin de la destination */
+ char *working; /* Répertoire de travail */
+ unsigned long valid; /* Durée de validité */
+ char *csr; /* Requête de signature */
+ char *cacert; /* Certificat d'autorité */
+ char *cakey; /* Clef de cette autorité */
+ char *cert; /* Certificat signé en sortie */
+
+ result = false;
+
+ working = get_cert_working_directory("servers", name);
+
+ if (working != NULL)
+ {
+ result = mkpath(working);
+ if (!result) goto rsc_quick_exit;
+
+ result = g_generic_config_get_value(get_main_configuration(), MPK_IDENTITY_VALIDITY, &valid);
+
+ if (!result)
+ goto rsc_quick_exit;
+
+ result = make_ca(working, "ca", valid, entries);
+ if (!result) goto rsc_quick_exit;
+
+ result = make_request(working, "server", entries);
+ if (!result) goto rsc_quick_exit;
+
+ csr = build_absolute_filename(working, "server-csr.pem");
+ cacert = build_absolute_filename(working, "ca-cert.pem");
+ cakey = build_absolute_filename(working, "ca-key.pem");
+ cert = build_absolute_filename(working, "server-cert.pem");
+
+ result = sign_cert(csr, cacert, cakey, cert, valid);
+
+ if (result)
+ store_identity(entries, false);
+
+ free(csr);
+ free(cacert);
+ free(cakey);
+ free(cert);
+
+ rsc_quick_exit:
+
+ free(working);
+
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : name = dénomination du serveur visé. *
+* entries = éléments d'identité à utiliser pour l'opération. *
+* *
+* Description : Elabore une demande de signature de certificat. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool make_client_sign_request(const char *name, const x509_entries *entries)
+{
+ bool result; /* Bilan de l'opération */
+ char *working; /* Répertoire par le client */
+
+ working = get_cert_working_directory("clients", name);
+
+ result = mkpath(working);
+
+ if (result)
+ result = make_request(working, "client", entries);
+
+ if (result)
+ store_identity(entries, true);
+
+ free(working);
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : name = dénomination du serveur visé. *
+* csr = fichier contenant le certificat à signer. *
+* cert = fichier contenant le certificat signé. *
+* *
+* Description : Signe un certificat client pour un accès un serveur donné. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool sign_client_request(const char *name, const char *csr, const char *cert)
+{
+ bool result; /* Bilan de l'opération */
+ char *working; /* Répertoire par le serveur */
+ unsigned long valid; /* Durée de validité */
+ char *cacert; /* Certificat d'autorité */
+ char *cakey; /* Clef de cette autorité */
+
+ working = get_cert_working_directory("servers", name);
+
+ result = g_generic_config_get_value(get_main_configuration(), MPK_IDENTITY_VALIDITY, &valid);
+
+ if (!result)
+ goto scr_exit;
+
+ cacert = build_absolute_filename(working, "ca-cert.pem");
+ cakey = build_absolute_filename(working, "ca-key.pem");
+
+ result = sign_cert(csr, cacert, cakey, cert, valid);
+
+ free(cacert);
+ free(cakey);
+
+ scr_exit:
+
+ free(working);
+
+ return result;
+
+}
+
+
+
+
+
+
+
+
+
+
#include <assert.h>
#include <glib.h>
#include <malloc.h>