version 1.45, 2001/12/29 21:56:01 |
version 1.46, 2002/02/14 23:41:01 |
|
|
{ |
{ |
Buffer buffer, encrypted; |
Buffer buffer, encrypted; |
u_char buf[100], *cp; |
u_char buf[100], *cp; |
int fd, i; |
int fd, i, cipher_num; |
CipherContext ciphercontext; |
CipherContext ciphercontext; |
Cipher *cipher; |
Cipher *cipher; |
u_int32_t rand; |
u_int32_t rand; |
|
|
* If the passphrase is empty, use SSH_CIPHER_NONE to ease converting |
* If the passphrase is empty, use SSH_CIPHER_NONE to ease converting |
* to another cipher; otherwise use SSH_AUTHFILE_CIPHER. |
* to another cipher; otherwise use SSH_AUTHFILE_CIPHER. |
*/ |
*/ |
if (strcmp(passphrase, "") == 0) |
cipher_num = (strcmp(passphrase, "") == 0) ? |
cipher = cipher_by_number(SSH_CIPHER_NONE); |
SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER; |
else |
if ((cipher = cipher_by_number(cipher_num)) == NULL) |
cipher = cipher_by_number(SSH_AUTHFILE_CIPHER); |
|
if (cipher == NULL) |
|
fatal("save_private_key_rsa: bad cipher"); |
fatal("save_private_key_rsa: bad cipher"); |
|
|
/* This buffer is used to built the secret part of the private key. */ |
/* This buffer is used to built the secret part of the private key. */ |
|
|
buffer_put_char(&encrypted, 0); |
buffer_put_char(&encrypted, 0); |
|
|
/* Store cipher type. */ |
/* Store cipher type. */ |
buffer_put_char(&encrypted, cipher->number); |
buffer_put_char(&encrypted, cipher_num); |
buffer_put_int(&encrypted, 0); /* For future extension */ |
buffer_put_int(&encrypted, 0); /* For future extension */ |
|
|
/* Store public key. This will be in plain text. */ |
/* Store public key. This will be in plain text. */ |
|
|
/* Allocate space for the private part of the key in the buffer. */ |
/* Allocate space for the private part of the key in the buffer. */ |
cp = buffer_append_space(&encrypted, buffer_len(&buffer)); |
cp = buffer_append_space(&encrypted, buffer_len(&buffer)); |
|
|
cipher_set_key_string(&ciphercontext, cipher, passphrase); |
cipher_set_key_string(&ciphercontext, cipher, passphrase, |
cipher_encrypt(&ciphercontext, cp, |
CIPHER_ENCRYPT); |
|
cipher_crypt(&ciphercontext, cp, |
buffer_ptr(&buffer), buffer_len(&buffer)); |
buffer_ptr(&buffer), buffer_len(&buffer)); |
|
cipher_cleanup(&ciphercontext); |
memset(&ciphercontext, 0, sizeof(ciphercontext)); |
memset(&ciphercontext, 0, sizeof(ciphercontext)); |
|
|
/* Destroy temporary data. */ |
/* Destroy temporary data. */ |
|
|
cp = buffer_append_space(&decrypted, buffer_len(&buffer)); |
cp = buffer_append_space(&decrypted, buffer_len(&buffer)); |
|
|
/* Rest of the buffer is encrypted. Decrypt it using the passphrase. */ |
/* Rest of the buffer is encrypted. Decrypt it using the passphrase. */ |
cipher_set_key_string(&ciphercontext, cipher, passphrase); |
cipher_set_key_string(&ciphercontext, cipher, passphrase, |
cipher_decrypt(&ciphercontext, cp, |
CIPHER_DECRYPT); |
|
cipher_crypt(&ciphercontext, cp, |
buffer_ptr(&buffer), buffer_len(&buffer)); |
buffer_ptr(&buffer), buffer_len(&buffer)); |
|
cipher_cleanup(&ciphercontext); |
memset(&ciphercontext, 0, sizeof(ciphercontext)); |
memset(&ciphercontext, 0, sizeof(ciphercontext)); |
buffer_free(&buffer); |
buffer_free(&buffer); |
|
|