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