v3_sxnet.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/conf.h>
  12. #include <openssl/asn1.h>
  13. #include <openssl/asn1t.h>
  14. #include <openssl/x509v3.h>
  15. #include "ext_dat.h"
  16. /* Support for Thawte strong extranet extension */
  17. #define SXNET_TEST
  18. static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,
  19. int indent);
  20. #ifdef SXNET_TEST
  21. static SXNET *sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
  22. STACK_OF(CONF_VALUE) *nval);
  23. #endif
  24. const X509V3_EXT_METHOD v3_sxnet = {
  25. NID_sxnet, X509V3_EXT_MULTILINE, ASN1_ITEM_ref(SXNET),
  26. 0, 0, 0, 0,
  27. 0, 0,
  28. 0,
  29. #ifdef SXNET_TEST
  30. (X509V3_EXT_V2I)sxnet_v2i,
  31. #else
  32. 0,
  33. #endif
  34. (X509V3_EXT_I2R)sxnet_i2r,
  35. 0,
  36. NULL
  37. };
  38. ASN1_SEQUENCE(SXNETID) = {
  39. ASN1_SIMPLE(SXNETID, zone, ASN1_INTEGER),
  40. ASN1_SIMPLE(SXNETID, user, ASN1_OCTET_STRING)
  41. } ASN1_SEQUENCE_END(SXNETID)
  42. IMPLEMENT_ASN1_FUNCTIONS(SXNETID)
  43. ASN1_SEQUENCE(SXNET) = {
  44. ASN1_SIMPLE(SXNET, version, ASN1_INTEGER),
  45. ASN1_SEQUENCE_OF(SXNET, ids, SXNETID)
  46. } ASN1_SEQUENCE_END(SXNET)
  47. IMPLEMENT_ASN1_FUNCTIONS(SXNET)
  48. static int sxnet_i2r(X509V3_EXT_METHOD *method, SXNET *sx, BIO *out,
  49. int indent)
  50. {
  51. long v;
  52. char *tmp;
  53. SXNETID *id;
  54. int i;
  55. v = ASN1_INTEGER_get(sx->version);
  56. BIO_printf(out, "%*sVersion: %ld (0x%lX)", indent, "", v + 1, v);
  57. for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
  58. id = sk_SXNETID_value(sx->ids, i);
  59. tmp = i2s_ASN1_INTEGER(NULL, id->zone);
  60. BIO_printf(out, "\n%*sZone: %s, User: ", indent, "", tmp);
  61. OPENSSL_free(tmp);
  62. ASN1_STRING_print(out, id->user);
  63. }
  64. return 1;
  65. }
  66. #ifdef SXNET_TEST
  67. /*
  68. * NBB: this is used for testing only. It should *not* be used for anything
  69. * else because it will just take static IDs from the configuration file and
  70. * they should really be separate values for each user.
  71. */
  72. static SXNET *sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
  73. STACK_OF(CONF_VALUE) *nval)
  74. {
  75. CONF_VALUE *cnf;
  76. SXNET *sx = NULL;
  77. int i;
  78. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  79. cnf = sk_CONF_VALUE_value(nval, i);
  80. if (!SXNET_add_id_asc(&sx, cnf->name, cnf->value, -1))
  81. return NULL;
  82. }
  83. return sx;
  84. }
  85. #endif
  86. /* Strong Extranet utility functions */
  87. /* Add an id given the zone as an ASCII number */
  88. int SXNET_add_id_asc(SXNET **psx, const char *zone, const char *user, int userlen)
  89. {
  90. ASN1_INTEGER *izone;
  91. if ((izone = s2i_ASN1_INTEGER(NULL, zone)) == NULL) {
  92. X509V3err(X509V3_F_SXNET_ADD_ID_ASC, X509V3_R_ERROR_CONVERTING_ZONE);
  93. return 0;
  94. }
  95. return SXNET_add_id_INTEGER(psx, izone, user, userlen);
  96. }
  97. /* Add an id given the zone as an unsigned long */
  98. int SXNET_add_id_ulong(SXNET **psx, unsigned long lzone, const char *user,
  99. int userlen)
  100. {
  101. ASN1_INTEGER *izone;
  102. if ((izone = ASN1_INTEGER_new()) == NULL
  103. || !ASN1_INTEGER_set(izone, lzone)) {
  104. X509V3err(X509V3_F_SXNET_ADD_ID_ULONG, ERR_R_MALLOC_FAILURE);
  105. ASN1_INTEGER_free(izone);
  106. return 0;
  107. }
  108. return SXNET_add_id_INTEGER(psx, izone, user, userlen);
  109. }
  110. /*
  111. * Add an id given the zone as an ASN1_INTEGER. Note this version uses the
  112. * passed integer and doesn't make a copy so don't free it up afterwards.
  113. */
  114. int SXNET_add_id_INTEGER(SXNET **psx, ASN1_INTEGER *zone, const char *user,
  115. int userlen)
  116. {
  117. SXNET *sx = NULL;
  118. SXNETID *id = NULL;
  119. if (!psx || !zone || !user) {
  120. X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER,
  121. X509V3_R_INVALID_NULL_ARGUMENT);
  122. return 0;
  123. }
  124. if (userlen == -1)
  125. userlen = strlen(user);
  126. if (userlen > 64) {
  127. X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER, X509V3_R_USER_TOO_LONG);
  128. return 0;
  129. }
  130. if (*psx == NULL) {
  131. if ((sx = SXNET_new()) == NULL)
  132. goto err;
  133. if (!ASN1_INTEGER_set(sx->version, 0))
  134. goto err;
  135. *psx = sx;
  136. } else
  137. sx = *psx;
  138. if (SXNET_get_id_INTEGER(sx, zone)) {
  139. X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER, X509V3_R_DUPLICATE_ZONE_ID);
  140. return 0;
  141. }
  142. if ((id = SXNETID_new()) == NULL)
  143. goto err;
  144. if (userlen == -1)
  145. userlen = strlen(user);
  146. if (!ASN1_OCTET_STRING_set(id->user, (const unsigned char *)user, userlen))
  147. goto err;
  148. if (!sk_SXNETID_push(sx->ids, id))
  149. goto err;
  150. id->zone = zone;
  151. return 1;
  152. err:
  153. X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER, ERR_R_MALLOC_FAILURE);
  154. SXNETID_free(id);
  155. SXNET_free(sx);
  156. *psx = NULL;
  157. return 0;
  158. }
  159. ASN1_OCTET_STRING *SXNET_get_id_asc(SXNET *sx, const char *zone)
  160. {
  161. ASN1_INTEGER *izone;
  162. ASN1_OCTET_STRING *oct;
  163. if ((izone = s2i_ASN1_INTEGER(NULL, zone)) == NULL) {
  164. X509V3err(X509V3_F_SXNET_GET_ID_ASC, X509V3_R_ERROR_CONVERTING_ZONE);
  165. return NULL;
  166. }
  167. oct = SXNET_get_id_INTEGER(sx, izone);
  168. ASN1_INTEGER_free(izone);
  169. return oct;
  170. }
  171. ASN1_OCTET_STRING *SXNET_get_id_ulong(SXNET *sx, unsigned long lzone)
  172. {
  173. ASN1_INTEGER *izone;
  174. ASN1_OCTET_STRING *oct;
  175. if ((izone = ASN1_INTEGER_new()) == NULL
  176. || !ASN1_INTEGER_set(izone, lzone)) {
  177. X509V3err(X509V3_F_SXNET_GET_ID_ULONG, ERR_R_MALLOC_FAILURE);
  178. ASN1_INTEGER_free(izone);
  179. return NULL;
  180. }
  181. oct = SXNET_get_id_INTEGER(sx, izone);
  182. ASN1_INTEGER_free(izone);
  183. return oct;
  184. }
  185. ASN1_OCTET_STRING *SXNET_get_id_INTEGER(SXNET *sx, ASN1_INTEGER *zone)
  186. {
  187. SXNETID *id;
  188. int i;
  189. for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
  190. id = sk_SXNETID_value(sx->ids, i);
  191. if (!ASN1_INTEGER_cmp(id->zone, zone))
  192. return id->user;
  193. }
  194. return NULL;
  195. }