X-Git-Url: http://git.silcnet.org/gitweb/?p=silc.git;a=blobdiff_plain;f=lib%2Fsilcasn1%2Fsilcasn1_encode.c;h=986909c4d7348cdd43e4146dc9cb9f217510e381;hp=0245185fc00686e7c2b81ec4d9a1c794af796f17;hb=ebfe5dc8641467efea3506a4797a2b1260b2da55;hpb=e4cf49012efbcc14d8f61f3c33aca73dbab55fdf diff --git a/lib/silcasn1/silcasn1_encode.c b/lib/silcasn1/silcasn1_encode.c index 0245185f..986909c4 100644 --- a/lib/silcasn1/silcasn1_encode.c +++ b/lib/silcasn1/silcasn1_encode.c @@ -4,7 +4,7 @@ Author: Pekka Riikonen - Copyright (C) 2003 - 2006 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 @@ -21,6 +21,8 @@ #include "silcasn1.h" #include "silcber.h" +#define SILC_ASN1_BUFFER_FREE(b, stack) if (!stack) silc_buffer_purge(b); + /************************** ASN.1 Encoder routines **************************/ /* Encode string from UTF-8 string to other string encodings. Encodes @@ -36,7 +38,7 @@ goto fail; \ } \ silc_stack_push(asn1->stack2, &frame); \ - s = silc_smalloc_ua(stack2, s_len + 1); \ + s = silc_smalloc(stack2, s_len + 1); \ if (s) { \ silc_utf8_decode(d, d_len, (enc), s, s_len); \ s[s_len] = '\0'; \ @@ -136,10 +138,10 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2, ret = silc_asn1_encoder(asn1, stack2, stack1, type, type, SILC_BER_CLASS_UNIVERSAL, opts, &buf, depth + 1, primitive); - silc_stack_pop(stack2); if (!ret) { SILC_LOG_DEBUG(("Error encoding explicit tag")); + silc_stack_pop(stack2); goto fail; } @@ -149,6 +151,8 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2, silc_buffer_truelen(dest) + len); ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_CONSTRUCTED, tag, buf.data, silc_buffer_len(&buf), FALSE); + SILC_ASN1_BUFFER_FREE(&buf, stack2); + silc_stack_pop(stack2); if (!ret) goto fail; if (primitive) { @@ -237,9 +241,9 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2, silc_stack_push(stack2, &frame); ret = silc_asn1_encoder(asn1, stack2, stack1, rtype, rtag, rclass, ropts, &buf, depth + 1, FALSE); - silc_stack_pop(stack2); if (!ret) { SILC_LOG_DEBUG(("Error traversing a SEQUENCE/SET")); + silc_stack_pop(stack2); goto fail; } @@ -249,6 +253,8 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2, silc_buffer_truelen(dest) + len); ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_CONSTRUCTED, tag, buf.data, silc_buffer_len(&buf), indef); + SILC_ASN1_BUFFER_FREE(&buf, stack2); + silc_stack_pop(stack2); if (!ret) goto fail; break; @@ -286,6 +292,7 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2, silc_buffer_truelen(dest) + len); ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE, tag, buf.data, silc_buffer_len(&buf), FALSE); + SILC_ASN1_BUFFER_FREE(&buf, stack2); silc_stack_pop(stack2); if (!ret) goto fail; @@ -324,6 +331,7 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2, silc_buffer_truelen(dest) + len); ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE, tag, buf.data, silc_buffer_len(&buf), FALSE); + SILC_ASN1_BUFFER_FREE(&buf, stack2); silc_stack_pop(stack2); if (!ret) goto fail; @@ -343,7 +351,7 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2, /* Get OID words from the string */ cp = strchr(oidstr, '.'); while (cp) { - if (sscanf(oidstr, "%lu", (unsigned long *)&oid) != 1) { + if (sscanf(oidstr, "%u", &oid) != 1) { SILC_LOG_DEBUG(("Malformed OID string")); goto fail; } @@ -354,7 +362,7 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2, cp = strchr(oidstr, '.'); if (!cp) { - if (sscanf(oidstr, "%lu", (unsigned long *)&oid) != 1) { + if (sscanf(oidstr, "%u", &oid) != 1) { SILC_LOG_DEBUG(("Malformed OID string")); goto fail; } @@ -414,6 +422,7 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2, silc_buffer_truelen(dest) + len); ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE, tag, buf.data, silc_buffer_len(&buf), FALSE); + SILC_ASN1_BUFFER_FREE(&buf, stack2); silc_stack_pop(stack2); if (!ret) goto fail; @@ -463,6 +472,7 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2, silc_buffer_truelen(dest) + len); ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE, tag, buf.data, silc_buffer_len(&buf), indef); + SILC_ASN1_BUFFER_FREE(&buf, stack2); silc_stack_pop(stack2); if (!ret) goto fail; @@ -555,8 +565,17 @@ silc_asn1_encoder(SilcAsn1 asn1, SilcStack stack1, SilcStack stack2, case SILC_ASN1_TAG_OCTET_STRING: { - /* Octet string. We put it in as 8-bit ASCII */ - SILC_ASN1_ENCODE_STRING(SILC_STRING_ASCII); + /* Octet string. Put data as is. */ + unsigned char *d = va_arg(asn1->ap, unsigned char *); + SilcUInt32 d_len = va_arg(asn1->ap, SilcUInt32); + + len = silc_ber_encoded_len(tag, d_len, indef); + dest = silc_buffer_srealloc_size(stack1, dest, + silc_buffer_truelen(dest) + len); + ret = silc_ber_encode(dest, ber_class, SILC_BER_ENC_PRIMITIVE, + tag, d, d_len, indef); + if (!ret) + goto fail; break; }