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