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.size() == 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.size());
53                 BIO_flush(b64);
54                 BIO_get_mem_ptr(b64, &bptr);
55
56                 result.assign((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.assign((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.size() == 0)
81                 {
82                         return ret;
83                 }
84
85                 length = buffer.size();
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((void *)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.assign((unsigned char *)temp, length);
105
106                         delete []temp;
107
108                         ret = true;
109                 }
110                 else
111                 {
112                         _ERR("alloc failed");
113                 }
114
115                 return ret;
116         }
117
118         bool OpensslHelper::digestBuffer(const char *algorithm,
119                 const uint8_t *buffer, uint32_t length, ByteArray &result)
120         {
121                 ByteArray temp(buffer, length);
122
123                 return digestBuffer(algorithm, temp, result);
124         }
125
126         bool OpensslHelper::digestBuffer(const char *algorithm,
127                 const ByteArray &buffer, ByteArray &result)
128         {
129                 const EVP_MD *md;
130                 bool ret = false;
131
132                 if (algorithm == NULL || buffer.size() == 0)
133                 {
134                         return ret;
135                 }
136
137                 OpenSSL_add_all_digests();
138
139                 if ((md = EVP_get_digestbyname(algorithm)) != NULL)
140                 {
141                         uint8_t temp[EVP_MAX_MD_SIZE] = { 0, };
142                         EVP_MD_CTX mdCtx;
143                         unsigned int resultLen = 0;
144
145                         if (EVP_DigestInit(&mdCtx, md) > 0)
146                         {
147                                 if (EVP_DigestUpdate(&mdCtx, buffer.getBuffer(), buffer.size()) == 0)
148                                 {
149                                         _ERR("EVP_DigestUpdate failed");
150                                 }
151
152                                 if (EVP_DigestFinal(&mdCtx, temp, &resultLen) > 0 &&
153                                         resultLen > 0)
154                                 {
155                                         result.assign(temp, resultLen);
156                                         ret = true;
157                                 }
158                         }
159                 }
160                 else
161                 {
162                         _ERR("EVP_get_digestbyname(\"%s\") returns NULL", algorithm);
163                 }
164
165                 return ret;
166         }
167
168 } /* namespace smartcard_service_api */