Added SILC Server library.
[silc.git] / lib / silcutil / silcapputil.c
index 4d05f16c4c806822b308c2a85b180d1358a996d5..d1d6739ff751b56e8f8173e93273d18a21f9b1cb 100644 (file)
@@ -1,10 +1,10 @@
 /*
 
-  silcapputil.c 
+  silcapputil.c
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2002 Pekka Riikonen
+  Copyright (C) 2002 - 2005 Pekka Riikonen
 
   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
 */
 /* $Id$ */
 
-#include "silcincludes.h"
+#include "silc.h"
 
 static char *silc_create_pk_identifier(void)
 {
   char *username = NULL, *realname = NULL;
   char *hostname, email[256];
   char *ident;
-  
+
   /* Get realname */
   realname = silc_get_real_name();
 
@@ -54,7 +54,7 @@ static char *silc_create_pk_identifier(void)
 
 /* Generate key pair */
 
-bool silc_create_key_pair(const char *pkcs_name,
+SilcBool silc_create_key_pair(const char *pkcs_name,
                          SilcUInt32 key_len_bits,
                          const char *pub_filename,
                          const char *prv_filename,
@@ -63,7 +63,7 @@ bool silc_create_key_pair(const char *pkcs_name,
                          SilcPKCS *return_pkcs,
                          SilcPublicKey *return_public_key,
                          SilcPrivateKey *return_private_key,
-                         bool interactive)
+                         SilcBool interactive)
 {
   SilcPKCS pkcs;
   SilcPublicKey pub_key;
@@ -89,7 +89,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
        alg = silc_get_input("PKCS name (l to list names) [rsa]: ", FALSE);
        if (!alg)
          alg = strdup("rsa");
-    
+
        if (*alg == 'l' || *alg == 'L') {
          char *list = silc_pkcs_get_supported();
          printf("%s\n", list);
@@ -175,15 +175,29 @@ New pair of keys will be created.  Please, answer to following questions.\n\
   }
 
   if (!pass) {
-    memset(line, 0, sizeof(line));
-    snprintf(line, sizeof(line), "Private key passphrase: ");
-    pass = silc_get_input(line, TRUE);
-    if (!pass)
-      pass = strdup("");
+    while (TRUE) {
+      char *pass2 = NULL;
+      pass = silc_get_input("Private key passphrase: ", TRUE);
+      if (!pass) {
+        pass = strdup("");
+       break;
+      } else {
+       SilcBool match;
+       printf("\n");
+       pass2 = silc_get_input("Retype private key passphrase: ", TRUE);
+       if (!pass2)
+         pass2 = strdup("");
+       match = !strcmp(pass, pass2);
+       silc_free(pass2);
+       if (match)
+         break;
+       fprintf(stderr, "\nPassphrases do not match\n\n");
+      }
+    }
   }
 
   /* Generate keys */
-  silc_pkcs_alloc(alg, &pkcs);
+  silc_pkcs_alloc(alg, SILC_PKCS_SILC, &pkcs);
   silc_pkcs_generate_key(pkcs, key_len_bits, rng);
 
   /* Save public key into file */
@@ -237,7 +251,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
 
 /* Load key pair */
 
-bool silc_load_key_pair(const char *pub_filename,
+SilcBool silc_load_key_pair(const char *pub_filename,
                        const char *prv_filename,
                        const char *passphrase,
                        SilcPKCS *return_pkcs,
@@ -252,7 +266,9 @@ bool silc_load_key_pair(const char *pub_filename,
                                SILC_PKCS_FILE_PEM) == FALSE)
     if (silc_pkcs_load_public_key((char *)pub_filename, return_public_key,
                                  SILC_PKCS_FILE_BIN) == FALSE) {
-      memset(pass, 0, strlen(pass));
+      if (pass)
+       memset(pass, 0, strlen(pass));
+      silc_free(pass);
       return FALSE;
     }
 
@@ -269,22 +285,24 @@ bool silc_load_key_pair(const char *pub_filename,
                                   (unsigned char *)pass, strlen(pass),
                                   SILC_PKCS_FILE_PEM) == FALSE) {
       memset(pass, 0, strlen(pass));
+      silc_free(pass);
       return FALSE;
     }
 
   if (return_pkcs) {
-    silc_pkcs_alloc((*return_public_key)->name, return_pkcs);
+    silc_pkcs_alloc((*return_public_key)->name, SILC_PKCS_SILC, return_pkcs);
     silc_pkcs_public_key_set(*return_pkcs, *return_public_key);
     silc_pkcs_private_key_set(*return_pkcs, *return_private_key);
   }
 
   memset(pass, 0, strlen(pass));
+  silc_free(pass);
   return TRUE;
 }
 
 /* Dump public key into stdout */
 
-bool silc_show_public_key(const char *pub_filename)
+SilcBool silc_show_public_key(const char *pub_filename)
 {
   SilcPublicKey public_key;
   SilcPublicKeyIdentifier ident;
@@ -308,7 +326,7 @@ bool silc_show_public_key(const char *pub_filename)
   fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
   babbleprint = silc_hash_babbleprint(NULL, pk, pk_len);
 
-  if (silc_pkcs_alloc(public_key->name, &pkcs)) {
+  if (silc_pkcs_alloc(public_key->name, SILC_PKCS_SILC, &pkcs)) {
     key_len = silc_pkcs_public_key_set(pkcs, public_key);
     silc_pkcs_free(pkcs);
   }
@@ -329,8 +347,8 @@ bool silc_show_public_key(const char *pub_filename)
     printf("Organization       : %s\n", ident->org);
   if (ident->country)
     printf("Country            : %s\n", ident->country);
-  printf("Fingerprint (SHA1) : %s\n", fingerprint); 
-  printf("Babbleprint (SHA1) : %s\n", babbleprint); 
+  printf("Fingerprint (SHA1) : %s\n", fingerprint);
+  printf("Babbleprint (SHA1) : %s\n", babbleprint);
 
   fflush(stdout);
 
@@ -342,3 +360,71 @@ bool silc_show_public_key(const char *pub_filename)
 
   return TRUE;
 }
+
+/* Change private key passphrase */
+
+SilcBool silc_change_private_key_passphrase(const char *prv_filename,
+                                       const char *old_passphrase,
+                                       const char *new_passphrase)
+{
+  SilcPrivateKey private_key;
+  SilcBool base64 = FALSE;
+  char *pass;
+
+  pass = old_passphrase ? strdup(old_passphrase) : NULL;
+  if (!pass) {
+    pass = silc_get_input("Old passphrase: ", TRUE);
+    if (!pass)
+      pass = strdup("");
+  }
+
+  if (silc_pkcs_load_private_key((char *)prv_filename, &private_key,
+                                (unsigned char *)pass, strlen(pass),
+                                SILC_PKCS_FILE_BIN) == FALSE) {
+    base64 = TRUE;
+    if (silc_pkcs_load_private_key((char *)prv_filename, &private_key,
+                                  (unsigned char *)pass, strlen(pass),
+                                  SILC_PKCS_FILE_PEM) == FALSE) {
+      memset(pass, 0, strlen(pass));
+      silc_free(pass);
+      fprintf(stderr, "Could not load private key `%s' file\n", prv_filename);
+      return FALSE;
+    }
+  }
+
+  memset(pass, 0, strlen(pass));
+  silc_free(pass);
+
+  pass = new_passphrase ? strdup(new_passphrase) : NULL;
+  if (!pass) {
+    char *pass2 = NULL;
+    fprintf(stdout, "\n");
+    pass = silc_get_input("New passphrase: ", TRUE);
+    if (!pass) {
+      pass = strdup("");
+    } else {
+      while (TRUE) {
+       printf("\n");
+       pass2 = silc_get_input("Retype new passphrase: ", TRUE);
+       if (!pass2)
+         pass2 = strdup("");
+       if (!strcmp(pass, pass2))
+         break;
+       fprintf(stderr, "\nPassphrases do not match");
+      }
+      silc_free(pass2);
+    }
+  }
+
+  silc_pkcs_save_private_key((char *)prv_filename, private_key,
+                            (unsigned char *)pass, strlen(pass),
+                            base64 ? SILC_PKCS_FILE_PEM : SILC_PKCS_FILE_BIN);
+
+  fprintf(stdout, "\nPassphrase changed\n");
+
+  memset(pass, 0, strlen(pass));
+  silc_free(pass);
+
+  silc_pkcs_private_key_free(private_key);
+  return TRUE;
+}