Disable security version discovery
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / adapter_util / pkix / crl.c
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
22
23 #include "crl.h"
24 #include "byte_array.h"
25 #include "der_dec.h"
26 #include "sn_store.h"
27 #include "der_dec.h"
28 #include "crypto_adapter.h"
29
30
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];
34
35 /*
36  *   TBSCertList  ::=  SEQUENCE  {
37  *       version                 Version OPTIONAL,
38  *                                     -- if present, MUST be v2
39  *        signature               AlgorithmIdentifier,
40  *        issuer                  Name,
41  *        thisUpdate              Time,
42  *        revokedCertificates     SEQUENCE OF SEQUENCE  {
43  *             userCertificate         CertificateSerialNumber,
44  *             revocationDate          Time
45  *                                  }  OPTIONAL,
46  *                                  }
47 */
48
49
50 /**
51  * Decodes TBS of CRL.
52  */
53 static PKIError DecodeTbs(CertificateList *const crl)
54 {
55     FUNCTION_INIT(
56         size_t length;
57         ByteArray tbs = crl->tbs, temp;
58         CHECK_NULL(crl, PKI_NULL_PASSED);
59         ByteArray sn = BYTE_ARRAY_INITIALIZER;
60         FreeSNStore();
61     );
62
63     CHECK_EQUAL(*(tbs.data), DER_SEQUENCE, PKI_INVALID_FORMAT);
64     CHECK_CALL(DecodeLength , &tbs, &length);
65
66     INC_BYTE_ARRAY(tbs, length); // skip algorithm identifier
67     //1.2.840.10045.4.3.2
68     //copy issuer X.500 name
69     COPY_DER_FIELD(tbs, crl, issuer, DER_SEQUENCE, length);
70     //copy date
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);
76     temp.data = tbs.data;
77     temp.len = length;
78     while (tbs.data < temp.data + temp.len)
79     {
80         CHECK_EQUAL(*(tbs.data), DER_SEQUENCE, PKI_INVALID_FORMAT);
81         CHECK_CALL(DecodeLength , &tbs, &length);
82         //serial number
83         CHECK_EQUAL(*(tbs.data), DER_INTEGER, PKI_INVALID_FORMAT);
84         CHECK_CALL(DecodeLength , &tbs, &length);
85         sn.data = tbs.data;
86         sn.len = length;
87         CHECK_CALL(StoreSerialNumber, sn);
88         INC_BYTE_ARRAY(tbs, length);
89         SKIP_DER_FIELD(tbs, DER_UTC_TIME, length);
90     }
91     FUNCTION_CLEAR();
92 }
93
94 /*
95  * CertificateList  ::=  SEQUENCE  {
96  *      tbsCertList          TBSCertList,
97  *      signatureAlgorithm   AlgorithmIdentifier,
98  *      signatureValue       BIT STRING  }
99 */
100
101 /**
102  * Decodes certificate in DER format.
103  */
104 PKIError DecodeCertificateList(ByteArray code, CertificateList *crl, ByteArray caPubKey)
105 {
106     FUNCTION_INIT(
107         size_t length, tempLen;
108         ByteArray temp;
109         CHECK_NULL(crl, PKI_NULL_PASSED);
110         CHECK_NULL(code.data, PKI_NULL_PASSED);
111     );
112     CHECK_EQUAL(*(code.data), DER_SEQUENCE, PKI_INVALID_FORMAT);
113     CHECK_CALL(DecodeLength , &code, &length);
114     //store sequence position
115     temp = code;
116     //TODO check length of TBS
117     //copy tbs
118     COPY_DER_FIELD(code, crl, tbs, DER_SEQUENCE, length);
119     //decode tbs
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;
124
125     CHECK_EQUAL(*(code.data), DER_SEQUENCE, PKI_INVALID_FORMAT);
126     CHECK_CALL(DecodeLength , &code, &length);
127     //copy to temp
128     temp = code;
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);
136     //skip DER_UNIVERSAL
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);
141     //copy sign r value
142     COPY_DER_FIELD(code, crl, signR, DER_INTEGER, length);
143     //copy sign s value
144     COPY_DER_FIELD(code, crl, signS, DER_INTEGER, length);
145     if (caPubKey.data != NULL)
146     {
147         PARSE_SIGNATURE(crl);
148         CHECK_SIGN(*crl, caPubKey);
149     }
150     FUNCTION_CLEAR();
151 }
152
153 #ifdef X509_DEBUG
154 /**
155  * Prints CRL to console.
156  */
157 PKIError PrintCRL(const CertificateList *const crl)
158 {
159     FUNCTION_INIT(
160         CHECK_NULL(crl, PKI_NULL_PASSED);
161     );
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");
167     FUNCTION_CLEAR(
168     );
169 }
170 #endif