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