2
0

v3_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright 2004-2016 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 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. BIO_puts(out, "\n");
  74. if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data)
  75. BIO_printf(out, "%*sPolicy Text: %s\n", indent, "",
  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. X509V3err(X509V3_F_PROCESS_PCI_VALUE,
  87. X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED);
  88. X509V3_conf_err(val);
  89. return 0;
  90. }
  91. if ((*language = OBJ_txt2obj(val->value, 0)) == NULL) {
  92. X509V3err(X509V3_F_PROCESS_PCI_VALUE,
  93. X509V3_R_INVALID_OBJECT_IDENTIFIER);
  94. X509V3_conf_err(val);
  95. return 0;
  96. }
  97. } else if (strcmp(val->name, "pathlen") == 0) {
  98. if (*pathlen) {
  99. X509V3err(X509V3_F_PROCESS_PCI_VALUE,
  100. X509V3_R_POLICY_PATH_LENGTH_ALREADY_DEFINED);
  101. X509V3_conf_err(val);
  102. return 0;
  103. }
  104. if (!X509V3_get_value_int(val, pathlen)) {
  105. X509V3err(X509V3_F_PROCESS_PCI_VALUE,
  106. X509V3_R_POLICY_PATH_LENGTH);
  107. X509V3_conf_err(val);
  108. return 0;
  109. }
  110. } else if (strcmp(val->name, "policy") == 0) {
  111. unsigned char *tmp_data = NULL;
  112. long val_len;
  113. if (!*policy) {
  114. *policy = ASN1_OCTET_STRING_new();
  115. if (*policy == NULL) {
  116. X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
  117. X509V3_conf_err(val);
  118. return 0;
  119. }
  120. free_policy = 1;
  121. }
  122. if (strncmp(val->value, "hex:", 4) == 0) {
  123. unsigned char *tmp_data2 =
  124. OPENSSL_hexstr2buf(val->value + 4, &val_len);
  125. if (!tmp_data2) {
  126. X509V3_conf_err(val);
  127. goto err;
  128. }
  129. tmp_data = OPENSSL_realloc((*policy)->data,
  130. (*policy)->length + val_len + 1);
  131. if (tmp_data) {
  132. (*policy)->data = tmp_data;
  133. memcpy(&(*policy)->data[(*policy)->length],
  134. tmp_data2, val_len);
  135. (*policy)->length += val_len;
  136. (*policy)->data[(*policy)->length] = '\0';
  137. } else {
  138. OPENSSL_free(tmp_data2);
  139. /*
  140. * realloc failure implies the original data space is b0rked
  141. * too!
  142. */
  143. OPENSSL_free((*policy)->data);
  144. (*policy)->data = NULL;
  145. (*policy)->length = 0;
  146. X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
  147. X509V3_conf_err(val);
  148. goto err;
  149. }
  150. OPENSSL_free(tmp_data2);
  151. } else if (strncmp(val->value, "file:", 5) == 0) {
  152. unsigned char buf[2048];
  153. int n;
  154. BIO *b = BIO_new_file(val->value + 5, "r");
  155. if (!b) {
  156. X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_BIO_LIB);
  157. X509V3_conf_err(val);
  158. goto err;
  159. }
  160. while ((n = BIO_read(b, buf, sizeof(buf))) > 0
  161. || (n == 0 && BIO_should_retry(b))) {
  162. if (!n)
  163. continue;
  164. tmp_data = OPENSSL_realloc((*policy)->data,
  165. (*policy)->length + n + 1);
  166. if (!tmp_data) {
  167. OPENSSL_free((*policy)->data);
  168. (*policy)->data = NULL;
  169. (*policy)->length = 0;
  170. X509V3err(X509V3_F_PROCESS_PCI_VALUE,
  171. ERR_R_MALLOC_FAILURE);
  172. X509V3_conf_err(val);
  173. BIO_free_all(b);
  174. goto err;
  175. }
  176. (*policy)->data = tmp_data;
  177. memcpy(&(*policy)->data[(*policy)->length], buf, n);
  178. (*policy)->length += n;
  179. (*policy)->data[(*policy)->length] = '\0';
  180. }
  181. BIO_free_all(b);
  182. if (n < 0) {
  183. X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_BIO_LIB);
  184. X509V3_conf_err(val);
  185. goto err;
  186. }
  187. } else if (strncmp(val->value, "text:", 5) == 0) {
  188. val_len = strlen(val->value + 5);
  189. tmp_data = OPENSSL_realloc((*policy)->data,
  190. (*policy)->length + val_len + 1);
  191. if (tmp_data) {
  192. (*policy)->data = tmp_data;
  193. memcpy(&(*policy)->data[(*policy)->length],
  194. val->value + 5, val_len);
  195. (*policy)->length += val_len;
  196. (*policy)->data[(*policy)->length] = '\0';
  197. } else {
  198. /*
  199. * realloc failure implies the original data space is b0rked
  200. * too!
  201. */
  202. OPENSSL_free((*policy)->data);
  203. (*policy)->data = NULL;
  204. (*policy)->length = 0;
  205. X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
  206. X509V3_conf_err(val);
  207. goto err;
  208. }
  209. } else {
  210. X509V3err(X509V3_F_PROCESS_PCI_VALUE,
  211. X509V3_R_INCORRECT_POLICY_SYNTAX_TAG);
  212. X509V3_conf_err(val);
  213. goto err;
  214. }
  215. if (!tmp_data) {
  216. X509V3err(X509V3_F_PROCESS_PCI_VALUE, ERR_R_MALLOC_FAILURE);
  217. X509V3_conf_err(val);
  218. goto err;
  219. }
  220. }
  221. return 1;
  222. err:
  223. if (free_policy) {
  224. ASN1_OCTET_STRING_free(*policy);
  225. *policy = NULL;
  226. }
  227. return 0;
  228. }
  229. static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
  230. X509V3_CTX *ctx, char *value)
  231. {
  232. PROXY_CERT_INFO_EXTENSION *pci = NULL;
  233. STACK_OF(CONF_VALUE) *vals;
  234. ASN1_OBJECT *language = NULL;
  235. ASN1_INTEGER *pathlen = NULL;
  236. ASN1_OCTET_STRING *policy = NULL;
  237. int i, j;
  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);
  245. goto err;
  246. }
  247. if (*cnf->name == '@') {
  248. STACK_OF(CONF_VALUE) *sect;
  249. int success_p = 1;
  250. sect = X509V3_get_section(ctx, cnf->name + 1);
  251. if (!sect) {
  252. X509V3err(X509V3_F_R2I_PCI, X509V3_R_INVALID_SECTION);
  253. X509V3_conf_err(cnf);
  254. goto err;
  255. }
  256. for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++) {
  257. success_p =
  258. process_pci_value(sk_CONF_VALUE_value(sect, j),
  259. &language, &pathlen, &policy);
  260. }
  261. X509V3_section_free(ctx, sect);
  262. if (!success_p)
  263. goto err;
  264. } else {
  265. if (!process_pci_value(cnf, &language, &pathlen, &policy)) {
  266. X509V3_conf_err(cnf);
  267. goto err;
  268. }
  269. }
  270. }
  271. /* Language is mandatory */
  272. if (!language) {
  273. X509V3err(X509V3_F_R2I_PCI,
  274. X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED);
  275. goto err;
  276. }
  277. i = OBJ_obj2nid(language);
  278. if ((i == NID_Independent || i == NID_id_ppl_inheritAll) && policy) {
  279. X509V3err(X509V3_F_R2I_PCI,
  280. X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY);
  281. goto err;
  282. }
  283. pci = PROXY_CERT_INFO_EXTENSION_new();
  284. if (pci == NULL) {
  285. X509V3err(X509V3_F_R2I_PCI, ERR_R_MALLOC_FAILURE);
  286. goto err;
  287. }
  288. pci->proxyPolicy->policyLanguage = language;
  289. language = NULL;
  290. pci->proxyPolicy->policy = policy;
  291. policy = NULL;
  292. pci->pcPathLengthConstraint = pathlen;
  293. pathlen = NULL;
  294. goto end;
  295. err:
  296. ASN1_OBJECT_free(language);
  297. ASN1_INTEGER_free(pathlen);
  298. pathlen = NULL;
  299. ASN1_OCTET_STRING_free(policy);
  300. policy = NULL;
  301. PROXY_CERT_INFO_EXTENSION_free(pci);
  302. pci = NULL;
  303. end:
  304. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  305. return pci;
  306. }