1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 #include "tomcrypt_private.h"
11 
12 /**
13   @file der_decode_asn1_length.c
14   ASN.1 DER, decode the ASN.1 Length field, Steffen Jaeckel
15 */
16 
17 #ifdef LTC_DER
18 /**
19   Decode the ASN.1 Length field
20   @param in       Where to read the length field from
21   @param inlen    [in/out] The size of in available/read
22   @param outlen   [out] The decoded ASN.1 length
23   @return CRYPT_OK if successful
24 */
der_decode_asn1_length(const unsigned char * in,unsigned long * inlen,unsigned long * outlen)25 int der_decode_asn1_length(const unsigned char *in, unsigned long *inlen, unsigned long *outlen)
26 {
27    unsigned long real_len, decoded_len, offset, i;
28 
29    LTC_ARGCHK(in    != NULL);
30    LTC_ARGCHK(inlen != NULL);
31 
32    if (*inlen < 1) {
33       return CRYPT_BUFFER_OVERFLOW;
34    }
35 
36    real_len = in[0];
37 
38    if (real_len < 128) {
39       decoded_len = real_len;
40       offset = 1;
41    } else {
42       real_len &= 0x7F;
43       if (real_len == 0) {
44          return CRYPT_PK_ASN1_ERROR;
45       }
46       if (real_len > sizeof(decoded_len)) {
47          return CRYPT_OVERFLOW;
48       }
49       if (real_len > (*inlen - 1)) {
50          return CRYPT_BUFFER_OVERFLOW;
51       }
52       decoded_len = 0;
53       offset = 1 + real_len;
54       for (i = 0; i < real_len; i++) {
55          decoded_len = (decoded_len << 8) | in[1 + i];
56       }
57    }
58 
59    if (outlen != NULL) *outlen = decoded_len;
60    if (decoded_len > (*inlen - offset)) return CRYPT_OVERFLOW;
61    *inlen = offset;
62 
63    return CRYPT_OK;
64 }
65 
66 #endif
67 
68 /* ref:         $Format:%D$ */
69 /* git commit:  $Format:%H$ */
70 /* commit time: $Format:%ai$ */
71