vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 | */ |
vlm | 39ba4c4 | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 6 | #include <ber_tlv_length.h> |
| 7 | #include <ber_tlv_tag.h> |
| 8 | |
| 9 | ssize_t |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 10 | ber_fetch_length(int _is_constructed, const void *bufptr, size_t size, |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 11 | ber_tlv_len_t *len_r) { |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 12 | const uint8_t *buf = (const uint8_t *)bufptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | unsigned oct; |
| 14 | |
| 15 | if(size == 0) |
| 16 | return 0; /* Want more */ |
| 17 | |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 18 | oct = *(const uint8_t *)buf; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 19 | if((oct & 0x80) == 0) { |
| 20 | /* |
| 21 | * Short definite length. |
| 22 | */ |
vlm | 15f28cb | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 23 | *len_r = oct; /* & 0x7F */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 24 | return 1; |
| 25 | } else { |
| 26 | ber_tlv_len_t len; |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 27 | size_t skipped; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 28 | |
| 29 | if(_is_constructed && oct == 0x80) { |
| 30 | *len_r = -1; /* Indefinite length */ |
| 31 | return 1; |
| 32 | } |
| 33 | |
| 34 | if(oct == 0xff) { |
| 35 | /* Reserved in standard for future use. */ |
| 36 | return -1; |
| 37 | } |
| 38 | |
| 39 | oct &= 0x7F; /* Leave only the 7 LS bits */ |
| 40 | for(len = 0, buf++, skipped = 1; |
vlm | 15f28cb | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 41 | oct && (++skipped <= size); buf++, oct--) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 42 | |
| 43 | len = (len << 8) | *buf; |
| 44 | if(len < 0 |
| 45 | || (len >> ((8 * sizeof(len)) - 8) && oct > 1)) { |
| 46 | /* |
| 47 | * Too large length value. |
| 48 | */ |
| 49 | return -1; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if(oct == 0) { |
vlm | 886e8a9 | 2006-07-27 13:16:51 +0000 | [diff] [blame] | 54 | ber_tlv_len_t lenplusepsilon = (size_t)len + 1024; |
vlm | eeb5ff9 | 2004-09-29 13:25:23 +0000 | [diff] [blame] | 55 | /* |
| 56 | * Here length may be very close or equal to 2G. |
vlm | 6c59384 | 2004-10-26 09:03:31 +0000 | [diff] [blame] | 57 | * However, the arithmetics used in some decoders |
| 58 | * may add some (small) quantities to the length, |
vlm | eeb5ff9 | 2004-09-29 13:25:23 +0000 | [diff] [blame] | 59 | * to check the resulting value against some limits. |
vlm | 886e8a9 | 2006-07-27 13:16:51 +0000 | [diff] [blame] | 60 | * This may result in integer wrap-around, which |
| 61 | * we try to avoid by checking it earlier here. |
vlm | eeb5ff9 | 2004-09-29 13:25:23 +0000 | [diff] [blame] | 62 | */ |
vlm | e939576 | 2006-07-13 12:01:26 +0000 | [diff] [blame] | 63 | if(lenplusepsilon < 0) { |
vlm | eeb5ff9 | 2004-09-29 13:25:23 +0000 | [diff] [blame] | 64 | /* Too large length value */ |
| 65 | return -1; |
| 66 | } |
| 67 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 68 | *len_r = len; |
| 69 | return skipped; |
| 70 | } |
| 71 | |
| 72 | return 0; /* Want more */ |
| 73 | } |
| 74 | |
| 75 | } |
| 76 | |
| 77 | ssize_t |
vlm | 6130a2f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 78 | ber_skip_length(asn_codec_ctx_t *opt_codec_ctx, |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 79 | int _is_constructed, const void *ptr, size_t size) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 80 | ber_tlv_len_t vlen; /* Length of V in TLV */ |
| 81 | ssize_t tl; /* Length of L in TLV */ |
| 82 | ssize_t ll; /* Length of L in TLV */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 83 | size_t skip; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | |
| 85 | /* |
vlm | 6130a2f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 86 | * Make sure we didn't exceed the maximum stack size. |
| 87 | */ |
vlm | 4d2ca12 | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 88 | if(_ASN_STACK_OVERFLOW_CHECK(opt_codec_ctx)) |
| 89 | return -1; |
vlm | 6130a2f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 90 | |
| 91 | /* |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 92 | * Determine the size of L in TLV. |
| 93 | */ |
| 94 | ll = ber_fetch_length(_is_constructed, ptr, size, &vlen); |
| 95 | if(ll <= 0) return ll; |
| 96 | |
| 97 | /* |
| 98 | * Definite length. |
| 99 | */ |
| 100 | if(vlen >= 0) { |
| 101 | skip = ll + vlen; |
| 102 | if(skip > size) |
| 103 | return 0; /* Want more */ |
| 104 | return skip; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Indefinite length! |
| 109 | */ |
| 110 | ASN_DEBUG("Skipping indefinite length"); |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 111 | for(skip = ll, ptr = ((const char *)ptr) + ll, size -= ll;;) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 112 | ber_tlv_tag_t tag; |
| 113 | |
| 114 | /* Fetch the tag */ |
| 115 | tl = ber_fetch_tag(ptr, size, &tag); |
| 116 | if(tl <= 0) return tl; |
| 117 | |
vlm | 6130a2f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 118 | ll = ber_skip_length(opt_codec_ctx, |
| 119 | BER_TLV_CONSTRUCTED(ptr), |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 120 | ((const char *)ptr) + tl, size - tl); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 121 | if(ll <= 0) return ll; |
| 122 | |
| 123 | skip += tl + ll; |
| 124 | |
| 125 | /* |
| 126 | * This may be the end of the indefinite length structure, |
| 127 | * two consecutive 0 octets. |
| 128 | * Check if it is true. |
| 129 | */ |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 130 | if(((const uint8_t *)ptr)[0] == 0 |
| 131 | && ((const uint8_t *)ptr)[1] == 0) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 132 | return skip; |
| 133 | |
vlm | b02dcc6 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 134 | ptr = ((const char *)ptr) + tl + ll; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 135 | size -= tl + ll; |
| 136 | } |
| 137 | |
| 138 | /* UNREACHABLE */ |
| 139 | } |
| 140 | |
vlm | 3326d45 | 2004-09-24 20:59:13 +0000 | [diff] [blame] | 141 | size_t |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 142 | der_tlv_length_serialize(ber_tlv_len_t len, void *bufp, size_t size) { |
vlm | 15f28cb | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 143 | size_t required_size; /* Size of len encoding */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 144 | uint8_t *buf = (uint8_t *)bufp; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 145 | uint8_t *end; |
vlm | 15f28cb | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 146 | size_t i; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 147 | |
| 148 | if(len <= 127) { |
| 149 | /* Encoded in 1 octet */ |
vlm | 8a09e0f | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 150 | if(size) *buf = (uint8_t)len; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Compute the size of the subsequent bytes. |
| 156 | */ |
vlm | 15f28cb | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 157 | for(required_size = 1, i = 8; i < 8 * sizeof(len); i += 8) { |
| 158 | if(len >> i) |
| 159 | required_size++; |
| 160 | else |
| 161 | break; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 162 | } |
| 163 | |
vlm | b179031 | 2006-04-16 08:57:39 +0000 | [diff] [blame] | 164 | if(size <= required_size) |
vlm | 15f28cb | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 165 | return required_size + 1; |
| 166 | |
vlm | 8a09e0f | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 167 | *buf++ = (uint8_t)(0x80 | required_size); /* Length of the encoding */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 168 | |
| 169 | /* |
| 170 | * Produce the len encoding, space permitting. |
| 171 | */ |
vlm | 15f28cb | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 172 | end = buf + required_size; |
| 173 | for(i -= 8; buf < end; i -= 8, buf++) |
vlm | 8a09e0f | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 174 | *buf = (uint8_t)(len >> i); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 175 | |
vlm | 15f28cb | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 176 | return required_size + 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 177 | } |
| 178 | |