Coverage Report

Created: 2026-10-19 07:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/x509/v3_bitst.cc
Line
Count
Source
1
// Copyright 1999-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 <stdio.h>
16
#include <string.h>
17
18
#include <openssl/conf.h>
19
#include <openssl/err.h>
20
#include <openssl/obj.h>
21
#include <openssl/x509.h>
22
23
#include "internal.h"
24
25
26
using namespace bssl;
27
28
static const BIT_STRING_BITNAME ns_cert_type_table[] = {
29
    {0, "SSL Client", "client"}, {1, "SSL Server", "server"},
30
    {2, "S/MIME", "email"},      {3, "Object Signing", "objsign"},
31
    {4, "Unused", "reserved"},   {5, "SSL CA", "sslCA"},
32
    {6, "S/MIME CA", "emailCA"}, {7, "Object Signing CA", "objCA"},
33
    {-1, nullptr, nullptr}};
34
35
static const BIT_STRING_BITNAME key_usage_type_table[] = {
36
    {0, "Digital Signature", "digitalSignature"},
37
    {1, "Non Repudiation", "nonRepudiation"},
38
    {2, "Key Encipherment", "keyEncipherment"},
39
    {3, "Data Encipherment", "dataEncipherment"},
40
    {4, "Key Agreement", "keyAgreement"},
41
    {5, "Certificate Sign", "keyCertSign"},
42
    {6, "CRL Sign", "cRLSign"},
43
    {7, "Encipher Only", "encipherOnly"},
44
    {8, "Decipher Only", "decipherOnly"},
45
    {-1, nullptr, nullptr}};
46
47
static STACK_OF(CONF_VALUE) *i2v_ASN1_BIT_STRING(
48
897
    const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *ret) {
49
897
  const ASN1_BIT_STRING *bits = reinterpret_cast<ASN1_BIT_STRING *>(ext);
50
897
  const BIT_STRING_BITNAME *bnam;
51
897
  for (bnam = reinterpret_cast<const BIT_STRING_BITNAME *>(method->usr_data);
52
8.92k
       bnam->lname; bnam++) {
53
8.02k
    if (ASN1_BIT_STRING_get_bit(bits, bnam->bitnum)) {
54
4.02k
      X509V3_add_value(bnam->lname, nullptr, &ret);
55
4.02k
    }
56
8.02k
  }
57
897
  return ret;
58
897
}
59
60
static void *v2i_ASN1_BIT_STRING(const X509V3_EXT_METHOD *method,
61
                                 const X509V3_CTX *ctx,
62
558
                                 const STACK_OF(CONF_VALUE) *nval) {
63
558
  ASN1_BIT_STRING *bs;
64
558
  if (!(bs = ASN1_BIT_STRING_new())) {
65
0
    return nullptr;
66
0
  }
67
1.03k
  for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) {
68
970
    const CONF_VALUE *val = sk_CONF_VALUE_value(nval, i);
69
970
    const BIT_STRING_BITNAME *bnam;
70
970
    for (bnam = reinterpret_cast<const BIT_STRING_BITNAME *>(method->usr_data);
71
7.75k
         bnam->lname; bnam++) {
72
7.25k
      if (!strcmp(bnam->sname, val->name) || !strcmp(bnam->lname, val->name)) {
73
473
        if (!ASN1_BIT_STRING_set_bit(bs, bnam->bitnum, 1)) {
74
0
          ASN1_BIT_STRING_free(bs);
75
0
          return nullptr;
76
0
        }
77
473
        break;
78
473
      }
79
7.25k
    }
80
970
    if (!bnam->lname) {
81
497
      OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_BIT_STRING_ARGUMENT);
82
497
      X509V3_conf_err(val);
83
497
      ASN1_BIT_STRING_free(bs);
84
497
      return nullptr;
85
497
    }
86
970
  }
87
61
  return bs;
88
558
}
89
90
#define EXT_BITSTRING(nid, table)                                             \
91
  {                                                                           \
92
    nid, 0, ASN1_ITEM_ref(ASN1_BIT_STRING), 0, 0, 0, 0, 0, 0,                 \
93
        i2v_ASN1_BIT_STRING, v2i_ASN1_BIT_STRING, nullptr, nullptr,           \
94
    (void *)(table)                                                           \
95
  }
96
97
const X509V3_EXT_METHOD bssl::v3_nscert =
98
    EXT_BITSTRING(NID_netscape_cert_type, ns_cert_type_table);
99
const X509V3_EXT_METHOD bssl::v3_key_usage =
100
    EXT_BITSTRING(NID_key_usage, key_usage_type_table);