X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcasn1%2Fsilcasn1_decode.c;h=3030d07cd4af28b3a2093f05ae2b1e58100e24f0;hb=52e57c880aba9c5e89f59d962eb9af75670b76e0;hp=f2d638b9c2118243981032b61290dc47fd0fa840;hpb=12cb6e09b64a9ecd8ee90f0243fc4af41a64238c;p=silc.git diff --git a/lib/silcasn1/silcasn1_decode.c b/lib/silcasn1/silcasn1_decode.c index f2d638b9..3030d07c 100644 --- a/lib/silcasn1/silcasn1_decode.c +++ b/lib/silcasn1/silcasn1_decode.c @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 2003 - 2005 Pekka Riikonen + Copyright (C) 2003 - 2007 Pekka Riikonen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,11 +17,10 @@ */ -#include "silcincludes.h" +#include "silc.h" #include "silcasn1.h" #include "silcber.h" - /************************** ASN.1 Decoder routines **************************/ /* Internal SEQUENCE OF and SET OF decoder. This is used only when decoding @@ -35,8 +34,7 @@ static SilcBool silc_asn1_decoder_sof(SilcAsn1 asn1, SilcBuffer src) SilcList types; SilcAsn1Tag type; SilcBuffer *retb; - SilcUInt32 *retc; - SilcAsn1Tag rtag; + SilcUInt32 *retc, rtag; const unsigned char *rdata; SilcUInt32 rdata_len, len = 0; SilcBool found = FALSE, rindef; @@ -95,7 +93,7 @@ static SilcBool silc_asn1_decoder_sof(SilcAsn1 asn1, SilcBuffer src) assert(type == SILC_ASN1_END); /* Decode the SEQUENCE or SET */ - ret = silc_ber_decode(src, NULL, NULL, (SilcUInt32 *)&rtag, &rdata, + ret = silc_ber_decode(src, NULL, NULL, &rtag, &rdata, &rdata_len, &rindef, &len); if (!ret) { SILC_LOG_DEBUG(("Error parsing BER block, malformed ASN.1 data")); @@ -109,7 +107,7 @@ static SilcBool silc_asn1_decoder_sof(SilcAsn1 asn1, SilcBuffer src) while (silc_buffer_len(src)) { /* Decode the BER data. */ - ret = silc_ber_decode(src, NULL, NULL, (SilcUInt32 *)&rtag, &rdata, + ret = silc_ber_decode(src, NULL, NULL, &rtag, &rdata, &rdata_len, &rindef, &len); if (!ret) { SILC_LOG_DEBUG(("Error parsing BER block, malformed ASN.1 data")); @@ -232,7 +230,7 @@ static SilcBool silc_asn1_decoder_sof(SilcAsn1 asn1, SilcBuffer src) ret = FALSE; \ goto fail; \ } \ - *s = silc_smalloc_ua(stack1, *s_len + 1); \ + *s = silc_smalloc(stack1, *s_len + 1); \ if (*s) { \ silc_utf8_encode(rdata, rdata_len, (enc), *s, *s_len); \ (*s)[*s_len] = '\0'; \ @@ -250,11 +248,11 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type, SilcBool primitive) { unsigned char *ptr = src->data; - SilcAsn1Tag rtype, rtag; + SilcAsn1Tag rtype; SilcAsn1Options ropts; SilcBerClass rclass; SilcBerEncoding renc; - SilcUInt32 len = 0; + SilcUInt32 len = 0, rtag; SilcBool ret, indef, rindef, found = FALSE, choice = FALSE; const unsigned char *rdata; SilcUInt32 rdata_len; @@ -296,17 +294,29 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type, indef = (opts & SILC_ASN1_INDEFINITE ? TRUE : FALSE); /* By default UNIVERSAL is implied unless the following conditions - are met when CONTEXT will apply. */ + are met when CONTEXT will apply. For SILC_ASN1_TAG_ANY_PRIMITIVE + the class is changed only if flags dictate it. */ if (ber_class == SILC_BER_CLASS_UNIVERSAL) { - if (tag != type || - opts & SILC_ASN1_IMPLICIT || - opts & SILC_ASN1_EXPLICIT) - ber_class = SILC_BER_CLASS_CONTEXT; + if (type == SILC_ASN1_TAG_ANY_PRIMITIVE) { + if (opts & SILC_ASN1_IMPLICIT || + opts & SILC_ASN1_EXPLICIT) + ber_class = SILC_BER_CLASS_CONTEXT; + } else { + if (tag != type || + opts & SILC_ASN1_IMPLICIT || + opts & SILC_ASN1_EXPLICIT) + ber_class = SILC_BER_CLASS_CONTEXT; + } } + /* Short integer is actually big integer in the BER data, so handle + it correctly */ + if (type == SILC_ASN1_TAG_SHORT_INTEGER && type == tag) + tag = SILC_ASN1_TAG_INTEGER; + /* Now decode a BER encoded block from the source buffer. It must be exactly the same user is expecting. */ - ret = silc_ber_decode(src, &rclass, &renc, (SilcUInt32 *)&rtag, &rdata, + ret = silc_ber_decode(src, &rclass, &renc, &rtag, &rdata, &rdata_len, &rindef, &len); if (!ret) { SILC_LOG_DEBUG(("Error parsing BER block, malformed ASN.1 data")); @@ -411,6 +421,16 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type, break; } + case SILC_ASN1_TAG_ANY_PRIMITIVE: + { + /* ANY_PRIMITIVE returns the raw data blob of any primitive type. */ + SILC_ASN1_VAD(asn1, opts, SilcBufferStruct, prim); + + *prim = silc_buffer_srealloc_size(stack1, *prim, rdata_len); + silc_buffer_put(*prim, rdata, rdata_len); + break; + } + case SILC_ASN1_TAG_SEQUENCE: case SILC_ASN1_TAG_SET: { @@ -455,7 +475,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type, /* 2s complement and change sign */ silc_mp_init(&z); - silc_mp_set(&z, 0); + silc_mp_set_ui(&z, 0); silc_mp_add_ui(*intval, *intval, 1); silc_mp_sub(*intval, &z, *intval); silc_mp_uninit(&z); @@ -467,6 +487,28 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type, break; } + case SILC_ASN1_TAG_SHORT_INTEGER: + { + /* Short Integer */ + SilcMPInt z; + SILC_ASN1_VAD(asn1, opts, SilcUInt32, intval); + + if (rdata_len < 1) { + SILC_LOG_DEBUG(("Malformed integer value")); + SILC_ASN1_VA_FREE(opts, intval); + ret = FALSE; + goto fail; + } + + silc_stack_push(asn1->stack1, NULL); + silc_mp_sinit(asn1->stack1, &z); + silc_mp_bin2mp((unsigned char *)rdata, rdata_len, &z); + *(*intval) = silc_mp_get_ui(&z); + silc_mp_uninit(&z); + silc_stack_pop(asn1->stack1); + break; + } + case SILC_ASN1_TAG_OID: { /* Object identifier */ @@ -477,6 +519,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type, if (rdata_len < 1) { SILC_LOG_DEBUG(("Malformed object identifier value")); + SILC_ASN1_VA_FREE(opts, oidstr); ret = FALSE; goto fail; } @@ -484,7 +527,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type, /* Set two OID values */ memset(&tmpb, 0, sizeof(tmpb)); memset(tmpstr, 0, sizeof(tmpstr)); - snprintf(tmpstr, sizeof(tmpstr) - 1, "%lu.%lu", + silc_snprintf(tmpstr, sizeof(tmpstr) - 1, "%lu.%lu", (unsigned long)(rdata[0] & 0xff) / 40, (unsigned long)(rdata[0] & 0xff) % 40); silc_buffer_sstrformat(asn1->stack1, &tmpb, tmpstr, SILC_STR_END); @@ -506,7 +549,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type, oid |= rdata[i]; memset(tmpstr, 0, sizeof(tmpstr)); - snprintf(tmpstr, sizeof(tmpstr) - 1, ".%lu", (unsigned long)oid); + silc_snprintf(tmpstr, sizeof(tmpstr) - 1, ".%lu", (unsigned long)oid); silc_buffer_sstrformat(asn1->stack1, &tmpb, tmpstr, SILC_STR_END); } *oidstr = tmpb.head; @@ -537,6 +580,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type, if (rdata_len < 2) { SILC_LOG_DEBUG(("Malformed bit string value")); + SILC_ASN1_VA_FREE(opts, d); ret = FALSE; goto fail; } @@ -563,6 +607,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type, if (rdata_len < 1) { SILC_LOG_DEBUG(("Malformed UTC time value")); + SILC_ASN1_VA_FREE(opts, t); ret = FALSE; goto fail; } @@ -584,6 +629,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type, if (rdata_len < 1) { SILC_LOG_DEBUG(("Malformed generalized time value")); + SILC_ASN1_VA_FREE(opts, t); ret = FALSE; goto fail; } @@ -591,6 +637,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type, /* Parse the time string */ if (!silc_time_generalized(rdata, *t)) { SILC_LOG_DEBUG(("Malformed generalized time value")); + SILC_ASN1_VA_FREE(opts, t); ret = FALSE; goto fail; } @@ -605,6 +652,7 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type, if (!silc_utf8_valid(rdata, rdata_len)) { SILC_LOG_DEBUG(("Malformed UTF-8 string value")); + SILC_ASN1_VA_FREE(opts, s); ret = FALSE; goto fail; } @@ -616,9 +664,10 @@ silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type, case SILC_ASN1_TAG_OCTET_STRING: { - /* Octet string. We take it as 8-bit ASCII */ + /* Octet string. Take data as is. */ SILC_ASN1_VAD_UCHAR(asn1, opts, unsigned char, s, s_len); - SILC_ASN1_DECODE_STRING(SILC_STRING_ASCII, s, s_len); + *s = silc_smemdup(stack1, rdata, rdata_len); + *s_len = rdata_len; break; } @@ -796,7 +845,6 @@ SilcBool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...) SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts); if (!type) { va_end(asn1->ap); - asn1->ap = NULL; return FALSE; } @@ -869,7 +917,6 @@ SilcBool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...) } va_end(asn1->ap); - asn1->ap = NULL; return ret; }