您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

275 行
8.2 KiB

  1. /* v3_sxnet.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 1999.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999 The OpenSSL Project. 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
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include <openssl/asn1.h>
  62. #include <openssl/asn1t.h>
  63. #include <openssl/conf.h>
  64. #include <openssl/err.h>
  65. #include <openssl/mem.h>
  66. #include <openssl/obj.h>
  67. #include <openssl/x509v3.h>
  68. /* Support for Thawte strong extranet extension */
  69. #define SXNET_TEST
  70. static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,
  71. int indent);
  72. #ifdef SXNET_TEST
  73. static SXNET *sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
  74. STACK_OF(CONF_VALUE) *nval);
  75. #endif
  76. const X509V3_EXT_METHOD v3_sxnet = {
  77. NID_sxnet, X509V3_EXT_MULTILINE, ASN1_ITEM_ref(SXNET),
  78. 0, 0, 0, 0,
  79. 0, 0,
  80. 0,
  81. #ifdef SXNET_TEST
  82. (X509V3_EXT_V2I)sxnet_v2i,
  83. #else
  84. 0,
  85. #endif
  86. (X509V3_EXT_I2R)sxnet_i2r,
  87. 0,
  88. NULL
  89. };
  90. ASN1_SEQUENCE(SXNETID) = {
  91. ASN1_SIMPLE(SXNETID, zone, ASN1_INTEGER),
  92. ASN1_SIMPLE(SXNETID, user, ASN1_OCTET_STRING)
  93. } ASN1_SEQUENCE_END(SXNETID)
  94. IMPLEMENT_ASN1_FUNCTIONS(SXNETID)
  95. ASN1_SEQUENCE(SXNET) = {
  96. ASN1_SIMPLE(SXNET, version, ASN1_INTEGER),
  97. ASN1_SEQUENCE_OF(SXNET, ids, SXNETID)
  98. } ASN1_SEQUENCE_END(SXNET)
  99. IMPLEMENT_ASN1_FUNCTIONS(SXNET)
  100. static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,
  101. int indent)
  102. {
  103. long v;
  104. char *tmp;
  105. SXNETID *id;
  106. size_t i;
  107. v = ASN1_INTEGER_get(sx->version);
  108. BIO_printf(out, "%*sVersion: %ld (0x%lX)", indent, "", v + 1, v);
  109. for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
  110. id = sk_SXNETID_value(sx->ids, i);
  111. tmp = i2s_ASN1_INTEGER(NULL, id->zone);
  112. BIO_printf(out, "\n%*sZone: %s, User: ", indent, "", tmp);
  113. OPENSSL_free(tmp);
  114. M_ASN1_OCTET_STRING_print(out, id->user);
  115. }
  116. return 1;
  117. }
  118. #ifdef SXNET_TEST
  119. /*
  120. * NBB: this is used for testing only. It should *not* be used for anything
  121. * else because it will just take static IDs from the configuration file and
  122. * they should really be separate values for each user.
  123. */
  124. static SXNET *sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
  125. STACK_OF(CONF_VALUE) *nval)
  126. {
  127. CONF_VALUE *cnf;
  128. SXNET *sx = NULL;
  129. size_t i;
  130. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  131. cnf = sk_CONF_VALUE_value(nval, i);
  132. if (!SXNET_add_id_asc(&sx, cnf->name, cnf->value, -1))
  133. return NULL;
  134. }
  135. return sx;
  136. }
  137. #endif
  138. /* Strong Extranet utility functions */
  139. /* Add an id given the zone as an ASCII number */
  140. int SXNET_add_id_asc(SXNET **psx, char *zone, char *user, int userlen)
  141. {
  142. ASN1_INTEGER *izone = NULL;
  143. if (!(izone = s2i_ASN1_INTEGER(NULL, zone))) {
  144. OPENSSL_PUT_ERROR(X509V3, X509V3_R_ERROR_CONVERTING_ZONE);
  145. return 0;
  146. }
  147. return SXNET_add_id_INTEGER(psx, izone, user, userlen);
  148. }
  149. /* Add an id given the zone as an unsigned long */
  150. int SXNET_add_id_ulong(SXNET **psx, unsigned long lzone, char *user,
  151. int userlen)
  152. {
  153. ASN1_INTEGER *izone = NULL;
  154. if (!(izone = M_ASN1_INTEGER_new()) || !ASN1_INTEGER_set(izone, lzone)) {
  155. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  156. M_ASN1_INTEGER_free(izone);
  157. return 0;
  158. }
  159. return SXNET_add_id_INTEGER(psx, izone, user, userlen);
  160. }
  161. /*
  162. * Add an id given the zone as an ASN1_INTEGER. Note this version uses the
  163. * passed integer and doesn't make a copy so don't free it up afterwards.
  164. */
  165. int SXNET_add_id_INTEGER(SXNET **psx, ASN1_INTEGER *zone, char *user,
  166. int userlen)
  167. {
  168. SXNET *sx = NULL;
  169. SXNETID *id = NULL;
  170. if (!psx || !zone || !user) {
  171. OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_ARGUMENT);
  172. return 0;
  173. }
  174. if (userlen == -1)
  175. userlen = strlen(user);
  176. if (userlen > 64) {
  177. OPENSSL_PUT_ERROR(X509V3, X509V3_R_USER_TOO_LONG);
  178. return 0;
  179. }
  180. if (!*psx) {
  181. if (!(sx = SXNET_new()))
  182. goto err;
  183. if (!ASN1_INTEGER_set(sx->version, 0))
  184. goto err;
  185. *psx = sx;
  186. } else
  187. sx = *psx;
  188. if (SXNET_get_id_INTEGER(sx, zone)) {
  189. OPENSSL_PUT_ERROR(X509V3, X509V3_R_DUPLICATE_ZONE_ID);
  190. return 0;
  191. }
  192. if (!(id = SXNETID_new()))
  193. goto err;
  194. if (userlen == -1)
  195. userlen = strlen(user);
  196. if (!M_ASN1_OCTET_STRING_set(id->user, user, userlen))
  197. goto err;
  198. if (!sk_SXNETID_push(sx->ids, id))
  199. goto err;
  200. id->zone = zone;
  201. return 1;
  202. err:
  203. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  204. SXNETID_free(id);
  205. SXNET_free(sx);
  206. *psx = NULL;
  207. return 0;
  208. }
  209. ASN1_OCTET_STRING *SXNET_get_id_asc(SXNET *sx, char *zone)
  210. {
  211. ASN1_INTEGER *izone = NULL;
  212. ASN1_OCTET_STRING *oct;
  213. if (!(izone = s2i_ASN1_INTEGER(NULL, zone))) {
  214. OPENSSL_PUT_ERROR(X509V3, X509V3_R_ERROR_CONVERTING_ZONE);
  215. return NULL;
  216. }
  217. oct = SXNET_get_id_INTEGER(sx, izone);
  218. M_ASN1_INTEGER_free(izone);
  219. return oct;
  220. }
  221. ASN1_OCTET_STRING *SXNET_get_id_ulong(SXNET *sx, unsigned long lzone)
  222. {
  223. ASN1_INTEGER *izone = NULL;
  224. ASN1_OCTET_STRING *oct;
  225. if (!(izone = M_ASN1_INTEGER_new()) || !ASN1_INTEGER_set(izone, lzone)) {
  226. OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
  227. M_ASN1_INTEGER_free(izone);
  228. return NULL;
  229. }
  230. oct = SXNET_get_id_INTEGER(sx, izone);
  231. M_ASN1_INTEGER_free(izone);
  232. return oct;
  233. }
  234. ASN1_OCTET_STRING *SXNET_get_id_INTEGER(SXNET *sx, ASN1_INTEGER *zone)
  235. {
  236. SXNETID *id;
  237. size_t i;
  238. for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
  239. id = sk_SXNETID_value(sx->ids, i);
  240. if (!M_ASN1_INTEGER_cmp(id->zone, zone))
  241. return id->user;
  242. }
  243. return NULL;
  244. }
  245. IMPLEMENT_ASN1_SET_OF(SXNETID)