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