1
- /* $OpenBSD: hostfile.c,v 1.58 2014/10/20 03:43:01 djm Exp $ */
1
+ /* $OpenBSD: hostfile.c,v 1.59 2015/01/15 09:40:00 djm Exp $ */
2
2
/*
3
3
* Author: Tatu Ylonen <[email protected] >
4
4
* Copyright (c) 1995 Tatu Ylonen <[email protected] >, Espoo, Finland
51
51
52
52
#include "xmalloc.h"
53
53
#include "match.h"
54
- #include "key .h"
54
+ #include "sshkey .h"
55
55
#include "hostfile.h"
56
56
#include "log.h"
57
57
#include "misc.h"
58
+ #include "ssherr.h"
58
59
#include "digest.h"
59
60
#include "hmac.h"
60
61
@@ -155,15 +156,16 @@ host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
155
156
*/
156
157
157
158
int
158
- hostfile_read_key (char * * cpp , int * bitsp , Key * ret )
159
+ hostfile_read_key (char * * cpp , u_int * bitsp , struct sshkey * ret )
159
160
{
160
161
char * cp ;
162
+ int r ;
161
163
162
164
/* Skip leading whitespace. */
163
165
for (cp = * cpp ; * cp == ' ' || * cp == '\t' ; cp ++ )
164
166
;
165
167
166
- if (key_read ( ret , & cp ) != 1 )
168
+ if (( r = sshkey_read ( ret , & cp )) != 0 )
167
169
return 0 ;
168
170
169
171
/* Skip trailing whitespace. */
@@ -172,15 +174,13 @@ hostfile_read_key(char **cpp, int *bitsp, Key *ret)
172
174
173
175
/* Return results. */
174
176
* cpp = cp ;
175
- if (bitsp != NULL ) {
176
- if ((* bitsp = key_size (ret )) <= 0 )
177
- return 0 ;
178
- }
177
+ if (bitsp != NULL )
178
+ * bitsp = sshkey_size (ret );
179
179
return 1 ;
180
180
}
181
181
182
182
static int
183
- hostfile_check_key (int bits , const Key * key , const char * host ,
183
+ hostfile_check_key (int bits , const struct sshkey * key , const char * host ,
184
184
const char * filename , u_long linenum )
185
185
{
186
186
#ifdef WITH_SSH1
@@ -249,8 +249,8 @@ load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
249
249
u_long linenum = 0 , num_loaded = 0 ;
250
250
char * cp , * cp2 , * hashed_host ;
251
251
HostkeyMarker marker ;
252
- Key * key ;
253
- int kbits ;
252
+ struct sshkey * key ;
253
+ u_int kbits ;
254
254
255
255
if ((f = fopen (path , "r" )) == NULL )
256
256
return ;
@@ -296,13 +296,19 @@ load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
296
296
* Extract the key from the line. This will skip any leading
297
297
* whitespace. Ignore badly formatted lines.
298
298
*/
299
- key = key_new (KEY_UNSPEC );
299
+ if ((key = sshkey_new (KEY_UNSPEC )) == NULL ) {
300
+ error ("%s: sshkey_new failed" , __func__ );
301
+ break ;
302
+ }
300
303
if (!hostfile_read_key (& cp , & kbits , key )) {
301
- key_free (key );
304
+ sshkey_free (key );
302
305
#ifdef WITH_SSH1
303
- key = key_new (KEY_RSA1 );
306
+ if ((key = sshkey_new (KEY_RSA1 )) == NULL ) {
307
+ error ("%s: sshkey_new failed" , __func__ );
308
+ break ;
309
+ }
304
310
if (!hostfile_read_key (& cp , & kbits , key )) {
305
- key_free (key );
311
+ sshkey_free (key );
306
312
continue ;
307
313
}
308
314
#else
@@ -315,7 +321,7 @@ load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
315
321
debug3 ("%s: found %skey type %s in file %s:%lu" , __func__ ,
316
322
marker == MRK_NONE ? "" :
317
323
(marker == MRK_CA ? "ca " : "revoked " ),
318
- key_type (key ), path , linenum );
324
+ sshkey_type (key ), path , linenum );
319
325
hostkeys -> entries = xrealloc (hostkeys -> entries ,
320
326
hostkeys -> num_entries + 1 , sizeof (* hostkeys -> entries ));
321
327
hostkeys -> entries [hostkeys -> num_entries ].host = xstrdup (host );
@@ -339,7 +345,7 @@ free_hostkeys(struct hostkeys *hostkeys)
339
345
for (i = 0 ; i < hostkeys -> num_entries ; i ++ ) {
340
346
free (hostkeys -> entries [i ].host );
341
347
free (hostkeys -> entries [i ].file );
342
- key_free (hostkeys -> entries [i ].key );
348
+ sshkey_free (hostkeys -> entries [i ].key );
343
349
explicit_bzero (hostkeys -> entries + i , sizeof (* hostkeys -> entries ));
344
350
}
345
351
free (hostkeys -> entries );
@@ -348,18 +354,18 @@ free_hostkeys(struct hostkeys *hostkeys)
348
354
}
349
355
350
356
static int
351
- check_key_not_revoked (struct hostkeys * hostkeys , Key * k )
357
+ check_key_not_revoked (struct hostkeys * hostkeys , struct sshkey * k )
352
358
{
353
- int is_cert = key_is_cert (k );
359
+ int is_cert = sshkey_is_cert (k );
354
360
u_int i ;
355
361
356
362
for (i = 0 ; i < hostkeys -> num_entries ; i ++ ) {
357
363
if (hostkeys -> entries [i ].marker != MRK_REVOKE )
358
364
continue ;
359
- if (key_equal_public (k , hostkeys -> entries [i ].key ))
365
+ if (sshkey_equal_public (k , hostkeys -> entries [i ].key ))
360
366
return -1 ;
361
367
if (is_cert &&
362
- key_equal_public (k -> cert -> signature_key ,
368
+ sshkey_equal_public (k -> cert -> signature_key ,
363
369
hostkeys -> entries [i ].key ))
364
370
return -1 ;
365
371
}
@@ -383,11 +389,11 @@ check_key_not_revoked(struct hostkeys *hostkeys, Key *k)
383
389
*/
384
390
static HostStatus
385
391
check_hostkeys_by_key_or_type (struct hostkeys * hostkeys ,
386
- Key * k , int keytype , const struct hostkey_entry * * found )
392
+ struct sshkey * k , int keytype , const struct hostkey_entry * * found )
387
393
{
388
394
u_int i ;
389
395
HostStatus end_return = HOST_NEW ;
390
- int want_cert = key_is_cert (k );
396
+ int want_cert = sshkey_is_cert (k );
391
397
HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE ;
392
398
int proto = (k ? k -> type : keytype ) == KEY_RSA1 ? 1 : 2 ;
393
399
@@ -411,7 +417,7 @@ check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
411
417
break ;
412
418
}
413
419
if (want_cert ) {
414
- if (key_equal_public (k -> cert -> signature_key ,
420
+ if (sshkey_equal_public (k -> cert -> signature_key ,
415
421
hostkeys -> entries [i ].key )) {
416
422
/* A matching CA exists */
417
423
end_return = HOST_OK ;
@@ -420,7 +426,7 @@ check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
420
426
break ;
421
427
}
422
428
} else {
423
- if (key_equal (k , hostkeys -> entries [i ].key )) {
429
+ if (sshkey_equal (k , hostkeys -> entries [i ].key )) {
424
430
end_return = HOST_OK ;
425
431
if (found != NULL )
426
432
* found = hostkeys -> entries + i ;
@@ -441,7 +447,7 @@ check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
441
447
}
442
448
443
449
HostStatus
444
- check_key_in_hostkeys (struct hostkeys * hostkeys , Key * key ,
450
+ check_key_in_hostkeys (struct hostkeys * hostkeys , struct sshkey * key ,
445
451
const struct hostkey_entry * * found )
446
452
{
447
453
if (key == NULL )
@@ -463,11 +469,11 @@ lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype,
463
469
*/
464
470
465
471
int
466
- add_host_to_hostfile (const char * filename , const char * host , const Key * key ,
467
- int store_hash )
472
+ add_host_to_hostfile (const char * filename , const char * host ,
473
+ const struct sshkey * key , int store_hash )
468
474
{
469
475
FILE * f ;
470
- int success = 0 ;
476
+ int r , success = 0 ;
471
477
char * hashed_host = NULL ;
472
478
473
479
if (key == NULL )
@@ -485,12 +491,12 @@ add_host_to_hostfile(const char *filename, const char *host, const Key *key,
485
491
}
486
492
fprintf (f , "%s " , store_hash ? hashed_host : host );
487
493
488
- if (key_write (key , f )) {
494
+ if ((r = sshkey_write (key , f )) != 0 ) {
495
+ error ("%s: saving key in %s failed: %s" ,
496
+ __func__ , filename , ssh_err (r ));
497
+ } else
489
498
success = 1 ;
490
- } else {
491
- error ("add_host_to_hostfile: saving key in %s failed" , filename );
492
- }
493
- fprintf (f , "\n" );
499
+ fputs ("\n" , f );
494
500
fclose (f );
495
501
return success ;
496
502
}
0 commit comments