diff options
Diffstat (limited to 'authfile.c')
-rw-r--r-- | authfile.c | 63 |
1 files changed, 36 insertions, 27 deletions
diff --git a/authfile.c b/authfile.c index 35a05d389..97d0a8783 100644 --- a/authfile.c +++ b/authfile.c | |||
@@ -15,7 +15,7 @@ | |||
15 | */ | 15 | */ |
16 | 16 | ||
17 | #include "includes.h" | 17 | #include "includes.h" |
18 | RCSID("$Id: authfile.c,v 1.4 1999/11/24 13:26:22 damien Exp $"); | 18 | RCSID("$Id: authfile.c,v 1.5 1999/11/25 00:54:58 damien Exp $"); |
19 | 19 | ||
20 | #ifdef HAVE_OPENSSL | 20 | #ifdef HAVE_OPENSSL |
21 | #include <openssl/bn.h> | 21 | #include <openssl/bn.h> |
@@ -33,10 +33,12 @@ RCSID("$Id: authfile.c,v 1.4 1999/11/24 13:26:22 damien Exp $"); | |||
33 | /* Version identification string for identity files. */ | 33 | /* Version identification string for identity files. */ |
34 | #define AUTHFILE_ID_STRING "SSH PRIVATE KEY FILE FORMAT 1.1\n" | 34 | #define AUTHFILE_ID_STRING "SSH PRIVATE KEY FILE FORMAT 1.1\n" |
35 | 35 | ||
36 | /* Saves the authentication (private) key in a file, encrypting it with | 36 | /* |
37 | passphrase. The identification of the file (lowest 64 bits of n) | 37 | * Saves the authentication (private) key in a file, encrypting it with |
38 | will precede the key to provide identification of the key without | 38 | * passphrase. The identification of the file (lowest 64 bits of n) will |
39 | needing a passphrase. */ | 39 | * precede the key to provide identification of the key without needing a |
40 | * passphrase. | ||
41 | */ | ||
40 | 42 | ||
41 | int | 43 | int |
42 | save_private_key(const char *filename, const char *passphrase, | 44 | save_private_key(const char *filename, const char *passphrase, |
@@ -49,9 +51,10 @@ save_private_key(const char *filename, const char *passphrase, | |||
49 | int cipher_type; | 51 | int cipher_type; |
50 | u_int32_t rand; | 52 | u_int32_t rand; |
51 | 53 | ||
52 | /* If the passphrase is empty, use SSH_CIPHER_NONE to ease | 54 | /* |
53 | converting to another cipher; otherwise use | 55 | * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting |
54 | SSH_AUTHFILE_CIPHER. */ | 56 | * to another cipher; otherwise use SSH_AUTHFILE_CIPHER. |
57 | */ | ||
55 | if (strcmp(passphrase, "") == 0) | 58 | if (strcmp(passphrase, "") == 0) |
56 | cipher_type = SSH_CIPHER_NONE; | 59 | cipher_type = SSH_CIPHER_NONE; |
57 | else | 60 | else |
@@ -68,9 +71,11 @@ save_private_key(const char *filename, const char *passphrase, | |||
68 | buf[3] = buf[1]; | 71 | buf[3] = buf[1]; |
69 | buffer_append(&buffer, buf, 4); | 72 | buffer_append(&buffer, buf, 4); |
70 | 73 | ||
71 | /* Store the private key (n and e will not be stored because they | 74 | /* |
72 | will be stored in plain text, and storing them also in | 75 | * Store the private key (n and e will not be stored because they |
73 | encrypted format would just give known plaintext). */ | 76 | * will be stored in plain text, and storing them also in encrypted |
77 | * format would just give known plaintext). | ||
78 | */ | ||
74 | buffer_put_bignum(&buffer, key->d); | 79 | buffer_put_bignum(&buffer, key->d); |
75 | buffer_put_bignum(&buffer, key->iqmp); | 80 | buffer_put_bignum(&buffer, key->iqmp); |
76 | buffer_put_bignum(&buffer, key->q); /* reverse from SSL p */ | 81 | buffer_put_bignum(&buffer, key->q); /* reverse from SSL p */ |
@@ -112,11 +117,9 @@ save_private_key(const char *filename, const char *passphrase, | |||
112 | memset(buf, 0, sizeof(buf)); | 117 | memset(buf, 0, sizeof(buf)); |
113 | buffer_free(&buffer); | 118 | buffer_free(&buffer); |
114 | 119 | ||
115 | /* Write to a file. */ | ||
116 | f = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); | 120 | f = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
117 | if (f < 0) | 121 | if (f < 0) |
118 | return 0; | 122 | return 0; |
119 | |||
120 | if (write(f, buffer_ptr(&encrypted), buffer_len(&encrypted)) != | 123 | if (write(f, buffer_ptr(&encrypted), buffer_len(&encrypted)) != |
121 | buffer_len(&encrypted)) { | 124 | buffer_len(&encrypted)) { |
122 | debug("Write to key file %.200s failed: %.100s", filename, | 125 | debug("Write to key file %.200s failed: %.100s", filename, |
@@ -131,9 +134,11 @@ save_private_key(const char *filename, const char *passphrase, | |||
131 | return 1; | 134 | return 1; |
132 | } | 135 | } |
133 | 136 | ||
134 | /* Loads the public part of the key file. Returns 0 if an error | 137 | /* |
135 | was encountered (the file does not exist or is not readable), and | 138 | * Loads the public part of the key file. Returns 0 if an error was |
136 | non-zero otherwise. */ | 139 | * encountered (the file does not exist or is not readable), and non-zero |
140 | * otherwise. | ||
141 | */ | ||
137 | 142 | ||
138 | int | 143 | int |
139 | load_public_key(const char *filename, RSA * pub, | 144 | load_public_key(const char *filename, RSA * pub, |
@@ -144,11 +149,9 @@ load_public_key(const char *filename, RSA * pub, | |||
144 | Buffer buffer; | 149 | Buffer buffer; |
145 | char *cp; | 150 | char *cp; |
146 | 151 | ||
147 | /* Read data from the file into the buffer. */ | ||
148 | f = open(filename, O_RDONLY); | 152 | f = open(filename, O_RDONLY); |
149 | if (f < 0) | 153 | if (f < 0) |
150 | return 0; | 154 | return 0; |
151 | |||
152 | len = lseek(f, (off_t) 0, SEEK_END); | 155 | len = lseek(f, (off_t) 0, SEEK_END); |
153 | lseek(f, (off_t) 0, SEEK_SET); | 156 | lseek(f, (off_t) 0, SEEK_SET); |
154 | 157 | ||
@@ -170,8 +173,10 @@ load_public_key(const char *filename, RSA * pub, | |||
170 | buffer_free(&buffer); | 173 | buffer_free(&buffer); |
171 | return 0; | 174 | return 0; |
172 | } | 175 | } |
173 | /* Make sure it begins with the id string. Consume the id string | 176 | /* |
174 | from the buffer. */ | 177 | * Make sure it begins with the id string. Consume the id string |
178 | * from the buffer. | ||
179 | */ | ||
175 | for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++) | 180 | for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++) |
176 | if (buffer_get_char(&buffer) != (unsigned char) AUTHFILE_ID_STRING[i]) { | 181 | if (buffer_get_char(&buffer) != (unsigned char) AUTHFILE_ID_STRING[i]) { |
177 | debug("Bad key file %.200s.", filename); | 182 | debug("Bad key file %.200s.", filename); |
@@ -197,9 +202,12 @@ load_public_key(const char *filename, RSA * pub, | |||
197 | return 1; | 202 | return 1; |
198 | } | 203 | } |
199 | 204 | ||
200 | /* Loads the private key from the file. Returns 0 if an error is encountered | 205 | /* |
201 | (file does not exist or is not readable, or passphrase is bad). | 206 | * Loads the private key from the file. Returns 0 if an error is encountered |
202 | This initializes the private key. */ | 207 | * (file does not exist or is not readable, or passphrase is bad). This |
208 | * initializes the private key. | ||
209 | * Assumes we are called under uid of the owner of the file. | ||
210 | */ | ||
203 | 211 | ||
204 | int | 212 | int |
205 | load_private_key(const char *filename, const char *passphrase, | 213 | load_private_key(const char *filename, const char *passphrase, |
@@ -214,12 +222,11 @@ load_private_key(const char *filename, const char *passphrase, | |||
214 | BIGNUM *aux; | 222 | BIGNUM *aux; |
215 | struct stat st; | 223 | struct stat st; |
216 | 224 | ||
217 | /* Read the file into the buffer. */ | ||
218 | f = open(filename, O_RDONLY); | 225 | f = open(filename, O_RDONLY); |
219 | if (f < 0) | 226 | if (f < 0) |
220 | return 0; | 227 | return 0; |
221 | 228 | ||
222 | /* We assume we are called under uid of the owner of the file */ | 229 | /* check owner and modes */ |
223 | if (fstat(f, &st) < 0 || | 230 | if (fstat(f, &st) < 0 || |
224 | (st.st_uid != 0 && st.st_uid != getuid()) || | 231 | (st.st_uid != 0 && st.st_uid != getuid()) || |
225 | (st.st_mode & 077) != 0) { | 232 | (st.st_mode & 077) != 0) { |
@@ -252,8 +259,10 @@ load_private_key(const char *filename, const char *passphrase, | |||
252 | buffer_free(&buffer); | 259 | buffer_free(&buffer); |
253 | return 0; | 260 | return 0; |
254 | } | 261 | } |
255 | /* Make sure it begins with the id string. Consume the id string | 262 | /* |
256 | from the buffer. */ | 263 | * Make sure it begins with the id string. Consume the id string |
264 | * from the buffer. | ||
265 | */ | ||
257 | for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++) | 266 | for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++) |
258 | if (buffer_get_char(&buffer) != (unsigned char) AUTHFILE_ID_STRING[i]) { | 267 | if (buffer_get_char(&buffer) != (unsigned char) AUTHFILE_ID_STRING[i]) { |
259 | debug("Bad key file %.200s.", filename); | 268 | debug("Bad key file %.200s.", filename); |