Coverage Report

Created: 2026-06-28 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/x509/x509name.cc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <string.h>
16
17
#include <openssl/asn1.h>
18
#include <openssl/bytestring.h>
19
#include <openssl/err.h>
20
#include <openssl/evp.h>
21
#include <openssl/obj.h>
22
#include <openssl/stack.h>
23
#include <openssl/x509.h>
24
25
#include "../internal.h"
26
#include "internal.h"
27
28
29
using namespace bssl;
30
31
int X509_NAME_get_text_by_NID(const X509_NAME *name, int nid, char *buf,
32
0
                              int len) {
33
0
  const ASN1_OBJECT *obj;
34
35
0
  obj = OBJ_nid2obj(nid);
36
0
  if (obj == nullptr) {
37
0
    return -1;
38
0
  }
39
0
  return (X509_NAME_get_text_by_OBJ(name, obj, buf, len));
40
0
}
41
42
int X509_NAME_get_text_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj,
43
0
                              char *buf, int len) {
44
0
  int i = X509_NAME_get_index_by_OBJ(name, obj, -1);
45
0
  if (i < 0) {
46
0
    return -1;
47
0
  }
48
0
  const ASN1_STRING *data =
49
0
      X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i));
50
0
  unsigned char *text = nullptr;
51
0
  int ret = -1;
52
0
  int text_len = ASN1_STRING_to_UTF8(&text, data);
53
  // Fail if we could not encode as UTF-8.
54
0
  if (text_len < 0) {
55
0
    goto out;
56
0
  }
57
0
  CBS cbs;
58
0
  CBS_init(&cbs, text, text_len);
59
  // Fail if the UTF-8 encoding contains a 0 byte because this is returned as a
60
  // C string and callers very often do not check.
61
0
  if (CBS_contains_zero_byte(&cbs)) {
62
0
    goto out;
63
0
  }
64
  // We still support the "pass NULL to find out how much" API
65
0
  if (buf != nullptr) {
66
0
    if (text_len >= len || len <= 0 ||
67
0
        !CBS_copy_bytes(&cbs, (uint8_t *)buf, text_len)) {
68
0
      goto out;
69
0
    }
70
    // It must be a C string
71
0
    buf[text_len] = '\0';
72
0
  }
73
0
  ret = text_len;
74
75
0
out:
76
0
  OPENSSL_free(text);
77
0
  return ret;
