summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/a_strnid.c
diff options
context:
space:
mode:
authorjsing <>2014-04-19 13:11:41 +0000
committerjsing <>2014-04-19 13:11:41 +0000
commit94b01fb1dea0219d681a8f31b420537fd56a8a20 (patch)
treed852cc766d4c429b41b38d7046ec6dfc58a87f6b /src/lib/libcrypto/asn1/a_strnid.c
parent1a4bc92d69fa85768e8b6d9592f3ca13bfb70c6e (diff)
downloadopenbsd-94b01fb1dea0219d681a8f31b420537fd56a8a20.tar.gz
openbsd-94b01fb1dea0219d681a8f31b420537fd56a8a20.tar.bz2
openbsd-94b01fb1dea0219d681a8f31b420537fd56a8a20.zip
More KNF.
Diffstat (limited to 'src/lib/libcrypto/asn1/a_strnid.c')
-rw-r--r--src/lib/libcrypto/asn1/a_strnid.c175
1 files changed, 103 insertions, 72 deletions
diff --git a/src/lib/libcrypto/asn1/a_strnid.c b/src/lib/libcrypto/asn1/a_strnid.c
index 6c59dcc8ec..d1b804aec1 100644
--- a/src/lib/libcrypto/asn1/a_strnid.c
+++ b/src/lib/libcrypto/asn1/a_strnid.c
@@ -10,7 +10,7 @@
10 * are met: 10 * are met:
11 * 11 *
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 14 *
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in 16 * notice, this list of conditions and the following disclaimer in
@@ -66,7 +66,7 @@
66static STACK_OF(ASN1_STRING_TABLE) *stable = NULL; 66static STACK_OF(ASN1_STRING_TABLE) *stable = NULL;
67static void st_free(ASN1_STRING_TABLE *tbl); 67static void st_free(ASN1_STRING_TABLE *tbl);
68static int sk_table_cmp(const ASN1_STRING_TABLE * const *a, 68static int sk_table_cmp(const ASN1_STRING_TABLE * const *a,
69 const ASN1_STRING_TABLE * const *b); 69 const ASN1_STRING_TABLE * const *b);
70 70
71 71
72/* This is the global mask for the mbstring functions: this is use to 72/* This is the global mask for the mbstring functions: this is use to
@@ -76,12 +76,14 @@ static int sk_table_cmp(const ASN1_STRING_TABLE * const *a,
76 76
77static unsigned long global_mask = 0xFFFFFFFFL; 77static unsigned long global_mask = 0xFFFFFFFFL;
78 78
79void ASN1_STRING_set_default_mask(unsigned long mask) 79void
80ASN1_STRING_set_default_mask(unsigned long mask)
80{ 81{
81 global_mask = mask; 82 global_mask = mask;
82} 83}
83 84
84unsigned long ASN1_STRING_get_default_mask(void) 85unsigned long
86ASN1_STRING_get_default_mask(void)
85{ 87{
86 return global_mask; 88 return global_mask;
87} 89}
@@ -95,47 +97,59 @@ unsigned long ASN1_STRING_get_default_mask(void)
95 * default: the default value, Printable, T61, BMP. 97 * default: the default value, Printable, T61, BMP.
96 */ 98 */
97 99
98int ASN1_STRING_set_default_mask_asc(const char *p) 100int
101ASN1_STRING_set_default_mask_asc(const char *p)
99{ 102{
100 unsigned long mask; 103 unsigned long mask;
101 char *end; 104 char *end;
102 if(!strncmp(p, "MASK:", 5)) { 105
103 if(!p[5]) return 0; 106 if (!strncmp(p, "MASK:", 5)) {
107 if (!p[5])
108 return 0;
104 mask = strtoul(p + 5, &end, 0); 109 mask = strtoul(p + 5, &end, 0);
105 if(*end) return 0; 110 if (*end)
106 } else if(!strcmp(p, "nombstr")) 111 return 0;
107 mask = ~((unsigned long)(B_ASN1_BMPSTRING|B_ASN1_UTF8STRING)); 112 } else if (!strcmp(p, "nombstr"))
108 else if(!strcmp(p, "pkix")) 113 mask = ~((unsigned long)(B_ASN1_BMPSTRING|B_ASN1_UTF8STRING));
109 mask = ~((unsigned long)B_ASN1_T61STRING); 114 else if (!strcmp(p, "pkix"))
110 else if(!strcmp(p, "utf8only")) mask = B_ASN1_UTF8STRING; 115 mask = ~((unsigned long)B_ASN1_T61STRING);
111 else if(!strcmp(p, "default")) 116 else if (!strcmp(p, "utf8only"))
112 mask = 0xFFFFFFFFL; 117 mask = B_ASN1_UTF8STRING;
113 else return 0; 118 else if (!strcmp(p, "default"))
119 mask = 0xFFFFFFFFL;
120 else
121 return 0;
114 ASN1_STRING_set_default_mask(mask); 122 ASN1_STRING_set_default_mask(mask);
115 return 1; 123 return 1;
116} 124}
117 125
118/* The following function generates an ASN1_STRING based on limits in a table. 126/* The following function generates an ASN1_STRING based on limits in a table.
119 * Frequently the types and length of an ASN1_STRING are restricted by a 127 * Frequently the types and length of an ASN1_STRING are restricted by a
120 * corresponding OID. For example certificates and certificate requests. 128 * corresponding OID. For example certificates and certificate requests.
121 */ 129 */
122 130
123ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in, 131ASN1_STRING *
124 int inlen, int inform, int nid) 132ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in, int inlen,
133 int inform, int nid)
125{ 134{
126 ASN1_STRING_TABLE *tbl; 135 ASN1_STRING_TABLE *tbl;
127 ASN1_STRING *str = NULL; 136 ASN1_STRING *str = NULL;
128 unsigned long mask; 137 unsigned long mask;
129 int ret; 138 int ret;
130 if(!out) out = &str; 139 if (!out)
140 out = &str;
131 tbl = ASN1_STRING_TABLE_get(nid); 141 tbl = ASN1_STRING_TABLE_get(nid);
132 if(tbl) { 142 if (tbl) {
133 mask = tbl->mask; 143 mask = tbl->mask;
134 if(!(tbl->flags & STABLE_NO_MASK)) mask &= global_mask; 144 if (!(tbl->flags & STABLE_NO_MASK))
145 mask &= global_mask;
135 ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask, 146 ret = ASN1_mbstring_ncopy(out, in, inlen, inform, mask,
136 tbl->minsize, tbl->maxsize); 147 tbl->minsize, tbl->maxsize);
137 } else ret = ASN1_mbstring_copy(out, in, inlen, inform, DIRSTRING_TYPE & global_mask); 148 } else
138 if(ret <= 0) return NULL; 149 ret = ASN1_mbstring_copy(out, in, inlen, inform,
150 DIRSTRING_TYPE & global_mask);
151 if (ret <= 0)
152 return NULL;
139 return *out; 153 return *out;
140} 154}
141 155
@@ -158,99 +172,116 @@ ASN1_STRING *ASN1_STRING_set_by_NID(ASN1_STRING **out, const unsigned char *in,
158/* This table must be kept in NID order */ 172/* This table must be kept in NID order */
159 173
160static const ASN1_STRING_TABLE tbl_standard[] = { 174static const ASN1_STRING_TABLE tbl_standard[] = {
161{NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0}, 175 {NID_commonName, 1, ub_common_name, DIRSTRING_TYPE, 0},
162{NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK}, 176 {NID_countryName, 2, 2, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
163{NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0}, 177 {NID_localityName, 1, ub_locality_name, DIRSTRING_TYPE, 0},
164{NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0}, 178 {NID_stateOrProvinceName, 1, ub_state_name, DIRSTRING_TYPE, 0},
165{NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0}, 179 {NID_organizationName, 1, ub_organization_name, DIRSTRING_TYPE, 0},
166{NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE, 0}, 180 {NID_organizationalUnitName, 1, ub_organization_unit_name, DIRSTRING_TYPE, 0},
167{NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING, STABLE_NO_MASK}, 181 {NID_pkcs9_emailAddress, 1, ub_email_address, B_ASN1_IA5STRING, STABLE_NO_MASK},
168{NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0}, 182 {NID_pkcs9_unstructuredName, 1, -1, PKCS9STRING_TYPE, 0},
169{NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0}, 183 {NID_pkcs9_challengePassword, 1, -1, PKCS9STRING_TYPE, 0},
170{NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0}, 184 {NID_pkcs9_unstructuredAddress, 1, -1, DIRSTRING_TYPE, 0},
171{NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0}, 185 {NID_givenName, 1, ub_name, DIRSTRING_TYPE, 0},
172{NID_surname, 1, ub_name, DIRSTRING_TYPE, 0}, 186 {NID_surname, 1, ub_name, DIRSTRING_TYPE, 0},
173{NID_initials, 1, ub_name, DIRSTRING_TYPE, 0}, 187 {NID_initials, 1, ub_name, DIRSTRING_TYPE, 0},
174{NID_serialNumber, 1, ub_serial_number, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK}, 188 {NID_serialNumber, 1, ub_serial_number, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
175{NID_friendlyName, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}, 189 {NID_friendlyName, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK},
176{NID_name, 1, ub_name, DIRSTRING_TYPE, 0}, 190 {NID_name, 1, ub_name, DIRSTRING_TYPE, 0},
177{NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK}, 191 {NID_dnQualifier, -1, -1, B_ASN1_PRINTABLESTRING, STABLE_NO_MASK},
178{NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK}, 192 {NID_domainComponent, 1, -1, B_ASN1_IA5STRING, STABLE_NO_MASK},
179{NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK} 193 {NID_ms_csp_name, -1, -1, B_ASN1_BMPSTRING, STABLE_NO_MASK}
180}; 194};
181 195
182static int sk_table_cmp(const ASN1_STRING_TABLE * const *a, 196static int
183 const ASN1_STRING_TABLE * const *b) 197sk_table_cmp(const ASN1_STRING_TABLE * const *a,
198 const ASN1_STRING_TABLE * const *b)
184{ 199{
185 return (*a)->nid - (*b)->nid; 200 return (*a)->nid - (*b)->nid;
186} 201}
187 202
188DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table); 203DECLARE_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
189 204
190static int table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b) 205static int
206table_cmp(const ASN1_STRING_TABLE *a, const ASN1_STRING_TABLE *b)
191{ 207{
192 return a->nid - b->nid; 208 return a->nid - b->nid;
193} 209}
194 210
195IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table); 211IMPLEMENT_OBJ_BSEARCH_CMP_FN(ASN1_STRING_TABLE, ASN1_STRING_TABLE, table);
196 212
197ASN1_STRING_TABLE *ASN1_STRING_TABLE_get(int nid) 213ASN1_STRING_TABLE *
214ASN1_STRING_TABLE_get(int nid)
198{ 215{
199 int idx; 216 int idx;
200 ASN1_STRING_TABLE *ttmp; 217 ASN1_STRING_TABLE *ttmp;
201 ASN1_STRING_TABLE fnd; 218 ASN1_STRING_TABLE fnd;
219
202 fnd.nid = nid; 220 fnd.nid = nid;
203 ttmp = OBJ_bsearch_table(&fnd, tbl_standard, 221 ttmp = OBJ_bsearch_table(&fnd, tbl_standard,
204 sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE)); 222 sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE));
205 if(ttmp) return ttmp; 223 if (ttmp)
206 if(!stable) return NULL; 224 return ttmp;
225 if (!stable)
226 return NULL;
207 idx = sk_ASN1_STRING_TABLE_find(stable, &fnd); 227 idx = sk_ASN1_STRING_TABLE_find(stable, &fnd);
208 if(idx < 0) return NULL; 228 if (idx < 0)
229 return NULL;
209 return sk_ASN1_STRING_TABLE_value(stable, idx); 230 return sk_ASN1_STRING_TABLE_value(stable, idx);
210} 231}
211 232
212int ASN1_STRING_TABLE_add(int nid, 233int
213 long minsize, long maxsize, unsigned long mask, 234ASN1_STRING_TABLE_add(int nid, long minsize, long maxsize, unsigned long mask,
214 unsigned long flags) 235 unsigned long flags)
215{ 236{
216 ASN1_STRING_TABLE *tmp; 237 ASN1_STRING_TABLE *tmp;
217 char new_nid = 0; 238 char new_nid = 0;
239
218 flags &= ~STABLE_FLAGS_MALLOC; 240 flags &= ~STABLE_FLAGS_MALLOC;
219 if(!stable) stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp); 241 if (!stable)
220 if(!stable) { 242 stable = sk_ASN1_STRING_TABLE_new(sk_table_cmp);
243 if (!stable) {
221 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE); 244 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, ERR_R_MALLOC_FAILURE);
222 return 0; 245 return 0;
223 } 246 }
224 if(!(tmp = ASN1_STRING_TABLE_get(nid))) { 247 if (!(tmp = ASN1_STRING_TABLE_get(nid))) {
225 tmp = malloc(sizeof(ASN1_STRING_TABLE)); 248 tmp = malloc(sizeof(ASN1_STRING_TABLE));
226 if(!tmp) { 249 if (!tmp) {
227 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD, 250 ASN1err(ASN1_F_ASN1_STRING_TABLE_ADD,
228 ERR_R_MALLOC_FAILURE); 251 ERR_R_MALLOC_FAILURE);
229 return 0; 252 return 0;
230 } 253 }
231 tmp->flags = flags | STABLE_FLAGS_MALLOC; 254 tmp->flags = flags | STABLE_FLAGS_MALLOC;
232 tmp->nid = nid; 255 tmp->nid = nid;
233 new_nid = 1; 256 new_nid = 1;
234 } else tmp->flags = (tmp->flags & STABLE_FLAGS_MALLOC) | flags; 257 } else tmp->flags = (tmp->flags & STABLE_FLAGS_MALLOC) | flags;
235 if(minsize != -1) tmp->minsize = minsize; 258 if (minsize != -1)
236 if(maxsize != -1) tmp->maxsize = maxsize; 259 tmp->minsize = minsize;
260 if (maxsize != -1)
261 tmp->maxsize = maxsize;
237 tmp->mask = mask; 262 tmp->mask = mask;
238 if(new_nid) sk_ASN1_STRING_TABLE_push(stable, tmp); 263 if (new_nid)
264 sk_ASN1_STRING_TABLE_push(stable, tmp);
239 return 1; 265 return 1;
240} 266}
241 267
242void ASN1_STRING_TABLE_cleanup(void) 268void
269ASN1_STRING_TABLE_cleanup(void)
243{ 270{
244 STACK_OF(ASN1_STRING_TABLE) *tmp; 271 STACK_OF(ASN1_STRING_TABLE) *tmp;
272
245 tmp = stable; 273 tmp = stable;
246 if(!tmp) return; 274 if (!tmp)
275 return;
247 stable = NULL; 276 stable = NULL;
248 sk_ASN1_STRING_TABLE_pop_free(tmp, st_free); 277 sk_ASN1_STRING_TABLE_pop_free(tmp, st_free);
249} 278}
250 279
251static void st_free(ASN1_STRING_TABLE *tbl) 280static void
281st_free(ASN1_STRING_TABLE *tbl)
252{ 282{
253 if(tbl->flags & STABLE_FLAGS_MALLOC) free(tbl); 283 if (tbl->flags & STABLE_FLAGS_MALLOC)
284 free(tbl);
254} 285}
255 286
256 287
@@ -264,7 +295,7 @@ main()
264 int i, last_nid = -1; 295 int i, last_nid = -1;
265 296
266 for (tmp = tbl_standard, i = 0; 297 for (tmp = tbl_standard, i = 0;
267 i < sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE); i++, tmp++) { 298 i < sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE); i++, tmp++) {
268 if (tmp->nid < last_nid) { 299 if (tmp->nid < last_nid) {
269 last_nid = 0; 300 last_nid = 0;
270 break; 301 break;
@@ -278,9 +309,9 @@ main()
278 } 309 }
279 310
280 for (tmp = tbl_standard, i = 0; 311 for (tmp = tbl_standard, i = 0;
281 i < sizeof(tbl_standard)/sizeof(ASN1_STRING_TABLE); i++, tmp++) { 312 i < sizeof(tbl_standard) / sizeof(ASN1_STRING_TABLE); i++, tmp++) {
282 printf("Index %d, NID %d, Name=%s\n", i, tmp->nid, 313 printf("Index %d, NID %d, Name=%s\n", i, tmp->nid,
283 OBJ_nid2ln(tmp->nid)); 314 OBJ_nid2ln(tmp->nid));
284 } 315 }
285 316
286} 317}