1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
/* key-func.c -- SSH key manipulation functions.
*
* Copyright (C) 2013, 2014 Artyom V. Poptsov <poptsov.artyom@gmail.com>
*
* This file is part of Guile-SSH.
*
* Guile-SSH is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Guile-SSH is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Guile-SSH. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <libguile.h>
#include <libssh/libssh.h>
#include "key-type.h"
#include "common.h"
#include "error.h"
/* Convert SSH public key KEY to a scheme string. */
SCM_DEFINE (guile_ssh_public_key_to_string, "public-key->string", 1, 0, 0,
(SCM key),
"\
Convert SSH public key to a scheme string.\
")
#define FUNC_NAME s_guile_ssh_public_key_to_string
{
struct key_data *key_data = _scm_to_key_data (key);
char *key_str;
SCM_ASSERT (_public_key_p (key_data), key, SCM_ARG1, FUNC_NAME);
int res = ssh_pki_export_pubkey_base64 (key_data->ssh_key, &key_str);
if (res != SSH_OK)
guile_ssh_error1 (FUNC_NAME, "Unable to convert the key to a string", key);
return scm_take_locale_string (key_str);
}
#undef FUNC_NAME
SCM_DEFINE (guile_ssh_string_to_public_key, "string->public-key", 2, 0, 0,
(SCM base64_str, SCM type),
"\
Convert Base64 string to a public key. Return new public key.\n\
Throw `guile-ssh-error' on error.\
")
#define FUNC_NAME s_guile_ssh_string_to_public_key
{
char *c_base64_str = NULL;
const struct symbol_mapping *key_type = NULL;
ssh_key ssh_public_key = NULL;
int res;
SCM_ASSERT (scm_is_string (base64_str), base64_str, SCM_ARG1, FUNC_NAME);
SCM_ASSERT (scm_is_symbol (type), type, SCM_ARG2, FUNC_NAME);
scm_dynwind_begin (0);
c_base64_str = scm_to_locale_string (base64_str);
scm_dynwind_free (c_base64_str);
key_type = _scm_to_ssh_key_type (type);
if (! key_type)
guile_ssh_error1 (FUNC_NAME, "Wrong key type", type);
res = ssh_pki_import_pubkey_base64 (c_base64_str,
key_type->value,
&ssh_public_key);
if (res != SSH_OK)
{
const char *msg = "Could not convert the given string to a public key";
guile_ssh_error1 (FUNC_NAME, msg, scm_list_2 (base64_str, type));
}
scm_dynwind_end ();
return _scm_from_ssh_key (ssh_public_key, SCM_BOOL_F);
}
#undef FUNC_NAME
SCM_DEFINE (guile_ssh_private_key_from_file, "private-key-from-file", 1, 0, 0,
(SCM filename),
"\
Read private key from a file FILENAME. If the the key isn encrypted the user\n\
will be asked for passphrase to decrypt the key.\n\
\n\
Return a new SSH key of #f on error.\
")
#define FUNC_NAME s_guile_ssh_private_key_from_file
{
ssh_key ssh_key = NULL;
char *c_filename;
/* NULL means that either the public key is unecrypted or the user
should be asked for the passphrase. */
char *passphrase = NULL;
int res;
scm_dynwind_begin (0);
SCM_ASSERT (scm_is_string (filename), filename, SCM_ARG1, FUNC_NAME);
c_filename = scm_to_locale_string (filename);
scm_dynwind_free (c_filename);
res = ssh_pki_import_privkey_file (c_filename,
passphrase,
NULL, /* auth_fn */
NULL, /* auth_data */
&ssh_key);
if (res == SSH_EOF)
{
const char *msg = "The file does not exist or permission denied";
guile_ssh_error1 (FUNC_NAME, msg, filename);
}
else if (res == SSH_ERROR)
{
const char *msg = "Unable to import a key from the file";
guile_ssh_error1 (FUNC_NAME, msg, filename);
}
scm_dynwind_end ();
return _scm_from_ssh_key (ssh_key, SCM_BOOL_F);
}
#undef FUNC_NAME
SCM_DEFINE (guile_ssh_private_key_to_file,
"private-key-to-file", 2, 0, 0,
(SCM key, SCM file_name),
"\
Export a private KEY to file FILE_NAME. Throw `guile-ssh-error' on error. \
Return value is undefined.\
")
#define FUNC_NAME s_guile_ssh_private_key_to_file
{
struct key_data *kd = _scm_to_key_data (key);
char *c_file_name = NULL;
int res;
scm_dynwind_begin (0);
SCM_ASSERT (_private_key_p (kd), key, SCM_ARG1, FUNC_NAME);
SCM_ASSERT (scm_is_string (file_name), file_name, SCM_ARG2, FUNC_NAME);
c_file_name = scm_to_locale_string (file_name);
scm_dynwind_free (c_file_name);
res = ssh_pki_export_privkey_file (kd->ssh_key,
NULL, /* passphrase */
NULL, /* auth_fn */
NULL, /* auth_data */
c_file_name);
if (res == SSH_ERROR)
{
guile_ssh_error1 (FUNC_NAME, "Unable to export a key to a file",
scm_list_2 (key, file_name));
}
scm_dynwind_end ();
return SCM_UNDEFINED;
}
#undef FUNC_NAME
SCM_DEFINE (guile_ssh_public_key_from_private_key, "private-key->public-key",
1, 0, 0,
(SCM key),
"\
Get public key from a private key KEY.\
")
#define FUNC_NAME s_guile_ssh_public_key_from_private_key
{
struct key_data *private_key_data = _scm_to_key_data (key);
ssh_key ssh_public_key = NULL;
int res;
SCM_ASSERT (_private_key_p (private_key_data), key, SCM_ARG1, FUNC_NAME);
res = ssh_pki_export_privkey_to_pubkey (private_key_data->ssh_key,
&ssh_public_key);
if (res != SSH_OK)
return SCM_BOOL_F;
return _scm_from_ssh_key (ssh_public_key, SCM_BOOL_F);
}
#undef FUNC_NAME
/* Read public key from a file FILENAME.
*
* Return a SSH key smob.
*/
SCM_DEFINE (guile_ssh_public_key_from_file, "public-key-from-file", 1, 0, 0,
(SCM filename),
"\
Read public key from a file FILENAME. Return a SSH key.\
")
#define FUNC_NAME s_guile_ssh_public_key_from_file
{
ssh_key ssh_public_key = NULL;
char *c_filename;
int res;
scm_dynwind_begin (0);
SCM_ASSERT (scm_is_string (filename), filename, SCM_ARG1, FUNC_NAME);
c_filename = scm_to_locale_string (filename);
scm_dynwind_free (c_filename);
res = ssh_pki_import_pubkey_file (c_filename, &ssh_public_key);
if (res == SSH_EOF)
{
const char *msg = "The file does not exist or permission denied";
guile_ssh_error1 (FUNC_NAME, msg, filename);
}
else if (res == SSH_ERROR)
{
const char *msg = "Unable to import a key from the file";
guile_ssh_error1 (FUNC_NAME, msg, filename);
}
scm_dynwind_end ();
return _scm_from_ssh_key (ssh_public_key, SCM_BOOL_F);
}
#undef FUNC_NAME
static struct symbol_mapping hash_types[] = {
{ "sha1", SSH_PUBLICKEY_HASH_SHA1 },
{ "md5", SSH_PUBLICKEY_HASH_MD5 },
{ NULL, -1 }
};
SCM_DEFINE (guile_ssh_get_public_key_hash, "get-public-key-hash", 2, 0, 0,
(SCM key, SCM type),
"\
Get hash of the public KEY as a bytevector.\n\
Possible types are: 'sha1, 'md5\n\
Return a bytevector on success, #f on error.\
")
#define FUNC_NAME s_guile_ssh_get_public_key_hash
{
struct key_data *kd = _scm_to_key_data (key);
unsigned char *hash = NULL;
size_t hash_len;
int res;
SCM ret;
const struct symbol_mapping *hash_type = NULL;
SCM_ASSERT (scm_is_symbol (type), type, SCM_ARG2, FUNC_NAME);
scm_dynwind_begin (0);
hash_type = _scm_to_ssh_const (hash_types, type);
if (! hash_type)
guile_ssh_error1 (FUNC_NAME, "Wrong type", type);
res = ssh_get_publickey_hash (kd->ssh_key, hash_type->value,
&hash, &hash_len);
scm_dynwind_free (hash);
if (res == SSH_OK)
{
size_t idx;
ret = scm_c_make_bytevector (hash_len);
for (idx = 0; idx < hash_len; ++idx)
scm_c_bytevector_set_x (ret, idx, hash[idx]);
}
else
{
ret = SCM_BOOL_F;
}
scm_dynwind_end ();
return ret;
}
#undef FUNC_NAME
/* Initialize Scheme procedures. */
void
init_key_func (void)
{
#include "key-func.x"
}
/* key-func.c ends here */
|