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