New SILC PKCS API, enabling support for other public keys/certs.
[silc.git] / lib / silcasn1 / silcasn1_decode.c
index f2d638b9c2118243981032b61290dc47fd0fa840..8fdfef24720343da660036fcb69806fb2d3f6e01 100644 (file)
@@ -4,7 +4,7 @@
 
   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
@@ -296,12 +295,19 @@ 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;
+      }
     }
 
     /* Now decode a BER encoded block from the source buffer.  It must be
@@ -411,6 +417,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 +471,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);