Added SILC Thread Queue API
[crypto.git] / lib / silcapputil / silcapputil.c
index b5901294b24cc4784f2ecba70d0a6ce2d7c2aed3..157cce57b3ed3261c0b795bdf850a3f987f2fa43 100644 (file)
@@ -31,18 +31,22 @@ static char *silc_create_pk_identifier(void)
 
   /* Get hostname */
   hostname = silc_net_localhost();
-  if (!hostname)
+  if (!hostname) {
+    fprintf(stderr, "Could not resolve local hostname/IP address");
     return NULL;
+  }
 
   /* Get username (mandatory) */
   username = silc_get_username();
-  if (!username)
+  if (!username) {
+    fprintf(stderr, "Could not determine username");
     return NULL;
+  }
 
   /* Create default email address, whether it is right or not */
   silc_snprintf(email, sizeof(email), "%s@%s", username, hostname);
 
-  ident = silc_pkcs_silc_encode_identifier(username, hostname, realname,
+  ident = silc_pkcs_silc_encode_identifier(NULL, username, hostname, realname,
                                           email, NULL, NULL, NULL);
   if (realname)
     silc_free(realname);
@@ -136,8 +140,7 @@ New pair of keys will be created.  Please, answer to following questions.\n\
       }
     } else {
       if (!def) {
-       fprintf(stderr, "Could not create public key identifier: %s\n",
-               strerror(errno));
+       fprintf(stderr, "Could not create public key identifier\n");
        return FALSE;
       }
       identifier = strdup(def);
@@ -146,6 +149,12 @@ New pair of keys will be created.  Please, answer to following questions.\n\
     silc_free(def);
   }
 
+  if (!strstr(identifier, "UN=") || !strstr(identifier, "HN=")) {
+    fprintf(stderr, "Invalid public key identifier.  You must specify both "
+           "UN and HN\n");
+    return FALSE;
+  }
+
   rng = silc_rng_alloc();
   silc_rng_init(rng);
   silc_rng_global_init(rng);
@@ -202,12 +211,14 @@ New pair of keys will be created.  Please, answer to following questions.\n\
     return FALSE;
 
   /* Save public key into file */
-  silc_pkcs_save_public_key(pkfile, public_key, SILC_PKCS_FILE_BASE64);
+  if (!silc_pkcs_save_public_key(pkfile, public_key, SILC_PKCS_FILE_BASE64))
+    return FALSE;
 
   /* Save private key into file */
-  silc_pkcs_save_private_key(prvfile, private_key,
-                            (const unsigned char *)pass, strlen(pass),
-                            SILC_PKCS_FILE_BIN, rng);
+  if (!silc_pkcs_save_private_key(prvfile, private_key,
+                                 (const unsigned char *)pass, strlen(pass),
+                                 SILC_PKCS_FILE_BIN, rng))
+    return FALSE;
 
   if (return_public_key)
     *return_public_key = public_key;
@@ -249,7 +260,8 @@ SilcBool silc_load_key_pair(const char *pub_filename,
 
   SILC_LOG_DEBUG(("Loading public and private keys"));
 
-  if (!silc_pkcs_load_public_key(pub_filename, return_public_key)) {
+  if (!silc_pkcs_load_public_key(pub_filename,
+                                SILC_PKCS_ANY, return_public_key)) {
     if (pass)
       memset(pass, 0, strlen(pass));
     silc_free(pass);
@@ -264,7 +276,10 @@ SilcBool silc_load_key_pair(const char *pub_filename,
 
   if (!silc_pkcs_load_private_key(prv_filename,
                                  (const unsigned char *)pass, strlen(pass),
+                                 SILC_PKCS_ANY,
                                  return_private_key)) {
+    silc_pkcs_public_key_free(*return_public_key);
+    *return_public_key = NULL;
     memset(pass, 0, strlen(pass));
     silc_free(pass);
     return FALSE;
@@ -286,19 +301,15 @@ SilcBool silc_show_public_key(SilcPublicKey public_key)
   SilcUInt32 pk_len;
   SilcUInt32 key_len = 0;
 
-  silc_pubkey = silc_pkcs_get_context(SILC_PKCS_SILC, public_key);
-  if (!silc_pubkey) {
-    silc_pkcs_public_key_free(public_key);
+  silc_pubkey = silc_pkcs_public_key_get_pkcs(SILC_PKCS_SILC, public_key);
+  if (!silc_pubkey)
     return FALSE;
-  }
 
   ident = &silc_pubkey->identifier;
   key_len = silc_pkcs_public_key_get_len(public_key);
-  pk = silc_pkcs_public_key_encode(public_key, &pk_len);
-  if (!pk) {
-    silc_pkcs_public_key_free(public_key);
+  pk = silc_pkcs_public_key_encode(NULL, public_key, &pk_len);
+  if (!pk)
     return FALSE;
-  }
   fingerprint = silc_hash_fingerprint(NULL, pk, pk_len);
   babbleprint = silc_hash_babbleprint(NULL, pk, pk_len);
 
@@ -338,7 +349,8 @@ SilcBool silc_show_public_key_file(const char *pub_filename)
   SilcPublicKey public_key;
   SilcBool ret;
 
-  if (!silc_pkcs_load_public_key((char *)pub_filename, &public_key)) {
+  if (!silc_pkcs_load_public_key((char *)pub_filename,
+                                SILC_PKCS_ANY, &public_key)) {
     fprintf(stderr, "Could not load public key file `%s'\n", pub_filename);
     return FALSE;
   }
@@ -369,6 +381,7 @@ SilcBool silc_change_private_key_passphrase(const char *prv_filename,
 
   if (!silc_pkcs_load_private_key(prv_filename,
                                  (const unsigned char *)pass, strlen(pass),
+                                 SILC_PKCS_ANY,
                                  &private_key)) {
     memset(pass, 0, strlen(pass));
     silc_free(pass);
@@ -618,6 +631,7 @@ static const SilcStatusMessage silc_status_messages[] = {
   { STAT(OPERATION_ALLOWED), "Operation is not allowed" },
   { STAT(BAD_SERVER), "Bad server name" },
   { STAT(BAD_USERNAME), "Bad user name" },
+  { STAT(NO_SUCH_PUBLIC_KEY), "Unknown public key" },
 
   { 0, NULL }
 };