v3_sxnet.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 "cryptlib.h"
  61. #include <openssl/conf.h>
  62. #include <openssl/asn1.h>
  63. #include <openssl/asn1t.h>
  64. #include <openssl/x509v3.h>
  65. /* Support for Thawte strong extranet extension */
  66. #define SXNET_TEST
  67. static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,
  68. int indent);
  69. #ifdef SXNET_TEST
  70. static SXNET *sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
  71. STACK_OF(CONF_VALUE) *nval);
  72. #endif
  73. const X509V3_EXT_METHOD v3_sxnet = {
  74. NID_sxnet, X509V3_EXT_MULTILINE, ASN1_ITEM_ref(SXNET),
  75. 0, 0, 0, 0,
  76. 0, 0,
  77. 0,
  78. #ifdef SXNET_TEST
  79. (X509V3_EXT_V2I)sxnet_v2i,
  80. #else
  81. 0,
  82. #endif
  83. (X509V3_EXT_I2R)sxnet_i2r,
  84. 0,
  85. NULL
  86. };
  87. ASN1_SEQUENCE(SXNETID) = {
  88. ASN1_SIMPLE(SXNETID, zone, ASN1_INTEGER),
  89. ASN1_SIMPLE(SXNETID, user, ASN1_OCTET_STRING)
  90. } ASN1_SEQUENCE_END(SXNETID)
  91. IMPLEMENT_ASN1_FUNCTIONS(SXNETID)
  92. ASN1_SEQUENCE(SXNET) = {
  93. ASN1_SIMPLE(SXNET, version, ASN1_INTEGER),
  94. ASN1_SEQUENCE_OF(SXNET, ids, SXNETID)
  95. } ASN1_SEQUENCE_END(SXNET)
  96. IMPLEMENT_ASN1_FUNCTIONS(SXNET)
  97. static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,
  98. int indent)
  99. {
  100. long v;
  101. char *tmp;
  102. SXNETID *id;
  103. int i;
  104. v = ASN1_INTEGER_get(sx->version);
  105. BIO_printf(out, "%*sVersion: %ld (0x%lX)", indent, "", v + 1, v);
  106. for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
  107. id = sk_SXNETID_value(sx->ids, i);
  108. tmp = i2s_ASN1_INTEGER(NULL, id->zone);
  109. BIO_printf(out, "\n%*sZone: %s, User: ", indent, "", tmp);
  110. OPENSSL_free(tmp);
  111. M_ASN1_OCTET_STRING_print(out, id->user);
  112. }
  113. return 1;
  114. }
  115. #ifdef SXNET_TEST
  116. /*
  117. * NBB: this is used for testing only. It should *not* be used for anything
  118. * else because it will just take static IDs from the configuration file and
  119. * they should really be separate values for each user.
  120. */
  121. static SXNET *sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
  122. STACK_OF(CONF_VALUE) *nval)
  123. {
  124. CONF_VALUE *cnf;
  125. SXNET *sx = NULL;
  126. int i;
  127. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  128. cnf = sk_CONF_VALUE_value(nval, i);
  129. if (!SXNET_add_id_asc(&sx, cnf->name, cnf->value, -1))
  130. return NULL;
  131. }
  132. return sx;
  133. }
  134. #endif
  135. /* Strong Extranet utility functions */
  136. /* Add an id given the zone as an ASCII number */
  137. int SXNET_add_id_asc(SXNET **psx, char *zone, char *user, int userlen)
  138. {
  139. ASN1_INTEGER *izone = NULL;
  140. if (!(izone = s2i_ASN1_INTEGER(NULL, zone))) {
  141. X509V3err(X509V3_F_SXNET_ADD_ID_ASC, X509V3_R_ERROR_CONVERTING_ZONE);
  142. return 0;
  143. }
  144. return SXNET_add_id_INTEGER(psx, izone, user, userlen);
  145. }
  146. /* Add an id given the zone as an unsigned long */
  147. int SXNET_add_id_ulong(SXNET **psx, unsigned long lzone, char *user,
  148. int userlen)
  149. {
  150. ASN1_INTEGER *izone = NULL;
  151. if (!(izone = M_ASN1_INTEGER_new()) || !ASN1_INTEGER_set(izone, lzone)) {
  152. X509V3err(X509V3_F_SXNET_ADD_ID_ULONG, ERR_R_MALLOC_FAILURE);
  153. M_ASN1_INTEGER_free(izone);
  154. return 0;
  155. }
  156. return SXNET_add_id_INTEGER(psx, izone, user, userlen);
  157. }
  158. /*
  159. * Add an id given the zone as an ASN1_INTEGER. Note this version uses the
  160. * passed integer and doesn't make a copy so don't free it up afterwards.
  161. */
  162. int SXNET_add_id_INTEGER(SXNET **psx, ASN1_INTEGER *zone, char *user,
  163. int userlen)
  164. {
  165. SXNET *sx = NULL;
  166. SXNETID *id = NULL;
  167. if (!psx || !zone || !user) {
  168. X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER,
  169. X509V3_R_INVALID_NULL_ARGUMENT);
  170. return 0;
  171. }
  172. if (userlen == -1)
  173. userlen = strlen(user);
  174. if (userlen > 64) {
  175. X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER, X509V3_R_USER_TOO_LONG);
  176. return 0;
  177. }
  178. if (!*psx) {
  179. if (!(sx = SXNET_new()))
  180. goto err;
  181. if (!ASN1_INTEGER_set(sx->version, 0))
  182. goto err;
  183. *psx = sx;
  184. } else
  185. sx = *psx;
  186. if (SXNET_get_id_INTEGER(sx, zone)) {
  187. X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER, X509V3_R_DUPLICATE_ZONE_ID);
  188. return 0;
  189. }
  190. if (!(id = SXNETID_new()))
  191. goto err;
  192. if (userlen == -1)
  193. userlen = strlen(user);
  194. if (!M_ASN1_OCTET_STRING_set(id->user, user, userlen))
  195. goto err;
  196. if (!sk_SXNETID_push(sx->ids, id))
  197. goto err;
  198. id->zone = zone;
  199. return 1;
  200. err:
  201. X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER, ERR_R_MALLOC_FAILURE);
  202. SXNETID_free(id);
  203. SXNET_free(sx);
  204. *psx = NULL;
  205. return 0;
  206. }
  207. ASN1_OCTET_STRING *SXNET_get_id_asc(SXNET *sx, char *zone)
  208. {
  209. ASN1_INTEGER *izone = NULL;
  210. ASN1_OCTET_STRING *oct;
  211. if (!(izone = s2i_ASN1_INTEGER(NULL, zone))) {
  212. X509V3err(X509V3_F_SXNET_GET_ID_ASC, X509V3_R_ERROR_CONVERTING_ZONE);
  213. return NULL;
  214. }
  215. oct = SXNET_get_id_INTEGER(sx, izone);
  216. M_ASN1_INTEGER_free(izone);
  217. return oct;
  218. }
  219. ASN1_OCTET_STRING *SXNET_get_id_ulong(SXNET *sx, unsigned long lzone)
  220. {
  221. ASN1_INTEGER *izone = NULL;
  222. ASN1_OCTET_STRING *oct;
  223. if (!(izone = M_ASN1_INTEGER_new()) || !ASN1_INTEGER_set(izone, lzone)) {
  224. X509V3err(X509V3_F_SXNET_GET_ID_ULONG, ERR_R_MALLOC_FAILURE);
  225. M_ASN1_INTEGER_free(izone);
  226. return NULL;
  227. }
  228. oct = SXNET_get_id_INTEGER(sx, izone);
  229. M_ASN1_INTEGER_free(izone);
  230. return oct;
  231. }
  232. ASN1_OCTET_STRING *SXNET_get_id_INTEGER(SXNET *sx, ASN1_INTEGER *zone)
  233. {
  234. SXNETID *id;
  235. int i;
  236. for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
  237. id = sk_SXNETID_value(sx->ids, i);
  238. if (!M_ASN1_INTEGER_cmp(id->zone, zone))
  239. return id->user;
  240. }
  241. return NULL;
  242. }
  243. IMPLEMENT_STACK_OF(SXNETID)
  244. IMPLEMENT_ASN1_SET_OF(SXNETID)