f05662af7a161d51be0af1fb72dc5a0f354c1184
[framework/web/wrt-commons.git] / modules / vcore / src / vcore / CertificateVerifier.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*!
17  * @author      Bartlomiej Grzelewski (b.grzelewski@gmail.com)
18  * @version     0.1
19  * @file        CertificateVerifier.cpp
20  * @brief       This class integrates OCSP and CRL.
21  */
22 #include "CertificateVerifier.h"
23
24 #include <dpl/assert.h>
25 #include <dpl/foreach.h>
26 #include <dpl/log/log.h>
27
28 namespace ValidationCore {
29
30 CertificateVerifier::CertificateVerifier(bool enableOcsp, bool enableCrl)
31 : m_enableOcsp(enableOcsp)
32 , m_enableCrl(enableCrl)
33 {}
34
35 VerificationStatus CertificateVerifier::check(
36         CertificateCollection &certCollection) const
37 {
38     LogDebug("== Certificate collection validation start ==");
39     Assert(certCollection.isChain() && "Collection must form chain.");
40
41     VerificationStatus statusOcsp;
42     VerificationStatus statusCrl;
43
44     if (m_enableOcsp) {
45         statusOcsp = obtainOcspStatus(certCollection);
46     } else {
47         statusOcsp = VERIFICATION_STATUS_GOOD;
48     }
49
50     if (m_enableCrl) {
51         statusCrl = obtainCrlStatus(certCollection);
52     } else {
53         statusCrl = VERIFICATION_STATUS_GOOD;
54     }
55     LogDebug("== Certificate collection validation end ==");
56     return getStatus(statusOcsp, statusCrl);
57 }
58
59 VerificationStatus CertificateVerifier::obtainOcspStatus(
60         const CertificateCollection &chain) const
61 {
62     LogDebug("== Obtain ocsp status ==");
63     CachedOCSP ocsp;
64     return ocsp.check(chain);
65 }
66
67 VerificationStatus CertificateVerifier::obtainCrlStatus(
68         const CertificateCollection &chain) const
69 {
70     LogDebug("== Obtain crl status ==");
71     CachedCRL crl;
72     return crl.check(chain);
73 }
74
75 VerificationStatus CertificateVerifier::getStatus(
76         VerificationStatus ocsp,
77         VerificationStatus crl) const
78 {
79     if (ocsp == VERIFICATION_STATUS_REVOKED ||
80         crl == VERIFICATION_STATUS_REVOKED)
81     {
82         LogDebug("Return status: REVOKED");
83         return VERIFICATION_STATUS_REVOKED;
84     }
85
86     if (ocsp == VERIFICATION_STATUS_GOOD) {
87         LogDebug("Return status: GOOD");
88         return VERIFICATION_STATUS_GOOD;
89     }
90
91     if (ocsp == VERIFICATION_STATUS_UNKNOWN) {
92         LogDebug("Return status: UNKNOWN");
93         return VERIFICATION_STATUS_UNKNOWN;
94     }
95
96     if (ocsp == VERIFICATION_STATUS_NOT_SUPPORT) {
97         LogDebug("Return status: NOT_SUPPORT");
98         return VERIFICATION_STATUS_GOOD;
99     }
100
101     LogDebug("Return status: ERROR");
102     return VERIFICATION_STATUS_ERROR;
103 }
104
105 VerificationStatus CertificateVerifier::checkEndEntity(
106         CertificateCollectionList &collectionList) const
107 {
108     VerificationStatusSet statusOcsp;
109     VerificationStatusSet statusCrl;
110
111     if (m_enableOcsp) {
112         CachedOCSP ocsp;
113         FOREACH(it, collectionList){
114             statusOcsp.add(ocsp.checkEndEntity(*it));
115         }
116     } else {
117         statusOcsp.add(VERIFICATION_STATUS_GOOD);
118     }
119
120     if (m_enableCrl) {
121         CachedCRL crl;
122         FOREACH(it, collectionList){
123             statusCrl.add(crl.checkEndEntity(*it));
124         }
125     } else {
126         statusCrl.add(VERIFICATION_STATUS_GOOD);
127     }
128     LogDebug("== Certificate collection validateion end ==");
129     return getStatus(statusOcsp.convertToStatus(), statusCrl.convertToStatus());
130 }
131
132 } // namespace ValidationCore