Allow out_present to be NULL in CBS_get_optional_asn1

This is useful to skip an optional element, and mirrors the behaviour of
CBS_get_optional_asn1_octet_string.

Change-Id: Icb538c5e99a1d4e46412cae3c438184a94fab339
Reviewed-on: https://boringssl-review.googlesource.com/5800
Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
Paul Lietar 2015-09-03 14:40:21 +01:00 committed by Adam Langley
parent c9d40ba5b0
commit 23b185a3cf
2 changed files with 12 additions and 7 deletions

View File

@ -329,14 +329,19 @@ int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
} }
int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) { int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, unsigned tag) {
int present = 0;
if (CBS_peek_asn1_tag(cbs, tag)) { if (CBS_peek_asn1_tag(cbs, tag)) {
if (!CBS_get_asn1(cbs, out, tag)) { if (!CBS_get_asn1(cbs, out, tag)) {
return 0; return 0;
} }
*out_present = 1; present = 1;
} else {
*out_present = 0;
} }
if (out_present != NULL) {
*out_present = present;
}
return 1; return 1;
} }

View File

@ -178,10 +178,10 @@ OPENSSL_EXPORT int CBS_get_any_ber_asn1_element(CBS *cbs, CBS *out,
* in 64 bits. */ * in 64 bits. */
OPENSSL_EXPORT int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out); OPENSSL_EXPORT int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out);
/* CBS_get_optional_asn1 gets an optional explicitly-tagged element /* CBS_get_optional_asn1 gets an optional explicitly-tagged element from |cbs|
* from |cbs| tagged with |tag| and sets |*out| to its contents. If * tagged with |tag| and sets |*out| to its contents. If present and if
* present, it sets |*out_present| to one, otherwise zero. It returns * |out_present| is not NULL, it sets |*out_present| to one, otherwise zero. It
* one on success, whether or not the element was present, and zero on * returns one on success, whether or not the element was present, and zero on
* decode failure. */ * decode failure. */
OPENSSL_EXPORT int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present, OPENSSL_EXPORT int CBS_get_optional_asn1(CBS *cbs, CBS *out, int *out_present,
unsigned tag); unsigned tag);