diff libtomcrypt/src/pk/asn1/der/sequence/der_encode_sequence_ex.c @ 1471:6dba84798cd5

Update to libtomcrypt 1.18.1, merged with Dropbear changes
author Matt Johnston <matt@ucc.asn.au>
date Fri, 09 Feb 2018 21:44:05 +0800
parents f849a5ca2efc
children
line wrap: on
line diff
--- a/libtomcrypt/src/pk/asn1/der/sequence/der_encode_sequence_ex.c	Thu Feb 08 23:11:40 2018 +0800
+++ b/libtomcrypt/src/pk/asn1/der/sequence/der_encode_sequence_ex.c	Fri Feb 09 21:44:05 2018 +0800
@@ -5,11 +5,8 @@
  *
  * The library is free for all purposes without any express
  * guarantee it works.
- *
- * Tom St Denis, [email protected], http://libtom.org
  */
 #include "tomcrypt.h"
-#include <stdarg.h>
 
 
 /**
@@ -23,15 +20,16 @@
    Encode a SEQUENCE
    @param list      The list of items to encode
    @param inlen     The number of items in the list
-   @param out       [out] The destination 
+   @param out       [out] The destination
    @param outlen    [in/out] The size of the output
    @param type_of   LTC_ASN1_SEQUENCE or LTC_ASN1_SET/LTC_ASN1_SETOF
    @return CRYPT_OK on success
 */
 int der_encode_sequence_ex(ltc_asn1_list *list, unsigned long inlen,
-                           unsigned char *out,  unsigned long *outlen, int type_of) 
+                           unsigned char *out,  unsigned long *outlen, int type_of)
 {
-   int           err, type;
+   int           err;
+   ltc_asn1_type type;
    unsigned long size, x, y, z, i;
    void          *data;
 
@@ -40,123 +38,8 @@
    LTC_ARGCHK(outlen  != NULL);
 
    /* get size of output that will be required */
-   y = 0;
-   for (i = 0; i < inlen; i++) {
-       type = list[i].type;
-       size = list[i].size;
-       data = list[i].data;
-
-       if (type == LTC_ASN1_EOL) { 
-          break;
-       }
-
-       switch (type) {
-            case LTC_ASN1_BOOLEAN:
-               if ((err = der_length_boolean(&x)) != CRYPT_OK) {
-                  goto LBL_ERR;
-               }
-               y += x;
-               break;
-
-           case LTC_ASN1_INTEGER:
-               if ((err = der_length_integer(data, &x)) != CRYPT_OK) {
-                  goto LBL_ERR;
-               }
-               y += x;
-               break;
-
-           case LTC_ASN1_SHORT_INTEGER:
-               if ((err = der_length_short_integer(*((unsigned long*)data), &x)) != CRYPT_OK) {
-                  goto LBL_ERR;
-               }
-               y += x;
-               break;
-
-           case LTC_ASN1_BIT_STRING:
-               if ((err = der_length_bit_string(size, &x)) != CRYPT_OK) {
-                  goto LBL_ERR;
-               }
-               y += x;
-               break;
-
-           case LTC_ASN1_OCTET_STRING:
-               if ((err = der_length_octet_string(size, &x)) != CRYPT_OK) {
-                  goto LBL_ERR;
-               }
-               y += x;
-               break;
-
-           case LTC_ASN1_NULL:
-               y += 2;
-               break;
-
-           case LTC_ASN1_OBJECT_IDENTIFIER:
-               if ((err = der_length_object_identifier(data, size, &x)) != CRYPT_OK) {
-                  goto LBL_ERR;
-               }
-               y += x;
-               break;
-
-           case LTC_ASN1_IA5_STRING:
-               if ((err = der_length_ia5_string(data, size, &x)) != CRYPT_OK) {
-                  goto LBL_ERR;
-               }
-               y += x;
-               break;
-
-           case LTC_ASN1_PRINTABLE_STRING:
-               if ((err = der_length_printable_string(data, size, &x)) != CRYPT_OK) {
-                  goto LBL_ERR;
-               }
-               y += x;
-               break;
-
-           case LTC_ASN1_UTF8_STRING:
-               if ((err = der_length_utf8_string(data, size, &x)) != CRYPT_OK) {
-                  goto LBL_ERR;
-               }
-               y += x;
-               break;
-
-           case LTC_ASN1_UTCTIME:
-               if ((err = der_length_utctime(data, &x)) != CRYPT_OK) {
-                  goto LBL_ERR;
-               }
-               y += x;
-               break;
-
-           case LTC_ASN1_SET:
-           case LTC_ASN1_SETOF:
-           case LTC_ASN1_SEQUENCE:
-               if ((err = der_length_sequence(data, size, &x)) != CRYPT_OK) {
-                  goto LBL_ERR;
-               }
-               y += x;
-               break;
-          
-           default:
-               err = CRYPT_INVALID_ARG;
-               goto LBL_ERR;
-       }
-   }
-
-   /* calc header size */
-   z = y;
-   if (y < 128) {
-      y += 2;
-   } else if (y < 256) {
-      /* 0x30 0x81 LL */
-      y += 3;
-   } else if (y < 65536UL) {
-      /* 0x30 0x82 LL LL */
-      y += 4;
-   } else if (y < 16777216UL) {
-      /* 0x30 0x83 LL LL LL */
-      y += 5;
-   } else {
-      err = CRYPT_INVALID_ARG;
-      goto LBL_ERR;
-   }
+   y = 0; z = 0;
+   if ((err = der_length_sequence_ex(list, inlen, &y, &z)) != CRYPT_OK) return CRYPT_INVALID_ARG;
 
    /* too big ? */
    if (*outlen < y) {
@@ -168,7 +51,7 @@
    /* store header */
    x = 0;
    out[x++] = (type_of == LTC_ASN1_SEQUENCE) ? 0x30 : 0x31;
-      
+
    if (z < 128) {
       out[x++] = (unsigned char)z;
    } else if (z < 256) {
@@ -192,7 +75,7 @@
        size = list[i].size;
        data = list[i].data;
 
-       if (type == LTC_ASN1_EOL) { 
+       if (type == LTC_ASN1_EOL) {
           break;
        }
 
@@ -202,17 +85,13 @@
                if ((err = der_encode_boolean(*((int *)data), out + x, &z)) != CRYPT_OK) {
                   goto LBL_ERR;
                }
-               x       += z;
-               *outlen -= z;
                break;
-          
+
            case LTC_ASN1_INTEGER:
                z = *outlen;
                if ((err = der_encode_integer(data, out + x, &z)) != CRYPT_OK) {
                   goto LBL_ERR;
                }
-               x       += z;
-               *outlen -= z;
                break;
 
            case LTC_ASN1_SHORT_INTEGER:
@@ -220,8 +99,6 @@
                if ((err = der_encode_short_integer(*((unsigned long*)data), out + x, &z)) != CRYPT_OK) {
                   goto LBL_ERR;
                }
-               x       += z;
-               *outlen -= z;
                break;
 
            case LTC_ASN1_BIT_STRING:
@@ -229,8 +106,13 @@
                if ((err = der_encode_bit_string(data, size, out + x, &z)) != CRYPT_OK) {
                   goto LBL_ERR;
                }
-               x       += z;
-               *outlen -= z;
+               break;
+
+           case LTC_ASN1_RAW_BIT_STRING:
+               z = *outlen;
+               if ((err = der_encode_raw_bit_string(data, size, out + x, &z)) != CRYPT_OK) {
+                  goto LBL_ERR;
+               }
                break;
 
            case LTC_ASN1_OCTET_STRING:
@@ -238,14 +120,12 @@
                if ((err = der_encode_octet_string(data, size, out + x, &z)) != CRYPT_OK) {
                   goto LBL_ERR;
                }
-               x       += z;
-               *outlen -= z;
                break;
 
            case LTC_ASN1_NULL:
-               out[x++] = 0x05;
-               out[x++] = 0x00;
-               *outlen -= 2;
+               out[x] = 0x05;
+               out[x+1] = 0x00;
+               z = 2;
                break;
 
            case LTC_ASN1_OBJECT_IDENTIFIER:
@@ -253,8 +133,6 @@
                if ((err = der_encode_object_identifier(data, size, out + x, &z)) != CRYPT_OK) {
                   goto LBL_ERR;
                }
-               x       += z;
-               *outlen -= z;
                break;
 
            case LTC_ASN1_IA5_STRING:
@@ -262,17 +140,13 @@
                if ((err = der_encode_ia5_string(data, size, out + x, &z)) != CRYPT_OK) {
                   goto LBL_ERR;
                }
-               x       += z;
-               *outlen -= z;
                break;
-          
+
            case LTC_ASN1_PRINTABLE_STRING:
                z = *outlen;
                if ((err = der_encode_printable_string(data, size, out + x, &z)) != CRYPT_OK) {
                   goto LBL_ERR;
                }
-               x       += z;
-               *outlen -= z;
                break;
 
            case LTC_ASN1_UTF8_STRING:
@@ -280,8 +154,6 @@
                if ((err = der_encode_utf8_string(data, size, out + x, &z)) != CRYPT_OK) {
                   goto LBL_ERR;
                }
-               x       += z;
-               *outlen -= z;
                break;
 
            case LTC_ASN1_UTCTIME:
@@ -289,8 +161,13 @@
                if ((err = der_encode_utctime(data, out + x, &z)) != CRYPT_OK) {
                   goto LBL_ERR;
                }
-               x       += z;
-               *outlen -= z;
+               break;
+
+           case LTC_ASN1_GENERALIZEDTIME:
+               z = *outlen;
+               if ((err = der_encode_generalizedtime(data, out + x, &z)) != CRYPT_OK) {
+                  goto LBL_ERR;
+               }
                break;
 
            case LTC_ASN1_SET:
@@ -298,8 +175,6 @@
                if ((err = der_encode_set(data, size, out + x, &z)) != CRYPT_OK) {
                   goto LBL_ERR;
                }
-               x       += z;
-               *outlen -= z;
                break;
 
            case LTC_ASN1_SETOF:
@@ -307,8 +182,6 @@
                if ((err = der_encode_setof(data, size, out + x, &z)) != CRYPT_OK) {
                   goto LBL_ERR;
                }
-               x       += z;
-               *outlen -= z;
                break;
 
            case LTC_ASN1_SEQUENCE:
@@ -316,20 +189,29 @@
                if ((err = der_encode_sequence_ex(data, size, out + x, &z, type)) != CRYPT_OK) {
                   goto LBL_ERR;
                }
-               x       += z;
-               *outlen -= z;
                break;
-           
-           default:
+
+           case LTC_ASN1_CHOICE:
+           case LTC_ASN1_CONSTRUCTED:
+           case LTC_ASN1_CONTEXT_SPECIFIC:
+           case LTC_ASN1_EOL:
+           case LTC_ASN1_TELETEX_STRING:
                err = CRYPT_INVALID_ARG;
                goto LBL_ERR;
        }
+
+       x       += z;
+       *outlen -= z;
    }
    *outlen = x;
-   err = CRYPT_OK;   
+   err = CRYPT_OK;
 
 LBL_ERR:
    return err;
 }
 
 #endif
+
+/* ref:         $Format:%D$ */
+/* git commit:  $Format:%H$ */
+/* commit time: $Format:%ai$ */