diff options
author | tb <> | 2021-12-28 16:26:53 +0000 |
---|---|---|
committer | tb <> | 2021-12-28 16:26:53 +0000 |
commit | c5e2fed5aebe8491f8f9f05ec8e17fa61fbfca9f (patch) | |
tree | 3fef000e0741e91a46b34ddae411b0e0603ba9fd | |
parent | 0e7d4e6fe6e9b90dd5e9e65301b1c7f33b277995 (diff) | |
download | openbsd-c5e2fed5aebe8491f8f9f05ec8e17fa61fbfca9f.tar.gz openbsd-c5e2fed5aebe8491f8f9f05ec8e17fa61fbfca9f.tar.bz2 openbsd-c5e2fed5aebe8491f8f9f05ec8e17fa61fbfca9f.zip |
Simplify and explain expand_addr() a bit
RFC 3779 section 2.1.2 does a decent job of explaining how IP addresses
are encoded in. What's stored amounts to a prefix with all trailing zero
octets omitted. If there are trailing zero bits in the last non-zero octet,
bs->flags & 7 indicates how many. addr_expand() expands this to an address
of length 4 or 16 depending on whether we deal with IPv4 or IPv6.
Since an address can be the lower or the upper bound of a prefix or
address range, expansion needs to be able to zero-fill or one-fill the
unused bits/octets. No other expansion is ever used, so simplify the
meaning of fill accordingly. There's no need to special case the case
that there are no unused bits, the masking/filling is a noop.
ok jsing
-rw-r--r-- | src/lib/libcrypto/x509/x509_addr.c | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/src/lib/libcrypto/x509/x509_addr.c b/src/lib/libcrypto/x509/x509_addr.c index e66d408ffb..038319087b 100644 --- a/src/lib/libcrypto/x509/x509_addr.c +++ b/src/lib/libcrypto/x509/x509_addr.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: x509_addr.c,v 1.33 2021/12/28 16:21:59 tb Exp $ */ | 1 | /* $OpenBSD: x509_addr.c,v 1.34 2021/12/28 16:26:53 tb Exp $ */ |
2 | /* | 2 | /* |
3 | * Contributed to the OpenSSL Project by the American Registry for | 3 | * Contributed to the OpenSSL Project by the American Registry for |
4 | * Internet Numbers ("ARIN"). | 4 | * Internet Numbers ("ARIN"). |
@@ -362,31 +362,42 @@ X509v3_addr_get_afi(const IPAddressFamily *f) | |||
362 | } | 362 | } |
363 | 363 | ||
364 | /* | 364 | /* |
365 | * Expand the bitstring form of an address into a raw byte array. | 365 | * Expand the bitstring form (RFC 3779, section 2.1.2) of an address into |
366 | * At the moment this is coded for simplicity, not speed. | 366 | * a raw byte array. At the moment this is coded for simplicity, not speed. |
367 | * | ||
368 | * Unused bits in the last octet of |bs| and all bits in subsequent bytes | ||
369 | * of |addr| are set to 0 or 1 depending on whether |fill| is 0 or not. | ||
367 | */ | 370 | */ |
368 | static int | 371 | static int |
369 | addr_expand(unsigned char *addr, const ASN1_BIT_STRING *bs, const int length, | 372 | addr_expand(unsigned char *addr, const ASN1_BIT_STRING *bs, const int length, |
370 | const unsigned char fill) | 373 | uint8_t fill) |
371 | { | 374 | { |
372 | if (bs->length < 0 || bs->length > length) | 375 | if (bs->length < 0 || bs->length > length) |
373 | return 0; | 376 | return 0; |
377 | |||
378 | if (fill != 0) | ||
379 | fill = 0xFF; | ||
380 | |||
374 | if (bs->length > 0) { | 381 | if (bs->length > 0) { |
382 | /* XXX - shouldn't this check ASN1_STRING_FLAG_BITS_LEFT? */ | ||
383 | uint8_t unused_bits = bs->flags & 7; | ||
384 | uint8_t mask = (1 << unused_bits) - 1; | ||
385 | |||
375 | memcpy(addr, bs->data, bs->length); | 386 | memcpy(addr, bs->data, bs->length); |
376 | if ((bs->flags & 7) != 0) { | 387 | |
377 | unsigned char mask = 0xFF >> (8 - (bs->flags & 7)); | 388 | if (fill == 0) |
378 | if (fill == 0) | 389 | addr[bs->length - 1] &= ~mask; |
379 | addr[bs->length - 1] &= ~mask; | 390 | else |
380 | else | 391 | addr[bs->length - 1] |= mask; |
381 | addr[bs->length - 1] |= mask; | ||
382 | } | ||
383 | } | 392 | } |
393 | |||
384 | memset(addr + bs->length, fill, length - bs->length); | 394 | memset(addr + bs->length, fill, length - bs->length); |
395 | |||
385 | return 1; | 396 | return 1; |
386 | } | 397 | } |
387 | 398 | ||
388 | /* | 399 | /* |
389 | * Extract the prefix length from a bitstring. | 400 | * Extract the prefix length from a bitstring: 8 * length - unused bits. |
390 | */ | 401 | */ |
391 | #define addr_prefixlen(bs) ((int) ((bs)->length * 8 - ((bs)->flags & 7))) | 402 | #define addr_prefixlen(bs) ((int) ((bs)->length * 8 - ((bs)->flags & 7))) |
392 | 403 | ||