summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordoug <>2015-04-25 15:28:47 +0000
committerdoug <>2015-04-25 15:28:47 +0000
commitf7c257c94b0ba9258a1f7a5ec03099db9586b791 (patch)
tree2d4124d3ad14035e2d57d39b5be185892b4a24c7 /src
parent61630c9631a2bd70570f348c1a65eefb0a864d9d (diff)
downloadopenbsd-f7c257c94b0ba9258a1f7a5ec03099db9586b791.tar.gz
openbsd-f7c257c94b0ba9258a1f7a5ec03099db9586b791.tar.bz2
openbsd-f7c257c94b0ba9258a1f7a5ec03099db9586b791.zip
Check for invalid leading zeros in CBS_get_asn1_uint64.
ASN.1 integers cannot have all zeros or all ones for the first 9 bits. This rule ensures the numbers are encoded with the smallest number of content octets (see ITU-T Rec X.690 section 8.3.2). Based on BoringSSL commit 5933723b7b592e9914f703d630b596e140c93e16 ok deraadt@ jsing@
Diffstat (limited to 'src')
-rw-r--r--src/lib/libssl/bs_cbs.c11
-rw-r--r--src/lib/libssl/src/ssl/bs_cbs.c11
-rw-r--r--src/regress/lib/libssl/bytestring/bytestringtest.c6
3 files changed, 20 insertions, 8 deletions
diff --git a/src/lib/libssl/bs_cbs.c b/src/lib/libssl/bs_cbs.c
index c3d3a8abf2..d7c0977cf3 100644
--- a/src/lib/libssl/bs_cbs.c
+++ b/src/lib/libssl/bs_cbs.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bs_cbs.c,v 1.2 2015/02/06 22:22:33 doug Exp $ */ 1/* $OpenBSD: bs_cbs.c,v 1.3 2015/04/25 15:28:47 doug Exp $ */
2/* 2/*
3 * Copyright (c) 2014, Google Inc. 3 * Copyright (c) 2014, Google Inc.
4 * 4 *
@@ -317,6 +317,7 @@ CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value)
317 return CBS_data(cbs)[0] == tag_value; 317 return CBS_data(cbs)[0] == tag_value;
318} 318}
319 319
320/* Encoding details are in ASN.1: X.690 section 8.3 */
320int 321int
321CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) 322CBS_get_asn1_uint64(CBS *cbs, uint64_t *out)
322{ 323{
@@ -332,11 +333,15 @@ CBS_get_asn1_uint64(CBS *cbs, uint64_t *out)
332 len = CBS_len(&bytes); 333 len = CBS_len(&bytes);
333 334
334 if (len == 0) 335 if (len == 0)
335 /* An INTEGER is encoded with at least one octet. */ 336 /* An INTEGER is encoded with at least one content octet. */
336 return 0; 337 return 0;
337 338
338 if ((data[0] & 0x80) != 0) 339 if ((data[0] & 0x80) != 0)
339 /* negative number */ 340 /* Negative number. */
341 return 0;
342
343 if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0)
344 /* Violates smallest encoding rule: excessive leading zeros. */
340 return 0; 345 return 0;
341 346
342 for (i = 0; i < len; i++) { 347 for (i = 0; i < len; i++) {
diff --git a/src/lib/libssl/src/ssl/bs_cbs.c b/src/lib/libssl/src/ssl/bs_cbs.c
index c3d3a8abf2..d7c0977cf3 100644
--- a/src/lib/libssl/src/ssl/bs_cbs.c
+++ b/src/lib/libssl/src/ssl/bs_cbs.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bs_cbs.c,v 1.2 2015/02/06 22:22:33 doug Exp $ */ 1/* $OpenBSD: bs_cbs.c,v 1.3 2015/04/25 15:28:47 doug Exp $ */
2/* 2/*
3 * Copyright (c) 2014, Google Inc. 3 * Copyright (c) 2014, Google Inc.
4 * 4 *
@@ -317,6 +317,7 @@ CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value)
317 return CBS_data(cbs)[0] == tag_value; 317 return CBS_data(cbs)[0] == tag_value;
318} 318}
319 319
320/* Encoding details are in ASN.1: X.690 section 8.3 */
320int 321int
321CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) 322CBS_get_asn1_uint64(CBS *cbs, uint64_t *out)
322{ 323{
@@ -332,11 +333,15 @@ CBS_get_asn1_uint64(CBS *cbs, uint64_t *out)
332 len = CBS_len(&bytes); 333 len = CBS_len(&bytes);
333 334
334 if (len == 0) 335 if (len == 0)
335 /* An INTEGER is encoded with at least one octet. */ 336 /* An INTEGER is encoded with at least one content octet. */
336 return 0; 337 return 0;
337 338
338 if ((data[0] & 0x80) != 0) 339 if ((data[0] & 0x80) != 0)
339 /* negative number */ 340 /* Negative number. */
341 return 0;
342
343 if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0)
344 /* Violates smallest encoding rule: excessive leading zeros. */
340 return 0; 345 return 0;
341 346
342 for (i = 0; i < len; i++) { 347 for (i = 0; i < len; i++) {
diff --git a/src/regress/lib/libssl/bytestring/bytestringtest.c b/src/regress/lib/libssl/bytestring/bytestringtest.c
index 8269151127..7ae9397a35 100644
--- a/src/regress/lib/libssl/bytestring/bytestringtest.c
+++ b/src/regress/lib/libssl/bytestring/bytestringtest.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bytestringtest.c,v 1.3 2015/02/16 06:48:17 doug Exp $ */ 1/* $OpenBSD: bytestringtest.c,v 1.4 2015/04/25 15:28:47 doug Exp $ */
2/* 2/*
3 * Copyright (c) 2014, Google Inc. 3 * Copyright (c) 2014, Google Inc.
4 * 4 *
@@ -607,8 +607,10 @@ static const ASN1_INVALID_UINT64_TEST kAsn1InvalidUint64Tests[] = {
607 {"\x02\x00", 2}, 607 {"\x02\x00", 2},
608 /* Negative number. */ 608 /* Negative number. */
609 {"\x02\x01\x80", 3}, 609 {"\x02\x01\x80", 3},
610 /* Overflow */ 610 /* Overflow. */
611 {"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11}, 611 {"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11},
612 /* Leading zeros. */
613 {"\x02\x02\x00\x01", 4},
612}; 614};
613 615
614static int 616static int