blob: e5337d9c4e651b4ec2e6029ca1b2abc8658affc9 [file] [log] [blame]
bigbiffd58ba182020-03-23 10:02:29 -04001/*
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
bigbiff1f9e4842020-10-31 11:33:15 -040020#include <stddef.h>
bigbiffd58ba182020-03-23 10:02:29 -040021#include <stdint.h>
22
23class 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);
34
35 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};
55
56#endif /* ASN1_DECODER_H_ */