]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/evp_asn1.c
Add CMS AuthEnvelopedData with AES-GCM support
[thirdparty/openssl.git] / crypto / asn1 / evp_asn1.c
CommitLineData
4f22f405 1/*
924663c3 2 * Copyright 1995-2020 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 16int 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
6b691a5c 30/* int max_len: for returned value */
08275a29 31int ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len)
0f113f3e
MC
32{
33 int ret, num;
17ebf85a 34 const unsigned char *p;
0f113f3e
MC
35
36 if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) {
37 ASN1err(ASN1_F_ASN1_TYPE_GET_OCTETSTRING, ASN1_R_DATA_IS_WRONG);
26a7d938 38 return -1;
0f113f3e 39 }
17ebf85a 40 p = ASN1_STRING_get0_data(a->value.octet_string);
f422a514 41 ret = ASN1_STRING_length(a->value.octet_string);
0f113f3e
MC
42 if (ret < max_len)
43 num = ret;
44 else
45 num = max_len;
46 memcpy(data, p, num);
26a7d938 47 return ret;
0f113f3e 48}
58964a49 49
924663c3
JZ
50static 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
59static 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
b9395187 78typedef struct {
6a32a3c0 79 int32_t num;
b9395187
DSH
80 ASN1_OCTET_STRING *oct;
81} asn1_int_oct;
0f113f3e 82
b9395187 83ASN1_SEQUENCE(asn1_int_oct) = {
9612e157 84 ASN1_EMBED(asn1_int_oct, num, INT32),
b9395187 85 ASN1_SIMPLE(asn1_int_oct, oct, ASN1_OCTET_STRING)
df2ee0e2 86} static_ASN1_SEQUENCE_END(asn1_int_oct)
0f113f3e 87
b9395187 88DECLARE_ASN1_ITEM(asn1_int_oct)
0f113f3e 89
b9395187
DSH
90int 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;
924663c3 98 asn1_type_init_oct(&oct, data, len);
b9395187
DSH
99
100 if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_int_oct), &atmp, &a))
101 return 1;
102 return 0;
0f113f3e
MC
103}
104
08275a29 105int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num,
0f113f3e
MC
106 unsigned char *data, int max_len)
107{
b9395187 108 asn1_int_oct *atmp = NULL;
924663c3 109 int ret = -1;
0f113f3e
MC
110
111 if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) {
112 goto err;
113 }
0f113f3e 114
b9395187 115 atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_int_oct), a);
0f113f3e 116
b9395187 117 if (atmp == NULL)
0f113f3e
MC
118 goto err;
119
924663c3 120 ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len);
0f113f3e 121
b9395187 122 if (ret == -1) {
0f113f3e
MC
123 err:
124 ASN1err(ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING, ASN1_R_DATA_IS_WRONG);
125 }
b9395187
DSH
126 M_ASN1_free_of(atmp, asn1_int_oct);
127 return ret;
0f113f3e 128}
924663c3
JZ
129
130typedef 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 */
139ASN1_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
144DECLARE_ASN1_ITEM(asn1_oct_int)
145
146int asn1_type_set_octetstring_int(ASN1_TYPE *a, long num, unsigned char *data,
147 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
161int 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 ASN1err(ASN1_F_ASN1_TYPE_GET_OCTETSTRING_INT, ASN1_R_DATA_IS_WRONG);
180 }
181 M_ASN1_free_of(atmp, asn1_oct_int);
182 return ret;
183}