/src/openssl/crypto/asn1/evp_asn1.c
Line  | Count  | Source (jump to first uncovered line)  | 
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  |  |  | 
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  |     const unsigned char *p;  | 
38  |  | 
  | 
39  | 0  |     if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) { | 
40  | 0  |         ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);  | 
41  | 0  |         return -1;  | 
42  | 0  |     }  | 
43  | 0  |     p = ASN1_STRING_get0_data(a->value.octet_string);  | 
44  | 0  |     ret = ASN1_STRING_length(a->value.octet_string);  | 
45  | 0  |     if (ret < max_len)  | 
46  | 0  |         num = ret;  | 
47  | 0  |     else  | 
48  | 0  |         num = max_len;  | 
49  | 0  |     if (num > 0 && data != NULL)  | 
50  | 0  |         memcpy(data, p, num);  | 
51  | 0  |     return ret;  | 
52  | 0  | }  | 
53  |  |  | 
54  |  | static ossl_inline void asn1_type_init_oct(ASN1_OCTET_STRING *oct,  | 
55  |  |                                            unsigned char *data, int len)  | 
56  | 0  | { | 
57  | 0  |     oct->data = data;  | 
58  | 0  |     oct->type = V_ASN1_OCTET_STRING;  | 
59  | 0  |     oct->length = len;  | 
60  | 0  |     oct->flags = 0;  | 
61  | 0  | }  | 
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  | 0  | { | 
66  | 0  |     int ret = ASN1_STRING_length(oct), n;  | 
67  |  | 
  | 
68  | 0  |     if (num != NULL)  | 
69  | 0  |         *num = anum;  | 
70  |  | 
  | 
71  | 0  |     if (max_len > ret)  | 
72  | 0  |         n = ret;  | 
73  | 0  |     else  | 
74  | 0  |         n = max_len;  | 
75  |  | 
  | 
76  | 0  |     if (data != NULL)  | 
77  | 0  |         memcpy(data, ASN1_STRING_get0_data(oct), n);  | 
78  |  | 
  | 
79  | 0  |     return ret;  | 
80  | 0  | }  | 
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  | 0  | { | 
97  | 0  |     asn1_int_oct atmp;  | 
98  | 0  |     ASN1_OCTET_STRING oct;  | 
99  |  | 
  | 
100  | 0  |     atmp.num = num;  | 
101  | 0  |     atmp.oct = &oct;  | 
102  | 0  |     asn1_type_init_oct(&oct, data, len);  | 
103  |  | 
  | 
104  | 0  |     if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_int_oct), &atmp, &a))  | 
105  | 0  |         return 1;  | 
106  | 0  |     return 0;  | 
107  | 0  | }  | 
108  |  |  | 
109  |  | int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num,  | 
110  |  |                                   unsigned char *data, int max_len)  | 
111  | 0  | { | 
112  | 0  |     asn1_int_oct *atmp = NULL;  | 
113  | 0  |     int ret = -1;  | 
114  |  | 
  | 
115  | 0  |     if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) { | 
116  | 0  |         goto err;  | 
117  | 0  |     }  | 
118  |  |  | 
119  | 0  |     atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_int_oct), a);  | 
120  |  | 
  | 
121  | 0  |     if (atmp == NULL)  | 
122  | 0  |         goto err;  | 
123  |  |  | 
124  | 0  |     ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len);  | 
125  |  | 
  | 
126  | 0  |     if (ret == -1) { | 
127  | 0  |  err:  | 
128  | 0  |         ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);  | 
129  | 0  |     }  | 
130  | 0  |     M_ASN1_free_of(atmp, asn1_int_oct);  | 
131  | 0  |     return ret;  | 
132  | 0  | }  | 
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  | 0  | { | 
153  | 0  |     asn1_oct_int atmp;  | 
154  | 0  |     ASN1_OCTET_STRING oct;  | 
155  |  | 
  | 
156  | 0  |     atmp.num = num;  | 
157  | 0  |     atmp.oct = &oct;  | 
158  | 0  |     asn1_type_init_oct(&oct, data, len);  | 
159  |  | 
  | 
160  | 0  |     if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_oct_int), &atmp, &a))  | 
161  | 0  |         return 1;  | 
162  | 0  |     return 0;  | 
163  | 0  | }  | 
164  |  |  | 
165  |  | int ossl_asn1_type_get_octetstring_int(const ASN1_TYPE *a, long *num,  | 
166  |  |                                        unsigned char *data, int max_len)  | 
167  | 0  | { | 
168  | 0  |     asn1_oct_int *atmp = NULL;  | 
169  | 0  |     int ret = -1;  | 
170  |  | 
  | 
171  | 0  |     if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL))  | 
172  | 0  |         goto err;  | 
173  |  |  | 
174  | 0  |     atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_oct_int), a);  | 
175  |  | 
  | 
176  | 0  |     if (atmp == NULL)  | 
177  | 0  |         goto err;  | 
178  |  |  | 
179  | 0  |     ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len);  | 
180  |  | 
  | 
181  | 0  |     if (ret == -1) { | 
182  | 0  |  err:  | 
183  | 0  |         ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);  | 
184  | 0  |     }  | 
185  | 0  |     M_ASN1_free_of(atmp, asn1_oct_int);  | 
186  | 0  |     return ret;  | 
187  | 0  | }  |