2f9c09b7057c865084c26af92a0120c62db39d63
[oweals/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 static int table_cmp(ASN1_STRING_TABLE *a, ASN1_STRING_TABLE *b);
70
71 /* The following function generates an ASN1_STRING based on limits in a table.
72  * Frequently the types and length of an ASN1_STRING are restricted by a 
73  * corresponding OID. For example certificates and certificate requests.
74  */
75
76 ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in,
77                                         int inlen, int inform, int nid)
78 {
79         ASN1_STRING_TABLE *tbl;
80         ASN1_STRING *str = NULL;
81         int ret;
82         if(!out) out = &str;
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 /* This table must be kept in NID order */
106
107 static ASN1_STRING_TABLE tbl_standard[] = {
108 {NID_commonName,                1, ub_common_name, 0, 0},
109 {NID_countryName,               2, 2, B_ASN1_PRINTABLESTRING, 0},
110 {NID_localityName,              1, ub_locality_name, 0, 0},
111 {NID_stateOrProvinceName,       1, ub_state_name, 0, 0},
112 {NID_organizationName,          1, ub_organization_name, 0, 0},
113 {NID_organizationalUnitName,    1, ub_organization_unit_name, 0, 0},
114 {NID_pkcs9_emailAddress,        1, ub_email_address, B_ASN1_IA5STRING, 0},
115 {NID_givenName,                 1, ub_name, 0, 0},
116 {NID_surname,                   1, ub_name, 0, 0},
117 {NID_initials,                  1, ub_name, 0, 0},
118 {NID_name,                      1, ub_name, 0, 0},
119 {NID_dnQualifier,               -1, -1, B_ASN1_PRINTABLESTRING, 0},
120 };
121
122 static int sk_table_cmp(ASN1_STRING_TABLE **a, ASN1_STRING_TABLE **b)
123 {
124         return (*a)->nid - (*b)->nid;
125 }
126
127 static int table_cmp(ASN1_STRING_TABLE *a, ASN1_STRING_TABLE *b)
128 {
129         return a->nid - b->nid;
130 }
131
132 ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid)
133 {
134         int idx;
135         ASN1_STRING_TABLE *ttmp;
136         ASN1_STRING_TABLE fnd;
137         fnd.nid = nid;
138         ttmp = (ASN1_STRING_TABLE *) OBJ_bsearch((char *)&fnd,
139                                         (char *)tbl_standard, 
140                         sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE),
141                         sizeof(ASN1_STRING_TABLE), (int(*)())table_cmp);
142         if(ttmp) return ttmp;
143         if(!stable) return NULL;
144         idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
145         if(idx < 0) return NULL;
146         return sk_ASN1_STRING_TABLE_value(stable, idx);
147 }
148         
149 int ASN1_STRING_TABLE_add(int nid,
150                  long minsize, long maxsize, unsigned long mask,
151                                 unsigned long flags)
152 {
153         ASN1_STRING_TABLE *tmp;
154         char new_nid = 0;
155         flags &= ~STABLE_FLAGS_MALLOC;
156         if(!stable) stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
157         if(!stable) {
158                 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
159                 return 0;
160         }
161         if(!(tmp = ASN1_STRING_TABLE_get(nid))) {
162                 tmp = Malloc(sizeof(ASN1_STRING_TABLE));
163                 if(!tmp) {
164                         ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD,
165                                                         ERR_R_MALLOC_FAILURE);
166                         return 0;
167                 }
168                 tmp->flags = flags | STABLE_FLAGS_MALLOC;
169                 tmp->nid = nid;
170                 new_nid = 1;
171         } else tmp->flags = (tmp->flags & STABLE_FLAGS_MALLOC) | flags;
172         if(minsize != -1) tmp->minsize = minsize;
173         if(maxsize != -1) tmp->maxsize = maxsize;
174         tmp->mask = mask;
175         if(new_nid) sk_ASN1_STRING_TABLE_push(stable, tmp);
176         return 1;
177 }
178
179 void ASN1_STRING_TABLE_cleanup(void)
180 {
181         STACK_OF(ASN1_STRING_TABLE) *tmp;
182         tmp = stable;
183         if(!tmp) return;
184         stable = NULL;
185         sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
186 }
187
188 static void st_free(ASN1_STRING_TABLE *tbl)
189 {
190         if(tbl->flags & STABLE_FLAGS_MALLOC) Free(tbl);
191 }
192
193 IMPLEMENT_STACK_OF(ASN1_STRING_TABLE)