+ if (!bits) {
+ char *length = NULL;
+ length =
+ silc_client_get_input("Key length in bits [1024]: ");
+ 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_client_get_input(line);
+ 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_client_get_input(line);
+ 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_client_get_input(line);
+ if (!prvfile)
+ prvfile = SILC_CLIENT_PRIVATE_KEY_NAME;
+ } else {
+ prvfile = private_key;
+ }
+
+ /* Generate keys */
+ silc_pkcs_alloc(pkcs_name, &pkcs);
+ pkcs->pkcs->init(pkcs->context, 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, sizeof(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, sizeof(key_len));
+ silc_free(key);
+
+ silc_rng_free(rng);
+ silc_pkcs_free(pkcs);
+
+ return TRUE;
+}
+
+/* This checks stats for various SILC files and directories. First it
+ checks if ~/.silc directory exist and is owned by the correct user. If
+ it doesn't exist, it will create the directory. After that it checks if
+ user's Public and Private key files exists and that they aren't expired.
+ If they doesn't exist or they are expired, they will be (re)created
+ after return. */
+
+int silc_client_check_silc_dir()
+{
+ char filename[256], file_public_key[256], file_private_key[256];
+ char servfilename[256], clientfilename[256];
+ char *identifier;
+ struct stat st;
+ struct passwd *pw;
+ int firstime = FALSE;
+ time_t curtime, modtime;
+
+ SILC_LOG_DEBUG(("Checking ~./silc directory"));
+