[BACK]Return to ssh-keygen.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Diff for /src/usr.bin/ssh/ssh-keygen.c between version 1.11 and 1.12

version 1.11, 1999/11/21 21:58:31 version 1.12, 1999/11/23 22:25:55
Line 63 
Line 63 
 void  void
 ask_filename(struct passwd *pw, const char *prompt)  ask_filename(struct passwd *pw, const char *prompt)
 {  {
   char buf[1024];          char buf[1024];
   snprintf(identity_file, sizeof(identity_file), "%s/%s",          snprintf(identity_file, sizeof(identity_file), "%s/%s",
            pw->pw_dir, SSH_CLIENT_IDENTITY);                   pw->pw_dir, SSH_CLIENT_IDENTITY);
   printf("%s (%s): ", prompt, identity_file);          printf("%s (%s): ", prompt, identity_file);
   fflush(stdout);          fflush(stdout);
   if (fgets(buf, sizeof(buf), stdin) == NULL)          if (fgets(buf, sizeof(buf), stdin) == NULL)
     exit(1);                  exit(1);
   if (strchr(buf, '\n'))          if (strchr(buf, '\n'))
     *strchr(buf, '\n') = 0;                  *strchr(buf, '\n') = 0;
   if (strcmp(buf, "") != 0)          if (strcmp(buf, "") != 0)
     strlcpy(identity_file, buf, sizeof(identity_file));                  strlcpy(identity_file, buf, sizeof(identity_file));
   have_identity = 1;          have_identity = 1;
 }  }
   
 void  void
 do_fingerprint(struct passwd *pw)  do_fingerprint(struct passwd *pw)
 {  {
   char *comment;          char *comment;
   RSA *public_key;          RSA *public_key;
   struct stat st;          struct stat st;
   
   if (!have_identity)          if (!have_identity)
     ask_filename(pw, "Enter file in which the key is");                  ask_filename(pw, "Enter file in which the key is");
   if (stat(identity_file, &st) < 0)          if (stat(identity_file, &st) < 0) {
     {                  perror(identity_file);
       perror(identity_file);                  exit(1);
       exit(1);          }
     }          public_key = RSA_new();
   public_key = RSA_new();          if (!load_public_key(identity_file, public_key, &comment)) {
   if (!load_public_key(identity_file, public_key, &comment)) {                  char *cp, line[1024];
     char *cp, line[1024];                  BIGNUM *e, *n;
     BIGNUM *e, *n;                  int dummy, invalid = 0;
     int dummy, invalid = 0;                  FILE *f = fopen(identity_file, "r");
     FILE *f = fopen(identity_file, "r");                  n = BN_new();
     n = BN_new();                  e = BN_new();
     e = BN_new();                  if (f && fgets(line, sizeof(line), f)) {
     if (f && fgets(line, sizeof(line), f)) {                          cp = line;
       cp = line;                          line[strlen(line) - 1] = '\0';
       line[strlen(line)-1] = '\0';                          if (auth_rsa_read_key(&cp, &dummy, e, n)) {
       if (auth_rsa_read_key(&cp, &dummy, e, n)) {                                  public_key->e = e;
         public_key->e = e;                                  public_key->n = n;
         public_key->n = n;                                  comment = xstrdup(cp ? cp : "no comment");
         comment = xstrdup(cp ? cp : "no comment");                          } else {
       } else {                                  invalid = 1;
         invalid = 1;                          }
       }                  } else {
     } else {                          invalid = 1;
       invalid = 1;                  }
     }                  if (invalid) {
     if (invalid) {                          printf("%s is not a valid key file.\n", identity_file);
       printf("%s is not a valid key file.\n", identity_file);                          BN_free(e);
       BN_free(e);                          BN_free(n);
       BN_free(n);                          exit(1);
       exit(1);                  }
     }          }
   }          printf("%d %s %s\n", BN_num_bits(public_key->n),
                  fingerprint(public_key->e, public_key->n),
   printf("%d %s %s\n", BN_num_bits(public_key->n),                 comment);
          fingerprint(public_key->e, public_key->n),          RSA_free(public_key);
          comment);          exit(0);
   RSA_free(public_key);  
   exit(0);  
 }  }
   
 /* Perform changing a passphrase.  The argument is the passwd structure  /* Perform changing a passphrase.  The argument is the passwd structure
Line 133 
Line 131 
 void  void
 do_change_passphrase(struct passwd *pw)  do_change_passphrase(struct passwd *pw)
 {  {
   char *comment;          char *comment;
   char *old_passphrase, *passphrase1, *passphrase2;          char *old_passphrase, *passphrase1, *passphrase2;
   struct stat st;          struct stat st;
   RSA *private_key;          RSA *private_key;
   
   if (!have_identity)          if (!have_identity)
     ask_filename(pw, "Enter file in which the key is");                  ask_filename(pw, "Enter file in which the key is");
   /* Check if the file exists. */          /* Check if the file exists. */
   if (stat(identity_file, &st) < 0)          if (stat(identity_file, &st) < 0) {
     {                  perror(identity_file);
       perror(identity_file);                  exit(1);
       exit(1);          }
     }          /* Try to load the public key from the file the verify that it is
              readable and of the proper format. */
   /* Try to load the public key from the file the verify that it is          public_key = RSA_new();
      readable and of the proper format. */          if (!load_public_key(identity_file, public_key, NULL)) {
   public_key = RSA_new();                  printf("%s is not a valid key file.\n", identity_file);
   if (!load_public_key(identity_file, public_key, NULL))                  exit(1);
     {          }
       printf("%s is not a valid key file.\n", identity_file);          /* Clear the public key since we are just about to load the whole file. */
       exit(1);          RSA_free(public_key);
     }  
   /* Clear the public key since we are just about to load the whole file. */  
   RSA_free(public_key);  
   
   /* Try to load the file with empty passphrase. */          /* Try to load the file with empty passphrase. */
   private_key = RSA_new();          private_key = RSA_new();
   if (!load_private_key(identity_file, "", private_key, &comment)) {          if (!load_private_key(identity_file, "", private_key, &comment)) {
     /* Read passphrase from the user. */                  /* Read passphrase from the user. */
     if (identity_passphrase)                  if (identity_passphrase)
       old_passphrase = xstrdup(identity_passphrase);                          old_passphrase = xstrdup(identity_passphrase);
     else                  else
       old_passphrase = read_passphrase("Enter old passphrase: ", 1);                          old_passphrase = read_passphrase("Enter old passphrase: ", 1);
     /* Try to load using the passphrase. */                  /* Try to load using the passphrase. */
     if (!load_private_key(identity_file, old_passphrase, private_key, &comment))                  if (!load_private_key(identity_file, old_passphrase, private_key, &comment)) {
       {                          memset(old_passphrase, 0, strlen(old_passphrase));
         memset(old_passphrase, 0, strlen(old_passphrase));                          xfree(old_passphrase);
         xfree(old_passphrase);                          printf("Bad passphrase.\n");
         printf("Bad passphrase.\n");                          exit(1);
         exit(1);                  }
       }                  /* Destroy the passphrase. */
     /* Destroy the passphrase. */                  memset(old_passphrase, 0, strlen(old_passphrase));
     memset(old_passphrase, 0, strlen(old_passphrase));                  xfree(old_passphrase);
     xfree(old_passphrase);          }
   }          printf("Key has comment '%s'\n", comment);
   printf("Key has comment '%s'\n", comment);  
   
   /* Ask the new passphrase (twice). */  
   if (identity_new_passphrase)  
     {  
       passphrase1 = xstrdup(identity_new_passphrase);  
       passphrase2 = NULL;  
     }  
   else  
     {  
       passphrase1 =  
         read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);  
       passphrase2 = read_passphrase("Enter same passphrase again: ", 1);  
   
       /* Verify that they are the same. */          /* Ask the new passphrase (twice). */
       if (strcmp(passphrase1, passphrase2) != 0)          if (identity_new_passphrase) {
         {                  passphrase1 = xstrdup(identity_new_passphrase);
           memset(passphrase1, 0, strlen(passphrase1));                  passphrase2 = NULL;
           memset(passphrase2, 0, strlen(passphrase2));          } else {
           xfree(passphrase1);                  passphrase1 =
           xfree(passphrase2);                          read_passphrase("Enter new passphrase (empty for no passphrase): ", 1);
           printf("Pass phrases do not match.  Try again.\n");                  passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
           exit(1);  
                   /* Verify that they are the same. */
                   if (strcmp(passphrase1, passphrase2) != 0) {
                           memset(passphrase1, 0, strlen(passphrase1));
                           memset(passphrase2, 0, strlen(passphrase2));
                           xfree(passphrase1);
                           xfree(passphrase2);
                           printf("Pass phrases do not match.  Try again.\n");
                           exit(1);
                   }
                   /* Destroy the other copy. */
                   memset(passphrase2, 0, strlen(passphrase2));
                   xfree(passphrase2);
         }          }
       /* Destroy the other copy. */  
       memset(passphrase2, 0, strlen(passphrase2));  
       xfree(passphrase2);  
     }  
   
   /* Save the file using the new passphrase. */          /* Save the file using the new passphrase. */
   if (!save_private_key(identity_file, passphrase1, private_key, comment))          if (!save_private_key(identity_file, passphrase1, private_key, comment)) {
     {                  printf("Saving the key failed: %s: %s.\n",
       printf("Saving the key failed: %s: %s.\n",                         identity_file, strerror(errno));
              identity_file, strerror(errno));                  memset(passphrase1, 0, strlen(passphrase1));
       memset(passphrase1, 0, strlen(passphrase1));                  xfree(passphrase1);
       xfree(passphrase1);                  RSA_free(private_key);
       RSA_free(private_key);                  xfree(comment);
       xfree(comment);                  exit(1);
       exit(1);          }
     }          /* Destroy the passphrase and the copy of the key in memory. */
   /* Destroy the passphrase and the copy of the key in memory. */          memset(passphrase1, 0, strlen(passphrase1));
   memset(passphrase1, 0, strlen(passphrase1));          xfree(passphrase1);
   xfree(passphrase1);          RSA_free(private_key);  /* Destroys contents */
   RSA_free(private_key); /* Destroys contents */          xfree(comment);
   xfree(comment);  
   
   printf("Your identification has been saved with the new passphrase.\n");          printf("Your identification has been saved with the new passphrase.\n");
   exit(0);          exit(0);
 }  }
   
 /* Change the comment of a private key file. */  /* Change the comment of a private key file. */
