Finish off support for Certificate Policies extension.
[openssl.git] / crypto / x509v3 / v3_cpols.c
1 /* v3_cpols.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <stdio.h>
60 #include "cryptlib.h"
61 #include "conf.h"
62 #include "asn1.h"
63 #include "asn1_mac.h"
64 #include "x509v3.h"
65
66 /* Certificate policies extension support: this one is a bit complex... */
67
68 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol, BIO *out, int indent);
69 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, char *value);
70 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals, int indent);
71 static void print_notice(BIO *out, USERNOTICE *notice, int indent);
72 static POLICYINFO *policy_section(X509V3_CTX *ctx, STACK *polstrs);
73 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx, STACK *unot);
74 static STACK *nref_nos(STACK *nos);
75
76 X509V3_EXT_METHOD v3_cpols = {
77 NID_certificate_policies, 0,
78 (X509V3_EXT_NEW)CERTIFICATEPOLICIES_new,
79 CERTIFICATEPOLICIES_free,
80 (X509V3_EXT_D2I)d2i_CERTIFICATEPOLICIES,
81 i2d_CERTIFICATEPOLICIES,
82 NULL, NULL,
83 NULL, NULL,
84 (X509V3_EXT_I2R)i2r_certpol,
85 (X509V3_EXT_R2I)r2i_certpol,
86 NULL
87 };
88
89
90 /*
91  * ASN1err(ASN1_F_POLICYINFO_NEW,ERR_R_MALLOC_FAILURE);
92  * ASN1err(ASN1_F_D2I_POLICYINFO,ERR_R_MALLOC_FAILURE);
93  * ASN1err(ASN1_F_POLICYQUALINFO_NEW,ERR_R_MALLOC_FAILURE);
94  * ASN1err(ASN1_F_D2I_POLICYQUALINFO,ERR_R_MALLOC_FAILURE);
95  * ASN1err(ASN1_F_USERNOTICE_NEW,ERR_R_MALLOC_FAILURE);
96  * ASN1err(ASN1_F_D2I_USERNOTICE,ERR_R_MALLOC_FAILURE);
97  * ASN1err(ASN1_F_NOTICEREF_NEW,ERR_R_MALLOC_FAILURE);
98  * ASN1err(ASN1_F_D2I_NOTICEREF,ERR_R_MALLOC_FAILURE);
99  */
100
101 static STACK_OF(POLICYINFO) *r2i_certpol(method, ctx, value)
102 X509V3_EXT_METHOD *method;
103 X509V3_CTX *ctx;
104 char *value;
105 {
106         STACK_OF(POLICYINFO) *pols = NULL;
107         char *pstr;
108         POLICYINFO *pol;
109         ASN1_OBJECT *pobj;
110         STACK *vals;
111         CONF_VALUE *cnf;
112         int i;
113         pols = sk_POLICYINFO_new_null();
114         vals =  X509V3_parse_list(value);
115         for(i = 0; i < sk_num(vals); i++) {
116                 cnf = (CONF_VALUE *)sk_value(vals, i);
117                 if(cnf->value || !cnf->name ) {
118                         X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_POLICY_IDENTIFIER);
119                         X509V3_conf_err(cnf);
120                         goto err;
121                 }
122                 pstr = cnf->name;
123                 if(*pstr == '@') {
124                         STACK *polsect;
125                         polsect = X509V3_get_section(ctx, pstr + 1);
126                         if(!polsect) {
127                                 X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_SECTION);
128
129                                 X509V3_conf_err(cnf);
130                                 goto err;
131                         }
132                         pol = policy_section(ctx, polsect);
133                         X509V3_section_free(ctx, polsect);
134                         if(!pol) goto err;
135                 } else {
136                         if(!(pobj = OBJ_txt2obj(cnf->name, 0))) {
137                                 X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_OBJECT_IDENTIFIER);
138                                 X509V3_conf_err(cnf);
139                                 goto err;
140                         }
141                         pol = POLICYINFO_new();
142                         pol->policyid = pobj;
143                 }
144                 sk_POLICYINFO_push(pols, pol);
145         }
146         sk_pop_free(vals, X509V3_conf_free);
147         return pols;
148         err:
149         sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
150         return NULL;
151 }
152
153 static POLICYINFO *policy_section(ctx, polstrs)
154 X509V3_CTX *ctx;
155 STACK *polstrs;
156 {
157         int i;
158         CONF_VALUE *cnf;
159         POLICYINFO *pol;
160         POLICYQUALINFO *qual;
161         if(!(pol = POLICYINFO_new())) goto merr;
162         for(i = 0; i < sk_num(polstrs); i++) {
163                 cnf = (CONF_VALUE *)sk_value(polstrs, i);
164                 if(!strcmp(cnf->name, "policyIdentifier")) {
165                         ASN1_OBJECT *pobj;
166                         if(!(pobj = OBJ_txt2obj(cnf->value, 0))) {
167                                 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_OBJECT_IDENTIFIER);
168                                 X509V3_conf_err(cnf);
169                                 goto err;
170                         }
171                         pol->policyid = pobj;
172
173                 } else if(!name_cmp(cnf->name, "CPS")) {
174                         if(!pol->qualifiers) pol->qualifiers =
175                                                  sk_POLICYQUALINFO_new_null();
176                         if(!(qual = POLICYQUALINFO_new())) goto merr;
177                         if(!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
178                                                                  goto merr;
179                         qual->pqualid = OBJ_nid2obj(NID_id_qt_cps);
180                         qual->d.cpsuri = ASN1_IA5STRING_new();
181                         if(!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
182                                                  strlen(cnf->value))) goto merr;
183                 } else if(!name_cmp(cnf->name, "userNotice")) {
184                         STACK *unot;
185                         if(*cnf->value != '@') {
186                                 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_EXPECTED_A_SECTION_NAME);
187                                 X509V3_conf_err(cnf);
188                                 goto err;
189                         }
190                         unot = X509V3_get_section(ctx, cnf->value + 1);
191                         if(!unot) {
192                                 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_SECTION);
193
194                                 X509V3_conf_err(cnf);
195                                 goto err;
196                         }
197                         qual = notice_section(ctx, unot);
198                         X509V3_section_free(ctx, unot);
199                         if(!qual) goto err;
200                         if(!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
201                                                                  goto merr;
202                 } else {
203                         X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_OPTION);
204
205                         X509V3_conf_err(cnf);
206                         goto err;
207                 }
208         }
209         if(!pol->policyid) {
210                 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_NO_POLICY_IDENTIFIER);
211                 goto err;
212         }
213
214         return pol;
215
216         err:
217         POLICYINFO_free(pol);
218         return NULL;
219         
220         merr:
221         X509V3err(X509V3_F_POLICY_SECTION,ERR_R_MALLOC_FAILURE);
222         POLICYINFO_free(pol);
223         return NULL;
224         
225 }
226
227 static POLICYQUALINFO *notice_section(ctx, unot)
228 X509V3_CTX *ctx;
229 STACK *unot;
230 {
231         int i;
232         CONF_VALUE *cnf;
233         USERNOTICE *not;
234         POLICYQUALINFO *qual;
235         if(!(qual = POLICYQUALINFO_new())) goto merr;
236         qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice);
237         if(!(not = USERNOTICE_new())) goto merr;
238         qual->d.usernotice = not;
239         for(i = 0; i < sk_num(unot); i++) {
240                 cnf = (CONF_VALUE *)sk_value(unot, i);
241                 if(!strcmp(cnf->name, "explicitText")) {
242                         not->exptext = ASN1_VISIBLESTRING_new();
243                         if(!ASN1_STRING_set(not->exptext, cnf->value,
244                                                  strlen(cnf->value))) goto merr;
245                 } else if(!strcmp(cnf->name, "organization")) {
246                         NOTICEREF *nref;
247                         if(!not->noticeref) {
248                                 if(!(nref = NOTICEREF_new())) goto merr;
249                                 not->noticeref = nref;
250                         } else nref = not->noticeref;
251                         nref->organization = ASN1_VISIBLESTRING_new();
252                         if(!ASN1_STRING_set(nref->organization, cnf->value,
253                                                  strlen(cnf->value))) goto merr;
254                 } else if(!strcmp(cnf->name, "noticeNumbers")) {
255                         NOTICEREF *nref;
256                         STACK *nos;
257                         if(!not->noticeref) {
258                                 if(!(nref = NOTICEREF_new())) goto merr;
259                                 not->noticeref = nref;
260                         } else nref = not->noticeref;
261                         nos = X509V3_parse_list(cnf->value);
262                         if(!nos || !sk_num(nos)) {
263                                 X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_INVALID_NUMBERS);
264                                 X509V3_conf_err(cnf);
265                                 goto err;
266                         }
267                         nref->noticenos = nref_nos(nos);
268                         sk_pop_free(nos, X509V3_conf_free);
269                         if(!nref->noticenos) goto err;
270                 } else {
271                         X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_INVALID_OPTION);
272
273                         X509V3_conf_err(cnf);
274                         goto err;
275                 }
276         }
277
278         if(not->noticeref && 
279               (!not->noticeref->noticenos || !not->noticeref->organization)) {
280                         X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
281                         goto err;
282         }
283
284         return qual;
285
286         err:
287         POLICYQUALINFO_free(qual);
288         return NULL;
289
290         merr:
291         X509V3err(X509V3_F_NOTICE_SECTION,ERR_R_MALLOC_FAILURE);
292         POLICYQUALINFO_free(qual);
293         return NULL;
294 }
295
296 static STACK *nref_nos(nos)
297 STACK *nos;
298 {
299         STACK *nnums;
300         CONF_VALUE *cnf;
301         ASN1_INTEGER *aint;
302         int i;
303         if(!(nnums = sk_new_null())) goto merr;
304         for(i = 0; i < sk_num(nos); i++) {
305                 cnf = (CONF_VALUE *)sk_value(nos, i);
306                 if(!(aint = s2i_ASN1_INTEGER(NULL, cnf->name))) {
307                         X509V3err(X509V3_F_NREF_NOS,X509V3_R_INVALID_NUMBER);
308                         goto err;
309                 }
310                 if(!sk_push(nnums, (char *)aint)) goto merr;
311         }
312         return nnums;
313
314         err:
315         sk_pop_free(nnums, ASN1_STRING_free);
316         return NULL;
317
318         merr:
319         X509V3err(X509V3_F_NOTICE_SECTION,ERR_R_MALLOC_FAILURE);
320         sk_pop_free(nnums, ASN1_STRING_free);
321         return NULL;
322 }
323
324
325 static int i2r_certpol(method, pol, out, indent)
326 X509V3_EXT_METHOD *method;
327 STACK_OF(POLICYINFO) *pol;
328 BIO *out;
329 int indent;
330 {
331         int i;
332         POLICYINFO *pinfo;
333         /* First print out the policy OIDs */
334         for(i = 0; i < sk_POLICYINFO_num(pol); i++) {
335                 pinfo = sk_POLICYINFO_value(pol, i);
336                 BIO_printf(out, "%*sPolicy: ", indent, "");
337                 i2a_ASN1_OBJECT(out, pinfo->policyid);
338                 BIO_puts(out, "\n");
339                 if(pinfo->qualifiers)
340                          print_qualifiers(out, pinfo->qualifiers, indent + 2);
341         }
342         return 1;
343 }
344
345
346 int i2d_CERTIFICATEPOLICIES(a, pp)
347 STACK_OF(POLICYINFO) *a;
348 unsigned char **pp;
349 {
350
351 return i2d_ASN1_SET_OF_POLICYINFO(a, pp, i2d_POLICYINFO, V_ASN1_SEQUENCE,
352                                                  V_ASN1_UNIVERSAL, IS_SEQUENCE);}
353
354 STACK_OF(POLICYINFO) *CERTIFICATEPOLICIES_new()
355 {
356         return sk_POLICYINFO_new_null();
357 }
358
359 void CERTIFICATEPOLICIES_free(a)
360 STACK_OF(POLICYINFO) *a;
361 {
362         sk_POLICYINFO_pop_free(a, POLICYINFO_free);
363 }
364
365 STACK_OF(POLICYINFO) *d2i_CERTIFICATEPOLICIES(a,pp,length)
366 STACK_OF(POLICYINFO) **a;
367 unsigned char **pp;
368 long length;
369 {
370 return d2i_ASN1_SET_OF_POLICYINFO(a, pp, length, d2i_POLICYINFO,
371                          POLICYINFO_free, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL);
372
373 }
374
375 IMPLEMENT_STACK_OF(POLICYINFO)
376 IMPLEMENT_ASN1_SET_OF(POLICYINFO)
377
378 int i2d_POLICYINFO(a,pp)
379 POLICYINFO *a;
380 unsigned char **pp;
381 {
382         M_ASN1_I2D_vars(a);
383
384         M_ASN1_I2D_len (a->policyid, i2d_ASN1_OBJECT);
385         M_ASN1_I2D_len_SEQUENCE_type(POLICYQUALINFO, a->qualifiers,
386                                                          i2d_POLICYQUALINFO);
387
388         M_ASN1_I2D_seq_total();
389
390         M_ASN1_I2D_put (a->policyid, i2d_ASN1_OBJECT);
391         M_ASN1_I2D_put_SEQUENCE_type(POLICYQUALINFO, a->qualifiers,
392                                                          i2d_POLICYQUALINFO);
393
394         M_ASN1_I2D_finish();
395 }
396
397 POLICYINFO *POLICYINFO_new()
398 {
399         POLICYINFO *ret=NULL;
400         ASN1_CTX c;
401         M_ASN1_New_Malloc(ret, POLICYINFO);
402         ret->policyid = NULL;
403         ret->qualifiers = NULL;
404         return (ret);
405         M_ASN1_New_Error(ASN1_F_POLICYINFO_NEW);
406 }
407
408 POLICYINFO *d2i_POLICYINFO(a,pp,length)
409 POLICYINFO **a;
410 unsigned char **pp;
411 long length;
412 {
413         M_ASN1_D2I_vars(a,POLICYINFO *,POLICYINFO_new);
414         M_ASN1_D2I_Init();
415         M_ASN1_D2I_start_sequence();
416         M_ASN1_D2I_get(ret->policyid, d2i_ASN1_OBJECT);
417         if(!M_ASN1_D2I_end_sequence()) {
418                 M_ASN1_D2I_get_seq_type (POLICYQUALINFO, ret->qualifiers,
419                                  d2i_POLICYQUALINFO, POLICYQUALINFO_free);
420         }
421         M_ASN1_D2I_Finish(a, POLICYINFO_free, ASN1_F_D2I_POLICYINFO);
422 }
423
424 void POLICYINFO_free(a)
425 POLICYINFO *a;
426 {
427         if (a == NULL) return;
428         ASN1_OBJECT_free(a->policyid);
429         sk_POLICYQUALINFO_pop_free(a->qualifiers, POLICYQUALINFO_free);
430         Free (a);
431 }
432
433 static void print_qualifiers(out, quals, indent)
434 BIO *out;
435 STACK_OF(POLICYQUALINFO) *quals;
436 int indent;
437 {
438         POLICYQUALINFO *qualinfo;
439         int i;
440         for(i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
441                 qualinfo = sk_POLICYQUALINFO_value(quals, i);
442                 switch(OBJ_obj2nid(qualinfo->pqualid))
443                 {
444                         case NID_id_qt_cps:
445                         BIO_printf(out, "%*sCPS: %s\n", indent, "",
446                                                 qualinfo->d.cpsuri->data);
447                         break;
448                 
449                         case NID_id_qt_unotice:
450                         BIO_printf(out, "%*sUser Notice:\n", indent, "");
451                         print_notice(out, qualinfo->d.usernotice, indent + 2);
452                         break;
453
454                         default:
455                         BIO_printf(out, "%*sUnknown Qualifier: ",
456                                                          indent + 2, "");
457                         
458                         i2a_ASN1_OBJECT(out, qualinfo->pqualid);
459                         BIO_puts(out, "\n");
460                         break;
461                 }
462         }
463 }
464
465 static void print_notice(out, notice, indent)
466 BIO *out;
467 USERNOTICE *notice;
468 int indent;
469 {
470         int i;
471         if(notice->noticeref) {
472                 NOTICEREF *ref;
473                 ref = notice->noticeref;
474                 BIO_printf(out, "%*sOrganization: %s\n", indent, "",
475                                                  ref->organization->data);
476                 BIO_printf(out, "%*sNumber%s: ", indent, "",
477                                  (sk_num(ref->noticenos) > 1) ? "s" : "");
478                 for(i = 0; i < sk_num(ref->noticenos); i++) {
479                         ASN1_INTEGER *num;
480                         char *tmp;
481                         num = (ASN1_INTEGER *)sk_value(ref->noticenos, i);
482                         if(i) BIO_puts(out, ", ");
483                         tmp = i2s_ASN1_INTEGER(NULL, num);
484                         BIO_puts(out, tmp);
485                         Free(tmp);
486                 }
487                 BIO_puts(out, "\n");
488         }
489         if(notice->exptext)
490                 BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
491                                                          notice->exptext->data);
492 }
493                 
494         
495
496 int i2d_POLICYQUALINFO(a,pp)
497 POLICYQUALINFO *a;
498 unsigned char **pp;
499 {
500         M_ASN1_I2D_vars(a);
501
502         M_ASN1_I2D_len (a->pqualid, i2d_ASN1_OBJECT);
503         switch(OBJ_obj2nid(a->pqualid)) {
504                 case NID_id_qt_cps:
505                 M_ASN1_I2D_len(a->d.cpsuri, i2d_ASN1_IA5STRING);
506                 break;
507
508                 case NID_id_qt_unotice:
509                 M_ASN1_I2D_len(a->d.usernotice, i2d_USERNOTICE);
510                 break;
511
512                 default:
513                 M_ASN1_I2D_len(a->d.other, i2d_ASN1_TYPE);
514                 break;
515         }
516
517         M_ASN1_I2D_seq_total();
518
519         M_ASN1_I2D_put (a->pqualid, i2d_ASN1_OBJECT);
520         switch(OBJ_obj2nid(a->pqualid)) {
521                 case NID_id_qt_cps:
522                 M_ASN1_I2D_put(a->d.cpsuri, i2d_ASN1_IA5STRING);
523                 break;
524
525                 case NID_id_qt_unotice:
526                 M_ASN1_I2D_put(a->d.usernotice, i2d_USERNOTICE);
527                 break;
528
529                 default:
530                 M_ASN1_I2D_put(a->d.other, i2d_ASN1_TYPE);
531                 break;
532         }
533
534         M_ASN1_I2D_finish();
535 }
536
537 POLICYQUALINFO *POLICYQUALINFO_new()
538 {
539         POLICYQUALINFO *ret=NULL;
540         ASN1_CTX c;
541         M_ASN1_New_Malloc(ret, POLICYQUALINFO);
542         ret->pqualid = NULL;
543         ret->d.other = NULL;
544         return (ret);
545         M_ASN1_New_Error(ASN1_F_POLICYQUALINFO_NEW);
546 }
547
548 POLICYQUALINFO *d2i_POLICYQUALINFO(a,pp,length)
549 POLICYQUALINFO **a;
550 unsigned char **pp;
551 long length;
552 {
553         M_ASN1_D2I_vars(a,POLICYQUALINFO *,POLICYQUALINFO_new);
554         M_ASN1_D2I_Init();
555         M_ASN1_D2I_start_sequence();
556         M_ASN1_D2I_get (ret->pqualid, d2i_ASN1_OBJECT);
557         switch(OBJ_obj2nid(ret->pqualid)) {
558                 case NID_id_qt_cps:
559                 M_ASN1_D2I_get(ret->d.cpsuri, d2i_ASN1_IA5STRING);
560                 break;
561
562                 case NID_id_qt_unotice:
563                 M_ASN1_D2I_get(ret->d.usernotice, d2i_USERNOTICE);
564                 break;
565
566                 default:
567                 M_ASN1_D2I_get(ret->d.other, d2i_ASN1_TYPE);
568                 break;
569         }
570         M_ASN1_D2I_Finish(a, POLICYQUALINFO_free, ASN1_F_D2I_POLICYQUALINFO);
571 }
572
573 void POLICYQUALINFO_free(a)
574 POLICYQUALINFO *a;
575 {
576         if (a == NULL) return;
577         switch(OBJ_obj2nid(a->pqualid)) {
578                 case NID_id_qt_cps:
579                 ASN1_IA5STRING_free(a->d.cpsuri);
580                 break;
581
582                 case NID_id_qt_unotice:
583                 USERNOTICE_free(a->d.usernotice);
584                 break;
585
586                 default:
587                 ASN1_TYPE_free(a->d.other);
588                 break;
589         }
590         
591         ASN1_OBJECT_free(a->pqualid);
592         Free (a);
593 }
594
595 int i2d_USERNOTICE(a,pp)
596 USERNOTICE *a;
597 unsigned char **pp;
598 {
599         M_ASN1_I2D_vars(a);
600
601         M_ASN1_I2D_len (a->noticeref, i2d_NOTICEREF);
602         M_ASN1_I2D_len (a->exptext, i2d_DISPLAYTEXT);
603
604         M_ASN1_I2D_seq_total();
605
606         M_ASN1_I2D_put (a->noticeref, i2d_NOTICEREF);
607         M_ASN1_I2D_put (a->exptext, i2d_DISPLAYTEXT);
608
609         M_ASN1_I2D_finish();
610 }
611
612 USERNOTICE *USERNOTICE_new()
613 {
614         USERNOTICE *ret=NULL;
615         ASN1_CTX c;
616         M_ASN1_New_Malloc(ret, USERNOTICE);
617         ret->noticeref = NULL;
618         ret->exptext = NULL;
619         return (ret);
620         M_ASN1_New_Error(ASN1_F_USERNOTICE_NEW);
621 }
622
623 USERNOTICE *d2i_USERNOTICE(a,pp,length)
624 USERNOTICE **a;
625 unsigned char **pp;
626 long length;
627 {
628         M_ASN1_D2I_vars(a,USERNOTICE *,USERNOTICE_new);
629         M_ASN1_D2I_Init();
630         M_ASN1_D2I_start_sequence();
631         M_ASN1_D2I_get_opt(ret->noticeref, d2i_NOTICEREF, V_ASN1_SEQUENCE);
632         if (!M_ASN1_D2I_end_sequence()) {
633                 M_ASN1_D2I_get(ret->exptext, d2i_DISPLAYTEXT);
634         }
635         M_ASN1_D2I_Finish(a, USERNOTICE_free, ASN1_F_D2I_USERNOTICE);
636 }
637
638 void USERNOTICE_free(a)
639 USERNOTICE *a;
640 {
641         if (a == NULL) return;
642         NOTICEREF_free(a->noticeref);
643         DISPLAYTEXT_free(a->exptext);
644         Free (a);
645 }
646
647 int i2d_NOTICEREF(a,pp)
648 NOTICEREF *a;
649 unsigned char **pp;
650 {
651         M_ASN1_I2D_vars(a);
652
653         M_ASN1_I2D_len (a->organization, i2d_DISPLAYTEXT);
654         M_ASN1_I2D_len_SEQUENCE(a->noticenos, i2d_ASN1_INTEGER);
655
656         M_ASN1_I2D_seq_total();
657
658         M_ASN1_I2D_put (a->organization, i2d_DISPLAYTEXT);
659         M_ASN1_I2D_put_SEQUENCE(a->noticenos, i2d_ASN1_INTEGER);
660
661         M_ASN1_I2D_finish();
662 }
663
664 NOTICEREF *NOTICEREF_new()
665 {
666         NOTICEREF *ret=NULL;
667         ASN1_CTX c;
668         M_ASN1_New_Malloc(ret, NOTICEREF);
669         ret->organization = NULL;
670         ret->noticenos = NULL;
671         return (ret);
672         M_ASN1_New_Error(ASN1_F_NOTICEREF_NEW);
673 }
674
675 NOTICEREF *d2i_NOTICEREF(a,pp,length)
676 NOTICEREF **a;
677 unsigned char **pp;
678 long length;
679 {
680         M_ASN1_D2I_vars(a,NOTICEREF *,NOTICEREF_new);
681         M_ASN1_D2I_Init();
682         M_ASN1_D2I_start_sequence();
683         /* This is to cope with some broken encodings that use IA5STRING for
684          * the organization field
685          */
686         M_ASN1_D2I_get_opt(ret->organization, d2i_ASN1_IA5STRING,
687                                                          V_ASN1_IA5STRING);
688         if(!ret->organization) {
689                  M_ASN1_D2I_get(ret->organization, d2i_DISPLAYTEXT);
690         }
691         M_ASN1_D2I_get_seq(ret->noticenos, d2i_ASN1_INTEGER, ASN1_STRING_free);
692         M_ASN1_D2I_Finish(a, NOTICEREF_free, ASN1_F_D2I_NOTICEREF);
693 }
694
695 void NOTICEREF_free(a)
696 NOTICEREF *a;
697 {
698         if (a == NULL) return;
699         DISPLAYTEXT_free(a->organization);
700         sk_pop_free(a->noticenos, ASN1_STRING_free);
701         Free (a);
702 }
703
704 IMPLEMENT_STACK_OF(POLICYQUALINFO)
705 IMPLEMENT_ASN1_SET_OF(POLICYQUALINFO)