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