Refactor log system
[platform/core/security/cert-svc.git] / vcore / src / vcore / Base64.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 <algorithm>
17 #include <string>
18 #include <string.h>
19 #include <openssl/bio.h>
20 #include <openssl/evp.h>
21 #include <openssl/buffer.h>
22
23 #include <dpl/log/log.h>
24
25 #include <dpl/scoped_free.h>
26
27 #include <vcore/Base64.h>
28
29 namespace ValidationCore {
30 Base64Encoder::Base64Encoder() :
31     m_b64(0),
32     m_bmem(0),
33     m_finalized(false)
34 {
35 }
36
37 void Base64Encoder::append(const std::string &data)
38 {
39     if (m_finalized) {
40         LogWarning("Already finalized.");
41         VcoreThrowMsg(Exception::AlreadyFinalized, "Already finalized");
42     }
43
44     if (!m_b64) {
45         reset();
46     }
47     BIO_write(m_b64, data.c_str(), data.size());
48 }
49
50 void Base64Encoder::finalize()
51 {
52     if (m_finalized) {
53         LogWarning("Already finalized.");
54         VcoreThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
55     }
56     m_finalized = true;
57
58     if (BIO_flush(m_b64) != 1)
59         VcoreThrowMsg(Exception::InternalError, "Bio internal error");
60 }
61
62 std::string Base64Encoder::get()
63 {
64     if (!m_finalized) {
65         LogWarning("Not finalized");
66         VcoreThrowMsg(Exception::NotFinalized, "Not finalized");
67     }
68     BUF_MEM *bptr = 0;
69     BIO_get_mem_ptr(m_b64, &bptr);
70     if (bptr == 0) {
71         LogError("Bio internal error");
72         VcoreThrowMsg(Exception::InternalError, "Bio internal error");
73     }
74
75     if (bptr->length > 0) {
76         return std::string(bptr->data, bptr->length);
77     }
78     return std::string();
79 }
80
81 void Base64Encoder::reset()
82 {
83     m_finalized = false;
84     BIO_free_all(m_b64);
85     m_b64 = BIO_new(BIO_f_base64());
86     m_bmem = BIO_new(BIO_s_mem());
87     if (!m_b64 || !m_bmem) {
88         LogError("Error during allocation memory in BIO");
89         VcoreThrowMsg(Exception::InternalError,
90                  "Error during allocation memory in BIO");
91     }
92     BIO_set_flags(m_b64, BIO_FLAGS_BASE64_NO_NL);
93     m_b64 = BIO_push(m_b64, m_bmem);
94 }
95
96 Base64Encoder::~Base64Encoder()
97 {
98     BIO_free_all(m_b64);
99 }
100
101 Base64Decoder::Base64Decoder() :
102     m_finalized(false)
103 {
104 }
105
106 void Base64Decoder::append(const std::string &data)
107 {
108     if (m_finalized) {
109         LogWarning("Already finalized.");
110         VcoreThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
111     }
112     m_input.append(data);
113 }
114
115 static bool whiteCharacter(char a)
116 {
117     if (a == '\n') { return true; }
118     return false;
119 }
120
121 bool Base64Decoder::finalize()
122 {
123     if (m_finalized) {
124         LogWarning("Already finalized.");
125         VcoreThrowMsg(Exception::AlreadyFinalized, "Already finalized.");
126     }
127
128     m_finalized = true;
129
130     m_input.erase(std::remove_if(m_input.begin(),
131                                  m_input.end(),
132                                  whiteCharacter),
133                   m_input.end());
134
135     for (size_t i = 0; i<m_input.size(); ++i) {
136         if (isalnum(m_input[i])
137             || m_input[i] == '+'
138             || m_input[i] == '/'
139             || m_input[i] == '=')
140         {
141             continue;
142         }
143         LogError("Base64 input contains illegal chars: " << m_input[i]);
144         return false;
145     }
146
147     BIO *b64, *bmem;
148     size_t len = m_input.size();
149
150     VcoreDPL::ScopedFree<char> buffer(static_cast<char*>(malloc(len)));
151
152     if (!buffer) {
153         LogError("Error in malloc.");
154         VcoreThrowMsg(Exception::InternalError, "Error in malloc.");
155     }
156
157     memset(buffer.Get(), 0, len);
158     b64 = BIO_new(BIO_f_base64());
159     if (!b64) {
160         LogError("Couldn't create BIO object.");
161         VcoreThrowMsg(Exception::InternalError, "Couldn't create BIO object.");
162     }
163     BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
164     VcoreDPL::ScopedFree<char> tmp(strdup(m_input.c_str()));
165     m_input.clear();
166
167     bmem = BIO_new_mem_buf(tmp.Get(), len);
168
169     if (!bmem) {
170         BIO_free(b64);
171         LogError("Internal error in BIO");
172         VcoreThrowMsg(Exception::InternalError, "Internal error in BIO");
173     }
174
175     bmem = BIO_push(b64, bmem);
176
177     if (!bmem) {
178         BIO_free(b64);
179         LogError("Internal error in BIO");
180         VcoreThrowMsg(Exception::InternalError, "Internal error in BIO");
181     }
182
183     int readlen = BIO_read(bmem, buffer.Get(), len);
184     m_output.clear();
185
186     bool status = true;
187
188     if (readlen > 0) {
189         m_output.append(buffer.Get(), readlen);
190     } else {
191         status = false;
192     }
193
194     BIO_free_all(bmem);
195     return status;
196 }
197
198 std::string Base64Decoder::get() const
199 {
200     if (!m_finalized) {
201         LogWarning("Not finalized.");
202         VcoreThrowMsg(Exception::NotFinalized, "Not finalized");
203     }
204     return m_output;
205 }
206
207 void Base64Decoder::reset()
208 {
209     m_finalized = false;
210     m_input.clear();
211     m_output.clear();
212 }
213 } // namespace ValidationCore