Browse Source

Refactor asn1_decoder functions into a class.

Test: mmma bootable/recovery
Test: recovery_unit_test passes.
Test: recovery_component_test passes.
Change-Id: If0bf25993158eaebeedff55ba4f4dd0f6e5f937d
Tao Bao 4 years ago
parent
commit
861c53c6c5
4 changed files with 361 additions and 410 deletions
  1. 110
    143
      asn1_decoder.cpp
  2. 32
    13
      asn1_decoder.h
  3. 180
    217
      tests/unit/asn1_decoder_test.cpp
  4. 39
    37
      verifier.cpp

+ 110
- 143
asn1_decoder.cpp View File

@@ -14,178 +14,145 @@
14 14
  * limitations under the License.
15 15
  */
16 16
 
17
-#include <malloc.h>
18
-#include <stdint.h>
19
-#include <string.h>
20
-
21 17
 #include "asn1_decoder.h"
22 18
 
19
+#include <stdint.h>
23 20
 
24
-typedef struct asn1_context {
25
-  size_t length;
26
-  const uint8_t* p;
27
-  int app_type;
28
-} asn1_context_t;
29
-
30
-
31
-static const int kMaskConstructed = 0xE0;
32
-static const int kMaskTag = 0x7F;
33
-static const int kMaskAppType = 0x1F;
34
-
35
-static const int kTagOctetString = 0x04;
36
-static const int kTagOid = 0x06;
37
-static const int kTagSequence = 0x30;
38
-static const int kTagSet = 0x31;
39
-static const int kTagConstructed = 0xA0;
40
-
41
-asn1_context_t* asn1_context_new(const uint8_t* buffer, size_t length) {
42
-    asn1_context_t* ctx = (asn1_context_t*) calloc(1, sizeof(asn1_context_t));
43
-    if (ctx == NULL) {
44
-        return NULL;
45
-    }
46
-    ctx->p = buffer;
47
-    ctx->length = length;
48
-    return ctx;
49
-}
50
-
51
-void asn1_context_free(asn1_context_t* ctx) {
52
-    free(ctx);
21
+int asn1_context::peek_byte() const {
22
+  if (length_ <= 0) {
23
+    return -1;
24
+  }
25
+  return *p_;
53 26
 }
54 27
 
55
-static inline int peek_byte(asn1_context_t* ctx) {
56
-    if (ctx->length <= 0) {
57
-        return -1;
58
-    }
59
-    return *ctx->p;
60
-}
28
+int asn1_context::get_byte() {
29
+  if (length_ <= 0) {
30
+    return -1;
31
+  }
61 32
 
62
-static inline int get_byte(asn1_context_t* ctx) {
63
-    if (ctx->length <= 0) {
64
-        return -1;
65
-    }
66
-    int byte = *ctx->p;
67
-    ctx->p++;
68
-    ctx->length--;
69
-    return byte;
33
+  int byte = *p_;
34
+  p_++;
35
+  length_--;
36
+  return byte;
70 37
 }
71 38
 
72
-static inline bool skip_bytes(asn1_context_t* ctx, size_t num_skip) {
73
-    if (ctx->length < num_skip) {
74
-        return false;
75
-    }
76
-    ctx->p += num_skip;
77
-    ctx->length -= num_skip;
78
-    return true;
39
+bool asn1_context::skip_bytes(size_t num_skip) {
40
+  if (length_ < num_skip) {
41
+    return false;
42
+  }
43
+  p_ += num_skip;
44
+  length_ -= num_skip;
45
+  return true;
79 46
 }
80 47
 
81
-static bool decode_length(asn1_context_t* ctx, size_t* out_len) {
82
-    int num_octets = get_byte(ctx);
83
-    if (num_octets == -1) {
84
-        return false;
85
-    }
86
-    if ((num_octets & 0x80) == 0x00) {
87
-        *out_len = num_octets;
88
-        return 1;
89
-    }
90
-    num_octets &= kMaskTag;
91
-    if ((size_t)num_octets >= sizeof(size_t)) {
92
-        return false;
93
-    }
94
-    size_t length = 0;
95
-    for (int i = 0; i < num_octets; ++i) {
96
-        int byte = get_byte(ctx);
97
-        if (byte == -1) {
98
-            return false;
99
-        }
100
-        length <<= 8;
101
-        length += byte;
102
-    }
103
-    *out_len = length;
48
+bool asn1_context::decode_length(size_t* out_len) {
49
+  int num_octets = get_byte();
50
+  if (num_octets == -1) {
51
+    return false;
52
+  }
53
+  if ((num_octets & 0x80) == 0x00) {
54
+    *out_len = num_octets;
104 55
     return true;
56
+  }
57
+  num_octets &= kMaskTag;
58
+  if (static_cast<size_t>(num_octets) >= sizeof(size_t)) {
59
+    return false;
60
+  }
61
+  size_t length = 0;
62
+  for (int i = 0; i < num_octets; ++i) {
63
+    int byte = get_byte();
64
+    if (byte == -1) {
65
+      return false;
66
+    }
67
+    length <<= 8;
68
+    length += byte;
69
+  }
70
+  *out_len = length;
71
+  return true;
105 72
 }
106 73
 
107 74
 /**
108 75
  * Returns the constructed type and advances the pointer. E.g. A0 -> 0
109 76
  */
110
-asn1_context_t* asn1_constructed_get(asn1_context_t* ctx) {
111
-    int type = get_byte(ctx);
112
-    if (type == -1 || (type & kMaskConstructed) != kTagConstructed) {
113
-        return NULL;
114
-    }
115
-    size_t length;
116
-    if (!decode_length(ctx, &length) || length > ctx->length) {
117
-        return NULL;
118
-    }
119
-    asn1_context_t* app_ctx = asn1_context_new(ctx->p, length);
120
-    app_ctx->app_type = type & kMaskAppType;
121
-    return app_ctx;
77
+asn1_context* asn1_context::asn1_constructed_get() {
78
+  int type = get_byte();
79
+  if (type == -1 || (type & kMaskConstructed) != kTagConstructed) {
80
+    return nullptr;
81
+  }
82
+  size_t length;
83
+  if (!decode_length(&length) || length > length_) {
84
+    return nullptr;
85
+  }
86
+  asn1_context* app_ctx = new asn1_context(p_, length);
87
+  app_ctx->app_type_ = type & kMaskAppType;
88
+  return app_ctx;
122 89
 }
123 90
 
124
-bool asn1_constructed_skip_all(asn1_context_t* ctx) {
125
-    int byte = peek_byte(ctx);
126
-    while (byte != -1 && (byte & kMaskConstructed) == kTagConstructed) {
127
-        skip_bytes(ctx, 1);
128
-        size_t length;
129
-        if (!decode_length(ctx, &length) || !skip_bytes(ctx, length)) {
130
-            return false;
131
-        }
132
-        byte = peek_byte(ctx);
91
+bool asn1_context::asn1_constructed_skip_all() {
92
+  int byte = peek_byte();
93
+  while (byte != -1 && (byte & kMaskConstructed) == kTagConstructed) {
94
+    skip_bytes(1);
95
+    size_t length;
96
+    if (!decode_length(&length) || !skip_bytes(length)) {
97
+      return false;
133 98
     }
134
-    return byte != -1;
99
+    byte = peek_byte();
100
+  }
101
+  return byte != -1;
135 102
 }
136 103
 
137
-int asn1_constructed_type(asn1_context_t* ctx) {
138
-    return ctx->app_type;
104
+int asn1_context::asn1_constructed_type() const {
105
+  return app_type_;
139 106
 }
140 107
 
141
-asn1_context_t* asn1_sequence_get(asn1_context_t* ctx) {
142
-    if ((get_byte(ctx) & kMaskTag) != kTagSequence) {
143
-        return NULL;
144
-    }
145
-    size_t length;
146
-    if (!decode_length(ctx, &length) || length > ctx->length) {
147
-        return NULL;
148
-    }
149
-    return asn1_context_new(ctx->p, length);
108
+asn1_context* asn1_context::asn1_sequence_get() {
109
+  if ((get_byte() & kMaskTag) != kTagSequence) {
110
+    return nullptr;
111
+  }
112
+  size_t length;
113
+  if (!decode_length(&length) || length > length_) {
114
+    return nullptr;
115
+  }
116
+  return new asn1_context(p_, length);
150 117
 }
151 118
 
152
-asn1_context_t* asn1_set_get(asn1_context_t* ctx) {
153
-    if ((get_byte(ctx) & kMaskTag) != kTagSet) {
154
-        return NULL;
155
-    }
156
-    size_t length;
157
-    if (!decode_length(ctx, &length) || length > ctx->length) {
158
-        return NULL;
159
-    }
160
-    return asn1_context_new(ctx->p, length);
119
+asn1_context* asn1_context::asn1_set_get() {
120
+  if ((get_byte() & kMaskTag) != kTagSet) {
121
+    return nullptr;
122
+  }
123
+  size_t length;
124
+  if (!decode_length(&length) || length > length_) {
125
+    return nullptr;
126
+  }
127
+  return new asn1_context(p_, length);
161 128
 }
162 129
 
163
-bool asn1_sequence_next(asn1_context_t* ctx) {
164
-    size_t length;
165
-    if (get_byte(ctx) == -1 || !decode_length(ctx, &length) || !skip_bytes(ctx, length)) {
166
-        return false;
167
-    }
168
-    return true;
130
+bool asn1_context::asn1_sequence_next() {
131
+  size_t length;
132
+  if (get_byte() == -1 || !decode_length(&length) || !skip_bytes(length)) {
133
+    return false;
134
+  }
135
+  return true;
169 136
 }
170 137
 
171
-bool asn1_oid_get(asn1_context_t* ctx, const uint8_t** oid, size_t* length) {
172
-    if (get_byte(ctx) != kTagOid) {
173
-        return false;
174
-    }
175
-    if (!decode_length(ctx, length) || *length == 0 || *length > ctx->length) {
176
-        return false;
177
-    }
178
-    *oid = ctx->p;
179
-    return true;
138
+bool asn1_context::asn1_oid_get(const uint8_t** oid, size_t* length) {
139
+  if (get_byte() != kTagOid) {
140
+    return false;
141
+  }
142
+  if (!decode_length(length) || *length == 0 || *length > length_) {
143
+    return false;
144
+  }
145
+  *oid = p_;
146
+  return true;
180 147
 }
181 148
 
182
-bool asn1_octet_string_get(asn1_context_t* ctx, const uint8_t** octet_string, size_t* length) {
183
-    if (get_byte(ctx) != kTagOctetString) {
184
-        return false;
185
-    }
186
-    if (!decode_length(ctx, length) || *length == 0 || *length > ctx->length) {
187
-        return false;
188
-    }
189
-    *octet_string = ctx->p;
190
-    return true;
149
+bool asn1_context::asn1_octet_string_get(const uint8_t** octet_string, size_t* length) {
150
+  if (get_byte() != kTagOctetString) {
151
+    return false;
152
+  }
153
+  if (!decode_length(length) || *length == 0 || *length > length_) {
154
+    return false;
155
+  }
156
+  *octet_string = p_;
157
+  return true;
191 158
 }

+ 32
- 13
asn1_decoder.h View File

@@ -14,23 +14,42 @@
14 14
  * limitations under the License.
15 15
  */
16 16
 
17
-
18 17
 #ifndef ASN1_DECODER_H_
19 18
 #define ASN1_DECODER_H_
20 19
 
21 20
 #include <stdint.h>
22 21
 
23
-typedef struct asn1_context asn1_context_t;
24
-
25
-asn1_context_t* asn1_context_new(const uint8_t* buffer, size_t length);
26
-void asn1_context_free(asn1_context_t* ctx);
27
-asn1_context_t* asn1_constructed_get(asn1_context_t* ctx);
28
-bool asn1_constructed_skip_all(asn1_context_t* ctx);
29
-int asn1_constructed_type(asn1_context_t* ctx);
30
-asn1_context_t* asn1_sequence_get(asn1_context_t* ctx);
31
-asn1_context_t* asn1_set_get(asn1_context_t* ctx);
32
-bool asn1_sequence_next(asn1_context_t* seq);
33
-bool asn1_oid_get(asn1_context_t* ctx, const uint8_t** oid, size_t* length);
34
-bool asn1_octet_string_get(asn1_context_t* ctx, const uint8_t** octet_string, size_t* length);
22
+class asn1_context {
23
+ public:
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
+};
35 54
 
36 55
 #endif /* ASN1_DECODER_H_ */

+ 180
- 217
tests/unit/asn1_decoder_test.cpp View File

@@ -14,225 +14,188 @@
14 14
  * limitations under the License.
15 15
  */
16 16
 
17
-#define LOG_TAG "asn1_decoder_test"
17
+#include <stdint.h>
18
+
19
+#include <memory>
18 20
 
19
-#include <cutils/log.h>
20 21
 #include <gtest/gtest.h>
21
-#include <stdint.h>
22
-#include <unistd.h>
23 22
 
24 23
 #include "asn1_decoder.h"
25 24
 
26
-namespace android {
27
-
28
-class Asn1DecoderTest : public testing::Test {
29
-};
30
-
31
-TEST_F(Asn1DecoderTest, Empty_Failure) {
32
-    uint8_t empty[] = { };
33
-    asn1_context_t* ctx = asn1_context_new(empty, sizeof(empty));
34
-
35
-    EXPECT_EQ(NULL, asn1_constructed_get(ctx));
36
-    EXPECT_FALSE(asn1_constructed_skip_all(ctx));
37
-    EXPECT_EQ(0, asn1_constructed_type(ctx));
38
-    EXPECT_EQ(NULL, asn1_sequence_get(ctx));
39
-    EXPECT_EQ(NULL, asn1_set_get(ctx));
40
-    EXPECT_FALSE(asn1_sequence_next(ctx));
41
-
42
-    const uint8_t* junk;
43
-    size_t length;
44
-    EXPECT_FALSE(asn1_oid_get(ctx, &junk, &length));
45
-    EXPECT_FALSE(asn1_octet_string_get(ctx, &junk, &length));
46
-
47
-    asn1_context_free(ctx);
48
-}
49
-
50
-TEST_F(Asn1DecoderTest, ConstructedGet_TruncatedLength_Failure) {
51
-    uint8_t truncated[] = { 0xA0, 0x82, };
52
-    asn1_context_t* ctx = asn1_context_new(truncated, sizeof(truncated));
53
-    EXPECT_EQ(NULL, asn1_constructed_get(ctx));
54
-    asn1_context_free(ctx);
55
-}
56
-
57
-TEST_F(Asn1DecoderTest, ConstructedGet_LengthTooBig_Failure) {
58
-    uint8_t truncated[] = { 0xA0, 0x8a, 0xA5, 0x5A, 0xA5, 0x5A,
59
-                            0xA5, 0x5A, 0xA5, 0x5A, 0xA5, 0x5A, };
60
-    asn1_context_t* ctx = asn1_context_new(truncated, sizeof(truncated));
61
-    EXPECT_EQ(NULL, asn1_constructed_get(ctx));
62
-    asn1_context_free(ctx);
63
-}
64
-
65
-TEST_F(Asn1DecoderTest, ConstructedGet_TooSmallForChild_Failure) {
66
-    uint8_t data[] = { 0xA5, 0x02, 0x06, 0x01, 0x01, };
67
-    asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
68
-    asn1_context_t* ptr = asn1_constructed_get(ctx);
69
-    ASSERT_NE((asn1_context_t*)NULL, ptr);
70
-    EXPECT_EQ(5, asn1_constructed_type(ptr));
71
-    const uint8_t* oid;
72
-    size_t length;
73
-    EXPECT_FALSE(asn1_oid_get(ptr, &oid, &length));
74
-    asn1_context_free(ptr);
75
-    asn1_context_free(ctx);
76
-}
77
-
78
-TEST_F(Asn1DecoderTest, ConstructedGet_Success) {
79
-    uint8_t data[] = { 0xA5, 0x03, 0x06, 0x01, 0x01, };
80
-    asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
81
-    asn1_context_t* ptr = asn1_constructed_get(ctx);
82
-    ASSERT_NE((asn1_context_t*)NULL, ptr);
83
-    EXPECT_EQ(5, asn1_constructed_type(ptr));
84
-    const uint8_t* oid;
85
-    size_t length;
86
-    ASSERT_TRUE(asn1_oid_get(ptr, &oid, &length));
87
-    EXPECT_EQ(1U, length);
88
-    EXPECT_EQ(0x01U, *oid);
89
-    asn1_context_free(ptr);
90
-    asn1_context_free(ctx);
91
-}
92
-
93
-TEST_F(Asn1DecoderTest, ConstructedSkipAll_TruncatedLength_Failure) {
94
-    uint8_t truncated[] = { 0xA2, 0x82, };
95
-    asn1_context_t* ctx = asn1_context_new(truncated, sizeof(truncated));
96
-    EXPECT_FALSE(asn1_constructed_skip_all(ctx));
97
-    asn1_context_free(ctx);
98
-}
99
-
100
-TEST_F(Asn1DecoderTest, ConstructedSkipAll_Success) {
101
-    uint8_t data[] = { 0xA0, 0x03, 0x02, 0x01, 0x01,
102
-                            0xA1, 0x03, 0x02, 0x01, 0x01,
103
-                            0x06, 0x01, 0xA5, };
104
-    asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
105
-    ASSERT_TRUE(asn1_constructed_skip_all(ctx));
106
-    const uint8_t* oid;
107
-    size_t length;
108
-    ASSERT_TRUE(asn1_oid_get(ctx, &oid, &length));
109
-    EXPECT_EQ(1U, length);
110
-    EXPECT_EQ(0xA5U, *oid);
111
-    asn1_context_free(ctx);
112
-}
113
-
114
-TEST_F(Asn1DecoderTest, SequenceGet_TruncatedLength_Failure) {
115
-    uint8_t truncated[] = { 0x30, 0x82, };
116
-    asn1_context_t* ctx = asn1_context_new(truncated, sizeof(truncated));
117
-    EXPECT_EQ(NULL, asn1_sequence_get(ctx));
118
-    asn1_context_free(ctx);
119
-}
120
-
121
-TEST_F(Asn1DecoderTest, SequenceGet_TooSmallForChild_Failure) {
122
-    uint8_t data[] = { 0x30, 0x02, 0x06, 0x01, 0x01, };
123
-    asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
124
-    asn1_context_t* ptr = asn1_sequence_get(ctx);
125
-    ASSERT_NE((asn1_context_t*)NULL, ptr);
126
-    const uint8_t* oid;
127
-    size_t length;
128
-    EXPECT_FALSE(asn1_oid_get(ptr, &oid, &length));
129
-    asn1_context_free(ptr);
130
-    asn1_context_free(ctx);
131
-}
132
-
133
-TEST_F(Asn1DecoderTest, SequenceGet_Success) {
134
-    uint8_t data[] = { 0x30, 0x03, 0x06, 0x01, 0x01, };
135
-    asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
136
-    asn1_context_t* ptr = asn1_sequence_get(ctx);
137
-    ASSERT_NE((asn1_context_t*)NULL, ptr);
138
-    const uint8_t* oid;
139
-    size_t length;
140
-    ASSERT_TRUE(asn1_oid_get(ptr, &oid, &length));
141
-    EXPECT_EQ(1U, length);
142
-    EXPECT_EQ(0x01U, *oid);
143
-    asn1_context_free(ptr);
144
-    asn1_context_free(ctx);
145
-}
146
-
147
-TEST_F(Asn1DecoderTest, SetGet_TruncatedLength_Failure) {
148
-    uint8_t truncated[] = { 0x31, 0x82, };
149
-    asn1_context_t* ctx = asn1_context_new(truncated, sizeof(truncated));
150
-    EXPECT_EQ(NULL, asn1_set_get(ctx));
151
-    asn1_context_free(ctx);
152
-}
153
-
154
-TEST_F(Asn1DecoderTest, SetGet_TooSmallForChild_Failure) {
155
-    uint8_t data[] = { 0x31, 0x02, 0x06, 0x01, 0x01, };
156
-    asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
157
-    asn1_context_t* ptr = asn1_set_get(ctx);
158
-    ASSERT_NE((asn1_context_t*)NULL, ptr);
159
-    const uint8_t* oid;
160
-    size_t length;
161
-    EXPECT_FALSE(asn1_oid_get(ptr, &oid, &length));
162
-    asn1_context_free(ptr);
163
-    asn1_context_free(ctx);
164
-}
165
-
166
-TEST_F(Asn1DecoderTest, SetGet_Success) {
167
-    uint8_t data[] = { 0x31, 0x03, 0x06, 0x01, 0xBA, };
168
-    asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
169
-    asn1_context_t* ptr = asn1_set_get(ctx);
170
-    ASSERT_NE((asn1_context_t*)NULL, ptr);
171
-    const uint8_t* oid;
172
-    size_t length;
173
-    ASSERT_TRUE(asn1_oid_get(ptr, &oid, &length));
174
-    EXPECT_EQ(1U, length);
175
-    EXPECT_EQ(0xBAU, *oid);
176
-    asn1_context_free(ptr);
177
-    asn1_context_free(ctx);
178
-}
179
-
180
-TEST_F(Asn1DecoderTest, OidGet_LengthZero_Failure) {
181
-    uint8_t data[] = { 0x06, 0x00, 0x01, };
182
-    asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
183
-    const uint8_t* oid;
184
-    size_t length;
185
-    EXPECT_FALSE(asn1_oid_get(ctx, &oid, &length));
186
-    asn1_context_free(ctx);
187
-}
188
-
189
-TEST_F(Asn1DecoderTest, OidGet_TooSmall_Failure) {
190
-    uint8_t data[] = { 0x06, 0x01, };
191
-    asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
192
-    const uint8_t* oid;
193
-    size_t length;
194
-    EXPECT_FALSE(asn1_oid_get(ctx, &oid, &length));
195
-    asn1_context_free(ctx);
196
-}
197
-
198
-TEST_F(Asn1DecoderTest, OidGet_Success) {
199
-    uint8_t data[] = { 0x06, 0x01, 0x99, };
200
-    asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
201
-    const uint8_t* oid;
202
-    size_t length;
203
-    ASSERT_TRUE(asn1_oid_get(ctx, &oid, &length));
204
-    EXPECT_EQ(1U, length);
205
-    EXPECT_EQ(0x99U, *oid);
206
-    asn1_context_free(ctx);
207
-}
208
-
209
-TEST_F(Asn1DecoderTest, OctetStringGet_LengthZero_Failure) {
210
-    uint8_t data[] = { 0x04, 0x00, 0x55, };
211
-    asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
212
-    const uint8_t* string;
213
-    size_t length;
214
-    ASSERT_FALSE(asn1_octet_string_get(ctx, &string, &length));
215
-    asn1_context_free(ctx);
216
-}
217
-
218
-TEST_F(Asn1DecoderTest, OctetStringGet_TooSmall_Failure) {
219
-    uint8_t data[] = { 0x04, 0x01, };
220
-    asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
221
-    const uint8_t* string;
222
-    size_t length;
223
-    ASSERT_FALSE(asn1_octet_string_get(ctx, &string, &length));
224
-    asn1_context_free(ctx);
225
-}
226
-
227
-TEST_F(Asn1DecoderTest, OctetStringGet_Success) {
228
-    uint8_t data[] = { 0x04, 0x01, 0xAA, };
229
-    asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
230
-    const uint8_t* string;
231
-    size_t length;
232
-    ASSERT_TRUE(asn1_octet_string_get(ctx, &string, &length));
233
-    EXPECT_EQ(1U, length);
234
-    EXPECT_EQ(0xAAU, *string);
235
-    asn1_context_free(ctx);
236
-}
237
-
238
-} // namespace android
25
+TEST(Asn1DecoderTest, Empty_Failure) {
26
+  uint8_t empty[] = {};
27
+  asn1_context ctx(empty, sizeof(empty));
28
+
29
+  ASSERT_EQ(nullptr, ctx.asn1_constructed_get());
30
+  ASSERT_FALSE(ctx.asn1_constructed_skip_all());
31
+  ASSERT_EQ(0, ctx.asn1_constructed_type());
32
+  ASSERT_EQ(nullptr, ctx.asn1_sequence_get());
33
+  ASSERT_EQ(nullptr, ctx.asn1_set_get());
34
+  ASSERT_FALSE(ctx.asn1_sequence_next());
35
+
36
+  const uint8_t* junk;
37
+  size_t length;
38
+  ASSERT_FALSE(ctx.asn1_oid_get(&junk, &length));
39
+  ASSERT_FALSE(ctx.asn1_octet_string_get(&junk, &length));
40
+}
41
+
42
+TEST(Asn1DecoderTest, ConstructedGet_TruncatedLength_Failure) {
43
+  uint8_t truncated[] = { 0xA0, 0x82 };
44
+  asn1_context ctx(truncated, sizeof(truncated));
45
+  ASSERT_EQ(nullptr, ctx.asn1_constructed_get());
46
+}
47
+
48
+TEST(Asn1DecoderTest, ConstructedGet_LengthTooBig_Failure) {
49
+  uint8_t truncated[] = { 0xA0, 0x8a, 0xA5, 0x5A, 0xA5, 0x5A, 0xA5, 0x5A, 0xA5, 0x5A, 0xA5, 0x5A };
50
+  asn1_context ctx(truncated, sizeof(truncated));
51
+  ASSERT_EQ(nullptr, ctx.asn1_constructed_get());
52
+}
53
+
54
+TEST(Asn1DecoderTest, ConstructedGet_TooSmallForChild_Failure) {
55
+  uint8_t data[] = { 0xA5, 0x02, 0x06, 0x01, 0x01 };
56
+  asn1_context ctx(data, sizeof(data));
57
+  std::unique_ptr<asn1_context> ptr(ctx.asn1_constructed_get());
58
+  ASSERT_NE(nullptr, ptr);
59
+  ASSERT_EQ(5, ptr->asn1_constructed_type());
60
+  const uint8_t* oid;
61
+  size_t length;
62
+  ASSERT_FALSE(ptr->asn1_oid_get(&oid, &length));
63
+}
64
+
65
+TEST(Asn1DecoderTest, ConstructedGet_Success) {
66
+  uint8_t data[] = { 0xA5, 0x03, 0x06, 0x01, 0x01 };
67
+  asn1_context ctx(data, sizeof(data));
68
+  std::unique_ptr<asn1_context> ptr(ctx.asn1_constructed_get());
69
+  ASSERT_NE(nullptr, ptr);
70
+  ASSERT_EQ(5, ptr->asn1_constructed_type());
71
+  const uint8_t* oid;
72
+  size_t length;
73
+  ASSERT_TRUE(ptr->asn1_oid_get(&oid, &length));
74
+  ASSERT_EQ(1U, length);
75
+  ASSERT_EQ(0x01U, *oid);
76
+}
77
+
78
+TEST(Asn1DecoderTest, ConstructedSkipAll_TruncatedLength_Failure) {
79
+  uint8_t truncated[] = { 0xA2, 0x82 };
80
+  asn1_context ctx(truncated, sizeof(truncated));
81
+  ASSERT_FALSE(ctx.asn1_constructed_skip_all());
82
+}
83
+
84
+TEST(Asn1DecoderTest, ConstructedSkipAll_Success) {
85
+  uint8_t data[] = { 0xA0, 0x03, 0x02, 0x01, 0x01, 0xA1, 0x03, 0x02, 0x01, 0x01, 0x06, 0x01, 0xA5 };
86
+  asn1_context ctx(data, sizeof(data));
87
+  ASSERT_TRUE(ctx.asn1_constructed_skip_all());
88
+  const uint8_t* oid;
89
+  size_t length;
90
+  ASSERT_TRUE(ctx.asn1_oid_get(&oid, &length));
91
+  ASSERT_EQ(1U, length);
92
+  ASSERT_EQ(0xA5U, *oid);
93
+}
94
+
95
+TEST(Asn1DecoderTest, SequenceGet_TruncatedLength_Failure) {
96
+  uint8_t truncated[] = { 0x30, 0x82 };
97
+  asn1_context ctx(truncated, sizeof(truncated));
98
+  ASSERT_EQ(nullptr, ctx.asn1_sequence_get());
99
+}
100
+
101
+TEST(Asn1DecoderTest, SequenceGet_TooSmallForChild_Failure) {
102
+  uint8_t data[] = { 0x30, 0x02, 0x06, 0x01, 0x01 };
103
+  asn1_context ctx(data, sizeof(data));
104
+  std::unique_ptr<asn1_context> ptr(ctx.asn1_sequence_get());
105
+  ASSERT_NE(nullptr, ptr);
106
+  const uint8_t* oid;
107
+  size_t length;
108
+  ASSERT_FALSE(ptr->asn1_oid_get(&oid, &length));
109
+}
110
+
111
+TEST(Asn1DecoderTest, SequenceGet_Success) {
112
+  uint8_t data[] = { 0x30, 0x03, 0x06, 0x01, 0x01 };
113
+  asn1_context ctx(data, sizeof(data));
114
+  std::unique_ptr<asn1_context> ptr(ctx.asn1_sequence_get());
115
+  ASSERT_NE(nullptr, ptr);
116
+  const uint8_t* oid;
117
+  size_t length;
118
+  ASSERT_TRUE(ptr->asn1_oid_get(&oid, &length));
119
+  ASSERT_EQ(1U, length);
120
+  ASSERT_EQ(0x01U, *oid);
121
+}
122
+
123
+TEST(Asn1DecoderTest, SetGet_TruncatedLength_Failure) {
124
+  uint8_t truncated[] = { 0x31, 0x82 };
125
+  asn1_context ctx(truncated, sizeof(truncated));
126
+  ASSERT_EQ(nullptr, ctx.asn1_set_get());
127
+}
128
+
129
+TEST(Asn1DecoderTest, SetGet_TooSmallForChild_Failure) {
130
+  uint8_t data[] = { 0x31, 0x02, 0x06, 0x01, 0x01 };
131
+  asn1_context ctx(data, sizeof(data));
132
+  std::unique_ptr<asn1_context> ptr(ctx.asn1_set_get());
133
+  ASSERT_NE(nullptr, ptr);
134
+  const uint8_t* oid;
135
+  size_t length;
136
+  ASSERT_FALSE(ptr->asn1_oid_get(&oid, &length));
137
+}
138
+
139
+TEST(Asn1DecoderTest, SetGet_Success) {
140
+  uint8_t data[] = { 0x31, 0x03, 0x06, 0x01, 0xBA };
141
+  asn1_context ctx(data, sizeof(data));
142
+  std::unique_ptr<asn1_context> ptr(ctx.asn1_set_get());
143
+  ASSERT_NE(nullptr, ptr);
144
+  const uint8_t* oid;
145
+  size_t length;
146
+  ASSERT_TRUE(ptr->asn1_oid_get(&oid, &length));
147
+  ASSERT_EQ(1U, length);
148
+  ASSERT_EQ(0xBAU, *oid);
149
+}
150
+
151
+TEST(Asn1DecoderTest, OidGet_LengthZero_Failure) {
152
+  uint8_t data[] = { 0x06, 0x00, 0x01 };
153
+  asn1_context ctx(data, sizeof(data));
154
+  const uint8_t* oid;
155
+  size_t length;
156
+  ASSERT_FALSE(ctx.asn1_oid_get(&oid, &length));
157
+}
158
+
159
+TEST(Asn1DecoderTest, OidGet_TooSmall_Failure) {
160
+  uint8_t data[] = { 0x06, 0x01 };
161
+  asn1_context ctx(data, sizeof(data));
162
+  const uint8_t* oid;
163
+  size_t length;
164
+  ASSERT_FALSE(ctx.asn1_oid_get(&oid, &length));
165
+}
166
+
167
+TEST(Asn1DecoderTest, OidGet_Success) {
168
+  uint8_t data[] = { 0x06, 0x01, 0x99 };
169
+  asn1_context ctx(data, sizeof(data));
170
+  const uint8_t* oid;
171
+  size_t length;
172
+  ASSERT_TRUE(ctx.asn1_oid_get(&oid, &length));
173
+  ASSERT_EQ(1U, length);
174
+  ASSERT_EQ(0x99U, *oid);
175
+}
176
+
177
+TEST(Asn1DecoderTest, OctetStringGet_LengthZero_Failure) {
178
+  uint8_t data[] = { 0x04, 0x00, 0x55 };
179
+  asn1_context ctx(data, sizeof(data));
180
+  const uint8_t* string;
181
+  size_t length;
182
+  ASSERT_FALSE(ctx.asn1_octet_string_get(&string, &length));
183
+}
184
+
185
+TEST(Asn1DecoderTest, OctetStringGet_TooSmall_Failure) {
186
+  uint8_t data[] = { 0x04, 0x01 };
187
+  asn1_context ctx(data, sizeof(data));
188
+  const uint8_t* string;
189
+  size_t length;
190
+  ASSERT_FALSE(ctx.asn1_octet_string_get(&string, &length));
191
+}
192
+
193
+TEST(Asn1DecoderTest, OctetStringGet_Success) {
194
+  uint8_t data[] = { 0x04, 0x01, 0xAA };
195
+  asn1_context ctx(data, sizeof(data));
196
+  const uint8_t* string;
197
+  size_t length;
198
+  ASSERT_TRUE(ctx.asn1_octet_string_get(&string, &length));
199
+  ASSERT_EQ(1U, length);
200
+  ASSERT_EQ(0xAAU, *string);
201
+}

+ 39
- 37
verifier.cpp View File

@@ -66,48 +66,50 @@ static bool read_pkcs7(const uint8_t* pkcs7_der, size_t pkcs7_der_len,
66 66
   CHECK(sig_der != nullptr);
67 67
   sig_der->clear();
68 68
 
69
-  asn1_context_t* ctx = asn1_context_new(pkcs7_der, pkcs7_der_len);
70
-  if (ctx == NULL) {
69
+  asn1_context ctx(pkcs7_der, pkcs7_der_len);
70
+
71
+  std::unique_ptr<asn1_context> pkcs7_seq(ctx.asn1_sequence_get());
72
+  if (pkcs7_seq == nullptr || !pkcs7_seq->asn1_sequence_next()) {
71 73
     return false;
72 74
   }
73 75
 
74
-  asn1_context_t* pkcs7_seq = asn1_sequence_get(ctx);
75
-  if (pkcs7_seq != NULL && asn1_sequence_next(pkcs7_seq)) {
76
-    asn1_context_t *signed_data_app = asn1_constructed_get(pkcs7_seq);
77
-    if (signed_data_app != NULL) {
78
-      asn1_context_t* signed_data_seq = asn1_sequence_get(signed_data_app);
79
-      if (signed_data_seq != NULL
80
-          && asn1_sequence_next(signed_data_seq)
81
-          && asn1_sequence_next(signed_data_seq)
82
-          && asn1_sequence_next(signed_data_seq)
83
-          && asn1_constructed_skip_all(signed_data_seq)) {
84
-        asn1_context_t *sig_set = asn1_set_get(signed_data_seq);
85
-        if (sig_set != NULL) {
86
-          asn1_context_t* sig_seq = asn1_sequence_get(sig_set);
87
-          if (sig_seq != NULL
88
-              && asn1_sequence_next(sig_seq)
89
-              && asn1_sequence_next(sig_seq)
90
-              && asn1_sequence_next(sig_seq)
91
-              && asn1_sequence_next(sig_seq)) {
92
-            const uint8_t* sig_der_ptr;
93
-            size_t sig_der_length;
94
-            if (asn1_octet_string_get(sig_seq, &sig_der_ptr, &sig_der_length)) {
95
-              sig_der->resize(sig_der_length);
96
-              std::copy(sig_der_ptr, sig_der_ptr + sig_der_length, sig_der->begin());
97
-            }
98
-            asn1_context_free(sig_seq);
99
-          }
100
-          asn1_context_free(sig_set);
101
-        }
102
-        asn1_context_free(signed_data_seq);
103
-      }
104
-      asn1_context_free(signed_data_app);
105
-    }
106
-    asn1_context_free(pkcs7_seq);
76
+  std::unique_ptr<asn1_context> signed_data_app(pkcs7_seq->asn1_constructed_get());
77
+  if (signed_data_app == nullptr) {
78
+    return false;
79
+  }
80
+
81
+  std::unique_ptr<asn1_context> signed_data_seq(signed_data_app->asn1_sequence_get());
82
+  if (signed_data_seq == nullptr ||
83
+      !signed_data_seq->asn1_sequence_next() ||
84
+      !signed_data_seq->asn1_sequence_next() ||
85
+      !signed_data_seq->asn1_sequence_next() ||
86
+      !signed_data_seq->asn1_constructed_skip_all()) {
87
+    return false;
88
+  }
89
+
90
+  std::unique_ptr<asn1_context> sig_set(signed_data_seq->asn1_set_get());
91
+  if (sig_set == nullptr) {
92
+    return false;
93
+  }
94
+
95
+  std::unique_ptr<asn1_context> sig_seq(sig_set->asn1_sequence_get());
96
+  if (sig_seq == nullptr ||
97
+      !sig_seq->asn1_sequence_next() ||
98
+      !sig_seq->asn1_sequence_next() ||
99
+      !sig_seq->asn1_sequence_next() ||
100
+      !sig_seq->asn1_sequence_next()) {
101
+    return false;
102
+  }
103
+
104
+  const uint8_t* sig_der_ptr;
105
+  size_t sig_der_length;
106
+  if (!sig_seq->asn1_octet_string_get(&sig_der_ptr, &sig_der_length)) {
107
+    return false;
107 108
   }
108
-  asn1_context_free(ctx);
109 109
 
110
-  return !sig_der->empty();
110
+  sig_der->resize(sig_der_length);
111
+  std::copy(sig_der_ptr, sig_der_ptr + sig_der_length, sig_der->begin());
112
+  return true;
111 113
 }
112 114
 
113 115
 /*