v3_admis.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright 2017-2023 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/conf.h>
  12. #include <openssl/types.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 ossl_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:\n", 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 namingAuthorityId: ", 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)
  112. goto err;
  113. if (entry->admissionAuthority != NULL) {
  114. if (BIO_printf(bp, "%*s admissionAuthority:\n", ind, "") <= 0
  115. || BIO_printf(bp, "%*s ", ind, "") <= 0
  116. || GENERAL_NAME_print(bp, entry->admissionAuthority) <= 0
  117. || BIO_printf(bp, "\n") <= 0)
  118. goto err;
  119. }
  120. if (entry->namingAuthority != NULL) {
  121. if (i2r_NAMING_AUTHORITY(method, entry->namingAuthority, bp, ind + 2) <= 0)
  122. goto err;
  123. }
  124. for (j = 0; j < sk_PROFESSION_INFO_num(entry->professionInfos); j++) {
  125. PROFESSION_INFO *pinfo = sk_PROFESSION_INFO_value(entry->professionInfos, j);
  126. if (BIO_printf(bp, "%*s Profession Info Entry %0d:\n", ind, "", 1 + j) <= 0)
  127. goto err;
  128. if (pinfo->registrationNumber != NULL) {
  129. if (BIO_printf(bp, "%*s registrationNumber: ", ind, "") <= 0
  130. || ASN1_STRING_print(bp, pinfo->registrationNumber) <= 0
  131. || BIO_printf(bp, "\n") <= 0)
  132. goto err;
  133. }
  134. if (pinfo->namingAuthority != NULL) {
  135. if (i2r_NAMING_AUTHORITY(method, pinfo->namingAuthority, bp, ind + 4) <= 0)
  136. goto err;
  137. }
  138. if (pinfo->professionItems != NULL) {
  139. if (BIO_printf(bp, "%*s Info Entries:\n", ind, "") <= 0)
  140. goto err;
  141. for (k = 0; k < sk_ASN1_STRING_num(pinfo->professionItems); k++) {
  142. ASN1_STRING *val = sk_ASN1_STRING_value(pinfo->professionItems, k);
  143. if (BIO_printf(bp, "%*s ", ind, "") <= 0
  144. || ASN1_STRING_print(bp, val) <= 0
  145. || BIO_printf(bp, "\n") <= 0)
  146. goto err;
  147. }
  148. }
  149. if (pinfo->professionOIDs != NULL) {
  150. if (BIO_printf(bp, "%*s Profession OIDs:\n", ind, "") <= 0)
  151. goto err;
  152. for (k = 0; k < sk_ASN1_OBJECT_num(pinfo->professionOIDs); k++) {
  153. ASN1_OBJECT *obj = sk_ASN1_OBJECT_value(pinfo->professionOIDs, k);
  154. const char *ln = OBJ_nid2ln(OBJ_obj2nid(obj));
  155. char objbuf[128];
  156. OBJ_obj2txt(objbuf, sizeof(objbuf), obj, 1);
  157. if (BIO_printf(bp, "%*s %s%s%s%s\n", ind, "",
  158. ln ? ln : "", ln ? " (" : "",
  159. objbuf, ln ? ")" : "") <= 0)
  160. goto err;
  161. }
  162. }
  163. }
  164. }
  165. return 1;
  166. err:
  167. return 0;
  168. }
  169. const ASN1_OBJECT *NAMING_AUTHORITY_get0_authorityId(const NAMING_AUTHORITY *n)
  170. {
  171. return n->namingAuthorityId;
  172. }
  173. void NAMING_AUTHORITY_set0_authorityId(NAMING_AUTHORITY *n, ASN1_OBJECT *id)
  174. {
  175. ASN1_OBJECT_free(n->namingAuthorityId);
  176. n->namingAuthorityId = id;
  177. }
  178. const ASN1_IA5STRING *NAMING_AUTHORITY_get0_authorityURL(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(const NAMING_AUTHORITY *n)
  188. {
  189. return n->namingAuthorityText;
  190. }
  191. void NAMING_AUTHORITY_set0_authorityText(NAMING_AUTHORITY *n, ASN1_STRING *t)
  192. {
  193. ASN1_IA5STRING_free(n->namingAuthorityText);
  194. n->namingAuthorityText = t;
  195. }
  196. const GENERAL_NAME *ADMISSION_SYNTAX_get0_admissionAuthority(const ADMISSION_SYNTAX *as)
  197. {
  198. return as->admissionAuthority;
  199. }
  200. void ADMISSION_SYNTAX_set0_admissionAuthority(ADMISSION_SYNTAX *as,
  201. GENERAL_NAME *aa)
  202. {
  203. GENERAL_NAME_free(as->admissionAuthority);
  204. as->admissionAuthority = aa;
  205. }
  206. const STACK_OF(ADMISSIONS) *ADMISSION_SYNTAX_get0_contentsOfAdmissions(const ADMISSION_SYNTAX *as)
  207. {
  208. return as->contentsOfAdmissions;
  209. }
  210. void ADMISSION_SYNTAX_set0_contentsOfAdmissions(ADMISSION_SYNTAX *as,
  211. STACK_OF(ADMISSIONS) *a)
  212. {
  213. sk_ADMISSIONS_pop_free(as->contentsOfAdmissions, ADMISSIONS_free);
  214. as->contentsOfAdmissions = a;
  215. }
  216. const GENERAL_NAME *ADMISSIONS_get0_admissionAuthority(const ADMISSIONS *a)
  217. {
  218. return a->admissionAuthority;
  219. }
  220. void ADMISSIONS_set0_admissionAuthority(ADMISSIONS *a, GENERAL_NAME *aa)
  221. {
  222. GENERAL_NAME_free(a->admissionAuthority);
  223. a->admissionAuthority = aa;
  224. }
  225. const NAMING_AUTHORITY *ADMISSIONS_get0_namingAuthority(const ADMISSIONS *a)
  226. {
  227. return a->namingAuthority;
  228. }
  229. void ADMISSIONS_set0_namingAuthority(ADMISSIONS *a, NAMING_AUTHORITY *na)
  230. {
  231. NAMING_AUTHORITY_free(a->namingAuthority);
  232. a->namingAuthority = na;
  233. }
  234. const PROFESSION_INFOS *ADMISSIONS_get0_professionInfos(const ADMISSIONS *a)
  235. {
  236. return a->professionInfos;
  237. }
  238. void ADMISSIONS_set0_professionInfos(ADMISSIONS *a, PROFESSION_INFOS *pi)
  239. {
  240. sk_PROFESSION_INFO_pop_free(a->professionInfos, PROFESSION_INFO_free);
  241. a->professionInfos = pi;
  242. }
  243. const ASN1_OCTET_STRING *PROFESSION_INFO_get0_addProfessionInfo(const PROFESSION_INFO *pi)
  244. {
  245. return pi->addProfessionInfo;
  246. }
  247. void PROFESSION_INFO_set0_addProfessionInfo(PROFESSION_INFO *pi,
  248. ASN1_OCTET_STRING *aos)
  249. {
  250. ASN1_OCTET_STRING_free(pi->addProfessionInfo);
  251. pi->addProfessionInfo = aos;
  252. }
  253. const NAMING_AUTHORITY *PROFESSION_INFO_get0_namingAuthority(const PROFESSION_INFO *pi)
  254. {
  255. return pi->namingAuthority;
  256. }
  257. void PROFESSION_INFO_set0_namingAuthority(PROFESSION_INFO *pi,
  258. NAMING_AUTHORITY *na)
  259. {
  260. NAMING_AUTHORITY_free(pi->namingAuthority);
  261. pi->namingAuthority = na;
  262. }
  263. const STACK_OF(ASN1_STRING) *PROFESSION_INFO_get0_professionItems(const PROFESSION_INFO *pi)
  264. {
  265. return pi->professionItems;
  266. }
  267. void PROFESSION_INFO_set0_professionItems(PROFESSION_INFO *pi,
  268. STACK_OF(ASN1_STRING) *as)
  269. {
  270. sk_ASN1_STRING_pop_free(pi->professionItems, ASN1_STRING_free);
  271. pi->professionItems = as;
  272. }
  273. const STACK_OF(ASN1_OBJECT) *PROFESSION_INFO_get0_professionOIDs(const PROFESSION_INFO *pi)
  274. {
  275. return pi->professionOIDs;
  276. }
  277. void PROFESSION_INFO_set0_professionOIDs(PROFESSION_INFO *pi,
  278. STACK_OF(ASN1_OBJECT) *po)
  279. {
  280. sk_ASN1_OBJECT_pop_free(pi->professionOIDs, ASN1_OBJECT_free);
  281. pi->professionOIDs = po;
  282. }
  283. const ASN1_PRINTABLESTRING *PROFESSION_INFO_get0_registrationNumber(const PROFESSION_INFO *pi)
  284. {
  285. return pi->registrationNumber;
  286. }
  287. void PROFESSION_INFO_set0_registrationNumber(PROFESSION_INFO *pi,
  288. ASN1_PRINTABLESTRING *rn)
  289. {
  290. ASN1_PRINTABLESTRING_free(pi->registrationNumber);
  291. pi->registrationNumber = rn;
  292. }