x509name.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 <string.h>
58 
59 #include <openssl/asn1.h>
60 #include <openssl/err.h>
61 #include <openssl/evp.h>
62 #include <openssl/obj.h>
63 #include <openssl/stack.h>
64 #include <openssl/x509.h>
65 
66 #include "../internal.h"
67 #include "internal.h"
68 
69 
71  int len)
72 {
73  const ASN1_OBJECT *obj;
74 
75  obj = OBJ_nid2obj(nid);
76  if (obj == NULL)
77  return (-1);
79 }
80 
82  char *buf, int len)
83 {
84  int i;
86 
88  if (i < 0)
89  return (-1);
91  i = (data->length > (len - 1)) ? (len - 1) : data->length;
92  if (buf == NULL)
93  return (data->length);
94  OPENSSL_memcpy(buf, data->data, i);
95  buf[i] = '\0';
96  return (i);
97 }
98 
100 {
101  if (name == NULL)
102  return (0);
103  return (sk_X509_NAME_ENTRY_num(name->entries));
104 }
105 
106 int X509_NAME_get_index_by_NID(const X509_NAME *name, int nid, int lastpos)
107 {
108  const ASN1_OBJECT *obj;
109 
110  obj = OBJ_nid2obj(nid);
111  if (obj == NULL)
112  return (-2);
113  return (X509_NAME_get_index_by_OBJ(name, obj, lastpos));
114 }
115 
116 /* NOTE: you should be passsing -1, not 0 as lastpos */
118  int lastpos)
119 {
120  int n;
121  X509_NAME_ENTRY *ne;
123 
124  if (name == NULL)
125  return (-1);
126  if (lastpos < 0)
127  lastpos = -1;
128  sk = name->entries;
129  n = sk_X509_NAME_ENTRY_num(sk);
130  for (lastpos++; lastpos < n; lastpos++) {
131  ne = sk_X509_NAME_ENTRY_value(sk, lastpos);
132  if (OBJ_cmp(ne->object, obj) == 0)
133  return (lastpos);
134  }
135  return (-1);
136 }
137 
139 {
140  if (name == NULL || loc < 0
141  || sk_X509_NAME_ENTRY_num(name->entries) <= (size_t)loc)
142  return (NULL);
143  else
144  return (sk_X509_NAME_ENTRY_value(name->entries, loc));
145 }
146 
148 {
150  int i, n, set_prev, set_next;
152 
153  if (name == NULL || loc < 0
154  || sk_X509_NAME_ENTRY_num(name->entries) <= (size_t)loc)
155  return (NULL);
156  sk = name->entries;
157  ret = sk_X509_NAME_ENTRY_delete(sk, loc);
158  n = sk_X509_NAME_ENTRY_num(sk);
159  name->modified = 1;
160  if (loc == n)
161  return (ret);
162 
163  /* else we need to fixup the set field */
164  if (loc != 0)
165  set_prev = (sk_X509_NAME_ENTRY_value(sk, loc - 1))->set;
166  else
167  set_prev = ret->set - 1;
168  set_next = sk_X509_NAME_ENTRY_value(sk, loc)->set;
169 
170  /*
171  * set_prev is the previous set set is the current set set_next is the
172  * following prev 1 1 1 1 1 1 1 1 set 1 1 2 2 next 1 1 2 2 2 2 3 2 so
173  * basically only if prev and next differ by 2, then re-number down by 1
174  */
175  if (set_prev + 1 < set_next)
176  for (i = loc; i < n; i++)
177  sk_X509_NAME_ENTRY_value(sk, i)->set--;
178  return (ret);
179 }
180 
182  const unsigned char *bytes, int len, int loc,
183  int set)
184 {
185  X509_NAME_ENTRY *ne;
186  int ret;
188  if (!ne)
189  return 0;
192  return ret;
193 }
194 
196  const unsigned char *bytes, int len, int loc,
197  int set)
198 {
199  X509_NAME_ENTRY *ne;
200  int ret;
202  if (!ne)
203  return 0;
206  return ret;
207 }
208 
210  const unsigned char *bytes, int len, int loc,
211  int set)
212 {
213  X509_NAME_ENTRY *ne;
214  int ret;
216  if (!ne)
217  return 0;
220  return ret;
221 }
222 
223 /*
224  * if set is -1, append to previous set, 0 'a new one', and 1, prepend to the
225  * guy we are about to stomp on.
226  */
228  int set)
229 {
230  X509_NAME_ENTRY *new_name = NULL;
231  int n, i, inc;
233 
234  if (name == NULL)
235  return (0);
236  sk = name->entries;
237  n = sk_X509_NAME_ENTRY_num(sk);
238  if (loc > n)
239  loc = n;
240  else if (loc < 0)
241  loc = n;
242 
243  inc = (set == 0);
244  name->modified = 1;
245 
246  if (set == -1) {
247  if (loc == 0) {
248  set = 0;
249  inc = 1;
250  } else {
251  set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set;
252  }
253  } else { /* if (set >= 0) */
254 
255  if (loc >= n) {
256  if (loc != 0)
257  set = sk_X509_NAME_ENTRY_value(sk, loc - 1)->set + 1;
258  else
259  set = 0;
260  } else
261  set = sk_X509_NAME_ENTRY_value(sk, loc)->set;
262  }
263 
264  if ((new_name = X509_NAME_ENTRY_dup(ne)) == NULL)
265  goto err;
266  new_name->set = set;
267  if (!sk_X509_NAME_ENTRY_insert(sk, new_name, loc)) {
269  goto err;
270  }
271  if (inc) {
272  n = sk_X509_NAME_ENTRY_num(sk);
273  for (i = loc + 1; i < n; i++)
274  sk_X509_NAME_ENTRY_value(sk, i)->set += 1;
275  }
276  return (1);
277  err:
278  if (new_name != NULL)
279  X509_NAME_ENTRY_free(new_name);
280  return (0);
281 }
282 
284  const char *field, int type,
285  const unsigned char *bytes,
286  int len)
287 {
288  ASN1_OBJECT *obj;
289  X509_NAME_ENTRY *nentry;
290 
291  obj = OBJ_txt2obj(field, 0);
292  if (obj == NULL) {
294  ERR_add_error_data(2, "name=", field);
295  return (NULL);
296  }
299  return nentry;
300 }
301 
303  int type,
304  const unsigned char *bytes,
305  int len)
306 {
307  const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
308  if (obj == NULL) {
310  return NULL;
311  }
313 }
314 
316  const ASN1_OBJECT *obj,
317  int type,
318  const unsigned char *bytes,
319  int len)
320 {
322 
323  if ((ne == NULL) || (*ne == NULL)) {
324  if ((ret = X509_NAME_ENTRY_new()) == NULL)
325  return (NULL);
326  } else
327  ret = *ne;
328 
330  goto err;
332  goto err;
333 
334  if ((ne != NULL) && (*ne == NULL))
335  *ne = ret;
336  return (ret);
337  err:
338  if ((ne == NULL) || (ret != *ne))
340  return (NULL);
341 }
342 
344 {
345  if ((ne == NULL) || (obj == NULL)) {
347  return (0);
348  }
349  ASN1_OBJECT_free(ne->object);
350  ne->object = OBJ_dup(obj);
351  return ((ne->object == NULL) ? 0 : 1);
352 }
353 
355  const unsigned char *bytes, int len)
356 {
357  int i;
358 
359  if ((ne == NULL) || ((bytes == NULL) && (len != 0)))
360  return (0);
361  if ((type > 0) && (type & MBSTRING_FLAG))
362  return ASN1_STRING_set_by_NID(&ne->value, bytes,
363  len, type,
364  OBJ_obj2nid(ne->object)) ? 1 : 0;
365  if (len < 0)
366  len = strlen((const char *)bytes);
367  i = ASN1_STRING_set(ne->value, bytes, len);
368  if (!i)
369  return (0);
370  if (type != V_ASN1_UNDEF) {
371  ne->value->type = type;
372  }
373  return (1);
374 }
375 
377 {
378  if (ne == NULL)
379  return (NULL);
380  return (ne->object);
381 }
382 
384 {
385  if (ne == NULL)
386  return (NULL);
387  return (ne->value);
388 }
X509_NAME_add_entry_by_NID
int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid, int type, const unsigned char *bytes, int len, int loc, int set)
Definition: x509name.c:195
X509_NAME_get_index_by_OBJ
int X509_NAME_get_index_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj, int lastpos)
Definition: x509name.c:117
obj
OPENSSL_EXPORT const ASN1_OBJECT * obj
Definition: x509.h:1671
X509_NAME_get_text_by_NID
int X509_NAME_get_text_by_NID(const X509_NAME *name, int nid, char *buf, int len)
Definition: x509name.c:70
inc
static void inc(void *v)
Definition: spinlock_test.cc:125
evp.h
X509_NAME_ENTRY_free
#define X509_NAME_ENTRY_free
Definition: boringssl_prefix_symbols.h:2363
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
internal.h
X509_NAME_ENTRY_set_data
int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne, int type, const unsigned char *bytes, int len)
Definition: x509name.c:354
X509_NAME_get_text_by_OBJ
int X509_NAME_get_text_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj, char *buf, int len)
Definition: x509name.c:81
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
error_ref_leak.err
err
Definition: error_ref_leak.py:35
loc
OPENSSL_EXPORT X509_EXTENSION int loc
Definition: x509.h:1418
setup.name
name
Definition: setup.py:542
X509_NAME_ENTRY_get_object
ASN1_OBJECT * X509_NAME_ENTRY_get_object(const X509_NAME_ENTRY *ne)
Definition: x509name.c:376
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
OBJ_nid2obj
#define OBJ_nid2obj
Definition: boringssl_prefix_symbols.h:1855
X509_R_UNKNOWN_NID
#define X509_R_UNKNOWN_NID
Definition: x509.h:2406
ASN1_STRING_set
#define ASN1_STRING_set
Definition: boringssl_prefix_symbols.h:688
X509_NAME_entry_count
int X509_NAME_entry_count(const X509_NAME *name)
Definition: x509name.c:99
OBJ_obj2nid
#define OBJ_obj2nid
Definition: boringssl_prefix_symbols.h:1857
STACK_OF
#define STACK_OF(type)
Definition: stack.h:125
ERR_R_PASSED_NULL_PARAMETER
#define ERR_R_PASSED_NULL_PARAMETER
Definition: err.h:373
X509_R_INVALID_FIELD_NAME
#define X509_R_INVALID_FIELD_NAME
Definition: x509.h:2388
X509_NAME_ENTRY_get_data
ASN1_STRING * X509_NAME_ENTRY_get_data(const X509_NAME_ENTRY *ne)
Definition: x509name.c:383
OBJ_txt2obj
#define OBJ_txt2obj
Definition: boringssl_prefix_symbols.h:1861
ASN1_STRING_set_by_NID
#define ASN1_STRING_set_by_NID
Definition: boringssl_prefix_symbols.h:690
X509_NAME_add_entry_by_txt
int X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type, const unsigned char *bytes, int len, int loc, int set)
Definition: x509name.c:209
X509_NAME_add_entry_by_OBJ
int X509_NAME_add_entry_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, int type, const unsigned char *bytes, int len, int loc, int set)
Definition: x509name.c:181
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
X509_NAME_add_entry
int X509_NAME_add_entry(X509_NAME *name, X509_NAME_ENTRY *ne, int loc, int set)
Definition: x509name.c:227
err.h
X509_NAME_ENTRY_new
#define X509_NAME_ENTRY_new
Definition: boringssl_prefix_symbols.h:2367
X509_NAME_get_index_by_NID
int X509_NAME_get_index_by_NID(const X509_NAME *name, int nid, int lastpos)
Definition: x509name.c:106
ASN1_OBJECT_free
#define ASN1_OBJECT_free
Definition: boringssl_prefix_symbols.h:655
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
MBSTRING_FLAG
#define MBSTRING_FLAG
Definition: asn1.h:721
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
X509_name_entry_st
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:88
nid
int nid
Definition: cipher_extra.c:71
OBJ_cmp
#define OBJ_cmp
Definition: boringssl_prefix_symbols.h:1845
X509_NAME_ENTRY_dup
OPENSSL_EXPORT X509_NAME_ENTRY * X509_NAME_ENTRY_dup(X509_NAME_ENTRY *ne)
X509_NAME_ENTRY_set_object
int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne, const ASN1_OBJECT *obj)
Definition: x509name.c:343
X509_NAME_delete_entry
X509_NAME_ENTRY * X509_NAME_delete_entry(X509_NAME *name, int loc)
Definition: x509name.c:147
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
x509_st
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:139
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
X509_NAME_ENTRY_create_by_OBJ
X509_NAME_ENTRY * X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne, const ASN1_OBJECT *obj, int type, const unsigned char *bytes, int len)
Definition: x509name.c:315
X509_NAME_ENTRY_create_by_NID
X509_NAME_ENTRY * X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid, int type, const unsigned char *bytes, int len)
Definition: x509name.c:302
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
ERR_add_error_data
#define ERR_add_error_data
Definition: boringssl_prefix_symbols.h:1411
cpp.gmock_class.set
set
Definition: bloaty/third_party/googletest/googlemock/scripts/generator/cpp/gmock_class.py:44
V_ASN1_UNDEF
#define V_ASN1_UNDEF
Definition: asn1.h:115
obj.h
X509_NAME_ENTRY_create_by_txt
X509_NAME_ENTRY * X509_NAME_ENTRY_create_by_txt(X509_NAME_ENTRY **ne, const char *field, int type, const unsigned char *bytes, int len)
Definition: x509name.c:283
X509_name_entry_st::set
int set
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:91
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
X509_NAME_get_entry
X509_NAME_ENTRY * X509_NAME_get_entry(const X509_NAME *name, int loc)
Definition: x509name.c:138
X509_name_st
Definition: third_party/boringssl-with-bazel/src/crypto/x509/internal.h:95
if
if(p->owned &&p->wrapped !=NULL)
Definition: call.c:42
asn1_string_st
Definition: asn1.h:543
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
stack.h
x509.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:56