e2e100e2eb8846d52161f94c2fcd2b0f5b977c2d
[openssl.git] / crypto / asn1 / a_strnid.c
1 /* a_strnid.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 <ctype.h>
61 #include "cryptlib.h"
62 #include <openssl/asn1.h>
63 #include <openssl/objects.h>
64
65
66 static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
67 static void st_free(ASN1_STRING_TABLE *tbl);
68 static int sk_table_cmp(ASN1_STRING_TABLE **a, ASN1_STRING_TABLE **b);
69
70 /* The following function generates an ASN1_STRING based on limits in a table.
71  * Frequently the types and length of an ASN1_STRING are restricted by a 
72  * corresponding OID. For example certificates and certificate requests.
73  */
74
75 ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in,
76                                         int inlen, int inform, int nid)
77 {
78         ASN1_STRING_TABLE *tbl;
79         ASN1_STRING *str = NULL;
80         int ret;
81         if(!out) out = &str;
82         if(!stable) ASN1_STRING_TABLE_add_standard();
83         tbl = ASN1_STRING_TABLE_get(nid);
84         if(tbl) ret = ASN1_mbstring_ncopy(out, in, inlen, inform, tbl->mask,
85                                         tbl->minsize, tbl->maxsize);
86         else ret = ASN1_mbstring_copy(out, in, inlen, inform, 0);
87         if(ret <= 0) return NULL;
88         return *out;
89 }
90
91 /* Now the tables and helper functions for the string table:
92  */
93
94 /* size limits: this stuff is taken straight from RFC2459 */
95
96 #define ub_name                         32768
97 #define ub_common_name                  64
98 #define ub_locality_name                128
99 #define ub_state_name                   128
100 #define ub_organization_name            64
101 #define ub_organization_unit_name       64
102 #define ub_title                        64
103 #define ub_email_address                128
104
105 static ASN1_STRING_TABLE tbl_standard[] = {
106 {NID_name,                      1, ub_name, 0, 0},
107 {NID_surname,                   1, ub_name, 0, 0},
108 {NID_givenName,                 1, ub_name, 0, 0},
109 {NID_initials,                  1, ub_name, 0, 0},
110 {NID_commonName,                1, ub_common_name, 0, 0},
111 {NID_localityName,              1, ub_locality_name, 0, 0},
112 {NID_stateOrProvinceName,       1, ub_state_name, 0, 0},
113 {NID_organizationName,          1, ub_organization_name, 0, 0},
114 {NID_organizationalUnitName,    1, ub_organization_unit_name, 0, 0},
115 {NID_dnQualifier,               -1, -1, B_ASN1_PRINTABLESTRING, 0},
116 {NID_countryName,               2, 2, B_ASN1_PRINTABLESTRING, 0},
117 {NID_pkcs9_emailAddress,        1, ub_email_address, B_ASN1_IA5STRING, 0},
118 {NID_undef, 0, 0, 0, 0}
119 };
120
121 int ASN1_STRING_TABLE_add_standard(void)
122 {
123         static int done = 0;
124         ASN1_STRING_TABLE *tmp;
125         if(done) return 1;
126         if(!stable) stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
127         if(!stable) {
128                 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD_STANDARD,
129                                                 ERR_R_MALLOC_FAILURE);
130                 return 0;
131         }
132         for(tmp = tbl_standard; tmp->nid != NID_undef; tmp++) {
133                 if(!sk_ASN1_STRING_TABLE_push(stable, tmp)) {
134                         ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD_STANDARD,
135                                                         ERR_R_MALLOC_FAILURE);
136                         return 0;
137                 }
138         }
139         return 1;
140 }
141
142 static int sk_table_cmp(ASN1_STRING_TABLE **a, ASN1_STRING_TABLE **b)
143 {
144         return (*a)->nid - (*b)->nid;
145 }
146
147 ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
148 {
149         int idx;
150         ASN1_STRING_TABLE fnd;
151         fnd.nid = nid;
152         idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
153         if(idx < 0) return NULL;
154         return sk_ASN1_STRING_TABLE_value(stable, idx);
155 }
156         
157 int ASN1_STRING_TABLE_add(int nid,
158                  long minsize, long maxsize, unsigned long mask,
159                                 unsigned long flags)
160 {
161         ASN1_STRING_TABLE *tmp;
162         char new_nid = 0;
163         if(!stable) stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
164         if(!stable) {
165                 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
166                 return 0;
167         }
168         if(!(tmp = ASN1_STRING_TABLE_get(nid))) {
169                 tmp = Malloc(sizeof(ASN1_STRING_TABLE));
170                 if(!tmp) {
171                         ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD,
172                                                         ERR_R_MALLOC_FAILURE);
173                         return 0;
174                 }
175                 tmp->flags = STABLE_FLAGS_MALLOC;
176                 tmp->nid = nid;
177                 new_nid = 1;
178         }
179         if(minsize != -1) tmp->minsize = minsize;
180         if(maxsize != -1) tmp->maxsize = maxsize;
181         tmp->mask = mask;
182         tmp->flags = flags & ~STABLE_FLAGS_MALLOC;
183         if(new_nid) sk_ASN1_STRING_TABLE_push(stable, tmp);
184         return 1;
185 }
186
187 void ASN1_STRING_TABLE_cleanup(void)
188 {
189         STACK_OF(ASN1_STRING_TABLE) *tmp;
190         tmp = stable;
191         if(!tmp) return;
192         stable = NULL;
193         sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
194 }
195
196 static void st_free(ASN1_STRING_TABLE *tbl)
197 {
198         if(tbl->flags & STABLE_FLAGS_MALLOC) Free(tbl);
199 }
200
201 IMPLEMENT_STACK_OF(ASN1_STRING_TABLE)