1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef HKS_ASN1_H
17 #define HKS_ASN1_H
18
19 #include <stddef.h>
20 #include <stdint.h>
21
22 #include "hks_type.h"
23
24 #define ASN_1_TAG_TYPE_BOOL 0x01
25 #define ASN_1_TAG_TYPE_INT 0x02
26 #define ASN_1_TAG_TYPE_BIT_STR 0x03
27 #define ASN_1_TAG_TYPE_OCT_STR 0x04
28 #define ASN_1_TAG_TYPE_NULL 0x05
29 #define ASN_1_TAG_TYPE_OID 0x06
30 #define ASN_1_TAG_TYPE_ENUMERATED 0x0A
31 #define ASN_1_TAG_TYPE_UTF8_STR 0x0C
32 #define ASN_1_TAG_TYPE_PRINTABLE_STR 0x13
33 #define ASN_1_TAG_TYPE_UTC_TIME 0x17
34
35 #define ASN_1_TAG_TYPE_SEQ 0x30
36 #define ASN_1_TAG_TYPE_SET 0x31
37
38 #define ASN_1_TAG_TYPE_CTX_SPEC0 0xA0
39 #define ASN_1_TAG_TYPE_CTX_SPEC3 0xA3
40
41 #define ASN_1_TAG_TYPE_RAW 0xff000001
42
43 #define ASN_1_MAX_VAL_NO_EXTRA_LEN_BYTE 0x7F
44 #define ASN_1_MIN_VAL_1_EXTRA_LEN_BYTE 0x80
45 #define ASN_1_MIN_VAL_2_EXTRA_LEN_BYTE 0x100
46
47 #define ASN_1_TAG_TYPE_1_BYTE_LEN 0x81
48 #define ASN_1_TAG_TYPE_2_BYTE_LEN 0x82
49
50 #define ASN_1_MAX_HEADER_LEN 0x5
51 #define ASN_1_MIN_HEADER_LEN 0x3
52
53 #define ASN_1_TRUE_VALUE 0xFF
54
55 #define ASN_1_MAX_SIZE (0x10000 - 0x100)
56
57 struct HksAsn1Blob {
58 uint32_t type;
59 uint32_t size;
60 uint8_t *data;
61 };
62
63 struct HksAsn1Obj {
64 struct HksAsn1Blob header;
65 struct HksAsn1Blob value;
66 };
67
68 #define HKS_ASN1_ENCODE_BYTE(ptr, value) \
69 do { \
70 (ptr)[0] = (uint8_t)((value) & 0xff); \
71 (ptr)++; \
72 } while (0)
73
74 #define HKS_ASN1_ENCODE_TWO_BYTE(ptr, value) \
75 do { \
76 (ptr)[0] = (uint8_t)(((value) >> 8) & 0xff); \
77 (ptr)++; \
78 (ptr)[0] = (uint8_t)((value) & 0xff); \
79 (ptr)++; \
80 } while (0)
81
82 #define HKS_ASN1_DECODE_BYTE(ptr, value) \
83 do { \
84 (value) = (uint32_t)((ptr)[0]); \
85 (ptr)++; \
86 } while (0)
87
88 #define HKS_ASN1_DECODE_TWO_BYTE(ptr, value) \
89 do { \
90 (value) = (uint32_t)((ptr)[0] & 0xff) << 8; \
91 (ptr)++; \
92 (value) |= (uint32_t)((ptr)[0] & 0xff); \
93 (ptr)++; \
94 } while (0)
95
96 #ifdef __cplusplus
97 extern "C" {
98 #endif
99
100 int32_t DcmAsn1InsertValue(struct HksBlob *buf, struct HksAsn1Obj *obj, const struct HksAsn1Blob *tlv);
101
102 int32_t DcmAsn1WriteFinal(struct HksBlob *final, const struct HksAsn1Blob *tlv);
103
104 int32_t DcmAsn1GetObj(struct HksBlob *next, struct HksAsn1Obj *obj, const struct HksBlob *data);
105
106 int32_t DcmAsn1ExtractTag(struct HksBlob *next, struct HksAsn1Obj *obj, const struct HksBlob *data,
107 uint32_t expectedTag);
108
CheckAsn1Blob(const struct HksAsn1Blob * blob)109 static inline int32_t CheckAsn1Blob(const struct HksAsn1Blob *blob)
110 {
111 if ((blob == NULL) || (blob->data == NULL) || (blob->size == 0)) {
112 return HKS_ERROR_INVALID_ARGUMENT;
113 }
114 return HKS_SUCCESS;
115 }
CheckAsn1Obj(const struct HksAsn1Obj * obj)116 static inline int32_t CheckAsn1Obj(const struct HksAsn1Obj *obj)
117 {
118 if ((obj == NULL) || (CheckAsn1Blob(&obj->header) != HKS_SUCCESS) || (CheckAsn1Blob(&obj->value) != HKS_SUCCESS)) {
119 return HKS_ERROR_INVALID_ARGUMENT;
120 }
121 return HKS_SUCCESS;
122 }
123
124 #ifdef __cplusplus
125 }
126 #endif
127
128 #endif
129