]> andersk Git - openssh.git/blobdiff - authfile.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / authfile.c
index 6a04cd7a95a290caa9b806b221a374fd3d478ed0..2c615709da7a4dd2eec1896fbfcb00dc8ca91e62 100644 (file)
@@ -1,3 +1,4 @@
+/* $OpenBSD: authfile.c,v 1.79 2010/01/12 00:16:47 dtucker Exp $ */
 /*
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: authfile.c,v 1.60 2004/12/11 01:48:56 dtucker Exp $");
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <sys/uio.h>
 
 #include <openssl/err.h>
 #include <openssl/evp.h>
 #include <openssl/pem.h>
 
-#include "cipher.h"
+/* compatibility with old or broken OpenSSL versions */
+#include "openbsd-compat/openssl-compat.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
 #include "xmalloc.h"
+#include "cipher.h"
 #include "buffer.h"
-#include "bufaux.h"
 #include "key.h"
 #include "ssh.h"
 #include "log.h"
 #include "authfile.h"
 #include "rsa.h"
 #include "misc.h"
+#include "atomicio.h"
 
 /* Version identification string for SSH v1 identity files. */
 static const char authfile_id_string[] =
@@ -147,8 +163,8 @@ key_save_private_rsa1(Key *key, const char *filename, const char *passphrase,
                buffer_free(&encrypted);
                return 0;
        }
-       if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
-           buffer_len(&encrypted)) {
+       if (atomicio(vwrite, fd, buffer_ptr(&encrypted),
+           buffer_len(&encrypted)) != buffer_len(&encrypted)) {
                error("write to key file %s failed: %s", filename,
                    strerror(errno));
                buffer_free(&encrypted);
@@ -171,7 +187,11 @@ key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
        int success = 0;
        int len = strlen(_passphrase);
        u_char *passphrase = (len > 0) ? (u_char *)_passphrase : NULL;
+#if (OPENSSL_VERSION_NUMBER < 0x00907000L)
        const EVP_CIPHER *cipher = (len > 0) ? EVP_des_ede3_cbc() : NULL;
+#else
+       const EVP_CIPHER *cipher = (len > 0) ? EVP_aes_128_cbc() : NULL;
+#endif
 
        if (len > 0 && len <= 4) {
                error("passphrase too short: have %d bytes, need > 4", len);
@@ -183,7 +203,7 @@ key_save_private_pem(Key *key, const char *filename, const char *_passphrase,
                return 0;
        }
        fp = fdopen(fd, "w");
-       if (fp == NULL ) {
+       if (fp == NULL) {
                error("fdopen %s failed: %s.", filename, strerror(errno));
                close(fd);
                return 0;
@@ -210,12 +230,10 @@ key_save_private(Key *key, const char *filename, const char *passphrase,
        case KEY_RSA1:
                return key_save_private_rsa1(key, filename, passphrase,
                    comment);
-               break;
        case KEY_DSA:
        case KEY_RSA:
                return key_save_private_pem(key, filename, passphrase,
                    comment);
-               break;
        default:
                break;
        }
@@ -236,7 +254,7 @@ key_load_public_rsa1(int fd, const char *filename, char **commentp)
        Key *pub;
        struct stat st;
        char *cp;
-       int i;
+       u_int i;
        size_t len;
 
        if (fstat(fd, &st) < 0) {
@@ -253,7 +271,7 @@ key_load_public_rsa1(int fd, const char *filename, char **commentp)
        buffer_init(&buffer);
        cp = buffer_append_space(&buffer, len);
 
-       if (read(fd, cp, (size_t) len) != (size_t) len) {
+       if (atomicio(read, fd, cp, len) != len) {
                debug("Read from key file %.200s failed: %.100s", filename,
                    strerror(errno));
                buffer_free(&buffer);
@@ -322,7 +340,8 @@ static Key *
 key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
     char **commentp)
 {
-       int i, check1, check2, cipher_type;
+       u_int i;
+       int check1, check2, cipher_type;
        size_t len;
        Buffer buffer, decrypted;
        u_char *cp;
@@ -347,7 +366,7 @@ key_load_private_rsa1(int fd, const char *filename, const char *passphrase,
        buffer_init(&buffer);
        cp = buffer_append_space(&buffer, len);
 
-       if (read(fd, cp, (size_t) len) != (size_t) len) {
+       if (atomicio(read, fd, cp, len) != len) {
                debug("Read from key file %.200s failed: %.100s", filename,
                    strerror(errno));
                buffer_free(&buffer);
@@ -505,7 +524,7 @@ key_load_private_pem(int fd, int type, const char *passphrase,
        return prv;
 }
 
-static int
+int
 key_perm_ok(int fd, const char *filename)
 {
        struct stat st;
@@ -535,30 +554,37 @@ key_perm_ok(int fd, const char *filename)
 
 Key *
 key_load_private_type(int type, const char *filename, const char *passphrase,
-    char **commentp)
+    char **commentp, int *perm_ok)
 {
        int fd;
 
        fd = open(filename, O_RDONLY);
-       if (fd < 0)
+       if (fd < 0) {
+               debug("could not open key file '%s': %s", filename,
+                   strerror(errno));
+               if (perm_ok != NULL)
+                       *perm_ok = 0;
                return NULL;
+       }
        if (!key_perm_ok(fd, filename)) {
+               if (perm_ok != NULL)
+                       *perm_ok = 0;
                error("bad permissions: ignore key: %s", filename);
                close(fd);
                return NULL;
        }
+       if (perm_ok != NULL)
+               *perm_ok = 1;
        switch (type) {
        case KEY_RSA1:
                return key_load_private_rsa1(fd, filename, passphrase,
                    commentp);
                /* closes fd */
-               break;
        case KEY_DSA:
        case KEY_RSA:
        case KEY_UNSPEC:
                return key_load_private_pem(fd, type, passphrase, commentp);
                /* closes fd */
-               break;
        default:
                close(fd);
                break;
@@ -574,8 +600,11 @@ key_load_private(const char *filename, const char *passphrase,
        int fd;
 
        fd = open(filename, O_RDONLY);
-       if (fd < 0)
+       if (fd < 0) {
+               debug("could not open key file '%s': %s", filename,
+                   strerror(errno));
                return NULL;
+       }
        if (!key_perm_ok(fd, filename)) {
                error("bad permissions: ignore key: %s", filename);
                close(fd);
This page took 0.144502 seconds and 4 git commands to generate.