]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/asn1/evp_asn1.c
Add ossl_asn1 symbols
[thirdparty/openssl.git] / crypto / asn1 / evp_asn1.c
1 /*
2 * Copyright 1995-2020 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 {
18 ASN1_STRING *os;
19
20 if ((os = ASN1_OCTET_STRING_new()) == NULL)
21 return 0;
22 if (!ASN1_OCTET_STRING_set(os, data, len)) {
23 ASN1_OCTET_STRING_free(os);
24 return 0;
25 }
26 ASN1_TYPE_set(a, V_ASN1_OCTET_STRING, os);
27 return 1;
28 }
29
30 /* int max_len: for returned value */
31 int ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len)
32 {
33 int ret, num;
34 const unsigned char *p;
35
36 if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) {
37 ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
38 return -1;
39 }
40 p = ASN1_STRING_get0_data(a->value.octet_string);
41 ret = ASN1_STRING_length(a->value.octet_string);
42 if (ret < max_len)
43 num = ret;
44 else
45 num = max_len;
46 memcpy(data, p, num);
47 return ret;
48 }
49
50 static ossl_inline void asn1_type_init_oct(ASN1_OCTET_STRING *oct,
51 unsigned char *data, int len)
52 {
53 oct->data = data;
54 oct->type = V_ASN1_OCTET_STRING;
55 oct->length = len;
56 oct->flags = 0;
57 }
58
59 static int asn1_type_get_int_oct(ASN1_OCTET_STRING *oct, int32_t anum,
60 long *num, unsigned char *data, int max_len)
61 {
62 int ret = ASN1_STRING_length(oct), n;
63
64 if (num != NULL)
65 *num = anum;
66
67 if (max_len > ret)
68 n = ret;
69 else
70 n = max_len;
71
72 if (data != NULL)
73 memcpy(data, ASN1_STRING_get0_data(oct), n);
74
75 return ret;
76 }
77
78 typedef struct {
79 int32_t num;
80 ASN1_OCTET_STRING *oct;
81 } asn1_int_oct;
82
83 ASN1_SEQUENCE(asn1_int_oct) = {
84 ASN1_EMBED(asn1_int_oct, num, INT32),
85 ASN1_SIMPLE(asn1_int_oct, oct, ASN1_OCTET_STRING)
86 } static_ASN1_SEQUENCE_END(asn1_int_oct)
87
88 DECLARE_ASN1_ITEM(asn1_int_oct)
89
90 int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data,
91 int len)
92 {
93 asn1_int_oct atmp;
94 ASN1_OCTET_STRING oct;
95
96 atmp.num = num;
97 atmp.oct = &oct;
98 asn1_type_init_oct(&oct, data, len);
99
100 if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_int_oct), &atmp, &a))
101 return 1;
102 return 0;
103 }
104
105 int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num,
106 unsigned char *data, int max_len)
107 {
108 asn1_int_oct *atmp = NULL;
109 int ret = -1;
110
111 if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) {
112 goto err;
113 }
114
115 atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_int_oct), a);
116
117 if (atmp == NULL)
118 goto err;
119
120 ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len);
121
122 if (ret == -1) {
123 err:
124 ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
125 }
126 M_ASN1_free_of(atmp, asn1_int_oct);
127 return ret;
128 }
129
130 typedef struct {
131 ASN1_OCTET_STRING *oct;
132 int32_t num;
133 } asn1_oct_int;
134
135 /*
136 * Defined in RFC 5084 -
137 * Section 2. "Content-Authenticated Encryption Algorithms"
138 */
139 ASN1_SEQUENCE(asn1_oct_int) = {
140 ASN1_SIMPLE(asn1_oct_int, oct, ASN1_OCTET_STRING),
141 ASN1_EMBED(asn1_oct_int, num, INT32)
142 } static_ASN1_SEQUENCE_END(asn1_oct_int)
143
144 DECLARE_ASN1_ITEM(asn1_oct_int)
145
146 int ossl_asn1_type_set_octetstring_int(ASN1_TYPE *a, long num,
147 unsigned char *data, int len)
148 {
149 asn1_oct_int atmp;
150 ASN1_OCTET_STRING oct;
151
152 atmp.num = num;
153 atmp.oct = &oct;
154 asn1_type_init_oct(&oct, data, len);
155
156 if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_oct_int), &atmp, &a))
157 return 1;
158 return 0;
159 }
160
161 int ossl_asn1_type_get_octetstring_int(const ASN1_TYPE *a, long *num,
162 unsigned char *data, int max_len)
163 {
164 asn1_oct_int *atmp = NULL;
165 int ret = -1;
166
167 if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL))
168 goto err;
169
170 atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_oct_int), a);
171
172 if (atmp == NULL)
173 goto err;
174
175 ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len);
176
177 if (ret == -1) {
178 err:
179 ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
180 }
181 M_ASN1_free_of(atmp, asn1_oct_int);
182 return ret;
183 }