1/* Copyright (c) (2021,2022) Apple Inc. All rights reserved.
2 *
3 * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which
4 * is contained in the License.txt file distributed with corecrypto) and only to
5 * people who accept that license. IMPORTANT: Any license rights granted to you by
6 * Apple Inc. (if any) are limited to internal use within your organization only on
7 * devices and computers you own or control, for the sole purpose of verifying the
8 * security characteristics and correct functioning of the Apple Software. You may
9 * not, directly or indirectly, redistribute the Apple Software or any portions thereof.
10 */
11
12#ifndef _CORECRYPTO_CCDER_BLOB_H_
13#define _CORECRYPTO_CCDER_BLOB_H_
14
15#include <corecrypto/cc.h>
16#include <corecrypto/ccasn1.h>
17#include <corecrypto/ccn.h>
18
19#define CCDER_MULTIBYTE_TAGS 1
20
21#ifdef CCDER_MULTIBYTE_TAGS
22typedef unsigned long ccder_tag;
23#else
24typedef uint8_t ccder_tag;
25#endif
26
27typedef struct ccder_blob {
28 uint8_t *cc_ended_by(der_end) der;
29 uint8_t *der_end;
30} ccder_blob;
31
32typedef struct ccder_read_blob {
33 const uint8_t *cc_ended_by(der_end) der;
34 const uint8_t *der_end;
35} ccder_read_blob;
36
37#define ccder_size(BEGIN, END) ((size_t)((END) - (BEGIN)))
38#define ccder_blob_size(BLOB) ccder_size((BLOB).der, (BLOB).der_end)
39
40// MARK: - ccder_blob_encode_ functions.
41
42CC_NONNULL((1)) CC_NODISCARD
43bool ccder_blob_encode_tag(ccder_blob *into, ccder_tag tag);
44
45CC_NONNULL((1)) CC_NODISCARD
46bool ccder_blob_encode_len(ccder_blob *into, size_t len);
47
48CC_NONNULL((1)) CC_NODISCARD
49bool ccder_blob_encode_tl(ccder_blob *into, ccder_tag tag, size_t len);
50
51CC_NONNULL((1)) CC_NODISCARD
52bool ccder_blob_encode_body(ccder_blob *into, size_t size, const uint8_t *cc_sized_by(size) body);
53
54CC_NONNULL((1, 4)) CC_NODISCARD
55bool ccder_blob_encode_body_tl(ccder_blob *into, ccder_tag tag, size_t size, const uint8_t *cc_sized_by(size) body);
56
57CC_NONNULL((1, 3)) CC_NODISCARD
58bool ccder_blob_reserve(ccder_blob *into, size_t reserve_size, ccder_blob *out_reserved);
59
60CC_NONNULL((1, 4)) CC_NODISCARD
61bool ccder_blob_reserve_tl(ccder_blob *into, ccder_tag tag, size_t reserve_size, ccder_blob *out_reserved);
62
63CC_NONNULL((1, 2)) CC_NODISCARD
64bool ccder_blob_encode_oid(ccder_blob *into, ccoid_t oid);
65
66CC_NONNULL((1, 4)) CC_NODISCARD
67bool ccder_blob_encode_implicit_integer(ccder_blob *into, ccder_tag implicit_tag, cc_size n, const cc_unit *cc_counted_by(n) s);
68
69CC_NONNULL((1, 3)) CC_NODISCARD
70bool ccder_blob_encode_integer(ccder_blob *into, cc_size n, const cc_unit *cc_counted_by(n) s);
71
72CC_NONNULL((1)) CC_NODISCARD
73bool ccder_blob_encode_implicit_uint64(ccder_blob *into, ccder_tag implicit_tag, uint64_t value);
74
75CC_NONNULL((1)) CC_NODISCARD
76bool ccder_blob_encode_uint64(ccder_blob *into, uint64_t value);
77
78CC_NONNULL((1, 3)) CC_NODISCARD
79bool ccder_blob_encode_octet_string(ccder_blob *into, cc_size n, const cc_unit *cc_counted_by(n) s);
80
81CC_NONNULL((1, 4)) CC_NODISCARD
82bool ccder_blob_encode_implicit_octet_string(ccder_blob *into, ccder_tag implicit_tag, cc_size n, const cc_unit *cc_counted_by(n) s);
83
84CC_NONNULL((1, 4)) CC_NODISCARD
85bool ccder_blob_encode_implicit_raw_octet_string(ccder_blob *into, ccder_tag implicit_tag, size_t s_size, const uint8_t *cc_sized_by(s_size) s);
86
87CC_NONNULL((1, 3)) CC_NODISCARD
88bool ccder_blob_encode_raw_octet_string(ccder_blob *into, size_t s_size, const uint8_t *cc_sized_by(s_size) s);
89
90CC_NONNULL((1, 3)) CC_NODISCARD
91bool ccder_blob_encode_eckey(ccder_blob *into, size_t priv_byte_size, const uint8_t *cc_sized_by(priv_byte_size) priv_key, ccoid_t oid, size_t pub_byte_size, const uint8_t *cc_sized_by(pub_byte_size) pub_key);
92
93// MARK: - ccder_blob_decode_ functions.
94CC_NONNULL((1, 2)) CC_NODISCARD
95bool ccder_blob_decode_tag(ccder_read_blob *from, ccder_tag *tag);
96
97CC_NONNULL((1, 2)) CC_NODISCARD
98bool ccder_blob_decode_len(ccder_read_blob *from, size_t *size);
99
100CC_NONNULL((1, 2)) CC_NODISCARD
101bool ccder_blob_decode_len_strict(ccder_read_blob *from, size_t *size);
102
103CC_NONNULL((1, 3)) CC_NODISCARD
104bool ccder_blob_decode_tl(ccder_read_blob *from, ccder_tag expected_tag, size_t *size);
105
106CC_NONNULL((1, 3)) CC_NODISCARD
107bool ccder_blob_decode_tl_strict(ccder_read_blob *from, ccder_tag expected_tag, size_t *size);
108
109CC_NONNULL((1, 3)) CC_NODISCARD
110bool ccder_blob_decode_range(ccder_read_blob *from, ccder_tag expected_tag, ccder_read_blob *range_blob);
111
112CC_NONNULL((1, 3)) CC_NODISCARD
113bool ccder_blob_decode_range_strict(ccder_read_blob *from, ccder_tag expected_tag, ccder_read_blob *range_blob);
114
115CC_NONNULL((1, 2)) CC_NODISCARD
116bool ccder_blob_decode_sequence_tl(ccder_read_blob *from, ccder_read_blob *range_blob);
117
118CC_NONNULL((1, 2)) CC_NODISCARD
119bool ccder_blob_decode_sequence_tl_strict(ccder_read_blob *from, ccder_read_blob *range_blob);
120
121CC_NONNULL((1, 2)) CC_NODISCARD
122bool ccder_blob_decode_uint_n(ccder_read_blob *from, cc_size *n);
123
124CC_NONNULL((1)) CC_NODISCARD
125bool ccder_blob_decode_uint64(ccder_read_blob *from, uint64_t *r);
126
127CC_NONNULL((1, 3)) CC_NODISCARD
128bool ccder_blob_decode_uint(ccder_read_blob *from, cc_size n, cc_unit *cc_counted_by(n));
129
130CC_NONNULL((1, 3)) CC_NODISCARD
131bool ccder_blob_decode_uint_strict(ccder_read_blob *from, cc_size n, cc_unit *cc_counted_by(n));
132
133CC_NONNULL((1, 3, 4)) CC_NODISCARD
134bool ccder_blob_decode_seqii(ccder_read_blob *from, size_t n, cc_unit *cc_counted_by(n) r, cc_unit *cc_counted_by(n) s);
135
136CC_NONNULL((1, 3, 4)) CC_NODISCARD
137bool ccder_blob_decode_seqii_strict(ccder_read_blob *from, size_t n, cc_unit *cc_counted_by(n) r, cc_unit *cc_counted_by(n) s);
138
139CC_NONNULL((1, 2)) CC_NODISCARD
140bool ccder_blob_decode_oid(ccder_read_blob *from, ccoid_t *oidp);
141
142CC_NONNULL((1, 2, 3)) CC_NODISCARD
143bool ccder_blob_decode_bitstring(ccder_read_blob *from, ccder_read_blob *bit_string_range, size_t *bit_count);
144
145CC_NONNULL((1, 2, 3, 4, 5, 6, 7)) CC_NODISCARD
146bool ccder_blob_decode_eckey(ccder_read_blob *from, uint64_t *version, size_t *priv_key_byte_size, const uint8_t *cc_sized_by(*priv_key_byte_size) *priv_key, ccoid_t *oid, size_t *pub_key_byte_size, const uint8_t *cc_sized_by(*pub_key_byte_size) *pub_key, size_t *pub_key_bit_count);
147
148#endif /* _CORECRYPTO_CCDER_BLOB_H_ */
149