Line 233 
Line 222 
 void  void
 do_change_comment(struct passwd *pw)  do_change_comment(struct passwd *pw)
 {  {
   char new_comment[1024], *comment;          char new_comment[1024], *comment;
   RSA *private_key;          RSA *private_key;
   char *passphrase;          char *passphrase;
   struct stat st;          struct stat st;
   FILE *f;          FILE *f;
   char *tmpbuf;          char *tmpbuf;
   
   if (!have_identity)          if (!have_identity)
     ask_filename(pw, "Enter file in which the key is");                  ask_filename(pw, "Enter file in which the key is");
   /* Check if the file exists. */          /* Check if the file exists. */
   if (stat(identity_file, &st) < 0)          if (stat(identity_file, &st) < 0) {
     {                  perror(identity_file);
       perror(identity_file);                  exit(1);
       exit(1);          }
     }          /* Try to load the public key from the file the verify that it is
              readable and of the proper format. */
   /* Try to load the public key from the file the verify that it is          public_key = RSA_new();
      readable and of the proper format. */          if (!load_public_key(identity_file, public_key, NULL)) {
   public_key = RSA_new();                  printf("%s is not a valid key file.\n", identity_file);
   if (!load_public_key(identity_file, public_key, NULL))                  exit(1);
     {          }
       printf("%s is not a valid key file.\n", identity_file);          private_key = RSA_new();
       exit(1);          /* Try to load the file with empty passphrase. */
     }          if (load_private_key(identity_file, "", private_key, &comment))
                   passphrase = xstrdup("");
           else {
                   /* Read passphrase from the user. */
                   if (identity_passphrase)
                           passphrase = xstrdup(identity_passphrase);
                   else if (identity_new_passphrase)
                           passphrase = xstrdup(identity_new_passphrase);
                   else
                           passphrase = read_passphrase("Enter passphrase: ", 1);
                   /* Try to load using the passphrase. */
                   if (!load_private_key(identity_file, passphrase, private_key, &comment)) {
                           memset(passphrase, 0, strlen(passphrase));
                           xfree(passphrase);
                           printf("Bad passphrase.\n");
                           exit(1);
                   }
           }
           printf("Key now has comment '%s'\n", comment);
   
   private_key = RSA_new();          if (identity_comment) {
   /* Try to load the file with empty passphrase. */                  strlcpy(new_comment, identity_comment, sizeof(new_comment));
   if (load_private_key(identity_file, "", private_key, &comment))          } else {
     passphrase = xstrdup("");                  printf("Enter new comment: ");
   else                  fflush(stdout);
     {                  if (!fgets(new_comment, sizeof(new_comment), stdin)) {
       /* Read passphrase from the user. */                          memset(passphrase, 0, strlen(passphrase));
       if (identity_passphrase)                          RSA_free(private_key);
         passphrase = xstrdup(identity_passphrase);                          exit(1);
       else                  }
         if (identity_new_passphrase)                  /* Remove terminating newline from comment. */
           passphrase = xstrdup(identity_new_passphrase);                  if (strchr(new_comment, '\n'))
         else                          *strchr(new_comment, '\n') = 0;
           passphrase = read_passphrase("Enter passphrase: ", 1);  
       /* Try to load using the passphrase. */  
       if (!load_private_key(identity_file, passphrase, private_key, &comment))  
         {  
           memset(passphrase, 0, strlen(passphrase));  
           xfree(passphrase);  
           printf("Bad passphrase.\n");  
           exit(1);  
         }          }
     }  
   printf("Key now has comment '%s'\n", comment);  
   
   if (identity_comment)          /* Save the file using the new passphrase. */
     {          if (!save_private_key(identity_file, passphrase, private_key, new_comment)) {
       strlcpy(new_comment, identity_comment, sizeof(new_comment));                  printf("Saving the key failed: %s: %s.\n",
     }                         identity_file, strerror(errno));
   else                  memset(passphrase, 0, strlen(passphrase));
     {                  xfree(passphrase);
       printf("Enter new comment: ");                  RSA_free(private_key);
       fflush(stdout);                  xfree(comment);
       if (!fgets(new_comment, sizeof(new_comment), stdin))                  exit(1);
         {  
           memset(passphrase, 0, strlen(passphrase));  
           RSA_free(private_key);  
           exit(1);  
         }          }
           /* Destroy the passphrase and the private key in memory. */
       /* Remove terminating newline from comment. */          memset(passphrase, 0, strlen(passphrase));
       if (strchr(new_comment, '\n'))          xfree(passphrase);
         *strchr(new_comment, '\n') = 0;          RSA_free(private_key);
     }  
   
   /* Save the file using the new passphrase. */  
   if (!save_private_key(identity_file, passphrase, private_key, new_comment))  
     {  
       printf("Saving the key failed: %s: %s.\n",  
              identity_file, strerror(errno));  
       memset(passphrase, 0, strlen(passphrase));  
       xfree(passphrase);  
       RSA_free(private_key);  
       xfree(comment);  
       exit(1);  
     }  
   
   /* Destroy the passphrase and the private key in memory. */          /* Save the public key in text format in a file with the same name
   memset(passphrase, 0, strlen(passphrase));             but .pub appended. */
   xfree(passphrase);          strlcat(identity_file, ".pub", sizeof(identity_file));
   RSA_free(private_key);          f = fopen(identity_file, "w");
           if (!f) {
                   printf("Could not save your public key in %s\n", identity_file);
                   exit(1);
           }
           fprintf(f, "%d ", BN_num_bits(public_key->n));
           tmpbuf = BN_bn2dec(public_key->e);
           fprintf(f, "%s ", tmpbuf);
           free(tmpbuf);
           tmpbuf = BN_bn2dec(public_key->n);
           fprintf(f, "%s %s\n", tmpbuf, new_comment);
           free(tmpbuf);
           fclose(f);
   
   /* Save the public key in text format in a file with the same name but          xfree(comment);
      .pub appended. */  
   strlcat(identity_file, ".pub", sizeof(identity_file));  
   f = fopen(identity_file, "w");  
   if (!f)  
     {  
       printf("Could not save your public key in %s\n", identity_file);  
       exit(1);  
     }  
   fprintf(f, "%d ", BN_num_bits(public_key->n));  
   tmpbuf = BN_bn2dec(public_key->e);  
   fprintf(f, "%s ", tmpbuf);  
   free (tmpbuf);  
   tmpbuf = BN_bn2dec(public_key->n);  
   fprintf(f, "%s %s\n", tmpbuf, new_comment);  
   free (tmpbuf);  
   fclose(f);  
   
   xfree(comment);          printf("The comment in your key file has been changed.\n");
           exit(0);
   printf("The comment in your key file has been changed.\n");  
   exit(0);  
 }  }
   
 void  void
 usage(void)  usage(void)
 {  {
   printf("ssh-keygen version %s\n", SSH_VERSION);          printf("ssh-keygen version %s\n", SSH_VERSION);
   printf("Usage: %s [-b bits] [-p] [-c] [-f file] [-P pass] [-N new-pass] [-C comment]\n", __progname);          printf("Usage: %s [-b bits] [-p] [-c] [-f file] [-P pass] [-N new-pass] [-C comment]\n", __progname);
   exit(1);          exit(1);
 }  }
   
 /* Main program for key management. */  /* Main program for key management. */
