1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * LICENSE-2.0" target="_blank">http://www.apache.org/licenses/LICENSE-2.0
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.
20 ******************************************************************/
28 #include "byte_array.h"
29 #include "pki_errors.h"
30 #include "crypto_adapter.h"
32 /// Maximal octet number in certificate's serial number
33 #define SERIAL_NUMBER_MAX_LEN (20)
36 * These constants comply with DER encoded the ANS.1 type tags.
37 * DER encoding uses hexadecimal representation.
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)
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)
56 #define SIZE_OF_BYTE (8)
58 #define ECDSA_WITH_SHA256_OID_LEN (8)
59 #define EC_PUBLIC_KEY_OID_LEN (7)
60 #define PRIME_256_V1_OID_LEN (8)
62 /**@def SKIP_DER_FIELD(array, type, length)
63 * Skips the field in the ASN.1 structure.
65 * @param array pointer to ASN.1 stucture
66 * @param type type of ASN.1 field
67 * @param length length of ASN.1 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); \
76 /**@def COPY_DER_FIELD(array, str, field, type, length)
77 * Copies the field from the ASN.1 structure.
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
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); \
95 /**@def CHECK_DER_OID(array, oid, length)
96 * Checks the field from the ASN.1 structure.
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
105 #define CHECK_DER_OID(array, oid, oidLen, length) do{ \
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); \
114 /**@def PARSE_SIGNATURE(structure)
115 * Parse signature of ASN.1 structure , remove ASN.1 extra bytes.
117 * @param structure Certificate or CertificateList structure
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); \
132 * Computes length of ASN.1 object in DER format.
134 * @param[in] code array with DER encoded ASN.1 structure
135 * @return PKI_SUCCESS if success, error code otherwise
137 PKIError DecodeLength(ByteArray *code, size_t *length);
144 #endif //_X509_PARSE_H_