Replace RawData with RawBuffer.
[platform/core/security/key-manager.git] / src / manager / client / client-certificate-impl.cpp
1 /* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
2  *
3  *  Licensed under the Apache License, Version 2.0 (the "License");
4  *  you may not use this file except in compliance with the License.
5  *  You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software
10  *  distributed under the License is distributed on an "AS IS" BASIS,
11  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  *  See the License for the specific language governing permissions and
13  *  limitations under the License
14  *
15  *
16  * @file        client-certificate-impl.cpp
17  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
18  * @version     1.0
19  * @brief       Key implementation.
20  */
21 #include <openssl/x509.h>
22
23 #include <dpl/log/log.h>
24
25 #include <client-certificate-impl.h>
26 #include <Base64.h>
27
28 namespace CKM {
29
30 CertificateImpl::CertificateImpl(const RawBuffer &der, Certificate::Format format) {
31     int size;
32     const unsigned char *ptr;
33     RawBuffer tmp;
34
35     if (Certificate::Format::FORM_BASE64 == format) {
36         Base64Decoder base64;
37         base64.reset();
38         base64.append(der);
39         base64.finalize();
40         tmp = base64.get();
41         ptr = reinterpret_cast<const unsigned char*>(tmp.data());
42         size = static_cast<int>(tmp.size());
43     } else {
44         ptr = reinterpret_cast<const unsigned char*>(der.data());
45         size = static_cast<int>(der.size());
46     }
47
48     m_x509 = d2i_X509(NULL, &ptr, size);
49     if (!m_x509) {
50         // TODO
51 //        LogError("Internal Openssl error in d2i_X509 function.");
52 //        ThrowMsg(Exception::OpensslInternalError,
53 //          "Internal Openssl error in d2i_X509 function.");
54     }
55 }
56
57 RawBuffer CertificateImpl::getDER(void) const {
58     unsigned char *rawDer = NULL;
59     int size = i2d_X509(m_x509, &rawDer);
60     if (!rawDer || size <= 0) {
61         // TODO
62 //        LogError("i2d_X509 failed");
63 //        ThrowMsg(Exception::OpensslInternalError,
64 //          "i2d_X509 failed");
65     }
66
67     RawBuffer output(
68         reinterpret_cast<char*>(rawDer),
69         reinterpret_cast<char*>(rawDer) + size);
70     OPENSSL_free(rawDer);
71     return output;
72 }
73
74 bool CertificateImpl::empty() const {
75     return m_x509 == NULL;
76 }
77
78 CertificateImpl::~CertificateImpl() {
79     X509_free(m_x509);
80 }
81
82 } // namespace CKM
83