Update wrt-commons_0.2.54
[framework/web/wrt-commons.git] / modules / encryption / src / resource_decryption.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file    resource_decryption.cpp
18  * @author  Soyoung Kim (sy037.kim@samsung.com)
19  * @version 1.0
20  * @brief   Implementation file for resource decryption
21  */
22 #include <dpl/encryption/resource_decryption.h>
23
24 #include <fcntl.h>
25 #include <string>
26 #include <dpl/log/log.h>
27 #include <dpl/exception.h>
28
29 namespace {
30 inline std::string GetDefaultEncryptKeyPath() {
31     return "/opt/apps/widget/data/";
32 }
33 }
34 namespace WRTDecryptor{
35 ResourceDecryptor::ResourceDecryptor() :
36     m_decKey(NULL)
37 {
38     LogDebug("Started Decryption");
39 }
40
41 ResourceDecryptor::ResourceDecryptor(std::string userKey) :
42     m_decKey(NULL)
43 {
44     LogDebug("Finished Decryption");
45     SetDecryptionKey(userKey);
46 }
47
48 ResourceDecryptor::~ResourceDecryptor()
49 {
50     delete m_decKey;
51 }
52
53 void ResourceDecryptor::SetDecryptionKey(std::string userKey)
54 {
55     /* TODO : get key from secure storage */
56     std::string keyPath = GetDefaultEncryptKeyPath() + userKey + "_dec";
57     LogDebug("Description Key path : " << keyPath);
58
59     FILE* fp = fopen(keyPath.c_str(), "rb");
60     if (fp == NULL) {
61         ThrowMsg(ResourceDecryptor::Exception::GetDecKeyFailed,
62                 "Failed to get decryption key");
63     }
64     m_decKey = new AES_KEY;
65     fread(m_decKey, 1, sizeof(AES_KEY),fp);
66     fclose(fp);
67 }
68
69 AES_KEY* ResourceDecryptor::GetDecryptionKey()
70 {
71     return m_decKey;
72 }
73
74 void ResourceDecryptor::GetDecryptedChunk(unsigned char*
75         inBuf, unsigned char* decBuf, size_t inBufSize)
76 {
77     Assert(decBuf);
78     Assert(m_decKey);
79     if (decBuf == NULL || m_decKey == NULL) {
80         ThrowMsg(ResourceDecryptor::Exception::EncryptionFailed,
81                 "Failed to Get Decryption Chunk");
82     }
83     unsigned char ivec[16] = {0, };
84
85     AES_cbc_encrypt(inBuf, decBuf, inBufSize, m_decKey, ivec, AES_DECRYPT);
86     LogDebug("Success decryption");
87 }
88
89 } //namespace WRTDecryptor