v3_genn.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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/asn1t.h>
  12. #include <openssl/conf.h>
  13. #include <openssl/x509v3.h>
  14. ASN1_SEQUENCE(OTHERNAME) = {
  15. ASN1_SIMPLE(OTHERNAME, type_id, ASN1_OBJECT),
  16. /* Maybe have a true ANY DEFINED BY later */
  17. ASN1_EXP(OTHERNAME, value, ASN1_ANY, 0)
  18. } ASN1_SEQUENCE_END(OTHERNAME)
  19. IMPLEMENT_ASN1_FUNCTIONS(OTHERNAME)
  20. ASN1_SEQUENCE(EDIPARTYNAME) = {
  21. /* DirectoryString is a CHOICE type so use explicit tagging */
  22. ASN1_EXP_OPT(EDIPARTYNAME, nameAssigner, DIRECTORYSTRING, 0),
  23. ASN1_EXP(EDIPARTYNAME, partyName, DIRECTORYSTRING, 1)
  24. } ASN1_SEQUENCE_END(EDIPARTYNAME)
  25. IMPLEMENT_ASN1_FUNCTIONS(EDIPARTYNAME)
  26. ASN1_CHOICE(GENERAL_NAME) = {
  27. ASN1_IMP(GENERAL_NAME, d.otherName, OTHERNAME, GEN_OTHERNAME),
  28. ASN1_IMP(GENERAL_NAME, d.rfc822Name, ASN1_IA5STRING, GEN_EMAIL),
  29. ASN1_IMP(GENERAL_NAME, d.dNSName, ASN1_IA5STRING, GEN_DNS),
  30. /* Don't decode this */
  31. ASN1_IMP(GENERAL_NAME, d.x400Address, ASN1_SEQUENCE, GEN_X400),
  32. /* X509_NAME is a CHOICE type so use EXPLICIT */
  33. ASN1_EXP(GENERAL_NAME, d.directoryName, X509_NAME, GEN_DIRNAME),
  34. ASN1_IMP(GENERAL_NAME, d.ediPartyName, EDIPARTYNAME, GEN_EDIPARTY),
  35. ASN1_IMP(GENERAL_NAME, d.uniformResourceIdentifier, ASN1_IA5STRING, GEN_URI),
  36. ASN1_IMP(GENERAL_NAME, d.iPAddress, ASN1_OCTET_STRING, GEN_IPADD),
  37. ASN1_IMP(GENERAL_NAME, d.registeredID, ASN1_OBJECT, GEN_RID)
  38. } ASN1_CHOICE_END(GENERAL_NAME)
  39. IMPLEMENT_ASN1_FUNCTIONS(GENERAL_NAME)
  40. ASN1_ITEM_TEMPLATE(GENERAL_NAMES) =
  41. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, GeneralNames, GENERAL_NAME)
  42. ASN1_ITEM_TEMPLATE_END(GENERAL_NAMES)
  43. IMPLEMENT_ASN1_FUNCTIONS(GENERAL_NAMES)
  44. GENERAL_NAME *GENERAL_NAME_dup(const GENERAL_NAME *a)
  45. {
  46. return (GENERAL_NAME *)ASN1_dup((i2d_of_void *)i2d_GENERAL_NAME,
  47. (d2i_of_void *)d2i_GENERAL_NAME,
  48. (char *)a);
  49. }
  50. static int edipartyname_cmp(const EDIPARTYNAME *a, const EDIPARTYNAME *b)
  51. {
  52. int res;
  53. if (a == NULL || b == NULL) {
  54. /*
  55. * Shouldn't be possible in a valid GENERAL_NAME, but we handle it
  56. * anyway. OTHERNAME_cmp treats NULL != NULL so we do the same here
  57. */
  58. return -1;
  59. }
  60. if (a->nameAssigner == NULL && b->nameAssigner != NULL)
  61. return -1;
  62. if (a->nameAssigner != NULL && b->nameAssigner == NULL)
  63. return 1;
  64. /* If we get here then both have nameAssigner set, or both unset */
  65. if (a->nameAssigner != NULL) {
  66. res = ASN1_STRING_cmp(a->nameAssigner, b->nameAssigner);
  67. if (res != 0)
  68. return res;
  69. }
  70. /*
  71. * partyName is required, so these should never be NULL. We treat it in
  72. * the same way as the a == NULL || b == NULL case above
  73. */
  74. if (a->partyName == NULL || b->partyName == NULL)
  75. return -1;
  76. return ASN1_STRING_cmp(a->partyName, b->partyName);
  77. }
  78. /* Returns 0 if they are equal, != 0 otherwise. */
  79. int GENERAL_NAME_cmp(GENERAL_NAME *a, GENERAL_NAME *b)
  80. {
  81. int result = -1;
  82. if (!a || !b || a->type != b->type)
  83. return -1;
  84. switch (a->type) {
  85. case GEN_X400:
  86. result = ASN1_TYPE_cmp(a->d.x400Address, b->d.x400Address);
  87. break;
  88. case GEN_EDIPARTY:
  89. result = edipartyname_cmp(a->d.ediPartyName, b->d.ediPartyName);
  90. break;
  91. case GEN_OTHERNAME:
  92. result = OTHERNAME_cmp(a->d.otherName, b->d.otherName);
  93. break;
  94. case GEN_EMAIL:
  95. case GEN_DNS:
  96. case GEN_URI:
  97. result = ASN1_STRING_cmp(a->d.ia5, b->d.ia5);
  98. break;
  99. case GEN_DIRNAME:
  100. result = X509_NAME_cmp(a->d.dirn, b->d.dirn);
  101. break;
  102. case GEN_IPADD:
  103. result = ASN1_OCTET_STRING_cmp(a->d.ip, b->d.ip);
  104. break;
  105. case GEN_RID:
  106. result = OBJ_cmp(a->d.rid, b->d.rid);
  107. break;
  108. }
  109. return result;
  110. }
  111. /* Returns 0 if they are equal, != 0 otherwise. */
  112. int OTHERNAME_cmp(OTHERNAME *a, OTHERNAME *b)
  113. {
  114. int result = -1;
  115. if (!a || !b)
  116. return -1;
  117. /* Check their type first. */
  118. if ((result = OBJ_cmp(a->type_id, b->type_id)) != 0)
  119. return result;
  120. /* Check the value. */
  121. result = ASN1_TYPE_cmp(a->value, b->value);
  122. return result;
  123. }
  124. void GENERAL_NAME_set0_value(GENERAL_NAME *a, int type, void *value)
  125. {
  126. switch (type) {
  127. case GEN_X400:
  128. a->d.x400Address = value;
  129. break;
  130. case GEN_EDIPARTY:
  131. a->d.ediPartyName = value;
  132. break;
  133. case GEN_OTHERNAME:
  134. a->d.otherName = value;
  135. break;
  136. case GEN_EMAIL:
  137. case GEN_DNS:
  138. case GEN_URI:
  139. a->d.ia5 = value;
  140. break;
  141. case GEN_DIRNAME:
  142. a->d.dirn = value;
  143. break;
  144. case GEN_IPADD:
  145. a->d.ip = value;
  146. break;
  147. case GEN_RID:
  148. a->d.rid = value;
  149. break;
  150. }
  151. a->type = type;
  152. }
  153. void *GENERAL_NAME_get0_value(const GENERAL_NAME *a, int *ptype)
  154. {
  155. if (ptype)
  156. *ptype = a->type;
  157. switch (a->type) {
  158. case GEN_X400:
  159. return a->d.x400Address;
  160. case GEN_EDIPARTY:
  161. return a->d.ediPartyName;
  162. case GEN_OTHERNAME:
  163. return a->d.otherName;
  164. case GEN_EMAIL:
  165. case GEN_DNS:
  166. case GEN_URI:
  167. return a->d.ia5;
  168. case GEN_DIRNAME:
  169. return a->d.dirn;
  170. case GEN_IPADD:
  171. return a->d.ip;
  172. case GEN_RID:
  173. return a->d.rid;
  174. default:
  175. return NULL;
  176. }
  177. }
  178. int GENERAL_NAME_set0_othername(GENERAL_NAME *gen,
  179. ASN1_OBJECT *oid, ASN1_TYPE *value)
  180. {
  181. OTHERNAME *oth;
  182. oth = OTHERNAME_new();
  183. if (oth == NULL)
  184. return 0;
  185. ASN1_TYPE_free(oth->value);
  186. oth->type_id = oid;
  187. oth->value = value;
  188. GENERAL_NAME_set0_value(gen, GEN_OTHERNAME, oth);
  189. return 1;
  190. }
  191. int GENERAL_NAME_get0_otherName(const GENERAL_NAME *gen,
  192. ASN1_OBJECT **poid, ASN1_TYPE **pvalue)
  193. {
  194. if (gen->type != GEN_OTHERNAME)
  195. return 0;
  196. if (poid)
  197. *poid = gen->d.otherName->type_id;
  198. if (pvalue)
  199. *pvalue = gen->d.otherName->value;
  200. return 1;
  201. }