2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* standard library header */
20 #include <openssl/evp.h>
21 #include <openssl/bio.h>
22 #include <openssl/buffer.h>
24 /* SLP library header */
28 #include "ByteArray.h"
29 #include "OpensslHelper.h"
31 namespace smartcard_service_api
34 bool OpensslHelper::encodeBase64String(const ByteArray &buffer, ByteArray &result, bool newLineChar)
40 if (buffer.size() == 0)
45 b64 = BIO_new(BIO_f_base64());
49 bmem = BIO_new(BIO_s_mem());
51 if (newLineChar == false)
52 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
54 b64 = BIO_push(b64, bmem);
56 BIO_write(b64, buffer.getBuffer(), buffer.size());
58 BIO_get_mem_ptr(b64, &bptr);
60 result.assign((unsigned char *)bptr->data, bptr->length);
69 bool OpensslHelper::decodeBase64String(const char *buffer, ByteArray &result, bool newLineChar)
73 temp.assign((unsigned char *)buffer, strlen(buffer));
75 return decodeBase64String(temp, result, newLineChar);
78 bool OpensslHelper::decodeBase64String(const ByteArray &buffer, ByteArray &result, bool newLineChar)
81 unsigned int length = 0;
84 if (buffer.getBuffer() == NULL || buffer.size() == 0) {
88 length = buffer.size();
90 temp = new char[length];
94 memset(temp, 0, length);
96 b64 = BIO_new(BIO_f_base64());
102 bmem = BIO_new_mem_buf((void *)buffer.getBuffer(), length);
103 if (newLineChar == false)
104 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
105 bmem = BIO_push(b64, bmem);
107 length = BIO_read(bmem, temp, length);
111 result.assign((unsigned char *)temp, length);
119 _ERR("alloc failed");
125 bool OpensslHelper::digestBuffer(const char *algorithm,
126 const uint8_t *buffer, uint32_t length, ByteArray &result)
128 ByteArray temp(buffer, length);
130 return digestBuffer(algorithm, temp, result);
133 bool OpensslHelper::digestBuffer(const char *algorithm,
134 const ByteArray &buffer, ByteArray &result)
139 if (algorithm == NULL || buffer.size() == 0) {
143 OpenSSL_add_all_digests();
145 if ((md = EVP_get_digestbyname(algorithm)) != NULL) {
146 uint8_t temp[EVP_MAX_MD_SIZE] = { 0, };
147 #if OPENSSL_VERSION_NUMBER < 0x10100000L // OpenSSL 1.0.2
149 #else // OpenSSL 1.1.1
151 mdCtx = EVP_MD_CTX_new();
153 unsigned int resultLen = 0;
155 #if OPENSSL_VERSION_NUMBER < 0x10100000L // OpenSSL 1.0.2
156 if (EVP_DigestInit(&mdCtx, md) > 0) {
157 if (EVP_DigestUpdate(&mdCtx, buffer.getBuffer(), buffer.size()) == 0) {
158 #else // OpenSSL 1.1.1
159 if (EVP_DigestInit(mdCtx, md) > 0) {
160 if (EVP_DigestUpdate(mdCtx, buffer.getBuffer(), buffer.size()) == 0) {
162 _ERR("EVP_DigestUpdate failed");
165 #if OPENSSL_VERSION_NUMBER < 0x10100000L // OpenSSL 1.0.2
166 if (EVP_DigestFinal(&mdCtx, temp, &resultLen) > 0 &&
168 #else // OpenSSL 1.1.1
169 if (EVP_DigestFinal(mdCtx, temp, &resultLen) > 0 &&
172 result.assign(temp, resultLen);
176 #if OPENSSL_VERSION_NUMBER >= 0x10100000L // OpenSSL 1.1.1
177 EVP_MD_CTX_free(mdCtx);
180 _ERR("EVP_get_digestbyname(\"%s\") returns NULL", algorithm);
187 } /* namespace smartcard_service_api */