-/* Returns identifier string for public key generation. */
-
-char *silc_client_create_identifier()
-{
- char *username = NULL, *realname = NULL;
- char *hostname, email[256];
- char *ident;
-
- /* Get realname */
- realname = silc_get_real_name();
-
- /* Get hostname */
- hostname = silc_net_localhost();
- if (!hostname)
- return NULL;
-
- /* Get username (mandatory) */
- username = silc_get_username();
- if (!username)
- return NULL;
-
- /* Create default email address, whether it is right or not */
- snprintf(email, sizeof(email), "%s@%s", username, hostname);
-
- ident = silc_pkcs_encode_identifier(username, hostname, realname, email,
- NULL, NULL);
- if (realname)
- silc_free(realname);
- silc_free(hostname);
- silc_free(username);
-
- return ident;
-}
-
-/* Creates new public key and private key pair. This is used only
- when user wants to create new key pair from command line. */
-
-int silc_client_create_key_pair(char *pkcs_name, int bits,
- char *public_key, char *private_key,
- char *identifier,
- SilcPublicKey *ret_pub_key,
- SilcPrivateKey *ret_prv_key)
-{
- SilcPKCS pkcs;
- SilcPublicKey pub_key;
- SilcPrivateKey prv_key;
- SilcRng rng;
- unsigned char *key;
- SilcUInt32 key_len;
- char line[256];
- char *pkfile = NULL, *prvfile = NULL;
-
- if (!pkcs_name || !public_key || !private_key)
- printf("\
-New pair of keys will be created. Please, answer to following questions.\n\
-");
-
- if (!pkcs_name) {
- again_name:
- pkcs_name = silc_get_input("PKCS name (l to list names) [rsa]: ", FALSE);
- if (!pkcs_name)
- pkcs_name = strdup("rsa");
-
- if (*pkcs_name == 'l' || *pkcs_name == 'L') {
- silc_client_list_pkcs();
- silc_free(pkcs_name);
- goto again_name;
- }
- }
-
- if (!silc_pkcs_is_supported(pkcs_name)) {
- fprintf(stderr, "Unknown PKCS `%s'", pkcs_name);
- return FALSE;
- }
-
- if (!bits) {
- char *length = NULL;
- length = silc_get_input("Key length in bits [1024]: ", FALSE);
- if (!length)
- bits = 1024;
- else
- bits = atoi(length);
- }
-
- if (!identifier) {
- char *def = silc_client_create_identifier();
-
- memset(line, 0, sizeof(line));
- if (def)
- snprintf(line, sizeof(line), "Identifier [%s]: ", def);
- else
- snprintf(line, sizeof(line),
- "Identifier (eg. UN=jon, HN=jon.dummy.com, "
- "RN=Jon Johnson, E=jon@dummy.com): ");
-
- while (!identifier) {
- identifier = silc_get_input(line, FALSE);
- if (!identifier && def)
- identifier = strdup(def);
- }
-
- if (def)
- silc_free(def);
- }
-
- rng = silc_rng_alloc();
- silc_rng_init(rng);
- silc_rng_global_init(rng);
-
- if (!public_key) {
- memset(line, 0, sizeof(line));
- snprintf(line, sizeof(line), "Public key filename [%s] ",
- SILC_CLIENT_PUBLIC_KEY_NAME);
- pkfile = silc_get_input(line, FALSE);
- if (!pkfile)
- pkfile = SILC_CLIENT_PUBLIC_KEY_NAME;
- } else {
- pkfile = public_key;
- }
-
- if (!private_key) {
- memset(line, 0, sizeof(line));
- snprintf(line, sizeof(line), "Public key filename [%s] ",
- SILC_CLIENT_PRIVATE_KEY_NAME);
- prvfile = silc_get_input(line, FALSE);
- if (!prvfile)
- prvfile = SILC_CLIENT_PRIVATE_KEY_NAME;
- } else {
- prvfile = private_key;
- }
-
- /* Generate keys */
- silc_pkcs_alloc(pkcs_name, &pkcs);
- silc_pkcs_generate_key(pkcs, bits, rng);
-
- /* Save public key into file */
- key = silc_pkcs_get_public_key(pkcs, &key_len);
- pub_key = silc_pkcs_public_key_alloc(pkcs->pkcs->name, identifier,
- key, key_len);
- silc_pkcs_save_public_key(pkfile, pub_key, SILC_PKCS_FILE_PEM);
- if (ret_pub_key)
- *ret_pub_key = pub_key;
-
- memset(key, 0, key_len);
- silc_free(key);
-
- /* Save private key into file */
- key = silc_pkcs_get_private_key(pkcs, &key_len);
- prv_key = silc_pkcs_private_key_alloc(pkcs->pkcs->name, key, key_len);
-
- silc_pkcs_save_private_key(prvfile, prv_key, NULL, SILC_PKCS_FILE_BIN);
- if (ret_prv_key)
- *ret_prv_key = prv_key;
-
- printf("Public key has been saved into `%s'.\n", pkfile);
- printf("Private key has been saved into `%s'.\n", prvfile);
- printf("Press <Enter> to continue...\n");
- getchar();
-
- memset(key, 0, key_len);
- silc_free(key);
-
- silc_rng_free(rng);
- silc_pkcs_free(pkcs);
-
- return TRUE;
-}
-