1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ASN1_DECODER_H_
18 #define ASN1_DECODER_H_
19 
20 #include <stdint.h>
21 
22 class asn1_context {
23  public:
asn1_context(const uint8_t * buffer,size_t length)24   asn1_context(const uint8_t* buffer, size_t length) : p_(buffer), length_(length), app_type_(0) {}
25   int asn1_constructed_type() const;
26   asn1_context* asn1_constructed_get();
27   bool asn1_constructed_skip_all();
28   asn1_context* asn1_sequence_get();
29   asn1_context* asn1_set_get();
30   bool asn1_sequence_next();
31   bool asn1_oid_get(const uint8_t** oid, size_t* length);
32   bool asn1_octet_string_get(const uint8_t** octet_string, size_t* length);
33 
34  private:
35   static constexpr int kMaskConstructed = 0xE0;
36   static constexpr int kMaskTag = 0x7F;
37   static constexpr int kMaskAppType = 0x1F;
38 
39   static constexpr int kTagOctetString = 0x04;
40   static constexpr int kTagOid = 0x06;
41   static constexpr int kTagSequence = 0x30;
42   static constexpr int kTagSet = 0x31;
43   static constexpr int kTagConstructed = 0xA0;
44 
45   int peek_byte() const;
46   int get_byte();
47   bool skip_bytes(size_t num_skip);
48   bool decode_length(size_t* out_len);
49 
50   const uint8_t* p_;
51   size_t length_;
52   int app_type_;
53 };
54 
55 #endif /* ASN1_DECODER_H_ */
56