1 |
/* |
/* |
2 |
* Copyright(C) 2008 TeraTerm Project |
* Copyright(C) 2008 TeraTerm Project |
3 |
*/ |
*/ |
4 |
// PuTTY is copyright 1997-2007 Simon Tatham. |
// PuTTY is copyright 1997-2007 Simon Tatham. |
5 |
|
|
6 |
#include <windows.h> |
#include <windows.h> |
7 |
#include <assert.h> |
#include <assert.h> |
8 |
|
|
9 |
#include "ssh.h" |
#include "ssh.h" |
10 |
#include "libputty.h" |
#include "libputty.h" |
11 |
|
|
12 |
/* |
/* |
13 |
* for SSH2 |
* for SSH2 |
14 |
* 鍵の一覧を得る |
* 鍵の一覧を得る |
15 |
*/ |
*/ |
16 |
int putty_get_ssh2_keylist(unsigned char **keylist) |
int putty_get_ssh2_keylist(unsigned char **keylist) |
17 |
{ |
{ |
18 |
int keylistlen; |
int keylistlen; |
19 |
|
|
20 |
*keylist = get_keylist2(&keylistlen); |
*keylist = get_keylist2(&keylistlen); |
21 |
if (*keylist == NULL){ |
if (*keylist == NULL){ |
22 |
// 取得に失敗 |
// 取得に失敗 |
23 |
return 0; |
return 0; |
24 |
} |
} |
25 |
return keylistlen; |
return keylistlen; |
26 |
} |
} |
27 |
|
|
28 |
/* |
/* |
29 |
* for SSH2 |
* for SSH2 |
30 |
* 公開鍵とデータ(同じく公開鍵)を渡し、 |
* 公開鍵とデータ(同じく公開鍵)を渡し、 |
31 |
* 公開鍵によって署名されたデータを得る |
* 公開鍵によって署名されたデータを得る |
32 |
*/ |
*/ |
33 |
void *putty_sign_ssh2_key(unsigned char *pubkey, |
void *putty_sign_ssh2_key(unsigned char *pubkey, |
34 |
unsigned char *data, |
unsigned char *data, |
35 |
int *outlen) |
int *outlen) |
36 |
{ |
{ |
37 |
void *ret; |
void *ret; |
38 |
|
|
39 |
unsigned char *request, *response; |
unsigned char *request, *response; |
40 |
void *vresponse; |
void *vresponse; |
41 |
int resplen, retval; |
int resplen, retval; |
42 |
int pubkeylen, datalen, reqlen; |
int pubkeylen, datalen, reqlen; |
43 |
|
|
44 |
pubkeylen = GET_32BIT(pubkey); |
pubkeylen = GET_32BIT(pubkey); |
45 |
datalen = GET_32BIT(data); |
datalen = GET_32BIT(data); |
46 |
reqlen = 4 + 1 + (4 + pubkeylen) + (4 + datalen); |
reqlen = 4 + 1 + (4 + pubkeylen) + (4 + datalen); |
47 |
request = (unsigned char *)malloc(reqlen); |
request = (unsigned char *)malloc(reqlen); |
48 |
|
|
49 |
// request length |
// request length |
50 |
PUT_32BIT(request, reqlen); |
PUT_32BIT(request, reqlen); |
51 |
// request type |
// request type |
52 |
request[4] = SSH2_AGENTC_SIGN_REQUEST; |
request[4] = SSH2_AGENTC_SIGN_REQUEST; |
53 |
// public key (length + data) |
// public key (length + data) |
54 |
memcpy(request + 5, pubkey, 4 + pubkeylen); |
memcpy(request + 5, pubkey, 4 + pubkeylen); |
55 |
// sign data (length + data) |
// sign data (length + data) |
56 |
memcpy(request + 5 + 4 + pubkeylen, data, 4 + datalen); |
memcpy(request + 5 + 4 + pubkeylen, data, 4 + datalen); |
57 |
|
|
58 |
retval = agent_query(request, reqlen, &vresponse, &resplen, NULL, NULL); |
retval = agent_query(request, reqlen, &vresponse, &resplen, NULL, NULL); |
59 |
assert(retval == 1); |
assert(retval == 1); |
60 |
response = vresponse; |
response = vresponse; |
61 |
if (resplen < 5 || response[4] != SSH2_AGENT_SIGN_RESPONSE) |
if (resplen < 5 || response[4] != SSH2_AGENT_SIGN_RESPONSE) |
62 |
return NULL; |
return NULL; |
63 |
|
|
64 |
ret = snewn(resplen-5, unsigned char); |
ret = snewn(resplen-5, unsigned char); |
65 |
memcpy(ret, response+5, resplen-5); |
memcpy(ret, response+5, resplen-5); |
66 |
sfree(response); |
sfree(response); |
67 |
|
|
68 |
if (outlen) |
if (outlen) |
69 |
*outlen = resplen-5; |
*outlen = resplen-5; |
70 |
|
|
71 |
return ret; |
return ret; |
72 |
} |
} |
73 |
|
|
74 |
/* |
/* |
75 |
* for SSH1 |
* for SSH1 |
76 |
* 鍵の一覧を得る |
* 鍵の一覧を得る |
77 |
*/ |
*/ |
78 |
int putty_get_ssh1_keylist(unsigned char **keylist) |
int putty_get_ssh1_keylist(unsigned char **keylist) |
79 |
{ |
{ |
80 |
int keylistlen; |
int keylistlen; |
81 |
|
|
82 |
*keylist = get_keylist1(&keylistlen); |
*keylist = get_keylist1(&keylistlen); |
83 |
if (*keylist == NULL){ |
if (*keylist == NULL){ |
84 |
// 取得に失敗 |
// 取得に失敗 |
85 |
return 0; |
return 0; |
86 |
} |
} |
87 |
return keylistlen; |
return keylistlen; |
88 |
} |
} |
89 |
|
|
90 |
/* |
/* |
91 |
* for SSH1 |
* for SSH1 |
92 |
* 公開鍵と暗号化データを渡し |
* 公開鍵と暗号化データを渡し |
93 |
* 復号データのハッシュを得る |
* 復号データのハッシュを得る |
94 |
*/ |
*/ |
95 |
void *putty_hash_ssh1_challenge(unsigned char *pubkey, |
void *putty_hash_ssh1_challenge(unsigned char *pubkey, |
96 |
int pubkeylen, |
int pubkeylen, |
97 |
unsigned char *data, |
unsigned char *data, |
98 |
int datalen, |
int datalen, |
99 |
unsigned char *session_id, |
unsigned char *session_id, |
100 |
int *outlen) |
int *outlen) |
101 |
{ |
{ |
102 |
void *ret; |
void *ret; |
103 |
|
|
104 |
unsigned char *request, *response, *p; |
unsigned char *request, *response, *p; |
105 |
void *vresponse; |
void *vresponse; |
106 |
int resplen, retval; |
int resplen, retval; |
107 |
int reqlen; |
int reqlen; |
108 |
|
|
109 |
reqlen = 4 + 1 + pubkeylen + datalen + 16 + 4; |
reqlen = 4 + 1 + pubkeylen + datalen + 16 + 4; |
110 |
request = (unsigned char *)malloc(reqlen); |
request = (unsigned char *)malloc(reqlen); |
111 |
p = request; |
p = request; |
112 |
|
|
113 |
// request length |
// request length |
114 |
PUT_32BIT(request, reqlen); |
PUT_32BIT(request, reqlen); |
115 |
// request type |
// request type |
116 |
request[4] = SSH1_AGENTC_RSA_CHALLENGE; |
request[4] = SSH1_AGENTC_RSA_CHALLENGE; |
117 |
p += 5; |
p += 5; |
118 |
|
|
119 |
// public key |
// public key |
120 |
memcpy(p, pubkey, pubkeylen); |
memcpy(p, pubkey, pubkeylen); |
121 |
p += pubkeylen; |
p += pubkeylen; |
122 |
// challange from server |
// challange from server |
123 |
memcpy(p, data, datalen); |
memcpy(p, data, datalen); |
124 |
p += datalen; |
p += datalen; |
125 |
// session_id |
// session_id |
126 |
memcpy(p, session_id, 16); |
memcpy(p, session_id, 16); |
127 |
p += 16; |
p += 16; |
128 |
// terminator? |
// terminator? |
129 |
PUT_32BIT(p, 1); |
PUT_32BIT(p, 1); |
130 |
|
|
131 |
retval = agent_query(request, reqlen, &vresponse, &resplen, NULL, NULL); |
retval = agent_query(request, reqlen, &vresponse, &resplen, NULL, NULL); |
132 |
assert(retval == 1); |
assert(retval == 1); |
133 |
response = vresponse; |
response = vresponse; |
134 |
if (resplen < 5 || response[4] != SSH1_AGENT_RSA_RESPONSE) |
if (resplen < 5 || response[4] != SSH1_AGENT_RSA_RESPONSE) |
135 |
return NULL; |
return NULL; |
136 |
|
|
137 |
ret = snewn(resplen-5, unsigned char); |
ret = snewn(resplen-5, unsigned char); |
138 |
memcpy(ret, response+5, resplen-5); |
memcpy(ret, response+5, resplen-5); |
139 |
sfree(response); |
sfree(response); |
140 |
|
|
141 |
if (outlen) |
if (outlen) |
142 |
*outlen = resplen-5; |
*outlen = resplen-5; |
143 |
|
|
144 |
return ret; |
return ret; |
145 |
} |
} |
146 |
|
|
147 |
int putty_get_ssh1_keylen(unsigned char *key, |
int putty_get_ssh1_keylen(unsigned char *key, |
148 |
int maxlen) |
int maxlen) |
149 |
{ |
{ |
150 |
return rsa_public_blob_len(key, maxlen); |
return rsa_public_blob_len(key, maxlen); |
151 |
} |
} |
152 |
|
|
153 |
|
|
154 |
/* |
/* |
155 |
* Following functions are copied from putty source. |
* Following functions are copied from putty source. |
156 |
*/ |
*/ |
157 |
|
|
158 |
|
|
159 |
// SSHRSA.C |
// SSHRSA.C |
160 |
/* Given a public blob, determine its length. */ |
/* Given a public blob, determine its length. */ |
161 |
int rsa_public_blob_len(void *data, int maxlen) |
int rsa_public_blob_len(void *data, int maxlen) |
162 |
{ |
{ |
163 |
unsigned char *p = (unsigned char *)data; |
unsigned char *p = (unsigned char *)data; |
164 |
int n; |
int n; |
165 |
|
|
166 |
if (maxlen < 4) |
if (maxlen < 4) |
167 |
return -1; |
return -1; |
168 |
p += 4; /* length word */ |
p += 4; /* length word */ |
169 |
maxlen -= 4; |
maxlen -= 4; |
170 |
|
|
171 |
n = ssh1_read_bignum(p, maxlen, NULL); /* exponent */ |
n = ssh1_read_bignum(p, maxlen, NULL); /* exponent */ |
172 |
if (n < 0) |
if (n < 0) |
173 |
return -1; |
return -1; |
174 |
p += n; |
p += n; |
175 |
|
|
176 |
n = ssh1_read_bignum(p, maxlen, NULL); /* modulus */ |
n = ssh1_read_bignum(p, maxlen, NULL); /* modulus */ |
177 |
if (n < 0) |
if (n < 0) |
178 |
return -1; |
return -1; |
179 |
p += n; |
p += n; |
180 |
|
|
181 |
return p - (unsigned char *)data; |
return p - (unsigned char *)data; |
182 |
} |
} |
183 |
|
|
184 |
// WINDOWS\WINPGNT.C |
// WINDOWS\WINPGNT.C |
185 |
/* |
/* |
186 |
* Acquire a keylist1 from the primary Pageant; this means either |
* Acquire a keylist1 from the primary Pageant; this means either |
187 |
* calling make_keylist1 (if that's us) or sending a message to the |
* calling make_keylist1 (if that's us) or sending a message to the |
188 |
* primary Pageant (if it's not). |
* primary Pageant (if it's not). |
189 |
*/ |
*/ |
190 |
static void *get_keylist1(int *length) |
static void *get_keylist1(int *length) |
191 |
{ |
{ |
192 |
void *ret; |
void *ret; |
193 |
|
|
194 |
unsigned char request[5], *response; |
unsigned char request[5], *response; |
195 |
void *vresponse; |
void *vresponse; |
196 |
int resplen, retval; |
int resplen, retval; |
197 |
request[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES; |
request[4] = SSH1_AGENTC_REQUEST_RSA_IDENTITIES; |
198 |
PUT_32BIT(request, 4); |
PUT_32BIT(request, 4); |
199 |
|
|
200 |
retval = agent_query(request, 5, &vresponse, &resplen, NULL, NULL); |
retval = agent_query(request, 5, &vresponse, &resplen, NULL, NULL); |
201 |
assert(retval == 1); |
assert(retval == 1); |
202 |
response = vresponse; |
response = vresponse; |
203 |
if (resplen < 5 || response[4] != SSH1_AGENT_RSA_IDENTITIES_ANSWER) |
if (resplen < 5 || response[4] != SSH1_AGENT_RSA_IDENTITIES_ANSWER) |
204 |
return NULL; |
return NULL; |
205 |
|
|
206 |
ret = snewn(resplen-5, unsigned char); |
ret = snewn(resplen-5, unsigned char); |
207 |
memcpy(ret, response+5, resplen-5); |
memcpy(ret, response+5, resplen-5); |
208 |
sfree(response); |
sfree(response); |
209 |
|
|
210 |
if (length) |
if (length) |
211 |
*length = resplen-5; |
*length = resplen-5; |
212 |
|
|
213 |
return ret; |
return ret; |
214 |
} |
} |
215 |
|
|
216 |
/* |
/* |
217 |
* Acquire a keylist2 from the primary Pageant; this means either |
* Acquire a keylist2 from the primary Pageant; this means either |
218 |
* calling make_keylist2 (if that's us) or sending a message to the |
* calling make_keylist2 (if that's us) or sending a message to the |
219 |
* primary Pageant (if it's not). |
* primary Pageant (if it's not). |
220 |
*/ |
*/ |
221 |
static void *get_keylist2(int *length) |
static void *get_keylist2(int *length) |
222 |
{ |
{ |
223 |
void *ret; |
void *ret; |
224 |
|
|
225 |
unsigned char request[5], *response; |
unsigned char request[5], *response; |
226 |
void *vresponse; |
void *vresponse; |
227 |
int resplen, retval; |
int resplen, retval; |
228 |
|
|
229 |
request[4] = SSH2_AGENTC_REQUEST_IDENTITIES; |
request[4] = SSH2_AGENTC_REQUEST_IDENTITIES; |
230 |
PUT_32BIT(request, 4); |
PUT_32BIT(request, 4); |
231 |
|
|
232 |
retval = agent_query(request, 5, &vresponse, &resplen, NULL, NULL); |
retval = agent_query(request, 5, &vresponse, &resplen, NULL, NULL); |
233 |
assert(retval == 1); |
assert(retval == 1); |
234 |
response = vresponse; |
response = vresponse; |
235 |
if (resplen < 5 || response[4] != SSH2_AGENT_IDENTITIES_ANSWER) |
if (resplen < 5 || response[4] != SSH2_AGENT_IDENTITIES_ANSWER) |
236 |
return NULL; |
return NULL; |
237 |
|
|
238 |
ret = snewn(resplen-5, unsigned char); |
ret = snewn(resplen-5, unsigned char); |
239 |
memcpy(ret, response+5, resplen-5); |
memcpy(ret, response+5, resplen-5); |
240 |
sfree(response); |
sfree(response); |
241 |
|
|
242 |
if (length) |
if (length) |
243 |
*length = resplen-5; |
*length = resplen-5; |
244 |
|
|
245 |
return ret; |
return ret; |
246 |
} |
} |
247 |
|
|
248 |
// WINDOWS\WINDOW.C |
// WINDOWS\WINDOW.C |
249 |
/* |
/* |
250 |
* Print a modal (Really Bad) message box and perform a fatal exit. |
* Print a modal (Really Bad) message box and perform a fatal exit. |
251 |
*/ |
*/ |
252 |
void modalfatalbox(char *fmt, ...) |
void modalfatalbox(char *fmt, ...) |
253 |
{ |
{ |
254 |
va_list ap; |
va_list ap; |
255 |
char *stuff, morestuff[100]; |
char *stuff, morestuff[100]; |
256 |
|
|
257 |
va_start(ap, fmt); |
va_start(ap, fmt); |
258 |
stuff = dupvprintf(fmt, ap); |
stuff = dupvprintf(fmt, ap); |
259 |
va_end(ap); |
va_end(ap); |
260 |
sprintf(morestuff, "%.70s Fatal Error", "TTSSH"); |
sprintf(morestuff, "%.70s Fatal Error", "TTSSH"); |
261 |
MessageBox(NULL, stuff, morestuff, |
MessageBox(NULL, stuff, morestuff, |
262 |
MB_SYSTEMMODAL | MB_ICONERROR | MB_OK); |
MB_SYSTEMMODAL | MB_ICONERROR | MB_OK); |
263 |
sfree(stuff); |
sfree(stuff); |
264 |
} |
} |