From: martijn Date: Mon, 22 Feb 2021 17:15:02 +0000 (+0000) Subject: Make the ober_get_* set of function to accept a NULL-pointer. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=561e0011345f2bdbf415cc64b5f82e69e3ad1a7f;p=openbsd Make the ober_get_* set of function to accept a NULL-pointer. This allows us to do ber-type checking inside ober_scanf_elements, which will allow for stricter ASN.1 parsing in the future. Manpage feedback and OK claudio@, jmc@ OK claudio@ --- diff --git a/lib/libutil/ber.c b/lib/libutil/ber.c index 25b78308864..175e2bb1960 100644 --- a/lib/libutil/ber.c +++ b/lib/libutil/ber.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ber.c,v 1.20 2021/01/28 19:56:33 martijn Exp $ */ +/* $OpenBSD: ber.c,v 1.21 2021/02/22 17:15:02 martijn Exp $ */ /* * Copyright (c) 2007, 2012 Reyk Floeter @@ -213,7 +213,8 @@ ober_get_integer(struct ber_element *elm, long long *n) if (elm->be_encoding != BER_TYPE_INTEGER) return -1; - *n = elm->be_numeric; + if (n != NULL) + *n = elm->be_numeric; return 0; } @@ -223,7 +224,8 @@ ober_get_enumerated(struct ber_element *elm, long long *n) if (elm->be_encoding != BER_TYPE_ENUMERATED) return -1; - *n = elm->be_numeric; + if (n != NULL) + *n = elm->be_numeric; return 0; } @@ -249,7 +251,8 @@ ober_get_boolean(struct ber_element *elm, int *b) if (elm->be_encoding != BER_TYPE_BOOLEAN) return -1; - *b = !(elm->be_numeric == 0); + if (b != NULL) + *b = !(elm->be_numeric == 0); return 0; } @@ -299,7 +302,8 @@ ober_get_string(struct ber_element *elm, char **s) return -1; #endif - *s = elm->be_val; + if (s != NULL) + *s = elm->be_val; return 0; } @@ -309,8 +313,14 @@ ober_get_nstring(struct ber_element *elm, void **p, size_t *len) if (elm->be_encoding != BER_TYPE_OCTETSTRING) return -1; - *p = elm->be_val; - *len = elm->be_len; + if (len != NULL) + *len = elm->be_len; + if (p != NULL) { + if (len != NULL) + *p = elm->be_val; + else + *p = NULL; + } return 0; } @@ -320,8 +330,10 @@ ober_get_ostring(struct ber_element *elm, struct ber_octetstring *s) if (elm->be_encoding != BER_TYPE_OCTETSTRING) return -1; - s->ostr_val = elm->be_val; - s->ostr_len = elm->be_len; + if (s != NULL) { + s->ostr_val = elm->be_val; + s->ostr_len = elm->be_len; + } return 0; } @@ -354,8 +366,14 @@ ober_get_bitstring(struct ber_element *elm, void **v, size_t *len) if (elm->be_encoding != BER_TYPE_BITSTRING) return -1; - *v = elm->be_val; - *len = elm->be_len; + if (len != NULL) + *len = elm->be_len; + if (v != NULL) { + if (len != NULL) + *v = elm->be_val; + else + *v = NULL; + } return 0; } @@ -543,6 +561,9 @@ ober_get_oid(struct ber_element *elm, struct ber_oid *o) if (elm->be_encoding != BER_TYPE_OBJECT) return (-1); + if (o == NULL) + return 0; + buf = elm->be_val; len = elm->be_len; @@ -709,7 +730,8 @@ ober_scanf_elements(struct ber_element *ber, char *fmt, ...) d = va_arg(ap, int *); if (ober_get_integer(ber, &l) == -1) goto fail; - *d = l; + if (d != NULL) + *d = l; ret++; break; case 'e': @@ -747,8 +769,10 @@ ober_scanf_elements(struct ber_element *ber, char *fmt, ...) case 't': d = va_arg(ap, int *); t = va_arg(ap, unsigned int *); - *d = ber->be_class; - *t = ber->be_type; + if (d != NULL) + *d = ber->be_class; + if (t != NULL) + *t = ber->be_type; ret++; continue; case 'x': diff --git a/lib/libutil/ober_get_string.3 b/lib/libutil/ober_get_string.3 index fda9f35718d..72092d7ee12 100644 --- a/lib/libutil/ober_get_string.3 +++ b/lib/libutil/ober_get_string.3 @@ -1,4 +1,4 @@ -.\" $OpenBSD: ober_get_string.3,v 1.3 2021/01/28 19:56:33 martijn Exp $ +.\" $OpenBSD: ober_get_string.3,v 1.4 2021/02/22 17:15:02 martijn Exp $ .\" .\" Copyright (c) 2007, 2012 Reyk Floeter .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: January 28 2021 $ +.Dd $Mdocdate: February 22 2021 $ .Dt OBER_GET_STRING 3 .Os .Sh NAME @@ -62,12 +62,27 @@ Functions which take two arguments save the value contained in the .Fa root element into the storage location pointed to by the second argument. +If the storage location is +.Dv NULL +then only a type check is performed. Additionally, .Fn ober_get_nstring and .Fn ober_get_bitstring save the number of bytes contained in the string into .Pf * Fa size . +If +.Fa buf +is +.Dv NULL +and size is not +.Dv NULL , +size is set. +.Fa size +must not be +.Dv NULL +to return a valid +.Fa buf . .Pp .Fn ober_scanf_elements retrieves the values from zero or more elements starting at