1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <openssl/asn1.h>
16 
17 #include <openssl/bytestring.h>
18 #include <openssl/err.h>
19 
20 #include "../bytestring/internal.h"
21 
22 
i2d_ASN1_BOOLEAN(ASN1_BOOLEAN a,unsigned char ** outp)23 int i2d_ASN1_BOOLEAN(ASN1_BOOLEAN a, unsigned char **outp) {
24   CBB cbb;
25   if (!CBB_init(&cbb, 3) ||  //
26       !CBB_add_asn1_bool(&cbb, a != ASN1_BOOLEAN_FALSE)) {
27     CBB_cleanup(&cbb);
28     return -1;
29   }
30   return CBB_finish_i2d(&cbb, outp);
31 }
32 
d2i_ASN1_BOOLEAN(ASN1_BOOLEAN * out,const unsigned char ** inp,long len)33 ASN1_BOOLEAN d2i_ASN1_BOOLEAN(ASN1_BOOLEAN *out, const unsigned char **inp,
34                               long len) {
35   if (len < 0) {
36     return ASN1_BOOLEAN_NONE;
37   }
38 
39   CBS cbs;
40   CBS_init(&cbs, *inp, (size_t)len);
41   int val;
42   if (!CBS_get_asn1_bool(&cbs, &val)) {
43     OPENSSL_PUT_ERROR(ASN1, ASN1_R_DECODE_ERROR);
44     return ASN1_BOOLEAN_NONE;
45   }
46 
47   ASN1_BOOLEAN ret = val ? ASN1_BOOLEAN_TRUE : ASN1_BOOLEAN_FALSE;
48   if (out != NULL) {
49     *out = ret;
50   }
51   *inp = CBS_data(&cbs);
52   return ret;
53 }
54