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