/src/openssl/crypto/asn1/evp_asn1.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/asn1.h> |
13 | | #include <openssl/asn1t.h> |
14 | | #include "crypto/asn1.h" |
15 | | |
16 | | int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len) |
17 | 0 | { |
18 | 0 | ASN1_STRING *os; |
19 | |
|
20 | 0 | if ((os = ASN1_OCTET_STRING_new()) == NULL) |
21 | 0 | return 0; |
22 | 0 | if (!ASN1_OCTET_STRING_set(os, data, len)) { |
23 | 0 | ASN1_OCTET_STRING_free(os); |
24 | 0 | return 0; |
25 | 0 | } |
26 | 0 | ASN1_TYPE_set(a, V_ASN1_OCTET_STRING, os); |
27 | 0 | return 1; |
28 | 0 | } |
29 | | |
30 | | /* int max_len: for returned value |
31 | | * if passing NULL in data, nothing is copied but the necessary length |
32 | | * for it is returned. |
33 | | */ |
34 | | int ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len) |
35 | 0 | { |
36 | 0 | int ret, num; |
37 | 0 | size_t tmp; |
38 | 0 | const unsigned char *p; |
39 | |
|
40 | 0 | if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) { |
41 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG); |
42 | 0 | return -1; |
43 | 0 | } |
44 | 0 | p = ASN1_STRING_get0_data(a->value.octet_string); |
45 | 0 | tmp = ASN1_STRING_length_ex(a->value.octet_string); |
46 | 0 | if (tmp > INT_MAX) { |
47 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_TOO_LARGE); |
48 | 0 | return -1; |
49 | 0 | } |
50 | 0 | ret = (int)tmp; |
51 | |
|
52 | 0 | if (ret < max_len) |
53 | 0 | num = ret; |
54 | 0 | else |
55 | 0 | num = max_len; |
56 | 0 | if (num > 0 && data != NULL) |
57 | 0 | memcpy(data, p, num); |
58 | 0 | return ret; |
59 | 0 | } |
60 | | |
61 | | static ossl_inline void asn1_type_init_oct(ASN1_OCTET_STRING *oct, |
62 | | unsigned char *data, int len) |
63 | 0 | { |
64 | 0 | oct->data = data; |
65 | 0 | oct->type = V_ASN1_OCTET_STRING; |
66 | 0 | oct->length = len; |
67 | 0 | oct->flags = 0; |
68 | 0 | } |
69 | | |
70 | | /* |
71 | | * This function copies 'anum' to 'num' and the data of 'oct' to 'data'. |
72 | | * If the length of 'data' > 'max_len', copies only the first 'max_len' |
73 | | * bytes, but returns the full length of 'oct'; this allows distinguishing |
74 | | * whether all the data was copied. |
75 | | */ |
76 | | static int asn1_type_get_int_oct(ASN1_OCTET_STRING *oct, int32_t anum, |
77 | | long *num, unsigned char *data, int max_len) |
78 | 0 | { |
79 | 0 | int ret, n; |
80 | 0 | size_t tmp; |
81 | |
|
82 | 0 | if (num != NULL) |
83 | 0 | *num = anum; |
84 | |
|
85 | 0 | tmp = ASN1_STRING_length_ex(oct); |
86 | |
|
87 | 0 | if (tmp > INT_MAX) |
88 | 0 | tmp = INT_MAX; |
89 | |
|
90 | 0 | ret = (int)tmp; |
91 | |
|
92 | 0 | if (max_len > ret) |
93 | 0 | n = ret; |
94 | 0 | else |
95 | 0 | n = max_len; |
96 | |
|
97 | 0 | if (data != NULL) |
98 | 0 | memcpy(data, ASN1_STRING_get0_data(oct), n); |
99 | |
|
100 | 0 | return ret; |
101 | 0 | } |
102 | | |
103 | | typedef struct { |
104 | | int32_t num; |
105 | | ASN1_OCTET_STRING *oct; |
106 | | } asn1_int_oct; |
107 | | |
108 | | ASN1_SEQUENCE(asn1_int_oct) = { |
109 | | ASN1_EMBED(asn1_int_oct, num, INT32), |
110 | | ASN1_SIMPLE(asn1_int_oct, oct, ASN1_OCTET_STRING) |
111 | 0 | } static_ASN1_SEQUENCE_END(asn1_int_oct) |
112 | 0 |
|
113 | 0 | DECLARE_ASN1_ITEM(asn1_int_oct) |
114 | 0 |
|
115 | 0 | int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data, |
116 | 0 | int len) |
117 | 0 | { |
118 | 0 | asn1_int_oct atmp; |
119 | 0 | ASN1_OCTET_STRING oct; |
120 | |
|
121 | 0 | atmp.num = num; |
122 | 0 | atmp.oct = &oct; |
123 | 0 | asn1_type_init_oct(&oct, data, len); |
124 | |
|
125 | 0 | if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_int_oct), &atmp, &a)) |
126 | 0 | return 1; |
127 | 0 | return 0; |
128 | 0 | } |
129 | | |
130 | | /* |
131 | | * This function decodes an int-octet sequence and copies the integer to 'num' |
132 | | * and the data of octet to 'data'. |
133 | | * If the length of 'data' > 'max_len', copies only the first 'max_len' |
134 | | * bytes, but returns the full length of 'oct'; this allows distinguishing |
135 | | * whether all the data was copied. |
136 | | */ |
137 | | int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num, |
138 | | unsigned char *data, int max_len) |
139 | 0 | { |
140 | 0 | asn1_int_oct *atmp = NULL; |
141 | 0 | int ret = -1; |
142 | |
|
143 | 0 | if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) { |
144 | 0 | goto err; |
145 | 0 | } |
146 | | |
147 | 0 | atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_int_oct), a); |
148 | |
|
149 | 0 | if (atmp == NULL) |
150 | 0 | goto err; |
151 | | |
152 | 0 | ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len); |
153 | |
|
154 | 0 | if (ret == -1) { |
155 | 0 | err: |
156 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG); |
157 | 0 | } |
158 | 0 | M_ASN1_free_of(atmp, asn1_int_oct); |
159 | 0 | return ret; |
160 | 0 | } |
161 | | |
162 | | typedef struct { |
163 | | ASN1_OCTET_STRING *oct; |
164 | | int32_t num; |
165 | | } asn1_oct_int; |
166 | | |
167 | | /* |
168 | | * Defined in RFC 5084 - |
169 | | * Section 2. "Content-Authenticated Encryption Algorithms" |
170 | | */ |
171 | | ASN1_SEQUENCE(asn1_oct_int) = { |
172 | | ASN1_SIMPLE(asn1_oct_int, oct, ASN1_OCTET_STRING), |
173 | | ASN1_EMBED(asn1_oct_int, num, INT32) |
174 | 0 | } static_ASN1_SEQUENCE_END(asn1_oct_int) |
175 | 0 |
|
176 | 0 | DECLARE_ASN1_ITEM(asn1_oct_int) |
177 | 0 |
|
178 | 0 | int ossl_asn1_type_set_octetstring_int(ASN1_TYPE *a, long num, |
179 | 0 | unsigned char *data, int len) |
180 | 0 | { |
181 | 0 | asn1_oct_int atmp; |
182 | 0 | ASN1_OCTET_STRING oct; |
183 | |
|
184 | 0 | atmp.num = num; |
185 | 0 | atmp.oct = &oct; |
186 | 0 | asn1_type_init_oct(&oct, data, len); |
187 | |
|
188 | 0 | if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_oct_int), &atmp, &a)) |
189 | 0 | return 1; |
190 | 0 | return 0; |
191 | 0 | } |
192 | | |
193 | | /* |
194 | | * This function decodes an octet-int sequence and copies the data of octet |
195 | | * to 'data' and the integer to 'num'. |
196 | | * If the length of 'data' > 'max_len', copies only the first 'max_len' |
197 | | * bytes, but returns the full length of 'oct'; this allows distinguishing |
198 | | * whether all the data was copied. |
199 | | */ |
200 | | int ossl_asn1_type_get_octetstring_int(const ASN1_TYPE *a, long *num, |
201 | | unsigned char *data, int max_len) |
202 | 0 | { |
203 | 0 | asn1_oct_int *atmp = NULL; |
204 | 0 | int ret = -1; |
205 | |
|
206 | 0 | if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) |
207 | 0 | goto err; |
208 | | |
209 | 0 | atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_oct_int), a); |
210 | |
|
211 | 0 | if (atmp == NULL) |
212 | 0 | goto err; |
213 | | |
214 | 0 | ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len); |
215 | |
|
216 | 0 | if (ret == -1) { |
217 | 0 | err: |
218 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG); |
219 | 0 | } |
220 | 0 | M_ASN1_free_of(atmp, asn1_oct_int); |
221 | 0 | return ret; |
222 | 0 | } |