update the latest source
[platform/core/connectivity/smartcard-service.git] / common / OpensslHelper.cpp
1 /*
2 * Copyright (c) 2012 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
17 /* standard library header */
18 #include <stdio.h>
19 #include <string.h>
20 #include <openssl/evp.h>
21 #include <openssl/bio.h>
22 #include <openssl/buffer.h>
23
24 /* SLP library header */
25
26 /* local header */
27 #include "Debug.h"
28 #include "ByteArray.h"
29 #include "OpensslHelper.h"
30
31 namespace smartcard_service_api
32 {
33         bool OpensslHelper::encodeBase64String(const ByteArray &buffer, ByteArray &result, bool newLineChar)
34         {
35                 bool ret = false;
36                 BUF_MEM *bptr;
37                 BIO *b64, *bmem;
38
39                 if (buffer.getLength() == 0)
40                 {
41                         return ret;
42                 }
43
44                 b64 = BIO_new(BIO_f_base64());
45                 bmem = BIO_new(BIO_s_mem());
46
47                 if (newLineChar == false)
48                         BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
49
50                 b64 = BIO_push(b64, bmem);
51
52                 BIO_write(b64, buffer.getBuffer(), buffer.getLength());
53                 BIO_flush(b64);
54                 BIO_get_mem_ptr(b64, &bptr);
55
56                 result.setBuffer((unsigned char *)bptr->data, bptr->length);
57
58                 BIO_free_all(b64);
59
60                 ret = true;
61
62                 return ret;
63         }
64
65         bool OpensslHelper::decodeBase64String(const char *buffer, ByteArray &result, bool newLineChar)
66         {
67                 ByteArray temp;
68
69                 temp.setBuffer((unsigned char *)buffer, strlen(buffer));
70
71                 return decodeBase64String(temp, result, newLineChar);
72         }
73
74         bool OpensslHelper::decodeBase64String(const ByteArray &buffer, ByteArray &result, bool newLineChar)
75         {
76                 bool ret = false;
77                 unsigned int length = 0;
78                 char *temp;
79
80                 if (buffer.getBuffer() == NULL || buffer.getLength() == 0)
81                 {
82                         return ret;
83                 }
84
85                 length = buffer.getLength();
86
87                 temp = new char[length];
88                 if (temp != NULL)
89                 {
90                         BIO *b64, *bmem;
91
92                         memset(temp, 0, length);
93
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);
99
100                         length = BIO_read(bmem, temp, length);
101
102                         BIO_free_all(bmem);
103
104                         result.setBuffer((unsigned char *)temp, length);
105
106                         delete []temp;
107
108                         ret = true;
109                 }
110                 else
111                 {
112                         SCARD_DEBUG_ERR("alloc failed");
113                 }
114
115                 return ret;
116         }
117
118         bool OpensslHelper::digestBuffer(const char *algorithm, const uint8_t *buffer, const uint32_t length, ByteArray &result)
119         {
120                 ByteArray temp((uint8_t *)buffer, (uint32_t)length);
121
122                 return digestBuffer(algorithm, temp, result);
123         }
124
125         bool OpensslHelper::digestBuffer(const char *algorithm, const ByteArray &buffer, ByteArray &result)
126         {
127                 const EVP_MD *md;
128                 unsigned char *temp;
129                 bool ret = false;
130
131                 if (algorithm == NULL || buffer.getLength() == 0)
132                 {
133                         return ret;
134                 }
135
136                 OpenSSL_add_all_digests();
137
138                 if ((md = EVP_get_digestbyname(algorithm)) != NULL)
139                 {
140                         temp = new unsigned char[EVP_MAX_MD_SIZE];
141                         if (temp != NULL)
142                         {
143                                 EVP_MD_CTX mdCtx;
144                                 unsigned int resultLen = 0;
145
146                                 memset(temp, 0, EVP_MAX_MD_SIZE);
147
148                                 EVP_DigestInit(&mdCtx, md);
149                                 EVP_DigestUpdate(&mdCtx, buffer.getBuffer(), buffer.getLength());
150                                 EVP_DigestFinal(&mdCtx, temp, &resultLen);
151
152                                 result.setBuffer(temp, resultLen);
153
154                                 delete []temp;
155
156                                 ret = true;
157                         }
158                         else
159                         {
160                                 SCARD_DEBUG_ERR("alloc failed");
161                         }
162                 }
163                 else
164                 {
165                         SCARD_DEBUG_ERR("EVP_get_digestbyname(\"%s\") returns NULL", algorithm);
166                 }
167
168                 return ret;
169         }
170
171 } /* namespace smartcard_service_api */