os/ossrv/ssl/libcrypto/src/crypto/x509v3/v3_pci.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/x509v3/v3_pci.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,323 @@
     1.4 +/* v3_pci.c -*- mode:C; c-file-style: "eay" -*- */
     1.5 +/* Contributed to the OpenSSL Project 2004
     1.6 + * by Richard Levitte (richard@levitte.org)
     1.7 + */
     1.8 +/* Copyright (c) 2004 Kungliga Tekniska Högskolan
     1.9 + * (Royal Institute of Technology, Stockholm, Sweden).
    1.10 + * All rights reserved.
    1.11 + *
    1.12 + * Redistribution and use in source and binary forms, with or without
    1.13 + * modification, are permitted provided that the following conditions
    1.14 + * are met:
    1.15 + *
    1.16 + * 1. Redistributions of source code must retain the above copyright
    1.17 + *    notice, this list of conditions and the following disclaimer.
    1.18 + *
    1.19 + * 2. Redistributions in binary form must reproduce the above copyright
    1.20 + *    notice, this list of conditions and the following disclaimer in the
    1.21 + *    documentation and/or other materials provided with the distribution.
    1.22 + *
    1.23 + * 3. Neither the name of the Institute nor the names of its contributors
    1.24 + *    may be used to endorse or promote products derived from this software
    1.25 + *    without specific prior written permission.
    1.26 + *
    1.27 + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
    1.28 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.29 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.30 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
    1.31 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.32 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.33 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.34 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.35 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.36 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.37 + * SUCH DAMAGE.
    1.38 + */
    1.39 +/*
    1.40 + © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    1.41 + */
    1.42 +#include <stdio.h>
    1.43 +#include "cryptlib.h"
    1.44 +#include <openssl/conf.h>
    1.45 +#include <openssl/x509v3.h>
    1.46 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
    1.47 +#include "libcrypto_wsd_macros.h"
    1.48 +#include "libcrypto_wsd.h"
    1.49 +#endif
    1.50 +
    1.51 +
    1.52 +static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *ext,
    1.53 +	BIO *out, int indent);
    1.54 +static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
    1.55 +	X509V3_CTX *ctx, char *str);
    1.56 +#ifndef EMULATOR
    1.57 +X509V3_EXT_METHOD v3_pci =
    1.58 +	{ NID_proxyCertInfo, 0, ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
    1.59 +	  0,0,0,0,
    1.60 +	  0,0,
    1.61 +	  NULL, NULL,
    1.62 +	  (X509V3_EXT_I2R)i2r_pci,
    1.63 +	  (X509V3_EXT_R2I)r2i_pci,
    1.64 +	  NULL,
    1.65 +	};
    1.66 +#else
    1.67 +const X509V3_EXT_METHOD v3_pci =
    1.68 +	{ NID_proxyCertInfo, 0, ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
    1.69 +	  0,0,0,0,
    1.70 +	  0,0,
    1.71 +	  NULL, NULL,
    1.72 +	  (X509V3_EXT_I2R)i2r_pci,
    1.73 +	  (X509V3_EXT_R2I)r2i_pci,
    1.74 +	  NULL,
    1.75 +	};
    1.76 +#endif
    1.77 +static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci,
    1.78 +	BIO *out, int indent)
    1.79 +	{
    1.80 +	BIO_printf(out, "%*sPath Length Constraint: ", indent, "");
    1.81 +	if (pci->pcPathLengthConstraint)
    1.82 +	  i2a_ASN1_INTEGER(out, pci->pcPathLengthConstraint);
    1.83 +	else
    1.84 +	  BIO_printf(out, "infinite");
    1.85 +	BIO_puts(out, "\n");
    1.86 +	BIO_printf(out, "%*sPolicy Language: ", indent, "");
    1.87 +	i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage);
    1.88 +	BIO_puts(out, "\n");
    1.89 +	if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data)
    1.90 +	  BIO_printf(out, "%*sPolicy Text: %s\n", indent, "",
    1.91 +		     pci->proxyPolicy->policy->data);
    1.92 +	return 1;
    1.93 +	}
    1.94 +
    1.95 +static int process_pci_value(CONF_VALUE *val,
    1.96 +	ASN1_OBJECT **language, ASN1_INTEGER **pathlen,
    1.97 +	ASN1_OCTET_STRING **policy)
    1.98 +	{
    1.99 +	int free_policy = 0;
   1.100 +
   1.101 +	if (strcmp(val->name, "language") == 0)
   1.102 +		{
   1.103 +		if (*language)
   1.104 +			{
   1.105 +			X509V3err(X509V3_F_PROCESS_PCI_VALUE,X509V3_R_POLICY_LANGUAGE_ALREADTY_DEFINED);
   1.106 +			X509V3_conf_err(val);
   1.107 +			return 0;
   1.108 +			}
   1.109 +		if (!(*language = OBJ_txt2obj(val->value, 0)))
   1.110 +			{
   1.111 +			X509V3err(X509V3_F_PROCESS_PCI_VALUE,X509V3_R_INVALID_OBJECT_IDENTIFIER);
   1.112 +			X509V3_conf_err(val);
   1.113 +			return 0;
   1.114 +			}
   1.115 +		}
   1.116 +	else if (strcmp(val->name, "pathlen") == 0)
   1.117 +		{
   1.118 +		if (*pathlen)
   1.119 +			{
   1.120 +			X509V3err(X509V3_F_PROCESS_PCI_VALUE,X509V3_R_POLICY_PATH_LENGTH_ALREADTY_DEFINED);
   1.121 +			X509V3_conf_err(val);
   1.122 +			return 0;
   1.123 +			}
   1.124 +		if (!X509V3_get_value_int(val, pathlen))
   1.125 +			{
   1.126 +			X509V3err(X509V3_F_PROCESS_PCI_VALUE,X509V3_R_POLICY_PATH_LENGTH);
   1.127 +			X509V3_conf_err(val);
   1.128 +			return 0;
   1.129 +			}
   1.130 +		}
   1.131 +	else if (strcmp(val->name, "policy") == 0)
   1.132 +		{
   1.133 +		unsigned char *tmp_data = NULL;
   1.134 +		long val_len;
   1.135 +		if (!*policy)
   1.136 +			{
   1.137 +			*policy = ASN1_OCTET_STRING_new();
   1.138 +			if (!*policy)
   1.139 +				{
   1.140 +				X509V3err(X509V3_F_PROCESS_PCI_VALUE,ERR_R_MALLOC_FAILURE);
   1.141 +				X509V3_conf_err(val);
   1.142 +				return 0;
   1.143 +				}
   1.144 +			free_policy = 1;
   1.145 +			}
   1.146 +		if (strncmp(val->value, "hex:", 4) == 0)
   1.147 +			{
   1.148 +			unsigned char *tmp_data2 =
   1.149 +				string_to_hex(val->value + 4, &val_len);
   1.150 +
   1.151 +			if (!tmp_data2) goto err;
   1.152 +
   1.153 +			tmp_data = OPENSSL_realloc((*policy)->data,
   1.154 +				(*policy)->length + val_len + 1);
   1.155 +			if (tmp_data)
   1.156 +				{
   1.157 +				(*policy)->data = tmp_data;
   1.158 +				memcpy(&(*policy)->data[(*policy)->length],
   1.159 +					tmp_data2, val_len);
   1.160 +				(*policy)->length += val_len;
   1.161 +				(*policy)->data[(*policy)->length] = '\0';
   1.162 +				}
   1.163 +			}
   1.164 +		else if (strncmp(val->value, "file:", 5) == 0)
   1.165 +			{
   1.166 +#ifndef SYMBIAN			
   1.167 +			unsigned char buf[2048];
   1.168 +#else
   1.169 +      unsigned char buf[1024];
   1.170 +#endif			
   1.171 +			int n;
   1.172 +			BIO *b = BIO_new_file(val->value + 5, "r");
   1.173 +			if (!b)
   1.174 +				{
   1.175 +				X509V3err(X509V3_F_PROCESS_PCI_VALUE,ERR_R_BIO_LIB);
   1.176 +				X509V3_conf_err(val);
   1.177 +				goto err;
   1.178 +				}
   1.179 +			while((n = BIO_read(b, buf, sizeof(buf))) > 0
   1.180 +				|| (n == 0 && BIO_should_retry(b)))
   1.181 +				{
   1.182 +				if (!n) continue;
   1.183 +
   1.184 +				tmp_data = OPENSSL_realloc((*policy)->data,
   1.185 +					(*policy)->length + n + 1);
   1.186 +
   1.187 +				if (!tmp_data)
   1.188 +					break;
   1.189 +
   1.190 +				(*policy)->data = tmp_data;
   1.191 +				memcpy(&(*policy)->data[(*policy)->length],
   1.192 +					buf, n);
   1.193 +				(*policy)->length += n;
   1.194 +				(*policy)->data[(*policy)->length] = '\0';
   1.195 +				}
   1.196 +
   1.197 +			if (n < 0)
   1.198 +				{
   1.199 +				X509V3err(X509V3_F_PROCESS_PCI_VALUE,ERR_R_BIO_LIB);
   1.200 +				X509V3_conf_err(val);
   1.201 +				goto err;
   1.202 +				}
   1.203 +			}
   1.204 +		else if (strncmp(val->value, "text:", 5) == 0)
   1.205 +			{
   1.206 +			val_len = strlen(val->value + 5);
   1.207 +			tmp_data = OPENSSL_realloc((*policy)->data,
   1.208 +				(*policy)->length + val_len + 1);
   1.209 +			if (tmp_data)
   1.210 +				{
   1.211 +				(*policy)->data = tmp_data;
   1.212 +				memcpy(&(*policy)->data[(*policy)->length],
   1.213 +					val->value + 5, val_len);
   1.214 +				(*policy)->length += val_len;
   1.215 +				(*policy)->data[(*policy)->length] = '\0';
   1.216 +				}
   1.217 +			}
   1.218 +		else
   1.219 +			{
   1.220 +			X509V3err(X509V3_F_PROCESS_PCI_VALUE,X509V3_R_INCORRECT_POLICY_SYNTAX_TAG);
   1.221 +			X509V3_conf_err(val);
   1.222 +			goto err;
   1.223 +			}
   1.224 +		if (!tmp_data)
   1.225 +			{
   1.226 +			X509V3err(X509V3_F_PROCESS_PCI_VALUE,ERR_R_MALLOC_FAILURE);
   1.227 +			X509V3_conf_err(val);
   1.228 +			goto err;
   1.229 +			}
   1.230 +		}
   1.231 +	return 1;
   1.232 +err:
   1.233 +	if (free_policy)
   1.234 +		{
   1.235 +		ASN1_OCTET_STRING_free(*policy);
   1.236 +		*policy = NULL;
   1.237 +		}
   1.238 +	return 0;
   1.239 +	}
   1.240 +
   1.241 +static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
   1.242 +	X509V3_CTX *ctx, char *value)
   1.243 +	{
   1.244 +	PROXY_CERT_INFO_EXTENSION *pci = NULL;
   1.245 +	STACK_OF(CONF_VALUE) *vals;
   1.246 +	ASN1_OBJECT *language = NULL;
   1.247 +	ASN1_INTEGER *pathlen = NULL;
   1.248 +	ASN1_OCTET_STRING *policy = NULL;
   1.249 +	int i, j;
   1.250 +
   1.251 +	vals = X509V3_parse_list(value);
   1.252 +	for (i = 0; i < sk_CONF_VALUE_num(vals); i++)
   1.253 +		{
   1.254 +		CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i);
   1.255 +		if (!cnf->name || (*cnf->name != '@' && !cnf->value))
   1.256 +			{
   1.257 +			X509V3err(X509V3_F_R2I_PCI,X509V3_R_INVALID_PROXY_POLICY_SETTING);
   1.258 +			X509V3_conf_err(cnf);
   1.259 +			goto err;
   1.260 +			}
   1.261 +		if (*cnf->name == '@')
   1.262 +			{
   1.263 +			STACK_OF(CONF_VALUE) *sect;
   1.264 +			int success_p = 1;
   1.265 +
   1.266 +			sect = X509V3_get_section(ctx, cnf->name + 1);
   1.267 +			if (!sect)
   1.268 +				{
   1.269 +				X509V3err(X509V3_F_R2I_PCI,X509V3_R_INVALID_SECTION);
   1.270 +				X509V3_conf_err(cnf);
   1.271 +				goto err;
   1.272 +				}
   1.273 +			for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++)
   1.274 +				{
   1.275 +				success_p =
   1.276 +					process_pci_value(sk_CONF_VALUE_value(sect, j),
   1.277 +						&language, &pathlen, &policy);
   1.278 +				}
   1.279 +			X509V3_section_free(ctx, sect);
   1.280 +			if (!success_p)
   1.281 +				goto err;
   1.282 +			}
   1.283 +		else
   1.284 +			{
   1.285 +			if (!process_pci_value(cnf,
   1.286 +					&language, &pathlen, &policy))
   1.287 +				{
   1.288 +				X509V3_conf_err(cnf);
   1.289 +				goto err;
   1.290 +				}
   1.291 +			}
   1.292 +		}
   1.293 +
   1.294 +	/* Language is mandatory */
   1.295 +	if (!language)
   1.296 +		{
   1.297 +		X509V3err(X509V3_F_R2I_PCI,X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED);
   1.298 +		goto err;
   1.299 +		}
   1.300 +	i = OBJ_obj2nid(language);
   1.301 +	if ((i == NID_Independent || i == NID_id_ppl_inheritAll) && policy)
   1.302 +		{
   1.303 +		X509V3err(X509V3_F_R2I_PCI,X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY);
   1.304 +		goto err;
   1.305 +		}
   1.306 +
   1.307 +	pci = PROXY_CERT_INFO_EXTENSION_new();
   1.308 +	if (!pci)
   1.309 +		{
   1.310 +		X509V3err(X509V3_F_R2I_PCI,ERR_R_MALLOC_FAILURE);
   1.311 +		goto err;
   1.312 +		}
   1.313 +
   1.314 +	pci->proxyPolicy->policyLanguage = language; language = NULL;
   1.315 +	pci->proxyPolicy->policy = policy; policy = NULL;
   1.316 +	pci->pcPathLengthConstraint = pathlen; pathlen = NULL;
   1.317 +	goto end;
   1.318 +err:
   1.319 +	if (language) { ASN1_OBJECT_free(language); language = NULL; }
   1.320 +	if (pathlen) { ASN1_INTEGER_free(pathlen); pathlen = NULL; }
   1.321 +	if (policy) { ASN1_OCTET_STRING_free(policy); policy = NULL; }
   1.322 +	if (pci) { PROXY_CERT_INFO_EXTENSION_free(pci); pci = NULL; }
   1.323 +end:
   1.324 +	sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
   1.325 +	return pci;
   1.326 +	}