Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

318 wiersze
11 KiB

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