summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorjsing <>2022-03-26 14:47:58 +0000
committerjsing <>2022-03-26 14:47:58 +0000
commitc6fe14d607c4444cd5cbc220cf8ca19aee4d8609 (patch)
tree0a8d6b082b77181948d6515b5e05a897597375c2 /src/lib
parent8073a49f93039cb1883eddcebe8c45e7b09510b4 (diff)
downloadopenbsd-c6fe14d607c4444cd5cbc220cf8ca19aee4d8609.tar.gz
openbsd-c6fe14d607c4444cd5cbc220cf8ca19aee4d8609.tar.bz2
openbsd-c6fe14d607c4444cd5cbc220cf8ca19aee4d8609.zip
Provide asn1_get_primitive()
This takes a CBS, gets the ASN.1 identifier and length, ensures the resulting identifier is a valid primitive, then returns the tag number and the content as a CBS. ok inoguchi@ tb@
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/libcrypto/asn1/asn1_lib.c33
-rw-r--r--src/lib/libcrypto/asn1/asn1_locl.h4
2 files changed, 35 insertions, 2 deletions
diff --git a/src/lib/libcrypto/asn1/asn1_lib.c b/src/lib/libcrypto/asn1/asn1_lib.c
index 542a72f6f1..6a29c327fe 100644
--- a/src/lib/libcrypto/asn1/asn1_lib.c
+++ b/src/lib/libcrypto/asn1/asn1_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: asn1_lib.c,v 1.51 2021/12/25 07:04:03 jsing Exp $ */ 1/* $OpenBSD: asn1_lib.c,v 1.52 2022/03/26 14:47:58 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2021 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2021 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -16,6 +16,7 @@
16 */ 16 */
17 17
18#include <limits.h> 18#include <limits.h>
19#include <stdlib.h>
19 20
20#include "bytestring.h" 21#include "bytestring.h"
21 22
@@ -169,3 +170,33 @@ asn1_get_object_cbs(CBS *cbs, int der_mode, uint8_t *out_tag_class,
169 170
170 return 1; 171 return 1;
171} 172}
173
174int
175asn1_get_primitive(CBS *cbs, int der_mode, uint32_t *out_tag_number,
176 CBS *out_content)
177{
178 int constructed, indefinite;
179 uint32_t tag_number, length;
180 uint8_t tag_class;
181
182 *out_tag_number = 0;
183
184 CBS_init(out_content, NULL, 0);
185
186 if (!asn1_get_identifier_cbs(cbs, der_mode, &tag_class, &constructed,
187 &tag_number))
188 return 0;
189 if (!asn1_get_length_cbs(cbs, der_mode, &indefinite, &length))
190 return 0;
191
192 /* A primitive is not constructed and has a definite length. */
193 if (constructed || indefinite)
194 return 0;
195
196 if (!CBS_get_bytes(cbs, out_content, length))
197 return 0;
198
199 *out_tag_number = tag_number;
200
201 return 1;
202}
diff --git a/src/lib/libcrypto/asn1/asn1_locl.h b/src/lib/libcrypto/asn1/asn1_locl.h
index 12f7eadfb3..756e4070ba 100644
--- a/src/lib/libcrypto/asn1/asn1_locl.h
+++ b/src/lib/libcrypto/asn1/asn1_locl.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: asn1_locl.h,v 1.23 2022/03/19 17:49:32 jsing Exp $ */ 1/* $OpenBSD: asn1_locl.h,v 1.24 2022/03/26 14:47:58 jsing Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2006. 3 * project 2006.
4 */ 4 */
@@ -196,6 +196,8 @@ int asn1_d2i_read_bio(BIO *in, BUF_MEM **pb);
196int asn1_get_object_cbs(CBS *cbs, int der_mode, uint8_t *out_class, 196int asn1_get_object_cbs(CBS *cbs, int der_mode, uint8_t *out_class,
197 int *out_constructed, uint32_t *out_tag_number, int *out_indefinite, 197 int *out_constructed, uint32_t *out_tag_number, int *out_indefinite,
198 uint32_t *out_length); 198 uint32_t *out_length);
199int asn1_get_primitive(CBS *cbs, int der_mode, uint32_t *out_tag_number,
200 CBS *out_content);
199 201
200int asn1_tag2charwidth(int tag); 202int asn1_tag2charwidth(int tag);
201 203