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 ******************************************************************/
24 #include "byte_array.h"
28 #include "crypto_adapter.h"
31 extern const uint8_t g_ECDSA_WITH_SHA256_OID[ECDSA_WITH_SHA256_OID_LEN];
32 extern const uint8_t g_EC_PUBLIC_KEY_OID[EC_PUBLIC_KEY_OID_LEN];
33 extern const uint8_t g_PRIME_256_V1_OID[PRIME_256_V1_OID_LEN];
36 * TBSCertList ::= SEQUENCE {
37 * version Version OPTIONAL,
38 * -- if present, MUST be v2
39 * signature AlgorithmIdentifier,
42 * revokedCertificates SEQUENCE OF SEQUENCE {
43 * userCertificate CertificateSerialNumber,
53 static PKIError DecodeTbs(CertificateList *const crl)
57 ByteArray tbs = crl->tbs, temp;
58 CHECK_NULL(crl, PKI_NULL_PASSED);
59 ByteArray sn = BYTE_ARRAY_INITIALIZER;
63 CHECK_EQUAL(*(tbs.data), DER_SEQUENCE, PKI_INVALID_FORMAT);
64 CHECK_CALL(DecodeLength , &tbs, &length);
66 INC_BYTE_ARRAY(tbs, length); // skip algorithm identifier
68 //copy issuer X.500 name
69 COPY_DER_FIELD(tbs, crl, issuer, DER_SEQUENCE, length);
71 COPY_DER_FIELD(tbs, crl, date, DER_UTC_TIME, length);
72 //COPY_DER_FIELD(tbs, crl, date, DER_UTC_TIME, length); // optional
73 // copy serial numbers
74 CHECK_EQUAL(*(tbs.data), DER_SEQUENCE, PKI_INVALID_FORMAT);
75 CHECK_CALL(DecodeLength , &tbs, &length);
78 while (tbs.data < temp.data + temp.len)
80 CHECK_EQUAL(*(tbs.data), DER_SEQUENCE, PKI_INVALID_FORMAT);
81 CHECK_CALL(DecodeLength , &tbs, &length);
83 CHECK_EQUAL(*(tbs.data), DER_INTEGER, PKI_INVALID_FORMAT);
84 CHECK_CALL(DecodeLength , &tbs, &length);
87 CHECK_CALL(StoreSerialNumber, sn);
88 INC_BYTE_ARRAY(tbs, length);
89 SKIP_DER_FIELD(tbs, DER_UTC_TIME, length);
95 * CertificateList ::= SEQUENCE {
96 * tbsCertList TBSCertList,
97 * signatureAlgorithm AlgorithmIdentifier,
98 * signatureValue BIT STRING }
102 * Decodes certificate in DER format.
104 PKIError DecodeCertificateList(ByteArray code, CertificateList *crl, ByteArray caPubKey)
107 size_t length, tempLen;
109 CHECK_NULL(crl, PKI_NULL_PASSED);
110 CHECK_NULL(code.data, PKI_NULL_PASSED);
112 CHECK_EQUAL(*(code.data), DER_SEQUENCE, PKI_INVALID_FORMAT);
113 CHECK_CALL(DecodeLength , &code, &length);
114 //store sequence position
116 //TODO check length of TBS
118 COPY_DER_FIELD(code, crl, tbs, DER_SEQUENCE, length);
120 CHECK_CALL(DecodeTbs, crl); //TODO
121 //include sequense and len to tbs
122 crl->tbs.len += crl->tbs.data - temp.data;
123 crl->tbs.data = temp.data;
125 CHECK_EQUAL(*(code.data), DER_SEQUENCE, PKI_INVALID_FORMAT);
126 CHECK_CALL(DecodeLength , &code, &length);
129 INC_BYTE_ARRAY(code, length); // skip algorithm identifier
130 //check_signature_algorithm
131 //1.2.840.10045.4.3.2
132 CHECK_DER_OID(temp, g_ECDSA_WITH_SHA256_OID, ECDSA_WITH_SHA256_OID_LEN, tempLen);
133 //decode_signature_value
134 CHECK_EQUAL(*(code.data), DER_BIT_STRING, PKI_INVALID_FORMAT);
135 CHECK_CALL(DecodeLength , &code, &length);
137 CHECK_EQUAL(*(code.data), DER_UNIVERSAL, PKI_INVALID_FORMAT);
138 CHECK_INC_BYTE_ARRAY(code, 1);
139 CHECK_EQUAL(*(code.data), DER_SEQUENCE, PKI_INVALID_FORMAT);
140 CHECK_CALL(DecodeLength , &code, &length);
142 COPY_DER_FIELD(code, crl, signR, DER_INTEGER, length);
144 COPY_DER_FIELD(code, crl, signS, DER_INTEGER, length);
145 if (caPubKey.data != NULL)
147 PARSE_SIGNATURE(crl);
148 CHECK_SIGN(*crl, caPubKey);
155 * Prints CRL to console.
157 PKIError PrintCRL(const CertificateList *const crl)
160 CHECK_NULL(crl, PKI_NULL_PASSED);
162 printf("\n-----BEGIN CRL-----\n");
163 PRINT_BYTE_ARRAY("ISSUER:\n", crl->issuer);
164 PRINT_BYTE_ARRAY("DATE:\n", crl->date);
165 PRINT_BYTE_ARRAY("TBS:\n", crl->tbs);
166 printf("-----END CRL-----\n");