- Buffer buffer, encrypted;
- char buf[100], *cp;
- int f, i;
- CipherContext cipher;
- int cipher_type;
- u_int32_t rand;
-
- /* If the passphrase is empty, use SSH_CIPHER_NONE to ease converting to
- another cipher; otherwise use SSH_AUTHFILE_CIPHER. */
- if (strcmp(passphrase, "") == 0)
- cipher_type = SSH_CIPHER_NONE;
- else
- cipher_type = SSH_AUTHFILE_CIPHER;
-
- /* This buffer is used to built the secret part of the private key. */
- buffer_init(&buffer);
-
- /* Put checkbytes for checking passphrase validity. */
- rand = arc4random();
- buf[0] = rand & 0xff;
- buf[1] = (rand >> 8) & 0xff;
- buf[2] = buf[0];
- buf[3] = buf[1];
- buffer_append(&buffer, buf, 4);
-
- /* Store the private key (n and e will not be stored because they will
- be stored in plain text, and storing them also in encrypted format
- would just give known plaintext). */
- buffer_put_bignum(&buffer, key->d);
- buffer_put_bignum(&buffer, key->iqmp);
- buffer_put_bignum(&buffer, key->q); /* reverse from SSL p */
- buffer_put_bignum(&buffer, key->p); /* reverse from SSL q */
-
- /* Pad the part to be encrypted until its size is a multiple of 8. */
- while (buffer_len(&buffer) % 8 != 0)
- buffer_put_char(&buffer, 0);
-
- /* This buffer will be used to contain the data in the file. */
- buffer_init(&encrypted);
-
- /* First store keyfile id string. */
- cp = AUTHFILE_ID_STRING;
- for (i = 0; cp[i]; i++)
- buffer_put_char(&encrypted, cp[i]);
- buffer_put_char(&encrypted, 0);
-
- /* Store cipher type. */
- buffer_put_char(&encrypted, cipher_type);
- buffer_put_int(&encrypted, 0); /* For future extension */
-
- /* Store public key. This will be in plain text. */
- buffer_put_int(&encrypted, BN_num_bits(key->n));
- buffer_put_bignum(&encrypted, key->n);
- buffer_put_bignum(&encrypted, key->e);
- buffer_put_string(&encrypted, comment, strlen(comment));
-
- /* Allocate space for the private part of the key in the buffer. */
- buffer_append_space(&encrypted, &cp, buffer_len(&buffer));
-
- cipher_set_key_string(&cipher, cipher_type, passphrase, 1);
- cipher_encrypt(&cipher, (unsigned char *)cp,
- (unsigned char *)buffer_ptr(&buffer),
- buffer_len(&buffer));
- memset(&cipher, 0, sizeof(cipher));
-
- /* Destroy temporary data. */
- memset(buf, 0, sizeof(buf));
- buffer_free(&buffer);
-
- /* Write to a file. */
- f = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0600);
- if (f < 0)
- return 0;
-
- if (write(f, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
- buffer_len(&encrypted))
- {
- debug("Write to key file %.200s failed: %.100s", filename,
- strerror(errno));
- buffer_free(&encrypted);
- close(f);
- remove(filename);
- return 0;
- }
- close(f);
- buffer_free(&encrypted);
- return 1;
+ Buffer buffer, encrypted;
+ char buf[100], *cp;
+ int fd, i;
+ CipherContext cipher;
+ int cipher_type;
+ u_int32_t rand;
+
+ /*
+ * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
+ * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
+ */
+ if (strcmp(passphrase, "") == 0)
+ cipher_type = SSH_CIPHER_NONE;
+ else
+ cipher_type = SSH_AUTHFILE_CIPHER;
+
+ /* This buffer is used to built the secret part of the private key. */
+ buffer_init(&buffer);
+
+ /* Put checkbytes for checking passphrase validity. */
+ rand = arc4random();
+ buf[0] = rand & 0xff;
+ buf[1] = (rand >> 8) & 0xff;
+ buf[2] = buf[0];
+ buf[3] = buf[1];
+ buffer_append(&buffer, buf, 4);
+
+ /*
+ * Store the private key (n and e will not be stored because they
+ * will be stored in plain text, and storing them also in encrypted
+ * format would just give known plaintext).
+ */
+ buffer_put_bignum(&buffer, key->d);
+ buffer_put_bignum(&buffer, key->iqmp);
+ buffer_put_bignum(&buffer, key->q); /* reverse from SSL p */
+ buffer_put_bignum(&buffer, key->p); /* reverse from SSL q */
+
+ /* Pad the part to be encrypted until its size is a multiple of 8. */
+ while (buffer_len(&buffer) % 8 != 0)
+ buffer_put_char(&buffer, 0);
+
+ /* This buffer will be used to contain the data in the file. */
+ buffer_init(&encrypted);
+
+ /* First store keyfile id string. */
+ cp = AUTHFILE_ID_STRING;
+ for (i = 0; cp[i]; i++)
+ buffer_put_char(&encrypted, cp[i]);
+ buffer_put_char(&encrypted, 0);
+
+ /* Store cipher type. */
+ buffer_put_char(&encrypted, cipher_type);
+ buffer_put_int(&encrypted, 0); /* For future extension */
+
+ /* Store public key. This will be in plain text. */
+ buffer_put_int(&encrypted, BN_num_bits(key->n));
+ buffer_put_bignum(&encrypted, key->n);
+ buffer_put_bignum(&encrypted, key->e);
+ buffer_put_string(&encrypted, comment, strlen(comment));
+
+ /* Allocate space for the private part of the key in the buffer. */
+ buffer_append_space(&encrypted, &cp, buffer_len(&buffer));
+
+ cipher_set_key_string(&cipher, cipher_type, passphrase, 1);
+ cipher_encrypt(&cipher, (unsigned char *) cp,
+ (unsigned char *) buffer_ptr(&buffer),
+ buffer_len(&buffer));
+ memset(&cipher, 0, sizeof(cipher));
+
+ /* Destroy temporary data. */
+ memset(buf, 0, sizeof(buf));
+ buffer_free(&buffer);
+
+ fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
+ if (fd < 0)
+ return 0;
+ if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
+ buffer_len(&encrypted)) {
+ debug("Write to key file %.200s failed: %.100s", filename,
+ strerror(errno));
+ buffer_free(&encrypted);
+ close(fd);
+ remove(filename);
+ return 0;
+ }
+ close(fd);
+ buffer_free(&encrypted);
+ return 1;