Author: Pekka Riikonen <priikone@silcnet.org>
- Copyright (C) 2003 - 2005 Pekka Riikonen
+ Copyright (C) 2003 - 2006 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
*/
-#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
silc_asn1_decoder function. This parses the sequence of types and returns
them as raw BER buffers in an array of SilcBuffers. */
-static bool silc_asn1_decoder_sof(SilcAsn1 asn1, SilcBuffer src)
+static SilcBool silc_asn1_decoder_sof(SilcAsn1 asn1, SilcBuffer src)
{
- bool ret = FALSE;
+ SilcBool ret = FALSE;
SilcList types;
SilcAsn1Tag type;
SilcBuffer *retb;
SilcAsn1Tag rtag;
const unsigned char *rdata;
SilcUInt32 rdata_len, len = 0;
- bool found = FALSE, rindef;
+ SilcBool found = FALSE, rindef;
struct SilcAsn1SofStruct {
SilcAsn1Tag type;
arguments (either very first or first for recursion) for a type.
The `depth' includes the current depth of recursion. */
-static bool
+static SilcBool
silc_asn1_decoder(SilcAsn1 asn1, SilcStack stack1, SilcAsn1Tag type,
SilcAsn1Tag tag, SilcBerClass ber_class,
SilcAsn1Options opts, SilcBuffer src, SilcUInt32 depth,
- bool primitive)
+ SilcBool primitive)
{
unsigned char *ptr = src->data;
SilcAsn1Tag rtype, rtag;
SilcBerClass rclass;
SilcBerEncoding renc;
SilcUInt32 len = 0;
- bool ret, indef, rindef, found = FALSE, choice = FALSE;
+ SilcBool ret, indef, rindef, found = FALSE, choice = FALSE;
const unsigned char *rdata;
SilcUInt32 rdata_len;
int i;
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,
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:
{
/* 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);
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 */
if (rdata_len < 1) {
SILC_LOG_DEBUG(("Malformed object identifier value"));
+ SILC_ASN1_VA_FREE(opts, oidstr);
ret = FALSE;
goto fail;
}
/* 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);
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;
case SILC_ASN1_TAG_BOOLEAN:
{
/* Decode boolean (TRUE/FALSE) value */
- SILC_ASN1_VAD(asn1, opts, bool, val);
+ SILC_ASN1_VAD(asn1, opts, SilcBool, val);
if (rdata_len != 1) {
SILC_LOG_DEBUG(("Malformed boolean value"));
if (rdata_len < 2) {
SILC_LOG_DEBUG(("Malformed bit string value"));
+ SILC_ASN1_VA_FREE(opts, d);
ret = FALSE;
goto fail;
}
if (rdata_len < 1) {
SILC_LOG_DEBUG(("Malformed UTC time value"));
+ SILC_ASN1_VA_FREE(opts, t);
ret = FALSE;
goto fail;
}
if (rdata_len < 1) {
SILC_LOG_DEBUG(("Malformed generalized time value"));
+ SILC_ASN1_VA_FREE(opts, t);
ret = FALSE;
goto fail;
}
/* 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;
}
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;
}
return ret;
}
-bool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...)
+SilcBool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...)
{
SilcAsn1Tag type, tag;
SilcAsn1Options opts;
SilcBerClass ber_class;
SilcStackFrame frame1, frame2;
SilcStack stack1 = NULL, stack2 = NULL;
- bool ret;
+ SilcBool ret;
if (!asn1)
return FALSE;
SILC_ASN1_ARGS(asn1, type, tag, ber_class, opts);
if (!type) {
va_end(asn1->ap);
- asn1->ap = NULL;
return FALSE;
}
}
va_end(asn1->ap);
- asn1->ap = NULL;
return ret;
}