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