sl@0: /* v3_pci.c -*- mode:C; c-file-style: "eay" -*- */ sl@0: /* Contributed to the OpenSSL Project 2004 sl@0: * by Richard Levitte (richard@levitte.org) sl@0: */ sl@0: /* Copyright (c) 2004 Kungliga Tekniska Högskolan sl@0: * (Royal Institute of Technology, Stockholm, Sweden). sl@0: * All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * sl@0: * 3. Neither the name of the Institute nor the names of its contributors sl@0: * may be used to endorse or promote products derived from this software sl@0: * without specific prior written permission. sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: #include sl@0: #include "cryptlib.h" sl@0: #include sl@0: #include sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: sl@0: static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *ext, sl@0: BIO *out, int indent); sl@0: static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method, sl@0: X509V3_CTX *ctx, char *str); sl@0: #ifndef EMULATOR sl@0: X509V3_EXT_METHOD v3_pci = sl@0: { NID_proxyCertInfo, 0, ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION), sl@0: 0,0,0,0, sl@0: 0,0, sl@0: NULL, NULL, sl@0: (X509V3_EXT_I2R)i2r_pci, sl@0: (X509V3_EXT_R2I)r2i_pci, sl@0: NULL, sl@0: }; sl@0: #else sl@0: const X509V3_EXT_METHOD v3_pci = sl@0: { NID_proxyCertInfo, 0, ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION), sl@0: 0,0,0,0, sl@0: 0,0, sl@0: NULL, NULL, sl@0: (X509V3_EXT_I2R)i2r_pci, sl@0: (X509V3_EXT_R2I)r2i_pci, sl@0: NULL, sl@0: }; sl@0: #endif sl@0: static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci, sl@0: BIO *out, int indent) sl@0: { sl@0: BIO_printf(out, "%*sPath Length Constraint: ", indent, ""); sl@0: if (pci->pcPathLengthConstraint) sl@0: i2a_ASN1_INTEGER(out, pci->pcPathLengthConstraint); sl@0: else sl@0: BIO_printf(out, "infinite"); sl@0: BIO_puts(out, "\n"); sl@0: BIO_printf(out, "%*sPolicy Language: ", indent, ""); sl@0: i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage); sl@0: BIO_puts(out, "\n"); sl@0: if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data) sl@0: BIO_printf(out, "%*sPolicy Text: %s\n", indent, "", sl@0: pci->proxyPolicy->policy->data); sl@0: return 1; sl@0: } sl@0: sl@0: static int process_pci_value(CONF_VALUE *val, sl@0: ASN1_OBJECT **language, ASN1_INTEGER **pathlen, sl@0: ASN1_OCTET_STRING **policy) sl@0: { sl@0: int free_policy = 0; sl@0: sl@0: if (strcmp(val->name, "language") == 0) sl@0: { sl@0: if (*language) sl@0: { sl@0: X509V3err(X509V3_F_PROCESS_PCI_VALUE,X509V3_R_POLICY_LANGUAGE_ALREADTY_DEFINED); sl@0: X509V3_conf_err(val); sl@0: return 0; sl@0: } sl@0: if (!(*language = OBJ_txt2obj(val->value, 0))) sl@0: { sl@0: X509V3err(X509V3_F_PROCESS_PCI_VALUE,X509V3_R_INVALID_OBJECT_IDENTIFIER); sl@0: X509V3_conf_err(val); sl@0: return 0; sl@0: } sl@0: } sl@0: else if (strcmp(val->name, "pathlen") == 0) sl@0: { sl@0: if (*pathlen) sl@0: { sl@0: X509V3err(X509V3_F_PROCESS_PCI_VALUE,X509V3_R_POLICY_PATH_LENGTH_ALREADTY_DEFINED); sl@0: X509V3_conf_err(val); sl@0: return 0; sl@0: } sl@0: if (!X509V3_get_value_int(val, pathlen)) sl@0: { sl@0: X509V3err(X509V3_F_PROCESS_PCI_VALUE,X509V3_R_POLICY_PATH_LENGTH); sl@0: X509V3_conf_err(val); sl@0: return 0; sl@0: } sl@0: } sl@0: else if (strcmp(val->name, "policy") == 0) sl@0: { sl@0: unsigned char *tmp_data = NULL; sl@0: long val_len; sl@0: if (!*policy) sl@0: { sl@0: *policy = ASN1_OCTET_STRING_new(); sl@0: if (!*policy) sl@0: { sl@0: X509V3err(X509V3_F_PROCESS_PCI_VALUE,ERR_R_MALLOC_FAILURE); sl@0: X509V3_conf_err(val); sl@0: return 0; sl@0: } sl@0: free_policy = 1; sl@0: } sl@0: if (strncmp(val->value, "hex:", 4) == 0) sl@0: { sl@0: unsigned char *tmp_data2 = sl@0: string_to_hex(val->value + 4, &val_len); sl@0: sl@0: if (!tmp_data2) goto err; sl@0: sl@0: tmp_data = OPENSSL_realloc((*policy)->data, sl@0: (*policy)->length + val_len + 1); sl@0: if (tmp_data) sl@0: { sl@0: (*policy)->data = tmp_data; sl@0: memcpy(&(*policy)->data[(*policy)->length], sl@0: tmp_data2, val_len); sl@0: (*policy)->length += val_len; sl@0: (*policy)->data[(*policy)->length] = '\0'; sl@0: } sl@0: } sl@0: else if (strncmp(val->value, "file:", 5) == 0) sl@0: { sl@0: #ifndef SYMBIAN sl@0: unsigned char buf[2048]; sl@0: #else sl@0: unsigned char buf[1024]; sl@0: #endif sl@0: int n; sl@0: BIO *b = BIO_new_file(val->value + 5, "r"); sl@0: if (!b) sl@0: { sl@0: X509V3err(X509V3_F_PROCESS_PCI_VALUE,ERR_R_BIO_LIB); sl@0: X509V3_conf_err(val); sl@0: goto err; sl@0: } sl@0: while((n = BIO_read(b, buf, sizeof(buf))) > 0 sl@0: || (n == 0 && BIO_should_retry(b))) sl@0: { sl@0: if (!n) continue; sl@0: sl@0: tmp_data = OPENSSL_realloc((*policy)->data, sl@0: (*policy)->length + n + 1); sl@0: sl@0: if (!tmp_data) sl@0: break; sl@0: sl@0: (*policy)->data = tmp_data; sl@0: memcpy(&(*policy)->data[(*policy)->length], sl@0: buf, n); sl@0: (*policy)->length += n; sl@0: (*policy)->data[(*policy)->length] = '\0'; sl@0: } sl@0: sl@0: if (n < 0) sl@0: { sl@0: X509V3err(X509V3_F_PROCESS_PCI_VALUE,ERR_R_BIO_LIB); sl@0: X509V3_conf_err(val); sl@0: goto err; sl@0: } sl@0: } sl@0: else if (strncmp(val->value, "text:", 5) == 0) sl@0: { sl@0: val_len = strlen(val->value + 5); sl@0: tmp_data = OPENSSL_realloc((*policy)->data, sl@0: (*policy)->length + val_len + 1); sl@0: if (tmp_data) sl@0: { sl@0: (*policy)->data = tmp_data; sl@0: memcpy(&(*policy)->data[(*policy)->length], sl@0: val->value + 5, val_len); sl@0: (*policy)->length += val_len; sl@0: (*policy)->data[(*policy)->length] = '\0'; sl@0: } sl@0: } sl@0: else sl@0: { sl@0: X509V3err(X509V3_F_PROCESS_PCI_VALUE,X509V3_R_INCORRECT_POLICY_SYNTAX_TAG); sl@0: X509V3_conf_err(val); sl@0: goto err; sl@0: } sl@0: if (!tmp_data) sl@0: { sl@0: X509V3err(X509V3_F_PROCESS_PCI_VALUE,ERR_R_MALLOC_FAILURE); sl@0: X509V3_conf_err(val); sl@0: goto err; sl@0: } sl@0: } sl@0: return 1; sl@0: err: sl@0: if (free_policy) sl@0: { sl@0: ASN1_OCTET_STRING_free(*policy); sl@0: *policy = NULL; sl@0: } sl@0: return 0; sl@0: } sl@0: sl@0: static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method, sl@0: X509V3_CTX *ctx, char *value) sl@0: { sl@0: PROXY_CERT_INFO_EXTENSION *pci = NULL; sl@0: STACK_OF(CONF_VALUE) *vals; sl@0: ASN1_OBJECT *language = NULL; sl@0: ASN1_INTEGER *pathlen = NULL; sl@0: ASN1_OCTET_STRING *policy = NULL; sl@0: int i, j; sl@0: sl@0: vals = X509V3_parse_list(value); sl@0: for (i = 0; i < sk_CONF_VALUE_num(vals); i++) sl@0: { sl@0: CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i); sl@0: if (!cnf->name || (*cnf->name != '@' && !cnf->value)) sl@0: { sl@0: X509V3err(X509V3_F_R2I_PCI,X509V3_R_INVALID_PROXY_POLICY_SETTING); sl@0: X509V3_conf_err(cnf); sl@0: goto err; sl@0: } sl@0: if (*cnf->name == '@') sl@0: { sl@0: STACK_OF(CONF_VALUE) *sect; sl@0: int success_p = 1; sl@0: sl@0: sect = X509V3_get_section(ctx, cnf->name + 1); sl@0: if (!sect) sl@0: { sl@0: X509V3err(X509V3_F_R2I_PCI,X509V3_R_INVALID_SECTION); sl@0: X509V3_conf_err(cnf); sl@0: goto err; sl@0: } sl@0: for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++) sl@0: { sl@0: success_p = sl@0: process_pci_value(sk_CONF_VALUE_value(sect, j), sl@0: &language, &pathlen, &policy); sl@0: } sl@0: X509V3_section_free(ctx, sect); sl@0: if (!success_p) sl@0: goto err; sl@0: } sl@0: else sl@0: { sl@0: if (!process_pci_value(cnf, sl@0: &language, &pathlen, &policy)) sl@0: { sl@0: X509V3_conf_err(cnf); sl@0: goto err; sl@0: } sl@0: } sl@0: } sl@0: sl@0: /* Language is mandatory */ sl@0: if (!language) sl@0: { sl@0: X509V3err(X509V3_F_R2I_PCI,X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED); sl@0: goto err; sl@0: } sl@0: i = OBJ_obj2nid(language); sl@0: if ((i == NID_Independent || i == NID_id_ppl_inheritAll) && policy) sl@0: { sl@0: X509V3err(X509V3_F_R2I_PCI,X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY); sl@0: goto err; sl@0: } sl@0: sl@0: pci = PROXY_CERT_INFO_EXTENSION_new(); sl@0: if (!pci) sl@0: { sl@0: X509V3err(X509V3_F_R2I_PCI,ERR_R_MALLOC_FAILURE); sl@0: goto err; sl@0: } sl@0: sl@0: pci->proxyPolicy->policyLanguage = language; language = NULL; sl@0: pci->proxyPolicy->policy = policy; policy = NULL; sl@0: pci->pcPathLengthConstraint = pathlen; pathlen = NULL; sl@0: goto end; sl@0: err: sl@0: if (language) { ASN1_OBJECT_free(language); language = NULL; } sl@0: if (pathlen) { ASN1_INTEGER_free(pathlen); pathlen = NULL; } sl@0: if (policy) { ASN1_OCTET_STRING_free(policy); policy = NULL; } sl@0: if (pci) { PROXY_CERT_INFO_EXTENSION_free(pci); pci = NULL; } sl@0: end: sl@0: sk_CONF_VALUE_pop_free(vals, X509V3_conf_free); sl@0: return pci; sl@0: }