Coverage Report

Created: 2023-06-07 07:13

/src/boringssl/crypto/asn1/a_object.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2
 * All rights reserved.
3
 *
4
 * This package is an SSL implementation written
5
 * by Eric Young (eay@cryptsoft.com).
6
 * The implementation was written so as to conform with Netscapes SSL.
7
 *
8
 * This library is free for commercial and non-commercial use as long as
9
 * the following conditions are aheared to.  The following conditions
10
 * apply to all code found in this distribution, be it the RC4, RSA,
11
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12
 * included with this distribution is covered by the same copyright terms
13
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14
 *
15
 * Copyright remains Eric Young's, and as such any Copyright notices in
16
 * the code are not to be removed.
17
 * If this package is used in a product, Eric Young should be given attribution
18
 * as the author of the parts of the library used.
19
 * This can be in the form of a textual message at program startup or
20
 * in documentation (online or textual) provided with the package.
21
 *
22
 * Redistribution and use in source and binary forms, with or without
23
 * modification, are permitted provided that the following conditions
24
 * are met:
25
 * 1. Redistributions of source code must retain the copyright
26
 *    notice, this list of conditions and the following disclaimer.
27
 * 2. Redistributions in binary form must reproduce the above copyright
28
 *    notice, this list of conditions and the following disclaimer in the
29
 *    documentation and/or other materials provided with the distribution.
30
 * 3. All advertising materials mentioning features or use of this software
31
 *    must display the following acknowledgement:
32
 *    "This product includes cryptographic software written by
33
 *     Eric Young (eay@cryptsoft.com)"
34
 *    The word 'cryptographic' can be left out if the rouines from the library
35
 *    being used are not cryptographic related :-).
36
 * 4. If you include any Windows specific code (or a derivative thereof) from
37
 *    the apps directory (application code) you must include an acknowledgement:
38
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50
 * SUCH DAMAGE.
51
 *
52
 * The licence and distribution terms for any publically available version or
53
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
54
 * copied and put under another distribution licence
55
 * [including the GNU Public Licence.] */
56
57
#include <openssl/asn1.h>
58
59
#include <limits.h>
60
#include <string.h>
61
62
#include <openssl/bytestring.h>
63
#include <openssl/err.h>
64
#include <openssl/mem.h>
65
#include <openssl/obj.h>
66
67
#include "../bytestring/internal.h"
68
#include "../internal.h"
69
#include "internal.h"
70
71
72
0
int i2d_ASN1_OBJECT(const ASN1_OBJECT *in, unsigned char **outp) {
73
0
  if (in == NULL) {
74
0
    OPENSSL_PUT_ERROR(ASN1, ERR_R_PASSED_NULL_PARAMETER);
75
0
    return -1;
76
0
  }
77
78
0
  if (in->length <= 0) {
79
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OBJECT);
80
0
    return -1;
81
0
  }
82
83
0
  CBB cbb, child;
84
0
  if (!CBB_init(&cbb, (size_t)in->length + 2) ||
85
0
      !CBB_add_asn1(&cbb, &child, CBS_ASN1_OBJECT) ||
86
0
      !CBB_add_bytes(&child, in->data, in->length)) {
87
0
    CBB_cleanup(&cbb);
88
0
    return -1;
89
0
  }
90
91
0
  return CBB_finish_i2d(&cbb, outp);
92
0
}
93
94
43.3k
int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a) {
95
43.3k
  return OBJ_obj2txt(buf, buf_len, a, 0);
96
43.3k
}
97
98
27.8k
static int write_str(BIO *bp, const char *str) {
99
27.8k
  size_t len = strlen(str);
100
27.8k
  if (len > INT_MAX) {
101
0
    OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW);
102
0
    return -1;
103
0
  }
104
27.8k
  return BIO_write(bp, str, (int)len) == (int)len ? (int)len : -1;
