| 1 | /* |
| 2 | * Copyright 2015-2018 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 "e_os.h" |
| 11 | #include "internal/cryptlib.h" |
| 12 | #include <stdio.h> |
| 13 | #include <openssl/asn1t.h> |
| 14 | #include <openssl/conf.h> |
| 15 | #include <openssl/x509v3.h> |
| 16 | #include "ext_dat.h" |
| 17 | |
| 18 | static STACK_OF(CONF_VALUE) *i2v_TLS_FEATURE(const X509V3_EXT_METHOD *method, |
| 19 | TLS_FEATURE *tls_feature, |
| 20 | STACK_OF(CONF_VALUE) *ext_list); |
| 21 | static TLS_FEATURE *v2i_TLS_FEATURE(const X509V3_EXT_METHOD *method, |
| 22 | X509V3_CTX *ctx, |
| 23 | STACK_OF(CONF_VALUE) *nval); |
| 24 | |
| 25 | ASN1_ITEM_TEMPLATE(TLS_FEATURE) = |
| 26 | ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, TLS_FEATURE, ASN1_INTEGER) |
| 27 | static_ASN1_ITEM_TEMPLATE_END(TLS_FEATURE) |
| 28 | |
| 29 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(TLS_FEATURE) |
| 30 | |
| 31 | const X509V3_EXT_METHOD v3_tls_feature = { |
| 32 | NID_tlsfeature, 0, |
| 33 | ASN1_ITEM_ref(TLS_FEATURE), |
| 34 | 0, 0, 0, 0, |
| 35 | 0, 0, |
| 36 | (X509V3_EXT_I2V)i2v_TLS_FEATURE, |
| 37 | (X509V3_EXT_V2I)v2i_TLS_FEATURE, |
| 38 | 0, 0, |
| 39 | NULL |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | typedef struct { |
| 44 | long num; |
| 45 | const char *name; |
| 46 | } TLS_FEATURE_NAME; |
| 47 | |
| 48 | static TLS_FEATURE_NAME tls_feature_tbl[] = { |
| 49 | { 5, "status_request" }, |
| 50 | { 17, "status_request_v2" } |
| 51 | }; |
| 52 | |
| 53 | /* |
| 54 | * i2v_TLS_FEATURE converts the TLS_FEATURE structure tls_feature into the |
| 55 | * STACK_OF(CONF_VALUE) structure ext_list. STACK_OF(CONF_VALUE) is the format |
| 56 | * used by the CONF library to represent a multi-valued extension. ext_list is |
| 57 | * returned. |
| 58 | */ |
| 59 | static STACK_OF(CONF_VALUE) *i2v_TLS_FEATURE(const X509V3_EXT_METHOD *method, |
| 60 | TLS_FEATURE *tls_feature, |
| 61 | STACK_OF(CONF_VALUE) *ext_list) |
| 62 | { |
| 63 | int i; |
| 64 | size_t j; |
| 65 | ASN1_INTEGER *ai; |
| 66 | long tlsextid; |
| 67 | for (i = 0; i < sk_ASN1_INTEGER_num(tls_feature); i++) { |
| 68 | ai = sk_ASN1_INTEGER_value(tls_feature, i); |
| 69 | tlsextid = ASN1_INTEGER_get(ai); |
| 70 | for (j = 0; j < OSSL_NELEM(tls_feature_tbl); j++) |
| 71 | if (tlsextid == tls_feature_tbl[j].num) |
| 72 | break; |
| 73 | if (j < OSSL_NELEM(tls_feature_tbl)) |
| 74 | X509V3_add_value(NULL, tls_feature_tbl[j].name, &ext_list); |
| 75 | else |
| 76 | X509V3_add_value_int(NULL, ai, &ext_list); |
| 77 | } |
| 78 | return ext_list; |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * v2i_TLS_FEATURE converts the multi-valued extension nval into a TLS_FEATURE |
| 83 | * structure, which is returned if the conversion is successful. In case of |
| 84 | * error, NULL is returned. |
| 85 | */ |
| 86 | static TLS_FEATURE *v2i_TLS_FEATURE(const X509V3_EXT_METHOD *method, |
| 87 | X509V3_CTX *ctx, STACK_OF(CONF_VALUE) *nval) |
| 88 | { |
| 89 | TLS_FEATURE *tlsf; |
| 90 | char *extval, *endptr; |
| 91 | ASN1_INTEGER *ai; |
| 92 | CONF_VALUE *val; |
| 93 | int i; |
| 94 | size_t j; |
| 95 | long tlsextid; |
| 96 | |
| 97 | if ((tlsf = sk_ASN1_INTEGER_new_null()) == NULL) { |
| 98 | X509V3err(X509V3_F_V2I_TLS_FEATURE, ERR_R_MALLOC_FAILURE); |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | for (i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
| 103 | val = sk_CONF_VALUE_value(nval, i); |
| 104 | if (val->value) |
| 105 | extval = val->value; |
| 106 | else |
| 107 | extval = val->name; |
| 108 | |
| 109 | for (j = 0; j < OSSL_NELEM(tls_feature_tbl); j++) |
| 110 | if (strcasecmp(extval, tls_feature_tbl[j].name) == 0) |
| 111 | break; |
| 112 | if (j < OSSL_NELEM(tls_feature_tbl)) |
| 113 | tlsextid = tls_feature_tbl[j].num; |
| 114 | else { |
| 115 | tlsextid = strtol(extval, &endptr, 10); |
| 116 | if (((*endptr) != '\0') || (extval == endptr) || (tlsextid < 0) || |
| 117 | (tlsextid > 65535)) { |
| 118 | X509V3err(X509V3_F_V2I_TLS_FEATURE, X509V3_R_INVALID_SYNTAX); |
| 119 | X509V3_conf_err(val); |
| 120 | goto err; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if ((ai = ASN1_INTEGER_new()) == NULL |
| 125 | || !ASN1_INTEGER_set(ai, tlsextid) |
| 126 | || sk_ASN1_INTEGER_push(tlsf, ai) <= 0) { |
| 127 | X509V3err(X509V3_F_V2I_TLS_FEATURE, ERR_R_MALLOC_FAILURE); |
| 128 | goto err; |
| 129 | } |
| 130 | } |
| 131 | return tlsf; |
| 132 | |
| 133 | err: |
| 134 | sk_ASN1_INTEGER_pop_free(tlsf, ASN1_INTEGER_free); |
| 135 | return NULL; |
| 136 | } |
| 137 | |