1 /*        $NetBSD: componentlib.c,v 1.2 2021/08/14 16:14:51 christos Exp $      */
2 
3 /* Copyright 2004 IBM Corporation
4  * All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted only as authorized by the OpenLDAP
7  * Public License.
8  */
9 /* ACKNOWLEDGEMENTS
10  * This work originally developed by Sang Seok Lim
11  * 2004/06/18       03:20:00  slim@OpenLDAP.org
12  */
13 
14 #include <sys/cdefs.h>
15 __RCSID("$NetBSD: componentlib.c,v 1.2 2021/08/14 16:14:51 christos Exp $");
16 
17 #include "portable.h"
18 #include <ac/string.h>
19 #include <ac/socket.h>
20 #include <ldap_pvt.h>
21 #include "lutil.h"
22 #include <ldap.h>
23 #include "slap.h"
24 #include "component.h"
25 
26 #include "componentlib.h"
27 #include "asn.h"
28 #include <asn-gser.h>
29 #include <stdlib.h>
30 
31 #include <string.h>
32 
33 #ifndef SLAPD_COMP_MATCH
34 #define SLAPD_COMP_MATCH SLAPD_MOD_DYNAMIC
35 #endif
36 
37 #ifdef SLAPD_COMP_MATCH
38 /*
39  * Matching function : BIT STRING
40  */
41 int
MatchingComponentBits(char * oid,ComponentSyntaxInfo * csi_attr,ComponentSyntaxInfo * csi_assert)42 MatchingComponentBits ( char* oid, ComponentSyntaxInfo *csi_attr,
43                               ComponentSyntaxInfo *csi_assert )
44 {
45           int rc;
46         MatchingRule* mr;
47         ComponentBits *a, *b;
48 
49         if ( oid ) {
50                 mr = retrieve_matching_rule(oid, (AsnTypeId)csi_attr->csi_comp_desc->cd_type_id );
51                 if ( mr )
52                         return component_value_match( mr, csi_attr , csi_assert );
53         }
54         a = ((ComponentBits*)csi_attr);
55         b = ((ComponentBits*)csi_assert);
56           rc = ( a->value.bitLen == b->value.bitLen &&
57                     strncmp( a->value.bits,b->value.bits,a->value.bitLen ) == 0 );
58         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
59 }
60 
61 /*
62  * Free function: BIT STRING
63  */
64 void
FreeComponentBits(ComponentBits * v)65 FreeComponentBits ( ComponentBits* v ) {
66           FreeAsnBits( &v->value );
67 }
68 
69 /*
70  * GSER Encoder : BIT STRING
71  */
72 int
GEncComponentBits(GenBuf * b,ComponentBits * in)73 GEncComponentBits ( GenBuf *b, ComponentBits *in )
74 {
75           GAsnBits bits = {0};
76 
77           bits.value = in->value;
78           if ( !in )
79                     return (-1);
80           return GEncAsnBitsContent ( b, &bits);
81 }
82 
83 
84 /*
85  * GSER Decoder : BIT STRING
86  */
87 int
GDecComponentBits(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)88 GDecComponentBits ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
89 {
90         char* peek_head;
91         int i, strLen;
92         void* component_values;
93         ComponentBits* k, **k2;
94           GAsnBits result;
95 
96         k = (ComponentBits*) v;
97 
98         if ( mode & DEC_ALLOC_MODE_0 ) {
99                 k2 = (ComponentBits**) v;
100                 *k2 = (ComponentBits*) CompAlloc( mem_op, sizeof( ComponentBits ) );
101                     if ( !*k2 ) return LDAP_DECODING_ERROR;
102                 k = *k2;
103         }
104 
105           if ( GDecAsnBitsContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
106                     if ( k ) CompFree( mem_op,  k );
107                     return LDAP_DECODING_ERROR;
108           }
109           k->value = result.value;
110           k->comp_desc = get_component_description (BASICTYPE_BITSTRING);
111 
112           return LDAP_SUCCESS;
113 }
114 
115 /*
116  * Component BER Decoder : BIT STRING
117  */
118 int
BDecComponentBitsTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)119 BDecComponentBitsTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
120           return BDecComponentBits ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
121 }
122 
123 int
BDecComponentBits(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)124 BDecComponentBits ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
125                               AsnLen *bytesDecoded, int mode )
126 {
127         char* peek_head;
128         int i, strLen, rc;
129         void* component_values;
130         ComponentBits* k, **k2;
131           AsnBits result;
132 
133         k = (ComponentBits*) v;
134 
135         if ( mode & DEC_ALLOC_MODE_0 ) {
136                 k2 = (ComponentBits**) v;
137                 *k2 = (ComponentBits*) CompAlloc( mem_op, sizeof( ComponentBits ) );
138                     if ( !*k2 ) return LDAP_DECODING_ERROR;
139                 k = *k2;
140         }
141 
142           if ( mode & CALL_TAG_DECODER ){
143                     mode = mode & CALL_CONTENT_DECODER;
144                     rc = BDecAsnBits ( mem_op, b, &result, bytesDecoded );
145           } else {
146                     rc = BDecAsnBitsContent ( mem_op, b, tagId, len, &result, bytesDecoded );
147           }
148 
149           if ( rc < 0 ) {
150                     if ( k ) CompFree( mem_op,  k );
151                     return LDAP_DECODING_ERROR;
152           }
153 
154           k->value = result;
155           k->comp_desc = get_component_description (BASICTYPE_BITSTRING);
156 
157           return LDAP_SUCCESS;
158 }
159 
160 /*
161  * Component GSER BMPString Encoder
162  */
163 int
GEncComponentBMPString(GenBuf * b,ComponentBMPString * in)164 GEncComponentBMPString ( GenBuf *b, ComponentBMPString *in )
165 {
166           GBMPString t = {0};
167 
168           if ( !in || in->value.octetLen <= 0 )
169                     return (-1);
170           t.value = in->value;
171           return GEncBMPStringContent ( b, &t );
172 }
173 
174 /*
175  * Component GSER BMPString Decoder
176  */
177 int
GDecComponentBMPString(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)178 GDecComponentBMPString ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode)
179 {
180         char* peek_head;
181         int i, strLen;
182         void* component_values;
183         ComponentBMPString* k, **k2;
184           GBMPString result;
185 
186         k = (ComponentBMPString*) v;
187 
188         if ( mode & DEC_ALLOC_MODE_0 ) {
189                 k2 = (ComponentBMPString**) v;
190                 *k2 = (ComponentBMPString*) CompAlloc( mem_op, sizeof( ComponentBMPString ) );
191                     if ( !*k2 ) return LDAP_DECODING_ERROR;
192                 k = *k2;
193         }
194 
195         *bytesDecoded = 0;
196 
197           if ( GDecBMPStringContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
198                     if ( k ) CompFree( mem_op, k );
199                     return LDAP_DECODING_ERROR;
200           }
201 
202           k->value = result.value;
203           k->comp_desc = get_component_description (BASICTYPE_BMP_STR);
204 
205           return LDAP_SUCCESS;
206 
207 }
208 
209 /*
210  * Component BER BMPString Decoder
211  */
212 int
BDecComponentBMPStringTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)213 BDecComponentBMPStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
214           return BDecComponentBMPString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
215 }
216 
217 int
BDecComponentBMPString(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)218 BDecComponentBMPString ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
219                               AsnLen *bytesDecoded, int mode )
220 {
221         char* peek_head;
222         int i, strLen, rc;
223         void* component_values;
224         ComponentBMPString* k, **k2;
225           BMPString result;
226 
227         k = (ComponentBMPString*) v;
228 
229         if ( mode & DEC_ALLOC_MODE_0 ) {
230                 k2 = (ComponentBMPString**) v;
231                 *k2 = (ComponentBMPString*) CompAlloc( mem_op, sizeof( ComponentBMPString ) );
232                     if ( !*k2 ) return LDAP_DECODING_ERROR;
233                 k = *k2;
234         }
235 
236           if ( mode & CALL_TAG_DECODER ){
237                     mode = mode & CALL_CONTENT_DECODER;
238                     rc = BDecBMPString ( mem_op, b, &result, bytesDecoded );
239           } else {
240                     rc = BDecBMPStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
241           }
242 
243           if ( rc < 0 ) {
244                     if ( k ) CompFree( mem_op, k );
245                     return LDAP_DECODING_ERROR;
246           }
247 
248           k->value = result;
249           k->comp_desc = get_component_description (BASICTYPE_BMP_STR);
250 
251           return LDAP_SUCCESS;
252 
253 }
254 
255 /*
256  * Component GSER Encoder : UTF8 String
257  */
258 int
GEncComponentUTF8String(GenBuf * b,ComponentUTF8String * in)259 GEncComponentUTF8String ( GenBuf *b, ComponentUTF8String *in )
260 {
261           GUTF8String t = {0};
262           if ( !in || in->value.octetLen <= 0 )
263                     return (-1);
264           t.value = in->value;
265           return GEncUTF8StringContent ( b, &t );
266 }
267 
268 /*
269  * Component GSER Decoder :  UTF8 String
270  */
271 int
GDecComponentUTF8String(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)272 GDecComponentUTF8String ( void* mem_op, GenBuf *b, void *v,
273                                         AsnLen *bytesDecoded, int mode) {
274         char* peek_head;
275         int i, strLen;
276         void* component_values;
277         ComponentUTF8String* k, **k2;
278           GUTF8String result;
279 
280         k = (ComponentUTF8String*) v;
281 
282         if ( mode & DEC_ALLOC_MODE_0 ) {
283                 k2 = (ComponentUTF8String**) v;
284                 *k2 = (ComponentUTF8String*)CompAlloc( mem_op, sizeof( ComponentUTF8String ) );
285                     if ( !*k2 ) return LDAP_DECODING_ERROR;
286                 k = *k2;
287         }
288 
289         *bytesDecoded = 0;
290 
291           if ( GDecUTF8StringContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
292                     if ( k ) CompFree( mem_op,  k );
293                     return LDAP_DECODING_ERROR;
294           }
295 
296           k->value = result.value;
297           k->comp_desc = get_component_description (BASICTYPE_UTF8_STR);
298 
299           return LDAP_SUCCESS;
300 }
301 
302 /*
303  * Component BER Decoder : UTF8String
304  */
305 int
BDecComponentUTF8StringTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)306 BDecComponentUTF8StringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
307           return BDecComponentUTF8String ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
308 }
309 
310 int
BDecComponentUTF8String(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)311 BDecComponentUTF8String ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len,
312                                         void *v, AsnLen *bytesDecoded, int mode )
313 {
314         char* peek_head;
315         int i, strLen, rc;
316         void* component_values;
317         ComponentUTF8String* k, **k2;
318           UTF8String result;
319 
320         k = (ComponentUTF8String*) v;
321 
322         if ( mode & DEC_ALLOC_MODE_0 ) {
323                 k2 = (ComponentUTF8String**) v;
324                 *k2 = (ComponentUTF8String*) CompAlloc( mem_op, sizeof( ComponentUTF8String ) );
325                     if ( !*k2 ) return LDAP_DECODING_ERROR;
326                 k = *k2;
327         }
328 
329           if ( mode & CALL_TAG_DECODER ){
330                     mode = mode & CALL_CONTENT_DECODER;
331                     rc = BDecUTF8String ( mem_op, b, &result, bytesDecoded );
332           } else {
333                     rc = BDecUTF8StringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
334           }
335           if ( rc < 0 ) {
336                     if ( k ) CompFree( mem_op,  k );
337                     return LDAP_DECODING_ERROR;
338           }
339 
340           k->value = result;
341           k->comp_desc = get_component_description (BASICTYPE_UTF8_STR);
342 
343           return LDAP_SUCCESS;
344 }
345 
346 /*
347  * Component GSER Encoder :  Teletex String
348  */
349 int
GEncComponentTeletexString(GenBuf * b,ComponentTeletexString * in)350 GEncComponentTeletexString ( GenBuf *b, ComponentTeletexString *in )
351 {
352           GTeletexString t = {0};
353 
354           if ( !in || in->value.octetLen <= 0 )
355                     return (-1);
356           t.value = in->value;
357           return GEncTeletexStringContent ( b, &t );
358 }
359 
360 /*
361  * Component GSER Decoder :  Teletex String
362  */
363 int
GDecComponentTeletexString(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)364 GDecComponentTeletexString  ( void* mem_op, GenBuf *b, void *v,
365                                                   AsnLen *bytesDecoded, int mode) {
366         char* peek_head;
367         int i, strLen;
368         void* component_values;
369         ComponentTeletexString* k, **k2;
370           GTeletexString result;
371 
372         k = (ComponentTeletexString*) v;
373 
374         if ( mode & DEC_ALLOC_MODE_0 ) {
375                 k2 = (ComponentTeletexString**) v;
376                 *k2 = (ComponentTeletexString*)CompAlloc( mem_op, sizeof( ComponentTeletexString ) );
377                     if ( !*k2 ) return LDAP_DECODING_ERROR;
378                 k = *k2;
379         }
380 
381         *bytesDecoded = 0;
382 
383           if ( GDecTeletexStringContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
384                     if ( k ) CompFree( mem_op,  k );
385                     return LDAP_DECODING_ERROR;
386           }
387 
388           k->value = result.value;
389           k->comp_desc = get_component_description (BASICTYPE_VIDEOTEX_STR);
390 
391           return LDAP_SUCCESS;
392 }
393 
394 
395 /*
396  * Matching function : BOOLEAN
397  */
398 int
MatchingComponentBool(char * oid,ComponentSyntaxInfo * csi_attr,ComponentSyntaxInfo * csi_assert)399 MatchingComponentBool(char* oid, ComponentSyntaxInfo* csi_attr,
400                         ComponentSyntaxInfo* csi_assert )
401 {
402         MatchingRule* mr;
403         ComponentBool *a, *b;
404 
405         if( oid ) {
406                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
407                 if ( mr )
408                         return component_value_match( mr, csi_attr , csi_assert );
409         }
410 
411         a = ((ComponentBool*)csi_attr);
412         b = ((ComponentBool*)csi_assert);
413 
414         return (a->value == b->value) ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
415 }
416 
417 /*
418  * GSER Encoder : BOOLEAN
419  */
420 int
GEncComponentBool(GenBuf * b,ComponentBool * in)421 GEncComponentBool ( GenBuf *b, ComponentBool *in )
422 {
423           GAsnBool t = {0};
424 
425           if ( !in )
426                     return (-1);
427           t.value = in->value;
428           return GEncAsnBoolContent ( b, &t );
429 }
430 
431 /*
432  * GSER Decoder : BOOLEAN
433  */
434 int
GDecComponentBool(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)435 GDecComponentBool ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
436 {
437         char* peek_head;
438         int i, strLen;
439         ComponentBool* k, **k2;
440           GAsnBool result;
441 
442         k = (ComponentBool*) v;
443 
444         if ( mode & DEC_ALLOC_MODE_0 ) {
445                 k2 = (ComponentBool**) v;
446                 *k2 = (ComponentBool*) CompAlloc( mem_op, sizeof( ComponentBool ) );
447                     if ( !*k2 ) return LDAP_DECODING_ERROR;
448                 k = *k2;
449         }
450 
451           if ( GDecAsnBoolContent( mem_op, b, &result, bytesDecoded ) < 0 ) {
452                     if ( k ) CompFree ( mem_op, k );
453                     return LDAP_DECODING_ERROR;
454           }
455 
456           k->value = result.value;
457           k->comp_desc = get_component_description (BASICTYPE_BOOLEAN);
458 
459         return LDAP_SUCCESS;
460 }
461 
462 /*
463  * Component BER Decoder : BOOLEAN
464  */
465 int
BDecComponentBoolTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)466 BDecComponentBoolTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
467           return BDecComponentBool ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
468 }
469 
470 int
BDecComponentBool(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)471 BDecComponentBool ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
472                               AsnLen *bytesDecoded, int mode )
473 {
474         char* peek_head;
475         int i, strLen, rc;
476         ComponentBool* k, **k2;
477           AsnBool result;
478 
479         k = (ComponentBool*) v;
480 
481         if ( mode & DEC_ALLOC_MODE_0 ) {
482                 k2 = (ComponentBool**) v;
483                 *k2 = (ComponentBool*) CompAlloc( mem_op, sizeof( ComponentBool ) );
484                     if ( !*k2 ) return LDAP_DECODING_ERROR;
485                 k = *k2;
486         }
487 
488           if ( mode & CALL_TAG_DECODER ){
489                     mode = mode & CALL_CONTENT_DECODER;
490                     rc = BDecAsnBool ( mem_op, b, &result, bytesDecoded );
491           } else {
492                     rc = BDecAsnBoolContent( mem_op, b, tagId, len, &result, bytesDecoded );
493           }
494           if ( rc < 0 ) {
495                     if ( k ) CompFree ( mem_op, k );
496                     return LDAP_DECODING_ERROR;
497           }
498 
499           k->value = result;
500           k->comp_desc = get_component_description (BASICTYPE_BOOLEAN);
501 
502         return LDAP_SUCCESS;
503 }
504 
505 /*
506  * Matching function : ENUMERATE
507  */
508 int
MatchingComponentEnum(char * oid,ComponentSyntaxInfo * csi_attr,ComponentSyntaxInfo * csi_assert)509 MatchingComponentEnum ( char* oid, ComponentSyntaxInfo *csi_attr,
510                               ComponentSyntaxInfo *csi_assert )
511 {
512         int rc;
513         MatchingRule* mr;
514         ComponentEnum *a, *b;
515 
516         if( oid ) {
517                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
518                 if ( mr )
519                         return component_value_match( mr, csi_attr , csi_assert );
520         }
521         a = ((ComponentEnum*)csi_attr);
522         b = ((ComponentEnum*)csi_assert);
523         rc = (a->value == b->value);
524 
525         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
526 }
527 
528 /*
529  * GSER Encoder : ENUMERATE
530  */
531 int
GEncComponentEnum(GenBuf * b,ComponentEnum * in)532 GEncComponentEnum ( GenBuf *b, ComponentEnum *in )
533 {
534           GAsnEnum t = {0};
535 
536           if ( !in )
537                     return (-1);
538           t.value = in->value;
539           return GEncAsnEnumContent ( b, &t );
540 }
541 
542 /*
543  * GSER Decoder : ENUMERATE
544  */
545 int
GDecComponentEnum(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)546 GDecComponentEnum ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
547 {
548         char* peek_head;
549         int i, strLen;
550         void* component_values;
551         ComponentEnum* k, **k2;
552           GAsnEnum result;
553 
554         k = (ComponentEnum*) v;
555 
556         if ( mode & DEC_ALLOC_MODE_0 ) {
557                 k2 = (ComponentEnum**) v;
558                 *k2 = (ComponentEnum*) CompAlloc( mem_op, sizeof( ComponentEnum ) );
559                     if ( !*k2 ) return LDAP_DECODING_ERROR;
560                 k = *k2;
561         }
562 
563           if ( GDecAsnEnumContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
564                     if ( k ) CompFree ( mem_op, k );
565                     return LDAP_DECODING_ERROR;
566           }
567 
568           k->value_identifier.bv_val = result.value_identifier;
569           k->value_identifier.bv_len = result.len;
570 
571           k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
572           if ( !k->comp_desc )  {
573                     if ( k ) CompFree ( mem_op, k );
574                     return LDAP_DECODING_ERROR;
575           }
576           k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentEnum;
577           k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentEnum;
578           k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentEnum;
579           k->comp_desc->cd_free = (comp_free_func*)NULL;
580           k->comp_desc->cd_extract_i = NULL;
581           k->comp_desc->cd_type = ASN_BASIC;
582           k->comp_desc->cd_type_id = BASICTYPE_ENUMERATED;
583           k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentEnum;
584 
585           return LDAP_SUCCESS;
586 }
587 
588 /*
589  * Component BER Decoder : ENUMERATE
590  */
591 int
BDecComponentEnumTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)592 BDecComponentEnumTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
593           return BDecComponentEnum ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
594 }
595 
596 int
BDecComponentEnum(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)597 BDecComponentEnum ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
598                               AsnLen *bytesDecoded, int mode )
599 {
600         char* peek_head;
601         int i, strLen, rc;
602         void* component_values;
603         ComponentEnum* k, **k2;
604           AsnEnum result;
605 
606         k = (ComponentEnum*) v;
607 
608         if ( mode & DEC_ALLOC_MODE_0 ) {
609                 k2 = (ComponentEnum**) v;
610                 *k2 = (ComponentEnum*) CompAlloc( mem_op, sizeof( ComponentEnum ) );
611                     if ( k ) return LDAP_DECODING_ERROR;
612                 k = *k2;
613         }
614 
615           if ( mode & CALL_TAG_DECODER ){
616                     mode = mode & CALL_CONTENT_DECODER;
617                     rc = BDecAsnEnum ( mem_op, b, &result, bytesDecoded );
618           } else {
619                     rc = BDecAsnEnumContent ( mem_op, b, tagId, len, &result, bytesDecoded );
620           }
621           if ( rc < 0 ) {
622                     if ( k ) CompFree ( mem_op, k );
623                     return LDAP_DECODING_ERROR;
624           }
625 
626           k->value = result;
627 
628         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
629           if ( !k->comp_desc )  {
630                     if ( k  ) CompFree ( mem_op, k );
631                     return LDAP_DECODING_ERROR;
632           }
633           k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentEnum;
634         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentEnum;
635         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentEnum;
636           k->comp_desc->cd_free = (comp_free_func*)NULL;
637         k->comp_desc->cd_extract_i = NULL;
638         k->comp_desc->cd_type = ASN_BASIC;
639         k->comp_desc->cd_type_id = BASICTYPE_ENUMERATED;
640         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentEnum;
641 
642           return LDAP_SUCCESS;
643 }
644 
645 /*
646  * Component GSER Encoder : IA5String
647  */
648 int
GEncComponentIA5Stirng(GenBuf * b,ComponentIA5String * in)649 GEncComponentIA5Stirng ( GenBuf *b, ComponentIA5String* in )
650 {
651           GIA5String t = {0};
652           t.value = in->value;
653           if ( !in || in->value.octetLen <= 0 ) return (-1);
654           return GEncIA5StringContent( b, &t );
655 }
656 
657 /*
658  * Component BER Decoder : IA5String
659  */
660 int
BDecComponentIA5StringTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)661 BDecComponentIA5StringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
662           return BDecComponentIA5String ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
663 }
664 
665 int
BDecComponentIA5String(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)666 BDecComponentIA5String ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
667                               AsnLen *bytesDecoded, int mode )
668 {
669         char* peek_head;
670         int i, strLen, rc;
671         void* component_values;
672         ComponentIA5String* k, **k2;
673           IA5String result;
674 
675         k = (ComponentIA5String*) v;
676 
677         if ( mode & DEC_ALLOC_MODE_0 ) {
678                 k2 = (ComponentIA5String**) v;
679                 *k2 = (ComponentIA5String*) CompAlloc( mem_op, sizeof( ComponentIA5String ) );
680                     if ( !*k2 ) return LDAP_DECODING_ERROR;
681                 k = *k2;
682         }
683 
684           if ( mode & CALL_TAG_DECODER ){
685                     mode = mode & CALL_CONTENT_DECODER;
686                     rc = BDecIA5String ( mem_op, b, &result, bytesDecoded );
687           } else {
688                     rc = BDecIA5StringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
689           }
690           if ( rc < 0 ) {
691                     if ( k ) CompFree ( mem_op, k );
692                     return LDAP_DECODING_ERROR;
693           }
694 
695           k->value = result;
696 
697         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
698           if ( !k->comp_desc )  {
699                     if ( k ) CompFree ( mem_op, k );
700                     return LDAP_DECODING_ERROR;
701           }
702           k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentIA5String;
703         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentIA5String;
704         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentIA5String;
705           k->comp_desc->cd_free = (comp_free_func*)FreeComponentIA5String;
706         k->comp_desc->cd_extract_i = NULL;
707         k->comp_desc->cd_type = ASN_BASIC;
708         k->comp_desc->cd_type_id = BASICTYPE_IA5_STR;
709         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentIA5String;
710 
711           return LDAP_SUCCESS;
712 }
713 
714 /*
715  * Matching function : INTEGER
716  */
717 int
MatchingComponentInt(char * oid,ComponentSyntaxInfo * csi_attr,ComponentSyntaxInfo * csi_assert)718 MatchingComponentInt(char* oid, ComponentSyntaxInfo* csi_attr,
719                         ComponentSyntaxInfo* csi_assert )
720 {
721         MatchingRule* mr;
722         ComponentInt *a, *b;
723 
724         if( oid ) {
725                 /* check if this ASN type's matching rule is overridden */
726                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
727                 /* if existing function is overridden, call the overriding
728 function*/
729                 if ( mr )
730                         return component_value_match( mr, csi_attr , csi_assert );
731         }
732         a = ((ComponentInt*)csi_attr);
733         b = ((ComponentInt*)csi_assert);
734 
735         return ( a->value == b->value ) ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
736 }
737 
738 /*
739  * GSER Encoder : INTEGER
740  */
741 int
GEncComponentInt(GenBuf * b,ComponentInt * in)742 GEncComponentInt ( GenBuf *b, ComponentInt* in )
743 {
744           GAsnInt t = {0};
745 
746           if ( !in )
747                     return (-1);
748           t.value = in->value;
749           return GEncAsnIntContent ( b, &t );
750 }
751 
752 /*
753  * GSER Decoder : INTEGER
754  */
755 int
GDecComponentInt(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)756 GDecComponentInt( void* mem_op, GenBuf * b, void *v, AsnLen *bytesDecoded, int mode)
757 {
758         char* peek_head;
759         int i, strLen;
760         void* component_values;
761         ComponentInt* k, **k2;
762           GAsnInt result;
763 
764         k = (ComponentInt*) v;
765 
766         if ( mode & DEC_ALLOC_MODE_0 ) {
767                 k2 = (ComponentInt**) v;
768                 *k2 = (ComponentInt*) CompAlloc( mem_op, sizeof( ComponentInt ) );
769                     if ( !*k2 ) return LDAP_DECODING_ERROR;
770                 k = *k2;
771         }
772 
773           if ( GDecAsnIntContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
774                     if ( k ) CompFree ( mem_op, k );
775                     return LDAP_DECODING_ERROR;
776           }
777           k->value = result.value;
778           k->comp_desc = get_component_description (BASICTYPE_INTEGER );
779 
780         return LDAP_SUCCESS;
781 }
782 
783 /*
784  * Component BER Decoder : INTEGER
785  */
786 int
BDecComponentIntTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)787 BDecComponentIntTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
788           return BDecComponentInt ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
789 }
790 
791 int
BDecComponentInt(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)792 BDecComponentInt ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
793                               AsnLen *bytesDecoded, int mode )
794 {
795         char* peek_head;
796         int i, strLen, rc;
797         void* component_values;
798         ComponentInt* k, **k2;
799           AsnInt result;
800 
801         k = (ComponentInt*) v;
802 
803         if ( mode & DEC_ALLOC_MODE_0 ) {
804                 k2 = (ComponentInt**) v;
805                 *k2 = (ComponentInt*) CompAlloc( mem_op, sizeof( ComponentInt ) );
806                     if ( !*k2 ) return LDAP_DECODING_ERROR;
807                 k = *k2;
808         }
809 
810           if ( mode & CALL_TAG_DECODER ){
811                     mode = mode & CALL_CONTENT_DECODER;
812                     rc = BDecAsnInt ( mem_op, b, &result, bytesDecoded );
813           } else {
814                     rc = BDecAsnIntContent ( mem_op, b, tagId, len, &result, bytesDecoded );
815           }
816           k->value = result;
817 
818           k->comp_desc = get_component_description (BASICTYPE_INTEGER );
819 
820         return LDAP_SUCCESS;
821 }
822 
823 /*
824  * Matching function : NULL
825  */
826 int
MatchingComponentNull(char * oid,ComponentSyntaxInfo * csi_attr,ComponentSyntaxInfo * csi_assert)827 MatchingComponentNull ( char *oid, ComponentSyntaxInfo *csi_attr,
828                               ComponentSyntaxInfo *csi_assert )
829 {
830         MatchingRule* mr;
831         ComponentNull *a, *b;
832 
833         if( oid ) {
834                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
835                 if ( mr )
836                         return component_value_match( mr, csi_attr , csi_assert );
837         }
838         a = ((ComponentNull*)csi_attr);
839         b = ((ComponentNull*)csi_assert);
840 
841         return (a->value == b->value) ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
842 }
843 
844 /*
845  * GSER Encoder : NULL
846  */
847 int
GEncComponentNull(GenBuf * b,ComponentNull * in)848 GEncComponentNull ( GenBuf *b, ComponentNull *in )
849 {
850           GAsnNull t = {0};
851 
852           if ( !in )
853                     return (-1);
854           t.value = in->value;
855           return GEncAsnNullContent ( b, &t );
856 }
857 
858 /*
859  * GSER Decoder : NULL
860  */
861 int
GDecComponentNull(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)862 GDecComponentNull ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
863 {
864         char* peek_head;
865         int i, strLen;
866         void* component_values;
867         ComponentNull* k, **k2;
868           GAsnNull result;
869 
870         k = (ComponentNull*) v;
871 
872         if ( mode & DEC_ALLOC_MODE_0 ) {
873                 k2 = (ComponentNull**) v;
874                 *k2 = (ComponentNull*) CompAlloc( mem_op, sizeof( ComponentNull ) );
875                     if ( !*k2 ) return LDAP_DECODING_ERROR;
876                 k = *k2;
877         }
878 
879           if ( GDecAsnNullContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
880                     if ( k ) CompFree ( mem_op, k );
881                     return LDAP_DECODING_ERROR;
882           }
883           k->value = result.value;
884 
885           k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
886           if ( !k->comp_desc )  {
887                     if ( k ) CompFree ( mem_op, k );
888                     return LDAP_DECODING_ERROR;
889           }
890           k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentNull;
891           k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentNull;
892           k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentNull;
893           k->comp_desc->cd_free = (comp_free_func*)FreeComponentNull;
894           k->comp_desc->cd_extract_i = NULL;
895           k->comp_desc->cd_type = ASN_BASIC;
896           k->comp_desc->cd_type_id = BASICTYPE_NULL;
897           k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentNull;
898 
899           return LDAP_SUCCESS;
900 }
901 
902 /*
903  * Component BER Decoder : NULL
904  */
905 int
BDecComponentNullTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)906 BDecComponentNullTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
907 {
908           return BDecComponentNull ( mem_op, b, 0, 0, v,bytesDecoded, mode|CALL_TAG_DECODER );
909 }
910 
911 int
BDecComponentNull(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)912 BDecComponentNull ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
913                               AsnLen *bytesDecoded, int mode )
914 {
915         char* peek_head;
916         int i, strLen, rc;
917         void* component_values;
918         ComponentNull* k, **k2;
919           AsnNull result;
920 
921         k = (ComponentNull*) v;
922 
923         if ( mode & DEC_ALLOC_MODE_0 ) {
924                 k2 = (ComponentNull**) v;
925                 *k2 = (ComponentNull*) CompAlloc( mem_op, sizeof( ComponentNull ) );
926                     if ( !*k2 ) return LDAP_DECODING_ERROR;
927                 k = *k2;
928         }
929 
930           if ( mode & CALL_TAG_DECODER ){
931                     mode = mode & CALL_CONTENT_DECODER;
932                     rc = BDecAsnNull ( mem_op, b, &result, bytesDecoded );
933           }
934           else {
935                     rc = BDecAsnNullContent ( mem_op, b, tagId, len, &result, bytesDecoded);
936           }
937           if ( rc < 0 ) {
938                     if ( k ) CompFree ( mem_op, k );
939                     return LDAP_DECODING_ERROR;
940           }
941           k->value = result;
942 
943           k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
944           if ( !k->comp_desc )  {
945                     if ( k ) CompFree ( mem_op, k );
946                     return LDAP_DECODING_ERROR;
947           }
948           k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentNull;
949           k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentNull;
950           k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentNull;
951           k->comp_desc->cd_free = (comp_free_func*)FreeComponentNull;
952           k->comp_desc->cd_extract_i = NULL;
953           k->comp_desc->cd_type = ASN_BASIC;
954           k->comp_desc->cd_type_id = BASICTYPE_NULL;
955           k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentNull;
956           return LDAP_SUCCESS;
957 }
958 
959 /*
960  * Component BER Decoder : NumericString
961  */
962 int
BDecComponentNumericStringTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)963 BDecComponentNumericStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
964           return BDecComponentNumericString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
965 }
966 
967 int
BDecComponentNumericString(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)968 BDecComponentNumericString ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
969 {
970         char* peek_head;
971         int i, strLen, rc;
972         void* component_values;
973         ComponentNumericString* k, **k2;
974           NumericString result;
975 
976         k = (ComponentNumericString*) v;
977 
978         if ( mode & DEC_ALLOC_MODE_0 ) {
979                 k2 = (ComponentNumericString**) v;
980                 *k2 = (ComponentNumericString*) CompAlloc( mem_op, sizeof( ComponentNumericString ) );
981                     if ( !*k2 ) return LDAP_DECODING_ERROR;
982                 k = *k2;
983         }
984 
985           if ( mode & CALL_TAG_DECODER ){
986                     mode = mode & CALL_CONTENT_DECODER;
987                     rc = BDecNumericString ( mem_op, b, &result, bytesDecoded );
988           } else {
989                     rc = BDecNumericStringContent ( mem_op, b, tagId, len, &result, bytesDecoded);
990           }
991           if ( rc < 0 ) {
992                     if ( k ) CompFree ( mem_op, k );
993                     return LDAP_DECODING_ERROR;
994           }
995           k->value = result;
996 
997         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
998           if ( !k->comp_desc )  {
999                     if ( k ) CompFree ( mem_op, k );
1000                     return LDAP_DECODING_ERROR;
1001           }
1002           k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentNumericString;
1003         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentNumericString;
1004         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentNumericString;
1005           k->comp_desc->cd_free = (comp_free_func*)FreeComponentNumericString;
1006         k->comp_desc->cd_extract_i = NULL;
1007         k->comp_desc->cd_type = ASN_BASIC;
1008         k->comp_desc->cd_type_id = BASICTYPE_NUMERIC_STR;
1009         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentNumericString;
1010 
1011           return LDAP_SUCCESS;
1012 }
1013 
1014 
1015 /*
1016  * Free function : OCTET STRING
1017  */
1018 void
FreeComponentOcts(ComponentOcts * v)1019 FreeComponentOcts ( ComponentOcts* v) {
1020           FreeAsnOcts( &v->value );
1021 }
1022 
1023 /*
1024  * Matching function : OCTET STRING
1025  */
1026 int
MatchingComponentOcts(char * oid,ComponentSyntaxInfo * csi_attr,ComponentSyntaxInfo * csi_assert)1027 MatchingComponentOcts ( char* oid, ComponentSyntaxInfo* csi_attr,
1028                               ComponentSyntaxInfo* csi_assert )
1029 {
1030         int rc;
1031         MatchingRule* mr;
1032         ComponentOcts *a, *b;
1033 
1034         if( oid ) {
1035                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
1036                 if ( mr )
1037                         return component_value_match( mr, csi_attr , csi_assert );
1038         }
1039         a = (ComponentOcts*) csi_attr;
1040         b = (ComponentOcts*) csi_assert;
1041           /* Assume that both of OCTET string has end of string character */
1042           if ( (a->value.octetLen == b->value.octetLen) &&
1043                     strncmp ( a->value.octs, b->value.octs, a->value.octetLen ) == 0 )
1044           return LDAP_COMPARE_TRUE;
1045           else
1046                     return LDAP_COMPARE_FALSE;
1047 }
1048 
1049 /*
1050  * GSER Encoder : OCTET STRING
1051  */
1052 int
GEncComponentOcts(GenBuf * b,ComponentOcts * in)1053 GEncComponentOcts ( GenBuf* b, ComponentOcts *in )
1054 {
1055           GAsnOcts t = {0};
1056           if ( !in || in->value.octetLen <= 0 )
1057                     return (-1);
1058 
1059           t.value = in->value;
1060           return GEncAsnOctsContent ( b, &t );
1061 }
1062 
1063 /*
1064  * GSER Decoder : OCTET STRING
1065  */
1066 int
GDecComponentOcts(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)1067 GDecComponentOcts ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
1068 {
1069         char *peek_head, *data;
1070         int i, j, strLen;
1071         void* component_values;
1072         ComponentOcts* k, **k2;
1073           GAsnOcts result;
1074 
1075         k = (ComponentOcts*) v;
1076 
1077         if ( mode & DEC_ALLOC_MODE_0 ) {
1078                 k2 = (ComponentOcts**) v;
1079                 *k2 = (ComponentOcts*) CompAlloc( mem_op, sizeof( ComponentOcts ) );
1080                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1081                 k = *k2;
1082         }
1083 
1084           if ( GDecAsnOctsContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
1085                     if ( k ) CompFree ( mem_op, k );
1086                     return LDAP_DECODING_ERROR;
1087           }
1088           k->value = result.value;
1089 
1090           k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
1091           if ( !k->comp_desc )  {
1092                     if ( k ) CompFree ( mem_op, k );
1093                     return LDAP_DECODING_ERROR;
1094           }
1095           k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentOcts;
1096           k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentOcts;
1097           k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentOcts;
1098           k->comp_desc->cd_free = (comp_free_func*)FreeComponentOcts;
1099           k->comp_desc->cd_extract_i = NULL;
1100           k->comp_desc->cd_type = ASN_BASIC;
1101           k->comp_desc->cd_type_id = BASICTYPE_OCTETSTRING;
1102           k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentOcts;
1103 
1104           return LDAP_SUCCESS;
1105 }
1106 
1107 /*
1108  * Component BER Decoder : OCTET STRING
1109  */
1110 int
BDecComponentOctsTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)1111 BDecComponentOctsTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1112           return BDecComponentOcts ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1113 }
1114 
1115 int
BDecComponentOcts(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)1116 BDecComponentOcts ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
1117                               AsnLen *bytesDecoded, int mode )
1118 {
1119         char *peek_head, *data;
1120         int i, strLen, rc;
1121         void* component_values;
1122         ComponentOcts* k, **k2;
1123           AsnOcts result;
1124 
1125         k = (ComponentOcts*) v;
1126 
1127         if ( mode & DEC_ALLOC_MODE_0 ) {
1128                 k2 = (ComponentOcts**) v;
1129                 *k2 = (ComponentOcts*) CompAlloc( mem_op, sizeof( ComponentOcts ) );
1130                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1131                 k = *k2;
1132         }
1133 
1134           if ( mode & CALL_TAG_DECODER ){
1135                     mode = mode & CALL_CONTENT_DECODER;
1136                     rc = BDecAsnOcts ( mem_op, b, &result, bytesDecoded );
1137           } else {
1138                     rc = BDecAsnOctsContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1139           }
1140           if ( rc < 0 ) {
1141                     if ( k ) CompFree ( mem_op, k );
1142                     return LDAP_DECODING_ERROR;
1143           }
1144           k->value = result;
1145 
1146         k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
1147           if ( !k->comp_desc )  {
1148                     if ( k ) CompFree ( mem_op, k );
1149                     return LDAP_DECODING_ERROR;
1150           }
1151           k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentOcts;
1152         k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentOcts;
1153         k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentOcts;
1154           k->comp_desc->cd_free = (comp_free_func*)FreeComponentOcts;
1155         k->comp_desc->cd_extract_i = NULL;
1156         k->comp_desc->cd_type = ASN_BASIC;
1157         k->comp_desc->cd_type_id = BASICTYPE_OCTETSTRING;
1158         k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentOcts;
1159           return LDAP_SUCCESS;
1160 }
1161 
1162 /*
1163  * Matching function : OBJECT IDENTIFIER
1164  */
1165 int
MatchingComponentOid(char * oid,ComponentSyntaxInfo * csi_attr,ComponentSyntaxInfo * csi_assert)1166 MatchingComponentOid ( char *oid, ComponentSyntaxInfo *csi_attr ,
1167                               ComponentSyntaxInfo *csi_assert )
1168 {
1169         int rc;
1170         MatchingRule* mr;
1171         ComponentOid *a, *b;
1172 
1173         if( oid ) {
1174                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
1175                 if ( mr )
1176                         return component_value_match( mr, csi_attr , csi_assert );
1177         }
1178 
1179         a = (ComponentOid*)csi_attr;
1180         b = (ComponentOid*)csi_assert;
1181           if ( a->value.octetLen != b->value.octetLen )
1182                     return LDAP_COMPARE_FALSE;
1183         rc = ( strncmp( a->value.octs, b->value.octs, a->value.octetLen ) == 0 );
1184 
1185         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
1186 }
1187 
1188 /*
1189  * GSER Encoder : OID
1190  */
GEncComponentOid(GenBuf * b,ComponentOid * in)1191 GEncComponentOid ( GenBuf *b, ComponentOid *in )
1192 {
1193           GAsnOid t = {0};
1194 
1195           if ( !in || in->value.octetLen <= 0 )
1196                     return (-1);
1197           t.value = in->value;
1198           return GEncAsnOidContent( b, (GAsnOcts*)&t );
1199 }
1200 
1201 /*
1202  * GSER Decoder : OID
1203  */
1204 int
GDecAsnDescOidContent(void * mem_op,GenBuf * b,GAsnOid * result,AsnLen * bytesDecoded)1205 GDecAsnDescOidContent ( void* mem_op, GenBuf *b, GAsnOid *result, AsnLen *bytesDecoded ){
1206           AttributeType *ad_type;
1207           struct berval name;
1208           char* peek_head;
1209           int strLen;
1210 
1211           strLen = LocateNextGSERToken ( mem_op, b, &peek_head, GSER_NO_COPY );
1212           name.bv_val = peek_head;
1213           name.bv_len = strLen;
1214 
1215           ad_type = at_bvfind( &name );
1216 
1217           if ( !ad_type )
1218                     return LDAP_DECODING_ERROR;
1219 
1220           peek_head = ad_type->sat_atype.at_oid;
1221           strLen = strlen ( peek_head );
1222 
1223           result->value.octs = (char*)EncodeComponentOid ( mem_op, peek_head , &strLen );
1224           result->value.octetLen = strLen;
1225           return LDAP_SUCCESS;
1226 }
1227 
1228 int
IsNumericOid(char * peek_head,int strLen)1229 IsNumericOid ( char* peek_head , int strLen ) {
1230           int i;
1231           int num_dot;
1232           for ( i = 0, num_dot = 0 ; i < strLen ; i++ ) {
1233                     if ( peek_head[i] == '.' ) num_dot++;
1234                     else if ( peek_head[i] > '9' || peek_head[i] < '0' )
1235                               return (-1);
1236           }
1237           if ( num_dot )
1238                     return (1);
1239           else
1240                     return (-1);
1241 }
1242 
1243 int
GDecComponentOid(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)1244 GDecComponentOid ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
1245 {
1246         char* peek_head;
1247         int i, strLen, rc;
1248         void* component_values;
1249         ComponentOid* k, **k2;
1250           GAsnOid result;
1251 
1252         k = (ComponentOid*) v;
1253 
1254         if ( mode & DEC_ALLOC_MODE_0 ) {
1255                 k2 = (ComponentOid**) v;
1256                 *k2 = (ComponentOid*) CompAlloc( mem_op, sizeof( ComponentOid ) );
1257                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1258                 k = *k2;
1259         }
1260 
1261           strLen = LocateNextGSERToken ( mem_op, b, &peek_head, GSER_PEEK );
1262           if ( IsNumericOid ( peek_head , strLen ) >= 1 ) {
1263                     /* numeric-oid */
1264                     if ( GDecAsnOidContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
1265                               if ( k ) CompFree ( mem_op, k );
1266                               return LDAP_DECODING_ERROR;
1267                     }
1268           }
1269           else {
1270                     /*descr*/
1271                     if ( GDecAsnDescOidContent ( mem_op, b, &result, bytesDecoded ) < 0 ){
1272                               if ( k ) CompFree ( mem_op, k );
1273                               return LDAP_DECODING_ERROR;
1274                     }
1275           }
1276           k->value = result.value;
1277           k->comp_desc = get_component_description (BASICTYPE_OID);
1278 
1279           return LDAP_SUCCESS;
1280 }
1281 
1282 /*
1283  * Component BER Decoder : OID
1284  */
1285 int
BDecComponentOidTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)1286 BDecComponentOidTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1287           return BDecComponentOid ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1288 }
1289 
1290 int
BDecComponentOid(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)1291 BDecComponentOid ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v,
1292                               AsnLen *bytesDecoded, int mode )
1293 {
1294         char* peek_head;
1295         int i, strLen, rc;
1296         void* component_values;
1297         ComponentOid* k, **k2;
1298           AsnOid result;
1299 
1300         k = (ComponentOid*) v;
1301 
1302         if ( mode & DEC_ALLOC_MODE_0 ) {
1303                 k2 = (ComponentOid**) v;
1304                 *k2 = (ComponentOid*) CompAlloc( mem_op, sizeof( ComponentOid ) );
1305                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1306                 k = *k2;
1307         }
1308 
1309           if ( mode & CALL_TAG_DECODER ){
1310                     mode = mode & CALL_CONTENT_DECODER;
1311                     rc = BDecAsnOid ( mem_op, b, &result, bytesDecoded );
1312           } else {
1313                     rc = BDecAsnOidContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1314           }
1315           if ( rc < 0 ) {
1316                     if ( k ) CompFree ( mem_op, k );
1317                     return LDAP_DECODING_ERROR;
1318           }
1319           k->value = result;
1320 
1321           k->comp_desc = get_component_description (BASICTYPE_OID);
1322 
1323           return LDAP_SUCCESS;
1324 }
1325 
1326 /*
1327  * Component BER Decoder : PrintableString
1328  */
1329 
1330 int
BDecComponentPrintableStringTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)1331 BDecComponentPrintableStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
1332 {
1333           return BDecComponentPrintableString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1334 }
1335 
1336 int
BDecComponentPrintableString(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)1337 BDecComponentPrintableString( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1338 {
1339         char* peek_head;
1340         int i, strLen, rc;
1341         void* component_values;
1342         ComponentPrintableString* k, **k2;
1343           AsnOid result;
1344 
1345         k = (ComponentPrintableString*) v;
1346 
1347         if ( mode & DEC_ALLOC_MODE_0 ) {
1348                 k2 = (ComponentPrintableString**) v;
1349                 *k2 = (ComponentPrintableString*) CompAlloc( mem_op, sizeof( ComponentPrintableString ) );
1350                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1351                 k = *k2;
1352         }
1353 
1354           if ( mode & CALL_TAG_DECODER ) {
1355                     mode = mode & CALL_CONTENT_DECODER;
1356                     rc = BDecPrintableString ( mem_op, b, &result, bytesDecoded );
1357           } else {
1358                     rc = BDecPrintableStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1359           }
1360           if ( rc < 0 ) {
1361                     if ( k ) CompFree ( mem_op, k );
1362                     return LDAP_DECODING_ERROR;
1363           }
1364           k->value = result;
1365 
1366           k->comp_desc = get_component_description (BASICTYPE_PRINTABLE_STR);
1367 
1368           return LDAP_SUCCESS;
1369 }
1370 
1371 /*
1372  * Component BER Decoder : TeletexString
1373  */
1374 
1375 int
BDecComponentTeletexStringTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)1376 BDecComponentTeletexStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
1377 {
1378           return BDecComponentTeletexString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1379 }
1380 
1381 int
BDecComponentTeletexString(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)1382 BDecComponentTeletexString( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1383 {
1384         char* peek_head;
1385         int i, strLen, rc;
1386         void* component_values;
1387         ComponentTeletexString* k, **k2;
1388           AsnOid result;
1389 
1390         k = (ComponentTeletexString*) v;
1391 
1392         if ( mode & DEC_ALLOC_MODE_0 ) {
1393                 k2 = (ComponentTeletexString**) v;
1394                 *k2 = (ComponentTeletexString*) CompAlloc( mem_op, sizeof( ComponentTeletexString ) );
1395                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1396                 k = *k2;
1397         }
1398 
1399           if ( mode & CALL_TAG_DECODER ) {
1400                     mode = mode & CALL_CONTENT_DECODER;
1401                     rc = BDecTeletexString ( mem_op, b, &result, bytesDecoded );
1402           } else {
1403                     rc = BDecTeletexStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1404           }
1405           if ( rc < 0 ) {
1406                     if ( k ) CompFree ( mem_op, k );
1407                     return LDAP_DECODING_ERROR;
1408           }
1409           k->value = result;
1410 
1411           k->comp_desc = get_component_description (BASICTYPE_T61_STR);
1412 
1413           return LDAP_SUCCESS;
1414 }
1415 
1416 
1417 /*
1418  * Matching function : Real
1419  */
1420 int
MatchingComponentReal(char * oid,ComponentSyntaxInfo * csi_attr,ComponentSyntaxInfo * csi_assert)1421 MatchingComponentReal (char* oid, ComponentSyntaxInfo *csi_attr,
1422                               ComponentSyntaxInfo *csi_assert )
1423 {
1424         int rc;
1425         MatchingRule* mr;
1426         ComponentReal *a, *b;
1427 
1428         if( oid ) {
1429                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
1430                 if ( mr )
1431                         return component_value_match( mr, csi_attr , csi_assert );
1432         }
1433         a = (ComponentReal*)csi_attr;
1434         b = (ComponentReal*)csi_assert;
1435         rc = (a->value == b->value);
1436 
1437         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
1438 }
1439 
1440 /*
1441  * GSER Encoder : Real
1442  */
1443 int
GEncComponentReal(GenBuf * b,ComponentReal * in)1444 GEncComponentReal ( GenBuf *b, ComponentReal *in )
1445 {
1446           GAsnReal t = {0};
1447           if ( !in )
1448                     return (-1);
1449           t.value = in->value;
1450           return GEncAsnRealContent ( b, &t );
1451 }
1452 
1453 /*
1454  * GSER Decoder : Real
1455  */
1456 int
GDecComponentReal(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)1457 GDecComponentReal ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
1458 {
1459         char* peek_head;
1460         int i, strLen;
1461         void* component_values;
1462         ComponentReal* k, **k2;
1463           GAsnReal result;
1464 
1465         k = (ComponentReal*) v;
1466 
1467         if ( mode & DEC_ALLOC_MODE_0 ) {
1468                 k2 = (ComponentReal**) v;
1469                 *k2 = (ComponentReal*) CompAlloc( mem_op, sizeof( ComponentReal ) );
1470                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1471                 k = *k2;
1472         }
1473 
1474           if ( GDecAsnRealContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
1475                     if ( k ) CompFree ( mem_op, k );
1476                     return LDAP_DECODING_ERROR;
1477           }
1478           k->value = result.value;
1479           k->comp_desc = get_component_description (BASICTYPE_REAL);
1480 
1481         return LDAP_SUCCESS;
1482 }
1483 
1484 /*
1485  * Component BER Decoder : Real
1486  */
1487 int
BDecComponentRealTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)1488 BDecComponentRealTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1489           return BDecComponentReal ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1490 }
1491 
1492 int
BDecComponentReal(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)1493 BDecComponentReal ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1494 {
1495         char* peek_head;
1496         int i, strLen, rc;
1497         void* component_values;
1498         ComponentReal* k, **k2;
1499           AsnReal result;
1500 
1501         k = (ComponentReal*) v;
1502 
1503         if ( mode & DEC_ALLOC_MODE_0 ) {
1504                 k2 = (ComponentReal**) v;
1505                 *k2 = (ComponentReal*) CompAlloc( mem_op, sizeof( ComponentReal ) );
1506                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1507                 k = *k2;
1508         }
1509 
1510           if ( mode & CALL_TAG_DECODER ){
1511                     mode = mode & CALL_CONTENT_DECODER;
1512                     rc = BDecAsnReal ( mem_op, b, &result, bytesDecoded );
1513           } else {
1514                     rc = BDecAsnRealContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1515           }
1516           if ( rc < 0 ) {
1517                     if ( k ) CompFree ( mem_op, k );
1518                     return LDAP_DECODING_ERROR;
1519           }
1520           k->value = result;
1521           k->comp_desc = get_component_description (BASICTYPE_REAL);
1522 
1523         return LDAP_SUCCESS;
1524 }
1525 
1526 /*
1527  * Matching function : Relative OID
1528  */
1529 int
MatchingComponentRelativeOid(char * oid,ComponentSyntaxInfo * csi_attr,ComponentSyntaxInfo * csi_assert)1530 MatchingComponentRelativeOid ( char* oid, ComponentSyntaxInfo *csi_attr,
1531                                                   ComponentSyntaxInfo *csi_assert )
1532 {
1533         int rc;
1534         MatchingRule* mr;
1535         ComponentRelativeOid *a, *b;
1536 
1537         if( oid ) {
1538                 mr = retrieve_matching_rule(oid, csi_attr->csi_comp_desc->cd_type_id );
1539                 if ( mr )
1540                         return component_value_match( mr, csi_attr , csi_assert );
1541         }
1542 
1543         a = (ComponentRelativeOid*)csi_attr;
1544         b = (ComponentRelativeOid*)csi_assert;
1545 
1546           if ( a->value.octetLen != b->value.octetLen )
1547                     return LDAP_COMPARE_FALSE;
1548         rc = ( strncmp( a->value.octs, b->value.octs, a->value.octetLen ) == 0 );
1549 
1550         return rc ? LDAP_COMPARE_TRUE:LDAP_COMPARE_FALSE;
1551 }
1552 
1553 /*
1554  * GSER Encoder : RELATIVE_OID.
1555  */
1556 int
GEncComponentRelativeOid(GenBuf * b,ComponentRelativeOid * in)1557 GEncComponentRelativeOid ( GenBuf *b, ComponentRelativeOid *in )
1558 {
1559           GAsnRelativeOid t = {0};
1560 
1561           if ( !in || in->value.octetLen <= 0 )
1562                     return (-1);
1563           t.value = in->value;
1564           return GEncAsnRelativeOidContent ( b , (GAsnOcts*)&t );
1565 }
1566 
1567 /*
1568  * GSER Decoder : RELATIVE_OID.
1569  */
1570 int
GDecComponentRelativeOid(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)1571 GDecComponentRelativeOid ( void* mem_op, GenBuf *b,void *v, AsnLen *bytesDecoded, int mode )
1572 {
1573         char* peek_head;
1574         int i, strLen;
1575         void* component_values;
1576         ComponentRelativeOid* k, **k2;
1577           GAsnRelativeOid result;
1578 
1579         k = (ComponentRelativeOid*) v;
1580 
1581         if ( mode & DEC_ALLOC_MODE_0 ) {
1582                 k2 = (ComponentRelativeOid**) v;
1583                 *k2 = (ComponentRelativeOid*) CompAlloc( mem_op, sizeof( ComponentRelativeOid ) );
1584                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1585                 k = *k2;
1586         }
1587 
1588           if ( GDecAsnRelativeOidContent ( mem_op, b, &result, bytesDecoded ) < 0 ) {
1589                     if ( k ) CompFree ( mem_op, k );
1590                     return LDAP_DECODING_ERROR;
1591           }
1592           k->value = result.value;
1593           k->comp_desc = get_component_description (BASICTYPE_OID);
1594 
1595           return LDAP_SUCCESS;
1596 }
1597 
1598 /*
1599  * Component BER Decoder : RELATIVE_OID.
1600  */
1601 int
BDecComponentRelativeOidTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)1602 BDecComponentRelativeOidTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1603           return BDecComponentRelativeOid ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1604 }
1605 
1606 int
BDecComponentRelativeOid(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)1607 BDecComponentRelativeOid ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1608 {
1609         char* peek_head;
1610         int i, strLen, rc;
1611         void* component_values;
1612         ComponentRelativeOid* k, **k2;
1613           AsnRelativeOid result;
1614 
1615         k = (ComponentRelativeOid*) v;
1616 
1617         if ( mode & DEC_ALLOC_MODE_0 ) {
1618                 k2 = (ComponentRelativeOid**) v;
1619                 *k2 = (ComponentRelativeOid*) CompAlloc( mem_op, sizeof( ComponentRelativeOid ) );
1620                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1621                 k = *k2;
1622         }
1623 
1624           if ( mode & CALL_TAG_DECODER ){
1625                     mode = mode & CALL_CONTENT_DECODER;
1626                     rc = BDecAsnRelativeOid ( mem_op, b, &result, bytesDecoded );
1627           } else {
1628                     rc = BDecAsnRelativeOidContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1629           }
1630           if ( rc < 0 ) {
1631                     if ( k ) CompFree ( mem_op, k );
1632                     return LDAP_DECODING_ERROR;
1633           }
1634           k->value = result;
1635           k->comp_desc = get_component_description (BASICTYPE_OID);
1636 
1637           return LDAP_SUCCESS;
1638 }
1639 
1640 /*
1641  * GSER Encoder : UniversalString
1642  */
1643 int
GEncComponentUniversalString(GenBuf * b,ComponentUniversalString * in)1644 GEncComponentUniversalString ( GenBuf *b, ComponentUniversalString *in )
1645 {
1646           GUniversalString t = {0};
1647           if ( !in || in->value.octetLen <= 0 )
1648                     return (-1);
1649           t.value = in->value;
1650           return GEncUniversalStringContent( b, &t );
1651 }
1652 
1653 /*
1654  * GSER Decoder : UniversalString
1655  */
1656 static int
UTF8toUniversalString(char * octs,int len)1657 UTF8toUniversalString( char* octs, int len){
1658           /* Need to be Implemented */
1659           return LDAP_SUCCESS;
1660 }
1661 
1662 int
GDecComponentUniversalString(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)1663 GDecComponentUniversalString ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode )
1664 {
1665           if ( GDecComponentUTF8String ( mem_op, b, v, bytesDecoded, mode) < 0 )
1666           UTF8toUniversalString( ((ComponentUniversalString*)v)->value.octs, ((ComponentUniversalString*)v)->value.octetLen );
1667                     return LDAP_DECODING_ERROR;
1668 }
1669 
1670 /*
1671  * Component BER Decoder : UniverseString
1672  */
1673 int
BDecComponentUniversalStringTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)1674 BDecComponentUniversalStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1675           return BDecComponentUniversalString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1676 }
1677 
1678 int
BDecComponentUniversalString(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)1679 BDecComponentUniversalString ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1680 {
1681         char* peek_head;
1682         int i, strLen, rc;
1683         void* component_values;
1684         ComponentUniversalString* k, **k2;
1685           UniversalString result;
1686 
1687         k = (ComponentUniversalString*) v;
1688 
1689         if ( mode & DEC_ALLOC_MODE_0 ) {
1690                 k2 = (ComponentUniversalString**) v;
1691                 *k2 = (ComponentUniversalString*) CompAlloc( mem_op, sizeof( ComponentUniversalString ) );
1692                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1693                 k = *k2;
1694         }
1695 
1696           if ( mode & CALL_TAG_DECODER ){
1697                     mode = mode & CALL_CONTENT_DECODER;
1698                     rc = BDecUniversalString ( mem_op, b, &result, bytesDecoded );
1699           } else {
1700                     rc = BDecUniversalStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1701           }
1702           if ( rc < 0 ) {
1703                     if ( k ) CompFree ( mem_op, k );
1704                     return LDAP_DECODING_ERROR;
1705           }
1706           k->value = result;
1707           k->comp_desc = get_component_description (BASICTYPE_UNIVERSAL_STR);
1708 
1709           return LDAP_SUCCESS;
1710 }
1711 
1712 /*
1713  * Component BER Decoder : VisibleString
1714  */
1715 int
BDecComponentVisibleStringTag(void * mem_op,GenBuf * b,void * v,AsnLen * bytesDecoded,int mode)1716 BDecComponentVisibleStringTag ( void* mem_op, GenBuf *b, void *v, AsnLen *bytesDecoded, int mode ) {
1717           return BDecComponentVisibleString ( mem_op, b, 0, 0, v, bytesDecoded, mode|CALL_TAG_DECODER );
1718 }
1719 
1720 int
BDecComponentVisibleString(void * mem_op,GenBuf * b,AsnTag tagId,AsnLen len,void * v,AsnLen * bytesDecoded,int mode)1721 BDecComponentVisibleString ( void* mem_op, GenBuf *b, AsnTag tagId, AsnLen len, void *v, AsnLen *bytesDecoded, int mode )
1722 {
1723         char* peek_head;
1724         int i, strLen, rc;
1725         void* component_values;
1726         ComponentVisibleString* k, **k2;
1727           VisibleString result;
1728 
1729         k = (ComponentVisibleString*) v;
1730 
1731         if ( mode & DEC_ALLOC_MODE_0 ) {
1732                 k2 = (ComponentVisibleString**) v;
1733                 *k2 = (ComponentVisibleString*) CompAlloc( mem_op, sizeof( ComponentVisibleString ) );
1734                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1735                 k = *k2;
1736         }
1737 
1738           if ( mode & CALL_TAG_DECODER ){
1739                     mode = mode & CALL_CONTENT_DECODER;
1740                     rc = BDecVisibleString ( mem_op, b, &result, bytesDecoded );
1741           } else {
1742                     rc = BDecVisibleStringContent ( mem_op, b, tagId, len, &result, bytesDecoded );
1743           }
1744           k->value = result;
1745           k->comp_desc = get_component_description (BASICTYPE_VISIBLE_STR);
1746 
1747           return LDAP_SUCCESS;
1748 }
1749 
1750 /*
1751  * Routines for handling an ANY DEFINED Type
1752  */
1753 
1754 /* Check if the <select> type CR and the OID of the given ANY type */
1755 int
CheckSelectTypeCorrect(void * mem_op,ComponentAnyInfo * cai,struct berval * select)1756 CheckSelectTypeCorrect ( void* mem_op, ComponentAnyInfo* cai, struct berval* select ) {
1757           int strLen;
1758           AttributeType* ad_type;
1759           char* oid;
1760           char* result;
1761 
1762           if ( IsNumericOid ( select->bv_val , select->bv_len ) ) {
1763                     oid = select->bv_val;
1764                     strLen = select->bv_len;
1765           } else {
1766                     ad_type = at_bvfind( select );
1767 
1768                     if ( !ad_type )
1769                               return LDAP_DECODING_ERROR;
1770 
1771                     oid = ad_type->sat_atype.at_oid;
1772                     strLen = strlen ( oid );
1773           }
1774           result = EncodeComponentOid ( mem_op, oid , &strLen );
1775           if ( !result || strLen <= 0 ) return (-1);
1776 
1777           if ( cai->oid.octetLen == strLen &&
1778                     strncmp ( cai->oid.octs, result, strLen ) == 0 )
1779                     return (1);
1780           else
1781                     return (-1);
1782 }
1783 
1784 int
SetAnyTypeByComponentOid(ComponentAny * v,ComponentOid * id)1785 SetAnyTypeByComponentOid ( ComponentAny *v, ComponentOid *id ) {
1786           Hash hash;
1787           void *anyInfo;
1788 
1789           /* use encoded oid as hash string */
1790           hash = MakeHash (id->value.octs, id->value.octetLen);
1791           if (CheckForAndReturnValue (anyOidHashTblG, hash, &anyInfo))
1792                     v->cai = (ComponentAnyInfo*) anyInfo;
1793           else
1794                     v->cai = NULL;
1795 
1796           if ( !v->cai ) {
1797           /*
1798            * If not found, the data considered as octet chunk
1799            * Yet-to-be-Implemented
1800            */
1801           }
1802           return LDAP_SUCCESS;
1803 }
1804 
1805 void
SetAnyTypeByComponentInt(ComponentAny * v,ComponentInt id)1806 SetAnyTypeByComponentInt( ComponentAny *v, ComponentInt id) {
1807           Hash hash;
1808           void *anyInfo;
1809 
1810           hash = MakeHash ((char*)&id, sizeof (id));
1811           if (CheckForAndReturnValue (anyIntHashTblG, hash, &anyInfo))
1812                     v->cai = (ComponentAnyInfo*) anyInfo;
1813           else
1814                     v->cai = NULL;
1815 }
1816 
1817 int
GEncComponentAny(GenBuf * b,ComponentAny * in)1818 GEncComponentAny ( GenBuf *b, ComponentAny *in )
1819 {
1820           if ( in->cai != NULL  && in->cai->Encode != NULL )
1821                     return in->cai->Encode(b, &in->value );
1822           else
1823                     return (-1);
1824 }
1825 
1826 int
BEncComponentAny(void * mem_op,GenBuf * b,ComponentAny * result,AsnLen * bytesDecoded,int mode)1827 BEncComponentAny ( void* mem_op, GenBuf *b, ComponentAny *result, AsnLen *bytesDecoded, int mode)
1828 {
1829         ComponentAny *k, **k2;
1830 
1831         k = (ComponentAny*) result;
1832 
1833           if ( !k ) return (-1);
1834 
1835         if ( mode & DEC_ALLOC_MODE_0 ) {
1836                 k2 = (ComponentAny**) result;
1837                 *k2 = (ComponentAny*) CompAlloc( mem_op, sizeof( ComponentAny ) );
1838                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1839                 k = *k2;
1840         }
1841 
1842           if ((result->cai != NULL) && (result->cai->BER_Decode != NULL)) {
1843                     result->value = (void*) CompAlloc ( mem_op, result->cai->size );
1844                     if ( !result->value ) return 0;
1845                     result->cai->BER_Decode ( mem_op, b, result->value, (int*)bytesDecoded, DEC_ALLOC_MODE_1);
1846 
1847                     k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
1848                     if ( !k->comp_desc )  {
1849                               if ( k ) CompFree ( mem_op, k );
1850                               return LDAP_DECODING_ERROR;
1851                     }
1852                     k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentAny;
1853                     k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentAny;
1854                     k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentAny;
1855                     k->comp_desc->cd_free = (comp_free_func*)FreeComponentAny;
1856                     k->comp_desc->cd_extract_i = NULL;
1857                     k->comp_desc->cd_type = ASN_BASIC;
1858                     k->comp_desc->cd_type_id = BASICTYPE_ANY;
1859                     k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentAny;
1860                     return LDAP_SUCCESS;
1861           }
1862           else {
1863                     Asn1Error ("ERROR - Component ANY Decode routine is NULL\n");
1864                     return 0;
1865           }
1866 }
1867 
1868 int
BDecComponentAny(void * mem_op,GenBuf * b,ComponentAny * result,AsnLen * bytesDecoded,int mode)1869 BDecComponentAny ( void* mem_op, GenBuf *b, ComponentAny *result, AsnLen *bytesDecoded, int mode) {
1870           int rc;
1871         ComponentAny *k, **k2;
1872 
1873         k = (ComponentAny*) result;
1874 
1875           if ( !k ) return (-1);
1876 
1877         if ( mode & DEC_ALLOC_MODE_0 ) {
1878                 k2 = (ComponentAny**) result;
1879                 *k2 = (ComponentAny*) CompAlloc( mem_op, sizeof( ComponentAny ) );
1880                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1881                 k = *k2;
1882         }
1883 
1884           if ((result->cai != NULL) && (result->cai->BER_Decode != NULL)) {
1885                     result->cai->BER_Decode ( mem_op, b, (ComponentSyntaxInfo*)&result->value, (int*)bytesDecoded, DEC_ALLOC_MODE_0 );
1886 
1887                     k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
1888                     if ( !k->comp_desc )  {
1889                               if ( k ) CompFree ( mem_op, k );
1890                               return LDAP_DECODING_ERROR;
1891                     }
1892                     k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentAny;
1893                     k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentAny;
1894                     k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentAny;
1895                     k->comp_desc->cd_free = (comp_free_func*)FreeComponentAny;
1896                     k->comp_desc->cd_extract_i = NULL;
1897                     k->comp_desc->cd_type = ASN_BASIC;
1898                     k->comp_desc->cd_type_id = BASICTYPE_ANY;
1899                     k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentAny;
1900                     return LDAP_SUCCESS;
1901           }
1902           else {
1903                     Asn1Error ("ERROR - Component ANY Decode routine is NULL\n");
1904                     return 0;
1905           }
1906 }
1907 
1908 int
GDecComponentAny(void * mem_op,GenBuf * b,ComponentAny * result,AsnLen * bytesDecoded,int mode)1909 GDecComponentAny ( void* mem_op, GenBuf *b, ComponentAny *result, AsnLen *bytesDecoded, int mode) {
1910         ComponentAny *k, **k2;
1911 
1912         k = (ComponentAny*) result;
1913 
1914         if ( mode & DEC_ALLOC_MODE_0 ) {
1915                 k2 = (ComponentAny**) result;
1916                 *k2 = (ComponentAny*) CompAlloc( mem_op, sizeof( ComponentAny ) );
1917                     if ( !*k2 ) return LDAP_DECODING_ERROR;
1918                 k = *k2;
1919         }
1920           if ((result->cai != NULL) && (result->cai->GSER_Decode != NULL)) {
1921                     result->value = (void*) CompAlloc ( mem_op, result->cai->size );
1922                     if ( !result->value ) return 0;
1923                     result->cai->GSER_Decode ( mem_op, b, result->value, (int*)bytesDecoded, DEC_ALLOC_MODE_1);
1924                     k->comp_desc = CompAlloc( mem_op, sizeof( ComponentDesc ) );
1925                     if ( !k->comp_desc )  {
1926                               if ( k ) CompFree ( mem_op, k );
1927                               return LDAP_DECODING_ERROR;
1928                     }
1929                     k->comp_desc->cd_gser_encoder = (encoder_func*)GEncComponentAny;
1930                     k->comp_desc->cd_gser_decoder = (gser_decoder_func*)GDecComponentAny;
1931                     k->comp_desc->cd_ber_decoder = (ber_decoder_func*)BDecComponentAny;
1932                     k->comp_desc->cd_free = (comp_free_func*)FreeComponentAny;
1933                     k->comp_desc->cd_type = ASN_BASIC;
1934                     k->comp_desc->cd_extract_i = NULL;
1935                     k->comp_desc->cd_type_id = BASICTYPE_ANY;
1936                     k->comp_desc->cd_all_match = (allcomponent_matching_func*)MatchingComponentAny;
1937                     return LDAP_SUCCESS;
1938           }
1939           else {
1940                     Asn1Error ("ERROR - ANY Decode routine is NULL\n");
1941                     return 0;
1942           }
1943 }
1944 
1945 int
MatchingComponentAny(char * oid,ComponentAny * result,ComponentAny * result2)1946 MatchingComponentAny (char* oid, ComponentAny *result, ComponentAny *result2) {
1947           void *comp1, *comp2;
1948 
1949           if ( result->comp_desc->cd_type_id == BASICTYPE_ANY )
1950                     comp1 = result->value;
1951           else
1952                     comp1 = result;
1953 
1954           if ( result2->comp_desc->cd_type_id == BASICTYPE_ANY )
1955                     comp2 = result2->value;
1956           else
1957                     comp2 = result2;
1958 
1959           if ((result->cai != NULL) && (result->cai->Match != NULL)) {
1960                     if ( result->comp_desc->cd_type_id == BASICTYPE_ANY )
1961                               return result->cai->Match(oid, comp1, comp2 );
1962                     else if ( result2->comp_desc->cd_type_id == BASICTYPE_ANY )
1963                               return result2->cai->Match(oid, comp1, comp2);
1964                     else
1965                               return LDAP_INVALID_SYNTAX;
1966           }
1967           else {
1968                     Asn1Error ("ERROR - ANY Matching routine is NULL\n");
1969                     return LDAP_INVALID_SYNTAX;
1970           }
1971 }
1972 
1973 void*
ExtractingComponentAny(void * mem_op,ComponentReference * cr,ComponentAny * result)1974 ExtractingComponentAny ( void* mem_op, ComponentReference* cr,  ComponentAny *result ) {
1975           if ((result->cai != NULL) && (result->cai->Extract != NULL)) {
1976                     return (void*) result->cai->Extract( mem_op, cr , result->value );
1977           }
1978           else {
1979                     Asn1Error ("ERROR - ANY Extracting routine is NULL\n");
1980                     return (void*)NULL;
1981           }
1982 }
1983 
1984 void
FreeComponentAny(ComponentAny * any)1985 FreeComponentAny (ComponentAny* any) {
1986           if ( any->cai != NULL && any->cai->Free != NULL ) {
1987                     any->cai->Free( any->value );
1988                     free ( ((ComponentSyntaxInfo*)any->value)->csi_comp_desc );
1989                     free ( any->value );
1990           }
1991           else
1992                     Asn1Error ("ERROR - ANY Free routine is NULL\n");
1993 }
1994 
1995 void
InstallAnyByComponentInt(int anyId,ComponentInt intId,unsigned int size,EncodeFcn encode,gser_decoder_func * G_decode,ber_tag_decoder_func * B_decode,ExtractFcn extract,MatchFcn match,FreeFcn free,PrintFcn print)1996 InstallAnyByComponentInt (int anyId, ComponentInt intId, unsigned int size,
1997                               EncodeFcn encode, gser_decoder_func* G_decode,
1998                               ber_tag_decoder_func* B_decode, ExtractFcn extract,
1999                               MatchFcn match, FreeFcn free,
2000                               PrintFcn print)
2001 {
2002           ComponentAnyInfo *a;
2003           Hash h;
2004 
2005           a = (ComponentAnyInfo*) malloc(sizeof (ComponentAnyInfo));
2006           a->anyId = anyId;
2007           a->oid.octs = NULL;
2008           a->oid.octetLen = 0;
2009           a->intId = intId;
2010           a->size = size;
2011           a->Encode = encode;
2012           a->GSER_Decode = G_decode;
2013           a->BER_Decode = B_decode;
2014           a->Match = match;
2015           a->Extract = extract;
2016           a->Free = free;
2017           a->Print = print;
2018 
2019           if (anyIntHashTblG == NULL)
2020                     anyIntHashTblG = InitHash();
2021 
2022           h = MakeHash ((char*)&intId, sizeof (intId));
2023 
2024           if(anyIntHashTblG != NULL)
2025                     Insert(anyIntHashTblG, a, h);
2026 }
2027 
2028 
2029 /*
2030  * OID and its corresponding decoder can be registered with this func.
2031  * If contained types constrained by <select> are used,
2032  * their OID and decoder MUST be registered, otherwise it will return no entry.
2033  * An open type(ANY type) also need be registered.
2034  */
2035 void
InstallOidDecoderMapping(char * ch_oid,EncodeFcn encode,gser_decoder_func * G_decode,ber_tag_decoder_func * B_decode,ExtractFcn extract,MatchFcn match)2036 InstallOidDecoderMapping ( char* ch_oid, EncodeFcn encode, gser_decoder_func* G_decode, ber_tag_decoder_func* B_decode, ExtractFcn extract, MatchFcn match ) {
2037           AsnOid oid;
2038           int strLen;
2039           void* mem_op;
2040 
2041           strLen = strlen( ch_oid );
2042           if( strLen <= 0 ) return;
2043           mem_op = comp_nibble_memory_allocator ( 128, 16 );
2044           oid.octs = EncodeComponentOid ( mem_op, ch_oid, &strLen );
2045           oid.octetLen = strLen;
2046           if( strLen <= 0 ) return;
2047 
2048 
2049           InstallAnyByComponentOid ( 0, &oid, 0, encode, G_decode, B_decode,
2050                                                             extract, match, NULL, NULL);
2051           comp_nibble_memory_free(mem_op);
2052 }
2053 
2054 /*
2055  * Look up Oid-decoder mapping table by berval have either
2056  * oid or description
2057  */
2058 OidDecoderMapping*
RetrieveOidDecoderMappingbyBV(struct berval * in)2059 RetrieveOidDecoderMappingbyBV( struct berval* in ) {
2060           if ( IsNumericOid ( in->bv_val, in->bv_len ) )
2061                     return RetrieveOidDecoderMappingbyOid( in->bv_val, in->bv_len );
2062           else
2063                     return RetrieveOidDecoderMappingbyDesc( in->bv_val, in->bv_len );
2064 }
2065 
2066 /*
2067  * Look up Oid-decoder mapping table by dotted OID
2068  */
2069 OidDecoderMapping*
RetrieveOidDecoderMappingbyOid(char * ch_oid,int oid_len)2070 RetrieveOidDecoderMappingbyOid( char* ch_oid, int oid_len ) {
2071           Hash hash;
2072           void *anyInfo;
2073           AsnOid oid;
2074           int strLen;
2075           void* mem_op;
2076 
2077           mem_op = comp_nibble_memory_allocator ( 128, 16 );
2078           oid.octs = EncodeComponentOid ( mem_op, ch_oid, &oid_len);
2079           oid.octetLen = oid_len;
2080           if( oid_len <= 0 ) {
2081                     comp_nibble_memory_free( mem_op );
2082                     return NULL;
2083           }
2084 
2085           /* use encoded oid as hash string */
2086           hash = MakeHash ( oid.octs, oid.octetLen);
2087           comp_nibble_memory_free( mem_op );
2088           if (CheckForAndReturnValue (anyOidHashTblG, hash, &anyInfo))
2089                     return (OidDecoderMapping*) anyInfo;
2090           else
2091                     return (OidDecoderMapping*) NULL;
2092 
2093 }
2094 
2095 /*
2096  * Look up Oid-decoder mapping table by description
2097  */
2098 OidDecoderMapping*
RetrieveOidDecoderMappingbyDesc(char * desc,int desc_len)2099 RetrieveOidDecoderMappingbyDesc( char* desc, int desc_len ) {
2100           Hash hash;
2101           void *anyInfo;
2102           AsnOid oid;
2103           AttributeType* ad_type;
2104           struct berval bv;
2105           void* mem_op;
2106 
2107           bv.bv_val = desc;
2108           bv.bv_len = desc_len;
2109           ad_type = at_bvfind( &bv );
2110 
2111           oid.octs = ad_type->sat_atype.at_oid;
2112           oid.octetLen = strlen ( oid.octs );
2113 
2114           if ( !ad_type )
2115                     return (OidDecoderMapping*) NULL;
2116 
2117           mem_op = comp_nibble_memory_allocator ( 128, 16 );
2118 
2119           oid.octs = EncodeComponentOid ( mem_op, oid.octs , (int*)&oid.octetLen );
2120           if( oid.octetLen <= 0 ) {
2121                     comp_nibble_memory_free( mem_op );
2122                     return (OidDecoderMapping*) NULL;
2123           }
2124 
2125           /* use encoded oid as hash string */
2126           hash = MakeHash ( oid.octs, oid.octetLen);
2127           comp_nibble_memory_free( mem_op );
2128           if (CheckForAndReturnValue (anyOidHashTblG, hash, &anyInfo))
2129                     return (OidDecoderMapping*) anyInfo;
2130           else
2131                     return (OidDecoderMapping*) NULL;
2132 
2133 }
2134 void
InstallAnyByComponentOid(int anyId,AsnOid * oid,unsigned int size,EncodeFcn encode,gser_decoder_func * G_decode,ber_tag_decoder_func * B_decode,ExtractFcn extract,MatchFcn match,FreeFcn free,PrintFcn print)2135 InstallAnyByComponentOid (int anyId, AsnOid *oid, unsigned int size,
2136                               EncodeFcn encode, gser_decoder_func* G_decode,
2137                               ber_tag_decoder_func* B_decode, ExtractFcn extract,
2138                                MatchFcn match, FreeFcn free, PrintFcn print)
2139 {
2140           ComponentAnyInfo *a;
2141           Hash h;
2142 
2143           a = (ComponentAnyInfo*) malloc (sizeof (ComponentAnyInfo));
2144           a->anyId = anyId;
2145           if ( oid ) {
2146                     a->oid.octs = malloc( oid->octetLen );
2147                     memcpy ( a->oid.octs, oid->octs, oid->octetLen );
2148                     a->oid.octetLen = oid->octetLen;
2149           }
2150           a->size = size;
2151           a->Encode = encode;
2152           a->GSER_Decode = G_decode;
2153           a->BER_Decode = B_decode;
2154           a->Match = match;
2155           a->Extract = extract;
2156           a->Free = free;
2157           a->Print = print;
2158 
2159           h = MakeHash (oid->octs, oid->octetLen);
2160 
2161           if (anyOidHashTblG == NULL)
2162                     anyOidHashTblG = InitHash();
2163 
2164           if(anyOidHashTblG != NULL)
2165                     Insert(anyOidHashTblG, a, h);
2166 }
2167 
2168 int
BDecComponentTop(ber_decoder_func * decoder _AND_ void * mem_op _AND_ GenBuf * b _AND_ AsnTag tag _AND_ AsnLen elmtLen _AND_ void ** v _AND_ AsnLen * bytesDecoded _AND_ int mode)2169 BDecComponentTop  (
2170 ber_decoder_func *decoder _AND_
2171 void* mem_op _AND_
2172 GenBuf *b _AND_
2173 AsnTag tag _AND_
2174 AsnLen elmtLen _AND_
2175 void **v _AND_
2176 AsnLen *bytesDecoded _AND_
2177 int mode) {
2178           tag = BDecTag ( b, bytesDecoded );
2179           elmtLen = BDecLen ( b, bytesDecoded );
2180           if ( elmtLen <= 0 ) return (-1);
2181           if ( tag != MAKE_TAG_ID (UNIV, CONS, SEQ_TAG_CODE) ) {
2182                     return (-1);
2183           }
2184 
2185           return (*decoder)( mem_op, b, tag, elmtLen, (ComponentSyntaxInfo*)v,(int*)bytesDecoded, mode );
2186 }
2187 
2188 /*
2189  * ASN.1 specification of a distinguished name
2190  * DistinguishedName ::= RDNSequence
2191  * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
2192  * RelativeDistinguishedName ::= SET SIZE(1..MAX) OF AttributeTypeandValue
2193  * AttributeTypeandValue ::= SEQUENCE {
2194  *        type      AttributeType
2195  *        value     AttributeValue
2196  * }
2197  * When dnMatch/rdnMatch is used in a component assertion value
2198  * the component in DistinguishedName/RelativeDistinguishedName
2199  * need to be converted to the LDAP encodings in RFC2253
2200  * in order to be matched against the assertion value
2201  * If allComponentMatch is used, the assertion value may be
2202  * decoded into the Internal Representation(Component Tree)
2203  * by the corresponding GSER or BER decoder
2204  * Following routine converts a component tree(DistinguishedName) into
2205  * LDAP encodings in RFC2253
2206  * Example)
2207  * IR : ComponentRDNSequence
2208  * GSER : { { type cn, value sang },{ type o, value ibm}, {type c, value us} }
2209  * LDAP Encodings : cn=sang,o=ibm,c=us
2210  */
2211 
increment_bv_mem_by_size(struct berval * in,int size)2212 increment_bv_mem_by_size ( struct berval* in, int size ) {
2213           int new_size = in->bv_len + size;
2214           in->bv_val = realloc( in->bv_val, new_size );
2215           in->bv_len = new_size;
2216 }
2217 
2218 int
ConvertBER2Desc(char * in,int size,struct berval * out,int * pos)2219 ConvertBER2Desc( char* in, int size, struct berval* out, int* pos ) {
2220           int desc_size;
2221           char* desc_ptr;
2222           unsigned int firstArcNum;
2223           unsigned int arcNum;
2224           int i, rc, start_pos = *pos;
2225           char buf[MAX_OID_LEN];
2226           AttributeType *at;
2227           struct berval bv_name;
2228 
2229           /*convert BER oid to desc*/
2230           for ( i = 0, arcNum = 0; (i < size) && (in[i] & 0x80 ); i++ )
2231                     arcNum = (arcNum << 7) + (in[i] & 0x7f);
2232           arcNum = (arcNum << 7) + (in[i] & 0x7f);
2233           i++;
2234           firstArcNum = (unsigned short)(arcNum/40);
2235           if ( firstArcNum > 2 )
2236                     firstArcNum = 2;
2237 
2238           arcNum = arcNum - (firstArcNum * 40 );
2239 
2240           rc = intToAscii ( arcNum, buf );
2241 
2242           /*check if the buffer can store the first/second arc and two dots*/
2243           if ( out->bv_len < *pos + 2 + 1 + rc )
2244                     increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2245 
2246           if ( firstArcNum == 1)
2247                     out->bv_val[*pos] = '1';
2248           else
2249                     out->bv_val[*pos] = '2';
2250           (*pos)++;
2251           out->bv_val[*pos] = '.';
2252           (*pos)++;
2253 
2254           memcpy( out->bv_val + *pos, buf, rc );
2255           *pos += rc;
2256           out->bv_val[*pos] = '.';
2257           (*pos)++;
2258 
2259           for ( ; i < size ; ) {
2260                     for ( arcNum=0; (i < size) && (in[i] & 0x80) ; i++ )
2261                               arcNum = (arcNum << 7) + (in[i] & 0x7f);
2262                     arcNum = (arcNum << 7) + (in[i] & 0x7f);
2263                     i++;
2264 
2265                     rc = intToAscii ( arcNum, buf );
2266 
2267                     if ( out->bv_len < *pos + rc + 1 )
2268                               increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2269 
2270                     memcpy( out->bv_val + *pos, buf, rc );
2271                     *pos += rc;
2272                     out->bv_val[*pos] = '.';
2273                     (*pos)++;
2274           }
2275           (*pos)--;/*remove the last '.'*/
2276 
2277           /*
2278            * lookup OID database to locate desc
2279            * then overwrite OID with desc in *out
2280            * If failed to look up desc, OID form is used
2281            */
2282           bv_name.bv_val = out->bv_val + start_pos;
2283           bv_name.bv_len = *pos - start_pos;
2284           at = at_bvfind( &bv_name );
2285           if ( !at )
2286                     return LDAP_SUCCESS;
2287           desc_size = at->sat_cname.bv_len;
2288           memcpy( out->bv_val + start_pos, at->sat_cname.bv_val, desc_size );
2289           *pos = start_pos + desc_size;
2290           return LDAP_SUCCESS;
2291 }
2292 
2293 int
ConvertComponentAttributeTypeAndValue2RFC2253(irAttributeTypeAndValue * in,struct berval * out,int * pos)2294 ConvertComponentAttributeTypeAndValue2RFC2253 ( irAttributeTypeAndValue* in, struct berval* out, int *pos ) {
2295           int rc;
2296           int value_size = ((ComponentUTF8String*)in->value.value)->value.octetLen;
2297           char* value_ptr =  ((ComponentUTF8String*)in->value.value)->value.octs;
2298 
2299           rc = ConvertBER2Desc( in->type.value.octs, in->type.value.octetLen, out, pos );
2300           if ( rc != LDAP_SUCCESS ) return rc;
2301           if ( out->bv_len < *pos + 1/*for '='*/  )
2302                     increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2303           /*Between type and value, put '='*/
2304           out->bv_val[*pos] = '=';
2305           (*pos)++;
2306 
2307           /*Assume it is string*/
2308           if ( out->bv_len < *pos + value_size )
2309                     increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2310           memcpy( out->bv_val + *pos, value_ptr, value_size );
2311           out->bv_len += value_size;
2312           *pos += value_size;
2313 
2314           return LDAP_SUCCESS;
2315 }
2316 
2317 int
ConvertRelativeDistinguishedName2RFC2253(irRelativeDistinguishedName * in,struct berval * out,int * pos)2318 ConvertRelativeDistinguishedName2RFC2253 ( irRelativeDistinguishedName* in, struct berval *out , int* pos) {
2319           irAttributeTypeAndValue* attr_typeNvalue;
2320           int rc;
2321 
2322 
2323           FOR_EACH_LIST_ELMT( attr_typeNvalue, &in->comp_list)
2324           {
2325                     rc = ConvertComponentAttributeTypeAndValue2RFC2253( attr_typeNvalue, out, pos );
2326                     if ( rc != LDAP_SUCCESS ) return LDAP_INVALID_SYNTAX;
2327 
2328                     if ( out->bv_len < *pos + 1/*for '+'*/  )
2329                               increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2330                     /*between multivalued RDNs, put comma*/
2331                     out->bv_val[(*pos)++] = '+';
2332           }
2333           (*pos)--;/*remove the last '+'*/
2334           return LDAP_SUCCESS;
2335 }
2336 
2337 int
ConvertRDN2RFC2253(irRelativeDistinguishedName * in,struct berval * out)2338 ConvertRDN2RFC2253 ( irRelativeDistinguishedName* in, struct berval *out ) {
2339           int rc, pos = 0;
2340           out->bv_val = (char*)malloc( INITIAL_DN_SIZE );
2341           out->bv_len = INITIAL_DN_SIZE;
2342 
2343           rc = ConvertRelativeDistinguishedName2RFC2253 ( in, out , &pos);
2344           if ( rc != LDAP_SUCCESS ) return rc;
2345           out->bv_val[pos] = '\0';
2346           out->bv_len = pos;
2347           return LDAP_SUCCESS;
2348 }
2349 
2350 int
ConvertRDNSequence2RFC2253(irRDNSequence * in,struct berval * out)2351 ConvertRDNSequence2RFC2253( irRDNSequence *in, struct berval* out ) {
2352           irRelativeDistinguishedName* rdn_seq;
2353           AsnList* seq = &in->comp_list;
2354           int pos = 0, rc ;
2355 
2356           out->bv_val = (char*)malloc( INITIAL_DN_SIZE );
2357           out->bv_len = INITIAL_DN_SIZE;
2358 
2359           FOR_EACH_LIST_ELMT( rdn_seq, seq )
2360           {
2361                     rc = ConvertRelativeDistinguishedName2RFC2253( rdn_seq, out, &pos );
2362                     if ( rc != LDAP_SUCCESS ) return LDAP_INVALID_SYNTAX;
2363 
2364                     if ( out->bv_len < pos + 1/*for ','*/ )
2365                               increment_bv_mem_by_size ( out, INCREMENT_SIZE );
2366                     /*Between RDN, put comma*/
2367                     out->bv_val[pos++] = ',';
2368           }
2369           pos--;/*remove the last '+'*/
2370           out->bv_val[pos] = '\0';
2371           out->bv_len =pos;
2372           return LDAP_SUCCESS;
2373 }
2374 
2375 #endif
2376