]> git.stg.codes - stg.git/blob - libs/smux/OBJECT_IDENTIFIER.c
Add an option to opt-out Firebird store.
[stg.git] / libs / smux / OBJECT_IDENTIFIER.c
1 /*-
2  * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3  * Redistribution and modifications are permitted subject to BSD license.
4  */
5 #include <asn_internal.h>
6 #include <INTEGER.h>
7 #include <OBJECT_IDENTIFIER.h>
8 #include <OCTET_STRING.h>
9 #include <limits.h>     /* for CHAR_BIT */
10 #include <errno.h>
11
12 /*
13  * OBJECT IDENTIFIER basic type description.
14  */
15 static const ber_tlv_tag_t asn_DEF_OBJECT_IDENTIFIER_tags[] = {
16         (ASN_TAG_CLASS_UNIVERSAL | (6 << 2))
17 };
18 asn_TYPE_operation_t asn_OP_OBJECT_IDENTIFIER = {
19         ASN__PRIMITIVE_TYPE_free,
20         OBJECT_IDENTIFIER_print,
21         OCTET_STRING_compare,   /* Implemented in terms of a string comparison */
22         ber_decode_primitive,
23         der_encode_primitive,
24         OBJECT_IDENTIFIER_decode_xer,
25         OBJECT_IDENTIFIER_encode_xer,
26 #ifdef  ASN_DISABLE_OER_SUPPORT
27         0,
28         0,
29 #else
30         OBJECT_IDENTIFIER_decode_oer,
31         OBJECT_IDENTIFIER_encode_oer,
32 #endif  /* ASN_DISABLE_OER_SUPPORT */
33 #ifdef  ASN_DISABLE_PER_SUPPORT
34         0,
35         0,
36 #else
37         OCTET_STRING_decode_uper,
38         OCTET_STRING_encode_uper,
39 #endif  /* ASN_DISABLE_PER_SUPPORT */
40         OBJECT_IDENTIFIER_random_fill,
41         0       /* Use generic outmost tag fetcher */
42 };
43 asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER = {
44         "OBJECT IDENTIFIER",
45         "OBJECT_IDENTIFIER",
46         &asn_OP_OBJECT_IDENTIFIER,
47         asn_DEF_OBJECT_IDENTIFIER_tags,
48         sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
49             / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
50         asn_DEF_OBJECT_IDENTIFIER_tags, /* Same as above */
51         sizeof(asn_DEF_OBJECT_IDENTIFIER_tags)
52             / sizeof(asn_DEF_OBJECT_IDENTIFIER_tags[0]),
53         { 0, 0, OBJECT_IDENTIFIER_constraint },
54         0, 0,   /* No members */
55         0       /* No specifics */
56 };
57
58 int
59 OBJECT_IDENTIFIER_constraint(const asn_TYPE_descriptor_t *td, const void *sptr,
60                              asn_app_constraint_failed_f *ctfailcb,
61                              void *app_key) {
62     const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
63
64         if(st && st->buf) {
65                 if(st->size < 1) {
66                         ASN__CTFAIL(app_key, td, sptr,
67                                 "%s: at least one numerical value "
68                                 "expected (%s:%d)",
69                                 td->name, __FILE__, __LINE__);
70                         return -1;
71                 }
72         } else {
73                 ASN__CTFAIL(app_key, td, sptr,
74                         "%s: value not given (%s:%d)",
75                         td->name, __FILE__, __LINE__);
76                 return -1;
77         }
78
79         return 0;
80 }
81
82 static ssize_t
83 OBJECT_IDENTIFIER_get_first_arcs(const uint8_t *arcbuf, size_t arcbuf_len,
84                                  asn_oid_arc_t *arc0, asn_oid_arc_t *arc1) {
85     asn_oid_arc_t value;
86
87     ssize_t rd = OBJECT_IDENTIFIER_get_single_arc(arcbuf, arcbuf_len, &value);
88     if(rd <= 0) return rd;
89
90     if(value >= 80) {
91         *arc0 = 2;
92         *arc1 = value - 80;
93     } else if(value >= 40) {
94         *arc0 = 1;
95         *arc1 = value - 40;
96     } else {
97         *arc0 = 0;
98         *arc1 = value;
99     }
100
101     return rd;
102 }
103
104 ssize_t
105 OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, size_t arcbuf_len,
106                                  asn_oid_arc_t *ret_value) {
107     const uint8_t *b = arcbuf;
108     const uint8_t *arcend = arcbuf + arcbuf_len; /* End of arc */
109
110     if(arcbuf == arcend) {
111         return 0;
112     } else {
113         asn_oid_arc_t accum;
114         /* Gather all bits into the accumulator */
115         for(accum = 0; b < arcend; b++) {
116             accum = (accum << 7) | (*b & ~0x80);
117             if((*b & 0x80) == 0) {
118                 if(accum <= ASN_OID_ARC_MAX) {
119                     *ret_value = accum;
120                     return 1 + (b - arcbuf);
121                 } else {
122                     errno = ERANGE; /* Overflow */
123                     return -1;
124                 }
125             }
126         }
127         errno = EINVAL;
128         return -1;
129     }
130
131 }
132
133 static ssize_t
134 OBJECT_IDENTIFIER__dump_body(const OBJECT_IDENTIFIER_t *st,
135                              asn_app_consume_bytes_f *cb, void *app_key) {
136     char scratch[32];
137     asn_oid_arc_t arc0, arc1;
138     size_t produced = 0;
139     size_t off = 0;
140     ssize_t rd;
141     int ret;
142
143     rd = OBJECT_IDENTIFIER_get_first_arcs(st->buf, st->size, &arc0, &arc1);
144     if(rd <= 0) {
145         return -1;
146     }
147
148     ret = snprintf(scratch, sizeof(scratch), "%"PRIu32".%"PRIu32, arc0, arc1);
149     if(ret >= (ssize_t)sizeof(scratch)) {
150         return -1;
151     }
152     produced += ret;
153     if(cb(scratch, ret, app_key) < 0)
154         return -1;
155
156     for(off = rd; ; ) {
157         asn_oid_arc_t arc;
158         rd = OBJECT_IDENTIFIER_get_single_arc(st->buf + off, st->size - off,
159                                               &arc);
160         if(rd < 0) {
161             return -1;
162         } else if(rd == 0) {
163             /* No more arcs. */
164             break;
165         } else {
166             off += rd;
167             assert(off <= st->size);
168             ret = snprintf(scratch, sizeof(scratch), ".%" PRIu32, arc);
169             if(ret >= (ssize_t)sizeof(scratch)) {
170                 return -1;
171             }
172             produced += ret;
173             if(cb(scratch, ret, app_key) < 0) return -1;
174         }
175     }
176
177     if(off != st->size) {
178         ASN_DEBUG("Could not scan to the end of Object Identifier");
179         return -1;
180     }
181
182         return produced;
183 }
184
185 static enum xer_pbd_rval
186 OBJECT_IDENTIFIER__xer_body_decode(const asn_TYPE_descriptor_t *td, void *sptr,
187                                    const void *chunk_buf, size_t chunk_size) {
188     OBJECT_IDENTIFIER_t *st = (OBJECT_IDENTIFIER_t *)sptr;
189         const char *chunk_end = (const char *)chunk_buf + chunk_size;
190         const char *endptr;
191         asn_oid_arc_t s_arcs[10];
192         asn_oid_arc_t *arcs = s_arcs;
193         ssize_t num_arcs;
194         ssize_t ret;
195
196         (void)td;
197
198     num_arcs = OBJECT_IDENTIFIER_parse_arcs(
199         (const char *)chunk_buf, chunk_size, arcs,
200         sizeof(s_arcs) / sizeof(s_arcs[0]), &endptr);
201     if(num_arcs < 0) {
202                 /* Expecting more than zero arcs */
203                 return XPBD_BROKEN_ENCODING;
204         } else if(num_arcs == 0) {
205                 return XPBD_NOT_BODY_IGNORE;
206         }
207         assert(endptr == chunk_end);
208
209         if((size_t)num_arcs > sizeof(s_arcs)/sizeof(s_arcs[0])) {
210                 arcs = (asn_oid_arc_t *)MALLOC(num_arcs * sizeof(asn_oid_arc_t));
211                 if(!arcs) return XPBD_SYSTEM_FAILURE;
212         ret = OBJECT_IDENTIFIER_parse_arcs((const char *)chunk_buf, chunk_size,
213                                            arcs, num_arcs, &endptr);
214         if(ret != num_arcs)
215                         return XPBD_SYSTEM_FAILURE;     /* assert?.. */
216         }
217
218         /*
219          * Convert arcs into BER representation.
220          */
221         ret = OBJECT_IDENTIFIER_set_arcs(st, arcs, num_arcs);
222         if(arcs != s_arcs) FREEMEM(arcs);
223
224         return ret ? XPBD_SYSTEM_FAILURE : XPBD_BODY_CONSUMED;
225 }
226
227 asn_dec_rval_t
228 OBJECT_IDENTIFIER_decode_xer(const asn_codec_ctx_t *opt_codec_ctx,
229                              const asn_TYPE_descriptor_t *td, void **sptr,
230                              const char *opt_mname, const void *buf_ptr,
231                              size_t size) {
232     return xer_decode_primitive(opt_codec_ctx, td,
233                 sptr, sizeof(OBJECT_IDENTIFIER_t), opt_mname,
234                         buf_ptr, size, OBJECT_IDENTIFIER__xer_body_decode);
235 }
236
237 asn_enc_rval_t
238 OBJECT_IDENTIFIER_encode_xer(const asn_TYPE_descriptor_t *td, const void *sptr,
239                              int ilevel, enum xer_encoder_flags_e flags,
240                              asn_app_consume_bytes_f *cb, void *app_key) {
241     const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
242         asn_enc_rval_t er;
243
244     (void)ilevel;
245     (void)flags;
246
247     if(!st || !st->buf) {
248         ASN__ENCODE_FAILED;
249     }
250
251     er.encoded = OBJECT_IDENTIFIER__dump_body(st, cb, app_key);
252     if(er.encoded < 0) ASN__ENCODE_FAILED;
253
254     ASN__ENCODED_OK(er);
255 }
256
257 int
258 OBJECT_IDENTIFIER_print(const asn_TYPE_descriptor_t *td, const void *sptr,
259                         int ilevel, asn_app_consume_bytes_f *cb,
260                         void *app_key) {
261     const OBJECT_IDENTIFIER_t *st = (const OBJECT_IDENTIFIER_t *)sptr;
262
263         (void)td;       /* Unused argument */
264         (void)ilevel;   /* Unused argument */
265
266         if(!st || !st->buf)
267                 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
268
269         /* Dump preamble */
270         if(cb("{ ", 2, app_key) < 0)
271                 return -1;
272
273     if(OBJECT_IDENTIFIER__dump_body(st, cb, app_key) < 0) {
274         return -1;
275     }
276
277     return (cb(" }", 2, app_key) < 0) ? -1 : 0;
278 }
279
280 ssize_t
281 OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *st, asn_oid_arc_t *arcs,
282                            size_t arc_slots) {
283     asn_oid_arc_t arc0, arc1;
284     size_t num_arcs = 0;
285     size_t off;
286     ssize_t rd;
287
288     if(!st || !st->buf) {
289         errno = EINVAL;
290         return -1;
291     }
292
293     rd = OBJECT_IDENTIFIER_get_first_arcs(st->buf, st->size, &arc0, &arc1);
294     if(rd <= 0) {
295         return -1;
296     }
297     num_arcs = 2;
298     switch(arc_slots) {
299     default:
300     case 2:
301         arcs[1] = arc1;
302         /* Fall through */
303     case 1:
304         arcs[0] = arc0;
305         /* Fall through */
306     case 0:
307         break;
308     }
309
310     for(off = rd; ; ) {
311         asn_oid_arc_t arc;
312         rd = OBJECT_IDENTIFIER_get_single_arc(st->buf + off, st->size - off,
313                                               &arc);
314         if(rd < 0) {
315             return -1;
316         } else if(rd == 0) {
317             /* No more arcs. */
318             break;
319         } else {
320             off += rd;
321             if(num_arcs < arc_slots) {
322                 arcs[num_arcs] = arc;
323             }
324             num_arcs++;
325         }
326     }
327
328     if(off != st->size) {
329         return -1;
330     }
331
332     return num_arcs;
333 }
334
335
336 /*
337  * Save the single value as an object identifier arc.
338  */
339 ssize_t
340 OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, size_t arcbuf_len,
341                                  asn_oid_arc_t value) {
342     /*
343          * The following conditions must hold:
344          * assert(arcbuf);
345          */
346     uint8_t scratch[((sizeof(value) * CHAR_BIT + 6) / 7)];
347     uint8_t *scratch_end = &scratch[sizeof(scratch)-1];
348     uint8_t *b;
349     size_t result_len;
350     uint8_t mask;
351
352     for(b = scratch_end, mask = 0; ; mask = 0x80, b--) {
353         *b = mask | (value & 0x7f);
354         value >>= 7;
355         if(!value) {
356             break;
357         }
358     }
359
360     result_len = (scratch_end - b) + 1;
361
362     if(result_len > arcbuf_len) {
363         return -1;
364     }
365
366     memcpy(arcbuf, b, result_len);
367
368         return result_len;
369 }
370
371 int
372 OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *st, const asn_oid_arc_t *arcs,
373                            size_t arc_slots) {
374     uint8_t *buf;
375     uint8_t *bp;
376     ssize_t wrote;
377     asn_oid_arc_t arc0;
378     asn_oid_arc_t arc1;
379     size_t size;
380     size_t i;
381
382     if(!st || !arcs || arc_slots < 2) {
383         errno = EINVAL;
384                 return -1;
385         }
386
387     arc0 = arcs[0];
388     arc1 = arcs[1];
389
390         if(arc0 <= 1) {
391                 if(arc1 >= 40) {
392                         /* 8.19.4: At most 39 subsequent values (including 0) */
393                         errno = ERANGE;
394                         return -1;
395                 }
396     } else if(arc0 == 2) {
397         if(arc1 > ASN_OID_ARC_MAX - 80) {
398             errno = ERANGE;
399             return -1;
400         }
401     } else if(arc0 > 2) {
402         /* 8.19.4: Only three values are allocated from the root node */
403         errno = ERANGE;
404         return -1;
405     }
406
407     /*
408          * After above tests it is known that the value of arc0 is completely
409          * trustworthy (0..2). However, the arc1's value is still meaningless.
410          */
411
412     /*
413      * Roughly estimate the maximum size necessary to encode these arcs.
414      * This estimation implicitly takes in account the following facts,
415      * that cancel each other:
416      *  * the first two arcs are encoded in a single value.
417      *  * the first value may require more space (+1 byte)
418      *  * the value of the first arc which is in range (0..2)
419      */
420     size = ((sizeof(asn_oid_arc_t) * CHAR_BIT + 6) / 7) * arc_slots;
421     bp = buf = (uint8_t *)MALLOC(size + 1);
422     if(!buf) {
423         /* ENOMEM */
424         return -1;
425     }
426
427     wrote = OBJECT_IDENTIFIER_set_single_arc(bp, size, arc0 * 40 + arc1);
428     if(wrote <= 0) {
429         FREEMEM(buf);
430         return -1;
431     }
432     assert((size_t)wrote <= size);
433     bp += wrote;
434     size -= wrote;
435
436     for(i = 2; i < arc_slots; i++) {
437                 wrote = OBJECT_IDENTIFIER_set_single_arc(bp, size, arcs[i]);
438         if(wrote <= 0) {
439             FREEMEM(buf);
440             return -1;
441         }
442         assert((size_t)wrote <= size);
443         bp += wrote;
444         size -= wrote;
445     }
446
447     /*
448          * Replace buffer.
449          */
450         st->size = bp - buf;
451         bp = st->buf;
452         st->buf = buf;
453         st->buf[st->size] = '\0';
454         if(bp) FREEMEM(bp);
455
456         return 0;
457 }
458
459 ssize_t
460 OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, ssize_t oid_txt_length,
461                              asn_oid_arc_t *arcs, size_t arcs_count,
462                              const char **opt_oid_text_end) {
463     size_t num_arcs = 0;
464     const char *oid_end;
465         enum {
466                 ST_LEADSPACE,
467                 ST_TAILSPACE,
468                 ST_AFTERVALUE,  /* Next character ought to be '.' or a space */
469                 ST_WAITDIGITS   /* Next character is expected to be a digit */
470         } state = ST_LEADSPACE;
471
472         if(!oid_text || oid_txt_length < -1 || (arcs_count && !arcs)) {
473                 if(opt_oid_text_end) *opt_oid_text_end = oid_text;
474                 errno = EINVAL;
475                 return -1;
476         }
477
478         if(oid_txt_length == -1)
479                 oid_txt_length = strlen(oid_text);
480
481 #define _OID_CAPTURE_ARC(oid_text, oid_end)                       \
482     do {                                                          \
483         const char *endp = oid_end;                               \
484         unsigned long value;                                      \
485         switch(asn_strtoul_lim(oid_text, &endp, &value)) {        \
486         case ASN_STRTOX_EXTRA_DATA:                               \
487         case ASN_STRTOX_OK:                                       \
488             if(value <= ASN_OID_ARC_MAX) {                        \
489                 if(num_arcs < arcs_count) arcs[num_arcs] = value; \
490                 num_arcs++;                                       \
491                 oid_text = endp - 1;                              \
492                 break;                                            \
493             }                                                     \
494             /* Fall through */                                    \
495         case ASN_STRTOX_ERROR_RANGE:                              \
496             if(opt_oid_text_end) *opt_oid_text_end = oid_text;    \
497             errno = ERANGE;                                       \
498             return -1;                                            \
499         case ASN_STRTOX_ERROR_INVAL:                              \
500         case ASN_STRTOX_EXPECT_MORE:                              \
501             if(opt_oid_text_end) *opt_oid_text_end = oid_text;    \
502             errno = EINVAL;                                       \
503             return -1;                                            \
504         }                                                         \
505     } while(0)
506
507     for(oid_end = oid_text + oid_txt_length; oid_text<oid_end; oid_text++) {
508             switch(*oid_text) {
509             case 0x09: case 0x0a: case 0x0d: case 0x20: /* whitespace */
510                 switch(state) {
511                 case ST_LEADSPACE:
512                 case ST_TAILSPACE:
513                         continue;
514                 case ST_AFTERVALUE:
515                         state = ST_TAILSPACE;
516                         continue;
517                 case ST_WAITDIGITS:
518                         break;  /* Digits expected after ".", got whitespace */
519                 }
520                 break;
521             case 0x2e:  /* '.' */
522                 switch(state) {
523                 case ST_LEADSPACE:
524                 case ST_TAILSPACE:
525                 case ST_WAITDIGITS:
526                         if(opt_oid_text_end)
527                                 *opt_oid_text_end = oid_text;
528                         errno = EINVAL; /* Broken OID */
529                         return -1;
530                         break;
531                 case ST_AFTERVALUE:
532                         state = ST_WAITDIGITS;
533                         continue;
534                 }
535                 break;
536             case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
537             case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
538                 switch(state) {
539                 case ST_TAILSPACE:
540                 case ST_AFTERVALUE:
541                         if(opt_oid_text_end)
542                                 *opt_oid_text_end = oid_text;
543                         errno = EINVAL; /* "1. 1" => broken OID */
544                         return -1;
545                 case ST_LEADSPACE:
546                 case ST_WAITDIGITS:
547                         _OID_CAPTURE_ARC(oid_text, oid_end);
548                         state = ST_AFTERVALUE;
549                         continue;
550                 }
551                 break;
552             default:
553                 /* Unexpected symbols */
554                 state = ST_WAITDIGITS;
555                 break;
556             } /* switch() */
557             break;
558         } /* for() */
559
560
561         if(opt_oid_text_end) *opt_oid_text_end = oid_text;
562
563         /* Finalize last arc */
564         switch(state) {
565         case ST_LEADSPACE:
566                 return 0; /* No OID found in input data */
567         case ST_WAITDIGITS:
568                 errno = EINVAL; /* Broken OID */
569                 return -1;
570         case ST_AFTERVALUE:
571         case ST_TAILSPACE:
572                 return num_arcs;
573         }
574
575         errno = EINVAL; /* Broken OID */
576         return -1;
577 }
578
579 /*
580  * Generate values from the list of interesting values, or just a random
581  * value up to the upper limit.
582  */
583 static asn_oid_arc_t
584 OBJECT_IDENTIFIER__biased_random_arc(asn_oid_arc_t upper_bound) {
585     const asn_oid_arc_t values[] = {0, 1, 127, 128, 129, 254, 255, 256};
586     size_t idx;
587
588     switch(asn_random_between(0, 2)) {
589     case 0:
590         idx = asn_random_between(0, sizeof(values) / sizeof(values[0]) - 1);
591         if(values[idx] < upper_bound) {
592             return values[idx];
593         }
594         /* Fall through */
595     case 1:
596         return asn_random_between(0, upper_bound);
597     case 2:
598     default:
599         return upper_bound;
600     }
601 }
602
603 asn_random_fill_result_t
604 OBJECT_IDENTIFIER_random_fill(const asn_TYPE_descriptor_t *td, void **sptr,
605                               const asn_encoding_constraints_t *constraints,
606                               size_t max_length) {
607     asn_random_fill_result_t result_ok = {ARFILL_OK, 1};
608     asn_random_fill_result_t result_failed = {ARFILL_FAILED, 0};
609     asn_random_fill_result_t result_skipped = {ARFILL_SKIPPED, 0};
610     OBJECT_IDENTIFIER_t *st;
611     asn_oid_arc_t arcs[5];
612     size_t arcs_len = asn_random_between(2, 5);
613     size_t i;
614
615     (void)constraints;
616
617     if(max_length < arcs_len) return result_skipped;
618
619     if(*sptr) {
620         st = *sptr;
621     } else {
622         st = CALLOC(1, sizeof(*st));
623     }
624
625     arcs[0] = asn_random_between(0, 2);
626     arcs[1] = OBJECT_IDENTIFIER__biased_random_arc(
627         arcs[0] <= 1 ? 39 : (ASN_OID_ARC_MAX - 80));
628     for(i = 2; i < arcs_len; i++) {
629         arcs[i] = OBJECT_IDENTIFIER__biased_random_arc(ASN_OID_ARC_MAX);
630     }
631
632     if(OBJECT_IDENTIFIER_set_arcs(st, arcs, arcs_len)) {
633         if(st != *sptr) {
634             ASN_STRUCT_FREE(*td, st);
635         }
636         return result_failed;
637     }
638
639     *sptr = st;
640
641     result_ok.length = st->size;
642     return result_ok;
643 }