v3_admis.c 12 KB

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