78
0
}
79
80
189
int X509_NAME_entry_count(const X509_NAME *name) {
81
189
  if (name == nullptr) {
82
0
    return 0;
83
0
  }
84
189
  const auto *impl = FromOpaque(name);
85
189
  return (int)sk_X509_NAME_ENTRY_num(impl->entries.get());
86
189
}
87
88
0
int X509_NAME_get_index_by_NID(const X509_NAME *name, int nid, int lastpos) {
89
0
  const ASN1_OBJECT *obj;
90
91
0
  obj = OBJ_nid2obj(nid);
92
0
  if (obj == nullptr) {
93
0
    return -2;
94
0
  }
95
0
  return X509_NAME_get_index_by_OBJ(name, obj, lastpos);
96
0
}
97
98
// NOTE: you should be passing -1, not 0 as lastpos
99
int X509_NAME_get_index_by_OBJ(const X509_NAME *name, const ASN1_OBJECT *obj,
100
0
                               int lastpos) {
101
0
  if (name == nullptr) {
102
0
    return -1;
103
0
  }
104
0
  const auto *impl = FromOpaque(name);
105
0
  if (lastpos < 0) {
106
0
    lastpos = -1;
107
0
  }
108
0
  const STACK_OF(X509_NAME_ENTRY) *sk = impl->entries.get();
109
0
  int n = (int)sk_X509_NAME_ENTRY_num(sk);
110
0
  for (lastpos++; lastpos < n; lastpos++) {
111
0
    const X509_NAME_ENTRY *ne = sk_X509_NAME_ENTRY_value(sk, lastpos);
112
0
    if (OBJ_cmp(X509_NAME_ENTRY_get_object(ne), obj) == 0) {
113
0
      return lastpos;
114
0
    }
115
0
  }
116
0
  return -1;
117
0
}
118
119
572
X509_NAME_ENTRY *X509_NAME_get_entry(const X509_NAME *name, int loc) {
120
572
  if (name == nullptr) {
121
0
    return nullptr;
122
0
  }
123
572
  const auto *impl = FromOpaque(name);
124
572
  if (loc < 0 || sk_X509_NAME_ENTRY_num(impl->entries.get()) <= (size_t)loc) {
125
0
    return nullptr;
126
572
  } else {
127
572
    return (sk_X509_NAME_ENTRY_value(impl->entries.get(), loc));
128
572
  }
129
572
}
130
131
0
X509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc) {
132
0
  if (name == nullptr) {
133
0
    return nullptr;
134
0
  }
135
0
  const auto *impl = FromOpaque(name);
136
0
  if (loc < 0 || sk_X509_NAME_ENTRY_num(impl->entries.get()) <= (size_t)loc) {
137
0
    return nullptr;
138
0
  }
139
140
0
  STACK_OF(X509_NAME_ENTRY) *sk = impl->entries.get();
141
0
  X509_NAME_ENTRY *ret = sk_X509_NAME_ENTRY_delete(sk, loc);
142
0
  size_t n = sk_X509_NAME_ENTRY_num(sk);
143
0
  x509_name_invalidate_cache(name);
144
0
  if ((size_t)loc == n) {
145
0
    return ret;
146
0
  }
147
148
0
  int set_prev;
149
0
  if (loc != 0) {
150
0
    set_prev = FromOpaque(sk_X509_NAME_ENTRY_value(sk, loc - 1))->set;
151
0
  } else {
152
0
    set_prev = FromOpaque(ret)->set - 1;
153
0
  }
154
0
  int set_next = FromOpaque(sk_X509_NAME_ENTRY_value(sk, loc))->set;
155
156
  // If we removed a singleton RDN, update the RDN indices so they are
157
  // consecutive again.
158
0
  if (set_prev + 1 < set_next) {
159
0
    for (size_t i = loc; i < n; i++) {
160
0
      FromOpaque(sk_X509_NAME_ENTRY_value(sk, i))->set--;
161
0
    }
162
0
  }
163
0
  return ret;
164
0
}
165
166
int X509_NAME_add_entry_by_OBJ(X509_NAME *name, const ASN1_OBJECT *obj,
167
                               int type, const unsigned char *bytes,
168
0
                               ossl_ssize_t len, int loc, int set) {
169
0
  X509_NAME_ENTRY *ne =
170
0
      X509_NAME_ENTRY_create_by_OBJ(nullptr, obj, type, bytes, len);
171
0
  if (!ne) {
172
0
    return 0;
173
0
  }
174
0
  int ret = X509_NAME_add_entry(name, ne, loc, set);
175
0
  X509_NAME_ENTRY_free(ne);
176
0
  return ret;
177
0
}
178
179
int X509_NAME_add_entry_by_NID(X509_NAME *name, int nid, int type,
180
                               const unsigned char *bytes, ossl_ssize_t len,
181
0
                               int loc, int set) {
182
0
  X509_NAME_ENTRY *ne =
183
0
      X509_NAME_ENTRY_create_by_NID(nullptr, nid, type, bytes, len);
184
0
  if (!ne) {
185
0
    return 0;
186
0
  }
187
0
  int ret = X509_NAME_add_entry(name, ne, loc, set);
188
0
  X509_NAME_ENTRY_free(ne);
189
0
  return ret;
190
0
}
191
192
int X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type,
193
                               const unsigned char *bytes, ossl_ssize_t len,
