Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

335 Zeilen
9.1 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 <string.h>
  38. #include <openssl/conf.h>
  39. #include <openssl/err.h>
  40. #include <openssl/mem.h>
  41. #include <openssl/obj.h>
  42. #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);
  47. const X509V3_EXT_METHOD v3_pci =
  48. { NID_proxyCertInfo, 0, ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
  49. 0,0,0,0,
  50. 0,0,
  51. NULL, NULL,
  52. (X509V3_EXT_I2R)i2r_pci,
  53. (X509V3_EXT_R2I)r2i_pci,
  54. NULL,
  55. };
  56. static int i2r_pci(X509V3_EXT_METHOD *method, PROXY_CERT_INFO_EXTENSION *pci,
  57. BIO *out, int indent)
  58. {
  59. BIO_printf(out, "%*sPath Length Constraint: ", indent, "");
  60. if (pci->pcPathLengthConstraint)
  61. i2a_ASN1_INTEGER(out, pci->pcPathLengthConstraint);
  62. else
  63. BIO_printf(out, "infinite");
  64. BIO_puts(out, "\n");
  65. BIO_printf(out, "%*sPolicy Language: ", indent, "");
  66. i2a_ASN1_OBJECT(out, pci->proxyPolicy->policyLanguage);
  67. BIO_puts(out, "\n");
  68. if (pci->proxyPolicy->policy && pci->proxyPolicy->policy->data)
  69. BIO_printf(out, "%*sPolicy Text: %s\n", indent, "",
  70. pci->proxyPolicy->policy->data);
  71. return 1;
  72. }
  73. static int process_pci_value(CONF_VALUE *val,
  74. ASN1_OBJECT **language, ASN1_INTEGER **pathlen,
  75. ASN1_OCTET_STRING **policy)
  76. {
  77. int free_policy = 0;
  78. if (strcmp(val->name, "language") == 0)
  79. {
  80. if (*language)
  81. {
  82. OPENSSL_PUT_ERROR(X509V3, process_pci_value, X509V3_R_POLICY_LANGUAGE_ALREADY_DEFINED);
  83. X509V3_conf_err(val);
  84. return 0;
  85. }
  86. if (!(*language = OBJ_txt2obj(val->value, 0)))
  87. {
  88. OPENSSL_PUT_ERROR(X509V3, process_pci_value, X509V3_R_INVALID_OBJECT_IDENTIFIER);
  89. X509V3_conf_err(val);
  90. return 0;
  91. }
  92. }
  93. else if (strcmp(val->name, "pathlen") == 0)
  94. {
  95. if (*pathlen)
  96. {
  97. OPENSSL_PUT_ERROR(X509V3, process_pci_value, X509V3_R_POLICY_PATH_LENGTH_ALREADY_DEFINED);
  98. X509V3_conf_err(val);
  99. return 0;
  100. }
  101. if (!X509V3_get_value_int(val, pathlen))
  102. {
  103. OPENSSL_PUT_ERROR(X509V3, process_pci_value, X509V3_R_POLICY_PATH_LENGTH);
  104. X509V3_conf_err(val);
  105. return 0;
  106. }
  107. }
  108. else if (strcmp(val->name, "policy") == 0)
  109. {
  110. unsigned char *tmp_data = NULL;
  111. long val_len;
  112. if (!*policy)
  113. {
  114. *policy = ASN1_OCTET_STRING_new();
  115. if (!*policy)
  116. {
  117. OPENSSL_PUT_ERROR(X509V3, process_pci_value, ERR_R_MALLOC_FAILURE);
  118. X509V3_conf_err(val);
  119. return 0;
  120. }
  121. free_policy = 1;
  122. }
  123. if (strncmp(val->value, "hex:", 4) == 0)
  124. {
  125. unsigned char *tmp_data2 =
  126. string_to_hex(val->value + 4, &val_len);
  127. if (!tmp_data2)
  128. {
  129. OPENSSL_PUT_ERROR(X509V3, process_pci_value, X509V3_R_ILLEGAL_HEX_DIGIT);
  130. X509V3_conf_err(val);
  131. goto err;
  132. }
  133. tmp_data = OPENSSL_realloc((*policy)->data,
  134. (*policy)->length + val_len + 1);
  135. if (tmp_data)
  136. {
  137. (*policy)->data = tmp_data;
  138. memcpy(&(*policy)->data[(*policy)->length],
  139. tmp_data2, val_len);
  140. (*policy)->length += val_len;
  141. (*policy)->data[(*policy)->length] = '\0';
  142. }
  143. else
  144. {
  145. OPENSSL_free(tmp_data2);
  146. /* realloc failure implies the original data space is b0rked too! */
  147. (*policy)->data = NULL;
  148. (*policy)->length = 0;
  149. OPENSSL_PUT_ERROR(X509V3, process_pci_value, ERR_R_MALLOC_FAILURE);
  150. X509V3_conf_err(val);
  151. goto err;
  152. }
  153. OPENSSL_free(tmp_data2);
  154. }
  155. else if (strncmp(val->value, "file:", 5) == 0)
  156. {
  157. unsigned char buf[2048];
  158. int n;
  159. BIO *b = BIO_new_file(val->value + 5, "r");
  160. if (!b)
  161. {
  162. OPENSSL_PUT_ERROR(X509V3, process_pci_value, ERR_R_BIO_LIB);
  163. X509V3_conf_err(val);
  164. goto err;
  165. }
  166. while((n = BIO_read(b, buf, sizeof(buf))) > 0
  167. || (n == 0 && BIO_should_retry(b)))
  168. {
  169. if (!n) continue;
  170. tmp_data = OPENSSL_realloc((*policy)->data,
  171. (*policy)->length + n + 1);
  172. if (!tmp_data)
  173. break;
  174. (*policy)->data = tmp_data;
  175. memcpy(&(*policy)->data[(*policy)->length],
  176. buf, n);
  177. (*policy)->length += n;
  178. (*policy)->data[(*policy)->length] = '\0';
  179. }
  180. BIO_free_all(b);
  181. if (n < 0)
  182. {
  183. OPENSSL_PUT_ERROR(X509V3, process_pci_value, ERR_R_BIO_LIB);
  184. X509V3_conf_err(val);
  185. goto err;
  186. }
  187. }
  188. else if (strncmp(val->value, "text:", 5) == 0)
  189. {
  190. val_len = strlen(val->value + 5);
  191. tmp_data = OPENSSL_realloc((*policy)->data,
  192. (*policy)->length + val_len + 1);
  193. if (tmp_data)
  194. {
  195. (*policy)->data = tmp_data;
  196. memcpy(&(*policy)->data[(*policy)->length],
  197. val->value + 5, val_len);
  198. (*policy)->length += val_len;
  199. (*policy)->data[(*policy)->length] = '\0';
  200. }
  201. else
  202. {
  203. /* realloc failure implies the original data space is b0rked too! */
  204. (*policy)->data = NULL;
  205. (*policy)->length = 0;
  206. OPENSSL_PUT_ERROR(X509V3, process_pci_value, ERR_R_MALLOC_FAILURE);
  207. X509V3_conf_err(val);
  208. goto err;
  209. }
  210. }
  211. else
  212. {
  213. OPENSSL_PUT_ERROR(X509V3, process_pci_value, X509V3_R_INCORRECT_POLICY_SYNTAX_TAG);
  214. X509V3_conf_err(val);
  215. goto err;
  216. }
  217. if (!tmp_data)
  218. {
  219. OPENSSL_PUT_ERROR(X509V3, process_pci_value, ERR_R_MALLOC_FAILURE);
  220. X509V3_conf_err(val);
  221. goto err;
  222. }
  223. }
  224. return 1;
  225. err:
  226. if (free_policy)
  227. {
  228. ASN1_OCTET_STRING_free(*policy);
  229. *policy = NULL;
  230. }
  231. return 0;
  232. }
  233. static PROXY_CERT_INFO_EXTENSION *r2i_pci(X509V3_EXT_METHOD *method,
  234. X509V3_CTX *ctx, char *value)
  235. {
  236. PROXY_CERT_INFO_EXTENSION *pci = NULL;
  237. STACK_OF(CONF_VALUE) *vals;
  238. ASN1_OBJECT *language = NULL;
  239. ASN1_INTEGER *pathlen = NULL;
  240. ASN1_OCTET_STRING *policy = NULL;
  241. size_t i, j;
  242. int nid;
  243. vals = X509V3_parse_list(value);
  244. for (i = 0; i < sk_CONF_VALUE_num(vals); i++)
  245. {
  246. CONF_VALUE *cnf = sk_CONF_VALUE_value(vals, i);
  247. if (!cnf->name || (*cnf->name != '@' && !cnf->value))
  248. {
  249. OPENSSL_PUT_ERROR(X509V3, r2i_pci, X509V3_R_INVALID_PROXY_POLICY_SETTING);
  250. X509V3_conf_err(cnf);
  251. goto err;
  252. }
  253. if (*cnf->name == '@')
  254. {
  255. STACK_OF(CONF_VALUE) *sect;
  256. int success_p = 1;
  257. sect = X509V3_get_section(ctx, cnf->name + 1);
  258. if (!sect)
  259. {
  260. OPENSSL_PUT_ERROR(X509V3, r2i_pci, X509V3_R_INVALID_SECTION);
  261. X509V3_conf_err(cnf);
  262. goto err;
  263. }
  264. for (j = 0; success_p && j < sk_CONF_VALUE_num(sect); j++)
  265. {
  266. success_p =
  267. process_pci_value(sk_CONF_VALUE_value(sect, j),
  268. &language, &pathlen, &policy);
  269. }
  270. X509V3_section_free(ctx, sect);
  271. if (!success_p)
  272. goto err;
  273. }
  274. else
  275. {
  276. if (!process_pci_value(cnf,
  277. &language, &pathlen, &policy))
  278. {
  279. X509V3_conf_err(cnf);
  280. goto err;
  281. }
  282. }
  283. }
  284. /* Language is mandatory */
  285. if (!language)
  286. {
  287. OPENSSL_PUT_ERROR(X509V3, r2i_pci, X509V3_R_NO_PROXY_CERT_POLICY_LANGUAGE_DEFINED);
  288. goto err;
  289. }
  290. nid = OBJ_obj2nid(language);
  291. if ((nid == NID_Independent || nid == NID_id_ppl_inheritAll) && policy)
  292. {
  293. OPENSSL_PUT_ERROR(X509V3, r2i_pci, X509V3_R_POLICY_WHEN_PROXY_LANGUAGE_REQUIRES_NO_POLICY);
  294. goto err;
  295. }
  296. pci = PROXY_CERT_INFO_EXTENSION_new();
  297. if (!pci)
  298. {
  299. OPENSSL_PUT_ERROR(X509V3, r2i_pci, ERR_R_MALLOC_FAILURE);
  300. goto err;
  301. }
  302. pci->proxyPolicy->policyLanguage = language; language = NULL;
  303. pci->proxyPolicy->policy = policy; policy = NULL;
  304. pci->pcPathLengthConstraint = pathlen; pathlen = NULL;
  305. goto end;
  306. err:
  307. if (language) { ASN1_OBJECT_free(language); language = NULL; }
  308. if (pathlen) { ASN1_INTEGER_free(pathlen); pathlen = NULL; }
  309. if (policy) { ASN1_OCTET_STRING_free(policy); policy = NULL; }
  310. if (pci) { PROXY_CERT_INFO_EXTENSION_free(pci); pci = NULL; }
  311. end:
  312. sk_CONF_VALUE_pop_free(vals, X509V3_conf_free);
  313. return pci;
  314. }