Apply tizen 2.4 smartcard-service
[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                 if(b64 == NULL)
46                         return false;
47
48                 bmem = BIO_new(BIO_s_mem());
49
50                 if (newLineChar == false)
51                         BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
52
53                 b64 = BIO_push(b64, bmem);
54
55                 BIO_write(b64, buffer.getBuffer(), buffer.size());
56                 BIO_flush(b64);
57                 BIO_get_mem_ptr(b64, &bptr);
58
59                 result.assign((unsigned char *)bptr->data, bptr->length);
60
61                 BIO_free_all(b64);
62
63                 ret = true;
64
65                 return ret;
66         }
67
68         bool OpensslHelper::decodeBase64String(const char *buffer, ByteArray &result, bool newLineChar)
69         {
70                 ByteArray temp;
71
72                 temp.assign((unsigned char *)buffer, strlen(buffer));
73
74                 return decodeBase64String(temp, result, newLineChar);
75         }
76
77         bool OpensslHelper::decodeBase64String(const ByteArray &buffer, ByteArray &result, bool newLineChar)
78         {
79                 bool ret = false;
80                 unsigned int length = 0;
81                 char *temp;
82
83                 if (buffer.getBuffer() == NULL || buffer.size() == 0)
84                 {
85                         return ret;
86                 }
87
88                 length = buffer.size();
89
90                 temp = new char[length];
91                 if (temp != NULL)
92                 {
93                         BIO *b64, *bmem;
94
95                         memset(temp, 0, length);
96
97                         b64 = BIO_new(BIO_f_base64());
98                         if(b64 == NULL)
99                         {
100                                 delete []temp;
101                                 return false;
102                         }
103
104                         bmem = BIO_new_mem_buf((void *)buffer.getBuffer(), length);
105                         if (newLineChar == false)
106                                 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
107                         bmem = BIO_push(b64, bmem);
108
109                         length = BIO_read(bmem, temp, length);
110
111                         BIO_free_all(bmem);
112                         if(length > 0)
113                         {
114                                 result.assign((unsigned char *)temp, length);
115                                 ret = true;
116                         }
117                         else
118                         {
119                                 ret = false;
120                         }
121
122                         delete []temp;
123                 }
124                 else
125                 {
126                         _ERR("alloc failed");
127                 }
128
129                 return ret;
130         }
131
132         bool OpensslHelper::digestBuffer(const char *algorithm,
133                 const uint8_t *buffer, uint32_t length, ByteArray &result)
134         {
135                 ByteArray temp(buffer, length);
136
137                 return digestBuffer(algorithm, temp, result);
138         }
139
140         bool OpensslHelper::digestBuffer(const char *algorithm,
141                 const ByteArray &buffer, ByteArray &result)
142         {
143                 const EVP_MD *md;
144                 bool ret = false;
145
146                 if (algorithm == NULL || buffer.size() == 0)
147                 {
148                         return ret;
149                 }
150
151                 OpenSSL_add_all_digests();
152
153                 if ((md = EVP_get_digestbyname(algorithm)) != NULL)
154                 {
155                         uint8_t temp[EVP_MAX_MD_SIZE] = { 0, };
156                         EVP_MD_CTX mdCtx;
157                         unsigned int resultLen = 0;
158
159                         if (EVP_DigestInit(&mdCtx, md) > 0)
160                         {
161                                 if (EVP_DigestUpdate(&mdCtx, buffer.getBuffer(), buffer.size()) == 0)
162                                 {
163                                         _ERR("EVP_DigestUpdate failed");
164                                 }
165
166                                 if (EVP_DigestFinal(&mdCtx, temp, &resultLen) > 0 &&
167                                         resultLen > 0)
168                                 {
169                                         result.assign(temp, resultLen);
170                                         ret = true;
171                                 }
172                         }
173                 }
174                 else
175                 {
176                         _ERR("EVP_get_digestbyname(\"%s\") returns NULL", algorithm);
177                 }
178
179                 return ret;
180         }
181
182 } /* namespace smartcard_service_api */