Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/asn1/evp_asn1.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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
128
{
36
128
    int ret, num;
37
128
    const unsigned char *p;
38
39
128
    if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) {
40
14
        ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
41
14
        return -1;
42
14
    }
43
114
    p = ASN1_STRING_get0_data(a->value.octet_string);
44
114
    ret = ASN1_STRING_length(a->value.octet_string);
45
114
    if (ret < max_len)
46
0
        num = ret;
47
114
    else
48
114
        num = max_len;
49
114
    if (num > 0 && data != NULL)
50
110
        memcpy(data, p, num);
51
114
    return ret;
52
128
}
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
/*
64
 * This function copies 'anum' to 'num' and the data of 'oct' to 'data'.
65
 * If the length of 'data' > 'max_len', copies only the first 'max_len'
66
 * bytes, but returns the full length of 'oct'; this allows distinguishing
67
 * whether all the data was copied.
68
 */
69
static int asn1_type_get_int_oct(ASN1_OCTET_STRING *oct, int32_t anum,
70
    long *num, unsigned char *data, int max_len)
71
0
{
72
0
    int ret = ASN1_STRING_length(oct), n;
73
74
0
    if (num != NULL)
75
0
        *num = anum;
76
77
0
    if (max_len > ret)
78
0
        n = ret;
79
0
    else
80
0
        n = max_len;
81
82
0
    if (data != NULL)
83
0
        memcpy(data, ASN1_STRING_get0_data(oct), n);
84
85
0
    return ret;
86
0
}
87
88
typedef struct {
89
    int32_t num;
90
    ASN1_OCTET_STRING *oct;
91
} asn1_int_oct;
92
93
ASN1_SEQUENCE(asn1_int_oct) = {
94
    ASN1_EMBED(asn1_int_oct, num, INT32),
95
    ASN1_SIMPLE(asn1_int_oct, oct, ASN1_OCTET_STRING)
96
0
} static_ASN1_SEQUENCE_END(asn1_int_oct)
97
0
98
0
    DECLARE_ASN1_ITEM(asn1_int_oct)
99
0
100
0
int ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data,
101
0
    int len)
102
0
{
103
0
    asn1_int_oct atmp;
104
0
    ASN1_OCTET_STRING oct;
105
106
0
    atmp.num = num;
107
0
    atmp.oct = &oct;
108
0
    asn1_type_init_oct(&oct, data, len);
109
110
0
    if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_int_oct), &atmp, &a))
111
0
        return 1;
112
0
    return 0;
113
0
}
114
115
/*
116
 * This function decodes an int-octet sequence and copies the integer to 'num'
117
 * and the data of octet to 'data'.
118
 * If the length of 'data' > 'max_len', copies only the first 'max_len'
119
 * bytes, but returns the full length of 'oct'; this allows distinguishing
120
 * whether all the data was copied.
121
 */
122
int ASN1_TYPE_get_int_octetstring(const ASN1_TYPE *a, long *num,
123
    unsigned char *data, int max_len)
124
0
{
125
0
    asn1_int_oct *atmp = NULL;
126
0
    int ret = -1;
127
128
0
    if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) {
129
0
        goto err;
130
0
    }
131
132
0
    atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_int_oct), a);
133
134
0
    if (atmp == NULL)
135
0
        goto err;
136
137
0
    ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len);
138
139
0
    if (ret == -1) {
140
0
    err:
141
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
142
0
    }
143
0
    M_ASN1_free_of(atmp, asn1_int_oct);
144
0
    return ret;
145
0
}
146
147
typedef struct {
148
    ASN1_OCTET_STRING *oct;
149
    int32_t num;
150
} asn1_oct_int;
151
152
/*
153
 * Defined in RFC 5084 -
154
 * Section 2. "Content-Authenticated Encryption Algorithms"
155
 */
156
ASN1_SEQUENCE(asn1_oct_int) = {
157
    ASN1_SIMPLE(asn1_oct_int, oct, ASN1_OCTET_STRING),
158
    ASN1_EMBED(asn1_oct_int, num, INT32)
159
0
} static_ASN1_SEQUENCE_END(asn1_oct_int)
160
0
161
0
    DECLARE_ASN1_ITEM(asn1_oct_int)
162
0
163
0
int ossl_asn1_type_set_octetstring_int(ASN1_TYPE *a, long num,
164
0
    unsigned char *data, int len)
165
0
{
166
0
    asn1_oct_int atmp;
167
0
    ASN1_OCTET_STRING oct;
168
169
0
    atmp.num = num;
170
0
    atmp.oct = &oct;
171
0
    asn1_type_init_oct(&oct, data, len);
172
173
0
    if (ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(asn1_oct_int), &atmp, &a))
174
0
        return 1;
175
0
    return 0;
176
0
}
177
178
/*
179
 * This function decodes an octet-int sequence and copies the data of octet
180
 * to 'data' and the integer to 'num'.
181
 * If the length of 'data' > 'max_len', copies only the first 'max_len'
182
 * bytes, but returns the full length of 'oct'; this allows distinguishing
183
 * whether all the data was copied.
184
 */
185
int ossl_asn1_type_get_octetstring_int(const ASN1_TYPE *a, long *num,
186
    unsigned char *data, int max_len)
187
0
{
188
0
    asn1_oct_int *atmp = NULL;
189
0
    int ret = -1;
190
191
0
    if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL))
192
0
        goto err;
193
194
0
    atmp = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(asn1_oct_int), a);
195
196
0
    if (atmp == NULL)
197
0
        goto err;
198
199
0
    ret = asn1_type_get_int_oct(atmp->oct, atmp->num, num, data, max_len);
200
201
0
    if (ret == -1) {
202
0
    err:
203
0
        ERR_raise(ERR_LIB_ASN1, ASN1_R_DATA_IS_WRONG);
204
0
    }
205
0
    M_ASN1_free_of(atmp, asn1_oct_int);
206
0
    return ret;
207
0
}