diff options
author | Chad Loder <cloder@cvs.openbsd.org> | 2005-11-15 21:49:05 +0000 |
---|---|---|
committer | Chad Loder <cloder@cvs.openbsd.org> | 2005-11-15 21:49:05 +0000 |
commit | 505ddd468d1d0f6e6bc7b9e84aec5c49436157f1 (patch) | |
tree | 280404340dd4fffa79e45eb102f71f7db9b1a7a2 /sbin/isakmpd | |
parent | 187917516a1cfdfbf2b5699c8ed48ba76a66b478 (diff) |
Add a new raw2hex function and yank out several pieces of code in other
places that were doing this. Prodding deraadt. OK hshoexer.
Diffstat (limited to 'sbin/isakmpd')
-rw-r--r-- | sbin/isakmpd/field.c | 17 | ||||
-rw-r--r-- | sbin/isakmpd/key.c | 16 | ||||
-rw-r--r-- | sbin/isakmpd/util.c | 25 | ||||
-rw-r--r-- | sbin/isakmpd/util.h | 3 | ||||
-rw-r--r-- | sbin/isakmpd/x509.c | 14 |
5 files changed, 33 insertions, 42 deletions
diff --git a/sbin/isakmpd/field.c b/sbin/isakmpd/field.c index 4d4e3f34b9f..67cbffd12df 100644 --- a/sbin/isakmpd/field.c +++ b/sbin/isakmpd/field.c @@ -1,4 +1,4 @@ -/* $OpenBSD: field.c,v 1.18 2005/11/14 23:25:11 deraadt Exp $ */ +/* $OpenBSD: field.c,v 1.19 2005/11/15 21:49:04 cloder Exp $ */ /* $EOM: field.c,v 1.11 2000/02/20 19:58:37 niklas Exp $ */ /* @@ -61,20 +61,7 @@ static char *(*decode_field[]) (u_int8_t *, size_t, static char * field_debug_raw(u_int8_t *buf, size_t len, struct constant_map **maps) { - char *retval, *p; - - if (len == 0) - return 0; - retval = malloc(3 + len * 2); - if (!retval) - return 0; - strlcpy(retval, "0x", 3 + len * 2); - p = retval + 2; - for (; len > 0; len--) { - snprintf(p, 1 + len * 2, "%02x", *buf++); - p += 2; - } - return retval; + return raw2hex(buf, len); } /* diff --git a/sbin/isakmpd/key.c b/sbin/isakmpd/key.c index fcc9d7e2bee..8c4a2573dcb 100644 --- a/sbin/isakmpd/key.c +++ b/sbin/isakmpd/key.c @@ -1,4 +1,4 @@ -/* $OpenBSD: key.c,v 1.22 2005/04/08 22:32:10 cloder Exp $ */ +/* $OpenBSD: key.c,v 1.23 2005/11/15 21:49:04 cloder Exp $ */ /* * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu) * @@ -95,24 +95,12 @@ key_serialize(int type, int private, void *key, u_int8_t **data, char * key_printable(int type, int private, u_int8_t *data, int datalen) { - char *s; - int i; - switch (type) { case ISAKMP_KEY_PASSPHRASE: return strdup((char *)data); case ISAKMP_KEY_RSA: - s = malloc(datalen * 2 + 1); - if (!s) { - log_error("key_printable: malloc (%d) failed", - datalen * 2 + 1); - return 0; - } - for (i = 0; i < datalen; i++) - snprintf(s + (2 * i), 2 * (datalen - i) + 1, "%02x", - data[i]); - return s; + return raw2hex(data, datalen); default: log_error("key_printable: unknown/unsupported key type %d", diff --git a/sbin/isakmpd/util.c b/sbin/isakmpd/util.c index 7725226620a..dfc08aa0095 100644 --- a/sbin/isakmpd/util.c +++ b/sbin/isakmpd/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.58 2005/07/25 14:56:42 hshoexer Exp $ */ +/* $OpenBSD: util.c,v 1.59 2005/11/15 21:49:04 cloder Exp $ */ /* $EOM: util.c,v 1.23 2000/11/23 12:22:08 niklas Exp $ */ /* @@ -224,6 +224,29 @@ hex2raw(char *s, u_int8_t *buf, size_t sz) return 0; } +/* + * Convert raw binary buffer to a newly allocated hexadecimal string. Returns + * NULL if an error occured. It is the caller's responsibility to free the + * returned string. + */ +char * +raw2hex(u_int8_t *buf, size_t sz) +{ + char *s; + size_t i; + + if ((s = (char *)malloc(sz * 2 + 1)) == NULL) { + log_error("raw2hex: malloc (%lu) failed", (unsigned long)sz * 2 + 1); + return NULL; + } + + for (i = 0; i < sz; i++) + snprintf(s + (2 * i), 2 * (sz - i) + 1, "%02x", buf[i]); + + s[sz * 2] = '\0'; + return s; +} + in_port_t text2port(char *port_str) { diff --git a/sbin/isakmpd/util.h b/sbin/isakmpd/util.h index 393e243e560..04674af5f91 100644 --- a/sbin/isakmpd/util.h +++ b/sbin/isakmpd/util.h @@ -1,4 +1,4 @@ -/* $OpenBSD: util.h,v 1.26 2005/07/25 14:56:42 hshoexer Exp $ */ +/* $OpenBSD: util.h,v 1.27 2005/11/15 21:49:04 cloder Exp $ */ /* $EOM: util.h,v 1.10 2000/10/24 13:33:39 niklas Exp $ */ /* @@ -53,6 +53,7 @@ extern void encode_64(u_int8_t *, u_int64_t); extern u_int32_t rand_32(void); extern u_int8_t *getrandom(u_int8_t *, size_t); extern int hex2raw(char *, u_int8_t *, size_t); +extern char *raw2hex(u_int8_t *, size_t); extern int ones_test(const u_int8_t *, size_t); extern int sockaddr2text(struct sockaddr *, char **, int); extern u_int8_t *sockaddr_addrdata(struct sockaddr *); diff --git a/sbin/isakmpd/x509.c b/sbin/isakmpd/x509.c index 52d6b8f152c..24f9e07b38c 100644 --- a/sbin/isakmpd/x509.c +++ b/sbin/isakmpd/x509.c @@ -1,4 +1,4 @@ -/* $OpenBSD: x509.c,v 1.104 2005/11/14 23:25:11 deraadt Exp $ */ +/* $OpenBSD: x509.c,v 1.105 2005/11/15 21:49:04 cloder Exp $ */ /* $EOM: x509.c,v 1.54 2001/01/16 18:42:16 ho Exp $ */ /* @@ -1283,21 +1283,13 @@ x509_printable(void *cert) { char *s; u_int8_t *data; - u_int32_t datalen, i; + u_int32_t datalen; x509_serialize(cert, &data, &datalen); if (!data) return 0; - s = malloc(datalen * 2 + 1); - if (!s) { - free(data); - log_error("x509_printable: malloc (%d) failed", - datalen * 2 + 1); - return 0; - } - for (i = 0; i < datalen; i++) - snprintf(s + (2 * i), 2 * (datalen - i) + 1, "%02x", data[i]); + s = raw2hex(data, datalen); free(data); return s; } |