Refactor log system
[platform/core/security/cert-svc.git] / vcore / src / vcore / CertificateLoader.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 #include <dpl/assert.h>
17 #include <openssl/x509v3.h>
18 #include <dpl/log/log.h>
19 #include <dpl/noncopyable.h>
20 #include <openssl/ecdsa.h>
21 #include <openssl/evp.h>
22
23 #include <vcore/Base64.h>
24 #include <vcore/CertificateLoader.h>
25
26 #ifdef TIZEN_FEATURE_CERT_SVC_OCSP_CRL
27 #include <vcore/SSLContainers.h>
28 #endif
29
30 namespace {
31 const int MIN_RSA_KEY_LENGTH = 1024;
32 } // namespace anonymous
33
34 namespace ValidationCore {
35 CertificateLoader::CertificateLoaderResult CertificateLoader::
36     loadCertificateBasedOnExponentAndModulus(const std::string &m_modulus,
37         const std::string &m_exponent)
38 {
39     (void) m_modulus;
40     (void) m_exponent;
41     LogError("Not implemented.");
42     return UNKNOWN_ERROR;
43 }
44
45 CertificateLoader::CertificateLoaderResult CertificateLoader::loadCertificate(
46         const std::string &storageName,
47         CertificateLoader::CertificateLoaderComparator *cmp)
48 {
49     (void) storageName;
50     (void) cmp;
51     LogError("Not Implemented");
52     return UNKNOWN_ERROR;
53 }
54
55 CertificateLoader::CertificateLoaderResult CertificateLoader::
56     loadCertificateBasedOnSubjectName(const std::string &subjectName)
57 {
58     (void) subjectName;
59     LogError("Not implemented.");
60     return UNKNOWN_ERROR;
61 }
62
63 CertificateLoader::CertificateLoaderResult CertificateLoader::
64     loadCertificateWithECKEY(const std::string &curveName,
65         const std::string &publicKey)
66 {
67     (void) curveName;
68     (void) publicKey;
69     LogError("Not implemented.");
70     return UNKNOWN_ERROR;
71 }
72
73 CertificateLoader::CertificateLoaderResult CertificateLoader::loadCertificateFromRawData(const std::string &rawData)
74 {
75     VcoreTry {
76         m_certificatePtr = CertificatePtr(new Certificate(rawData, Certificate::FORM_BASE64));
77     } VcoreCatch(Certificate::Exception::Base) {
78         LogWarning("Error reading certificate by openssl.");
79         return UNKNOWN_ERROR;
80     }
81
82     // Check the key length if sig algorithm is RSA
83     EVP_PKEY *pKey = X509_get_pubkey(m_certificatePtr->getX509());
84
85     if (pKey != NULL) {
86         if (pKey->type == EVP_PKEY_RSA) {
87             RSA* pRSA = pKey->pkey.rsa;
88
89             if (pRSA) {
90                 int keyLength = RSA_size(pRSA);
91
92                 // key Length (modulus) is in bytes
93                 keyLength <<= 3;
94                 LogDebug("RSA key length: " << keyLength << " bits");
95
96                 if (keyLength < MIN_RSA_KEY_LENGTH) {
97                     LogError("RSA key too short! Has only " << keyLength << " bits");
98                     return CERTIFICATE_SECURITY_ERROR;
99                 }
100             }
101         }
102     }
103
104     return NO_ERROR;
105 }
106
107 CertificateLoader::CertificateLoaderResult CertificateLoader::
108     loadCertificateBasedOnDSAComponents(const std::string& strP,
109         const std::string& strQ,
110         const std::string& strG,
111         const std::string& strY,
112         const std::string& strJ,
113         const std::string& strSeed,
114         const std::string& strPGenCounter)
115 {
116     (void) strP;
117     (void) strQ;
118     (void) strG;
119     (void) strY;
120     (void) strJ;
121     (void) strSeed;
122     (void) strPGenCounter;
123     LogError("Not implemented.");
124     return UNKNOWN_ERROR;
125 }
126
127 bool CertificateLoader::convertBase64NodeToBigNum(const std::string& strNode,
128         BIGNUM** ppBigNum)
129 {
130     (void) strNode;
131     (void) ppBigNum;
132     LogError("Not implemented.");
133     return false;
134 }
135
136 } // namespace ValidationCore
137