sjana | 76e88d2 | 2020-03-19 13:24:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2003-2017 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | #ifndef _OBJECT_IDENTIFIER_H_ |
| 6 | #define _OBJECT_IDENTIFIER_H_ |
| 7 | |
| 8 | #include <asn_application.h> |
| 9 | #include <asn_codecs_prim.h> |
| 10 | #include <OCTET_STRING.h> |
| 11 | |
| 12 | #ifdef __cplusplus |
| 13 | extern "C" { |
| 14 | #endif |
| 15 | |
| 16 | typedef uint32_t asn_oid_arc_t; |
| 17 | #define ASN_OID_ARC_MAX (~((asn_oid_arc_t)0)) |
| 18 | |
| 19 | typedef ASN__PRIMITIVE_TYPE_t OBJECT_IDENTIFIER_t; |
| 20 | |
| 21 | extern asn_TYPE_descriptor_t asn_DEF_OBJECT_IDENTIFIER; |
| 22 | extern asn_TYPE_operation_t asn_OP_OBJECT_IDENTIFIER; |
| 23 | |
| 24 | asn_struct_print_f OBJECT_IDENTIFIER_print; |
| 25 | asn_constr_check_f OBJECT_IDENTIFIER_constraint; |
| 26 | der_type_encoder_f OBJECT_IDENTIFIER_encode_der; |
| 27 | xer_type_decoder_f OBJECT_IDENTIFIER_decode_xer; |
| 28 | xer_type_encoder_f OBJECT_IDENTIFIER_encode_xer; |
| 29 | asn_random_fill_f OBJECT_IDENTIFIER_random_fill; |
| 30 | |
| 31 | #define OBJECT_IDENTIFIER_free ASN__PRIMITIVE_TYPE_free |
| 32 | #define OBJECT_IDENTIFIER_compare OCTET_STRING_compare |
| 33 | #define OBJECT_IDENTIFIER_decode_ber ber_decode_primitive |
| 34 | #define OBJECT_IDENTIFIER_encode_der der_encode_primitive |
| 35 | #define OBJECT_IDENTIFIER_decode_oer oer_decode_primitive |
| 36 | #define OBJECT_IDENTIFIER_encode_oer oer_encode_primitive |
| 37 | #define OBJECT_IDENTIFIER_decode_uper OCTET_STRING_decode_uper |
| 38 | #define OBJECT_IDENTIFIER_encode_uper OCTET_STRING_encode_uper |
| 39 | #define OBJECT_IDENTIFIER_decode_aper OCTET_STRING_decode_aper |
| 40 | #define OBJECT_IDENTIFIER_encode_aper OCTET_STRING_encode_aper |
| 41 | |
| 42 | /********************************** |
| 43 | * Some handy conversion routines * |
| 44 | **********************************/ |
| 45 | |
| 46 | /* |
| 47 | * This function fills an (arcs) array with OBJECT IDENTIFIER arcs |
| 48 | * up to specified (arc_slots) elements. |
| 49 | * |
| 50 | * EXAMPLE: |
| 51 | * void print_arcs(OBJECT_IDENTIFIER_t *oid) { |
| 52 | * asn_oid_arc_t fixed_arcs[10]; // Try with fixed space first |
| 53 | * asn_oid_arc_t *arcs = fixed_arcs; |
| 54 | * size_t arc_slots = sizeof(fixed_arcs)/sizeof(fixed_arcs[0]); // 10 |
| 55 | * ssize_t count; // Real number of arcs. |
| 56 | * int i; |
| 57 | * |
| 58 | * count = OBJECT_IDENTIFIER_get_arcs(oid, arcs, arc_slots); |
| 59 | * // If necessary, reallocate arcs array and try again. |
| 60 | * if(count > arc_slots) { |
| 61 | * arc_slots = count; |
| 62 | * arcs = malloc(sizeof(asn_oid_arc_t) * arc_slots); |
| 63 | * if(!arcs) return; |
| 64 | * count = OBJECT_IDENTIFIER_get_arcs(oid, arcs, arc_slots); |
| 65 | * assert(count == arc_slots); |
| 66 | * } |
| 67 | * |
| 68 | * // Print the contents of the arcs array. |
| 69 | * for(i = 0; i < count; i++) |
| 70 | * printf("%"PRIu32"\n", arcs[i]); |
| 71 | * |
| 72 | * // Avoid memory leak. |
| 73 | * if(arcs != fixed_arcs) free(arcs); |
| 74 | * } |
| 75 | * |
| 76 | * RETURN VALUES: |
| 77 | * -1/EINVAL: Invalid arguments (oid is missing) |
| 78 | * -1/ERANGE: One or more arcs have value out of array cell type range. |
| 79 | * >=0: Number of arcs contained in the OBJECT IDENTIFIER |
| 80 | * |
| 81 | * WARNING: The function always returns the actual number of arcs, |
| 82 | * even if there is no sufficient (arc_slots) provided. |
| 83 | */ |
| 84 | ssize_t OBJECT_IDENTIFIER_get_arcs(const OBJECT_IDENTIFIER_t *oid, |
| 85 | asn_oid_arc_t *arcs, size_t arc_slots); |
| 86 | |
| 87 | /* |
| 88 | * This functions initializes the OBJECT IDENTIFIER object with |
| 89 | * the given set of arcs. |
| 90 | * The minimum of two arcs must be present; some restrictions apply. |
| 91 | * RETURN VALUES: |
| 92 | * -1/EINVAL: Invalid arguments |
| 93 | * -1/ERANGE: The first two arcs do not conform to ASN.1 restrictions. |
| 94 | * -1/ENOMEM: Memory allocation failed |
| 95 | * 0: The object was initialized with new arcs. |
| 96 | */ |
| 97 | int OBJECT_IDENTIFIER_set_arcs(OBJECT_IDENTIFIER_t *oid, |
| 98 | const asn_oid_arc_t *arcs, size_t arcs_count); |
| 99 | |
| 100 | |
| 101 | /* |
| 102 | * Parse the OBJECT IDENTIFIER textual representation ("1.3.6.1.4.1.9363"). |
| 103 | * No arc can exceed the (0..ASN_OID_ARC_MAX, which is the same as UINT32_MAX). |
| 104 | * This function is not specific to OBJECT IDENTIFIER, it may be used to parse |
| 105 | * the RELATIVE-OID data, or any other data consisting of dot-separated |
| 106 | * series of numeric values. |
| 107 | * |
| 108 | * If (oid_txt_length == -1), the strlen() will be invoked to determine the |
| 109 | * size of the (oid_text) string. |
| 110 | * |
| 111 | * After return, the optional (opt_oid_text_end) is set to the character after |
| 112 | * the last parsed one. (opt_oid_text_end) is never less than (oid_text). |
| 113 | * |
| 114 | * RETURN VALUES: |
| 115 | * -1: Parse error. |
| 116 | * >= 0: Number of arcs contained in the OBJECT IDENTIFIER. |
| 117 | * |
| 118 | * WARNING: The function always returns the real number of arcs, |
| 119 | * even if there is no sufficient (arc_slots) provided. |
| 120 | * This is useful for (arc_slots) value estimation. |
| 121 | */ |
| 122 | ssize_t OBJECT_IDENTIFIER_parse_arcs(const char *oid_text, |
| 123 | ssize_t oid_txt_length, |
| 124 | asn_oid_arc_t *arcs, size_t arcs_count, |
| 125 | const char **opt_oid_text_end); |
| 126 | |
| 127 | /* |
| 128 | * Internal functions. |
| 129 | * Used by RELATIVE-OID implementation in particular. |
| 130 | */ |
| 131 | |
| 132 | /* |
| 133 | * Retrieve a single arc of size from the (arcbuf) buffer. |
| 134 | * RETURN VALUES: |
| 135 | * -1: Failed to retrieve the value from the (arcbuf). |
| 136 | * >0: Number of bytes consumed from the (arcbuf), <= (arcbuf_len). |
| 137 | */ |
| 138 | ssize_t OBJECT_IDENTIFIER_get_single_arc(const uint8_t *arcbuf, |
| 139 | size_t arcbuf_len, |
| 140 | asn_oid_arc_t *ret_value); |
| 141 | |
| 142 | /* |
| 143 | * Write the unterminated arc value into the (arcbuf) which has the size at |
| 144 | * least (arcbuf_len). |
| 145 | * RETURN VALUES: |
| 146 | * -1: (arcbuf_len) size is not sufficient to write the value. |
| 147 | * <n>: Number of bytes appended to the arcbuf (<= arcbuf_len). |
| 148 | */ |
| 149 | ssize_t OBJECT_IDENTIFIER_set_single_arc(uint8_t *arcbuf, size_t arcbuf_len, |
| 150 | asn_oid_arc_t arc_value); |
| 151 | |
| 152 | #ifdef __cplusplus |
| 153 | } |
| 154 | #endif |
| 155 | |
| 156 | #endif /* _OBJECT_IDENTIFIER_H_ */ |