Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / resource / csdk / connectivity / inc / pkix / der_dec.h
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      LICENSE-2.0" target="_blank">http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19
20  ******************************************************************/
21 #ifndef _DER_DEC_H_
22 #define _DER_DEC_H_
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif //__cplusplus
27
28 #include "byte_array.h"
29 #include "pki_errors.h"
30 #include "crypto_adapter.h"
31
32 /// Maximal octet number in certificate's serial number
33 #define SERIAL_NUMBER_MAX_LEN    (20)
34 /**
35  * @name DER constants
36  * These constants comply with DER encoded the ANS.1 type tags.
37  * DER encoding uses hexadecimal representation.
38  */
39 #define DER_UNIVERSAL               (0x00)
40 #define DER_SEQUENCE                (0x30)
41 #define DER_OBJECT_IDENTIFIER       (0x06)
42 #define DER_BIT_STRING              (0x03)
43 #define DER_INTEGER                 (0x02)
44 #define DER_UTC_TIME                (0x17)
45 #define DER_VERSION                 (0xa0)
46
47 /* The first octet of the OCTET STRING indicates whether the key is
48 compressed or uncompressed.  The uncompressed form is indicated by 0x04
49 and the compressed form is indicated by either 0x02 or 0x03 (RFC 5480)*/
50 #define ASN1_UNCOMPRESSED_KEY   (0x04)
51 /// ASN.1 UTC time length
52 #define UTC_TIME_LEN            (13)
53 ///  Length Octet ASN.1
54 #define LEN_LONG                (128)
55 /// Size of byte
56 #define SIZE_OF_BYTE            (8)
57
58 #define ECDSA_WITH_SHA256_OID_LEN    (8)
59 #define EC_PUBLIC_KEY_OID_LEN        (7)
60 #define PRIME_256_V1_OID_LEN         (8)
61
62 /**@def SKIP_DER_FIELD(array, type, length)
63  * Skips the field in the ASN.1 structure.
64  *
65  * @param array pointer to ASN.1 stucture
66  * @param type type of ASN.1 field
67  * @param length length of ASN.1 field
68  */
69 #undef SKIP_DER_FIELD
70 #define SKIP_DER_FIELD(array, type, length) do{                 \
71         CHECK_EQUAL(*((array).data), type, PKI_INVALID_FORMAT); \
72         CHECK_CALL(DecodeLength , &(array), &(length));         \
73         INC_BYTE_ARRAY(array, length);                          \
74         }while(0)
75
76 /**@def COPY_DER_FIELD(array, str, field, type, length)
77  * Copies the field from the ASN.1 structure.
78  *
79  * @param array pointer to ASN.1 stucture
80  * @param str structure in which the array is copied
81  * @param field field of the structure in which the array is copied
82  * @param type type of ASN.1 field
83  * @param length length of ASN.1 field
84  */
85 #undef COPY_DER_FIELD
86 #define COPY_DER_FIELD(array, crt, field, type, length) do{     \
87         CHECK_EQUAL(*((array).data), type, PKI_INVALID_FORMAT); \
88         CHECK_CALL(DecodeLength , &(array), &(length));         \
89         ((crt)->field).data = (array).data;                     \
90         ((crt)->field).len = length;                            \
91         INC_BYTE_ARRAY(array, length);                          \
92         }while(0)
93
94
95 /**@def CHECK_DER_OID(array, oid, length)
96  * Checks the field from the ASN.1 structure.
97  *
98  * @param array pointer to ASN.1 stucture
99  * @param oid type of DER object
100  * @param oidLen length of DER array
101  * @param length length of ASN.1 field
102  */
103 #undef CHECK_DER_OID
104 #undef CHECK_DER_OID
105 #define CHECK_DER_OID(array, oid, oidLen, length) do{                              \
106         int ret = 0;                                                               \
107         CHECK_EQUAL(*((array).data), DER_OBJECT_IDENTIFIER, PKI_INVALID_FORMAT);   \
108         CHECK_CALL(DecodeLength , &(array), &(length));                            \
109         CHECK_EQUAL(length, oidLen, PKI_UNKNOWN_OID);                              \
110         ret = memcmp ((array).data, oid, oidLen);                                  \
111         CHECK_EQUAL(ret, 0, PKI_UNKNOWN_OID);                                      \
112         }while(0)
113
114 /**@def PARSE_SIGNATURE(structure)
115  * Parse signature of ASN.1 structure , remove ASN.1 extra bytes.
116  *
117  * @param structure Certificate or CertificateList structure
118  */
119 #undef PARSE_SIGNATURE
120 #define PARSE_SIGNATURE(structure) do{                                                       \
121         if (((structure)->signR.len == SIGN_R_LEN + 1) && ((structure)->signR.data[0] == 0)) \
122         INC_BYTE_ARRAY((structure)->signR, 1);                                               \
123         else if ((structure)->signR.len != SIGN_R_LEN)                                       \
124         CHECK_NULL(NULL, PKI_WRONG_ARRAY_LEN);                                               \
125         if (((structure)->signS.len == SIGN_S_LEN + 1) && ((structure)->signS.data[0] == 0)) \
126         INC_BYTE_ARRAY((structure)->signS, 1);                                               \
127         else if ((structure)->signS.len != SIGN_S_LEN)                                       \
128         CHECK_NULL(NULL, PKI_WRONG_ARRAY_LEN);                                               \
129         }while(0)
130
131 /**
132  * Computes length of ASN.1 object in DER format.
133  *
134  * @param[in] code array with DER encoded ASN.1 structure
135  * @return PKI_SUCCESS if success, error code otherwise
136  */
137 PKIError DecodeLength(ByteArray *code, size_t *length);
138
139 #ifdef __cplusplus
140 }
141 #endif //__cplusplus
142
143
144 #endif //_X509_PARSE_H_