Lev Walkin | f15320b | 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 | */ |
Lev Walkin | a9cc46e | 2004-09-22 16:06:28 +0000 | [diff] [blame] | 5 | #include <asn_internal.h> |
Lev Walkin | f15320b | 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 |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 10 | ber_fetch_length(int _is_constructed, const void *bufptr, size_t size, |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 11 | ber_tlv_len_t *len_r) { |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 12 | const uint8_t *buf = (const uint8_t *)bufptr; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 13 | unsigned oct; |
| 14 | |
| 15 | if(size == 0) |
| 16 | return 0; /* Want more */ |
| 17 | |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 18 | oct = *(const uint8_t *)buf; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 19 | if((oct & 0x80) == 0) { |
| 20 | /* |
| 21 | * Short definite length. |
| 22 | */ |
Lev Walkin | 619b6aa | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 23 | *len_r = oct; /* & 0x7F */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 24 | return 1; |
| 25 | } else { |
| 26 | ber_tlv_len_t len; |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 27 | size_t skipped; |
Lev Walkin | f15320b | 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; |
Lev Walkin | 619b6aa | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 41 | oct && (++skipped <= size); buf++, oct--) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 42 | |
Lev Walkin | 07a50d8 | 2017-09-15 23:26:11 -0700 | [diff] [blame] | 43 | /* Verify that we won't overflow. */ |
| 44 | if(!(len >> ((8 * sizeof(len)) - (8+1)))) { |
| 45 | len = (len << 8) | *buf; |
| 46 | } else { |
| 47 | /* Too large length value. */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | return -1; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if(oct == 0) { |
Lev Walkin | 9368263 | 2017-09-15 23:32:36 -0700 | [diff] [blame] | 53 | if(len < 0 || len > RSSIZE_MAX) { |
Lev Walkin | 07a50d8 | 2017-09-15 23:26:11 -0700 | [diff] [blame] | 54 | /* Length value out of sane range. */ |
Lev Walkin | c4e4b68 | 2004-09-29 13:25:23 +0000 | [diff] [blame] | 55 | return -1; |
| 56 | } |
| 57 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 58 | *len_r = len; |
| 59 | return skipped; |
| 60 | } |
| 61 | |
| 62 | return 0; /* Want more */ |
| 63 | } |
| 64 | |
| 65 | } |
| 66 | |
| 67 | ssize_t |
Lev Walkin | afbf2a9 | 2017-09-12 23:30:27 -0700 | [diff] [blame] | 68 | ber_skip_length(const asn_codec_ctx_t *opt_codec_ctx, |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 69 | int _is_constructed, const void *ptr, size_t size) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 70 | ber_tlv_len_t vlen; /* Length of V in TLV */ |
| 71 | ssize_t tl; /* Length of L in TLV */ |
| 72 | ssize_t ll; /* Length of L in TLV */ |
Lev Walkin | d9bd775 | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 73 | size_t skip; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 74 | |
| 75 | /* |
Lev Walkin | baaa24f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 76 | * Make sure we didn't exceed the maximum stack size. |
| 77 | */ |
Lev Walkin | 7c1dc05 | 2016-03-14 03:08:15 -0700 | [diff] [blame] | 78 | if(ASN__STACK_OVERFLOW_CHECK(opt_codec_ctx)) |
Lev Walkin | 1d9e8dd | 2005-12-07 05:46:03 +0000 | [diff] [blame] | 79 | return -1; |
Lev Walkin | baaa24f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 80 | |
| 81 | /* |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 82 | * Determine the size of L in TLV. |
| 83 | */ |
| 84 | ll = ber_fetch_length(_is_constructed, ptr, size, &vlen); |
| 85 | if(ll <= 0) return ll; |
| 86 | |
| 87 | /* |
| 88 | * Definite length. |
| 89 | */ |
| 90 | if(vlen >= 0) { |
| 91 | skip = ll + vlen; |
| 92 | if(skip > size) |
| 93 | return 0; /* Want more */ |
| 94 | return skip; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Indefinite length! |
| 99 | */ |
| 100 | ASN_DEBUG("Skipping indefinite length"); |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 101 | for(skip = ll, ptr = ((const char *)ptr) + ll, size -= ll;;) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 102 | ber_tlv_tag_t tag; |
| 103 | |
| 104 | /* Fetch the tag */ |
| 105 | tl = ber_fetch_tag(ptr, size, &tag); |
| 106 | if(tl <= 0) return tl; |
| 107 | |
Lev Walkin | baaa24f | 2004-09-29 14:19:14 +0000 | [diff] [blame] | 108 | ll = ber_skip_length(opt_codec_ctx, |
| 109 | BER_TLV_CONSTRUCTED(ptr), |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 110 | ((const char *)ptr) + tl, size - tl); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 111 | if(ll <= 0) return ll; |
| 112 | |
| 113 | skip += tl + ll; |
| 114 | |
| 115 | /* |
| 116 | * This may be the end of the indefinite length structure, |
| 117 | * two consecutive 0 octets. |
| 118 | * Check if it is true. |
| 119 | */ |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 120 | if(((const uint8_t *)ptr)[0] == 0 |
| 121 | && ((const uint8_t *)ptr)[1] == 0) |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 122 | return skip; |
| 123 | |
Lev Walkin | 8c3b854 | 2005-03-10 18:52:02 +0000 | [diff] [blame] | 124 | ptr = ((const char *)ptr) + tl + ll; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 125 | size -= tl + ll; |
| 126 | } |
| 127 | |
| 128 | /* UNREACHABLE */ |
| 129 | } |
| 130 | |
Lev Walkin | 91b29c5 | 2004-09-24 20:59:13 +0000 | [diff] [blame] | 131 | size_t |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 132 | der_tlv_length_serialize(ber_tlv_len_t len, void *bufp, size_t size) { |
Lev Walkin | 619b6aa | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 133 | size_t required_size; /* Size of len encoding */ |
Lev Walkin | c234657 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 134 | uint8_t *buf = (uint8_t *)bufp; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 135 | uint8_t *end; |
Lev Walkin | 38306e1 | 2017-09-17 22:33:45 -0700 | [diff] [blame] | 136 | int i; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 137 | |
| 138 | if(len <= 127) { |
| 139 | /* Encoded in 1 octet */ |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 140 | if(size) *buf = (uint8_t)len; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Compute the size of the subsequent bytes. |
| 146 | */ |
Lev Walkin | 38306e1 | 2017-09-17 22:33:45 -0700 | [diff] [blame] | 147 | for(required_size = 1, i = 8; i < 8 * (int)sizeof(len); i += 8) { |
Lev Walkin | 619b6aa | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 148 | if(len >> i) |
| 149 | required_size++; |
| 150 | else |
| 151 | break; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Lev Walkin | 37ec073 | 2006-04-16 08:57:39 +0000 | [diff] [blame] | 154 | if(size <= required_size) |
Lev Walkin | 619b6aa | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 155 | return required_size + 1; |
| 156 | |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 157 | *buf++ = (uint8_t)(0x80 | required_size); /* Length of the encoding */ |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 158 | |
| 159 | /* |
| 160 | * Produce the len encoding, space permitting. |
| 161 | */ |
Lev Walkin | 619b6aa | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 162 | end = buf + required_size; |
| 163 | for(i -= 8; buf < end; i -= 8, buf++) |
Lev Walkin | 4efbfb7 | 2005-02-25 14:20:30 +0000 | [diff] [blame] | 164 | *buf = (uint8_t)(len >> i); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 165 | |
Lev Walkin | 619b6aa | 2004-08-19 18:10:27 +0000 | [diff] [blame] | 166 | return required_size + 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 167 | } |
| 168 | |