3 * Contributed to the OpenSSL Project 2004 by Richard Levitte
4 * (richard@levitte.org)
6 /* Copyright (c) 2004 Kungliga Tekniska Högskolan
7 * (Royal Institute of Technology, Stockholm, Sweden).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the Institute nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <openssl/conf.h>
41 #include <openssl/x509v3.h>
43 static int i2r_pci(X509V3_EXT_METHOD
*method
, PROXY_CERT_INFO_EXTENSION
*ext
,
44 BIO
*out
, int indent
);
45 static PROXY_CERT_INFO_EXTENSION
*r2i_pci(X509V3_EXT_METHOD
*method
,
46 X509V3_CTX
*ctx
, char *str
);
48 const X509V3_EXT_METHOD v3_pci
=
49 { NID_proxyCertInfo
, 0, ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION
),
53 (X509V3_EXT_I2R
)i2r_pci
,
54 (X509V3_EXT_R2I
)r2i_pci
,
58 static int i2r_pci(X509V3_EXT_METHOD
*method
, PROXY_CERT_INFO_EXTENSION
*pci
,
61 BIO_printf(out
, "%*sPath Length Constraint: ", indent
, "");
62 if (pci
->pcPathLengthConstraint
)
63 i2a_ASN1_INTEGER(out
, pci
->pcPathLengthConstraint
);
65 BIO_printf(out
, "infinite");
67 BIO_printf(out
, "%*sPolicy Language: ", indent
, "");
68 i2a_ASN1_OBJECT(out
, pci
->proxyPolicy
->policyLanguage
);
70 if (pci
->proxyPolicy
->policy
&& pci
->proxyPolicy
->policy
->data
)
71 BIO_printf(out
, "%*sPolicy Text: %s\n", indent
, "",
72 pci
->proxyPolicy
->policy
->data
);
76 static int process_pci_value(CONF_VALUE
*val
,
77 ASN1_OBJECT
**language
, ASN1_INTEGER
**pathlen
,
78 ASN1_OCTET_STRING
**policy
)
82 if (strcmp(val
->name
, "language") == 0) {
84 X509V3err(X509V3_F_PROCESS_PCI_VALUE
,
85 X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED
);
89 if (!(*language
= OBJ_txt2obj(val
->value
, 0))) {
90 X509V3err(X509V3_F_PROCESS_PCI_VALUE
,
91 X509V3_R_INVALID_OBJECT_IDENTIFIER
);
95 } else if (strcmp(val
->name
, "pathlen") == 0) {
97 X509V3err(X509V3_F_PROCESS_PCI_VALUE
,
98 X509V3_R_POLICY_PATH_LENGTH_ALREADY_DEFINED
);
102 if (!X509V3_get_value_int(val
, pathlen
)) {
103 X509V3err(X509V3_F_PROCESS_PCI_VALUE
,
104 X509V3_R_POLICY_PATH_LENGTH
);
105 X509V3_conf_err(val
);
108 } else if (strcmp(val
->name
, "policy") == 0) {
109 unsigned char *tmp_data
= NULL
;
112 *policy
= ASN1_OCTET_STRING_new();
114 X509V3err(X509V3_F_PROCESS_PCI_VALUE
, ERR_R_MALLOC_FAILURE
);
115 X509V3_conf_err(val
);
120 if (strncmp(val
->value
, "hex:", 4) == 0) {
121 unsigned char *tmp_data2
=
122 string_to_hex(val
->value
+ 4, &val_len
);
125 X509V3err(X509V3_F_PROCESS_PCI_VALUE
,
126 X509V3_R_ILLEGAL_HEX_DIGIT
);
127 X509V3_conf_err(val
);
131 tmp_data
= OPENSSL_realloc((*policy
)->data
,
132 (*policy
)->length
+ val_len
+ 1);
134 (*policy
)->data
= tmp_data
;
135 memcpy(&(*policy
)->data
[(*policy
)->length
],
137 (*policy
)->length
+= val_len
;
138 (*policy
)->data
[(*policy
)->length
] = '\0';
140 OPENSSL_free(tmp_data2
);
142 * realloc failure implies the original data space is b0rked
145 (*policy
)->data
= NULL
;
146 (*policy
)->length
= 0;
147 X509V3err(X509V3_F_PROCESS_PCI_VALUE
, ERR_R_MALLOC_FAILURE
);
148 X509V3_conf_err(val
);
151 OPENSSL_free(tmp_data2
);
152 #ifndef OPENSSL_NO_STDIO
153 } else if (strncmp(val
->value
, "file:", 5) == 0) {
154 unsigned char buf
[2048];
156 BIO
*b
= BIO_new_file(val
->value
+ 5, "r");
158 X509V3err(X509V3_F_PROCESS_PCI_VALUE
, ERR_R_BIO_LIB
);
159 X509V3_conf_err(val
);
162 while ((n
= BIO_read(b
, buf
, sizeof(buf
))) > 0
163 || (n
== 0 && BIO_should_retry(b
))) {
167 tmp_data
= OPENSSL_realloc((*policy
)->data
,
168 (*policy
)->length
+ n
+ 1);
173 (*policy
)->data
= tmp_data
;
174 memcpy(&(*policy
)->data
[(*policy
)->length
], buf
, n
);
175 (*policy
)->length
+= n
;
176 (*policy
)->data
[(*policy
)->length
] = '\0';
181 X509V3err(X509V3_F_PROCESS_PCI_VALUE
, ERR_R_BIO_LIB
);
182 X509V3_conf_err(val
);
185 #endif /* !OPENSSL_NO_STDIO */
186 } else if (strncmp(val
->value
, "text:", 5) == 0) {
187 val_len
= strlen(val
->value
+ 5);
188 tmp_data
= OPENSSL_realloc((*policy
)->data
,
189 (*policy
)->length
+ val_len
+ 1);
191 (*policy
)->data
= tmp_data
;
192 memcpy(&(*policy
)->data
[(*policy
)->length
],
193 val
->value
+ 5, val_len
);
194 (*policy
)->length
+= val_len
;
195 (*policy
)->data
[(*policy
)->length
] = '\0';
198 * realloc failure implies the original data space is b0rked
201 (*policy
)->data
= NULL
;
202 (*policy
)->length
= 0;
203 X509V3err(X509V3_F_PROCESS_PCI_VALUE
, ERR_R_MALLOC_FAILURE
);
204 X509V3_conf_err(val
);
208 X509V3err(X509V3_F_PROCESS_PCI_VALUE
,
209 X509V3_R_INCORRECT_POLICY_SYNTAX_TAG
);
210 X509V3_conf_err(val
);
214 X509V3err(X509V3_F_PROCESS_PCI_VALUE
, ERR_R_MALLOC_FAILURE
);
215 X509V3_conf_err(val
);
222 ASN1_OCTET_STRING_free(*policy
);
228 static PROXY_CERT_INFO_EXTENSION
*r2i_pci(X509V3_EXT_METHOD
*method
,
229 X509V3_CTX
*ctx
, char *value
)
231 PROXY_CERT_INFO_EXTENSION
*pci
= NULL
;
232 STACK_OF(CONF_VALUE
) *vals
;
233 ASN1_OBJECT
*language
= NULL
;
234 ASN1_INTEGER
*pathlen
= NULL
;
235 ASN1_OCTET_STRING
*policy
= NULL
;
238 vals
= X509V3_parse_list(value
);
239 for (i
= 0; i
< sk_CONF_VALUE_num(vals
); i
++) {
240 CONF_VALUE
*cnf
= sk_CONF_VALUE_value(vals
, i
);
241 if (!cnf
->name
|| (*cnf
->name
!= '@' && !cnf
->value
)) {
242 X509V3err(X509V3_F_R2I_PCI
,
243 X509V3_R_INVALID_PROXY_POLICY_SETTING
);
244 X509V3_conf_err(cnf
);
247 if (*cnf
->name
== '@') {
248 STACK_OF(CONF_VALUE
) *sect
;
251 sect
= X509V3_get_section(ctx
, cnf
->name
+ 1);
253 X509V3err(X509V3_F_R2I_PCI
, X509V3_R_INVALID_SECTION
);
254 X509V3_conf_err(cnf
);
257 for (j
= 0; success_p
&& j
< sk_CONF_VALUE_num(sect
); j
++) {
259 process_pci_value(sk_CONF_VALUE_value(sect
, j
),
260 &language
, &pathlen
, &policy
);
262 X509V3_section_free(ctx
, sect
);
266 if (!process_pci_value(cnf
, &language
, &pathlen
, &policy
)) {
267 X509V3_conf_err(cnf
);
273 /* Language is mandatory */
275 X509V3err(X509V3_F_R2I_PCI
,
276 X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED
);
279 i
= OBJ_obj2nid(language
);
280 if ((i
== NID_Independent
|| i
== NID_id_ppl_inheritAll
) && policy
) {
281 X509V3err(X509V3_F_R2I_PCI
,
282 X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY
);
286 pci
= PROXY_CERT_INFO_EXTENSION_new();
288 X509V3err(X509V3_F_R2I_PCI
, ERR_R_MALLOC_FAILURE
);
292 pci
->proxyPolicy
->policyLanguage
= language
;
294 pci
->proxyPolicy
->policy
= policy
;
296 pci
->pcPathLengthConstraint
= pathlen
;
301 ASN1_OBJECT_free(language
);
305 ASN1_INTEGER_free(pathlen
);
309 ASN1_OCTET_STRING_free(policy
);
313 PROXY_CERT_INFO_EXTENSION_free(pci
);
317 sk_CONF_VALUE_pop_free(vals
, X509V3_conf_free
);