/* Version identification string for SSH v1 identity files. */
static const char authfile_id_string[] =
/* Version identification string for SSH v1 identity files. */
static const char authfile_id_string[] =
key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
const char *comment)
{
Buffer buffer, encrypted;
key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
const char *comment)
{
Buffer buffer, encrypted;
* If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
* to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
*/
* 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 = cipher_by_number(SSH_CIPHER_NONE);
- else
- cipher = cipher_by_number(SSH_AUTHFILE_CIPHER);
- if (cipher == NULL)
+ cipher_num = (strcmp(passphrase, "") == 0) ?
+ SSH_CIPHER_NONE : SSH_AUTHFILE_CIPHER;
+ if ((cipher = cipher_by_number(cipher_num)) == NULL)
fatal("save_private_key_rsa: bad cipher");
/* This buffer is used to built the secret part of the private key. */
fatal("save_private_key_rsa: bad cipher");
/* This buffer is used to built the secret part of the private key. */
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->rsa->n));
buffer_put_bignum(&encrypted, key->rsa->n);
buffer_put_bignum(&encrypted, key->rsa->e);
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->rsa->n));
buffer_put_bignum(&encrypted, key->rsa->n);
buffer_put_bignum(&encrypted, key->rsa->e);
- cipher_set_key_string(&ciphercontext, cipher, passphrase);
- cipher_encrypt(&ciphercontext, (u_char *) cp,
- (u_char *) buffer_ptr(&buffer), buffer_len(&buffer));
+ cipher_set_key_string(&ciphercontext, cipher, passphrase,
+ CIPHER_ENCRYPT);
+ cipher_crypt(&ciphercontext, cp,
+ buffer_ptr(&buffer), buffer_len(&buffer));
+ cipher_cleanup(&ciphercontext);
memset(&ciphercontext, 0, sizeof(ciphercontext));
/* Destroy temporary data. */
memset(&ciphercontext, 0, sizeof(ciphercontext));
/* Destroy temporary data. */
if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
buffer_len(&encrypted)) {
if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
buffer_len(&encrypted)) {
key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
const char *comment)
{
key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
const char *comment)
{
- char *passphrase = (len > 0) ? (char *)_passphrase : NULL;
- EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
+ u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
+ const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
- case KEY_DSA:
- success = PEM_write_DSAPrivateKey(fp, key->dsa,
- cipher, passphrase, len, NULL, NULL);
- break;
- case KEY_RSA:
- success = PEM_write_RSAPrivateKey(fp, key->rsa,
- cipher, passphrase, len, NULL, NULL);
- break;
+ case KEY_DSA:
+ success = PEM_write_DSAPrivateKey(fp, key->dsa,
+ cipher, passphrase, len, NULL, NULL);
+ break;
+ case KEY_RSA:
+ success = PEM_write_RSAPrivateKey(fp, key->rsa,
+ cipher, passphrase, len, NULL, NULL);
+ break;
- len = lseek(fd, (off_t) 0, SEEK_END);
- lseek(fd, (off_t) 0, SEEK_SET);
+ if (fstat(fd, &st) < 0) {
+ error("fstat for key file %.200s failed: %.100s",
+ filename, strerror(errno));
+ return NULL;
+ }
+ len = st.st_size;
if (read(fd, cp, (size_t) len) != (size_t) len) {
debug("Read from key file %.200s failed: %.100s", filename,
if (read(fd, cp, (size_t) len) != (size_t) len) {
debug("Read from key file %.200s failed: %.100s", filename,
/* Check that it is at least big enough to contain the ID string. */
if (len < sizeof(authfile_id_string)) {
/* Check that it is at least big enough to contain the ID string. */
if (len < sizeof(authfile_id_string)) {
*/
for (i = 0; i < sizeof(authfile_id_string); i++)
if (buffer_get_char(&buffer) != authfile_id_string[i]) {
*/
for (i = 0; i < sizeof(authfile_id_string); i++)
if (buffer_get_char(&buffer) != authfile_id_string[i]) {
pub = key_new(KEY_RSA1);
buffer_get_bignum(&buffer, pub->rsa->n);
buffer_get_bignum(&buffer, pub->rsa->e);
pub = key_new(KEY_RSA1);
buffer_get_bignum(&buffer, pub->rsa->n);
buffer_get_bignum(&buffer, pub->rsa->e);
key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
char **commentp)
{
int i, check1, check2, cipher_type;
off_t len;
Buffer buffer, decrypted;
key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
char **commentp)
{
int i, check1, check2, cipher_type;
off_t len;
Buffer buffer, decrypted;
- len = lseek(fd, (off_t) 0, SEEK_END);
- lseek(fd, (off_t) 0, SEEK_SET);
+ if (fstat(fd, &st) < 0) {
+ error("fstat for key file %.200s failed: %.100s",
+ filename, strerror(errno));
+ close(fd);
+ return NULL;
+ }
+ len = st.st_size;
if (read(fd, cp, (size_t) len) != (size_t) len) {
debug("Read from key file %.200s failed: %.100s", filename,
if (read(fd, cp, (size_t) len) != (size_t) len) {
debug("Read from key file %.200s failed: %.100s", filename,
/* Check that it is at least big enough to contain the ID string. */
if (len < sizeof(authfile_id_string)) {
/* Check that it is at least big enough to contain the ID string. */
if (len < sizeof(authfile_id_string)) {
*/
for (i = 0; i < sizeof(authfile_id_string); i++)
if (buffer_get_char(&buffer) != authfile_id_string[i]) {
*/
for (i = 0; i < sizeof(authfile_id_string); i++)
if (buffer_get_char(&buffer) != authfile_id_string[i]) {
prv = key_new_private(KEY_RSA1);
buffer_get_bignum(&buffer, prv->rsa->n);
prv = key_new_private(KEY_RSA1);
buffer_get_bignum(&buffer, prv->rsa->n);
- cipher_set_key_string(&ciphercontext, cipher, passphrase);
- cipher_decrypt(&ciphercontext, (u_char *) cp,
- (u_char *) buffer_ptr(&buffer), buffer_len(&buffer));
+ cipher_set_key_string(&ciphercontext, cipher, passphrase,
+ CIPHER_DECRYPT);
+ cipher_crypt(&ciphercontext, cp,
+ buffer_ptr(&buffer), buffer_len(&buffer));
+ cipher_cleanup(&ciphercontext);
buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
/* calculate p-1 and q-1 */
buffer_get_bignum(&decrypted, prv->rsa->p); /* q */
/* calculate p-1 and q-1 */
- ctx = BN_CTX_new();
- aux = BN_new();
-
- BN_sub(aux, prv->rsa->q, BN_value_one());
- BN_mod(prv->rsa->dmq1, prv->rsa->d, aux, ctx);
-
- BN_sub(aux, prv->rsa->p, BN_value_one());
- BN_mod(prv->rsa->dmp1, prv->rsa->d, aux, ctx);
-
- BN_clear_free(aux);
- BN_CTX_free(ctx);
+ rsa_generate_additional_parameters(prv->rsa);
- /* check owner and modes */
+ if (fstat(fd, &st) < 0)
+ return 0;
+ /*
+ * if a key owned by the user is accessed, then we check the
+ * permissions of the file. if the key owned by a different user,
+ * then we don't care.
+ */
- if (fstat(fd, &st) < 0 ||
- (st.st_uid != 0 && getuid() != 0 && st.st_uid != getuid()) ||
- (st.st_mode & 077) != 0) {
- close(fd);
+ if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0) {
error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
- error("Bad ownership or mode(0%3.3o) for '%s'.",
- st.st_mode & 0777, filename);
+ error("Permissions 0%3.3o for '%s' are too open.",
+ (u_int)st.st_mode & 0777, filename);
error("It is recommended that your private key files are NOT accessible by others.");
error("This private key will be ignored.");
return 0;
error("It is recommended that your private key files are NOT accessible by others.");
error("This private key will be ignored.");
return 0;
int fd;
fd = open(filename, O_RDONLY);
if (fd < 0)
return NULL;
if (!key_perm_ok(fd, filename)) {
int fd;
fd = open(filename, O_RDONLY);
if (fd < 0)
return NULL;
if (!key_perm_ok(fd, filename)) {
- return key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
+ prv = key_load_private_pem(fd, KEY_UNSPEC, passphrase, NULL);
+ /* use the filename as a comment for PEM */
+ if (commentp && prv)
+ *commentp = xstrdup(filename);
- return key_load_private_rsa1(fd, filename, passphrase, NULL);
+ prv = key_load_private_rsa1(fd, filename, passphrase, NULL);
while (fgets(line, sizeof(line), f)) {
line[sizeof(line)-1] = '\0';
cp = line;
while (fgets(line, sizeof(line), f)) {
line[sizeof(line)-1] = '\0';
cp = line;
+
+ /* try rsa1 public key */
+ pub = key_new(KEY_RSA1);
+ if (key_try_load_public(pub, filename, commentp) == 1)
+ return pub;
+ key_free(pub);
+
+ /* try ssh2 public key */
pub = key_new(KEY_UNSPEC);
if (key_try_load_public(pub, filename, commentp) == 1)
return pub;
pub = key_new(KEY_UNSPEC);
if (key_try_load_public(pub, filename, commentp) == 1)
return pub;