Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/asn1/a_object.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/asn1.h>
5
6 #include <limits.h>
7 #include <string.h>
8
9 #include <openssl/bytestring.h>
10 #include <openssl/err.h>
11 #include <openssl/mem.h>
12 #include <openssl/obj.h>
13
14 #include "../bytestring/internal.h"
15 #include "../internal.h"
16 #include "internal.h"
17
18
19 int i2d_ASN1_OBJECT(const ASN1_OBJECT *in, unsigned char **outp) {
20 if (in == NULL) {
21 OPENSSL_PUT_ERROR(ASN1, ERR_R_PASSED_NULL_PARAMETER);
22 return -1;
23 }
24
25 if (in->length <= 0) {
26 OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OBJECT);
27 return -1;
28 }
29
30 CBB cbb, child;
31 if (!CBB_init(&cbb, (size_t)in->length + 2) ||
32 !CBB_add_asn1(&cbb, &child, CBS_ASN1_OBJECT) ||
33 !CBB_add_bytes(&child, in->data, in->length)) {
34 CBB_cleanup(&cbb);
35 return -1;
36 }
37
38 return CBB_finish_i2d(&cbb, outp);
39 }
40
41 int i2t_ASN1_OBJECT(char *buf, int buf_len, const ASN1_OBJECT *a) {
42 return OBJ_obj2txt(buf, buf_len, a, 0);
43 }
44
45 static int write_str(BIO *bp, const char *str) {
46 size_t len = strlen(str);
47 if (len > INT_MAX) {
48 OPENSSL_PUT_ERROR(ASN1, ERR_R_OVERFLOW);
49 return -1;
50 }
51 return BIO_write(bp, str, (int)len) == (int)len ? (int)len : -1;
52 }
53
54 int i2a_ASN1_OBJECT(BIO *bp, const ASN1_OBJECT *a) {
55 if (a == NULL || a->data == NULL) {
56 return write_str(bp, "NULL");
57 }
58
59 char buf[80], *allocated = NULL;
60 const char *str = buf;
61 int len = i2t_ASN1_OBJECT(buf, sizeof(buf), a);
62 if (len > (int)sizeof(buf) - 1) {
63 // The input was truncated. Allocate a buffer that fits.
64 allocated = OPENSSL_malloc(len + 1);
65 if (allocated == NULL) {
66 return -1;
67 }
68 len = i2t_ASN1_OBJECT(allocated, len + 1, a);
69 str = allocated;
70 }
71 if (len <= 0) {
72 str = "<INVALID>";
73 }
74
75 int ret = write_str(bp, str);
76 OPENSSL_free(allocated);
77 return ret;
78 }
79
80 ASN1_OBJECT *d2i_ASN1_OBJECT(ASN1_OBJECT **out, const unsigned char **inp,
81 long len) {
82 if (len < 0) {
83 return NULL;
84 }
85
86 CBS cbs, child;
87 CBS_init(&cbs, *inp, (size_t)len);
88 if (!CBS_get_asn1(&cbs, &child, CBS_ASN1_OBJECT)) {
89 OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
90 return NULL;
91 }
92
93 const uint8_t *contents = CBS_data(&child);
94 ASN1_OBJECT *ret = c2i_ASN1_OBJECT(out, &contents, CBS_len(&child));
95 if (ret != NULL) {
96 // |c2i_ASN1_OBJECT| should have consumed the entire input.
97 assert(CBS_data(&cbs) == contents);
98 *inp = CBS_data(&cbs);
99 }
100 return ret;
101 }
102
103 ASN1_OBJECT *c2i_ASN1_OBJECT(ASN1_OBJECT **out, const unsigned char **inp,
104 long len) {
105 if (len < 0) {
106 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
107 return NULL;
108 }
109
110 CBS cbs;
111 CBS_init(&cbs, *inp, (size_t)len);
112 if (!CBS_is_valid_asn1_oid(&cbs)) {
113 OPENSSL_PUT_ERROR(ASN1, ASN1_R_INVALID_OBJECT_ENCODING);
114 return NULL;
115 }
116
117 ASN1_OBJECT *ret = ASN1_OBJECT_create(NID_undef, *inp, (size_t)len,
118 /*sn=*/NULL, /*ln=*/NULL);
119 if (ret == NULL) {
120 return NULL;
121 }
122
123 if (out != NULL) {
124 ASN1_OBJECT_free(*out);
125 *out = ret;
126 }
127 *inp += len; // All bytes were consumed.
128 return ret;
129 }
130
131 ASN1_OBJECT *ASN1_OBJECT_new(void) {
132 ASN1_OBJECT *ret;
133
134 ret = (ASN1_OBJECT *)OPENSSL_zalloc(sizeof(ASN1_OBJECT));
135 if (ret == NULL) {
136 return NULL;
137 }
138 ret->flags = ASN1_OBJECT_FLAG_DYNAMIC;
139 return ret;
140 }
141
142 void ASN1_OBJECT_free(ASN1_OBJECT *a) {
143 if (a == NULL) {
144 return;
145 }
146 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_STRINGS) {
147 OPENSSL_free((void *)a->sn);
148 OPENSSL_free((void *)a->ln);
149 a->sn = a->ln = NULL;
150 }
151 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
152 OPENSSL_free((void *)a->data);
153 a->data = NULL;
154 a->length = 0;
155 }
156 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC) {
157 OPENSSL_free(a);
158 }
159 }
160
161 ASN1_OBJECT *ASN1_OBJECT_create(int nid, const unsigned char *data, size_t len,
162 const char *sn, const char *ln) {
163 if (len > INT_MAX) {
164 OPENSSL_PUT_ERROR(ASN1, ASN1_R_STRING_TOO_LONG);
165 return NULL;
166 }
167
168 ASN1_OBJECT o;
169 o.sn = sn;
170 o.ln = ln;
171 o.data = data;
172 o.nid = nid;
173 o.length = (int)len;
174 o.flags = ASN1_OBJECT_FLAG_DYNAMIC | ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
175 ASN1_OBJECT_FLAG_DYNAMIC_DATA;
176 return OBJ_dup(&o);
177 }