xref: /third_party/openssl/crypto/x509/v3_bitst.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
12e1051a39Sopenharmony_ci#include <openssl/conf.h>
13e1051a39Sopenharmony_ci#include <openssl/x509v3.h>
14e1051a39Sopenharmony_ci#include "ext_dat.h"
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_cistatic BIT_STRING_BITNAME ns_cert_type_table[] = {
17e1051a39Sopenharmony_ci    {0, "SSL Client", "client"},
18e1051a39Sopenharmony_ci    {1, "SSL Server", "server"},
19e1051a39Sopenharmony_ci    {2, "S/MIME", "email"},
20e1051a39Sopenharmony_ci    {3, "Object Signing", "objsign"},
21e1051a39Sopenharmony_ci    {4, "Unused", "reserved"},
22e1051a39Sopenharmony_ci    {5, "SSL CA", "sslCA"},
23e1051a39Sopenharmony_ci    {6, "S/MIME CA", "emailCA"},
24e1051a39Sopenharmony_ci    {7, "Object Signing CA", "objCA"},
25e1051a39Sopenharmony_ci    {-1, NULL, NULL}
26e1051a39Sopenharmony_ci};
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_cistatic BIT_STRING_BITNAME key_usage_type_table[] = {
29e1051a39Sopenharmony_ci    {0, "Digital Signature", "digitalSignature"},
30e1051a39Sopenharmony_ci    {1, "Non Repudiation", "nonRepudiation"},
31e1051a39Sopenharmony_ci    {2, "Key Encipherment", "keyEncipherment"},
32e1051a39Sopenharmony_ci    {3, "Data Encipherment", "dataEncipherment"},
33e1051a39Sopenharmony_ci    {4, "Key Agreement", "keyAgreement"},
34e1051a39Sopenharmony_ci    {5, "Certificate Sign", "keyCertSign"},
35e1051a39Sopenharmony_ci    {6, "CRL Sign", "cRLSign"},
36e1051a39Sopenharmony_ci    {7, "Encipher Only", "encipherOnly"},
37e1051a39Sopenharmony_ci    {8, "Decipher Only", "decipherOnly"},
38e1051a39Sopenharmony_ci    {-1, NULL, NULL}
39e1051a39Sopenharmony_ci};
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_nscert =
42e1051a39Sopenharmony_ciEXT_BITSTRING(NID_netscape_cert_type, ns_cert_type_table);
43e1051a39Sopenharmony_ciconst X509V3_EXT_METHOD ossl_v3_key_usage =
44e1051a39Sopenharmony_ciEXT_BITSTRING(NID_key_usage, key_usage_type_table);
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_ciSTACK_OF(CONF_VALUE) *i2v_ASN1_BIT_STRING(X509V3_EXT_METHOD *method,
47e1051a39Sopenharmony_ci                                          ASN1_BIT_STRING *bits,
48e1051a39Sopenharmony_ci                                          STACK_OF(CONF_VALUE) *ret)
49e1051a39Sopenharmony_ci{
50e1051a39Sopenharmony_ci    BIT_STRING_BITNAME *bnam;
51e1051a39Sopenharmony_ci    for (bnam = method->usr_data; bnam->lname; bnam++) {
52e1051a39Sopenharmony_ci        if (ASN1_BIT_STRING_get_bit(bits, bnam->bitnum))
53e1051a39Sopenharmony_ci            X509V3_add_value(bnam->lname, NULL, &ret);
54e1051a39Sopenharmony_ci    }
55e1051a39Sopenharmony_ci    return ret;
56e1051a39Sopenharmony_ci}
57e1051a39Sopenharmony_ci
58e1051a39Sopenharmony_ciASN1_BIT_STRING *v2i_ASN1_BIT_STRING(X509V3_EXT_METHOD *method,
59e1051a39Sopenharmony_ci                                     X509V3_CTX *ctx,
60e1051a39Sopenharmony_ci                                     STACK_OF(CONF_VALUE) *nval)
61e1051a39Sopenharmony_ci{
62e1051a39Sopenharmony_ci    CONF_VALUE *val;
63e1051a39Sopenharmony_ci    ASN1_BIT_STRING *bs;
64e1051a39Sopenharmony_ci    int i;
65e1051a39Sopenharmony_ci    BIT_STRING_BITNAME *bnam;
66e1051a39Sopenharmony_ci    if ((bs = ASN1_BIT_STRING_new()) == NULL) {
67e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
68e1051a39Sopenharmony_ci        return NULL;
69e1051a39Sopenharmony_ci    }
70e1051a39Sopenharmony_ci    for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
71e1051a39Sopenharmony_ci        val = sk_CONF_VALUE_value(nval, i);
72e1051a39Sopenharmony_ci        for (bnam = method->usr_data; bnam->lname; bnam++) {
73e1051a39Sopenharmony_ci            if (strcmp(bnam->sname, val->name) == 0
74e1051a39Sopenharmony_ci                || strcmp(bnam->lname, val->name) == 0) {
75e1051a39Sopenharmony_ci                if (!ASN1_BIT_STRING_set_bit(bs, bnam->bitnum, 1)) {
76e1051a39Sopenharmony_ci                    ERR_raise(ERR_LIB_X509V3, ERR_R_MALLOC_FAILURE);
77e1051a39Sopenharmony_ci                    ASN1_BIT_STRING_free(bs);
78e1051a39Sopenharmony_ci                    return NULL;
79e1051a39Sopenharmony_ci                }
80e1051a39Sopenharmony_ci                break;
81e1051a39Sopenharmony_ci            }
82e1051a39Sopenharmony_ci        }
83e1051a39Sopenharmony_ci        if (!bnam->lname) {
84e1051a39Sopenharmony_ci            ERR_raise_data(ERR_LIB_X509V3, X509V3_R_UNKNOWN_BIT_STRING_ARGUMENT,
85e1051a39Sopenharmony_ci                           "%s", val->name);
86e1051a39Sopenharmony_ci            ASN1_BIT_STRING_free(bs);
87e1051a39Sopenharmony_ci            return NULL;
88e1051a39Sopenharmony_ci        }
89e1051a39Sopenharmony_ci    }
90e1051a39Sopenharmony_ci    return bs;
91e1051a39Sopenharmony_ci}
92