v3_pci.c 11 KB

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