1 /*
2  * Copyright 1995-2021 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 
ASN1_TYPE_set_octetstring(ASN1_TYPE * a,unsigned char * data,int len)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  * if passing NULL in data, nothing is copied but the necessary length
32  * for it is returned.
33  */
ASN1_TYPE_get_octetstring(const ASN1_TYPE * a,unsigned char * data,int max_len)34 int ASN1_TYPE_get_octetstring(const ASN1_TYPE *a, unsigned char *data, int max_len)
35 {
36     int ret, num;
37     const unsigned char *p;
38 
39     if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) {
40         ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
41         return -1;
42     }
43     p = ASN1_STRING_get0_data(a->value.octet_string);
44     ret = ASN1_STRING_length(a->value.octet_string);
45     if (ret < max_len)
46         num = ret;
47     else
48         num = max_len;
49     if (num > 0 && data != NULL)
50         memcpy(data, p, num);
51     return ret;
52 }
53 
asn1_type_init_oct(ASN1_OCTET_STRING * oct,unsigned char * data,int len)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 
asn1_type_get_int_oct(ASN1_OCTET_STRING * oct,int32_t anum,long * num,unsigned char * data,int max_len)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 
82 typedef struct {
83     int32_t num;
84     ASN1_OCTET_STRING *oct;
85 } asn1_int_oct;
86 
87 ASN1_SEQUENCE(asn1_int_oct) = {
88         ASN1_EMBED(asn1_int_oct, num, INT32),
89         ASN1_SIMPLE(asn1_int_oct, oct, ASN1_OCTET_STRING)
90 } static_ASN1_SEQUENCE_END(asn1_int_oct)
91 
92 DECLARE_ASN1_ITEM(asn1_int_oct)
93 
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;
102     asn1_type_init_oct(&oct, data, len);
103 
104     if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_int_oct), &atmp, &a))
105         return 1;
106     return 0;
107 }
108 
ASN1_TYPE_get_int_octetstring(const ASN1_TYPE * a,long * num,unsigned char * data,int max_len)109 int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num,
110                                   unsigned char *data, int max_len)
111 {
112     asn1_int_oct *atmp = NULL;
113     int ret = -1;
114 
115     if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) {
116         goto err;
117     }
118 
119     atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_int_oct), a);
120 
121     if (atmp == NULL)
122         goto err;
123 
124     ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len);
125 
126     if (ret == -1) {
127  err:
128         ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
129     }
130     M_ASN1_free_of(atmp, asn1_int_oct);
131     return ret;
132 }
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 
150 int ossl_asn1_type_set_octetstring_int(ASN1_TYPE *a, long num,
151                                        unsigned char *data, int len)
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 
ossl_asn1_type_get_octetstring_int(const ASN1_TYPE * a,long * num,unsigned char * data,int max_len)165 int ossl_asn1_type_get_octetstring_int(const ASN1_TYPE *a, long *num,
166                                        unsigned char *data, int max_len)
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:
183         ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
184     }
185     M_ASN1_free_of(atmp, asn1_oct_int);
186     return ret;
187 }
188