v3_genn.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. ASN1_IMP_OPT(EDIPARTYNAME, nameAssigner, DIRECTORYSTRING, 0),
  22. ASN1_IMP_OPT(EDIPARTYNAME, partyName, DIRECTORYSTRING, 1)
  23. } ASN1_SEQUENCE_END(EDIPARTYNAME)
  24. IMPLEMENT_ASN1_FUNCTIONS(EDIPARTYNAME)
  25. ASN1_CHOICE(GENERAL_NAME) = {
  26. ASN1_IMP(GENERAL_NAME, d.otherName, OTHERNAME, GEN_OTHERNAME),
  27. ASN1_IMP(GENERAL_NAME, d.rfc822Name, ASN1_IA5STRING, GEN_EMAIL),
  28. ASN1_IMP(GENERAL_NAME, d.dNSName, ASN1_IA5STRING, GEN_DNS),
  29. /* Don't decode this */
  30. ASN1_IMP(GENERAL_NAME, d.x400Address, ASN1_SEQUENCE, GEN_X400),
  31. /* X509_NAME is a CHOICE type so use EXPLICIT */
  32. ASN1_EXP(GENERAL_NAME, d.directoryName, X509_NAME, GEN_DIRNAME),
  33. ASN1_IMP(GENERAL_NAME, d.ediPartyName, EDIPARTYNAME, GEN_EDIPARTY),
  34. ASN1_IMP(GENERAL_NAME, d.uniformResourceIdentifier, ASN1_IA5STRING, GEN_URI),
  35. ASN1_IMP(GENERAL_NAME, d.iPAddress, ASN1_OCTET_STRING, GEN_IPADD),
  36. ASN1_IMP(GENERAL_NAME, d.registeredID, ASN1_OBJECT, GEN_RID)
  37. } ASN1_CHOICE_END(GENERAL_NAME)
  38. IMPLEMENT_ASN1_FUNCTIONS(GENERAL_NAME)
  39. ASN1_ITEM_TEMPLATE(GENERAL_NAMES) =
  40. ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, GeneralNames, GENERAL_NAME)
  41. ASN1_ITEM_TEMPLATE_END(GENERAL_NAMES)
  42. IMPLEMENT_ASN1_FUNCTIONS(GENERAL_NAMES)
  43. GENERAL_NAME *GENERAL_NAME_dup(const GENERAL_NAME *a)
  44. {
  45. return (GENERAL_NAME *)ASN1_dup((i2d_of_void *)i2d_GENERAL_NAME,
  46. (d2i_of_void *)d2i_GENERAL_NAME,
  47. (char *)a);
  48. }
  49. /* Returns 0 if they are equal, != 0 otherwise. */
  50. int GENERAL_NAME_cmp(GENERAL_NAME *a, GENERAL_NAME *b)
  51. {
  52. int result = -1;
  53. if (!a || !b || a->type != b->type)
  54. return -1;
  55. switch (a->type) {
  56. case GEN_X400:
  57. case GEN_EDIPARTY:
  58. result = ASN1_TYPE_cmp(a->d.other, b->d.other);
  59. break;
  60. case GEN_OTHERNAME:
  61. result = OTHERNAME_cmp(a->d.otherName, b->d.otherName);
  62. break;
  63. case GEN_EMAIL:
  64. case GEN_DNS:
  65. case GEN_URI:
  66. result = ASN1_STRING_cmp(a->d.ia5, b->d.ia5);
  67. break;
  68. case GEN_DIRNAME:
  69. result = X509_NAME_cmp(a->d.dirn, b->d.dirn);
  70. break;
  71. case GEN_IPADD:
  72. result = ASN1_OCTET_STRING_cmp(a->d.ip, b->d.ip);
  73. break;
  74. case GEN_RID:
  75. result = OBJ_cmp(a->d.rid, b->d.rid);
  76. break;
  77. }
  78. return result;
  79. }
  80. /* Returns 0 if they are equal, != 0 otherwise. */
  81. int OTHERNAME_cmp(OTHERNAME *a, OTHERNAME *b)
  82. {
  83. int result = -1;
  84. if (!a || !b)
  85. return -1;
  86. /* Check their type first. */
  87. if ((result = OBJ_cmp(a->type_id, b->type_id)) != 0)
  88. return result;
  89. /* Check the value. */
  90. result = ASN1_TYPE_cmp(a->value, b->value);
  91. return result;
  92. }
  93. void GENERAL_NAME_set0_value(GENERAL_NAME *a, int type, void *value)
  94. {
  95. switch (type) {
  96. case GEN_X400:
  97. case GEN_EDIPARTY:
  98. a->d.other = value;
  99. break;
  100. case GEN_OTHERNAME:
  101. a->d.otherName = value;
  102. break;
  103. case GEN_EMAIL:
  104. case GEN_DNS:
  105. case GEN_URI:
  106. a->d.ia5 = value;
  107. break;
  108. case GEN_DIRNAME:
  109. a->d.dirn = value;
  110. break;
  111. case GEN_IPADD:
  112. a->d.ip = value;
  113. break;
  114. case GEN_RID:
  115. a->d.rid = value;
  116. break;
  117. }
  118. a->type = type;
  119. }
  120. void *GENERAL_NAME_get0_value(const GENERAL_NAME *a, int *ptype)
  121. {
  122. if (ptype)
  123. *ptype = a->type;
  124. switch (a->type) {
  125. case GEN_X400:
  126. case GEN_EDIPARTY:
  127. return a->d.other;
  128. case GEN_OTHERNAME:
  129. return a->d.otherName;
  130. case GEN_EMAIL:
  131. case GEN_DNS:
  132. case GEN_URI:
  133. return a->d.ia5;
  134. case GEN_DIRNAME:
  135. return a->d.dirn;
  136. case GEN_IPADD:
  137. return a->d.ip;
  138. case GEN_RID:
  139. return a->d.rid;
  140. default:
  141. return NULL;
  142. }
  143. }
  144. int GENERAL_NAME_set0_othername(GENERAL_NAME *gen,
  145. ASN1_OBJECT *oid, ASN1_TYPE *value)
  146. {
  147. OTHERNAME *oth;
  148. oth = OTHERNAME_new();
  149. if (oth == NULL)
  150. return 0;
  151. ASN1_TYPE_free(oth->value);
  152. oth->type_id = oid;
  153. oth->value = value;
  154. GENERAL_NAME_set0_value(gen, GEN_OTHERNAME, oth);
  155. return 1;
  156. }
  157. int GENERAL_NAME_get0_otherName(const GENERAL_NAME *gen,
  158. ASN1_OBJECT **poid, ASN1_TYPE **pvalue)
  159. {
  160. if (gen->type != GEN_OTHERNAME)
  161. return 0;
  162. if (poid)
  163. *poid = gen->d.otherName->type_id;
  164. if (pvalue)
  165. *pvalue = gen->d.otherName->value;
  166. return 1;
  167. }