Add sshpubk.c to load authenticating keys from files
[u/mdw/putty] / sshpubk.c
CommitLineData
3eca8453 1/*
2 * Read SSH public keys from files.
3 *
4 * First implementation: only supports unencrypted SSH 1.1 format
5 * RSA keys. Encryption, and SSH 2 DSS keys, to be supported later.
6 */
7
8#include <stdio.h>
9
10#include <stdio.h> /* FIXME */
11#include <stdarg.h> /* FIXME */
12#include <windows.h> /* FIXME */
13#include "putty.h" /* FIXME */
14
15#include "ssh.h"
16
17#define GET_32BIT(cp) \
18 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
19 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
20 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
21 ((unsigned long)(unsigned char)(cp)[3]))
22
23#define rsa_signature "SSH PRIVATE KEY FILE FORMAT 1.1\n"
24
25int loadrsakey(char *filename, struct RSAKey *key, char *passphrase) {
26 FILE *fp;
27 unsigned char buf[16384];
28 unsigned char keybuf[16];
29 int len;
30 int i, j, ciphertype;
31 int ret = 0;
32 struct MD5Context md5c;
33
34 fp = fopen(filename, "rb");
35 if (!fp)
36 goto end;
37
38 /* Slurp the whole file into a buffer. */
39 len = fread(buf, 1, sizeof(buf), fp);
40 fclose(fp);
41 if (len < 0 || len == sizeof(buf))
42 goto end; /* file too big or not read */
43
44 if (len < sizeof(rsa_signature) ||
45 memcmp(buf, rsa_signature, sizeof(rsa_signature)) != 0)
46 goto end; /* failure to have sig at front */
47
48 i = sizeof(rsa_signature);
49
50 /* Next, one byte giving encryption type, and one reserved uint32. */
51 if (len-i < 1)
52 goto end;
53 ciphertype = buf[i];
54 if (ciphertype != 0 && ciphertype != SSH_CIPHER_3DES)
55 goto end;
56 i++;
57 if (len-i < 4)
58 goto end; /* reserved field not present */
59 if (buf[i] != 0 || buf[i+1] != 0 || buf[i+2] != 0 || buf[i+3] != 0)
60 goto end; /* reserved field nonzero, panic! */
61 i += 4;
62
63 /* Now the serious stuff. An ordinary SSH 1 public key. */
64 i += makekey(buf+i, key, NULL, 1);
65 if (len-i < 0)
66 goto end; /* overran */
67
68 /* Next, the comment field. */
69 j = GET_32BIT(buf+i);
70 if (len-i < 4+j) goto end; i += 4+j;
71 /*
72 * FIXME: might need to use this string.
73 */
74
75 /*
76 * Decrypt remainder of buffer.
77 */
78 if (ciphertype) {
79 MD5Init(&md5c);
80 MD5Update(&md5c, passphrase, strlen(passphrase));
81 MD5Final(keybuf, &md5c);
82 des3_decrypt_pubkey(keybuf, buf+i, (len-i+7)&~7);
83 memset(keybuf, 0, sizeof(buf)); /* burn the evidence */
84 }
85
86 /*
87 * We are now in the secret part of the key. The first four
88 * bytes should be of the form a, b, a, b.
89 */
90 if (len-i < 4) goto end;
91 if (buf[i] != buf[i+2] || buf[i+1] != buf[i+3]) { ret = -1; goto end; }
92 i += 4;
93
94 /*
95 * After that, we have one further bignum which is our
96 * decryption modulus, and then we're done.
97 */
98 i += makeprivate(buf+i, key);
99 if (len-i < 0) goto end;
100
101 ret = 1;
102 end:
103 memset(buf, 0, sizeof(buf)); /* burn the evidence */
104 return ret;
105}
106
107/*
108 * See whether an RSA key is encrypted.
109 */
110int rsakey_encrypted(char *filename) {
111 FILE *fp;
112 unsigned char buf[1+sizeof(rsa_signature)];
113 int len;
114
115 fp = fopen(filename, "rb");
116 if (!fp)
117 return 0; /* doesn't even exist */
118
119 /* Slurp the whole file into a buffer. */
120 len = fread(buf, 1, sizeof(buf), fp);
121 fclose(fp);
122 if (len < sizeof(buf))
123 return 0; /* not even valid */
124 if (buf[sizeof(buf)-1])
125 return 1; /* encrypted */
126 return 0;
127}