blob: e5337d9c4e651b4ec2e6029ca1b2abc8658affc9 [file] [log] [blame]
Kenny Root7a4adb52013-10-09 10:14:35 -07001/*
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
Kenny Root7a4adb52013-10-09 10:14:35 -070017#ifndef ASN1_DECODER_H_
18#define ASN1_DECODER_H_
19
xunchang24788852019-03-22 16:08:52 -070020#include <stddef.h>
Kenny Root7a4adb52013-10-09 10:14:35 -070021#include <stdint.h>
22
Tao Bao861c53c2017-03-20 17:09:13 -070023class asn1_context {
24 public:
25 asn1_context(const uint8_t* buffer, size_t length) : p_(buffer), length_(length), app_type_(0) {}
26 int asn1_constructed_type() const;
27 asn1_context* asn1_constructed_get();
28 bool asn1_constructed_skip_all();
29 asn1_context* asn1_sequence_get();
30 asn1_context* asn1_set_get();
31 bool asn1_sequence_next();
32 bool asn1_oid_get(const uint8_t** oid, size_t* length);
33 bool asn1_octet_string_get(const uint8_t** octet_string, size_t* length);
Kenny Root7a4adb52013-10-09 10:14:35 -070034
Tao Bao861c53c2017-03-20 17:09:13 -070035 private:
36 static constexpr int kMaskConstructed = 0xE0;
37 static constexpr int kMaskTag = 0x7F;
38 static constexpr int kMaskAppType = 0x1F;
39
40 static constexpr int kTagOctetString = 0x04;
41 static constexpr int kTagOid = 0x06;
42 static constexpr int kTagSequence = 0x30;
43 static constexpr int kTagSet = 0x31;
44 static constexpr int kTagConstructed = 0xA0;
45
46 int peek_byte() const;
47 int get_byte();
48 bool skip_bytes(size_t num_skip);
49 bool decode_length(size_t* out_len);
50
51 const uint8_t* p_;
52 size_t length_;
53 int app_type_;
54};
Kenny Root7a4adb52013-10-09 10:14:35 -070055
56#endif /* ASN1_DECODER_H_ */