Skip to content

Commit 1129dcf

Browse files
committed
upstream commit
sync ssh-keysign, ssh-keygen and some dependencies to the new buffer/key API; mostly mechanical, ok markus@
1 parent e4ebf55 commit 1129dcf

13 files changed

+594
-494
lines changed

dns.c

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: dns.c,v 1.32 2014/12/21 22:27:56 djm Exp $ */
1+
/* $OpenBSD: dns.c,v 1.33 2015/01/15 09:40:00 djm Exp $ */
22

33
/*
44
* Copyright (c) 2003 Wesley Griffin. All rights reserved.
@@ -38,7 +38,8 @@
3838
#include <stdlib.h>
3939

4040
#include "xmalloc.h"
41-
#include "key.h"
41+
#include "sshkey.h"
42+
#include "ssherr.h"
4243
#include "dns.h"
4344
#include "log.h"
4445
#include "digest.h"
@@ -78,9 +79,9 @@ dns_result_totext(unsigned int res)
7879
*/
7980
static int
8081
dns_read_key(u_int8_t *algorithm, u_int8_t *digest_type,
81-
u_char **digest, u_int *digest_len, Key *key)
82+
u_char **digest, size_t *digest_len, struct sshkey *key)
8283
{
83-
int success = 0;
84+
int r, success = 0;
8485
int fp_alg = -1;
8586

8687
switch (key->type) {
@@ -121,9 +122,10 @@ dns_read_key(u_int8_t *algorithm, u_int8_t *digest_type,
121122
}
122123

123124
if (*algorithm && *digest_type) {
124-
*digest = key_fingerprint_raw(key, fp_alg, digest_len);
125-
if (*digest == NULL)
126-
fatal("dns_read_key: null from key_fingerprint_raw()");
125+
if ((r = sshkey_fingerprint_raw(key, fp_alg, digest,
126+
digest_len)) != 0)
127+
fatal("%s: sshkey_fingerprint_raw: %s", __func__,
128+
ssh_err(r));
127129
success = 1;
128130
} else {
129131
*digest = NULL;
@@ -139,7 +141,7 @@ dns_read_key(u_int8_t *algorithm, u_int8_t *digest_type,
139141
*/
140142
static int
141143
dns_read_rdata(u_int8_t *algorithm, u_int8_t *digest_type,
142-
u_char **digest, u_int *digest_len, u_char *rdata, int rdata_len)
144+
u_char **digest, size_t *digest_len, u_char *rdata, int rdata_len)
143145
{
144146
int success = 0;
145147

@@ -200,7 +202,7 @@ is_numeric_hostname(const char *hostname)
200202
*/
201203
int
202204
verify_host_key_dns(const char *hostname, struct sockaddr *address,
203-
Key *hostkey, int *flags)
205+
struct sshkey *hostkey, int *flags)
204206
{
205207
u_int counter;
206208
int result;
@@ -209,12 +211,12 @@ verify_host_key_dns(const char *hostname, struct sockaddr *address,
209211
u_int8_t hostkey_algorithm;
210212
u_int8_t hostkey_digest_type = SSHFP_HASH_RESERVED;
211213
u_char *hostkey_digest;
212-
u_int hostkey_digest_len;
214+
size_t hostkey_digest_len;
213215

214216
u_int8_t dnskey_algorithm;
215217
u_int8_t dnskey_digest_type;
216218
u_char *dnskey_digest;
217-
u_int dnskey_digest_len;
219+
size_t dnskey_digest_len;
218220

219221
*flags = 0;
220222

@@ -310,21 +312,21 @@ verify_host_key_dns(const char *hostname, struct sockaddr *address,
310312
* Export the fingerprint of a key as a DNS resource record
311313
*/
312314
int
313-
export_dns_rr(const char *hostname, Key *key, FILE *f, int generic)
315+
export_dns_rr(const char *hostname, struct sshkey *key, FILE *f, int generic)
314316
{
315317
u_int8_t rdata_pubkey_algorithm = 0;
316318
u_int8_t rdata_digest_type = SSHFP_HASH_RESERVED;
317319
u_int8_t dtype;
318320
u_char *rdata_digest;
319-
u_int i, rdata_digest_len;
321+
size_t i, rdata_digest_len;
320322
int success = 0;
321323

322324
for (dtype = SSHFP_HASH_SHA1; dtype < SSHFP_HASH_MAX; dtype++) {
323325
rdata_digest_type = dtype;
324326
if (dns_read_key(&rdata_pubkey_algorithm, &rdata_digest_type,
325327
&rdata_digest, &rdata_digest_len, key)) {
326328
if (generic) {
327-
fprintf(f, "%s IN TYPE%d \\# %d %02x %02x ",
329+
fprintf(f, "%s IN TYPE%d \\# %zu %02x %02x ",
328330
hostname, DNS_RDATATYPE_SSHFP,
329331
2 + rdata_digest_len,
330332
rdata_pubkey_algorithm, rdata_digest_type);

dns.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: dns.h,v 1.13 2014/04/20 09:24:26 logan Exp $ */
1+
/* $OpenBSD: dns.h,v 1.14 2015/01/15 09:40:00 djm Exp $ */
22

33
/*
44
* Copyright (c) 2003 Wesley Griffin. All rights reserved.
@@ -50,7 +50,8 @@ enum sshfp_hashes {
5050
#define DNS_VERIFY_MATCH 0x00000002
5151
#define DNS_VERIFY_SECURE 0x00000004
5252

53-
int verify_host_key_dns(const char *, struct sockaddr *, Key *, int *);
54-
int export_dns_rr(const char *, Key *, FILE *, int);
53+
int verify_host_key_dns(const char *, struct sockaddr *,
54+
struct sshkey *, int *);
55+
int export_dns_rr(const char *, struct sshkey *, FILE *, int);
5556

5657
#endif /* DNS_H */

hostfile.c

+40-34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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 $ */
22
/*
33
* Author: Tatu Ylonen <[email protected]>
44
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
@@ -51,10 +51,11 @@
5151

5252
#include "xmalloc.h"
5353
#include "match.h"
54-
#include "key.h"
54+
#include "sshkey.h"
5555
#include "hostfile.h"
5656
#include "log.h"
5757
#include "misc.h"
58+
#include "ssherr.h"
5859
#include "digest.h"
5960
#include "hmac.h"
6061

@@ -155,15 +156,16 @@ host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
155156
*/
156157

157158
int
158-
hostfile_read_key(char **cpp, int *bitsp, Key *ret)
159+
hostfile_read_key(char **cpp, u_int *bitsp, struct sshkey *ret)
159160
{
160161
char *cp;
162+
int r;
161163

162164
/* Skip leading whitespace. */
163165
for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
164166
;
165167

166-
if (key_read(ret, &cp) != 1)
168+
if ((r = sshkey_read(ret, &cp)) != 0)
167169
return 0;
168170

169171
/* Skip trailing whitespace. */
@@ -172,15 +174,13 @@ hostfile_read_key(char **cpp, int *bitsp, Key *ret)
172174

173175
/* Return results. */
174176
*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);
179179
return 1;
180180
}
181181

182182
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,
184184
const char *filename, u_long linenum)
185185
{
186186
#ifdef WITH_SSH1
@@ -249,8 +249,8 @@ load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
249249
u_long linenum = 0, num_loaded = 0;
250250
char *cp, *cp2, *hashed_host;
251251
HostkeyMarker marker;
252-
Key *key;
253-
int kbits;
252+
struct sshkey *key;
253+
u_int kbits;
254254

255255
if ((f = fopen(path, "r")) == NULL)
256256
return;
@@ -296,13 +296,19 @@ load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
296296
* Extract the key from the line. This will skip any leading
297297
* whitespace. Ignore badly formatted lines.
298298
*/
299-
key = key_new(KEY_UNSPEC);
299+
if ((key = sshkey_new(KEY_UNSPEC)) == NULL) {
300+
error("%s: sshkey_new failed", __func__);
301+
break;
302+
}
300303
if (!hostfile_read_key(&cp, &kbits, key)) {
301-
key_free(key);
304+
sshkey_free(key);
302305
#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+
}
304310
if (!hostfile_read_key(&cp, &kbits, key)) {
305-
key_free(key);
311+
sshkey_free(key);
306312
continue;
307313
}
308314
#else
@@ -315,7 +321,7 @@ load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
315321
debug3("%s: found %skey type %s in file %s:%lu", __func__,
316322
marker == MRK_NONE ? "" :
317323
(marker == MRK_CA ? "ca " : "revoked "),
318-
key_type(key), path, linenum);
324+
sshkey_type(key), path, linenum);
319325
hostkeys->entries = xrealloc(hostkeys->entries,
320326
hostkeys->num_entries + 1, sizeof(*hostkeys->entries));
321327
hostkeys->entries[hostkeys->num_entries].host = xstrdup(host);
@@ -339,7 +345,7 @@ free_hostkeys(struct hostkeys *hostkeys)
339345
for (i = 0; i < hostkeys->num_entries; i++) {
340346
free(hostkeys->entries[i].host);
341347
free(hostkeys->entries[i].file);
342-
key_free(hostkeys->entries[i].key);
348+
sshkey_free(hostkeys->entries[i].key);
343349
explicit_bzero(hostkeys->entries + i, sizeof(*hostkeys->entries));
344350
}
345351
free(hostkeys->entries);
@@ -348,18 +354,18 @@ free_hostkeys(struct hostkeys *hostkeys)
348354
}
349355

350356
static int
351-
check_key_not_revoked(struct hostkeys *hostkeys, Key *k)
357+
check_key_not_revoked(struct hostkeys *hostkeys, struct sshkey *k)
352358
{
353-
int is_cert = key_is_cert(k);
359+
int is_cert = sshkey_is_cert(k);
354360
u_int i;
355361

356362
for (i = 0; i < hostkeys->num_entries; i++) {
357363
if (hostkeys->entries[i].marker != MRK_REVOKE)
358364
continue;
359-
if (key_equal_public(k, hostkeys->entries[i].key))
365+
if (sshkey_equal_public(k, hostkeys->entries[i].key))
360366
return -1;
361367
if (is_cert &&
362-
key_equal_public(k->cert->signature_key,
368+
sshkey_equal_public(k->cert->signature_key,
363369
hostkeys->entries[i].key))
364370
return -1;
365371
}
@@ -383,11 +389,11 @@ check_key_not_revoked(struct hostkeys *hostkeys, Key *k)
383389
*/
384390
static HostStatus
385391
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)
387393
{
388394
u_int i;
389395
HostStatus end_return = HOST_NEW;
390-
int want_cert = key_is_cert(k);
396+
int want_cert = sshkey_is_cert(k);
391397
HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE;
392398
int proto = (k ? k->type : keytype) == KEY_RSA1 ? 1 : 2;
393399

@@ -411,7 +417,7 @@ check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
411417
break;
412418
}
413419
if (want_cert) {
414-
if (key_equal_public(k->cert->signature_key,
420+
if (sshkey_equal_public(k->cert->signature_key,
415421
hostkeys->entries[i].key)) {
416422
/* A matching CA exists */
417423
end_return = HOST_OK;
@@ -420,7 +426,7 @@ check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
420426
break;
421427
}
422428
} else {
423-
if (key_equal(k, hostkeys->entries[i].key)) {
429+
if (sshkey_equal(k, hostkeys->entries[i].key)) {
424430
end_return = HOST_OK;
425431
if (found != NULL)
426432
*found = hostkeys->entries + i;
@@ -441,7 +447,7 @@ check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
441447
}
442448

443449
HostStatus
444-
check_key_in_hostkeys(struct hostkeys *hostkeys, Key *key,
450+
check_key_in_hostkeys(struct hostkeys *hostkeys, struct sshkey *key,
445451
const struct hostkey_entry **found)
446452
{
447453
if (key == NULL)
@@ -463,11 +469,11 @@ lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype,
463469
*/
464470

465471
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)
468474
{
469475
FILE *f;
470-
int success = 0;
476+
int r, success = 0;
471477
char *hashed_host = NULL;
472478

473479
if (key == NULL)
@@ -485,12 +491,12 @@ add_host_to_hostfile(const char *filename, const char *host, const Key *key,
485491
}
486492
fprintf(f, "%s ", store_hash ? hashed_host : host);
487493

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
489498
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);
494500
fclose(f);
495501
return success;
496502
}

hostfile.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: hostfile.h,v 1.20 2013/07/12 00:19:58 djm Exp $ */
1+
/* $OpenBSD: hostfile.h,v 1.21 2015/01/15 09:40:00 djm Exp $ */
22

33
/*
44
* Author: Tatu Ylonen <[email protected]>
@@ -26,7 +26,7 @@ struct hostkey_entry {
2626
char *host;
2727
char *file;
2828
u_long line;
29-
Key *key;
29+
struct sshkey *key;
3030
HostkeyMarker marker;
3131
};
3232
struct hostkeys;
@@ -35,13 +35,14 @@ struct hostkeys *init_hostkeys(void);
3535
void load_hostkeys(struct hostkeys *, const char *, const char *);
3636
void free_hostkeys(struct hostkeys *);
3737

38-
HostStatus check_key_in_hostkeys(struct hostkeys *, Key *,
38+
HostStatus check_key_in_hostkeys(struct hostkeys *, struct sshkey *,
3939
const struct hostkey_entry **);
4040
int lookup_key_in_hostkeys_by_type(struct hostkeys *, int,
4141
const struct hostkey_entry **);
4242

43-
int hostfile_read_key(char **, int *, Key *);
44-
int add_host_to_hostfile(const char *, const char *, const Key *, int);
43+
int hostfile_read_key(char **, u_int *, struct sshkey *);
44+
int add_host_to_hostfile(const char *, const char *,
45+
const struct sshkey *, int);
4546

4647
#define HASH_MAGIC "|1|"
4748
#define HASH_DELIM '|'

kex.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: kex.h,v 1.65 2015/01/13 19:31:40 markus Exp $ */
1+
/* $OpenBSD: kex.h,v 1.66 2015/01/15 09:40:00 djm Exp $ */
22

33
/*
44
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
@@ -27,6 +27,8 @@
2727
#define KEX_H
2828

2929
#include "mac.h"
30+
#include "buffer.h" /* XXX for typedef */
31+
#include "key.h" /* XXX for typedef */
3032

3133
#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
3234
#include <openssl/ec.h>

0 commit comments

Comments
 (0)