194
0
                               int loc, int set) {
195
0
  X509_NAME_ENTRY *ne =
196
0
      X509_NAME_ENTRY_create_by_txt(nullptr, field, type, bytes, len);
197
0
  if (!ne) {
198
0
    return 0;
199
0
  }
200
0
  int ret = X509_NAME_add_entry(name, ne, loc, set);
201
0
  X509_NAME_ENTRY_free(ne);
202
0
  return ret;
203
0
}
204
205
// if set is -1, append to previous set, 0 'a new one', and 1, prepend to the
206
// guy we are about to stomp on.
207
int X509_NAME_add_entry(X509_NAME *name, const X509_NAME_ENTRY *entry, int loc,
208
0
                        int set) {
209
0
  if (name == nullptr) {
210
0
    return 0;
211
0
  }
212
0
  auto *impl = FromOpaque(name);
213
0
  if (impl->entries == nullptr) {
214
0
    impl->entries.reset(sk_X509_NAME_ENTRY_new_null());
215
0
    if (impl->entries == nullptr) {
216
0
      return 0;
217
0
    }
218
0
  }
219
220
0
  STACK_OF(X509_NAME_ENTRY) *sk = impl->entries.get();
221
0
  int n = (int)sk_X509_NAME_ENTRY_num(sk);
222
0
  if (loc > n) {
223
0
    loc = n;
224
0
  } else if (loc < 0) {
225
0
    loc = n;
226
0
  }
227
228
0
  bool inc = set == 0;
229
0
  x509_name_invalidate_cache(name);
230
231
0
  if (set == -1) {
232
0
    if (loc == 0) {
233
0
      set = 0;
234
0
      inc = 1;
235
0
    } else {
236
0
      set = FromOpaque(sk_X509_NAME_ENTRY_value(sk, loc - 1))->set;
237
0
    }
238
0
  } else {  // if (set >= 0)
239
0
    if (loc >= n) {
240
0
      if (loc != 0) {
241
0
        set = FromOpaque(sk_X509_NAME_ENTRY_value(sk, loc - 1))->set + 1;
242
0
      } else {
243
0
        set = 0;
244
0
      }
245
0
    } else {
246
0
      set = FromOpaque(sk_X509_NAME_ENTRY_value(sk, loc))->set;
247
0
    }
248
0
  }
249
250
0
  UniquePtr<X509_NAME_ENTRY> new_entry(X509_NAME_ENTRY_dup(entry));
251
0
  if (new_entry == nullptr) {
252
0
    return 0;
253
0
  }
254
0
  FromOpaque(new_entry.get())->set = set;
255
0
  if (!sk_X509_NAME_ENTRY_insert(sk, new_entry.get(), loc)) {
256
0
    return 0;
257
0
  }
258
0
  new_entry.release(); // `sk` took ownership.
259
0
  if (inc) {
260
0
    n = (int)sk_X509_NAME_ENTRY_num(sk);
261
0
    for (int i = loc + 1; i < n; i++) {
262
0
      FromOpaque(sk_X509_NAME_ENTRY_value(sk, i))->set += 1;
263
0
    }
264
0
  }
265
0
  return 1;
266
0
}
267
268
X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_txt(X509_NAME_ENTRY **ne,
269
                                               const char *field, int type,
270
                                               const unsigned char *bytes,
271
0
                                               ossl_ssize_t len) {
272
0
  ASN1_OBJECT *obj;
273
0
  X509_NAME_ENTRY *nentry;
274
275
0
  obj = OBJ_txt2obj(field, 0);
276
0
  if (obj == nullptr) {
277
0
    OPENSSL_PUT_ERROR(X509, X509_R_INVALID_FIELD_NAME);
278
0
    ERR_add_error_data(2, "name=", field);
279
0
    return nullptr;
280
0
  }
281
0
  nentry = X509_NAME_ENTRY_create_by_OBJ(ne, obj, type, bytes, len);
282
0
  ASN1_OBJECT_free(obj);
283
0
  return nentry;
284
0
}
285
286
X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid,
287
                                               int type,
