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_boolean.c
14   ASN.1 DER, decode a BOOLEAN, Tom St Denis
15 */
16 
17 
18 #ifdef LTC_DER
19 
20 /**
21   Read a BOOLEAN
22   @param in      The destination for the DER encoded BOOLEAN
23   @param inlen   The size of the DER BOOLEAN
24   @param out     [out]  The boolean to decode
25   @return CRYPT_OK if successful
26 */
der_decode_boolean(const unsigned char * in,unsigned long inlen,int * out)27 int der_decode_boolean(const unsigned char *in, unsigned long inlen,
28                                        int *out)
29 {
30    LTC_ARGCHK(in  != NULL);
31    LTC_ARGCHK(out != NULL);
32 
33    if (inlen < 3 || in[0] != 0x01 || in[1] != 0x01 || (in[2] != 0x00 && in[2] != 0xFF)) {
34       return CRYPT_INVALID_ARG;
35    }
36 
37    *out = (in[2]==0xFF) ? 1 : 0;
38 
39    return CRYPT_OK;
40 }
41 
42 #endif
43 
44 /* ref:         $Format:%D$ */
45 /* git commit:  $Format:%H$ */
46 /* commit time: $Format:%ai$ */
47