v3_sxnet.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 1999-2022 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. int64_t v;
  52. char *tmp;
  53. SXNETID *id;
  54. int i;
  55. /*
  56. * Since we add 1 to the version number to display it, we don't support
  57. * LONG_MAX since that would cause on overflow.
  58. */
  59. if (!ASN1_INTEGER_get_int64(&v, sx->version)
  60. || v >= LONG_MAX
  61. || v < LONG_MIN) {
  62. BIO_printf(out, "%*sVersion: <unsupported>", indent, "");
  63. } else {
  64. long vl = (long)v;
  65. BIO_printf(out, "%*sVersion: %ld (0x%lX)", indent, "", vl + 1, vl);
  66. }
  67. for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
  68. id = sk_SXNETID_value(sx->ids, i);
  69. tmp = i2s_ASN1_INTEGER(NULL, id->zone);
  70. BIO_printf(out, "\n%*sZone: %s, User: ", indent, "", tmp);
  71. OPENSSL_free(tmp);
  72. ASN1_STRING_print(out, id->user);
  73. }
  74. return 1;
  75. }
  76. #ifdef SXNET_TEST
  77. /*
  78. * NBB: this is used for testing only. It should *not* be used for anything
  79. * else because it will just take static IDs from the configuration file and
  80. * they should really be separate values for each user.
  81. */
  82. static SXNET *sxnet_v2i(X509V3_EXT_METHOD *method, X509V3_CTX *ctx,
  83. STACK_OF(CONF_VALUE) *nval)
  84. {
  85. CONF_VALUE *cnf;
  86. SXNET *sx = NULL;
  87. int i;
  88. for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
  89. cnf = sk_CONF_VALUE_value(nval, i);
  90. if (!SXNET_add_id_asc(&sx, cnf->name, cnf->value, -1))
  91. return NULL;
  92. }
  93. return sx;
  94. }
  95. #endif
  96. /* Strong Extranet utility functions */
  97. /* Add an id given the zone as an ASCII number */
  98. int SXNET_add_id_asc(SXNET **psx, const char *zone, const char *user, int userlen)
  99. {
  100. ASN1_INTEGER *izone;
  101. if ((izone = s2i_ASN1_INTEGER(NULL, zone)) == NULL) {
  102. X509V3err(X509V3_F_SXNET_ADD_ID_ASC, X509V3_R_ERROR_CONVERTING_ZONE);
  103. return 0;
  104. }
  105. return SXNET_add_id_INTEGER(psx, izone, user, userlen);
  106. }
  107. /* Add an id given the zone as an unsigned long */
  108. int SXNET_add_id_ulong(SXNET **psx, unsigned long lzone, const char *user,
  109. int userlen)
  110. {
  111. ASN1_INTEGER *izone;
  112. if ((izone = ASN1_INTEGER_new()) == NULL
  113. || !ASN1_INTEGER_set(izone, lzone)) {
  114. X509V3err(X509V3_F_SXNET_ADD_ID_ULONG, ERR_R_MALLOC_FAILURE);
  115. ASN1_INTEGER_free(izone);
  116. return 0;
  117. }
  118. return SXNET_add_id_INTEGER(psx, izone, user, userlen);
  119. }
  120. /*
  121. * Add an id given the zone as an ASN1_INTEGER. Note this version uses the
  122. * passed integer and doesn't make a copy so don't free it up afterwards.
  123. */
  124. int SXNET_add_id_INTEGER(SXNET **psx, ASN1_INTEGER *zone, const char *user,
  125. int userlen)
  126. {
  127. SXNET *sx = NULL;
  128. SXNETID *id = NULL;
  129. if (!psx || !zone || !user) {
  130. X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER,
  131. X509V3_R_INVALID_NULL_ARGUMENT);
  132. return 0;
  133. }
  134. if (userlen == -1)
  135. userlen = strlen(user);
  136. if (userlen > 64) {
  137. X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER, X509V3_R_USER_TOO_LONG);
  138. return 0;
  139. }
  140. if (*psx == NULL) {
  141. if ((sx = SXNET_new()) == NULL)
  142. goto err;
  143. if (!ASN1_INTEGER_set(sx->version, 0))
  144. goto err;
  145. *psx = sx;
  146. } else
  147. sx = *psx;
  148. if (SXNET_get_id_INTEGER(sx, zone)) {
  149. X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER, X509V3_R_DUPLICATE_ZONE_ID);
  150. return 0;
  151. }
  152. if ((id = SXNETID_new()) == NULL)
  153. goto err;
  154. if (userlen == -1)
  155. userlen = strlen(user);
  156. if (!ASN1_OCTET_STRING_set(id->user, (const unsigned char *)user, userlen))
  157. goto err;
  158. if (!sk_SXNETID_push(sx->ids, id))
  159. goto err;
  160. id->zone = zone;
  161. return 1;
  162. err:
  163. X509V3err(X509V3_F_SXNET_ADD_ID_INTEGER, ERR_R_MALLOC_FAILURE);
  164. SXNETID_free(id);
  165. SXNET_free(sx);
  166. *psx = NULL;
  167. return 0;
  168. }
  169. ASN1_OCTET_STRING *SXNET_get_id_asc(SXNET *sx, const char *zone)
  170. {
  171. ASN1_INTEGER *izone;
  172. ASN1_OCTET_STRING *oct;
  173. if ((izone = s2i_ASN1_INTEGER(NULL, zone)) == NULL) {
  174. X509V3err(X509V3_F_SXNET_GET_ID_ASC, X509V3_R_ERROR_CONVERTING_ZONE);
  175. return NULL;
  176. }
  177. oct = SXNET_get_id_INTEGER(sx, izone);
  178. ASN1_INTEGER_free(izone);
  179. return oct;
  180. }
  181. ASN1_OCTET_STRING *SXNET_get_id_ulong(SXNET *sx, unsigned long lzone)
  182. {
  183. ASN1_INTEGER *izone;
  184. ASN1_OCTET_STRING *oct;
  185. if ((izone = ASN1_INTEGER_new()) == NULL
  186. || !ASN1_INTEGER_set(izone, lzone)) {
  187. X509V3err(X509V3_F_SXNET_GET_ID_ULONG, ERR_R_MALLOC_FAILURE);
  188. ASN1_INTEGER_free(izone);
  189. return NULL;
  190. }
  191. oct = SXNET_get_id_INTEGER(sx, izone);
  192. ASN1_INTEGER_free(izone);
  193. return oct;
  194. }
  195. ASN1_OCTET_STRING *SXNET_get_id_INTEGER(SXNET *sx, ASN1_INTEGER *zone)
  196. {
  197. SXNETID *id;
  198. int i;
  199. for (i = 0; i < sk_SXNETID_num(sx->ids); i++) {
  200. id = sk_SXNETID_value(sx->ids, i);
  201. if (!ASN1_INTEGER_cmp(id->zone, zone))
  202. return id->user;
  203. }
  204. return NULL;
  205. }