v3_admis.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Copyright 2017-2018 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/ossl_typ.h>
  13. #include <openssl/asn1.h>
  14. #include <openssl/asn1t.h>
  15. #include <openssl/x509v3.h>
  16. #include <openssl/safestack.h>
  17. #include "v3_admis.h"
  18. #include "ext_dat.h"
  19. ASN1_SEQUENCE(NAMING_AUTHORITY) = {
  20. ASN1_OPT(NAMING_AUTHORITY, namingAuthorityId, ASN1_OBJECT),
  21. ASN1_OPT(NAMING_AUTHORITY, namingAuthorityUrl, ASN1_IA5STRING),
  22. ASN1_OPT(NAMING_AUTHORITY, namingAuthorityText, DIRECTORYSTRING),
  23. } ASN1_SEQUENCE_END(NAMING_AUTHORITY)
  24. ASN1_SEQUENCE(PROFESSION_INFO) = {
  25. ASN1_EXP_OPT(PROFESSION_INFO, namingAuthority, NAMING_AUTHORITY, 0),
  26. ASN1_SEQUENCE_OF(PROFESSION_INFO, professionItems, DIRECTORYSTRING),
  27. ASN1_SEQUENCE_OF_OPT(PROFESSION_INFO, professionOIDs, ASN1_OBJECT),
  28. ASN1_OPT(PROFESSION_INFO, registrationNumber, ASN1_PRINTABLESTRING),
  29. ASN1_OPT(PROFESSION_INFO, addProfessionInfo, ASN1_OCTET_STRING),
  30. } ASN1_SEQUENCE_END(PROFESSION_INFO)
  31. ASN1_SEQUENCE(ADMISSIONS) = {
  32. ASN1_EXP_OPT(ADMISSIONS, admissionAuthority, GENERAL_NAME, 0),
  33. ASN1_EXP_OPT(ADMISSIONS, namingAuthority, NAMING_AUTHORITY, 1),
  34. ASN1_SEQUENCE_OF(ADMISSIONS, professionInfos, PROFESSION_INFO),
  35. } ASN1_SEQUENCE_END(ADMISSIONS)
  36. ASN1_SEQUENCE(ADMISSION_SYNTAX) = {
  37. ASN1_OPT(ADMISSION_SYNTAX, admissionAuthority, GENERAL_NAME),
  38. ASN1_SEQUENCE_OF(ADMISSION_SYNTAX, contentsOfAdmissions, ADMISSIONS),
  39. } ASN1_SEQUENCE_END(ADMISSION_SYNTAX)
  40. IMPLEMENT_ASN1_FUNCTIONS(NAMING_AUTHORITY)
  41. IMPLEMENT_ASN1_FUNCTIONS(PROFESSION_INFO)
  42. IMPLEMENT_ASN1_FUNCTIONS(ADMISSIONS)
  43. IMPLEMENT_ASN1_FUNCTIONS(ADMISSION_SYNTAX)
  44. static int i2r_ADMISSION_SYNTAX(const struct v3_ext_method *method, void *in,
  45. BIO *bp, int ind);
  46. const X509V3_EXT_METHOD v3_ext_admission = {
  47. NID_x509ExtAdmission, /* .ext_nid = */
  48. 0, /* .ext_flags = */
  49. ASN1_ITEM_ref(ADMISSION_SYNTAX), /* .it = */
  50. NULL, NULL, NULL, NULL,
  51. NULL, /* .i2s = */
  52. NULL, /* .s2i = */
  53. NULL, /* .i2v = */
  54. NULL, /* .v2i = */
  55. &i2r_ADMISSION_SYNTAX, /* .i2r = */
  56. NULL, /* .r2i = */
  57. NULL /* extension-specific data */
  58. };
  59. static int i2r_NAMING_AUTHORITY(const struct v3_ext_method *method, void *in,
  60. BIO *bp, int ind)
  61. {
  62. NAMING_AUTHORITY * namingAuthority = (NAMING_AUTHORITY*) in;
  63. if (namingAuthority == NULL)
  64. return 0;
  65. if (namingAuthority->namingAuthorityId == NULL
  66. && namingAuthority->namingAuthorityText == NULL
  67. && namingAuthority->namingAuthorityUrl == NULL)
  68. return 0;
  69. if (BIO_printf(bp, "%*snamingAuthority: ", ind, "") <= 0)
  70. goto err;
  71. if (namingAuthority->namingAuthorityId != NULL) {
  72. char objbuf[128];
  73. const char *ln = OBJ_nid2ln(OBJ_obj2nid(namingAuthority->namingAuthorityId));
  74. if (BIO_printf(bp, "%*s admissionAuthorityId: ", ind, "") <= 0)
  75. goto err;
  76. OBJ_obj2txt(objbuf, sizeof(objbuf), namingAuthority->namingAuthorityId, 1);
  77. if (BIO_printf(bp, "%s%s%s%s\n", ln ? ln : "",
  78. ln ? " (" : "", objbuf, ln ? ")" : "") <= 0)
  79. goto err;
  80. }
  81. if (namingAuthority->namingAuthorityText != NULL) {
  82. if (BIO_printf(bp, "%*s namingAuthorityText: ", ind, "") <= 0
  83. || ASN1_STRING_print(bp, namingAuthority->namingAuthorityText) <= 0
  84. || BIO_printf(bp, "\n") <= 0)
  85. goto err;
  86. }
  87. if (namingAuthority->namingAuthorityUrl != NULL ) {
  88. if (BIO_printf(bp, "%*s namingAuthorityUrl: ", ind, "") <= 0
  89. || ASN1_STRING_print(bp, namingAuthority->namingAuthorityUrl) <= 0
  90. || BIO_printf(bp, "\n") <= 0)
  91. goto err;
  92. }
  93. return 1;
  94. err:
  95. return 0;
  96. }
  97. static int i2r_ADMISSION_SYNTAX(const struct v3_ext_method *method, void *in,
  98. BIO *bp, int ind)
  99. {
  100. ADMISSION_SYNTAX * admission = (ADMISSION_SYNTAX *)in;
  101. int i, j, k;
  102. if (admission->admissionAuthority != NULL) {
  103. if (BIO_printf(bp, "%*sadmissionAuthority:\n", ind, "") <= 0
  104. || BIO_printf(bp, "%*s ", ind, "") <= 0
  105. || GENERAL_NAME_print(bp, admission->admissionAuthority) <= 0
  106. || BIO_printf(bp, "\n") <= 0)
  107. goto err;
  108. }
  109. for (i = 0; i < sk_ADMISSIONS_num(admission->contentsOfAdmissions); i++) {
  110. ADMISSIONS* entry = sk_ADMISSIONS_value(admission->contentsOfAdmissions, i);
  111. if (BIO_printf(bp, "%*sEntry %0d:\n", ind, "", 1 + i) <= 0) goto err;
  112. if (entry->admissionAuthority != NULL) {
  113. if (BIO_printf(bp, "%*s admissionAuthority:\n", ind, "") <= 0
  114. || BIO_printf(bp, "%*s ", ind, "") <= 0
  115. || GENERAL_NAME_print(bp, entry->admissionAuthority) <= 0
  116. || BIO_printf(bp, "\n") <= 0)
  117. goto err;
  118. }
  119. if (entry->namingAuthority != NULL) {
  120. if (i2r_NAMING_AUTHORITY(method, entry->namingAuthority, bp, ind) <= 0)
  121. goto err;
  122. }
  123. for (j = 0; j < sk_PROFESSION_INFO_num(entry->professionInfos); j++) {
  124. PROFESSION_INFO* pinfo = sk_PROFESSION_INFO_value(entry->professionInfos, j);
  125. if (BIO_printf(bp, "%*s Profession Info Entry %0d:\n", ind, "", 1 + j) <= 0)
  126. goto err;
  127. if (pinfo->registrationNumber != NULL) {
  128. if (BIO_printf(bp, "%*s registrationNumber: ", ind, "") <= 0
  129. || ASN1_STRING_print(bp, pinfo->registrationNumber) <= 0
  130. || BIO_printf(bp, "\n") <= 0)
  131. goto err;
  132. }
  133. if (pinfo->namingAuthority != NULL) {
  134. if (i2r_NAMING_AUTHORITY(method, pinfo->namingAuthority, bp, ind + 2) <= 0)
  135. goto err;
  136. }
  137. if (pinfo->professionItems != NULL) {
  138. if (BIO_printf(bp, "%*s Info Entries:\n", ind, "") <= 0)
  139. goto err;
  140. for (k = 0; k < sk_ASN1_STRING_num(pinfo->professionItems); k++) {
  141. ASN1_STRING* val = sk_ASN1_STRING_value(pinfo->professionItems, k);
  142. if (BIO_printf(bp, "%*s ", ind, "") <= 0
  143. || ASN1_STRING_print(bp, val) <= 0
  144. || BIO_printf(bp, "\n") <= 0)
  145. goto err;
  146. }
  147. }
  148. if (pinfo->professionOIDs != NULL) {
  149. if (BIO_printf(bp, "%*s Profession OIDs:\n", ind, "") <= 0)
  150. goto err;
  151. for (k = 0; k < sk_ASN1_OBJECT_num(pinfo->professionOIDs); k++) {
  152. ASN1_OBJECT* obj = sk_ASN1_OBJECT_value(pinfo->professionOIDs, k);
  153. const char *ln = OBJ_nid2ln(OBJ_obj2nid(obj));
  154. char objbuf[128];
  155. OBJ_obj2txt(objbuf, sizeof(objbuf), obj, 1);
  156. if (BIO_printf(bp, "%*s %s%s%s%s\n", ind, "",
  157. ln ? ln : "", ln ? " (" : "",
  158. objbuf, ln ? ")" : "") <= 0)
  159. goto err;
  160. }
  161. }
  162. }
  163. }
  164. return 1;
  165. err:
  166. return -1;
  167. }
  168. const ASN1_OBJECT *NAMING_AUTHORITY_get0_authorityId(const NAMING_AUTHORITY *n)
  169. {
  170. return n->namingAuthorityId;
  171. }
  172. void NAMING_AUTHORITY_set0_authorityId(NAMING_AUTHORITY *n, ASN1_OBJECT* id)
  173. {
  174. ASN1_OBJECT_free(n->namingAuthorityId);
  175. n->namingAuthorityId = id;
  176. }
  177. const ASN1_IA5STRING *NAMING_AUTHORITY_get0_authorityURL(
  178. const NAMING_AUTHORITY *n)
  179. {
  180. return n->namingAuthorityUrl;
  181. }
  182. void NAMING_AUTHORITY_set0_authorityURL(NAMING_AUTHORITY *n, ASN1_IA5STRING* u)
  183. {
  184. ASN1_IA5STRING_free(n->namingAuthorityUrl);
  185. n->namingAuthorityUrl = u;
  186. }
  187. const ASN1_STRING *NAMING_AUTHORITY_get0_authorityText(
  188. const NAMING_AUTHORITY *n)
  189. {
  190. return n->namingAuthorityText;
  191. }
  192. void NAMING_AUTHORITY_set0_authorityText(NAMING_AUTHORITY *n, ASN1_STRING* t)
  193. {
  194. ASN1_IA5STRING_free(n->namingAuthorityText);
  195. n->namingAuthorityText = t;
  196. }
  197. const GENERAL_NAME *ADMISSION_SYNTAX_get0_admissionAuthority(const ADMISSION_SYNTAX *as)
  198. {
  199. return as->admissionAuthority;
  200. }
  201. void ADMISSION_SYNTAX_set0_admissionAuthority(ADMISSION_SYNTAX *as,
  202. GENERAL_NAME *aa)
  203. {
  204. GENERAL_NAME_free(as->admissionAuthority);
  205. as->admissionAuthority = aa;
  206. }
  207. const STACK_OF(ADMISSIONS) *ADMISSION_SYNTAX_get0_contentsOfAdmissions(const ADMISSION_SYNTAX *as)
  208. {
  209. return as->contentsOfAdmissions;
  210. }
  211. void ADMISSION_SYNTAX_set0_contentsOfAdmissions(ADMISSION_SYNTAX *as,
  212. STACK_OF(ADMISSIONS) *a)
  213. {
  214. sk_ADMISSIONS_pop_free(as->contentsOfAdmissions, ADMISSIONS_free);
  215. as->contentsOfAdmissions = a;
  216. }
  217. const GENERAL_NAME *ADMISSIONS_get0_admissionAuthority(const ADMISSIONS *a)
  218. {
  219. return a->admissionAuthority;
  220. }
  221. void ADMISSIONS_set0_admissionAuthority(ADMISSIONS *a, GENERAL_NAME *aa)
  222. {
  223. GENERAL_NAME_free(a->admissionAuthority);
  224. a->admissionAuthority = aa;
  225. }
  226. const NAMING_AUTHORITY *ADMISSIONS_get0_namingAuthority(const ADMISSIONS *a)
  227. {
  228. return a->namingAuthority;
  229. }
  230. void ADMISSIONS_set0_namingAuthority(ADMISSIONS *a, NAMING_AUTHORITY *na)
  231. {
  232. NAMING_AUTHORITY_free(a->namingAuthority);
  233. a->namingAuthority = na;
  234. }
  235. const PROFESSION_INFOS *ADMISSIONS_get0_professionInfos(const ADMISSIONS *a)
  236. {
  237. return a->professionInfos;
  238. }
  239. void ADMISSIONS_set0_professionInfos(ADMISSIONS *a, PROFESSION_INFOS *pi)
  240. {
  241. sk_PROFESSION_INFO_pop_free(a->professionInfos, PROFESSION_INFO_free);
  242. a->professionInfos = pi;
  243. }
  244. const ASN1_OCTET_STRING *PROFESSION_INFO_get0_addProfessionInfo(const PROFESSION_INFO *pi)
  245. {
  246. return pi->addProfessionInfo;
  247. }
  248. void PROFESSION_INFO_set0_addProfessionInfo(PROFESSION_INFO *pi,
  249. ASN1_OCTET_STRING *aos)
  250. {
  251. ASN1_OCTET_STRING_free(pi->addProfessionInfo);
  252. pi->addProfessionInfo = aos;
  253. }
  254. const NAMING_AUTHORITY *PROFESSION_INFO_get0_namingAuthority(const PROFESSION_INFO *pi)
  255. {
  256. return pi->namingAuthority;
  257. }
  258. void PROFESSION_INFO_set0_namingAuthority(PROFESSION_INFO *pi,
  259. NAMING_AUTHORITY *na)
  260. {
  261. NAMING_AUTHORITY_free(pi->namingAuthority);
  262. pi->namingAuthority = na;
  263. }
  264. const STACK_OF(ASN1_STRING) *PROFESSION_INFO_get0_professionItems(const PROFESSION_INFO *pi)
  265. {
  266. return pi->professionItems;
  267. }
  268. void PROFESSION_INFO_set0_professionItems(PROFESSION_INFO *pi,
  269. STACK_OF(ASN1_STRING) *as)
  270. {
  271. sk_ASN1_STRING_pop_free(pi->professionItems, ASN1_STRING_free);
  272. pi->professionItems = as;
  273. }
  274. const STACK_OF(ASN1_OBJECT) *PROFESSION_INFO_get0_professionOIDs(const PROFESSION_INFO *pi)
  275. {
  276. return pi->professionOIDs;
  277. }
  278. void PROFESSION_INFO_set0_professionOIDs(PROFESSION_INFO *pi,
  279. STACK_OF(ASN1_OBJECT) *po)
  280. {
  281. sk_ASN1_OBJECT_pop_free(pi->professionOIDs, ASN1_OBJECT_free);
  282. pi->professionOIDs = po;
  283. }
  284. const ASN1_PRINTABLESTRING *PROFESSION_INFO_get0_registrationNumber(const PROFESSION_INFO *pi)
  285. {
  286. return pi->registrationNumber;
  287. }
  288. void PROFESSION_INFO_set0_registrationNumber(PROFESSION_INFO *pi,
  289. ASN1_PRINTABLESTRING *rn)
  290. {
  291. ASN1_PRINTABLESTRING_free(pi->registrationNumber);
  292. pi->registrationNumber = rn;
  293. }