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