Path: blob/main/crypto/heimdal/lib/asn1/der_get.c
107074 views
/*1* Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "der_locl.h"3435/*36* All decoding functions take a pointer `p' to first position in37* which to read, from the left, `len' which means the maximum number38* of characters we are able to read, `ret' were the value will be39* returned and `size' where the number of used bytes is stored.40* Either 0 or an error code is returned.41*/4243int44der_get_unsigned (const unsigned char *p, size_t len,45unsigned *ret, size_t *size)46{47unsigned val = 0;48size_t oldlen = len;4950if (len == sizeof(val) + 1 && p[0] == 0)51;52else if (len > sizeof(val))53return ASN1_OVERRUN;5455while (len--)56val = val * 256 + *p++;57*ret = val;58if(size) *size = oldlen;59return 0;60}6162int63der_get_unsigned64 (const unsigned char *p, size_t len,64uint64_t *ret, size_t *size)65{66uint64_t val = 0;67size_t oldlen = len;6869if (len == sizeof(val) + 1 && p[0] == 0)70;71else if (len > sizeof(val))72return ASN1_OVERRUN;7374while (len--)75val = val * 256 + *p++;76*ret = val;77if(size) *size = oldlen;78return 0;79}8081int82der_get_integer (const unsigned char *p, size_t len,83int *ret, size_t *size)84{85int val = 0;86size_t oldlen = len;8788if (len > sizeof(val))89return ASN1_OVERRUN;9091if (len > 0) {92val = (signed char)*p++;93while (--len)94val = val * 256 + *p++;95}96*ret = val;97if(size) *size = oldlen;98return 0;99}100101int102der_get_integer64 (const unsigned char *p, size_t len,103int64_t *ret, size_t *size)104{105int64_t val = 0;106size_t oldlen = len;107108if (len > sizeof(val))109return ASN1_OVERRUN;110111if (len > 0) {112val = (signed char)*p++;113while (--len)114val = val * 256 + *p++;115}116*ret = val;117if(size) *size = oldlen;118return 0;119}120121int122der_get_length (const unsigned char *p, size_t len,123size_t *val, size_t *size)124{125size_t v;126127if (len <= 0)128return ASN1_OVERRUN;129--len;130v = *p++;131if (v < 128) {132*val = v;133if(size) *size = 1;134} else {135int e;136size_t l;137unsigned tmp;138139if(v == 0x80){140*val = ASN1_INDEFINITE;141if(size) *size = 1;142return 0;143}144v &= 0x7F;145if (len < v)146return ASN1_OVERRUN;147e = der_get_unsigned (p, v, &tmp, &l);148if(e) return e;149*val = tmp;150if(size) *size = l + 1;151}152return 0;153}154155int156der_get_boolean(const unsigned char *p, size_t len, int *data, size_t *size)157{158if(len < 1)159return ASN1_OVERRUN;160if(*p != 0)161*data = 1;162else163*data = 0;164*size = 1;165return 0;166}167168int169der_get_general_string (const unsigned char *p, size_t len,170heim_general_string *str, size_t *size)171{172const unsigned char *p1;173char *s;174175p1 = memchr(p, 0, len);176if (p1 != NULL) {177/*178* Allow trailing NULs. We allow this since MIT Kerberos sends179* an strings in the NEED_PREAUTH case that includes a180* trailing NUL.181*/182while ((size_t)(p1 - p) < len && *p1 == '\0')183p1++;184if ((size_t)(p1 - p) != len)185return ASN1_BAD_CHARACTER;186}187if (len > len + 1)188return ASN1_BAD_LENGTH;189190s = malloc (len + 1);191if (s == NULL)192return ENOMEM;193memcpy (s, p, len);194s[len] = '\0';195*str = s;196if(size) *size = len;197return 0;198}199200int201der_get_utf8string (const unsigned char *p, size_t len,202heim_utf8_string *str, size_t *size)203{204return der_get_general_string(p, len, str, size);205}206207int208der_get_printable_string(const unsigned char *p, size_t len,209heim_printable_string *str, size_t *size)210{211str->length = len;212str->data = malloc(len + 1);213if (str->data == NULL)214return ENOMEM;215memcpy(str->data, p, len);216((char *)str->data)[len] = '\0';217if(size) *size = len;218return 0;219}220221int222der_get_ia5_string(const unsigned char *p, size_t len,223heim_ia5_string *str, size_t *size)224{225return der_get_printable_string(p, len, str, size);226}227228int229der_get_bmp_string (const unsigned char *p, size_t len,230heim_bmp_string *data, size_t *size)231{232size_t i;233234if (len & 1)235return ASN1_BAD_FORMAT;236data->length = len / 2;237if (data->length > UINT_MAX/sizeof(data->data[0]))238return ERANGE;239data->data = malloc(data->length * sizeof(data->data[0]));240if (data->data == NULL && data->length != 0)241return ENOMEM;242243for (i = 0; i < data->length; i++) {244data->data[i] = (p[0] << 8) | p[1];245p += 2;246/* check for NUL in the middle of the string */247if (data->data[i] == 0 && i != (data->length - 1)) {248free(data->data);249data->data = NULL;250data->length = 0;251return ASN1_BAD_CHARACTER;252}253}254if (size) *size = len;255256return 0;257}258259int260der_get_universal_string (const unsigned char *p, size_t len,261heim_universal_string *data, size_t *size)262{263size_t i;264265if (len & 3)266return ASN1_BAD_FORMAT;267data->length = len / 4;268if (data->length > UINT_MAX/sizeof(data->data[0]))269return ERANGE;270data->data = malloc(data->length * sizeof(data->data[0]));271if (data->data == NULL && data->length != 0)272return ENOMEM;273274for (i = 0; i < data->length; i++) {275data->data[i] = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];276p += 4;277/* check for NUL in the middle of the string */278if (data->data[i] == 0 && i != (data->length - 1)) {279free(data->data);280data->data = NULL;281data->length = 0;282return ASN1_BAD_CHARACTER;283}284}285if (size) *size = len;286return 0;287}288289int290der_get_visible_string (const unsigned char *p, size_t len,291heim_visible_string *str, size_t *size)292{293return der_get_general_string(p, len, str, size);294}295296int297der_get_octet_string (const unsigned char *p, size_t len,298heim_octet_string *data, size_t *size)299{300data->length = len;301data->data = malloc(len);302if (data->data == NULL && data->length != 0)303return ENOMEM;304memcpy (data->data, p, len);305if(size) *size = len;306return 0;307}308309int310der_get_octet_string_ber (const unsigned char *p, size_t len,311heim_octet_string *data, size_t *size)312{313int e;314Der_type type;315Der_class class;316unsigned int tag, depth = 0;317size_t l, datalen, oldlen = len;318319data->length = 0;320data->data = NULL;321322while (len) {323e = der_get_tag (p, len, &class, &type, &tag, &l);324if (e) goto out;325if (class != ASN1_C_UNIV) {326e = ASN1_BAD_ID;327goto out;328}329if (type == PRIM && tag == UT_EndOfContent) {330if (depth == 0)331break;332depth--;333}334if (tag != UT_OctetString) {335e = ASN1_BAD_ID;336goto out;337}338339p += l;340len -= l;341e = der_get_length (p, len, &datalen, &l);342if (e) goto out;343p += l;344len -= l;345346if (datalen > len)347return ASN1_OVERRUN;348349if (type == PRIM) {350void *ptr;351352ptr = realloc(data->data, data->length + datalen);353if (ptr == NULL) {354e = ENOMEM;355goto out;356}357data->data = ptr;358memcpy(((unsigned char *)data->data) + data->length, p, datalen);359data->length += datalen;360} else361depth++;362363p += datalen;364len -= datalen;365}366if (depth != 0)367return ASN1_INDEF_OVERRUN;368if(size) *size = oldlen - len;369return 0;370out:371free(data->data);372data->data = NULL;373data->length = 0;374return e;375}376377378int379der_get_heim_integer (const unsigned char *p, size_t len,380heim_integer *data, size_t *size)381{382data->length = 0;383data->negative = 0;384data->data = NULL;385386if (len == 0) {387if (size)388*size = 0;389return 0;390}391if (p[0] & 0x80) {392unsigned char *q;393int carry = 1;394data->negative = 1;395396data->length = len;397398if (p[0] == 0xff) {399p++;400data->length--;401}402data->data = malloc(data->length);403if (data->data == NULL) {404data->length = 0;405if (size)406*size = 0;407return ENOMEM;408}409q = &((unsigned char*)data->data)[data->length - 1];410p += data->length - 1;411while (q >= (unsigned char*)data->data) {412*q = *p ^ 0xff;413if (carry)414carry = !++*q;415p--;416q--;417}418} else {419data->negative = 0;420data->length = len;421422if (p[0] == 0) {423p++;424data->length--;425}426data->data = malloc(data->length);427if (data->data == NULL && data->length != 0) {428data->length = 0;429if (size)430*size = 0;431return ENOMEM;432}433memcpy(data->data, p, data->length);434}435if (size)436*size = len;437return 0;438}439440static int441generalizedtime2time (const char *s, time_t *t)442{443struct tm tm;444445memset(&tm, 0, sizeof(tm));446if (sscanf (s, "%04d%02d%02d%02d%02d%02dZ",447&tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,448&tm.tm_min, &tm.tm_sec) != 6) {449if (sscanf (s, "%02d%02d%02d%02d%02d%02dZ",450&tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour,451&tm.tm_min, &tm.tm_sec) != 6)452return ASN1_BAD_TIMEFORMAT;453if (tm.tm_year < 50)454tm.tm_year += 2000;455else456tm.tm_year += 1900;457}458tm.tm_year -= 1900;459tm.tm_mon -= 1;460*t = _der_timegm (&tm);461return 0;462}463464static int465der_get_time (const unsigned char *p, size_t len,466time_t *data, size_t *size)467{468char *times;469int e;470471if (len > len + 1 || len == 0)472return ASN1_BAD_LENGTH;473474times = malloc(len + 1);475if (times == NULL)476return ENOMEM;477memcpy(times, p, len);478times[len] = '\0';479e = generalizedtime2time(times, data);480free (times);481if(size) *size = len;482return e;483}484485int486der_get_generalized_time (const unsigned char *p, size_t len,487time_t *data, size_t *size)488{489return der_get_time(p, len, data, size);490}491492int493der_get_utctime (const unsigned char *p, size_t len,494time_t *data, size_t *size)495{496return der_get_time(p, len, data, size);497}498499int500der_get_oid (const unsigned char *p, size_t len,501heim_oid *data, size_t *size)502{503size_t n;504size_t oldlen = len;505506if (len < 1)507return ASN1_OVERRUN;508509if (len > len + 1)510return ASN1_BAD_LENGTH;511512if (len + 1 > UINT_MAX/sizeof(data->components[0]))513return ERANGE;514515data->components = malloc((len + 1) * sizeof(data->components[0]));516if (data->components == NULL)517return ENOMEM;518data->components[0] = (*p) / 40;519data->components[1] = (*p) % 40;520--len;521++p;522for (n = 2; len > 0; ++n) {523unsigned u = 0, u1;524525do {526--len;527u1 = u * 128 + (*p++ % 128);528/* check that we don't overflow the element */529if (u1 < u) {530der_free_oid(data);531return ASN1_OVERRUN;532}533u = u1;534} while (len > 0 && p[-1] & 0x80);535data->components[n] = u;536}537if (n > 2 && p[-1] & 0x80) {538der_free_oid (data);539return ASN1_OVERRUN;540}541data->length = n;542if (size)543*size = oldlen;544return 0;545}546547int548der_get_tag (const unsigned char *p, size_t len,549Der_class *class, Der_type *type,550unsigned int *tag, size_t *size)551{552size_t ret = 0;553if (len < 1)554return ASN1_OVERRUN;555*class = (Der_class)(((*p) >> 6) & 0x03);556*type = (Der_type)(((*p) >> 5) & 0x01);557*tag = (*p) & 0x1f;558p++; len--; ret++;559if(*tag == 0x1f) {560unsigned int continuation;561unsigned int tag1;562*tag = 0;563do {564if(len < 1)565return ASN1_OVERRUN;566continuation = *p & 128;567tag1 = *tag * 128 + (*p % 128);568/* check that we don't overflow the tag */569if (tag1 < *tag)570return ASN1_OVERFLOW;571*tag = tag1;572p++; len--; ret++;573} while(continuation);574}575if(size) *size = ret;576return 0;577}578579int580der_match_tag (const unsigned char *p, size_t len,581Der_class class, Der_type type,582unsigned int tag, size_t *size)583{584Der_type thistype;585int e;586587e = der_match_tag2(p, len, class, &thistype, tag, size);588if (e) return e;589if (thistype != type) return ASN1_BAD_ID;590return 0;591}592593int594der_match_tag2 (const unsigned char *p, size_t len,595Der_class class, Der_type *type,596unsigned int tag, size_t *size)597{598size_t l;599Der_class thisclass;600unsigned int thistag;601int e;602603e = der_get_tag (p, len, &thisclass, type, &thistag, &l);604if (e) return e;605if (class != thisclass)606return ASN1_BAD_ID;607if(tag > thistag)608return ASN1_MISPLACED_FIELD;609if(tag < thistag)610return ASN1_MISSING_FIELD;611if(size) *size = l;612return 0;613}614615int616der_match_tag_and_length (const unsigned char *p, size_t len,617Der_class class, Der_type *type, unsigned int tag,618size_t *length_ret, size_t *size)619{620size_t l, ret = 0;621int e;622623e = der_match_tag2 (p, len, class, type, tag, &l);624if (e) return e;625p += l;626len -= l;627ret += l;628e = der_get_length (p, len, length_ret, &l);629if (e) return e;630if(size) *size = ret + l;631return 0;632}633634635636/*637* Old versions of DCE was based on a very early beta of the MIT code,638* which used MAVROS for ASN.1 encoding. MAVROS had the interesting639* feature that it encoded data in the forward direction, which has640* it's problems, since you have no idea how long the data will be641* until after you're done. MAVROS solved this by reserving one byte642* for length, and later, if the actual length was longer, it reverted643* to indefinite, BER style, lengths. The version of MAVROS used by644* the DCE people could apparently generate correct X.509 DER encodings, and645* did this by making space for the length after encoding, but646* unfortunately this feature wasn't used with Kerberos.647*/648649int650_heim_fix_dce(size_t reallen, size_t *len)651{652if(reallen == ASN1_INDEFINITE)653return 1;654if(*len < reallen)655return -1;656*len = reallen;657return 0;658}659660int661der_get_bit_string (const unsigned char *p, size_t len,662heim_bit_string *data, size_t *size)663{664if (len < 1)665return ASN1_OVERRUN;666if (p[0] > 7)667return ASN1_BAD_FORMAT;668if (len - 1 == 0 && p[0] != 0)669return ASN1_BAD_FORMAT;670/* check if any of the three upper bits are set671* any of them will cause a interger overrun */672if ((len - 1) >> (sizeof(len) * 8 - 3))673return ASN1_OVERRUN;674data->length = (len - 1) * 8;675data->data = malloc(len - 1);676if (data->data == NULL && (len - 1) != 0)677return ENOMEM;678/* copy data is there is data to copy */679if (len - 1 != 0) {680memcpy (data->data, p + 1, len - 1);681data->length -= p[0];682}683if(size) *size = len;684return 0;685}686687688