]>
Commit | Line | Data |
---|---|---|
4f22f405 | 1 | /* |
3c2bdd7d | 2 | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
58964a49 | 3 | * |
365a2d99 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
4f22f405 RS |
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 | |
58964a49 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
ec577822 | 12 | #include <openssl/asn1.h> |
b9395187 | 13 | #include <openssl/asn1t.h> |
924663c3 | 14 | #include "crypto/asn1.h" |
58964a49 | 15 | |
6343829a | 16 | int ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len) |
0f113f3e MC |
17 | { |
18 | ASN1_STRING *os; | |
19 | ||
f422a514 | 20 | if ((os = ASN1_OCTET_STRING_new()) == NULL) |
26a7d938 | 21 | return 0; |
f422a514 DSH |
22 | if (!ASN1_OCTET_STRING_set(os, data, len)) { |
23 | ASN1_OCTET_STRING_free(os); | |
0f113f3e MC |
24 | return 0; |
25 | } | |
26 | ASN1_TYPE_set(a, V_ASN1_OCTET_STRING, os); | |
208fb891 | 27 | return 1; |
0f113f3e | 28 | } |
58964a49 | 29 | |
0760d132 PD |
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 | */ | |
08275a29 | 34 | int ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len) |
0f113f3e MC |
35 | { |
36 | int ret, num; | |
17ebf85a | 37 | const unsigned char *p; |
0f113f3e MC |
38 | |
39 | if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) { | |
9311d0c4 | 40 | ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG); |
26a7d938 | 41 | return -1; |
0f113f3e | 42 | } |
17ebf85a | 43 | p = ASN1_STRING_get0_data(a->value.octet_string); |
f422a514 | 44 | ret = ASN1_STRING_length(a->value.octet_string); |
0f113f3e MC |
45 | if (ret < max_len) |
46 | num = ret; | |
47 | else | |
48 | num = max_len; | |
0760d132 PD |
49 | if (num > 0 && data != NULL) |
50 | memcpy(data, p, num); | |
26a7d938 | 51 | return ret; |
0f113f3e | 52 | } |
58964a49 | 53 | |
924663c3 JZ |
54 | static ossl_inline void asn1_type_init_oct(ASN1_OCTET_STRING *oct, |
55 | unsigned char *data, int len) | |
56 | { | |
57 | oct->data = data; | |
58 | oct->type = V_ASN1_OCTET_STRING; | |
59 | oct->length = len; | |
60 | oct->flags = 0; | |
61 | } | |
62 | ||
63 | static int asn1_type_get_int_oct(ASN1_OCTET_STRING *oct, int32_t anum, | |
64 | long *num, unsigned char *data, int max_len) | |
65 | { | |
66 | int ret = ASN1_STRING_length(oct), n; | |
67 | ||
68 | if (num != NULL) | |
69 | *num = anum; | |
70 | ||
71 | if (max_len > ret) | |
72 | n = ret; | |
73 | else | |
74 | n = max_len; | |
75 | ||
76 | if (data != NULL) | |
77 | memcpy(data, ASN1_STRING_get0_data(oct), n); | |
78 | ||
79 | return ret; | |
80 | } | |
81 | ||
b9395187 | 82 | typedef struct { |
6a32a3c0 | 83 | int32_t num; |
b9395187 DSH |
84 | ASN1_OCTET_STRING *oct; |
85 | } asn1_int_oct; | |
0f113f3e | 86 | |
b9395187 | 87 | ASN1_SEQUENCE(asn1_int_oct) = { |
9612e157 | 88 | ASN1_EMBED(asn1_int_oct, num, INT32), |
b9395187 | 89 | ASN1_SIMPLE(asn1_int_oct, oct, ASN1_OCTET_STRING) |
df2ee0e2 | 90 | } static_ASN1_SEQUENCE_END(asn1_int_oct) |
0f113f3e | 91 | |
b9395187 | 92 | DECLARE_ASN1_ITEM(asn1_int_oct) |
0f113f3e | 93 | |
b9395187 DSH |
94 | int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data, |
95 | int len) | |
96 | { | |
97 | asn1_int_oct atmp; | |
98 | ASN1_OCTET_STRING oct; | |
99 | ||
100 | atmp.num = num; | |
101 | atmp.oct = &oct; | |
924663c3 | 102 | asn1_type_init_oct(&oct, data, len); |
b9395187 DSH |
103 | |
104 | if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_int_oct), &atmp, &a)) | |
105 | return 1; | |
106 | return 0; | |
0f113f3e MC |
107 | } |
108 | ||
08275a29 | 109 | int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num, |
0f113f3e MC |
110 | unsigned char *data, int max_len) |
111 | { | |
b9395187 | 112 | asn1_int_oct *atmp = NULL; |
924663c3 | 113 | int ret = -1; |
0f113f3e MC |
114 | |
115 | if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) { | |
116 | goto err; | |
117 | } | |
0f113f3e | 118 | |
b9395187 | 119 | atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_int_oct), a); |
0f113f3e | 120 | |
b9395187 | 121 | if (atmp == NULL) |
0f113f3e MC |
122 | goto err; |
123 | ||
924663c3 | 124 | ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len); |
0f113f3e | 125 | |
b9395187 | 126 | if (ret == -1) { |
0f113f3e | 127 | err: |
9311d0c4 | 128 | ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG); |
0f113f3e | 129 | } |
b9395187 DSH |
130 | M_ASN1_free_of(atmp, asn1_int_oct); |
131 | return ret; | |
0f113f3e | 132 | } |
924663c3 JZ |
133 | |
134 | typedef struct { | |
135 | ASN1_OCTET_STRING *oct; | |
136 | int32_t num; | |
137 | } asn1_oct_int; | |
138 | ||
139 | /* | |
140 | * Defined in RFC 5084 - | |
141 | * Section 2. "Content-Authenticated Encryption Algorithms" | |
142 | */ | |
143 | ASN1_SEQUENCE(asn1_oct_int) = { | |
144 | ASN1_SIMPLE(asn1_oct_int, oct, ASN1_OCTET_STRING), | |
145 | ASN1_EMBED(asn1_oct_int, num, INT32) | |
146 | } static_ASN1_SEQUENCE_END(asn1_oct_int) | |
147 | ||
148 | DECLARE_ASN1_ITEM(asn1_oct_int) | |
149 | ||
adf7e6d1 SL |
150 | int ossl_asn1_type_set_octetstring_int(ASN1_TYPE *a, long num, |
151 | unsigned char *data, int len) | |
924663c3 JZ |
152 | { |
153 | asn1_oct_int atmp; | |
154 | ASN1_OCTET_STRING oct; | |
155 | ||
156 | atmp.num = num; | |
157 | atmp.oct = &oct; | |
158 | asn1_type_init_oct(&oct, data, len); | |
159 | ||
160 | if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_oct_int), &atmp, &a)) | |
161 | return 1; | |
162 | return 0; | |
163 | } | |
164 | ||
adf7e6d1 SL |
165 | int ossl_asn1_type_get_octetstring_int(const ASN1_TYPE *a, long *num, |
166 | unsigned char *data, int max_len) | |
924663c3 JZ |
167 | { |
168 | asn1_oct_int *atmp = NULL; | |
169 | int ret = -1; | |
170 | ||
171 | if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) | |
172 | goto err; | |
173 | ||
174 | atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_oct_int), a); | |
175 | ||
176 | if (atmp == NULL) | |
177 | goto err; | |
178 | ||
179 | ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len); | |
180 | ||
181 | if (ret == -1) { | |
182 | err: | |
9311d0c4 | 183 | ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG); |
924663c3 JZ |
184 | } |
185 | M_ASN1_free_of(atmp, asn1_oct_int); | |
186 | return ret; | |
187 | } |