5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2007 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
22 /************************* Static utility functions *************************/
24 /* Key fields destructor */
26 static void silc_ssh_field_dest(void *key, void *context, void *user_context)
32 /* Parse header line from key. Doesn't return the line termination
35 SilcBool silc_ssh_parse_line(SilcBuffer key, SilcBuffer line,
40 SilcBool valid = cont;
42 data_len = silc_buffer_len(key);
43 tmp = silc_buffer_data(key);
44 for (i = 0; i < data_len; i++) {
45 /* All header lines must have ':' character */
46 if (!cont && tmp[i] == ':')
49 if ((data_len - i >= 1 && tmp[i] == '\r') ||
50 (data_len - i >= 1 && tmp[i] == '\n')) {
56 silc_buffer_set(line, tmp, i);
58 if (data_len - i >= 2 && tmp[i] == '\r' && tmp[i + 1] == '\n')
59 silc_buffer_pull(key, i + 2);
61 silc_buffer_pull(key, i + 1);
70 /* Allocate fields hash table */
72 SilcHashTable silc_ssh_allocate_fields(void)
74 return silc_hash_table_alloc(NULL, 0, silc_hash_string, NULL,
75 silc_hash_string_compare, NULL,
76 silc_ssh_field_dest, NULL, TRUE);
79 /* Parse key headers and return them into a hash table */
81 SilcHashTable silc_ssh_parse_headers(SilcBuffer key)
84 unsigned char *field, *value;
85 SilcBufferStruct line, v;
86 SilcBool quoted = FALSE;
88 SILC_LOG_DEBUG(("Parsing SSH key headers"));
90 fields = silc_ssh_allocate_fields();
94 /* Parse the fields */
95 while (silc_buffer_len(key) > 0) {
96 if (!silc_ssh_parse_line(key, &line, FALSE))
101 field = strchr(silc_buffer_data(&line), ':');
104 if (field - silc_buffer_data(&line) > 64)
106 field = silc_memdup(silc_buffer_data(&line),
107 field - silc_buffer_data(&line));
111 /* Skip ':' and following whitespace */
112 if (!silc_buffer_pull(&line, strlen(field) + 2))
117 memset(&v, 0, sizeof(v));
118 silc_buffer_format(&v,
119 SILC_STR_DATA(silc_buffer_data(&line),
120 silc_buffer_len(&line)),
123 /* Handle quoted Comment lines by removing the quotation */
124 if (*silc_buffer_data(&v) == '"' && !strcmp(field, "Comment"))
127 /* Handle wrapping value lines */
128 while (silc_buffer_len(&v) > 0) {
129 if (*silc_buffer_data(&v) == '\\') {
130 if (!silc_ssh_parse_line(key, &line, TRUE))
132 silc_buffer_format(&v,
133 SILC_STR_DATA(silc_buffer_data(&line),
134 silc_buffer_len(&line)),
138 silc_buffer_pull(&v, 1);
140 silc_buffer_start(&v);
142 if (silc_buffer_len(&v) > 1024)
146 /* If the last character is quotation also, remove the quotation */
147 if (*(silc_buffer_data(&v) + silc_buffer_len(&v) - 1) == '"') {
148 silc_buffer_pull(&v, 1);
149 silc_buffer_push_tail(&v, 1);
153 value = silc_memdup(silc_buffer_data(&v), silc_buffer_len(&v));
156 silc_buffer_purge(&v);
158 /* Add to hash table */
159 SILC_LOG_DEBUG(("Header '%s' '%s'", field, value));
160 silc_hash_table_add(fields, field, value);
166 SILC_LOG_ERROR(("Malformed SSH2 key headers"));
167 silc_hash_table_free(fields);
171 /******************************* SILC SSH API *******************************/
173 /* Generate key pair */
175 SilcBool silc_ssh_generate_key(const char *algorithm,
176 int bits_len, SilcRng rng,
177 SilcPublicKey *ret_public_key,
178 SilcPrivateKey *ret_private_key)
180 SilcSshPublicKey pubkey;
181 SilcSshPrivateKey privkey;
182 const SilcPKCSAlgorithm *alg;
183 const SilcPKCSObject *pkcs;
185 SILC_LOG_DEBUG(("Generating SSH2 %s key pair with key length %d bits",
186 algorithm, bits_len));
191 pkcs = silc_pkcs_find_pkcs(SILC_PKCS_SSH2);
195 /* Allocate SSH public key */
196 pubkey = silc_calloc(1, sizeof(*pubkey));
200 /* Allocate algorithm */
201 alg = silc_pkcs_find_algorithm(algorithm, "ssh");
203 SILC_LOG_ERROR(("Public key algorithm %s/ssh not supported", algorithm));
208 pubkey->type = SILC_SSH_KEY_OPENSSH;
210 /* Allocate SSH private key */
211 privkey = silc_calloc(1, sizeof(*privkey));
217 privkey->type = SILC_SSH_KEY_OPENSSH;
219 /* Allocate public key */
220 *ret_public_key = silc_calloc(1, sizeof(**ret_public_key));
221 if (!(*ret_public_key)) {
226 (*ret_public_key)->pkcs = (SilcPKCSObject *)pkcs;
227 (*ret_public_key)->alg = alg;
228 (*ret_public_key)->public_key = pubkey;
230 /* Allocate private key */
231 *ret_private_key = silc_calloc(1, sizeof(**ret_private_key));
232 if (!(*ret_private_key)) {
235 silc_free(*ret_public_key);
238 (*ret_private_key)->pkcs = (SilcPKCSObject *)pkcs;
239 (*ret_private_key)->alg = alg;
240 (*ret_private_key)->private_key = privkey;
242 /* Generate the algorithm key pair */
243 if (!alg->generate_key(alg, bits_len, rng, &pubkey->public_key,
244 &privkey->private_key)) {
247 silc_free(*ret_public_key);
248 silc_free(*ret_private_key);
255 /* Decode SSH public key. */
257 int silc_ssh_public_key_decode(unsigned char *key, SilcUInt32 key_len,
258 SilcSshPublicKey *ret_public_key)
260 SilcSshPublicKey public_key;
261 const SilcPKCSAlgorithm *alg;
262 SilcBufferStruct keybuf;
265 SILC_LOG_DEBUG(("Parse SSH2 public key"));
270 public_key = silc_calloc(1, sizeof(*public_key));
274 silc_buffer_set(&keybuf, key, key_len);
276 SILC_LOG_HEXDUMP(("SSH public key, len %d", key_len), key, key_len);
278 /* Parse public key type */
279 if (silc_buffer_unformat(&keybuf,
281 SILC_STR_UI32_STRING_ALLOC(&type),
283 SILC_LOG_ERROR(("Malformed SSH2 public key"));
287 SILC_LOG_DEBUG(("SSH2 public key type %s", type));
289 if (!strcmp(type, "ssh-rsa")) {
291 alg = silc_pkcs_find_algorithm("rsa", "ssh");
293 SILC_LOG_ERROR(("Unsupported SSH2 public key type '%s'", type));
296 public_key->pkcs = alg;
298 } else if (!strcmp(type, "ssh-dss")) {
300 alg = silc_pkcs_find_algorithm("dsa", "ssh");
302 SILC_LOG_ERROR(("Unsupported SSH2 public key type '%s'", type));
305 public_key->pkcs = alg;
308 SILC_LOG_ERROR(("Unsupported SSH2 public key type '%s'", type));
312 /* Parse the algorithm specific public key */
313 if (!alg->import_public_key(alg, silc_buffer_data(&keybuf),
314 silc_buffer_len(&keybuf),
315 &public_key->public_key))
320 *ret_public_key = public_key;
326 silc_free(public_key);
330 /* Encode SSH public key */
332 unsigned char *silc_ssh_public_key_encode(SilcStack stack,
333 SilcSshPublicKey public_key,
334 SilcUInt32 *ret_key_len)
336 const SilcPKCSAlgorithm *alg = public_key->pkcs;
337 SilcBufferStruct buf;
338 unsigned char *pk = NULL, tmp[16];
341 SILC_LOG_DEBUG(("Encode SSH2 public key"));
343 /* Get algorithm name */
344 if (!strcmp(alg->name, "rsa"))
345 silc_snprintf(tmp, sizeof(tmp), "ssh-rsa");
346 else if (!strcmp(alg->name, "dsa"))
347 silc_snprintf(tmp, sizeof(tmp), "ssh-dss");
351 /* Export PKCS algorithm public key */
352 if (alg->export_public_key)
353 pk = alg->export_public_key(alg, stack, public_key->public_key, &pk_len);
355 SILC_LOG_ERROR(("Error exporting PKCS algorithm key"));
359 /* Encode public key */
360 memset(&buf, 0, sizeof(buf));
361 if (silc_buffer_sformat(stack, &buf,
362 SILC_STR_UI_INT(strlen(tmp)),
363 SILC_STR_UI32_STRING(tmp),
364 SILC_STR_UI_XNSTRING(pk, pk_len),
366 silc_sfree(stack, pk);
370 silc_sfree(stack, pk);
371 pk = silc_buffer_steal(&buf, ret_key_len);
376 /* Free public key */
378 void silc_ssh_public_key_free(SilcSshPublicKey public_key)
380 if (public_key->fields)
381 silc_hash_table_free(public_key->fields);
382 silc_free(public_key);
385 /* Return public key header field value */
387 const char *silc_ssh_public_key_get_field(SilcSshPublicKey public_key,
392 if (!field || !public_key->fields)
395 if (!silc_hash_table_find(public_key->fields, (void *)field,
396 NULL, (void *)&value))
399 return (const char *)value;
402 /* Add public key header value */
404 SilcBool silc_ssh_public_key_add_field(SilcSshPublicKey public_key,
408 if (!field || !value)
411 if (!public_key->fields) {
413 silc_hash_table_alloc(NULL, 0, silc_hash_string, NULL,
414 silc_hash_string_compare, NULL,
415 silc_ssh_field_dest, NULL, TRUE);
416 if (!public_key->fields)
420 return silc_hash_table_add(public_key->fields, strdup(field), strdup(value));
423 /* Set public key type */
425 void silc_ssh_public_key_set_type(SilcSshPublicKey public_key,
428 public_key->type = type;