| 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/asn1.h> |
| 19 | #include <openssl/asn1t.h> |
| 20 | #include <openssl/conf.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/mem.h> |
| 23 | #include <openssl/obj.h> |
| 24 | #include <openssl/x509.h> |
| 25 |
| 26 | #include "ext_dat.h" |
| 27 | #include "internal.h" |
| 28 |
| 29 |
| 30 | static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS( |
| 31 | const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *ret); |
| 32 | static void *v2i_AUTHORITY_INFO_ACCESS(const X509V3_EXT_METHOD *method, |
| 33 | const X509V3_CTX *ctx, |
| 34 | const STACK_OF(CONF_VALUE) *nval); |
| 35 |
| 36 | const X509V3_EXT_METHOD v3_info = { |
| 37 | NID_info_access, |
| 38 | X509V3_EXT_MULTILINE, |
| 39 | ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS), |
| 40 | 0, |
| 41 | 0, |
| 42 | 0, |
| 43 | 0, |
| 44 | 0, |
| 45 | 0, |
| 46 | i2v_AUTHORITY_INFO_ACCESS, |
| 47 | v2i_AUTHORITY_INFO_ACCESS, |
| 48 | 0, |
| 49 | 0, |
| 50 | NULL, |
| 51 | }; |
| 52 |
| 53 | const X509V3_EXT_METHOD v3_sinfo = { |
| 54 | NID_sinfo_access, |
| 55 | X509V3_EXT_MULTILINE, |
| 56 | ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS), |
| 57 | 0, |
| 58 | 0, |
| 59 | 0, |
| 60 | 0, |
| 61 | 0, |
| 62 | 0, |
| 63 | i2v_AUTHORITY_INFO_ACCESS, |
| 64 | v2i_AUTHORITY_INFO_ACCESS, |
| 65 | 0, |
| 66 | 0, |
| 67 | NULL, |
| 68 | }; |
| 69 |
| 70 | ASN1_SEQUENCE(ACCESS_DESCRIPTION) = { |
| 71 | ASN1_SIMPLE(ACCESS_DESCRIPTION, method, ASN1_OBJECT), |
| 72 | ASN1_SIMPLE(ACCESS_DESCRIPTION, location, GENERAL_NAME), |
| 73 | } ASN1_SEQUENCE_END(ACCESS_DESCRIPTION) |
| 74 |
| 75 | IMPLEMENT_ASN1_ALLOC_FUNCTIONS(ACCESS_DESCRIPTION) |
| 76 |
| 77 | ASN1_ITEM_TEMPLATE(AUTHORITY_INFO_ACCESS) = ASN1_EX_TEMPLATE_TYPE( |
| 78 | ASN1_TFLG_SEQUENCE_OF, 0, GeneralNames, ACCESS_DESCRIPTION) |
| 79 | ASN1_ITEM_TEMPLATE_END(AUTHORITY_INFO_ACCESS) |
| 80 |
| 81 | IMPLEMENT_ASN1_FUNCTIONS(AUTHORITY_INFO_ACCESS) |
| 82 |
| 83 | static STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS( |
| 84 | const X509V3_EXT_METHOD *method, void *ext, STACK_OF(CONF_VALUE) *ret) { |
| 85 | const AUTHORITY_INFO_ACCESS *ainfo = |
| 86 | reinterpret_cast<const AUTHORITY_INFO_ACCESS *>(ext); |
| 87 | ACCESS_DESCRIPTION *desc; |
| 88 | char objtmp[80], *name; |
| 89 | CONF_VALUE *vtmp; |
| 90 | STACK_OF(CONF_VALUE) *tret = ret; |
| 91 |
| 92 | for (size_t i = 0; i < sk_ACCESS_DESCRIPTION_num(ainfo); i++) { |
| 93 | STACK_OF(CONF_VALUE) *tmp; |
| 94 |
| 95 | desc = sk_ACCESS_DESCRIPTION_value(ainfo, i); |
| 96 | tmp = i2v_GENERAL_NAME(method, desc->location, tret); |
| 97 | if (tmp == NULL) { |
| 98 | goto err; |
| 99 | } |
| 100 | tret = tmp; |
| 101 | vtmp = sk_CONF_VALUE_value(tret, i); |
| 102 | i2t_ASN1_OBJECT(objtmp, sizeof objtmp, desc->method); |
| 103 |
| 104 | if (OPENSSL_asprintf(&name, "%s - %s", objtmp, vtmp->name) == -1) { |
| 105 | goto err; |
| 106 | } |
| 107 | OPENSSL_free(vtmp->name); |
| 108 | vtmp->name = name; |
| 109 | } |
| 110 | if (ret == NULL && tret == NULL) { |
| 111 | return sk_CONF_VALUE_new_null(); |
| 112 | } |
| 113 |
| 114 | return tret; |
| 115 | err: |
| 116 | if (ret == NULL && tret != NULL) { |
| 117 | sk_CONF_VALUE_pop_free(tret, X509V3_conf_free); |
| 118 | } |
| 119 | return NULL; |
| 120 | } |
| 121 |
| 122 | static void *v2i_AUTHORITY_INFO_ACCESS(const X509V3_EXT_METHOD *method, |
| 123 | const X509V3_CTX *ctx, |
| 124 | const STACK_OF(CONF_VALUE) *nval) { |
| 125 | AUTHORITY_INFO_ACCESS *ainfo = NULL; |
| 126 | ACCESS_DESCRIPTION *acc; |
| 127 | if (!(ainfo = sk_ACCESS_DESCRIPTION_new_null())) { |
| 128 | return NULL; |
| 129 | } |
| 130 | for (size_t i = 0; i < sk_CONF_VALUE_num(nval); i++) { |
| 131 | const CONF_VALUE *cnf = sk_CONF_VALUE_value(nval, i); |
| 132 | if (!(acc = ACCESS_DESCRIPTION_new()) || |
| 133 | !sk_ACCESS_DESCRIPTION_push(ainfo, acc)) { |
| 134 | goto err; |
| 135 | } |
| 136 | char *ptmp = strchr(cnf->name, ';'); |
| 137 | if (!ptmp) { |
| 138 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_SYNTAX); |
| 139 | goto err; |
| 140 | } |
| 141 | CONF_VALUE ctmp; |
| 142 | ctmp.name = ptmp + 1; |
| 143 | ctmp.value = cnf->value; |
| 144 | if (!v2i_GENERAL_NAME_ex(acc->location, method, ctx, &ctmp, 0)) { |
| 145 | goto err; |
| 146 | } |
| 147 | char *objtmp = OPENSSL_strndup(cnf->name, ptmp - cnf->name); |
| 148 | if (objtmp == NULL) { |
| 149 | goto err; |
| 150 | } |
| 151 | acc->method = OBJ_txt2obj(objtmp, 0); |
| 152 | if (!acc->method) { |
| 153 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_BAD_OBJECT); |
| 154 | ERR_add_error_data(2, "value=", objtmp); |
| 155 | OPENSSL_free(objtmp); |
| 156 | goto err; |
| 157 | } |
| 158 | OPENSSL_free(objtmp); |
| 159 | } |
| 160 | return ainfo; |
| 161 | err: |
| 162 | sk_ACCESS_DESCRIPTION_pop_free(ainfo, ACCESS_DESCRIPTION_free); |
| 163 | return NULL; |
| 164 | } |
| 165 |