v3_sxnet.c 6.4 KB

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