2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
33 bool OpensslHelper::encodeBase64String(const ByteArray &buffer, ByteArray &result, bool newLineChar)
39 if (buffer.getLength() == 0)
44 b64 = BIO_new(BIO_f_base64());
45 bmem = BIO_new(BIO_s_mem());
47 if (newLineChar == false)
48 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
50 b64 = BIO_push(b64, bmem);
52 BIO_write(b64, buffer.getBuffer(), buffer.getLength());
54 BIO_get_mem_ptr(b64, &bptr);
56 result.setBuffer((unsigned char *)bptr->data, bptr->length);
65 bool OpensslHelper::decodeBase64String(const char *buffer, ByteArray &result, bool newLineChar)
69 temp.setBuffer((unsigned char *)buffer, strlen(buffer));
71 return decodeBase64String(temp, result, newLineChar);
74 bool OpensslHelper::decodeBase64String(const ByteArray &buffer, ByteArray &result, bool newLineChar)
77 unsigned int length = 0;
80 if (buffer.getBuffer() == NULL || buffer.getLength() == 0)
85 length = buffer.getLength();
87 temp = new char[length];
92 memset(temp, 0, length);
94 b64 = BIO_new(BIO_f_base64());
95 bmem = BIO_new_mem_buf(buffer.getBuffer(), length);
96 if (newLineChar == false)
97 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
98 bmem = BIO_push(b64, bmem);
100 length = BIO_read(bmem, temp, length);
104 result.setBuffer((unsigned char *)temp, length);
112 SCARD_DEBUG_ERR("alloc failed");
118 bool OpensslHelper::digestBuffer(const char *algorithm, const uint8_t *buffer, const uint32_t length, ByteArray &result)
120 ByteArray temp((uint8_t *)buffer, (uint32_t)length);
122 return digestBuffer(algorithm, temp, result);
125 bool OpensslHelper::digestBuffer(const char *algorithm, const ByteArray &buffer, ByteArray &result)
131 if (algorithm == NULL || buffer.getLength() == 0)
136 OpenSSL_add_all_digests();
138 if ((md = EVP_get_digestbyname(algorithm)) != NULL)
140 temp = new unsigned char[EVP_MAX_MD_SIZE];
144 unsigned int resultLen = 0;
146 memset(temp, 0, EVP_MAX_MD_SIZE);
148 EVP_DigestInit(&mdCtx, md);
149 if (EVP_DigestUpdate(&mdCtx, buffer.getBuffer(), buffer.getLength()) != 0)
151 SCARD_DEBUG_ERR("EVP_DigestUpdate failed");
153 EVP_DigestFinal(&mdCtx, temp, &resultLen);
155 result.setBuffer(temp, resultLen);
163 SCARD_DEBUG_ERR("alloc failed");
168 SCARD_DEBUG_ERR("EVP_get_digestbyname(\"%s\") returns NULL", algorithm);
174 } /* namespace smartcard_service_api */