Skip to content

Commit

Permalink
upstream commit
Browse files Browse the repository at this point in the history
sync ssh-keysign, ssh-keygen and some dependencies to the
 new buffer/key API; mostly mechanical, ok markus@
  • Loading branch information
djmdjm committed Jan 15, 2015
1 parent e4ebf55 commit 1129dcf
Show file tree
Hide file tree
Showing 13 changed files with 594 additions and 494 deletions.
30 changes: 16 additions & 14 deletions dns.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: dns.c,v 1.32 2014/12/21 22:27:56 djm Exp $ */
/* $OpenBSD: dns.c,v 1.33 2015/01/15 09:40:00 djm Exp $ */

/*
* Copyright (c) 2003 Wesley Griffin. All rights reserved.
Expand Down Expand Up @@ -38,7 +38,8 @@
#include <stdlib.h>

#include "xmalloc.h"
#include "key.h"
#include "sshkey.h"
#include "ssherr.h"
#include "dns.h"
#include "log.h"
#include "digest.h"
Expand Down Expand Up @@ -78,9 +79,9 @@ dns_result_totext(unsigned int res)
*/
static int
dns_read_key(u_int8_t *algorithm, u_int8_t *digest_type,
u_char **digest, u_int *digest_len, Key *key)
u_char **digest, size_t *digest_len, struct sshkey *key)
{
int success = 0;
int r, success = 0;
int fp_alg = -1;

switch (key->type) {
Expand Down Expand Up @@ -121,9 +122,10 @@ dns_read_key(u_int8_t *algorithm, u_int8_t *digest_type,
}

if (*algorithm && *digest_type) {
*digest = key_fingerprint_raw(key, fp_alg, digest_len);
if (*digest == NULL)
fatal("dns_read_key: null from key_fingerprint_raw()");
if ((r = sshkey_fingerprint_raw(key, fp_alg, digest,
digest_len)) != 0)
fatal("%s: sshkey_fingerprint_raw: %s", __func__,
ssh_err(r));
success = 1;
} else {
*digest = NULL;
Expand All @@ -139,7 +141,7 @@ dns_read_key(u_int8_t *algorithm, u_int8_t *digest_type,
*/
static int
dns_read_rdata(u_int8_t *algorithm, u_int8_t *digest_type,
u_char **digest, u_int *digest_len, u_char *rdata, int rdata_len)
u_char **digest, size_t *digest_len, u_char *rdata, int rdata_len)
{
int success = 0;

Expand Down Expand Up @@ -200,7 +202,7 @@ is_numeric_hostname(const char *hostname)
*/
int
verify_host_key_dns(const char *hostname, struct sockaddr *address,
Key *hostkey, int *flags)
struct sshkey *hostkey, int *flags)
{
u_int counter;
int result;
Expand All @@ -209,12 +211,12 @@ verify_host_key_dns(const char *hostname, struct sockaddr *address,
u_int8_t hostkey_algorithm;
u_int8_t hostkey_digest_type = SSHFP_HASH_RESERVED;
u_char *hostkey_digest;
u_int hostkey_digest_len;
size_t hostkey_digest_len;

u_int8_t dnskey_algorithm;
u_int8_t dnskey_digest_type;
u_char *dnskey_digest;
u_int dnskey_digest_len;
size_t dnskey_digest_len;

*flags = 0;

Expand Down Expand Up @@ -310,21 +312,21 @@ verify_host_key_dns(const char *hostname, struct sockaddr *address,
* Export the fingerprint of a key as a DNS resource record
*/
int
export_dns_rr(const char *hostname, Key *key, FILE *f, int generic)
export_dns_rr(const char *hostname, struct sshkey *key, FILE *f, int generic)
{
u_int8_t rdata_pubkey_algorithm = 0;
u_int8_t rdata_digest_type = SSHFP_HASH_RESERVED;
u_int8_t dtype;
u_char *rdata_digest;
u_int i, rdata_digest_len;
size_t i, rdata_digest_len;
int success = 0;

for (dtype = SSHFP_HASH_SHA1; dtype < SSHFP_HASH_MAX; dtype++) {
rdata_digest_type = dtype;
if (dns_read_key(&rdata_pubkey_algorithm, &rdata_digest_type,
&rdata_digest, &rdata_digest_len, key)) {
if (generic) {
fprintf(f, "%s IN TYPE%d \\# %d %02x %02x ",
fprintf(f, "%s IN TYPE%d \\# %zu %02x %02x ",
hostname, DNS_RDATATYPE_SSHFP,
2 + rdata_digest_len,
rdata_pubkey_algorithm, rdata_digest_type);
Expand Down
7 changes: 4 additions & 3 deletions dns.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: dns.h,v 1.13 2014/04/20 09:24:26 logan Exp $ */
/* $OpenBSD: dns.h,v 1.14 2015/01/15 09:40:00 djm Exp $ */

/*
* Copyright (c) 2003 Wesley Griffin. All rights reserved.
Expand Down Expand Up @@ -50,7 +50,8 @@ enum sshfp_hashes {
#define DNS_VERIFY_MATCH 0x00000002
#define DNS_VERIFY_SECURE 0x00000004

int verify_host_key_dns(const char *, struct sockaddr *, Key *, int *);
int export_dns_rr(const char *, Key *, FILE *, int);
int verify_host_key_dns(const char *, struct sockaddr *,
struct sshkey *, int *);
int export_dns_rr(const char *, struct sshkey *, FILE *, int);

#endif /* DNS_H */
74 changes: 40 additions & 34 deletions hostfile.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: hostfile.c,v 1.58 2014/10/20 03:43:01 djm Exp $ */
/* $OpenBSD: hostfile.c,v 1.59 2015/01/15 09:40:00 djm Exp $ */
/*
* Author: Tatu Ylonen <[email protected]>
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
Expand Down Expand Up @@ -51,10 +51,11 @@

#include "xmalloc.h"
#include "match.h"
#include "key.h"
#include "sshkey.h"
#include "hostfile.h"
#include "log.h"
#include "misc.h"
#include "ssherr.h"
#include "digest.h"
#include "hmac.h"

Expand Down Expand Up @@ -155,15 +156,16 @@ host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
*/

int
hostfile_read_key(char **cpp, int *bitsp, Key *ret)
hostfile_read_key(char **cpp, u_int *bitsp, struct sshkey *ret)
{
char *cp;
int r;

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

if (key_read(ret, &cp) != 1)
if ((r = sshkey_read(ret, &cp)) != 0)
return 0;

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

/* Return results. */
*cpp = cp;
if (bitsp != NULL) {
if ((*bitsp = key_size(ret)) <= 0)
return 0;
}
if (bitsp != NULL)
*bitsp = sshkey_size(ret);
return 1;
}

static int
hostfile_check_key(int bits, const Key *key, const char *host,
hostfile_check_key(int bits, const struct sshkey *key, const char *host,
const char *filename, u_long linenum)
{
#ifdef WITH_SSH1
Expand Down Expand Up @@ -249,8 +249,8 @@ load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
u_long linenum = 0, num_loaded = 0;
char *cp, *cp2, *hashed_host;
HostkeyMarker marker;
Key *key;
int kbits;
struct sshkey *key;
u_int kbits;

if ((f = fopen(path, "r")) == NULL)
return;
Expand Down Expand Up @@ -296,13 +296,19 @@ load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
* Extract the key from the line. This will skip any leading
* whitespace. Ignore badly formatted lines.
*/
key = key_new(KEY_UNSPEC);
if ((key = sshkey_new(KEY_UNSPEC)) == NULL) {
error("%s: sshkey_new failed", __func__);
break;
}
if (!hostfile_read_key(&cp, &kbits, key)) {
key_free(key);
sshkey_free(key);
#ifdef WITH_SSH1
key = key_new(KEY_RSA1);
if ((key = sshkey_new(KEY_RSA1)) == NULL) {
error("%s: sshkey_new failed", __func__);
break;
}
if (!hostfile_read_key(&cp, &kbits, key)) {
key_free(key);
sshkey_free(key);
continue;
}
#else
Expand All @@ -315,7 +321,7 @@ load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
debug3("%s: found %skey type %s in file %s:%lu", __func__,
marker == MRK_NONE ? "" :
(marker == MRK_CA ? "ca " : "revoked "),
key_type(key), path, linenum);
sshkey_type(key), path, linenum);
hostkeys->entries = xrealloc(hostkeys->entries,
hostkeys->num_entries + 1, sizeof(*hostkeys->entries));
hostkeys->entries[hostkeys->num_entries].host = xstrdup(host);
Expand All @@ -339,7 +345,7 @@ free_hostkeys(struct hostkeys *hostkeys)
for (i = 0; i < hostkeys->num_entries; i++) {
free(hostkeys->entries[i].host);
free(hostkeys->entries[i].file);
key_free(hostkeys->entries[i].key);
sshkey_free(hostkeys->entries[i].key);
explicit_bzero(hostkeys->entries + i, sizeof(*hostkeys->entries));
}
free(hostkeys->entries);
Expand All @@ -348,18 +354,18 @@ free_hostkeys(struct hostkeys *hostkeys)
}

static int
check_key_not_revoked(struct hostkeys *hostkeys, Key *k)
check_key_not_revoked(struct hostkeys *hostkeys, struct sshkey *k)
{
int is_cert = key_is_cert(k);
int is_cert = sshkey_is_cert(k);
u_int i;

for (i = 0; i < hostkeys->num_entries; i++) {
if (hostkeys->entries[i].marker != MRK_REVOKE)
continue;
if (key_equal_public(k, hostkeys->entries[i].key))
if (sshkey_equal_public(k, hostkeys->entries[i].key))
return -1;
if (is_cert &&
key_equal_public(k->cert->signature_key,
sshkey_equal_public(k->cert->signature_key,
hostkeys->entries[i].key))
return -1;
}
Expand All @@ -383,11 +389,11 @@ check_key_not_revoked(struct hostkeys *hostkeys, Key *k)
*/
static HostStatus
check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
Key *k, int keytype, const struct hostkey_entry **found)
struct sshkey *k, int keytype, const struct hostkey_entry **found)
{
u_int i;
HostStatus end_return = HOST_NEW;
int want_cert = key_is_cert(k);
int want_cert = sshkey_is_cert(k);
HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE;
int proto = (k ? k->type : keytype) == KEY_RSA1 ? 1 : 2;

Expand All @@ -411,7 +417,7 @@ check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
break;
}
if (want_cert) {
if (key_equal_public(k->cert->signature_key,
if (sshkey_equal_public(k->cert->signature_key,
hostkeys->entries[i].key)) {
/* A matching CA exists */
end_return = HOST_OK;
Expand All @@ -420,7 +426,7 @@ check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
break;
}
} else {
if (key_equal(k, hostkeys->entries[i].key)) {
if (sshkey_equal(k, hostkeys->entries[i].key)) {
end_return = HOST_OK;
if (found != NULL)
*found = hostkeys->entries + i;
Expand All @@ -441,7 +447,7 @@ check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
}

HostStatus
check_key_in_hostkeys(struct hostkeys *hostkeys, Key *key,
check_key_in_hostkeys(struct hostkeys *hostkeys, struct sshkey *key,
const struct hostkey_entry **found)
{
if (key == NULL)
Expand All @@ -463,11 +469,11 @@ lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype,
*/

int
add_host_to_hostfile(const char *filename, const char *host, const Key *key,
int store_hash)
add_host_to_hostfile(const char *filename, const char *host,
const struct sshkey *key, int store_hash)
{
FILE *f;
int success = 0;
int r, success = 0;
char *hashed_host = NULL;

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

if (key_write(key, f)) {
if ((r = sshkey_write(key, f)) != 0) {
error("%s: saving key in %s failed: %s",
__func__, filename, ssh_err(r));
} else
success = 1;
} else {
error("add_host_to_hostfile: saving key in %s failed", filename);
}
fprintf(f, "\n");
fputs("\n", f);
fclose(f);
return success;
}
11 changes: 6 additions & 5 deletions hostfile.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: hostfile.h,v 1.20 2013/07/12 00:19:58 djm Exp $ */
/* $OpenBSD: hostfile.h,v 1.21 2015/01/15 09:40:00 djm Exp $ */

/*
* Author: Tatu Ylonen <[email protected]>
Expand Down Expand Up @@ -26,7 +26,7 @@ struct hostkey_entry {
char *host;
char *file;
u_long line;
Key *key;
struct sshkey *key;
HostkeyMarker marker;
};
struct hostkeys;
Expand All @@ -35,13 +35,14 @@ struct hostkeys *init_hostkeys(void);
void load_hostkeys(struct hostkeys *, const char *, const char *);
void free_hostkeys(struct hostkeys *);

HostStatus check_key_in_hostkeys(struct hostkeys *, Key *,
HostStatus check_key_in_hostkeys(struct hostkeys *, struct sshkey *,
const struct hostkey_entry **);
int lookup_key_in_hostkeys_by_type(struct hostkeys *, int,
const struct hostkey_entry **);

int hostfile_read_key(char **, int *, Key *);
int add_host_to_hostfile(const char *, const char *, const Key *, int);
int hostfile_read_key(char **, u_int *, struct sshkey *);
int add_host_to_hostfile(const char *, const char *,
const struct sshkey *, int);

#define HASH_MAGIC "|1|"
#define HASH_DELIM '|'
Expand Down
4 changes: 3 additions & 1 deletion kex.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $OpenBSD: kex.h,v 1.65 2015/01/13 19:31:40 markus Exp $ */
/* $OpenBSD: kex.h,v 1.66 2015/01/15 09:40:00 djm Exp $ */

/*
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
Expand Down Expand Up @@ -27,6 +27,8 @@
#define KEX_H

#include "mac.h"
#include "buffer.h" /* XXX for typedef */
#include "key.h" /* XXX for typedef */

#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
#include <openssl/ec.h>
Expand Down
Loading

0 comments on commit 1129dcf

Please sign in to comment.