~ubuntu-branches/ubuntu/utopic/dropbear/utopic-proposed

« back to all changes in this revision

Viewing changes to libtomcrypt/src/pk/asn1/der/sequence/der_length_sequence.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Johnston
  • Date: 2005-12-08 19:20:21 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051208192021-nyp9rwnt77nsg6ty
Tags: 0.47-1
* New upstream release.
* SECURITY: Fix incorrect buffer sizing.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
 
2
 *
 
3
 * LibTomCrypt is a library that provides various cryptographic
 
4
 * algorithms in a highly modular and flexible manner.
 
5
 *
 
6
 * The library is free for all purposes without any express
 
7
 * guarantee it works.
 
8
 *
 
9
 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
 
10
 */
 
11
#include "tomcrypt.h"
 
12
#include <stdarg.h>
 
13
 
 
14
 
 
15
/**
 
16
  @file der_length_sequence.c
 
17
  ASN.1 DER, length a SEQUENCE, Tom St Denis
 
18
*/
 
19
 
 
20
#ifdef LTC_DER
 
21
 
 
22
/**
 
23
   Get the length of a DER sequence 
 
24
   @param list   The sequences of items in the SEQUENCE
 
25
   @param inlen  The number of items
 
26
   @param outlen [out] The length required in octets to store it 
 
27
   @return CRYPT_OK on success
 
28
*/
 
29
int der_length_sequence(ltc_asn1_list *list, unsigned long inlen,
 
30
                        unsigned long *outlen) 
 
31
{
 
32
   int           err, type;
 
33
   unsigned long size, x, y, z, i;
 
34
   void          *data;
 
35
 
 
36
   LTC_ARGCHK(list    != NULL);
 
37
   LTC_ARGCHK(outlen  != NULL);
 
38
 
 
39
   /* get size of output that will be required */
 
40
   y = 0;
 
41
   for (i = 0; i < inlen; i++) {
 
42
       type = list[i].type;
 
43
       size = list[i].size;
 
44
       data = list[i].data;
 
45
 
 
46
       if (type == LTC_ASN1_EOL) { 
 
47
          break;
 
48
       }
 
49
 
 
50
       switch (type) {
 
51
           case LTC_ASN1_INTEGER:
 
52
               if ((err = der_length_integer(data, &x)) != CRYPT_OK) {
 
53
                  goto LBL_ERR;
 
54
               }
 
55
               y += x;
 
56
               break;
 
57
 
 
58
           case LTC_ASN1_SHORT_INTEGER:
 
59
               if ((err = der_length_short_integer(*((unsigned long *)data), &x)) != CRYPT_OK) {
 
60
                  goto LBL_ERR;
 
61
               }
 
62
               y += x;
 
63
               break;
 
64
 
 
65
           case LTC_ASN1_BIT_STRING:
 
66
               if ((err = der_length_bit_string(size, &x)) != CRYPT_OK) {
 
67
                  goto LBL_ERR;
 
68
               }
 
69
               y += x;
 
70
               break;
 
71
 
 
72
           case LTC_ASN1_OCTET_STRING:
 
73
               if ((err = der_length_octet_string(size, &x)) != CRYPT_OK) {
 
74
                  goto LBL_ERR;
 
75
               }
 
76
               y += x;
 
77
               break;
 
78
 
 
79
           case LTC_ASN1_NULL:
 
80
               y += 2;
 
81
               break;
 
82
 
 
83
           case LTC_ASN1_OBJECT_IDENTIFIER:
 
84
               if ((err = der_length_object_identifier(data, size, &x)) != CRYPT_OK) {
 
85
                  goto LBL_ERR;
 
86
               }
 
87
               y += x;
 
88
               break;
 
89
 
 
90
           case LTC_ASN1_IA5_STRING:
 
91
               if ((err = der_length_ia5_string(data, size, &x)) != CRYPT_OK) {
 
92
                  goto LBL_ERR;
 
93
               }
 
94
               y += x;
 
95
               break;
 
96
 
 
97
           case LTC_ASN1_PRINTABLE_STRING:
 
98
               if ((err = der_length_printable_string(data, size, &x)) != CRYPT_OK) {
 
99
                  goto LBL_ERR;
 
100
               }
 
101
               y += x;
 
102
               break;
 
103
 
 
104
           case LTC_ASN1_SEQUENCE:
 
105
               if ((err = der_length_sequence(data, size, &x)) != CRYPT_OK) {
 
106
                  goto LBL_ERR;
 
107
               }
 
108
               y += x;
 
109
               break;
 
110
 
 
111
          
 
112
           default:
 
113
               err = CRYPT_INVALID_ARG;
 
114
               goto LBL_ERR;
 
115
       }
 
116
   }
 
117
 
 
118
   /* calc header size */
 
119
   z = y;
 
120
   if (y < 128) {
 
121
      y += 2;
 
122
   } else if (y < 256) {
 
123
      /* 0x30 0x81 LL */
 
124
      y += 3;
 
125
   } else if (y < 65536UL) {
 
126
      /* 0x30 0x82 LL LL */
 
127
      y += 4;
 
128
   } else if (y < 16777216UL) {
 
129
      /* 0x30 0x83 LL LL LL */
 
130
      y += 5;
 
131
   } else {
 
132
      err = CRYPT_INVALID_ARG;
 
133
      goto LBL_ERR;
 
134
   }
 
135
 
 
136
   /* store size */
 
137
   *outlen = y;
 
138
   err     = CRYPT_OK;
 
139
 
 
140
LBL_ERR:
 
141
   return err;
 
142
}
 
143
 
 
144
#endif