Introduce new (much simpler) Exception type.
[platform/core/security/key-manager.git] / src / manager / crypto / sw-backend / store.cpp
1 /*
2  *  Copyright (c) 2000 - 2015 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       store.cpp
18  * @author     BartÅ‚omiej Grzelewski (b.grzelewski@samsung.com)
19  * @version    1.0
20  */
21 #include <memory>
22
23 #include <generic-backend/exception.h>
24 #include <sw-backend/key.h>
25 #include <sw-backend/store.h>
26 #include <sw-backend/internals.h>
27
28 namespace CKM {
29 namespace Crypto {
30 namespace SW {
31
32 Store::Store(CryptoBackend backendId)
33   : GStore(backendId)
34 {
35     // initialize openssl internals
36     Internals::initialize();
37 }
38
39 GKeyShPtr Store::getKey(const Token &token) {
40     if (token.backendId != m_backendId) {
41         ThrowErr(Exc::Crypto::WrongBackend, "Decider choose wrong backend!");
42     }
43
44     if (token.dataType.isKeyPrivate() || token.dataType.isKeyPublic()) {
45          return std::make_shared<AKey>(token.data, token.dataType);
46     }
47
48     if (token.dataType == DataType(DataType::KEY_AES)) {
49          return std::make_shared<SKey>(token.data, token.dataType);
50     }
51
52     if (token.dataType.isCertificate()) {
53         return std::make_shared<Cert>(token.data, token.dataType);
54     }
55
56     ThrowErr(Exc::Crypto::KeyNotSupported,
57         "This type of data is not supported by openssl backend: ", (int)token.dataType);
58 }
59
60 TokenPair Store::generateAKey(const CryptoAlgorithm &algorithm)
61 {
62     AlgoType keyType = AlgoType::RSA_GEN;
63     algorithm.getParam(ParamName::ALGO_TYPE, keyType);
64
65     if(keyType == AlgoType::RSA_GEN || keyType == AlgoType::DSA_GEN)
66     {
67         int keyLength = 0;
68         if(!algorithm.getParam(ParamName::GEN_KEY_LEN, keyLength))
69             ThrowErr(Exc::Crypto::InputParam, "Error, parameter GEN_KEY_LEN not found.");
70
71         if(keyType == AlgoType::RSA_GEN)
72             return Internals::createKeyPairRSA(m_backendId, keyLength);
73         else
74             return Internals::createKeyPairDSA(m_backendId, keyLength);
75     }
76     else if(keyType == AlgoType::ECDSA_GEN)
77     {
78         int ecType = 0;
79         if(!algorithm.getParam(ParamName::GEN_EC, ecType))
80             ThrowErr(Exc::Crypto::InputParam, "Error, parameter GEN_EC not found.");
81
82         return Internals::createKeyPairECDSA(m_backendId, static_cast<ElipticCurve>(ecType));
83     }
84     ThrowErr(Exc::Crypto::InputParam, "wrong key type");
85 }
86
87 Token Store::generateSKey(const CryptoAlgorithm &algorithm)
88 {
89     int keyLength = 0;
90     if(!algorithm.getParam(ParamName::GEN_KEY_LEN, keyLength))
91         ThrowErr(Exc::Crypto::InputParam, "Error, parameter GEN_KEY_LEN not found.");
92
93     return Internals::createKeyAES(m_backendId, keyLength);
94 }
95
96 Token Store::import(DataType dataType, const RawBuffer &buffer) {
97     return Token(m_backendId, dataType, buffer);
98 }
99
100 } // namespace SW
101 } // namespace Crypto
102 } // namespace CKM
103