Harald Welte | 77847ad | 2015-10-06 22:07:04 +0200 | [diff] [blame] | 1 | /* helper functions to dela with asn1c data types */ |
| 2 | |
| 3 | /* (C) 2014-2015 by Harald Welte <laforge@gnumonks.org> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 20 | |
| 21 | #include <string.h> |
Harald Welte | fcabec3 | 2015-12-18 17:50:57 +0100 | [diff] [blame] | 22 | #include <errno.h> |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 23 | #include <arpa/inet.h> |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 24 | |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 25 | #include <osmocom/core/utils.h> |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 26 | |
| 27 | #include "asn1helpers.h" |
Harald Welte | fcabec3 | 2015-12-18 17:50:57 +0100 | [diff] [blame] | 28 | #include "asn_internal.h" |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 29 | |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 30 | void asn1_u32_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in) |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 31 | { |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 32 | *buf = htonl(in); |
| 33 | bitstr->buf = (uint8_t *) buf; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 34 | bitstr->size = sizeof(uint32_t); |
| 35 | bitstr->bits_unused = 0; |
| 36 | } |
| 37 | |
Daniel Willmann | d6a45b4 | 2015-12-08 13:55:17 +0100 | [diff] [blame] | 38 | void asn1_u28_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in) |
| 39 | { |
| 40 | *buf = htonl(in<<4); |
| 41 | bitstr->buf = (uint8_t *) buf; |
| 42 | bitstr->size = sizeof(uint32_t); |
| 43 | bitstr->bits_unused = 4; |
| 44 | } |
| 45 | |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 46 | void asn1_u24_to_bitstring(BIT_STRING_t *bitstr, uint32_t *buf, uint32_t in) |
Harald Welte | 0bb1261 | 2015-10-06 22:08:54 +0200 | [diff] [blame] | 47 | { |
Daniel Willmann | 9224731 | 2015-12-09 19:04:33 +0100 | [diff] [blame] | 48 | *buf = htonl(in<<8); |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 49 | bitstr->buf = (uint8_t *) buf; |
Harald Welte | 0bb1261 | 2015-10-06 22:08:54 +0200 | [diff] [blame] | 50 | bitstr->size = 24/8; |
| 51 | bitstr->bits_unused = 0; |
| 52 | } |
| 53 | |
Harald Welte | fcabec3 | 2015-12-18 17:50:57 +0100 | [diff] [blame] | 54 | int BIT_STRING_fromBuf(BIT_STRING_t *st, const uint8_t *str, unsigned int bit_len) |
| 55 | { |
| 56 | void *buf; |
| 57 | unsigned int len = bit_len / 8; |
| 58 | |
| 59 | if (bit_len % 8) |
| 60 | len++; |
| 61 | |
| 62 | if (!st || (!str && len)) { |
| 63 | errno = EINVAL; |
| 64 | return -1; |
| 65 | } |
| 66 | |
| 67 | if (!str) { |
| 68 | FREEMEM(st->buf); |
| 69 | st->buf = 0; |
| 70 | st->size = 0; |
| 71 | st->bits_unused = 0; |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | if (len < 0) |
| 76 | len = strlen(str); |
| 77 | |
| 78 | buf = MALLOC(len); |
| 79 | if (!buf) { |
| 80 | errno = ENOMEM; |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | memcpy(buf, str, len); |
| 85 | FREEMEM(st->buf); |
| 86 | st->buf = buf; |
| 87 | st->size = len; |
| 88 | st->bits_unused = (len * 8) - bit_len; |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Daniel Willmann | f3685c2 | 2015-12-07 17:18:56 +0100 | [diff] [blame] | 93 | void asn1_u16_to_str(OCTET_STRING_t *str, uint16_t *buf, uint16_t in) |
| 94 | { |
| 95 | *buf = htons(in); |
| 96 | str->buf = (uint8_t *) buf; |
| 97 | str->size = sizeof(uint16_t); |
| 98 | } |
| 99 | |
| 100 | void asn1_u8_to_str(OCTET_STRING_t *str, uint8_t *buf, uint8_t in) |
| 101 | { |
| 102 | *buf = in; |
| 103 | str->buf = buf; |
| 104 | str->size = sizeof(uint8_t); |
| 105 | } |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 106 | |
| 107 | int asn1_strncpy(char *out, const OCTET_STRING_t *in, size_t n) |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 108 | { |
Daniel Willmann | 53018e9 | 2015-11-23 15:49:29 +0100 | [diff] [blame] | 109 | size_t cpylen = n-1; |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 110 | |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 111 | if (in->size < cpylen) |
| 112 | cpylen = in->size; |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 113 | |
| 114 | strncpy(out, (char *)in->buf, cpylen); |
Daniel Willmann | 53018e9 | 2015-11-23 15:49:29 +0100 | [diff] [blame] | 115 | out[cpylen] = '\0'; |
Harald Welte | 30afef3 | 2015-08-30 12:28:29 +0200 | [diff] [blame] | 116 | |
| 117 | return cpylen; |
| 118 | } |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 119 | |
| 120 | uint16_t asn1str_to_u16(const OCTET_STRING_t *in) |
| 121 | { |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 122 | OSMO_ASSERT(in && in->size == sizeof(uint16_t)); |
Daniel Willmann | e3adf0e | 2015-11-27 17:53:19 +0100 | [diff] [blame] | 123 | return ntohs(*(uint16_t *)in->buf); |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | uint8_t asn1str_to_u8(const OCTET_STRING_t *in) |
| 127 | { |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 128 | OSMO_ASSERT(in && in->size == sizeof(uint8_t)); |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 129 | return *(uint8_t *)in->buf; |
| 130 | } |
| 131 | |
| 132 | uint32_t asn1bitstr_to_u32(const BIT_STRING_t *in) |
| 133 | { |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 134 | OSMO_ASSERT(in && in->size == sizeof(uint32_t)); |
Harald Welte | 4dd16b9 | 2015-10-06 22:08:10 +0200 | [diff] [blame] | 135 | |
Daniel Willmann | e3adf0e | 2015-11-27 17:53:19 +0100 | [diff] [blame] | 136 | return ntohl(*(uint32_t *)in->buf); |
| 137 | } |
Daniel Willmann | 6e59d41 | 2015-11-23 14:03:04 +0100 | [diff] [blame] | 138 | |
Daniel Willmann | d6a45b4 | 2015-12-08 13:55:17 +0100 | [diff] [blame] | 139 | uint32_t asn1bitstr_to_u28(const BIT_STRING_t *in) |
| 140 | { |
| 141 | OSMO_ASSERT(in && in->size == sizeof(uint32_t) && in->bits_unused == 4); |
| 142 | |
| 143 | return ntohl(*(uint32_t *)in->buf) >> 4; |
| 144 | } |
| 145 | |
Daniel Willmann | e3adf0e | 2015-11-27 17:53:19 +0100 | [diff] [blame] | 146 | uint32_t asn1bitstr_to_u24(const BIT_STRING_t *in) |
| 147 | { |
Daniel Willmann | b2548fb | 2015-11-30 16:24:57 +0100 | [diff] [blame] | 148 | OSMO_ASSERT(in && in->size == 3); |
Daniel Willmann | 6e59d41 | 2015-11-23 14:03:04 +0100 | [diff] [blame] | 149 | |
Daniel Willmann | 9224731 | 2015-12-09 19:04:33 +0100 | [diff] [blame] | 150 | return ntohl(*(uint32_t *)in->buf) >> 8; |
Harald Welte | 27f9c4a | 2015-08-30 22:47:18 +0200 | [diff] [blame] | 151 | } |