Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

334 рядки
9.0 KiB

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