blob: f93c9d4705bb4ca845aed1254f42e5fa0d77db3f [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * authfile.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Mon Mar 27 03:52:05 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 * This file contains functions for reading and writing identity files, and
13 * for reading the passphrase from the user.
Damien Miller4af51302000-04-16 11:18:38 +100014 *
Damien Miller95def091999-11-25 00:26:21 +110015 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100016
17#include "includes.h"
Damien Millereba71ba2000-04-29 23:57:08 +100018RCSID("$Id: authfile.c,v 1.12 2000/04/29 13:57:10 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
20#include <openssl/bn.h>
Damien Millereba71ba2000-04-29 23:57:08 +100021#include <openssl/dsa.h>
22#include <openssl/rsa.h>
23#include <openssl/pem.h>
24#include <openssl/evp.h>
25
Damien Millerd4a8b7e1999-10-27 13:42:43 +100026#include "xmalloc.h"
27#include "buffer.h"
28#include "bufaux.h"
29#include "cipher.h"
30#include "ssh.h"
Damien Millereba71ba2000-04-29 23:57:08 +100031#include "key.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032
33/* Version identification string for identity files. */
34#define AUTHFILE_ID_STRING "SSH PRIVATE KEY FILE FORMAT 1.1\n"
35
Damien Miller5428f641999-11-25 11:54:57 +110036/*
37 * Saves the authentication (private) key in a file, encrypting it with
38 * passphrase. The identification of the file (lowest 64 bits of n) will
39 * precede the key to provide identification of the key without needing a
40 * passphrase.
41 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
43int
Damien Millereba71ba2000-04-29 23:57:08 +100044save_private_key_rsa(const char *filename, const char *passphrase,
45 RSA *key, const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046{
Damien Miller95def091999-11-25 00:26:21 +110047 Buffer buffer, encrypted;
48 char buf[100], *cp;
Damien Miller037a0dc1999-12-07 15:38:31 +110049 int fd, i;
Damien Miller95def091999-11-25 00:26:21 +110050 CipherContext cipher;
51 int cipher_type;
52 u_int32_t rand;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Damien Miller5428f641999-11-25 11:54:57 +110054 /*
55 * If the passphrase is empty, use SSH_CIPHER_NONE to ease converting
56 * to another cipher; otherwise use SSH_AUTHFILE_CIPHER.
57 */
Damien Miller95def091999-11-25 00:26:21 +110058 if (strcmp(passphrase, "") == 0)
59 cipher_type = SSH_CIPHER_NONE;
60 else
61 cipher_type = SSH_AUTHFILE_CIPHER;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Damien Miller95def091999-11-25 00:26:21 +110063 /* This buffer is used to built the secret part of the private key. */
64 buffer_init(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065
Damien Miller95def091999-11-25 00:26:21 +110066 /* Put checkbytes for checking passphrase validity. */
67 rand = arc4random();
68 buf[0] = rand & 0xff;
69 buf[1] = (rand >> 8) & 0xff;
70 buf[2] = buf[0];
71 buf[3] = buf[1];
72 buffer_append(&buffer, buf, 4);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073
Damien Miller5428f641999-11-25 11:54:57 +110074 /*
75 * Store the private key (n and e will not be stored because they
76 * will be stored in plain text, and storing them also in encrypted
77 * format would just give known plaintext).
78 */
Damien Miller95def091999-11-25 00:26:21 +110079 buffer_put_bignum(&buffer, key->d);
80 buffer_put_bignum(&buffer, key->iqmp);
81 buffer_put_bignum(&buffer, key->q); /* reverse from SSL p */
82 buffer_put_bignum(&buffer, key->p); /* reverse from SSL q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100083
Damien Miller95def091999-11-25 00:26:21 +110084 /* Pad the part to be encrypted until its size is a multiple of 8. */
85 while (buffer_len(&buffer) % 8 != 0)
86 buffer_put_char(&buffer, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Miller95def091999-11-25 00:26:21 +110088 /* This buffer will be used to contain the data in the file. */
89 buffer_init(&encrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100090
Damien Miller95def091999-11-25 00:26:21 +110091 /* First store keyfile id string. */
92 cp = AUTHFILE_ID_STRING;
93 for (i = 0; cp[i]; i++)
94 buffer_put_char(&encrypted, cp[i]);
95 buffer_put_char(&encrypted, 0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
Damien Miller95def091999-11-25 00:26:21 +110097 /* Store cipher type. */
98 buffer_put_char(&encrypted, cipher_type);
99 buffer_put_int(&encrypted, 0); /* For future extension */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000100
Damien Miller95def091999-11-25 00:26:21 +1100101 /* Store public key. This will be in plain text. */
102 buffer_put_int(&encrypted, BN_num_bits(key->n));
103 buffer_put_bignum(&encrypted, key->n);
104 buffer_put_bignum(&encrypted, key->e);
105 buffer_put_string(&encrypted, comment, strlen(comment));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000106
Damien Miller95def091999-11-25 00:26:21 +1100107 /* Allocate space for the private part of the key in the buffer. */
108 buffer_append_space(&encrypted, &cp, buffer_len(&buffer));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109
Damien Miller1383bd82000-04-06 12:32:37 +1000110 cipher_set_key_string(&cipher, cipher_type, passphrase);
Damien Miller95def091999-11-25 00:26:21 +1100111 cipher_encrypt(&cipher, (unsigned char *) cp,
112 (unsigned char *) buffer_ptr(&buffer),
113 buffer_len(&buffer));
114 memset(&cipher, 0, sizeof(cipher));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
Damien Miller95def091999-11-25 00:26:21 +1100116 /* Destroy temporary data. */
117 memset(buf, 0, sizeof(buf));
118 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000119
Damien Miller037a0dc1999-12-07 15:38:31 +1100120 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
121 if (fd < 0)
Damien Miller95def091999-11-25 00:26:21 +1100122 return 0;
Damien Miller037a0dc1999-12-07 15:38:31 +1100123 if (write(fd, buffer_ptr(&encrypted), buffer_len(&encrypted)) !=
Damien Miller95def091999-11-25 00:26:21 +1100124 buffer_len(&encrypted)) {
125 debug("Write to key file %.200s failed: %.100s", filename,
126 strerror(errno));
127 buffer_free(&encrypted);
Damien Miller037a0dc1999-12-07 15:38:31 +1100128 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100129 remove(filename);
130 return 0;
131 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100132 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100133 buffer_free(&encrypted);
134 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135}
136
Damien Millereba71ba2000-04-29 23:57:08 +1000137/* save DSA key in OpenSSL PEM format */
138
139int
140save_private_key_dsa(const char *filename, const char *passphrase,
141 DSA *dsa, const char *comment)
142{
143 FILE *fp;
144 int fd;
145 int success = 1;
146 int len = strlen(passphrase);
147
148 if (len > 0 && len <= 4) {
149 error("passphrase too short: %d bytes", len);
150 errno = 0;
151 return 0;
152 }
153 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
154 if (fd < 0) {
155 debug("open %s failed", filename);
156 return 0;
157 }
158 fp = fdopen(fd, "w");
159 if (fp == NULL ) {
160 debug("fdopen %s failed", filename);
161 close(fd);
162 return 0;
163 }
164 if (len > 0) {
165 if (!PEM_write_DSAPrivateKey(fp, dsa, EVP_des_ede3_cbc(),
166 (char *)passphrase, strlen(passphrase), NULL, NULL))
167 success = 0;
168 } else {
169 if (!PEM_write_DSAPrivateKey(fp, dsa, NULL,
170 NULL, 0, NULL, NULL))
171 success = 0;
172 }
173 fclose(fp);
174 return success;
175}
176
177int
178save_private_key(const char *filename, const char *passphrase, Key *key,
179 const char *comment)
180{
181 switch (key->type) {
182 case KEY_RSA:
183 return save_private_key_rsa(filename, passphrase, key->rsa, comment);
184 break;
185 case KEY_DSA:
186 return save_private_key_dsa(filename, passphrase, key->dsa, comment);
187 break;
188 default:
189 break;
190 }
191 return 0;
192}
193
Damien Miller5428f641999-11-25 11:54:57 +1100194/*
195 * Loads the public part of the key file. Returns 0 if an error was
196 * encountered (the file does not exist or is not readable), and non-zero
197 * otherwise.
198 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199
200int
Damien Millereba71ba2000-04-29 23:57:08 +1000201load_public_key_rsa(const char *filename, RSA * pub, char **comment_return)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202{
Damien Miller037a0dc1999-12-07 15:38:31 +1100203 int fd, i;
Damien Miller95def091999-11-25 00:26:21 +1100204 off_t len;
205 Buffer buffer;
206 char *cp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000207
Damien Miller037a0dc1999-12-07 15:38:31 +1100208 fd = open(filename, O_RDONLY);
209 if (fd < 0)
Damien Miller95def091999-11-25 00:26:21 +1100210 return 0;
Damien Miller037a0dc1999-12-07 15:38:31 +1100211 len = lseek(fd, (off_t) 0, SEEK_END);
212 lseek(fd, (off_t) 0, SEEK_SET);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213
Damien Miller95def091999-11-25 00:26:21 +1100214 buffer_init(&buffer);
215 buffer_append_space(&buffer, &cp, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000216
Damien Miller037a0dc1999-12-07 15:38:31 +1100217 if (read(fd, cp, (size_t) len) != (size_t) len) {
Damien Miller95def091999-11-25 00:26:21 +1100218 debug("Read from key file %.200s failed: %.100s", filename,
Damien Millereba71ba2000-04-29 23:57:08 +1000219 strerror(errno));
Damien Miller95def091999-11-25 00:26:21 +1100220 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100221 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100222 return 0;
223 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100224 close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000225
Damien Miller95def091999-11-25 00:26:21 +1100226 /* Check that it is at least big enought to contain the ID string. */
227 if (len < strlen(AUTHFILE_ID_STRING) + 1) {
228 debug("Bad key file %.200s.", filename);
229 buffer_free(&buffer);
230 return 0;
231 }
Damien Miller5428f641999-11-25 11:54:57 +1100232 /*
233 * Make sure it begins with the id string. Consume the id string
234 * from the buffer.
235 */
Damien Miller95def091999-11-25 00:26:21 +1100236 for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++)
Damien Miller037a0dc1999-12-07 15:38:31 +1100237 if (buffer_get_char(&buffer) != (u_char) AUTHFILE_ID_STRING[i]) {
Damien Miller95def091999-11-25 00:26:21 +1100238 debug("Bad key file %.200s.", filename);
239 buffer_free(&buffer);
240 return 0;
241 }
242 /* Skip cipher type and reserved data. */
243 (void) buffer_get_char(&buffer); /* cipher type */
244 (void) buffer_get_int(&buffer); /* reserved */
245
246 /* Read the public key from the buffer. */
247 buffer_get_int(&buffer);
Damien Millereba71ba2000-04-29 23:57:08 +1000248 /* XXX alloc */
249 if (pub->n == NULL)
250 pub->n = BN_new();
Damien Miller95def091999-11-25 00:26:21 +1100251 buffer_get_bignum(&buffer, pub->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000252 /* XXX alloc */
253 if (pub->e == NULL)
254 pub->e = BN_new();
Damien Miller95def091999-11-25 00:26:21 +1100255 buffer_get_bignum(&buffer, pub->e);
256 if (comment_return)
257 *comment_return = buffer_get_string(&buffer, NULL);
258 /* The encrypted private part is not parsed by this function. */
259
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000260 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261
Damien Miller95def091999-11-25 00:26:21 +1100262 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263}
264
Damien Millereba71ba2000-04-29 23:57:08 +1000265int
266load_public_key(const char *filename, Key * key, char **comment_return)
267{
268 switch (key->type) {
269 case KEY_RSA:
270 return load_public_key_rsa(filename, key->rsa, comment_return);
271 break;
272 case KEY_DSA:
273 default:
274 break;
275 }
276 return 0;
277}
278
Damien Miller5428f641999-11-25 11:54:57 +1100279/*
280 * Loads the private key from the file. Returns 0 if an error is encountered
281 * (file does not exist or is not readable, or passphrase is bad). This
282 * initializes the private key.
283 * Assumes we are called under uid of the owner of the file.
284 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000285
286int
Damien Millereba71ba2000-04-29 23:57:08 +1000287load_private_key_rsa(int fd, const char *filename,
288 const char *passphrase, RSA * prv, char **comment_return)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289{
Damien Millereba71ba2000-04-29 23:57:08 +1000290 int i, check1, check2, cipher_type;
Damien Miller95def091999-11-25 00:26:21 +1100291 off_t len;
292 Buffer buffer, decrypted;
293 char *cp;
294 CipherContext cipher;
295 BN_CTX *ctx;
296 BIGNUM *aux;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000297
Damien Miller037a0dc1999-12-07 15:38:31 +1100298 len = lseek(fd, (off_t) 0, SEEK_END);
299 lseek(fd, (off_t) 0, SEEK_SET);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000300
Damien Miller95def091999-11-25 00:26:21 +1100301 buffer_init(&buffer);
302 buffer_append_space(&buffer, &cp, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
Damien Miller037a0dc1999-12-07 15:38:31 +1100304 if (read(fd, cp, (size_t) len) != (size_t) len) {
Damien Miller95def091999-11-25 00:26:21 +1100305 debug("Read from key file %.200s failed: %.100s", filename,
306 strerror(errno));
307 buffer_free(&buffer);
Damien Miller037a0dc1999-12-07 15:38:31 +1100308 close(fd);
Damien Miller95def091999-11-25 00:26:21 +1100309 return 0;
310 }
Damien Miller037a0dc1999-12-07 15:38:31 +1100311 close(fd);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000312
Damien Miller95def091999-11-25 00:26:21 +1100313 /* Check that it is at least big enought to contain the ID string. */
314 if (len < strlen(AUTHFILE_ID_STRING) + 1) {
315 debug("Bad key file %.200s.", filename);
316 buffer_free(&buffer);
317 return 0;
318 }
Damien Miller5428f641999-11-25 11:54:57 +1100319 /*
320 * Make sure it begins with the id string. Consume the id string
321 * from the buffer.
322 */
Damien Miller95def091999-11-25 00:26:21 +1100323 for (i = 0; i < (unsigned int) strlen(AUTHFILE_ID_STRING) + 1; i++)
324 if (buffer_get_char(&buffer) != (unsigned char) AUTHFILE_ID_STRING[i]) {
325 debug("Bad key file %.200s.", filename);
326 buffer_free(&buffer);
327 return 0;
328 }
329 /* Read cipher type. */
330 cipher_type = buffer_get_char(&buffer);
331 (void) buffer_get_int(&buffer); /* Reserved data. */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332
Damien Miller95def091999-11-25 00:26:21 +1100333 /* Read the public key from the buffer. */
334 buffer_get_int(&buffer);
335 prv->n = BN_new();
336 buffer_get_bignum(&buffer, prv->n);
337 prv->e = BN_new();
338 buffer_get_bignum(&buffer, prv->e);
339 if (comment_return)
340 *comment_return = buffer_get_string(&buffer, NULL);
341 else
342 xfree(buffer_get_string(&buffer, NULL));
343
344 /* Check that it is a supported cipher. */
Damien Miller1383bd82000-04-06 12:32:37 +1000345 if (((cipher_mask1() | SSH_CIPHER_NONE | SSH_AUTHFILE_CIPHER) &
Damien Miller95def091999-11-25 00:26:21 +1100346 (1 << cipher_type)) == 0) {
347 debug("Unsupported cipher %.100s used in key file %.200s.",
348 cipher_name(cipher_type), filename);
349 buffer_free(&buffer);
350 goto fail;
351 }
352 /* Initialize space for decrypted data. */
353 buffer_init(&decrypted);
354 buffer_append_space(&decrypted, &cp, buffer_len(&buffer));
355
356 /* Rest of the buffer is encrypted. Decrypt it using the passphrase. */
Damien Miller1383bd82000-04-06 12:32:37 +1000357 cipher_set_key_string(&cipher, cipher_type, passphrase);
Damien Miller95def091999-11-25 00:26:21 +1100358 cipher_decrypt(&cipher, (unsigned char *) cp,
359 (unsigned char *) buffer_ptr(&buffer),
360 buffer_len(&buffer));
361
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362 buffer_free(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000363
Damien Miller95def091999-11-25 00:26:21 +1100364 check1 = buffer_get_char(&decrypted);
365 check2 = buffer_get_char(&decrypted);
366 if (check1 != buffer_get_char(&decrypted) ||
367 check2 != buffer_get_char(&decrypted)) {
368 if (strcmp(passphrase, "") != 0)
369 debug("Bad passphrase supplied for key file %.200s.", filename);
370 /* Bad passphrase. */
371 buffer_free(&decrypted);
372fail:
373 BN_clear_free(prv->n);
Damien Millereba71ba2000-04-29 23:57:08 +1000374 prv->n = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100375 BN_clear_free(prv->e);
Damien Millereba71ba2000-04-29 23:57:08 +1000376 prv->e = NULL;
Damien Miller95def091999-11-25 00:26:21 +1100377 if (comment_return)
378 xfree(*comment_return);
379 return 0;
380 }
381 /* Read the rest of the private key. */
382 prv->d = BN_new();
383 buffer_get_bignum(&decrypted, prv->d);
384 prv->iqmp = BN_new();
385 buffer_get_bignum(&decrypted, prv->iqmp); /* u */
386 /* in SSL and SSH p and q are exchanged */
387 prv->q = BN_new();
388 buffer_get_bignum(&decrypted, prv->q); /* p */
389 prv->p = BN_new();
390 buffer_get_bignum(&decrypted, prv->p); /* q */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391
Damien Miller95def091999-11-25 00:26:21 +1100392 ctx = BN_CTX_new();
393 aux = BN_new();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000394
Damien Miller95def091999-11-25 00:26:21 +1100395 BN_sub(aux, prv->q, BN_value_one());
396 prv->dmq1 = BN_new();
397 BN_mod(prv->dmq1, prv->d, aux, ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000398
Damien Miller95def091999-11-25 00:26:21 +1100399 BN_sub(aux, prv->p, BN_value_one());
400 prv->dmp1 = BN_new();
401 BN_mod(prv->dmp1, prv->d, aux, ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000402
Damien Miller95def091999-11-25 00:26:21 +1100403 BN_clear_free(aux);
404 BN_CTX_free(ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000405
Damien Miller95def091999-11-25 00:26:21 +1100406 buffer_free(&decrypted);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000407
Damien Miller95def091999-11-25 00:26:21 +1100408 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000409}
Damien Millereba71ba2000-04-29 23:57:08 +1000410
411int
412load_private_key_dsa(int fd, const char *passphrase, Key *k, char **comment_return)
413{
414 DSA *dsa;
415 BIO *in;
416 FILE *fp;
417
418 in = BIO_new(BIO_s_file());
419 if (in == NULL) {
420 error("BIO_new failed");
421 return 0;
422 }
423 fp = fdopen(fd, "r");
424 if (fp == NULL) {
425 error("fdopen failed");
426 return 0;
427 }
428 BIO_set_fp(in, fp, BIO_NOCLOSE);
429 dsa = PEM_read_bio_DSAPrivateKey(in, NULL, NULL, (char *)passphrase);
430 if (dsa == NULL) {
431 debug("PEM_read_bio_DSAPrivateKey failed");
432 } else {
433 /* replace k->dsa with loaded key */
434 DSA_free(k->dsa);
435 k->dsa = dsa;
436 }
437 BIO_free(in);
438 fclose(fp);
439 if (comment_return)
440 *comment_return = xstrdup("dsa w/o comment");
441 debug("read DSA private key done");
442#ifdef DEBUG_DSS
443 DSA_print_fp(stderr, dsa, 8);
444#endif
445 return dsa != NULL ? 1 : 0;
446}
447
448int
449load_private_key(const char *filename, const char *passphrase, Key *key,
450 char **comment_return)
451{
452 int fd;
453 int ret = 0;
454 struct stat st;
455
456 fd = open(filename, O_RDONLY);
457 if (fd < 0)
458 return 0;
459
460 /* check owner and modes */
461 if (fstat(fd, &st) < 0 ||
462 (st.st_uid != 0 && st.st_uid != getuid()) ||
463 (st.st_mode & 077) != 0) {
464 close(fd);
465 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
466 error("@ WARNING: UNPROTECTED PRIVATE KEY FILE! @");
467 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
468 error("Bad ownership or mode(0%3.3o) for '%s'.",
469 st.st_mode & 0777, filename);
470 error("It is recommended that your private key files are NOT accessible by others.");
471 return 0;
472 }
473 switch (key->type) {
474 case KEY_RSA:
475 if (key->rsa->e != NULL) {
476 BN_clear_free(key->rsa->e);
477 key->rsa->e = NULL;
478 }
479 if (key->rsa->n != NULL) {
480 BN_clear_free(key->rsa->n);
481 key->rsa->n = NULL;
482 }
483 ret = load_private_key_rsa(fd, filename, passphrase,
484 key->rsa, comment_return);
485 break;
486 case KEY_DSA:
487 ret = load_private_key_dsa(fd, passphrase, key, comment_return);
488 default:
489 break;
490 }
491 close(fd);
492 return ret;
493}