Develop and Download Open Source Software

Browse Subversion Repository

Contents of /trunk/ttssh2/ttxssh/hostkey.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10064 - (show annotations) (download) (as text)
Sun Jul 17 15:39:09 2022 UTC (20 months, 3 weeks ago) by doda
File MIME type: text/x-csrc
File size: 6571 byte(s)
公開鍵形式と公開鍵署名アルゴリズムを分離した

rsa-sha2-256/512(署名形式)では公開鍵形式としてssh-rsaを使うため。
pvar->hostkey_type は署名アルゴリズムが格納されるようになる。
1 /*
2 * (C) 2021- TeraTerm Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "ttxssh.h"
30 #include "hostkey.h"
31 #include "kex.h"
32
33
34 struct ssh2_host_key_t {
35 ssh_keyalgo algo;
36 ssh_keytype type;
37 int digest_type;
38 char *name;
39 };
40
41 static const struct ssh2_host_key_t ssh2_host_key[] = {
42 {KEY_ALGO_RSA1, KEY_RSA1, NID_sha1, "ssh-rsa1"}, // for SSH1 only
43 {KEY_ALGO_RSA, KEY_RSA, NID_sha1, "ssh-rsa"}, // RFC4253
44 {KEY_ALGO_DSA, KEY_DSA, NID_sha1, "ssh-dss"}, // RFC4253
45 {KEY_ALGO_ECDSA256, KEY_ECDSA256, NID_sha256, "ecdsa-sha2-nistp256"}, // RFC5656
46 {KEY_ALGO_ECDSA384, KEY_ECDSA384, NID_sha384, "ecdsa-sha2-nistp384"}, // RFC5656
47 {KEY_ALGO_ECDSA521, KEY_ECDSA521, NID_sha512, "ecdsa-sha2-nistp521"}, // RFC5656
48 {KEY_ALGO_ED25519, KEY_ED25519, NID_sha512, "ssh-ed25519"}, // RDC8709
49 {KEY_ALGO_UNSPEC, KEY_UNSPEC, NID_undef, "ssh-unknown"},
50 {KEY_ALGO_NONE, KEY_NONE, NID_undef, NULL},
51 };
52
53 struct ssh_digest_t {
54 digest_algorithm id;
55 char *name;
56 };
57
58 /* NB. Indexed directly by algorithm number */
59 static const struct ssh_digest_t ssh_digests[] = {
60 { SSH_DIGEST_MD5, "MD5" },
61 { SSH_DIGEST_RIPEMD160, "RIPEMD160" },
62 { SSH_DIGEST_SHA1, "SHA1" },
63 { SSH_DIGEST_SHA256, "SHA256" },
64 { SSH_DIGEST_SHA384, "SHA384" },
65 { SSH_DIGEST_SHA512, "SHA512" },
66 { SSH_DIGEST_MAX, NULL },
67 };
68
69
70 ssh_keytype get_hostkey_type_from_name(char *name)
71 {
72 if (strcmp(name, "rsa1") == 0) {
73 return KEY_RSA1;
74 } else if (strcmp(name, "rsa") == 0) {
75 return KEY_RSA;
76 } else if (strcmp(name, "dsa") == 0) {
77 return KEY_DSA;
78 } else if (strcmp(name, "ssh-rsa") == 0) {
79 return KEY_RSA;
80 } else if (strcmp(name, "ssh-dss") == 0) {
81 return KEY_DSA;
82 } else if (strcmp(name, "ecdsa-sha2-nistp256") == 0) {
83 return KEY_ECDSA256;
84 } else if (strcmp(name, "ecdsa-sha2-nistp384") == 0) {
85 return KEY_ECDSA384;
86 } else if (strcmp(name, "ecdsa-sha2-nistp521") == 0) {
87 return KEY_ECDSA521;
88 } else if (strcmp(name, "ssh-ed25519") == 0) {
89 return KEY_ED25519;
90 }
91 return KEY_UNSPEC;
92 }
93
94 char* get_ssh2_hostkey_type_name(ssh_keytype type)
95 {
96 const struct ssh2_host_key_t *ptr = ssh2_host_key;
97
98 while (ptr->name != NULL) {
99 if (type == ptr->type) {
100 return ptr->name;
101 }
102 ptr++;
103 }
104
105 // not found.
106 return "ssh-unknown";
107 }
108
109 char *get_ssh2_hostkey_type_name_from_key(Key *key)
110 {
111 return get_ssh2_hostkey_type_name(key->type);
112 }
113
114 char* get_ssh2_keyalgo_name(ssh_keyalgo algo)
115 {
116 const struct ssh2_host_key_t *ptr = ssh2_host_key;
117
118 while (ptr->name != NULL) {
119 if (algo == ptr->algo) {
120 return ptr->name;
121 }
122 ptr++;
123 }
124
125 // not found.
126 return "ssh-unknown";
127 }
128
129 ssh_keyalgo get_ssh2_keyalgo_from_name(const char *name)
130 {
131 const struct ssh2_host_key_t *ptr = ssh2_host_key;
132
133 while (ptr->name != NULL) {
134 if (strcmp(name, ptr->name) == 0) {
135 return ptr->algo;
136 }
137 ptr++;
138 }
139
140 // not found.
141 return KEY_ALGO_UNSPEC;
142 }
143
144 int get_ssh2_keyalgo_hashtype(ssh_keyalgo algo)
145 {
146 const struct ssh2_host_key_t *ptr = ssh2_host_key;
147
148 while (ptr->name != NULL) {
149 if (algo == ptr->algo) {
150 return ptr->digest_type;
151 }
152 ptr++;
153 }
154
155 // not found.
156 return NID_sha1;
157 }
158
159 ssh_keytype get_ssh2_keytype_from_keyalgo(ssh_keyalgo algo)
160 {
161 const struct ssh2_host_key_t *ptr = ssh2_host_key;
162
163 while (ptr->name != NULL) {
164 if (algo == ptr->algo) {
165 return ptr->type;
166 }
167 ptr++;
168 }
169
170 // not found.
171 return KEY_UNSPEC;
172 }
173
174 const char* get_ssh2_keytype_name_from_keyalgo(ssh_keyalgo algo)
175 {
176 return get_ssh2_hostkey_type_name(get_ssh2_keytype_from_keyalgo(algo));
177 }
178
179 char* get_digest_algorithm_name(digest_algorithm id)
180 {
181 const struct ssh_digest_t *ptr = ssh_digests;
182
183 while (ptr->name != NULL) {
184 if (id == ptr->id) {
185 return ptr->name;
186 }
187 ptr++;
188 }
189
190 // not found.
191 return "unknown";
192 }
193
194 void normalize_host_key_order(char *buf)
195 {
196 static char default_strings[] = {
197 KEY_ALGO_ECDSA256,
198 KEY_ALGO_ECDSA384,
199 KEY_ALGO_ECDSA521,
200 KEY_ALGO_ED25519,
201 KEY_ALGO_RSA,
202 KEY_ALGO_DSA,
203 KEY_ALGO_NONE,
204 };
205
206 normalize_generic_order(buf, default_strings, NUM_ELEM(default_strings));
207 }
208
209 ssh_keyalgo choose_SSH2_host_key_algorithm(char *server_proposal, char *my_proposal)
210 {
211 ssh_keytype type = KEY_UNSPEC;
212 char str_keytype[20];
213 const struct ssh2_host_key_t *ptr = ssh2_host_key;
214
215 choose_SSH2_proposal(server_proposal, my_proposal, str_keytype, sizeof(str_keytype));
216
217 return get_ssh2_keyalgo_from_name(str_keytype);
218 }
219
220 // Host Key�A���S���Y���D���������������Amyproposal[]�������������B
221 // (2011.2.28 yutaka)
222 void SSH2_update_host_key_myproposal(PTInstVar pvar)
223 {
224 static char buf[256]; // TODO: malloc()��������
225 int index;
226 int len, i;
227
228 // ���M�������������������������A�O�������B(2006.6.26 maya)
229 if (pvar->socket != INVALID_SOCKET) {
230 return;
231 }
232
233 buf[0] = '\0';
234 for (i = 0 ; pvar->settings.HostKeyOrder[i] != 0 ; i++) {
235 index = pvar->settings.HostKeyOrder[i] - '0';
236 if (index == KEY_NONE) // disabled line
237 break;
238 strncat_s(buf, sizeof(buf), get_ssh2_keyalgo_name(index), _TRUNCATE);
239 strncat_s(buf, sizeof(buf), ",", _TRUNCATE);
240 }
241 len = strlen(buf);
242 if (len > 0)
243 buf[len - 1] = '\0'; // get rid of comma
244 myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS] = buf;
245 }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26