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