Fix the prevent problems
[platform/core/connectivity/smartcard-service.git] / common / OpensslHelper.cpp
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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                 bool ret = false;
129
130                 if (algorithm == NULL || buffer.getLength() == 0)
131                 {
132                         return ret;
133                 }
134
135                 OpenSSL_add_all_digests();
136
137                 if ((md = EVP_get_digestbyname(algorithm)) != NULL)
138                 {
139                         uint8_t temp[EVP_MAX_MD_SIZE] = { 0, };
140                         EVP_MD_CTX mdCtx;
141                         unsigned int resultLen = 0;
142
143                         if (EVP_DigestInit(&mdCtx, md) > 0)
144                         {
145                                 if (EVP_DigestUpdate(&mdCtx, buffer.getBuffer(), buffer.getLength()) == 0)
146                                 {
147                                         SCARD_DEBUG_ERR("EVP_DigestUpdate failed");
148                                 }
149
150                                 if (EVP_DigestFinal(&mdCtx, temp, &resultLen) > 0 &&
151                                         resultLen > 0)
152                                 {
153                                         result.setBuffer(temp, resultLen);
154                                         ret = true;
155                                 }
156                         }
157                 }
158                 else
159                 {
160                         SCARD_DEBUG_ERR("EVP_get_digestbyname(\"%s\") returns NULL", algorithm);
161                 }
162
163                 return ret;
164         }
165
166 } /* namespace smartcard_service_api */