v3_sxnet.c 7.7 KB

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