288
                                               const unsigned char *bytes,
289
0
                                               ossl_ssize_t len) {
290
0
  const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
291
0
  if (obj == nullptr) {
292
0
    OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_NID);
293
0
    return nullptr;
294
0
  }
295
0
  return X509_NAME_ENTRY_create_by_OBJ(ne, obj, type, bytes, len);
296
0
}
297
298
X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne,
299
                                               const ASN1_OBJECT *obj, int type,
300
                                               const unsigned char *bytes,
301
0
                                               ossl_ssize_t len) {
302
0
  X509_NAME_ENTRY *ret;
303
304
0
  if ((ne == nullptr) || (*ne == nullptr)) {
305
0
    if ((ret = X509_NAME_ENTRY_new()) == nullptr) {
306
0
      return nullptr;
307
0
    }
308
0
  } else {
309
0
    ret = *ne;
310
0
  }
311
312
0
  if (!X509_NAME_ENTRY_set_object(ret, obj)) {
313
0
    goto err;
314
0
  }
315
0
  if (!X509_NAME_ENTRY_set_data(ret, type, bytes, len)) {
316
0
    goto err;
317
0
  }
318
319
0
  if ((ne != nullptr) && (*ne == nullptr)) {
320
0
    *ne = ret;
321
0
  }
322
0
  return ret;
323
0
err:
324
0
  if ((ne == nullptr) || (ret != *ne)) {
325
0
    X509_NAME_ENTRY_free(ret);
326
0
  }
327
0
  return nullptr;
328
0
}
329
330
0
int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne, const ASN1_OBJECT *obj) {
331
0
  auto *ne_impl = FromOpaque(ne);
332
0
  if (ne_impl == nullptr || obj == nullptr) {
333
0
    OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER);
334
0
    return 0;
335
0
  }
336
0
  ne_impl->object.reset(OBJ_dup(obj));
337
0
  return ne_impl->object != nullptr;
338
0
}
339
340
int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne, int type,
341
0
                             const unsigned char *bytes, ossl_ssize_t len) {
342
0
  auto *ne_impl = FromOpaque(ne);
343
0
  if (ne_impl == nullptr || (bytes == nullptr && len != 0)) {
344
0
    return 0;
345
0
  }
346
0
  if ((type > 0) && (type & MBSTRING_FLAG)) {
347
0
    ASN1_STRING *dst = ne_impl->value.get();
348
0
    return ASN1_STRING_set_by_NID(&dst, bytes, len, type,
349
0
                                  OBJ_obj2nid(ne_impl->object.get())) !=
350
0
           nullptr;
351
0
  }
352
0
  if (len < 0) {
353
0
    len = strlen((const char *)bytes);
354
0
  }
355
0
  if (!ASN1_STRING_set(ne_impl->value.get(), bytes, len)) {
356
0
    return 0;
357
0
  }
358
0
  if (type != V_ASN1_UNDEF) {
359
0
    ne_impl->value->type = type;
360
0
  }
361
0
  return 1;
362
0
}
363
364
24.2k
ASN1_OBJECT *X509_NAME_ENTRY_get_object(const X509_NAME_ENTRY *ne) {
365
24.2k
  if (ne == nullptr) {
366
0
    return nullptr;
367
0
  }
368
24.2k
  return FromOpaque(ne)->object.get();
369
24.2k
}
370
371
15.2k
ASN1_STRING *X509_NAME_ENTRY_get_data(const X509_NAME_ENTRY *ne) {
372
15.2k
  if (ne == nullptr) {
373
0
    return nullptr;
374
0
  }
375
  // This function is not const-correct for OpenSSL compatibility.
376
15.2k
  return const_cast<ASN1_STRING*>(FromOpaque(ne)->value.get());
377
15.2k
}