Allows PKCS#12 password to be placed on command line and add allow config
[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 <openssl/conf.h>
62 #include <openssl/asn1.h>
63 #include <openssl/asn1_mac.h>
64 #include <openssl/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, int ia5org);
73 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx, STACK *unot, int ia5org);
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 static STACK_OF(POLICYINFO) *r2i_certpol(X509V3_EXT_METHOD *method,
91                 X509V3_CTX *ctx, char *value)
92 {
93         STACK_OF(POLICYINFO) *pols = NULL;
94         char *pstr;
95         POLICYINFO *pol;
96         ASN1_OBJECT *pobj;
97         STACK *vals;
98         CONF_VALUE *cnf;
99         int i, ia5org;
100         pols = sk_POLICYINFO_new_null();
101         vals =  X509V3_parse_list(value);
102         ia5org = 0;
103         for(i = 0; i < sk_num(vals); i++) {
104                 cnf = (CONF_VALUE *)sk_value(vals, i);
105                 if(cnf->value || !cnf->name ) {
106                         X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_POLICY_IDENTIFIER);
107                         X509V3_conf_err(cnf);
108                         goto err;
109                 }
110                 pstr = cnf->name;
111                 if(!strcmp(pstr,"ia5org")) {
112                         ia5org = 1;
113                         continue;
114                 } else if(*pstr == '@') {
115                         STACK *polsect;
116                         polsect = X509V3_get_section(ctx, pstr + 1);
117                         if(!polsect) {
118                                 X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_SECTION);
119
120                                 X509V3_conf_err(cnf);
121                                 goto err;
122                         }
123                         pol = policy_section(ctx, polsect, ia5org);
124                         X509V3_section_free(ctx, polsect);
125                         if(!pol) goto err;
126                 } else {
127                         if(!(pobj = OBJ_txt2obj(cnf->name, 0))) {
128                                 X509V3err(X509V3_F_R2I_CERTPOL,X509V3_R_INVALID_OBJECT_IDENTIFIER);
129                                 X509V3_conf_err(cnf);
130                                 goto err;
131                         }
132                         pol = POLICYINFO_new();
133                         pol->policyid = pobj;
134                 }
135                 sk_POLICYINFO_push(pols, pol);
136         }
137         sk_pop_free(vals, X509V3_conf_free);
138         return pols;
139         err:
140         sk_POLICYINFO_pop_free(pols, POLICYINFO_free);
141         return NULL;
142 }
143
144 static POLICYINFO *policy_section(X509V3_CTX *ctx, STACK *polstrs, int ia5org)
145 {
146         int i;
147         CONF_VALUE *cnf;
148         POLICYINFO *pol;
149         POLICYQUALINFO *qual;
150         if(!(pol = POLICYINFO_new())) goto merr;
151         for(i = 0; i < sk_num(polstrs); i++) {
152                 cnf = (CONF_VALUE *)sk_value(polstrs, i);
153                 if(!strcmp(cnf->name, "policyIdentifier")) {
154                         ASN1_OBJECT *pobj;
155                         if(!(pobj = OBJ_txt2obj(cnf->value, 0))) {
156                                 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_OBJECT_IDENTIFIER);
157                                 X509V3_conf_err(cnf);
158                                 goto err;
159                         }
160                         pol->policyid = pobj;
161
162                 } else if(!name_cmp(cnf->name, "CPS")) {
163                         if(!pol->qualifiers) pol->qualifiers =
164                                                  sk_POLICYQUALINFO_new_null();
165                         if(!(qual = POLICYQUALINFO_new())) goto merr;
166                         if(!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
167                                                                  goto merr;
168                         qual->pqualid = OBJ_nid2obj(NID_id_qt_cps);
169                         qual->d.cpsuri = ASN1_IA5STRING_new();
170                         if(!ASN1_STRING_set(qual->d.cpsuri, cnf->value,
171                                                  strlen(cnf->value))) goto merr;
172                 } else if(!name_cmp(cnf->name, "userNotice")) {
173                         STACK *unot;
174                         if(*cnf->value != '@') {
175                                 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_EXPECTED_A_SECTION_NAME);
176                                 X509V3_conf_err(cnf);
177                                 goto err;
178                         }
179                         unot = X509V3_get_section(ctx, cnf->value + 1);
180                         if(!unot) {
181                                 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_SECTION);
182
183                                 X509V3_conf_err(cnf);
184                                 goto err;
185                         }
186                         qual = notice_section(ctx, unot, ia5org);
187                         X509V3_section_free(ctx, unot);
188                         if(!qual) goto err;
189                         if(!sk_POLICYQUALINFO_push(pol->qualifiers, qual))
190                                                                  goto merr;
191                 } else {
192                         X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_INVALID_OPTION);
193
194                         X509V3_conf_err(cnf);
195                         goto err;
196                 }
197         }
198         if(!pol->policyid) {
199                 X509V3err(X509V3_F_POLICY_SECTION,X509V3_R_NO_POLICY_IDENTIFIER);
200                 goto err;
201         }
202
203         return pol;
204
205         merr:
206         X509V3err(X509V3_F_POLICY_SECTION,ERR_R_MALLOC_FAILURE);
207
208         err:
209         POLICYINFO_free(pol);
210         return NULL;
211         
212         
213 }
214
215 static POLICYQUALINFO *notice_section(X509V3_CTX *ctx, STACK *unot, int ia5org)
216 {
217         int i;
218         CONF_VALUE *cnf;
219         USERNOTICE *not;
220         POLICYQUALINFO *qual;
221         if(!(qual = POLICYQUALINFO_new())) goto merr;
222         qual->pqualid = OBJ_nid2obj(NID_id_qt_unotice);
223         if(!(not = USERNOTICE_new())) goto merr;
224         qual->d.usernotice = not;
225         for(i = 0; i < sk_num(unot); i++) {
226                 cnf = (CONF_VALUE *)sk_value(unot, i);
227                 if(!strcmp(cnf->name, "explicitText")) {
228                         not->exptext = ASN1_VISIBLESTRING_new();
229                         if(!ASN1_STRING_set(not->exptext, cnf->value,
230                                                  strlen(cnf->value))) goto merr;
231                 } else if(!strcmp(cnf->name, "organization")) {
232                         NOTICEREF *nref;
233                         if(!not->noticeref) {
234                                 if(!(nref = NOTICEREF_new())) goto merr;
235                                 not->noticeref = nref;
236                         } else nref = not->noticeref;
237                         if(ia5org) nref->organization = ASN1_IA5STRING_new();
238                         else nref->organization = ASN1_VISIBLESTRING_new();
239                         if(!ASN1_STRING_set(nref->organization, cnf->value,
240                                                  strlen(cnf->value))) goto merr;
241                 } else if(!strcmp(cnf->name, "noticeNumbers")) {
242                         NOTICEREF *nref;
243                         STACK *nos;
244                         if(!not->noticeref) {
245                                 if(!(nref = NOTICEREF_new())) goto merr;
246                                 not->noticeref = nref;
247                         } else nref = not->noticeref;
248                         nos = X509V3_parse_list(cnf->value);
249                         if(!nos || !sk_num(nos)) {
250                                 X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_INVALID_NUMBERS);
251                                 X509V3_conf_err(cnf);
252                                 goto err;
253                         }
254                         nref->noticenos = nref_nos(nos);
255                         sk_pop_free(nos, X509V3_conf_free);
256                         if(!nref->noticenos) goto err;
257                 } else {
258                         X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_INVALID_OPTION);
259
260                         X509V3_conf_err(cnf);
261                         goto err;
262                 }
263         }
264
265         if(not->noticeref && 
266               (!not->noticeref->noticenos || !not->noticeref->organization)) {
267                         X509V3err(X509V3_F_NOTICE_SECTION,X509V3_R_NEED_ORGANIZATION_AND_NUMBERS);
268                         goto err;
269         }
270
271         return qual;
272
273         merr:
274         X509V3err(X509V3_F_NOTICE_SECTION,ERR_R_MALLOC_FAILURE);
275
276         err:
277         POLICYQUALINFO_free(qual);
278         return NULL;
279 }
280
281 static STACK *nref_nos(STACK *nos)
282 {
283         STACK *nnums;
284         CONF_VALUE *cnf;
285         ASN1_INTEGER *aint;
286         int i;
287         if(!(nnums = sk_new_null())) goto merr;
288         for(i = 0; i < sk_num(nos); i++) {
289                 cnf = (CONF_VALUE *)sk_value(nos, i);
290                 if(!(aint = s2i_ASN1_INTEGER(NULL, cnf->name))) {
291                         X509V3err(X509V3_F_NREF_NOS,X509V3_R_INVALID_NUMBER);
292                         goto err;
293                 }
294                 if(!sk_push(nnums, (char *)aint)) goto merr;
295         }
296         return nnums;
297
298         merr:
299         X509V3err(X509V3_F_NOTICE_SECTION,ERR_R_MALLOC_FAILURE);
300
301         err:
302         sk_pop_free(nnums, ASN1_STRING_free);
303         return NULL;
304 }
305
306
307 static int i2r_certpol(X509V3_EXT_METHOD *method, STACK_OF(POLICYINFO) *pol,
308                 BIO *out, int indent)
309 {
310         int i;
311         POLICYINFO *pinfo;
312         /* First print out the policy OIDs */
313         for(i = 0; i < sk_POLICYINFO_num(pol); i++) {
314                 pinfo = sk_POLICYINFO_value(pol, i);
315                 BIO_printf(out, "%*sPolicy: ", indent, "");
316                 i2a_ASN1_OBJECT(out, pinfo->policyid);
317                 BIO_puts(out, "\n");
318                 if(pinfo->qualifiers)
319                          print_qualifiers(out, pinfo->qualifiers, indent + 2);
320         }
321         return 1;
322 }
323
324
325 int i2d_CERTIFICATEPOLICIES(STACK_OF(POLICYINFO) *a, unsigned char **pp)
326 {
327
328 return i2d_ASN1_SET_OF_POLICYINFO(a, pp, i2d_POLICYINFO, V_ASN1_SEQUENCE,
329                                                  V_ASN1_UNIVERSAL, IS_SEQUENCE);}
330
331 STACK_OF(POLICYINFO) *CERTIFICATEPOLICIES_new(void)
332 {
333         return sk_POLICYINFO_new_null();
334 }
335
336 void CERTIFICATEPOLICIES_free(STACK_OF(POLICYINFO) *a)
337 {
338         sk_POLICYINFO_pop_free(a, POLICYINFO_free);
339 }
340
341 STACK_OF(POLICYINFO) *d2i_CERTIFICATEPOLICIES(STACK_OF(POLICYINFO) **a,
342                 unsigned char **pp,long length)
343 {
344 return d2i_ASN1_SET_OF_POLICYINFO(a, pp, length, d2i_POLICYINFO,
345                          POLICYINFO_free, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL);
346
347 }
348
349 IMPLEMENT_STACK_OF(POLICYINFO)
350 IMPLEMENT_ASN1_SET_OF(POLICYINFO)
351
352 int i2d_POLICYINFO(POLICYINFO *a, unsigned char **pp)
353 {
354         M_ASN1_I2D_vars(a);
355
356         M_ASN1_I2D_len (a->policyid, i2d_ASN1_OBJECT);
357         M_ASN1_I2D_len_SEQUENCE_type(POLICYQUALINFO, a->qualifiers,
358                                                          i2d_POLICYQUALINFO);
359
360         M_ASN1_I2D_seq_total();
361
362         M_ASN1_I2D_put (a->policyid, i2d_ASN1_OBJECT);
363         M_ASN1_I2D_put_SEQUENCE_type(POLICYQUALINFO, a->qualifiers,
364                                                          i2d_POLICYQUALINFO);
365
366         M_ASN1_I2D_finish();
367 }
368
369 POLICYINFO *POLICYINFO_new(void)
370 {
371         POLICYINFO *ret=NULL;
372         ASN1_CTX c;
373         M_ASN1_New_Malloc(ret, POLICYINFO);
374         ret->policyid = NULL;
375         ret->qualifiers = NULL;
376         return (ret);
377         M_ASN1_New_Error(ASN1_F_POLICYINFO_NEW);
378 }
379
380 POLICYINFO *d2i_POLICYINFO(POLICYINFO **a, unsigned char **pp,long length)
381 {
382         M_ASN1_D2I_vars(a,POLICYINFO *,POLICYINFO_new);
383         M_ASN1_D2I_Init();
384         M_ASN1_D2I_start_sequence();
385         M_ASN1_D2I_get(ret->policyid, d2i_ASN1_OBJECT);
386         if(!M_ASN1_D2I_end_sequence()) {
387                 M_ASN1_D2I_get_seq_type (POLICYQUALINFO, ret->qualifiers,
388                                  d2i_POLICYQUALINFO, POLICYQUALINFO_free);
389         }
390         M_ASN1_D2I_Finish(a, POLICYINFO_free, ASN1_F_D2I_POLICYINFO);
391 }
392
393 void POLICYINFO_free(POLICYINFO *a)
394 {
395         if (a == NULL) return;
396         ASN1_OBJECT_free(a->policyid);
397         sk_POLICYQUALINFO_pop_free(a->qualifiers, POLICYQUALINFO_free);
398         Free (a);
399 }
400
401 static void print_qualifiers(BIO *out, STACK_OF(POLICYQUALINFO) *quals,
402                 int indent)
403 {
404         POLICYQUALINFO *qualinfo;
405         int i;
406         for(i = 0; i < sk_POLICYQUALINFO_num(quals); i++) {
407                 qualinfo = sk_POLICYQUALINFO_value(quals, i);
408                 switch(OBJ_obj2nid(qualinfo->pqualid))
409                 {
410                         case NID_id_qt_cps:
411                         BIO_printf(out, "%*sCPS: %s\n", indent, "",
412                                                 qualinfo->d.cpsuri->data);
413                         break;
414                 
415                         case NID_id_qt_unotice:
416                         BIO_printf(out, "%*sUser Notice:\n", indent, "");
417                         print_notice(out, qualinfo->d.usernotice, indent + 2);
418                         break;
419
420                         default:
421                         BIO_printf(out, "%*sUnknown Qualifier: ",
422                                                          indent + 2, "");
423                         
424                         i2a_ASN1_OBJECT(out, qualinfo->pqualid);
425                         BIO_puts(out, "\n");
426                         break;
427                 }
428         }
429 }
430
431 static void print_notice(BIO *out, USERNOTICE *notice, int indent)
432 {
433         int i;
434         if(notice->noticeref) {
435                 NOTICEREF *ref;
436                 ref = notice->noticeref;
437                 BIO_printf(out, "%*sOrganization: %s\n", indent, "",
438                                                  ref->organization->data);
439                 BIO_printf(out, "%*sNumber%s: ", indent, "",
440                                  (sk_num(ref->noticenos) > 1) ? "s" : "");
441                 for(i = 0; i < sk_num(ref->noticenos); i++) {
442                         ASN1_INTEGER *num;
443                         char *tmp;
444                         num = (ASN1_INTEGER *)sk_value(ref->noticenos, i);
445                         if(i) BIO_puts(out, ", ");
446                         tmp = i2s_ASN1_INTEGER(NULL, num);
447                         BIO_puts(out, tmp);
448                         Free(tmp);
449                 }
450                 BIO_puts(out, "\n");
451         }
452         if(notice->exptext)
453                 BIO_printf(out, "%*sExplicit Text: %s\n", indent, "",
454                                                          notice->exptext->data);
455 }
456                 
457         
458
459 int i2d_POLICYQUALINFO(POLICYQUALINFO *a, unsigned char **pp)
460 {
461         M_ASN1_I2D_vars(a);
462
463         M_ASN1_I2D_len (a->pqualid, i2d_ASN1_OBJECT);
464         switch(OBJ_obj2nid(a->pqualid)) {
465                 case NID_id_qt_cps:
466                 M_ASN1_I2D_len(a->d.cpsuri, i2d_ASN1_IA5STRING);
467                 break;
468
469                 case NID_id_qt_unotice:
470                 M_ASN1_I2D_len(a->d.usernotice, i2d_USERNOTICE);
471                 break;
472
473                 default:
474                 M_ASN1_I2D_len(a->d.other, i2d_ASN1_TYPE);
475                 break;
476         }
477
478         M_ASN1_I2D_seq_total();
479
480         M_ASN1_I2D_put (a->pqualid, i2d_ASN1_OBJECT);
481         switch(OBJ_obj2nid(a->pqualid)) {
482                 case NID_id_qt_cps:
483                 M_ASN1_I2D_put(a->d.cpsuri, i2d_ASN1_IA5STRING);
484                 break;
485
486                 case NID_id_qt_unotice:
487                 M_ASN1_I2D_put(a->d.usernotice, i2d_USERNOTICE);
488                 break;
489
490                 default:
491                 M_ASN1_I2D_put(a->d.other, i2d_ASN1_TYPE);
492                 break;
493         }
494
495         M_ASN1_I2D_finish();
496 }
497
498 POLICYQUALINFO *POLICYQUALINFO_new(void)
499 {
500         POLICYQUALINFO *ret=NULL;
501         ASN1_CTX c;
502         M_ASN1_New_Malloc(ret, POLICYQUALINFO);
503         ret->pqualid = NULL;
504         ret->d.other = NULL;
505         return (ret);
506         M_ASN1_New_Error(ASN1_F_POLICYQUALINFO_NEW);
507 }
508
509 POLICYQUALINFO *d2i_POLICYQUALINFO(POLICYQUALINFO **a, unsigned char **pp,
510                 long length)
511 {
512         M_ASN1_D2I_vars(a,POLICYQUALINFO *,POLICYQUALINFO_new);
513         M_ASN1_D2I_Init();
514         M_ASN1_D2I_start_sequence();
515         M_ASN1_D2I_get (ret->pqualid, d2i_ASN1_OBJECT);
516         switch(OBJ_obj2nid(ret->pqualid)) {
517                 case NID_id_qt_cps:
518                 M_ASN1_D2I_get(ret->d.cpsuri, d2i_ASN1_IA5STRING);
519                 break;
520
521                 case NID_id_qt_unotice:
522                 M_ASN1_D2I_get(ret->d.usernotice, d2i_USERNOTICE);
523                 break;
524
525                 default:
526                 M_ASN1_D2I_get(ret->d.other, d2i_ASN1_TYPE);
527                 break;
528         }
529         M_ASN1_D2I_Finish(a, POLICYQUALINFO_free, ASN1_F_D2I_POLICYQUALINFO);
530 }
531
532 void POLICYQUALINFO_free(POLICYQUALINFO *a)
533 {
534         if (a == NULL) return;
535         switch(OBJ_obj2nid(a->pqualid)) {
536                 case NID_id_qt_cps:
537                 ASN1_IA5STRING_free(a->d.cpsuri);
538                 break;
539
540                 case NID_id_qt_unotice:
541                 USERNOTICE_free(a->d.usernotice);
542                 break;
543
544                 default:
545                 ASN1_TYPE_free(a->d.other);
546                 break;
547         }
548         
549         ASN1_OBJECT_free(a->pqualid);
550         Free (a);
551 }
552
553 int i2d_USERNOTICE(USERNOTICE *a, unsigned char **pp)
554 {
555         M_ASN1_I2D_vars(a);
556
557         M_ASN1_I2D_len (a->noticeref, i2d_NOTICEREF);
558         M_ASN1_I2D_len (a->exptext, i2d_DISPLAYTEXT);
559
560         M_ASN1_I2D_seq_total();
561
562         M_ASN1_I2D_put (a->noticeref, i2d_NOTICEREF);
563         M_ASN1_I2D_put (a->exptext, i2d_DISPLAYTEXT);
564
565         M_ASN1_I2D_finish();
566 }
567
568 USERNOTICE *USERNOTICE_new(void)
569 {
570         USERNOTICE *ret=NULL;
571         ASN1_CTX c;
572         M_ASN1_New_Malloc(ret, USERNOTICE);
573         ret->noticeref = NULL;
574         ret->exptext = NULL;
575         return (ret);
576         M_ASN1_New_Error(ASN1_F_USERNOTICE_NEW);
577 }
578
579 USERNOTICE *d2i_USERNOTICE(USERNOTICE **a, unsigned char **pp,long length)
580 {
581         M_ASN1_D2I_vars(a,USERNOTICE *,USERNOTICE_new);
582         M_ASN1_D2I_Init();
583         M_ASN1_D2I_start_sequence();
584         M_ASN1_D2I_get_opt(ret->noticeref, d2i_NOTICEREF, V_ASN1_SEQUENCE);
585         if (!M_ASN1_D2I_end_sequence()) {
586                 M_ASN1_D2I_get(ret->exptext, d2i_DISPLAYTEXT);
587         }
588         M_ASN1_D2I_Finish(a, USERNOTICE_free, ASN1_F_D2I_USERNOTICE);
589 }
590
591 void USERNOTICE_free(USERNOTICE *a)
592 {
593         if (a == NULL) return;
594         NOTICEREF_free(a->noticeref);
595         DISPLAYTEXT_free(a->exptext);
596         Free (a);
597 }
598
599 int i2d_NOTICEREF(NOTICEREF *a, unsigned char **pp)
600 {
601         M_ASN1_I2D_vars(a);
602
603         M_ASN1_I2D_len (a->organization, i2d_DISPLAYTEXT);
604         M_ASN1_I2D_len_SEQUENCE(a->noticenos, i2d_ASN1_INTEGER);
605
606         M_ASN1_I2D_seq_total();
607
608         M_ASN1_I2D_put (a->organization, i2d_DISPLAYTEXT);
609         M_ASN1_I2D_put_SEQUENCE(a->noticenos, i2d_ASN1_INTEGER);
610
611         M_ASN1_I2D_finish();
612 }
613
614 NOTICEREF *NOTICEREF_new(void)
615 {
616         NOTICEREF *ret=NULL;
617         ASN1_CTX c;
618         M_ASN1_New_Malloc(ret, NOTICEREF);
619         ret->organization = NULL;
620         ret->noticenos = NULL;
621         return (ret);
622         M_ASN1_New_Error(ASN1_F_NOTICEREF_NEW);
623 }
624
625 NOTICEREF *d2i_NOTICEREF(NOTICEREF **a, unsigned char **pp,long length)
626 {
627         M_ASN1_D2I_vars(a,NOTICEREF *,NOTICEREF_new);
628         M_ASN1_D2I_Init();
629         M_ASN1_D2I_start_sequence();
630         /* This is to cope with some broken encodings that use IA5STRING for
631          * the organization field
632          */
633         M_ASN1_D2I_get_opt(ret->organization, d2i_ASN1_IA5STRING,
634                                                          V_ASN1_IA5STRING);
635         if(!ret->organization) {
636                  M_ASN1_D2I_get(ret->organization, d2i_DISPLAYTEXT);
637         }
638         M_ASN1_D2I_get_seq(ret->noticenos, d2i_ASN1_INTEGER, ASN1_STRING_free);
639         M_ASN1_D2I_Finish(a, NOTICEREF_free, ASN1_F_D2I_NOTICEREF);
640 }
641
642 void NOTICEREF_free(NOTICEREF *a)
643 {
644         if (a == NULL) return;
645         DISPLAYTEXT_free(a->organization);
646         sk_pop_free(a->noticenos, ASN1_STRING_free);
647         Free (a);
648 }
649
650 IMPLEMENT_STACK_OF(POLICYQUALINFO)
651 IMPLEMENT_ASN1_SET_OF(POLICYQUALINFO)