Line 357 
Line 331 
 int  int
 main(int ac, char **av)  main(int ac, char **av)
 {  {
   char dotsshdir[16*1024], comment[1024], *passphrase1, *passphrase2;          char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2;
   struct passwd *pw;          struct passwd *pw;
   char *tmpbuf;          char *tmpbuf;
   int opt;          int opt;
   struct stat st;          struct stat st;
   FILE *f;          FILE *f;
   char hostname[MAXHOSTNAMELEN];          char hostname[MAXHOSTNAMELEN];
   extern int optind;          extern int optind;
   extern char *optarg;          extern char *optarg;
   
   /* check if RSA support exists */          /* check if RSA support exists */
   if (rsa_alive() == 0) {          if (rsa_alive() == 0) {
     extern char *__progname;                  extern char *__progname;
   
     fprintf(stderr,                  fprintf(stderr,
       "%s: no RSA support in libssl and libcrypto.  See ssl(8).\n",                          "%s: no RSA support in libssl and libcrypto.  See ssl(8).\n",
       __progname);                          __progname);
     exit(1);                  exit(1);
   }          }
           /* Get user\'s passwd structure.  We need this for the home
              directory. */
           pw = getpwuid(getuid());
           if (!pw) {
                   printf("You don't exist, go away!\n");
                   exit(1);
           }
           /* Parse command line arguments. */
           while ((opt = getopt(ac, av, "qpclb:f:P:N:C:")) != EOF) {
                   switch (opt) {
                   case 'b':
                           bits = atoi(optarg);
                           if (bits < 512 || bits > 32768) {
                                   printf("Bits has bad value.\n");
                                   exit(1);
                           }
                           break;
   
   /* Get user\'s passwd structure.  We need this for the home directory. */                  case 'l':
   pw = getpwuid(getuid());                          print_fingerprint = 1;
   if (!pw)                          break;
     {  
       printf("You don't exist, go away!\n");  
       exit(1);  
     }  
   
   /* Parse command line arguments. */                  case 'p':
   while ((opt = getopt(ac, av, "qpclb:f:P:N:C:")) != EOF)                          change_passphrase = 1;
     {                          break;
       switch (opt)  
         {  
         case 'b':  
           bits = atoi(optarg);  
           if (bits < 512 || bits > 32768)  
             {  
               printf("Bits has bad value.\n");  
               exit(1);  
             }  
           break;  
   
         case 'l':                  case 'c':
           print_fingerprint = 1;                          change_comment = 1;
           break;                          break;
   
         case 'p':                  case 'f':
           change_passphrase = 1;                          strlcpy(identity_file, optarg, sizeof(identity_file));
           break;                          have_identity = 1;
                           break;
   
         case 'c':                  case 'P':
           change_comment = 1;                          identity_passphrase = optarg;
           break;                          break;
   
         case 'f':                  case 'N':
           strlcpy(identity_file, optarg, sizeof(identity_file));                          identity_new_passphrase = optarg;
           have_identity = 1;                          break;
           break;  
   
         case 'P':  
           identity_passphrase = optarg;  
           break;  
   
         case 'N':                  case 'C':
           identity_new_passphrase = optarg;                          identity_comment = optarg;
           break;                          break;
   
         case 'C':                  case 'q':
           identity_comment = optarg;                          quiet = 1;
           break;                          break;
   
         case 'q':                  case '?':
           quiet = 1;                  default:
           break;                          usage();
                   }
         case '?':  
         default:  
           usage();  
         }          }
     }          if (optind < ac) {
   if (optind < ac)                  printf("Too many arguments.\n");
     {                  usage();
       printf("Too many arguments.\n");          }
       usage();          if (change_passphrase && change_comment) {
     }                  printf("Can only have one of -p and -c.\n");
   if (change_passphrase && change_comment)                  usage();
     {          }
       printf("Can only have one of -p and -c.\n");          if (print_fingerprint)
       usage();                  do_fingerprint(pw);
     }  
   
   if (print_fingerprint)          /* If the user requested to change the passphrase, do it now.
     do_fingerprint(pw);             This function never returns. */
           if (change_passphrase)
                   do_change_passphrase(pw);
   
   /* If the user requested to change the passphrase, do it now.  This          /* If the user requested to change the comment, do it now.  This
      function never returns. */             function never returns. */
   if (change_passphrase)          if (change_comment)
     do_change_passphrase(pw);                  do_change_comment(pw);
   
   /* If the user requested to change the comment, do it now.  This function          arc4random_stir();
      never returns. */  
   if (change_comment)  
     do_change_comment(pw);  
   
   arc4random_stir();          if (quiet)
                   rsa_set_verbose(0);
   
   if (quiet)          /* Generate the rsa key pair. */
     rsa_set_verbose(0);          private_key = RSA_new();
           public_key = RSA_new();
           rsa_generate_key(private_key, public_key, bits);
   
   /* Generate the rsa key pair. */          if (!have_identity)
   private_key = RSA_new();                  ask_filename(pw, "Enter file in which to save the key");
   public_key = RSA_new();  
   rsa_generate_key(private_key, public_key, bits);  
   
   if (!have_identity)          /* Create ~/.ssh directory if it doesn\'t already exist. */
     ask_filename(pw, "Enter file in which to save the key");          snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, SSH_USER_DIR);
           if (strstr(identity_file, dotsshdir) != NULL &&
               stat(dotsshdir, &st) < 0) {
                   if (mkdir(dotsshdir, 0755) < 0)
                           error("Could not create directory '%s'.", dotsshdir);
                   else if (!quiet)
                           printf("Created directory '%s'.\n", dotsshdir);
           }
           /* If the file already exists, ask the user to confirm. */
           if (stat(identity_file, &st) >= 0) {
                   char yesno[3];
                   printf("%s already exists.\n", identity_file);
                   printf("Overwrite (y/n)? ");
                   fflush(stdout);
                   if (fgets(yesno, sizeof(yesno), stdin) == NULL)
                           exit(1);
                   if (yesno[0] != 'y' && yesno[0] != 'Y')
                           exit(1);
           }
           /* Ask for a passphrase (twice). */
           if (identity_passphrase)
                   passphrase1 = xstrdup(identity_passphrase);
           else if (identity_new_passphrase)
                   passphrase1 = xstrdup(identity_new_passphrase);
           else {
   passphrase_again:
                   passphrase1 =
                           read_passphrase("Enter passphrase (empty for no passphrase): ", 1);
                   passphrase2 = read_passphrase("Enter same passphrase again: ", 1);
                   if (strcmp(passphrase1, passphrase2) != 0) {
                           /* The passphrases do not match.  Clear them and retry. */
                           memset(passphrase1, 0, strlen(passphrase1));
                           memset(passphrase2, 0, strlen(passphrase2));
                           xfree(passphrase1);
                           xfree(passphrase2);
                           printf("Passphrases do not match.  Try again.\n");
                           goto passphrase_again;
                   }
                   /* Clear the other copy of the passphrase. */
                   memset(passphrase2, 0, strlen(passphrase2));
                   xfree(passphrase2);
           }
   
   /* Create ~/.ssh directory if it doesn\'t already exist. */          /* Create default commend field for the passphrase.  The user can
   snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, SSH_USER_DIR);             later edit this field. */
   if (strstr(identity_file, dotsshdir) != NULL &&          if (identity_comment) {
       stat(dotsshdir, &st) < 0) {                  strlcpy(comment, identity_comment, sizeof(comment));
     if (mkdir(dotsshdir, 0755) < 0)          } else {
       error("Could not create directory '%s'.", dotsshdir);                  if (gethostname(hostname, sizeof(hostname)) < 0) {
     else if(!quiet)                          perror("gethostname");
       printf("Created directory '%s'.\n", dotsshdir);                          exit(1);
  }                  }
                   snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
           }
   
   /* If the file already exists, ask the user to confirm. */          /* Save the key with the given passphrase and comment. */
   if (stat(identity_file, &st) >= 0)          if (!save_private_key(identity_file, passphrase1, private_key, comment)) {
     {                  printf("Saving the key failed: %s: %s.\n",
       char yesno[3];                         identity_file, strerror(errno));
       printf("%s already exists.\n", identity_file);                  memset(passphrase1, 0, strlen(passphrase1));
       printf("Overwrite (y/n)? ");                  xfree(passphrase1);
       fflush(stdout);                  exit(1);
       if (fgets(yesno, sizeof(yesno), stdin) == NULL)  
         exit(1);  
       if (yesno[0] != 'y' && yesno[0] != 'Y')  
         exit(1);  
     }  
   
   /* Ask for a passphrase (twice). */  
   if (identity_passphrase)  
     passphrase1 = xstrdup(identity_passphrase);  
   else  
     if (identity_new_passphrase)  
       passphrase1 = xstrdup(identity_new_passphrase);  
     else  
       {  
       passphrase_again:  
         passphrase1 =  
           read_passphrase("Enter passphrase (empty for no passphrase): ", 1);  
         passphrase2 = read_passphrase("Enter same passphrase again: ", 1);  
         if (strcmp(passphrase1, passphrase2) != 0)  
           {  
             /* The passphrases do not match.  Clear them and retry. */  
             memset(passphrase1, 0, strlen(passphrase1));  
             memset(passphrase2, 0, strlen(passphrase2));  
             xfree(passphrase1);  
             xfree(passphrase2);  
             printf("Passphrases do not match.  Try again.\n");  
             goto passphrase_again;  
           }  
         /* Clear the other copy of the passphrase. */  
         memset(passphrase2, 0, strlen(passphrase2));  
         xfree(passphrase2);  
       }  
   
   /* Create default commend field for the passphrase.  The user can later  
      edit this field. */  
   if (identity_comment)  
     {  
       strlcpy(comment, identity_comment, sizeof(comment));  
     }  
   else  
     {  
       if (gethostname(hostname, sizeof(hostname)) < 0)  
         {  
           perror("gethostname");  
           exit(1);  
         }          }
       snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);          /* Clear the passphrase. */
     }          memset(passphrase1, 0, strlen(passphrase1));
           xfree(passphrase1);
   
   /* Save the key with the given passphrase and comment. */          /* Clear the private key and the random number generator. */
   if (!save_private_key(identity_file, passphrase1, private_key, comment))          RSA_free(private_key);
     {          arc4random_stir();
       printf("Saving the key failed: %s: %s.\n",  
              identity_file, strerror(errno));  
       memset(passphrase1, 0, strlen(passphrase1));  
       xfree(passphrase1);  
       exit(1);  
     }  
   /* Clear the passphrase. */  
   memset(passphrase1, 0, strlen(passphrase1));  
   xfree(passphrase1);  
   
   /* Clear the private key and the random number generator. */          if (!quiet)
   RSA_free(private_key);                  printf("Your identification has been saved in %s.\n", identity_file);
   arc4random_stir();  
   
   if (!quiet)          /* Save the public key in text format in a file with the same name
     printf("Your identification has been saved in %s.\n", identity_file);             but .pub appended. */
           strlcat(identity_file, ".pub", sizeof(identity_file));
           f = fopen(identity_file, "w");
           if (!f) {
                   printf("Could not save your public key in %s\n", identity_file);
                   exit(1);
           }
           fprintf(f, "%d ", BN_num_bits(public_key->n));
           tmpbuf = BN_bn2dec(public_key->e);
           fprintf(f, "%s ", tmpbuf);
           free(tmpbuf);
           tmpbuf = BN_bn2dec(public_key->n);
           fprintf(f, "%s %s\n", tmpbuf, comment);
           free(tmpbuf);
           fclose(f);
   
   /* Save the public key in text format in a file with the same name but          if (!quiet) {
      .pub appended. */                  printf("Your public key has been saved in %s.\n", identity_file);
   strlcat(identity_file, ".pub", sizeof(identity_file));                  printf("The key fingerprint is:\n");
   f = fopen(identity_file, "w");                  printf("%d %s %s\n", BN_num_bits(public_key->n),
   if (!f)                         fingerprint(public_key->e, public_key->n),
     {                         comment);
       printf("Could not save your public key in %s\n", identity_file);          }
       exit(1);          exit(0);
     }  
   fprintf(f, "%d ", BN_num_bits(public_key->n));  
   tmpbuf = BN_bn2dec(public_key->e);  
   fprintf(f, "%s ", tmpbuf);  
   free(tmpbuf);  
   tmpbuf = BN_bn2dec(public_key->n);  
   fprintf(f, "%s %s\n", tmpbuf, comment);  
   free(tmpbuf);  
   fclose(f);  
   
   if (!quiet) {  
     printf("Your public key has been saved in %s.\n", identity_file);  
     printf("The key fingerprint is:\n");  
     printf("%d %s %s\n", BN_num_bits(public_key->n),  
            fingerprint(public_key->e, public_key->n),  
            comment);  
   }  
   
   exit(0);  
 }  }

Legend:
Removed from v.1.11  
changed lines
  Added in v.1.12