a_object.c
Go to the documentation of this file.
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/err.h>
63 #include <openssl/mem.h>
64 #include <openssl/obj.h>
65 
66 #include "../internal.h"
67 #include "internal.h"
68 
69 
70 int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
71 {
72  if (a == NULL) {
74  return -1;
75  }
76 
77  if (a->length == 0) {
79  return -1;
80  }
81 
82  int objsize = ASN1_object_size(0, a->length, V_ASN1_OBJECT);
83  if (pp == NULL || objsize == -1) {
84  return objsize;
85  }
86 
87  unsigned char *p, *allocated = NULL;
88  if (*pp == NULL) {
89  if ((p = allocated = OPENSSL_malloc(objsize)) == NULL) {
91  return -1;
92  }
93  } else {
94  p = *pp;
95  }
96 
98  OPENSSL_memcpy(p, a->data, a->length);
99 
100  /*
101  * If a new buffer was allocated, just return it back.
102  * If not, return the incremented buffer pointer.
103  */
104  *pp = allocated != NULL ? allocated : p + a->length;
105  return objsize;
106 }
107 
108 int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
109 {
110  return OBJ_obj2txt(buf, buf_len, a, 0);
111 }
112 
113 static int write_str(BIO *bp, const char *str)
114 {
115  int len = strlen(str);
116  return BIO_write(bp, str, len) == len ? len : -1;
117 }
118 
120 {
121  if (a == NULL || a->data == NULL) {
122  return write_str(bp, "NULL");
123  }
124 
125  char buf[80], *allocated = NULL;
126  const char *str = buf;
127  int len = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
128  if (len > (int)sizeof(buf) - 1) {
129  /* The input was truncated. Allocate a buffer that fits. */
130  allocated = OPENSSL_malloc(len + 1);
131  if (allocated == NULL) {
132  return -1;
133  }
134  len = i2t_ASN1_OBJECT(allocated, len + 1, a);
135  str = allocated;
136  }
137  if (len <= 0) {
138  str = "<INVALID>";
139  }
140 
141  int ret = write_str(bp, str);
142  OPENSSL_free(allocated);
143  return ret;
144 }
145 
146 ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
147  long length)
148 {
149  long len;
150  int tag, xclass;
151  const unsigned char *p = *pp;
152  int inf = ASN1_get_object(&p, &len, &tag, &xclass, length);
153  if (inf & 0x80) {
155  return NULL;
156  }
157 
158  if (inf & V_ASN1_CONSTRUCTED) {
160  return NULL;
161  }
162 
163  if (tag != V_ASN1_OBJECT || xclass != V_ASN1_UNIVERSAL) {
165  return NULL;
166  }
168  if (ret) {
169  *pp = p;
170  }
171  return ret;
172 }
173 
174 ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp,
175  long len)
176 {
177  ASN1_OBJECT *ret = NULL;
178  const unsigned char *p;
179  unsigned char *data;
180  int i, length;
181 
182  /*
183  * Sanity check OID encoding. Need at least one content octet. MSB must
184  * be clear in the last octet. can't have leading 0x80 in subidentifiers,
185  * see: X.690 8.19.2
186  */
187  if (len <= 0 || len > INT_MAX || pp == NULL || (p = *pp) == NULL ||
188  p[len - 1] & 0x80) {
190  return NULL;
191  }
192  /* Now 0 < len <= INT_MAX, so the cast is safe. */
193  length = (int)len;
194  for (i = 0; i < length; i++, p++) {
195  if (*p == 0x80 && (!i || !(p[-1] & 0x80))) {
197  return NULL;
198  }
199  }
200 
201  if ((a == NULL) || ((*a) == NULL) ||
202  !((*a)->flags & ASN1_OBJECT_FLAG_DYNAMIC)) {
203  if ((ret = ASN1_OBJECT_new()) == NULL)
204  return (NULL);
205  } else {
206  ret = (*a);
207  }
208 
209  p = *pp;
210  /* detach data from object */
211  data = (unsigned char *)ret->data;
212  ret->data = NULL;
213  /* once detached we can change it */
214  if ((data == NULL) || (ret->length < length)) {
215  ret->length = 0;
216  if (data != NULL)
218  data = (unsigned char *)OPENSSL_malloc(length);
219  if (data == NULL) {
221  goto err;
222  }
224  }
226  /* If there are dynamic strings, free them here, and clear the flag */
227  if ((ret->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) != 0) {
228  OPENSSL_free((char *)ret->sn);
229  OPENSSL_free((char *)ret->ln);
231  }
232  /* reattach data to object, after which it remains const */
233  ret->data = data;
234  ret->length = length;
235  ret->sn = NULL;
236  ret->ln = NULL;
237  p += length;
238 
239  if (a != NULL)
240  (*a) = ret;
241  *pp = p;
242  return (ret);
243  err:
244  OPENSSL_PUT_ERROR(ASN1, i);
245  if ((ret != NULL) && ((a == NULL) || (*a != ret)))
247  return (NULL);
248 }
249 
251 {
252  ASN1_OBJECT *ret;
253 
255  if (ret == NULL) {
257  return (NULL);
258  }
259  ret->length = 0;
260  ret->data = NULL;
261  ret->nid = 0;
262  ret->sn = NULL;
263  ret->ln = NULL;
264  ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
265  return (ret);
266 }
267 
269 {
270  if (a == NULL)
271  return;
272  if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
273  OPENSSL_free((void *)a->sn);
274  OPENSSL_free((void *)a->ln);
275  a->sn = a->ln = NULL;
276  }
277  if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
278  OPENSSL_free((void *)a->data);
279  a->data = NULL;
280  a->length = 0;
281  }
282  if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC)
283  OPENSSL_free(a);
284 }
285 
286 ASN1_OBJECT *ASN1_OBJECT_create(int nid, const unsigned char *data, int len,
287  const char *sn, const char *ln)
288 {
289  ASN1_OBJECT o;
290 
291  o.sn = sn;
292  o.ln = ln;
293  o.data = data;
294  o.nid = nid;
295  o.length = len;
298  return (OBJ_dup(&o));
299 }
ASN1_OBJECT_FLAG_DYNAMIC_DATA
#define ASN1_OBJECT_FLAG_DYNAMIC_DATA
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:94
xds_interop_client.str
str
Definition: xds_interop_client.py:487
ASN1_put_object
#define ASN1_put_object
Definition: boringssl_prefix_symbols.h:761
bio_st
Definition: bio.h:822
ASN1_R_ILLEGAL_OBJECT
#define ASN1_R_ILLEGAL_OBJECT
Definition: asn1.h:1977
ASN1_OBJECT_FLAG_DYNAMIC_STRINGS
#define ASN1_OBJECT_FLAG_DYNAMIC_STRINGS
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:93
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
ASN1_OBJECT_free
void ASN1_OBJECT_free(ASN1_OBJECT *a)
Definition: a_object.c:268
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
ASN1_R_TYPE_NOT_PRIMITIVE
#define ASN1_R_TYPE_NOT_PRIMITIVE
Definition: asn1.h:2022
error_ref_leak.err
err
Definition: error_ref_leak.py:35
BIO_write
#define BIO_write
Definition: boringssl_prefix_symbols.h:870
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
xds_manager.p
p
Definition: xds_manager.py:60
d2i_ASN1_OBJECT
ASN1_OBJECT * d2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, long length)
Definition: a_object.c:146
OBJ_dup
#define OBJ_dup
Definition: boringssl_prefix_symbols.h:1847
asn1_object_st
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:102
o
UnboundConversion o
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:97
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
ASN1_OBJECT_FLAG_DYNAMIC
#define ASN1_OBJECT_FLAG_DYNAMIC
Definition: third_party/boringssl-with-bazel/src/crypto/asn1/internal.h:92
V_ASN1_UNIVERSAL
#define V_ASN1_UNIVERSAL
Definition: asn1.h:92
internal.h
xds_interop_client.int
int
Definition: xds_interop_client.py:113
ERR_R_PASSED_NULL_PARAMETER
#define ERR_R_PASSED_NULL_PARAMETER
Definition: err.h:373
tag
static void * tag(intptr_t t)
Definition: bad_client.cc:318
i2t_ASN1_OBJECT
int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a)
Definition: a_object.c:108
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
err.h
ASN1_R_INVALID_OBJECT_ENCODING
#define ASN1_R_INVALID_OBJECT_ENCODING
Definition: asn1.h:1989
i2a_ASN1_OBJECT
int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a)
Definition: a_object.c:119
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
V_ASN1_CONSTRUCTED
#define V_ASN1_CONSTRUCTED
Definition: asn1.h:99
ASN1_R_BAD_OBJECT_HEADER
#define ASN1_R_BAD_OBJECT_HEADER
Definition: asn1.h:1946
inf
const char inf[]
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/upb.c:12099
nid
int nid
Definition: cipher_extra.c:71
pp
const uint8_t ** pp
Definition: ssl_x509.cc:1020
c2i_ASN1_OBJECT
ASN1_OBJECT * c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, long len)
Definition: a_object.c:174
ASN1_R_EXPECTING_AN_OBJECT
#define ASN1_R_EXPECTING_AN_OBJECT
Definition: asn1.h:1959
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
write_str
static int write_str(BIO *bp, const char *str)
Definition: a_object.c:113
ASN1_get_object
#define ASN1_get_object
Definition: boringssl_prefix_symbols.h:736
ASN1_OBJECT_create
ASN1_OBJECT * ASN1_OBJECT_create(int nid, const unsigned char *data, int len, const char *sn, const char *ln)
Definition: a_object.c:286
OBJ_obj2txt
#define OBJ_obj2txt
Definition: boringssl_prefix_symbols.h:1858
obj.h
mem.h
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
ASN1_OBJECT_new
ASN1_OBJECT * ASN1_OBJECT_new(void)
Definition: a_object.c:250
ASN1_object_size
#define ASN1_object_size
Definition: boringssl_prefix_symbols.h:758
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
V_ASN1_OBJECT
#define V_ASN1_OBJECT
Definition: asn1.h:130
i2d_ASN1_OBJECT
int i2d_ASN1_OBJECT(const ASN1_OBJECT *a, unsigned char **pp)
Definition: a_object.c:70
if
if(p->owned &&p->wrapped !=NULL)
Definition: call.c:42
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
asn1.h
ERR_R_MALLOC_FAILURE
#define ERR_R_MALLOC_FAILURE
Definition: err.h:371


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:28