105
27.8k
}
106
107
27.8k
int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a) {
108
27.8k
  if (a == NULL || a->data == NULL) {
109
0
    return write_str(bp, "NULL");
110
0
  }
111
112
27.8k
  char buf[80], *allocated = NULL;
113
27.8k
  const char *str = buf;
114
27.8k
  int len = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
115
27.8k
  if (len > (int)sizeof(buf) - 1) {
116
    // The input was truncated. Allocate a buffer that fits.
117
79
    allocated = OPENSSL_malloc(len + 1);
118
79
    if (allocated == NULL) {
119
0
      return -1;
120
0
    }
121
79
    len = i2t_ASN1_OBJECT(allocated, len + 1, a);
122
79
    str = allocated;
123
79
  }
124
27.8k
  if (len <= 0) {
125
61
    str = "<INVALID>";
126
61
  }
127
128
27.8k
  int ret = write_str(bp, str);
129
27.8k
  OPENSSL_free(allocated);
130
27.8k
  return ret;
131
27.8k
}
132
133
ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **out, const unsigned char **inp,
134
0
                             long len) {
135
0
  if (len < 0) {
136
0
    return NULL;
137
0
  }
138
139
0
  CBS cbs, child;
140
0
  CBS_init(&cbs, *inp, (size_t)len);
141
0
  if (!CBS_get_asn1(&cbs, &child, CBS_ASN1_OBJECT)) {
142
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
143
0
    return NULL;
144
0
  }
145
146
0
  const uint8_t *contents = CBS_data(&child);
147
0
  ASN1_OBJECT *ret = c2i_ASN1_OBJECT(out, &contents, CBS_len(&child));
148
0
  if (ret != NULL) {
149
    // |c2i_ASN1_OBJECT| should have consumed the entire input.
150
0
    assert(CBS_data(&cbs) == contents);
151
0
    *inp = CBS_data(&cbs);
152
0
  }
153
0
  return ret;
154
0
}
155
156
ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **out, const unsigned char **inp,
157
1.58M
                             long len) {
158
1.58M
  if (len < 0) {
159
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
160
0
    return NULL;
161
0
  }
162
163
1.58M
  CBS cbs;
164
1.58M
  CBS_init(&cbs, *inp, (size_t)len);
165
1.58M
  if (!CBS_is_valid_asn1_oid(&cbs)) {
166
195
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
167
195
    return NULL;
168
195
  }
169
170
1.58M
  ASN1_OBJECT *ret = ASN1_OBJECT_create(NID_undef, *inp, (size_t)len,
171
                                        /*sn=*/NULL, /*ln=*/NULL);
172
1.58M
  if (ret == NULL) {
173
0
    return NULL;
174
0
  }
175
176
1.58M
  if (out != NULL) {
177
1.58M
    ASN1_OBJECT_free(*out);
178
1.58M
    *out = ret;
179
1.58M
  }
180
1.58M
  *inp += len;  // All bytes were consumed.
181
1.58M
  return ret;
182
1.58M
}
183
184
2.68M
ASN1_OBJECT *ASN1_OBJECT_new(void) {
185
2.68M
  ASN1_OBJECT *ret;
186
187
2.68M
  ret = (ASN1_OBJECT *)OPENSSL_malloc(sizeof(ASN1_OBJECT));
188
2.68M
  if (ret == NULL) {
189
0
    return NULL;
190
0
  }
191
2.68M
  ret->length = 0;
192
2.68M
  ret->data = NULL;
193
2.68M
  ret->nid = 0;
194
2.68M
  ret->sn = NULL;
195
2.68M
  ret->ln = NULL;
196
2.68M
  ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
197
2.68M
  return ret;
198
2.68M
}
199
200
4.59M
void ASN1_OBJECT_free(ASN1_OBJECT *a) {
201
4.59M
  if (a == NULL) {
202
38.9k
    return;
203
38.9k
  }
204
4.55M
  if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
205
2.68M
    OPENSSL_free((void *)a->sn);
206
2.68M
    OPENSSL_free((void *)a->ln);
207
2.68M
    a->sn = a->ln = NULL;
208
2.68M
  }
209
4.55M
  if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
210
2.68M
    OPENSSL_free((void *)a->data);
211
2.68M
    a->data = NULL;
212
2.68M
    a->length = 0;
213
2.68M
  }
214
4.55M
  if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC) {
215
2.68M
    OPENSSL_free(a);
216
2.68M
  }
217
4.55M
}
218
219
ASN1_OBJECT *ASN1_OBJECT_create(int nid, const unsigned char *data, size_t len,
220
1.74M
                                const char *sn, const char *ln) {
221
1.74M
  if (len > INT_MAX) {
222
0
    OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
223
0
    return NULL;
224
0
  }
225
226
1.74M
  ASN1_OBJECT o;
227
1.74M
  o.sn = sn;
228
1.74M
  o.ln = ln;
229
1.74M
  o.data = data;
230
1.74M
  o.nid = nid;
231
1.74M
  o.length = (int)len;
232
1.74M
  o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
233
1.74M
            ASN1_OBJECT_FLAG_DYNAMIC_DATA;
234
1.74M
  return OBJ_dup(&o);
235
1.74M
}