Coverage Report

Created: 2025-08-28 06:59

/src/boringssl/crypto/x509/v3_bitst.cc
Line
Count
Source (jump to first uncovered line)
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
static const BIT_STRING_BITNAME ns_cert_type_table[] = {
27
    {0, "SSL Client", "client"},
28
    {1, "SSL Server", "server"},
29
    {2, "S/MIME", "email"},
30
    {3, "Object Signing", "objsign"},
31
    {4, "Unused", "reserved"},
32
    {5, "SSL CA", "sslCA"},
33
    {6, "S/MIME CA", "emailCA"},
34
    {7, "Object Signing CA", "objCA"},
35
    {-1, NULL, NULL}};
36
37
static const BIT_STRING_BITNAME key_usage_type_table[] = {
38
    {0, "Digital Signature", "digitalSignature"},
39
    {1, "Non Repudiation", "nonRepudiation"},
40
    {2, "Key Encipherment", "keyEncipherment"},
41
    {3, "Data Encipherment", "dataEncipherment"},
42
    {4, "Key Agreement", "keyAgreement"},
43
    {5, "Certificate Sign", "keyCertSign"},
44
    {6, "CRL Sign", "cRLSign"},
45
    {7, "Encipher Only", "encipherOnly"},
46
    {8, "Decipher Only", "decipherOnly"},
47
    {-1, NULL, NULL}};
48
49
static STACK_OF(CONF_VALUE) *i2v_ASN1_BIT_STRING(
50
1.56k
    const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *ret) {
51
1.56k
  const ASN1_BIT_STRING *bits = reinterpret_cast<ASN1_BIT_STRING *>(ext);
52
1.56k
  const BIT_STRING_BITNAME *bnam;
53
1.56k
  for (bnam = reinterpret_cast<const BIT_STRING_BITNAME *>(method->usr_data);
54
15.5k
       bnam->lname; bnam++) {
55
14.0k
    if (ASN1_BIT_STRING_get_bit(bits, bnam->bitnum)) {
56
6.30k
      X509V3_add_value(bnam->lname, NULL, &ret);
57
6.30k
    }
58
14.0k
  }
59
1.56k
  return ret;
60
1.56k
}
61
62
static void *v2i_ASN1_BIT_STRING(const X509V3_EXT_METHOD *method,
63
                                 const X509V3_CTX *ctx,
64
551
                                 const STACK_OF(CONF_VALUE) *nval) {
65
551
  ASN1_BIT_STRING *bs;
66
551
  if (!(bs = ASN1_BIT_STRING_new())) {
67
0
    return NULL;
68
0
  }
69
1.05k
  for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) {
70
994
    const CONF_VALUE *val = sk_CONF_VALUE_value(nval, i);
71
994
    const BIT_STRING_BITNAME *bnam;
72
994
    for (bnam = reinterpret_cast<const BIT_STRING_BITNAME *>(method->usr_data);
73
7.75k
         bnam->lname; bnam++) {
74
7.26k
      if (!strcmp(bnam->sname, val->name) || !strcmp(bnam->lname, val->name)) {
75
506
        if (!ASN1_BIT_STRING_set_bit(bs, bnam->bitnum, 1)) {
76
0
          ASN1_BIT_STRING_free(bs);
77
0
          return NULL;
78
0
        }
79
506
        break;
80
506
      }
81
7.26k
    }
82
994
    if (!bnam->lname) {
83
488
      OPENSSL_PUT_ERROR(X509V3, X509V3_R_UNKNOWN_BIT_STRING_ARGUMENT);
84
488
      X509V3_conf_err(val);
85
488
      ASN1_BIT_STRING_free(bs);
86
488
      return NULL;
87
488
    }
88
994
  }
89
63
  return bs;
90
551
}
91
92
#define EXT_BITSTRING(nid, table)                                             \
93
  {                                                                           \
94
    nid, 0, ASN1_ITEM_ref(ASN1_BIT_STRING), 0, 0, 0, 0, 0, 0,                 \
95
        i2v_ASN1_BIT_STRING, v2i_ASN1_BIT_STRING, NULL, NULL, (void *)(table) \
96
  }
97
98
const X509V3_EXT_METHOD v3_nscert =
99
    EXT_BITSTRING(NID_netscape_cert_type, ns_cert_type_table);
100
const X509V3_EXT_METHOD v3_key_usage =
101
    EXT_BITSTRING(NID_key_usage, key_usage_type_table);