[331] | 1 | /*
|
---|
| 2 | * Copyright 2004-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the OpenSSL license (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 | /* Copyright (c) 2004 Kungliga Tekniska H旦gskolan
|
---|
| 11 | * (Royal Institute of Technology, Stockholm, Sweden).
|
---|
| 12 | * All rights reserved.
|
---|
| 13 | *
|
---|
| 14 | * Redistribution and use in source and binary forms, with or without
|
---|
| 15 | * modification, are permitted provided that the following conditions
|
---|
| 16 | * are met:
|
---|
| 17 | *
|
---|
| 18 | * 1. Redistributions of source code must retain the above copyright
|
---|
| 19 | * notice, this list of conditions and the following disclaimer.
|
---|
| 20 | *
|
---|
| 21 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 22 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 23 | * documentation and/or other materials provided with the distribution.
|
---|
| 24 | *
|
---|
| 25 | * 3. Neither the name of the Institute nor the names of its contributors
|
---|
| 26 | * may be used to endorse or promote products derived from this software
|
---|
| 27 | * without specific prior written permission.
|
---|
| 28 | *
|
---|
| 29 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
---|
| 30 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
| 32 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
---|
| 33 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
| 34 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
| 35 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
| 36 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
| 37 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
| 38 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
| 39 | * SUCH DAMAGE.
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 | #include <stdio.h>
|
---|
| 43 | #include "internal/cryptlib.h"
|
---|
| 44 | #include <openssl/conf.h>
|
---|
| 45 | #include <openssl/x509v3.h>
|
---|
| 46 | #include "ext_dat.h"
|
---|
| 47 |
|
---|
| 48 | static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *ext,
|
---|
| 49 | BIO *out, int indent);
|
---|
| 50 | static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
|
---|
| 51 | X509V3_CTX *ctx, char *str);
|
---|
| 52 |
|
---|
| 53 | const X509V3_EXT_METHOD v3_pci =
|
---|
| 54 | { NID_proxyCertInfo, 0, ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
|
---|
| 55 | 0, 0, 0, 0,
|
---|
| 56 | 0, 0,
|
---|
| 57 | NULL, NULL,
|
---|
| 58 | (X509V3_EXT_I2R)i2r_pci,
|
---|
| 59 | (X509V3_EXT_R2I)r2i_pci,
|
---|
| 60 | NULL,
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci,
|
---|
| 64 | BIO *out, int indent)
|
---|
| 65 | {
|
---|
| 66 | BIO_printf(out, "%*sPath Length Constraint: ", indent, "");
|
---|
| 67 | if (pci->pcPathLengthConstraint)
|
---|
| 68 | i2a_ASN1_INTEGER(out, pci->pcPathLengthConstraint);
|
---|
| 69 | else
|
---|
| 70 | BIO_printf(out, "infinite");
|
---|
| 71 | BIO_puts(out, "\n");
|
---|
| 72 | BIO_printf(out, "%*sPolicy Language: ", indent, "");
|
---|
| 73 | i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage);
|
---|
| 74 | BIO_puts(out, "\n");
|
---|
| 75 | if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data)
|
---|
| 76 | BIO_printf(out, "%*sPolicy Text: %s\n", indent, "",
|
---|
| 77 | pci->proxyPolicy->policy->data);
|
---|
| 78 | return 1;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | static int process_pci_value(CONF_VALUE *val,
|
---|
| 82 | ASN1_OBJECT **language, ASN1_INTEGER **pathlen,
|
---|
| 83 | ASN1_OCTET_STRING **policy)
|
---|
| 84 | {
|
---|
| 85 | int free_policy = 0;
|
---|
| 86 |
|
---|
| 87 | if (strcmp(val->name, "language") == 0) {
|
---|
| 88 | if (*language) {
|
---|
| 89 | X509V3err(X509V3_F_PROCESS_PCI_VALUE,
|
---|
| 90 | X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED);
|
---|
| 91 | X509V3_conf_err(val);
|
---|
| 92 | return 0;
|
---|
| 93 | }
|
---|
| 94 | if ((*language = OBJ_txt2obj(val->value, 0)) == NULL) {
|
---|
| 95 | X509V3err(X509V3_F_PROCESS_PCI_VALUE,
|
---|
| 96 | X509V3_R_INVALID_OBJECT_IDENTIFIER);
|
---|
| 97 | X509V3_conf_err(val);
|
---|
| 98 | return 0;
|
---|
| 99 | }
|
---|
| 100 | } else if (strcmp(val->name, "pathlen") == 0) {
|
---|
| 101 | if (*pathlen) {
|
---|
| 102 | X509V3err(X509V3_F_PROCESS_PCI_VALUE,
|
---|
| 103 | X509V3_R_POLICY_PATH_LENGTH_ALREADY_DEFINED);
|
---|
| 104 | X509V3_conf_err(val);
|
---|
| 105 | return 0;
|
---|
| 106 | }
|
---|
| 107 | if (!X509V3_get_value_int(val, pathlen)) {
|
---|
| 108 | X509V3err(X509V3_F_PROCESS_PCI_VALUE,
|
---|
| 109 | X509V3_R_POLICY_PATH_LENGTH);
|
---|
| 110 | X509V3_conf_err(val);
|
---|
| 111 | return 0;
|
---|
| 112 | }
|
---|
| 113 | } else if (strcmp(val->name, "policy") == 0) {
|
---|
| 114 | unsigned char *tmp_data = NULL;
|
---|
| 115 | long val_len;
|
---|
| 116 | if (!*policy) {
|
---|
| 117 | *policy = ASN1_OCTET_STRING_new();
|
---|
| 118 | if (*policy == NULL) {
|
---|
| 119 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
|
---|
| 120 | X509V3_conf_err(val);
|
---|
| 121 | return 0;
|
---|
| 122 | }
|
---|
| 123 | free_policy = 1;
|
---|
| 124 | }
|
---|
| 125 | if (strncmp(val->value, "hex:", 4) == 0) {
|
---|
| 126 | unsigned char *tmp_data2 =
|
---|
| 127 | OPENSSL_hexstr2buf(val->value + 4, &val_len);
|
---|
| 128 |
|
---|
| 129 | if (!tmp_data2) {
|
---|
| 130 | X509V3_conf_err(val);
|
---|
| 131 | goto err;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | tmp_data = OPENSSL_realloc((*policy)->data,
|
---|
| 135 | (*policy)->length + val_len + 1);
|
---|
| 136 | if (tmp_data) {
|
---|
| 137 | (*policy)->data = tmp_data;
|
---|
| 138 | memcpy(&(*policy)->data[(*policy)->length],
|
---|
| 139 | tmp_data2, val_len);
|
---|
| 140 | (*policy)->length += val_len;
|
---|
| 141 | (*policy)->data[(*policy)->length] = '\0';
|
---|
| 142 | } else {
|
---|
| 143 | OPENSSL_free(tmp_data2);
|
---|
| 144 | /*
|
---|
| 145 | * realloc failure implies the original data space is b0rked
|
---|
| 146 | * too!
|
---|
| 147 | */
|
---|
| 148 | OPENSSL_free((*policy)->data);
|
---|
| 149 | (*policy)->data = NULL;
|
---|
| 150 | (*policy)->length = 0;
|
---|
| 151 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
|
---|
| 152 | X509V3_conf_err(val);
|
---|
| 153 | goto err;
|
---|
| 154 | }
|
---|
| 155 | OPENSSL_free(tmp_data2);
|
---|
| 156 | } else if (strncmp(val->value, "file:", 5) == 0) {
|
---|
| 157 | unsigned char buf[2048];
|
---|
| 158 | int n;
|
---|
| 159 | BIO *b = BIO_new_file(val->value + 5, "r");
|
---|
| 160 | if (!b) {
|
---|
| 161 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_BIO_LIB);
|
---|
| 162 | X509V3_conf_err(val);
|
---|
| 163 | goto err;
|
---|
| 164 | }
|
---|
| 165 | while ((n = BIO_read(b, buf, sizeof(buf))) > 0
|
---|
| 166 | || (n == 0 && BIO_should_retry(b))) {
|
---|
| 167 | if (!n)
|
---|
| 168 | continue;
|
---|
| 169 |
|
---|
| 170 | tmp_data = OPENSSL_realloc((*policy)->data,
|
---|
| 171 | (*policy)->length + n + 1);
|
---|
| 172 |
|
---|
| 173 | if (!tmp_data) {
|
---|
| 174 | OPENSSL_free((*policy)->data);
|
---|
| 175 | (*policy)->data = NULL;
|
---|
| 176 | (*policy)->length = 0;
|
---|
| 177 | X509V3err(X509V3_F_PROCESS_PCI_VALUE,
|
---|
| 178 | ERR_R_MALLOC_FAILURE);
|
---|
| 179 | X509V3_conf_err(val);
|
---|
| 180 | BIO_free_all(b);
|
---|
| 181 | goto err;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | (*policy)->data = tmp_data;
|
---|
| 185 | memcpy(&(*policy)->data[(*policy)->length], buf, n);
|
---|
| 186 | (*policy)->length += n;
|
---|
| 187 | (*policy)->data[(*policy)->length] = '\0';
|
---|
| 188 | }
|
---|
| 189 | BIO_free_all(b);
|
---|
| 190 |
|
---|
| 191 | if (n < 0) {
|
---|
| 192 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_BIO_LIB);
|
---|
| 193 | X509V3_conf_err(val);
|
---|
| 194 | goto err;
|
---|
| 195 | }
|
---|
| 196 | } else if (strncmp(val->value, "text:", 5) == 0) {
|
---|
| 197 | val_len = strlen(val->value + 5);
|
---|
| 198 | tmp_data = OPENSSL_realloc((*policy)->data,
|
---|
| 199 | (*policy)->length + val_len + 1);
|
---|
| 200 | if (tmp_data) {
|
---|
| 201 | (*policy)->data = tmp_data;
|
---|
| 202 | memcpy(&(*policy)->data[(*policy)->length],
|
---|
| 203 | val->value + 5, val_len);
|
---|
| 204 | (*policy)->length += val_len;
|
---|
| 205 | (*policy)->data[(*policy)->length] = '\0';
|
---|
| 206 | } else {
|
---|
| 207 | /*
|
---|
| 208 | * realloc failure implies the original data space is b0rked
|
---|
| 209 | * too!
|
---|
| 210 | */
|
---|
| 211 | OPENSSL_free((*policy)->data);
|
---|
| 212 | (*policy)->data = NULL;
|
---|
| 213 | (*policy)->length = 0;
|
---|
| 214 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
|
---|
| 215 | X509V3_conf_err(val);
|
---|
| 216 | goto err;
|
---|
| 217 | }
|
---|
| 218 | } else {
|
---|
| 219 | X509V3err(X509V3_F_PROCESS_PCI_VALUE,
|
---|
| 220 | X509V3_R_INCORRECT_POLICY_SYNTAX_TAG);
|
---|
| 221 | X509V3_conf_err(val);
|
---|
| 222 | goto err;
|
---|
| 223 | }
|
---|
| 224 | if (!tmp_data) {
|
---|
| 225 | X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
|
---|
| 226 | X509V3_conf_err(val);
|
---|
| 227 | goto err;
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 | return 1;
|
---|
| 231 | err:
|
---|
| 232 | if (free_policy) {
|
---|
| 233 | ASN1_OCTET_STRING_free(*policy);
|
---|
| 234 | *policy = NULL;
|
---|
| 235 | }
|
---|
| 236 | return 0;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
|
---|
| 240 | X509V3_CTX *ctx, char *value)
|
---|
| 241 | {
|
---|
| 242 | PROXY_CERT_INFO_EXTENSION *pci = NULL;
|
---|
| 243 | STACK_OF(CONF_VALUE) *vals;
|
---|
| 244 | ASN1_OBJECT *language = NULL;
|
---|
| 245 | ASN1_INTEGER *pathlen = NULL;
|
---|
| 246 | ASN1_OCTET_STRING *policy = NULL;
|
---|
| 247 | int i, j;
|
---|
| 248 |
|
---|
| 249 | vals = X509V3_parse_list(value);
|
---|
| 250 | for (i = 0; i < sk_CONF_VALUE_num(vals); i++) {
|
---|
| 251 | CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i);
|
---|
| 252 | if (!cnf->name || (*cnf->name != '@' && !cnf->value)) {
|
---|
| 253 | X509V3err(X509V3_F_R2I_PCI,
|
---|
| 254 | X509V3_R_INVALID_PROXY_POLICY_SETTING);
|
---|
| 255 | X509V3_conf_err(cnf);
|
---|
| 256 | goto err;
|
---|
| 257 | }
|
---|
| 258 | if (*cnf->name == '@') {
|
---|
| 259 | STACK_OF(CONF_VALUE) *sect;
|
---|
| 260 | int success_p = 1;
|
---|
| 261 |
|
---|
| 262 | sect = X509V3_get_section(ctx, cnf->name + 1);
|
---|
| 263 | if (!sect) {
|
---|
| 264 | X509V3err(X509V3_F_R2I_PCI, X509V3_R_INVALID_SECTION);
|
---|
| 265 | X509V3_conf_err(cnf);
|
---|
| 266 | goto err;
|
---|
| 267 | }
|
---|
| 268 | for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++) {
|
---|
| 269 | success_p =
|
---|
| 270 | process_pci_value(sk_CONF_VALUE_value(sect, j),
|
---|
| 271 | &language, &pathlen, &policy);
|
---|
| 272 | }
|
---|
| 273 | X509V3_section_free(ctx, sect);
|
---|
| 274 | if (!success_p)
|
---|
| 275 | goto err;
|
---|
| 276 | } else {
|
---|
| 277 | if (!process_pci_value(cnf, &language, &pathlen, &policy)) {
|
---|
| 278 | X509V3_conf_err(cnf);
|
---|
| 279 | goto err;
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 |
|
---|
| 284 | /* Language is mandatory */
|
---|
| 285 | if (!language) {
|
---|
| 286 | X509V3err(X509V3_F_R2I_PCI,
|
---|
| 287 | X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED);
|
---|
| 288 | goto err;
|
---|
| 289 | }
|
---|
| 290 | i = OBJ_obj2nid(language);
|
---|
| 291 | if ((i == NID_Independent || i == NID_id_ppl_inheritAll) && policy) {
|
---|
| 292 | X509V3err(X509V3_F_R2I_PCI,
|
---|
| 293 | X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY);
|
---|
| 294 | goto err;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | pci = PROXY_CERT_INFO_EXTENSION_new();
|
---|
| 298 | if (pci == NULL) {
|
---|
| 299 | X509V3err(X509V3_F_R2I_PCI, ERR_R_MALLOC_FAILURE);
|
---|
| 300 | goto err;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | pci->proxyPolicy->policyLanguage = language;
|
---|
| 304 | language = NULL;
|
---|
| 305 | pci->proxyPolicy->policy = policy;
|
---|
| 306 | policy = NULL;
|
---|
| 307 | pci->pcPathLengthConstraint = pathlen;
|
---|
| 308 | pathlen = NULL;
|
---|
| 309 | goto end;
|
---|
| 310 | err:
|
---|
| 311 | ASN1_OBJECT_free(language);
|
---|
| 312 | ASN1_INTEGER_free(pathlen);
|
---|
| 313 | pathlen = NULL;
|
---|
| 314 | ASN1_OCTET_STRING_free(policy);
|
---|
| 315 | policy = NULL;
|
---|
| 316 | PROXY_CERT_INFO_EXTENSION_free(pci);
|
---|
| 317 | pci = NULL;
|
---|
| 318 | end:
|
---|
| 319 | sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
|
---|
| 320 | return pci;
|
---|
| 321 | }
|
---|