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 |
22 | typedef unsigned long ccder_tag; |
23 | #else |
24 | typedef uint8_t ccder_tag; |
25 | #endif |
26 | |
27 | typedef struct ccder_blob { |
28 | uint8_t *cc_ended_by(der_end) der; |
29 | uint8_t *der_end; |
30 | } ccder_blob; |
31 | |
32 | typedef 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 | |
42 | CC_NONNULL((1)) CC_NODISCARD |
43 | bool ccder_blob_encode_tag(ccder_blob *into, ccder_tag tag); |
44 | |
45 | CC_NONNULL((1)) CC_NODISCARD |
46 | bool ccder_blob_encode_len(ccder_blob *into, size_t len); |
47 | |
48 | CC_NONNULL((1)) CC_NODISCARD |
49 | bool ccder_blob_encode_tl(ccder_blob *into, ccder_tag tag, size_t len); |
50 | |
51 | CC_NONNULL((1)) CC_NODISCARD |
52 | bool ccder_blob_encode_body(ccder_blob *into, size_t size, const uint8_t *cc_sized_by(size) body); |
53 | |
54 | CC_NONNULL((1, 4)) CC_NODISCARD |
55 | bool ccder_blob_encode_body_tl(ccder_blob *into, ccder_tag tag, size_t size, const uint8_t *cc_sized_by(size) body); |
56 | |
57 | CC_NONNULL((1, 3)) CC_NODISCARD |
58 | bool ccder_blob_reserve(ccder_blob *into, size_t reserve_size, ccder_blob *out_reserved); |
59 | |
60 | CC_NONNULL((1, 4)) CC_NODISCARD |
61 | bool ccder_blob_reserve_tl(ccder_blob *into, ccder_tag tag, size_t reserve_size, ccder_blob *out_reserved); |
62 | |
63 | CC_NONNULL((1, 2)) CC_NODISCARD |
64 | bool ccder_blob_encode_oid(ccder_blob *into, ccoid_t oid); |
65 | |
66 | CC_NONNULL((1, 4)) CC_NODISCARD |
67 | bool ccder_blob_encode_implicit_integer(ccder_blob *into, ccder_tag implicit_tag, cc_size n, const cc_unit *cc_counted_by(n) s); |
68 | |
69 | CC_NONNULL((1, 3)) CC_NODISCARD |
70 | bool ccder_blob_encode_integer(ccder_blob *into, cc_size n, const cc_unit *cc_counted_by(n) s); |
71 | |
72 | CC_NONNULL((1)) CC_NODISCARD |
73 | bool ccder_blob_encode_implicit_uint64(ccder_blob *into, ccder_tag implicit_tag, uint64_t value); |
74 | |
75 | CC_NONNULL((1)) CC_NODISCARD |
76 | bool ccder_blob_encode_uint64(ccder_blob *into, uint64_t value); |
77 | |
78 | CC_NONNULL((1, 3)) CC_NODISCARD |
79 | bool ccder_blob_encode_octet_string(ccder_blob *into, cc_size n, const cc_unit *cc_counted_by(n) s); |
80 | |
81 | CC_NONNULL((1, 4)) CC_NODISCARD |
82 | bool 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 | |
84 | CC_NONNULL((1, 4)) CC_NODISCARD |
85 | bool 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 | |
87 | CC_NONNULL((1, 3)) CC_NODISCARD |
88 | bool ccder_blob_encode_raw_octet_string(ccder_blob *into, size_t s_size, const uint8_t *cc_sized_by(s_size) s); |
89 | |
90 | CC_NONNULL((1, 3)) CC_NODISCARD |
91 | bool 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. |
94 | CC_NONNULL((1, 2)) CC_NODISCARD |
95 | bool ccder_blob_decode_tag(ccder_read_blob *from, ccder_tag *tag); |
96 | |
97 | CC_NONNULL((1, 2)) CC_NODISCARD |
98 | bool ccder_blob_decode_len(ccder_read_blob *from, size_t *size); |
99 | |
100 | CC_NONNULL((1, 2)) CC_NODISCARD |
101 | bool ccder_blob_decode_len_strict(ccder_read_blob *from, size_t *size); |
102 | |
103 | CC_NONNULL((1, 3)) CC_NODISCARD |
104 | bool ccder_blob_decode_tl(ccder_read_blob *from, ccder_tag expected_tag, size_t *size); |
105 | |
106 | CC_NONNULL((1, 3)) CC_NODISCARD |
107 | bool ccder_blob_decode_tl_strict(ccder_read_blob *from, ccder_tag expected_tag, size_t *size); |
108 | |
109 | CC_NONNULL((1, 3)) CC_NODISCARD |
110 | bool ccder_blob_decode_range(ccder_read_blob *from, ccder_tag expected_tag, ccder_read_blob *range_blob); |
111 | |
112 | CC_NONNULL((1, 3)) CC_NODISCARD |
113 | bool ccder_blob_decode_range_strict(ccder_read_blob *from, ccder_tag expected_tag, ccder_read_blob *range_blob); |
114 | |
115 | CC_NONNULL((1, 2)) CC_NODISCARD |
116 | bool ccder_blob_decode_sequence_tl(ccder_read_blob *from, ccder_read_blob *range_blob); |
117 | |
118 | CC_NONNULL((1, 2)) CC_NODISCARD |
119 | bool ccder_blob_decode_sequence_tl_strict(ccder_read_blob *from, ccder_read_blob *range_blob); |
120 | |
121 | CC_NONNULL((1, 2)) CC_NODISCARD |
122 | bool ccder_blob_decode_uint_n(ccder_read_blob *from, cc_size *n); |
123 | |
124 | CC_NONNULL((1)) CC_NODISCARD |
125 | bool ccder_blob_decode_uint64(ccder_read_blob *from, uint64_t *r); |
126 | |
127 | CC_NONNULL((1, 3)) CC_NODISCARD |
128 | bool ccder_blob_decode_uint(ccder_read_blob *from, cc_size n, cc_unit *cc_counted_by(n)); |
129 | |
130 | CC_NONNULL((1, 3)) CC_NODISCARD |
131 | bool ccder_blob_decode_uint_strict(ccder_read_blob *from, cc_size n, cc_unit *cc_counted_by(n)); |
132 | |
133 | CC_NONNULL((1, 3, 4)) CC_NODISCARD |
134 | bool 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 | |
136 | CC_NONNULL((1, 3, 4)) CC_NODISCARD |
137 | bool 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 | |
139 | CC_NONNULL((1, 2)) CC_NODISCARD |
140 | bool ccder_blob_decode_oid(ccder_read_blob *from, ccoid_t *oidp); |
141 | |
142 | CC_NONNULL((1, 2, 3)) CC_NODISCARD |
143 | bool ccder_blob_decode_bitstring(ccder_read_blob *from, ccder_read_blob *bit_string_range, size_t *bit_count); |
144 | |
145 | CC_NONNULL((1, 2, 3, 4, 5, 6, 7)) CC_NODISCARD |
146 | bool 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 | |