summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/objects/obj_dat.c
diff options
context:
space:
mode:
authorjsing <>2014-04-19 16:44:36 +0000
committerjsing <>2014-04-19 16:44:36 +0000
commita267d032e2aaa6ac481f33615c9e57c613d49e2e (patch)
tree49030d3fea9f966297eb3218259a4faca4c52c9f /src/lib/libcrypto/objects/obj_dat.c
parenta21c0bb00d07d5263cf88747b598ac88a87d65ec (diff)
downloadopenbsd-a267d032e2aaa6ac481f33615c9e57c613d49e2e.tar.gz
openbsd-a267d032e2aaa6ac481f33615c9e57c613d49e2e.tar.bz2
openbsd-a267d032e2aaa6ac481f33615c9e57c613d49e2e.zip
KNF.
Diffstat (limited to 'src/lib/libcrypto/objects/obj_dat.c')
-rw-r--r--src/lib/libcrypto/objects/obj_dat.c858
1 files changed, 429 insertions, 429 deletions
diff --git a/src/lib/libcrypto/objects/obj_dat.c b/src/lib/libcrypto/objects/obj_dat.c
index cc007f1206..9fa23ae916 100644
--- a/src/lib/libcrypto/objects/obj_dat.c
+++ b/src/lib/libcrypto/objects/obj_dat.c
@@ -5,21 +5,21 @@
5 * This package is an SSL implementation written 5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com). 6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL. 7 * The implementation was written so as to conform with Netscapes SSL.
8 * 8 *
9 * This library is free for commercial and non-commercial use as long as 9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions 10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA, 11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms 13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * 15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed. 17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution 18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used. 19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or 20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package. 21 * in documentation (online or textual) provided with the package.
22 * 22 *
23 * Redistribution and use in source and binary forms, with or without 23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions 24 * modification, are permitted provided that the following conditions
25 * are met: 25 * are met:
@@ -34,10 +34,10 @@
34 * Eric Young (eay@cryptsoft.com)" 34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library 35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-). 36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from 37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement: 38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * 40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -49,7 +49,7 @@
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE. 51 * SUCH DAMAGE.
52 * 52 *
53 * The licence and distribution terms for any publically available version or 53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be 54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence 55 * copied and put under another distribution licence
@@ -90,117 +90,134 @@ DECLARE_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
90#define ADDED_LNAME 2 90#define ADDED_LNAME 2
91#define ADDED_NID 3 91#define ADDED_NID 3
92 92
93typedef struct added_obj_st 93typedef struct added_obj_st {
94 {
95 int type; 94 int type;
96 ASN1_OBJECT *obj; 95 ASN1_OBJECT *obj;
97 } ADDED_OBJ; 96} ADDED_OBJ;
98DECLARE_LHASH_OF(ADDED_OBJ); 97DECLARE_LHASH_OF(ADDED_OBJ);
99 98
100static int new_nid=NUM_NID; 99static int new_nid = NUM_NID;
101static LHASH_OF(ADDED_OBJ) *added=NULL; 100static LHASH_OF(ADDED_OBJ) *added = NULL;
102 101
103static int sn_cmp(const ASN1_OBJECT * const *a, const unsigned int *b) 102static int sn_cmp(const ASN1_OBJECT * const *a, const unsigned int *b)
104 { return(strcmp((*a)->sn,nid_objs[*b].sn)); } 103{
104 return (strcmp((*a)->sn, nid_objs[*b].sn));
105}
105 106
106IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn); 107IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, sn);
107 108
108static int ln_cmp(const ASN1_OBJECT * const *a, const unsigned int *b) 109static int ln_cmp(const ASN1_OBJECT * const *a, const unsigned int *b)
109 { return(strcmp((*a)->ln,nid_objs[*b].ln)); } 110{
111 return (strcmp((*a)->ln, nid_objs[*b].ln));
112}
110 113
111IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln); 114IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, ln);
112 115
113static unsigned long added_obj_hash(const ADDED_OBJ *ca) 116static unsigned long
114 { 117added_obj_hash(const ADDED_OBJ *ca)
118{
115 const ASN1_OBJECT *a; 119 const ASN1_OBJECT *a;
116 int i; 120 int i;
117 unsigned long ret=0; 121 unsigned long ret = 0;
118 unsigned char *p; 122 unsigned char *p;
119 123
120 a=ca->obj; 124 a = ca->obj;
121 switch (ca->type) 125 switch (ca->type) {
122 {
123 case ADDED_DATA: 126 case ADDED_DATA:
124 ret=a->length<<20L; 127 ret = a->length << 20L;
125 p=(unsigned char *)a->data; 128 p = (unsigned char *)a->data;
126 for (i=0; i<a->length; i++) 129 for (i = 0; i < a->length; i++)
127 ret^=p[i]<<((i*3)%24); 130 ret ^= p[i] << ((i * 3) % 24);
128 break; 131 break;
129 case ADDED_SNAME: 132 case ADDED_SNAME:
130 ret=lh_strhash(a->sn); 133 ret = lh_strhash(a->sn);
131 break; 134 break;
132 case ADDED_LNAME: 135 case ADDED_LNAME:
133 ret=lh_strhash(a->ln); 136 ret = lh_strhash(a->ln);
134 break; 137 break;
135 case ADDED_NID: 138 case ADDED_NID:
136 ret=a->nid; 139 ret = a->nid;
137 break; 140 break;
138 default: 141 default:
139 /* abort(); */ 142 /* abort(); */
140 return 0; 143 return 0;
141 }
142 ret&=0x3fffffffL;
143 ret|=ca->type<<30L;
144 return(ret);
145 } 144 }
145 ret &= 0x3fffffffL;
146 ret |= ca->type << 30L;
147 return (ret);
148}
146static IMPLEMENT_LHASH_HASH_FN(added_obj, ADDED_OBJ) 149static IMPLEMENT_LHASH_HASH_FN(added_obj, ADDED_OBJ)
147 150
148static int added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb) 151static int
149 { 152added_obj_cmp(const ADDED_OBJ *ca, const ADDED_OBJ *cb)
150 ASN1_OBJECT *a,*b; 153{
154 ASN1_OBJECT *a, *b;
151 int i; 155 int i;
152 156
153 i=ca->type-cb->type; 157 i = ca->type - cb->type;
154 if (i) return(i); 158 if (i)
155 a=ca->obj; 159 return (i);
156 b=cb->obj; 160 a = ca->obj;
157 switch (ca->type) 161 b = cb->obj;
158 { 162 switch (ca->type) {
159 case ADDED_DATA: 163 case ADDED_DATA:
160 i=(a->length - b->length); 164 i = (a->length - b->length);
161 if (i) return(i); 165 if (i)
162 return(memcmp(a->data,b->data,(size_t)a->length)); 166 return (i);
167 return (memcmp(a->data, b->data, (size_t)a->length));
163 case ADDED_SNAME: 168 case ADDED_SNAME:
164 if (a->sn == NULL) return(-1); 169 if (a->sn == NULL)
165 else if (b->sn == NULL) return(1); 170 return (-1);
166 else return(strcmp(a->sn,b->sn)); 171 else if (b->sn == NULL)
172 return (1);
173 else
174 return (strcmp(a->sn, b->sn));
167 case ADDED_LNAME: 175 case ADDED_LNAME:
168 if (a->ln == NULL) return(-1); 176 if (a->ln == NULL)
169 else if (b->ln == NULL) return(1); 177 return (-1);
170 else return(strcmp(a->ln,b->ln)); 178 else if (b->ln == NULL)
179 return (1);
180 else
181 return (strcmp(a->ln, b->ln));
171 case ADDED_NID: 182 case ADDED_NID:
172 return(a->nid-b->nid); 183 return (a->nid - b->nid);
173 default: 184 default:
174 /* abort(); */ 185 /* abort(); */
175 return 0; 186 return 0;
176 }
177 } 187 }
188}
178static IMPLEMENT_LHASH_COMP_FN(added_obj, ADDED_OBJ) 189static IMPLEMENT_LHASH_COMP_FN(added_obj, ADDED_OBJ)
179 190
180static int init_added(void) 191static int
181 { 192init_added(void)
182 if (added != NULL) return(1); 193{
183 added=lh_ADDED_OBJ_new(); 194 if (added != NULL)
184 return(added != NULL); 195 return (1);
185 } 196 added = lh_ADDED_OBJ_new();
197 return (added != NULL);
198}
186 199
187static void cleanup1_doall(ADDED_OBJ *a) 200static void
188 { 201cleanup1_doall(ADDED_OBJ *a)
189 a->obj->nid=0; 202{
190 a->obj->flags|=ASN1_OBJECT_FLAG_DYNAMIC| 203 a->obj->nid = 0;
191 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS| 204 a->obj->flags |= ASN1_OBJECT_FLAG_DYNAMIC |
192 ASN1_OBJECT_FLAG_DYNAMIC_DATA; 205 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
193 } 206 ASN1_OBJECT_FLAG_DYNAMIC_DATA;
207}
194 208
195static void cleanup2_doall(ADDED_OBJ *a) 209static void cleanup2_doall(ADDED_OBJ *a)
196 { a->obj->nid++; } 210{
211 a->obj->nid++;
212}
197 213
198static void cleanup3_doall(ADDED_OBJ *a) 214static void
199 { 215cleanup3_doall(ADDED_OBJ *a)
216{
200 if (--a->obj->nid == 0) 217 if (--a->obj->nid == 0)
201 ASN1_OBJECT_free(a->obj); 218 ASN1_OBJECT_free(a->obj);
202 free(a); 219 free(a);
203 } 220}
204 221
205static IMPLEMENT_LHASH_DOALL_FN(cleanup1, ADDED_OBJ) 222static IMPLEMENT_LHASH_DOALL_FN(cleanup1, ADDED_OBJ)
206static IMPLEMENT_LHASH_DOALL_FN(cleanup2, ADDED_OBJ) 223static IMPLEMENT_LHASH_DOALL_FN(cleanup2, ADDED_OBJ)
@@ -213,210 +230,213 @@ static IMPLEMENT_LHASH_DOALL_FN(cleanup3, ADDED_OBJ)
213 230
214int obj_cleanup_defer = 0; 231int obj_cleanup_defer = 0;
215 232
216void check_defer(int nid) 233void
217 { 234check_defer(int nid)
235{
218 if (!obj_cleanup_defer && nid >= NUM_NID) 236 if (!obj_cleanup_defer && nid >= NUM_NID)
219 obj_cleanup_defer = 1; 237 obj_cleanup_defer = 1;
220 } 238}
221 239
222void OBJ_cleanup(void) 240void
223 { 241OBJ_cleanup(void)
224 if (obj_cleanup_defer) 242{
225 { 243 if (obj_cleanup_defer) {
226 obj_cleanup_defer = 2; 244 obj_cleanup_defer = 2;
227 return ; 245 return;
228 } 246 }
229 if (added == NULL) return; 247 if (added == NULL)
248 return;
230 lh_ADDED_OBJ_down_load(added) = 0; 249 lh_ADDED_OBJ_down_load(added) = 0;
231 lh_ADDED_OBJ_doall(added,LHASH_DOALL_FN(cleanup1)); /* zero counters */ 250 lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup1)); /* zero counters */
232 lh_ADDED_OBJ_doall(added,LHASH_DOALL_FN(cleanup2)); /* set counters */ 251 lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup2)); /* set counters */
233 lh_ADDED_OBJ_doall(added,LHASH_DOALL_FN(cleanup3)); /* free objects */ 252 lh_ADDED_OBJ_doall(added, LHASH_DOALL_FN(cleanup3)); /* free objects */
234 lh_ADDED_OBJ_free(added); 253 lh_ADDED_OBJ_free(added);
235 added=NULL; 254 added = NULL;
236 } 255}
237 256
238int OBJ_new_nid(int num) 257int
239 { 258OBJ_new_nid(int num)
259{
240 int i; 260 int i;
241 261
242 i=new_nid; 262 i = new_nid;
243 new_nid+=num; 263 new_nid += num;
244 return(i); 264 return (i);
245 } 265}
246 266
247int OBJ_add_object(const ASN1_OBJECT *obj) 267int
248 { 268OBJ_add_object(const ASN1_OBJECT *obj)
269{
249 ASN1_OBJECT *o; 270 ASN1_OBJECT *o;
250 ADDED_OBJ *ao[4]={NULL,NULL,NULL,NULL},*aop; 271 ADDED_OBJ *ao[4] = {NULL, NULL, NULL, NULL}, *aop;
251 int i; 272 int i;
252 273
253 if (added == NULL) 274 if (added == NULL)
254 if (!init_added()) return(0); 275 if (!init_added())
255 if ((o=OBJ_dup(obj)) == NULL) goto err; 276 return (0);
256 if (!(ao[ADDED_NID]=(ADDED_OBJ *)malloc(sizeof(ADDED_OBJ)))) goto err2; 277 if ((o = OBJ_dup(obj)) == NULL)
278 goto err;
279 if (!(ao[ADDED_NID] = (ADDED_OBJ *)malloc(sizeof(ADDED_OBJ))))
280 goto err2;
257 if ((o->length != 0) && (obj->data != NULL)) 281 if ((o->length != 0) && (obj->data != NULL))
258 if (!(ao[ADDED_DATA]=(ADDED_OBJ *)malloc(sizeof(ADDED_OBJ)))) goto err2; 282 if (!(ao[ADDED_DATA] = (ADDED_OBJ *)malloc(sizeof(ADDED_OBJ))))
283 goto err2;
259 if (o->sn != NULL) 284 if (o->sn != NULL)
260 if (!(ao[ADDED_SNAME]=(ADDED_OBJ *)malloc(sizeof(ADDED_OBJ)))) goto err2; 285 if (!(ao[ADDED_SNAME] = (ADDED_OBJ *)malloc(sizeof(ADDED_OBJ))))
286 goto err2;
261 if (o->ln != NULL) 287 if (o->ln != NULL)
262 if (!(ao[ADDED_LNAME]=(ADDED_OBJ *)malloc(sizeof(ADDED_OBJ)))) goto err2; 288 if (!(ao[ADDED_LNAME] = (ADDED_OBJ *)malloc(sizeof(ADDED_OBJ))))
263 289 goto err2;
264 for (i=ADDED_DATA; i<=ADDED_NID; i++) 290
265 { 291 for (i = ADDED_DATA; i <= ADDED_NID; i++) {
266 if (ao[i] != NULL) 292 if (ao[i] != NULL) {
267 { 293 ao[i]->type = i;
268 ao[i]->type=i; 294 ao[i]->obj = o;
269 ao[i]->obj=o; 295 aop = lh_ADDED_OBJ_insert(added, ao[i]);
270 aop=lh_ADDED_OBJ_insert(added,ao[i]);
271 /* memory leak, buit should not normally matter */ 296 /* memory leak, buit should not normally matter */
272 if (aop != NULL) 297 if (aop != NULL)
273 free(aop); 298 free(aop);
274 }
275 } 299 }
276 o->flags&= ~(ASN1_OBJECT_FLAG_DYNAMIC|ASN1_OBJECT_FLAG_DYNAMIC_STRINGS| 300 }
277 ASN1_OBJECT_FLAG_DYNAMIC_DATA); 301 o->flags &= ~(ASN1_OBJECT_FLAG_DYNAMIC |
302 ASN1_OBJECT_FLAG_DYNAMIC_STRINGS |
303 ASN1_OBJECT_FLAG_DYNAMIC_DATA);
304
305 return (o->nid);
278 306
279 return(o->nid);
280err2: 307err2:
281 OBJerr(OBJ_F_OBJ_ADD_OBJECT,ERR_R_MALLOC_FAILURE); 308 OBJerr(OBJ_F_OBJ_ADD_OBJECT, ERR_R_MALLOC_FAILURE);
282err: 309err:
283 for (i=ADDED_DATA; i<=ADDED_NID; i++) 310 for (i = ADDED_DATA; i <= ADDED_NID; i++)
284 if (ao[i] != NULL) free(ao[i]); 311 if (ao[i] != NULL)
285 if (o != NULL) free(o); 312 free(ao[i]);
286 return(NID_undef); 313 if (o != NULL)
287 } 314 free(o);
315 return (NID_undef);
316}
288 317
289ASN1_OBJECT *OBJ_nid2obj(int n) 318ASN1_OBJECT *
290 { 319OBJ_nid2obj(int n)
291 ADDED_OBJ ad,*adp; 320{
321 ADDED_OBJ ad, *adp;
292 ASN1_OBJECT ob; 322 ASN1_OBJECT ob;
293 323
294 if ((n >= 0) && (n < NUM_NID)) 324 if ((n >= 0) && (n < NUM_NID)) {
295 { 325 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
296 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) 326 OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);
297 { 327 return (NULL);
298 OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID);
299 return(NULL);
300 }
301 return((ASN1_OBJECT *)&(nid_objs[n]));
302 } 328 }
303 else if (added == NULL) 329 return ((ASN1_OBJECT *)&(nid_objs[n]));
304 return(NULL); 330 } else if (added == NULL)
305 else 331 return (NULL);
306 { 332 else {
307 ad.type=ADDED_NID; 333 ad.type = ADDED_NID;
308 ad.obj= &ob; 334 ad.obj = &ob;
309 ob.nid=n; 335 ob.nid = n;
310 adp=lh_ADDED_OBJ_retrieve(added,&ad); 336 adp = lh_ADDED_OBJ_retrieve(added, &ad);
311 if (adp != NULL) 337 if (adp != NULL)
312 return(adp->obj); 338 return (adp->obj);
313 else 339 else {
314 { 340 OBJerr(OBJ_F_OBJ_NID2OBJ, OBJ_R_UNKNOWN_NID);
315 OBJerr(OBJ_F_OBJ_NID2OBJ,OBJ_R_UNKNOWN_NID); 341 return (NULL);
316 return(NULL);
317 }
318 } 342 }
319 } 343 }
344}
320 345
321const char *OBJ_nid2sn(int n) 346const char *
322 { 347OBJ_nid2sn(int n)
323 ADDED_OBJ ad,*adp; 348{
349 ADDED_OBJ ad, *adp;
324 ASN1_OBJECT ob; 350 ASN1_OBJECT ob;
325 351
326 if ((n >= 0) && (n < NUM_NID)) 352 if ((n >= 0) && (n < NUM_NID)) {
327 { 353 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
328 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) 354 OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
329 { 355 return (NULL);
330 OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID);
331 return(NULL);
332 }
333 return(nid_objs[n].sn);
334 } 356 }
335 else if (added == NULL) 357 return (nid_objs[n].sn);
336 return(NULL); 358 } else if (added == NULL)
337 else 359 return (NULL);
338 { 360 else {
339 ad.type=ADDED_NID; 361 ad.type = ADDED_NID;
340 ad.obj= &ob; 362 ad.obj = &ob;
341 ob.nid=n; 363 ob.nid = n;
342 adp=lh_ADDED_OBJ_retrieve(added,&ad); 364 adp = lh_ADDED_OBJ_retrieve(added, &ad);
343 if (adp != NULL) 365 if (adp != NULL)
344 return(adp->obj->sn); 366 return (adp->obj->sn);
345 else 367 else {
346 { 368 OBJerr(OBJ_F_OBJ_NID2SN, OBJ_R_UNKNOWN_NID);
347 OBJerr(OBJ_F_OBJ_NID2SN,OBJ_R_UNKNOWN_NID); 369 return (NULL);
348 return(NULL);
349 }
350 } 370 }
351 } 371 }
372}
352 373
353const char *OBJ_nid2ln(int n) 374const char *
354 { 375OBJ_nid2ln(int n)
355 ADDED_OBJ ad,*adp; 376{
377 ADDED_OBJ ad, *adp;
356 ASN1_OBJECT ob; 378 ASN1_OBJECT ob;
357 379
358 if ((n >= 0) && (n < NUM_NID)) 380 if ((n >= 0) && (n < NUM_NID)) {
359 { 381 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) {
360 if ((n != NID_undef) && (nid_objs[n].nid == NID_undef)) 382 OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
361 { 383 return (NULL);
362 OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID);
363 return(NULL);
364 }
365 return(nid_objs[n].ln);
366 } 384 }
367 else if (added == NULL) 385 return (nid_objs[n].ln);
368 return(NULL); 386 } else if (added == NULL)
369 else 387 return (NULL);
370 { 388 else {
371 ad.type=ADDED_NID; 389 ad.type = ADDED_NID;
372 ad.obj= &ob; 390 ad.obj = &ob;
373 ob.nid=n; 391 ob.nid = n;
374 adp=lh_ADDED_OBJ_retrieve(added,&ad); 392 adp = lh_ADDED_OBJ_retrieve(added, &ad);
375 if (adp != NULL) 393 if (adp != NULL)
376 return(adp->obj->ln); 394 return (adp->obj->ln);
377 else 395 else {
378 { 396 OBJerr(OBJ_F_OBJ_NID2LN, OBJ_R_UNKNOWN_NID);
379 OBJerr(OBJ_F_OBJ_NID2LN,OBJ_R_UNKNOWN_NID); 397 return (NULL);
380 return(NULL);
381 }
382 } 398 }
383 } 399 }
400}
384 401
385static int obj_cmp(const ASN1_OBJECT * const *ap, const unsigned int *bp) 402static int
386 { 403obj_cmp(const ASN1_OBJECT * const *ap, const unsigned int *bp)
404{
387 int j; 405 int j;
388 const ASN1_OBJECT *a= *ap; 406 const ASN1_OBJECT *a= *ap;
389 const ASN1_OBJECT *b= &nid_objs[*bp]; 407 const ASN1_OBJECT *b = &nid_objs[*bp];
390 408
391 j=(a->length - b->length); 409 j = (a->length - b->length);
392 if (j) return(j); 410 if (j)
393 return(memcmp(a->data,b->data,a->length)); 411 return (j);
394 } 412 return (memcmp(a->data, b->data, a->length));
413}
395 414
396IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj); 415IMPLEMENT_OBJ_BSEARCH_CMP_FN(const ASN1_OBJECT *, unsigned int, obj);
397 416
398int OBJ_obj2nid(const ASN1_OBJECT *a) 417int
399 { 418OBJ_obj2nid(const ASN1_OBJECT *a)
419{
400 const unsigned int *op; 420 const unsigned int *op;
401 ADDED_OBJ ad,*adp; 421 ADDED_OBJ ad, *adp;
402 422
403 if (a == NULL) 423 if (a == NULL)
404 return(NID_undef); 424 return (NID_undef);
405 if (a->nid != 0) 425 if (a->nid != 0)
406 return(a->nid); 426 return (a->nid);
407 427
408 if (added != NULL) 428 if (added != NULL) {
409 { 429 ad.type = ADDED_DATA;
410 ad.type=ADDED_DATA;
411 ad.obj=(ASN1_OBJECT *)a; /* XXX: ugly but harmless */ 430 ad.obj=(ASN1_OBJECT *)a; /* XXX: ugly but harmless */
412 adp=lh_ADDED_OBJ_retrieve(added,&ad); 431 adp = lh_ADDED_OBJ_retrieve(added, &ad);
413 if (adp != NULL) return (adp->obj->nid); 432 if (adp != NULL)
414 } 433 return (adp->obj->nid);
415 op=OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
416 if (op == NULL)
417 return(NID_undef);
418 return(nid_objs[*op].nid);
419 } 434 }
435 op = OBJ_bsearch_obj(&a, obj_objs, NUM_OBJ);
436 if (op == NULL)
437 return (NID_undef);
438 return (nid_objs[*op].nid);
439}
420 440
421/* Convert an object name into an ASN1_OBJECT 441/* Convert an object name into an ASN1_OBJECT
422 * if "noname" is not set then search for short and long names first. 442 * if "noname" is not set then search for short and long names first.
@@ -424,371 +444,351 @@ int OBJ_obj2nid(const ASN1_OBJECT *a)
424 * it can be used with any objects, not just registered ones. 444 * it can be used with any objects, not just registered ones.
425 */ 445 */
426 446
427ASN1_OBJECT *OBJ_txt2obj(const char *s, int no_name) 447ASN1_OBJECT *
428 { 448OBJ_txt2obj(const char *s, int no_name)
449{
429 int nid = NID_undef; 450 int nid = NID_undef;
430 ASN1_OBJECT *op=NULL; 451 ASN1_OBJECT *op = NULL;
431 unsigned char *buf; 452 unsigned char *buf;
432 unsigned char *p; 453 unsigned char *p;
433 const unsigned char *cp; 454 const unsigned char *cp;
434 int i, j; 455 int i, j;
435 456
436 if(!no_name) { 457 if (!no_name) {
437 if( ((nid = OBJ_sn2nid(s)) != NID_undef) || 458 if (((nid = OBJ_sn2nid(s)) != NID_undef) ||
438 ((nid = OBJ_ln2nid(s)) != NID_undef) ) 459 ((nid = OBJ_ln2nid(s)) != NID_undef) )
439 return OBJ_nid2obj(nid); 460 return OBJ_nid2obj(nid);
440 } 461 }
441 462
442 /* Work out size of content octets */ 463 /* Work out size of content octets */
443 i=a2d_ASN1_OBJECT(NULL,0,s,-1); 464 i = a2d_ASN1_OBJECT(NULL, 0, s, -1);
444 if (i <= 0) { 465 if (i <= 0) {
445 /* Don't clear the error */ 466 /* Don't clear the error */
446 /*ERR_clear_error();*/ 467 /*ERR_clear_error();*/
447 return NULL; 468 return NULL;
448 } 469 }
449 /* Work out total size */ 470 /* Work out total size */
450 j = ASN1_object_size(0,i,V_ASN1_OBJECT); 471 j = ASN1_object_size(0, i, V_ASN1_OBJECT);
451 472
452 if((buf=(unsigned char *)malloc(j)) == NULL) return NULL; 473 if ((buf = (unsigned char *)malloc(j)) == NULL)
474 return NULL;
453 475
454 p = buf; 476 p = buf;
455 /* Write out tag+length */ 477 /* Write out tag+length */
456 ASN1_put_object(&p,0,i,V_ASN1_OBJECT,V_ASN1_UNIVERSAL); 478 ASN1_put_object(&p, 0, i, V_ASN1_OBJECT, V_ASN1_UNIVERSAL);
457 /* Write out contents */ 479 /* Write out contents */
458 a2d_ASN1_OBJECT(p,i,s,-1); 480 a2d_ASN1_OBJECT(p, i, s, -1);
459 481
460 cp=buf; 482 cp = buf;
461 op=d2i_ASN1_OBJECT(NULL,&cp,j); 483 op = d2i_ASN1_OBJECT(NULL, &cp, j);
462 free(buf); 484 free(buf);
463 return op; 485 return op;
464 } 486}
465 487
466int OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name) 488int
489OBJ_obj2txt(char *buf, int buf_len, const ASN1_OBJECT *a, int no_name)
467{ 490{
468 int i,n=0,len,nid, first, use_bn; 491 int i, n = 0, len, nid, first, use_bn;
469 BIGNUM *bl; 492 BIGNUM *bl;
470 unsigned long l; 493 unsigned long l;
471 const unsigned char *p; 494 const unsigned char *p;
472 char tbuf[DECIMAL_SIZE(l) + 1]; 495 char tbuf[DECIMAL_SIZE(l) + 1];
473 496
474 if ((a == NULL) || (a->data == NULL)) { 497 if ((a == NULL) || (a->data == NULL)) {
475 buf[0]='\0'; 498 buf[0] = '\0';
476 return(0); 499 return (0);
477 } 500 }
478 501
479 502 if (!no_name && (nid = OBJ_obj2nid(a)) != NID_undef) {
480 if (!no_name && (nid=OBJ_obj2nid(a)) != NID_undef)
481 {
482 const char *s; 503 const char *s;
483 s=OBJ_nid2ln(nid); 504 s = OBJ_nid2ln(nid);
484 if (s == NULL) 505 if (s == NULL)
485 s=OBJ_nid2sn(nid); 506 s = OBJ_nid2sn(nid);
486 if (s) 507 if (s) {
487 {
488 if (buf) 508 if (buf)
489 strlcpy(buf,s,buf_len); 509 strlcpy(buf, s, buf_len);
490 n=strlen(s); 510 n = strlen(s);
491 return n; 511 return n;
492 }
493 } 512 }
513 }
494 514
495 515 len = a->length;
496 len=a->length; 516 p = a->data;
497 p=a->data;
498 517
499 first = 1; 518 first = 1;
500 bl = NULL; 519 bl = NULL;
501 520
502 while (len > 0) 521 while (len > 0) {
503 { 522 l = 0;
504 l=0;
505 use_bn = 0; 523 use_bn = 0;
506 for (;;) 524 for (;;) {
507 {
508 unsigned char c = *p++; 525 unsigned char c = *p++;
509 len--; 526 len--;
510 if ((len == 0) && (c & 0x80)) 527 if ((len == 0) && (c & 0x80))
511 goto err; 528 goto err;
512 if (use_bn) 529 if (use_bn) {
513 {
514 if (!BN_add_word(bl, c & 0x7f)) 530 if (!BN_add_word(bl, c & 0x7f))
515 goto err; 531 goto err;
516 } 532 } else
517 else 533 l |= c & 0x7f;
518 l |= c & 0x7f;
519 if (!(c & 0x80)) 534 if (!(c & 0x80))
520 break; 535 break;
521 if (!use_bn && (l > (ULONG_MAX >> 7L))) 536 if (!use_bn && (l > (ULONG_MAX >> 7L))) {
522 {
523 if (!bl && !(bl = BN_new())) 537 if (!bl && !(bl = BN_new()))
524 goto err; 538 goto err;
525 if (!BN_set_word(bl, l)) 539 if (!BN_set_word(bl, l))
526 goto err; 540 goto err;
527 use_bn = 1; 541 use_bn = 1;
528 } 542 }
529 if (use_bn) 543 if (use_bn) {
530 {
531 if (!BN_lshift(bl, bl, 7)) 544 if (!BN_lshift(bl, bl, 7))
532 goto err; 545 goto err;
533 } 546 } else
534 else 547 l <<= 7L;
535 l<<=7L; 548 }
536 }
537 549
538 if (first) 550 if (first) {
539 {
540 first = 0; 551 first = 0;
541 if (l >= 80) 552 if (l >= 80) {
542 {
543 i = 2; 553 i = 2;
544 if (use_bn) 554 if (use_bn) {
545 {
546 if (!BN_sub_word(bl, 80)) 555 if (!BN_sub_word(bl, 80))
547 goto err; 556 goto err;
548 } 557 } else
549 else
550 l -= 80; 558 l -= 80;
551 } 559 } else {
552 else 560 i = (int)(l / 40);
553 { 561 l -= (long)(i * 40);
554 i=(int)(l/40); 562 }
555 l-=(long)(i*40); 563 if (buf && (buf_len > 0)) {
556 }
557 if (buf && (buf_len > 0))
558 {
559 *buf++ = i + '0'; 564 *buf++ = i + '0';
560 buf_len--; 565 buf_len--;
561 }
562 n++;
563 } 566 }
567 n++;
568 }
564 569
565 if (use_bn) 570 if (use_bn) {
566 {
567 char *bndec; 571 char *bndec;
568 bndec = BN_bn2dec(bl); 572 bndec = BN_bn2dec(bl);
569 if (!bndec) 573 if (!bndec)
570 goto err; 574 goto err;
571 i = strlen(bndec); 575 i = strlen(bndec);
572 if (buf) 576 if (buf) {
573 { 577 if (buf_len > 0) {
574 if (buf_len > 0)
575 {
576 *buf++ = '.'; 578 *buf++ = '.';
577 buf_len--; 579 buf_len--;
578 } 580 }
579 strlcpy(buf,bndec,buf_len); 581 strlcpy(buf, bndec, buf_len);
580 if (i > buf_len) 582 if (i > buf_len) {
581 {
582 buf += buf_len; 583 buf += buf_len;
583 buf_len = 0; 584 buf_len = 0;
584 } 585 } else {
585 else 586 buf += i;
586 { 587 buf_len -= i;
587 buf+=i;
588 buf_len-=i;
589 }
590 } 588 }
589 }
591 n++; 590 n++;
592 n += i; 591 n += i;
593 free(bndec); 592 free(bndec);
594 } 593 } else {
595 else 594 snprintf(tbuf, sizeof tbuf, ".%lu", l);
596 { 595 i = strlen(tbuf);
597 snprintf(tbuf,sizeof tbuf,".%lu",l); 596 if (buf && (buf_len > 0)) {
598 i=strlen(tbuf); 597 strlcpy(buf, tbuf, buf_len);
599 if (buf && (buf_len > 0)) 598 if (i > buf_len) {
600 {
601 strlcpy(buf,tbuf,buf_len);
602 if (i > buf_len)
603 {
604 buf += buf_len; 599 buf += buf_len;
605 buf_len = 0; 600 buf_len = 0;
606 } 601 } else {
607 else 602 buf += i;
608 { 603 buf_len -= i;
609 buf+=i;
610 buf_len-=i;
611 }
612 } 604 }
613 n+=i;
614 l=0;
615 } 605 }
606 n += i;
607 l = 0;
616 } 608 }
609 }
617 610
618 if (bl) 611 if (bl)
619 BN_free(bl); 612 BN_free(bl);
620 return n; 613 return n;
621 614
622 err: 615err:
623 if (bl) 616 if (bl)
624 BN_free(bl); 617 BN_free(bl);
625 return -1; 618 return -1;
626} 619}
627 620
628int OBJ_txt2nid(const char *s) 621int
622OBJ_txt2nid(const char *s)
629{ 623{
630 ASN1_OBJECT *obj; 624 ASN1_OBJECT *obj;
631 int nid; 625 int nid;
626
632 obj = OBJ_txt2obj(s, 0); 627 obj = OBJ_txt2obj(s, 0);
633 nid = OBJ_obj2nid(obj); 628 nid = OBJ_obj2nid(obj);
634 ASN1_OBJECT_free(obj); 629 ASN1_OBJECT_free(obj);
635 return nid; 630 return nid;
636} 631}
637 632
638int OBJ_ln2nid(const char *s) 633int
639 { 634OBJ_ln2nid(const char *s)
635{
640 ASN1_OBJECT o; 636 ASN1_OBJECT o;
641 const ASN1_OBJECT *oo= &o; 637 const ASN1_OBJECT *oo = &o;
642 ADDED_OBJ ad,*adp; 638 ADDED_OBJ ad, *adp;
643 const unsigned int *op; 639 const unsigned int *op;
644 640
645 o.ln=s; 641 o.ln = s;
646 if (added != NULL) 642 if (added != NULL) {
647 { 643 ad.type = ADDED_LNAME;
648 ad.type=ADDED_LNAME; 644 ad.obj = &o;
649 ad.obj= &o; 645 adp = lh_ADDED_OBJ_retrieve(added, &ad);
650 adp=lh_ADDED_OBJ_retrieve(added,&ad); 646 if (adp != NULL)
651 if (adp != NULL) return (adp->obj->nid); 647 return (adp->obj->nid);
652 }
653 op=OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
654 if (op == NULL) return(NID_undef);
655 return(nid_objs[*op].nid);
656 } 648 }
649 op = OBJ_bsearch_ln(&oo, ln_objs, NUM_LN);
650 if (op == NULL)
651 return (NID_undef);
652 return (nid_objs[*op].nid);
653}
657 654
658int OBJ_sn2nid(const char *s) 655int
659 { 656OBJ_sn2nid(const char *s)
657{
660 ASN1_OBJECT o; 658 ASN1_OBJECT o;
661 const ASN1_OBJECT *oo= &o; 659 const ASN1_OBJECT *oo = &o;
662 ADDED_OBJ ad,*adp; 660 ADDED_OBJ ad, *adp;
663 const unsigned int *op; 661 const unsigned int *op;
664 662
665 o.sn=s; 663 o.sn = s;
666 if (added != NULL) 664 if (added != NULL) {
667 { 665 ad.type = ADDED_SNAME;
668 ad.type=ADDED_SNAME; 666 ad.obj = &o;
669 ad.obj= &o; 667 adp = lh_ADDED_OBJ_retrieve(added, &ad);
670 adp=lh_ADDED_OBJ_retrieve(added,&ad); 668 if (adp != NULL)
671 if (adp != NULL) return (adp->obj->nid); 669 return (adp->obj->nid);
672 }
673 op=OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
674 if (op == NULL) return(NID_undef);
675 return(nid_objs[*op].nid);
676 } 670 }
671 op = OBJ_bsearch_sn(&oo, sn_objs, NUM_SN);
672 if (op == NULL)
673 return (NID_undef);
674 return (nid_objs[*op].nid);
675}
677 676
678const void *OBJ_bsearch_(const void *key, const void *base, int num, int size, 677const void *
679 int (*cmp)(const void *, const void *)) 678OBJ_bsearch_(const void *key, const void *base, int num, int size,
680 { 679 int (*cmp)(const void *, const void *))
680{
681 return OBJ_bsearch_ex_(key, base, num, size, cmp, 0); 681 return OBJ_bsearch_ex_(key, base, num, size, cmp, 0);
682 } 682}
683 683
684const void *OBJ_bsearch_ex_(const void *key, const void *base_, int num, 684const void *
685 int size, 685OBJ_bsearch_ex_(const void *key, const void *base_, int num, int size,
686 int (*cmp)(const void *, const void *), 686 int (*cmp)(const void *, const void *), int flags)
687 int flags) 687{
688 { 688 const char *base = base_;
689 const char *base=base_; 689 int l, h, i = 0, c = 0;
690 int l,h,i=0,c=0;
691 const char *p = NULL; 690 const char *p = NULL;
692 691
693 if (num == 0) return(NULL); 692 if (num == 0)
694 l=0; 693 return (NULL);
695 h=num; 694 l = 0;
696 while (l < h) 695 h = num;
697 { 696 while (l < h) {
698 i=(l+h)/2; 697 i = (l + h) / 2;
699 p= &(base[i*size]); 698 p = &(base[i * size]);
700 c=(*cmp)(key,p); 699 c = (*cmp)(key, p);
701 if (c < 0) 700 if (c < 0)
702 h=i; 701 h = i;
703 else if (c > 0) 702 else if (c > 0)
704 l=i+1; 703 l = i + 1;
705 else 704 else
706 break; 705 break;
707 } 706 }
708 if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH)) 707 if (c != 0 && !(flags & OBJ_BSEARCH_VALUE_ON_NOMATCH))
709 p = NULL; 708 p = NULL;
710 else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH)) 709 else if (c == 0 && (flags & OBJ_BSEARCH_FIRST_VALUE_ON_MATCH)) {
711 { 710 while (i > 0 && (*cmp)(key, &(base[(i - 1) * size])) == 0)
712 while(i > 0 && (*cmp)(key,&(base[(i-1)*size])) == 0)
713 i--; 711 i--;
714 p = &(base[i*size]); 712 p = &(base[i * size]);
715 }
716 return(p);
717 } 713 }
714 return (p);
715}
718 716
719int OBJ_create_objects(BIO *in) 717int
720 { 718OBJ_create_objects(BIO *in)
719{
721 char buf[512]; 720 char buf[512];
722 int i,num=0; 721 int i, num = 0;
723 char *o,*s,*l=NULL; 722 char *o, *s, *l = NULL;
724 723
725 for (;;) 724 for (;;) {
726 { 725 s = o = NULL;
727 s=o=NULL; 726 i = BIO_gets(in, buf, 512);
728 i=BIO_gets(in,buf,512); 727 if (i <= 0)
729 if (i <= 0) return(num); 728 return (num);
730 buf[i-1]='\0'; 729 buf[i - 1] = '\0';
731 if (!isalnum((unsigned char)buf[0])) return(num); 730 if (!isalnum((unsigned char)buf[0]))
732 o=s=buf; 731 return (num);
732 o = s=buf;
733 while (isdigit((unsigned char)*s) || (*s == '.')) 733 while (isdigit((unsigned char)*s) || (*s == '.'))
734 s++; 734 s++;
735 if (*s != '\0') 735 if (*s != '\0') {
736 { 736 *(s++) = '\0';
737 *(s++)='\0';
738 while (isspace((unsigned char)*s)) 737 while (isspace((unsigned char)*s))
739 s++; 738 s++;
740 if (*s == '\0') 739 if (*s == '\0')
741 s=NULL; 740 s = NULL;
742 else 741 else {
743 { 742 l = s;
744 l=s; 743 while ((*l != '\0') &&
745 while ((*l != '\0') && !isspace((unsigned char)*l)) 744 !isspace((unsigned char)*l))
746 l++; 745 l++;
747 if (*l != '\0') 746 if (*l != '\0') {
748 { 747 *(l++) = '\0';
749 *(l++)='\0';
750 while (isspace((unsigned char)*l)) 748 while (isspace((unsigned char)*l))
751 l++; 749 l++;
752 if (*l == '\0') l=NULL; 750 if (*l == '\0')
753 } 751 l = NULL;
754 else 752 } else
755 l=NULL; 753 l = NULL;
756 }
757 } 754 }
758 else 755 } else
759 s=NULL; 756 s = NULL;
760 if ((o == NULL) || (*o == '\0')) return(num); 757 if ((o == NULL) || (*o == '\0'))
761 if (!OBJ_create(o,s,l)) return(num); 758 return (num);
759 if (!OBJ_create(o, s, l))
760 return (num);
762 num++; 761 num++;
763 }
764 /* return(num); */
765 } 762 }
763 /* return(num); */
764}
766 765
767int OBJ_create(const char *oid, const char *sn, const char *ln) 766int
768 { 767OBJ_create(const char *oid, const char *sn, const char *ln)
769 int ok=0; 768{
770 ASN1_OBJECT *op=NULL; 769 int ok = 0;
770 ASN1_OBJECT *op = NULL;
771 unsigned char *buf; 771 unsigned char *buf;
772 int i; 772 int i;
773 773
774 i=a2d_ASN1_OBJECT(NULL,0,oid,-1); 774 i = a2d_ASN1_OBJECT(NULL, 0, oid, -1);
775 if (i <= 0) return(0); 775 if (i <= 0)
776 return (0);
776 777
777 if ((buf=(unsigned char *)malloc(i)) == NULL) 778 if ((buf = (unsigned char *)malloc(i)) == NULL) {
778 { 779 OBJerr(OBJ_F_OBJ_CREATE, ERR_R_MALLOC_FAILURE);
779 OBJerr(OBJ_F_OBJ_CREATE,ERR_R_MALLOC_FAILURE); 780 return (0);
780 return(0); 781 }
781 } 782 i = a2d_ASN1_OBJECT(buf, i, oid, -1);
782 i=a2d_ASN1_OBJECT(buf,i,oid,-1);
783 if (i == 0) 783 if (i == 0)
784 goto err; 784 goto err;
785 op=(ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1),buf,i,sn,ln); 785 op = (ASN1_OBJECT *)ASN1_OBJECT_create(OBJ_new_nid(1), buf, i, sn, ln);
786 if (op == NULL) 786 if (op == NULL)
787 goto err; 787 goto err;
788 ok=OBJ_add_object(op); 788 ok = OBJ_add_object(op);
789
789err: 790err:
790 ASN1_OBJECT_free(op); 791 ASN1_OBJECT_free(op);
791 free(buf); 792 free(buf);
792 return(ok); 793 return (ok);
793 } 794}
794