sync with tizen_2.0
[platform/framework/native/appfw.git] / src / security / FSecKeyPair.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FSecKeyPair.cpp
20  * @brief               This is the implementation file for KeyPair class.
21  */
22 #include <FBaseResult.h>
23 #include <FBaseErrors.h>
24 #include <FSecKeyPair.h>
25 #include <FSecPublicKey.h>
26 #include <FSecPrivateKey.h>
27 #include <FBaseSysLog.h>
28
29 using namespace Tizen::Base;
30 using namespace Tizen::Security;
31
32
33 namespace Tizen { namespace Security
34 {
35
36 KeyPair::KeyPair(void)
37         : __pPubKey(null)
38         , __pPriKey(null)
39         , __pKeyPairImpl(null)
40 {
41 }
42
43 KeyPair::~KeyPair(void)
44 {
45         delete __pPubKey;
46         delete __pPriKey;
47 }
48
49 result
50 KeyPair::Construct(IPublicKey& publicKey, IPrivateKey& privateKey)
51 {
52         result r = E_SUCCESS;
53
54         SysAssertf(__pPubKey == null && __pPriKey == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
55
56         __pPubKey = new (std::nothrow) PublicKey();
57         SysTryReturn(NID_SEC, __pPubKey != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
58
59         __pPriKey = new (std::nothrow) PrivateKey();
60         SysTryReturn(NID_SEC, __pPriKey != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
61
62         *__pPubKey = publicKey;
63         *__pPriKey = privateKey;
64
65         return r;
66 }
67
68 IPublicKey*
69 KeyPair::GetPublicKey(void) const
70 {
71         return __pPubKey;
72 }
73
74 IPrivateKey*
75 KeyPair::GetPrivateKey(void) const
76 {
77         return __pPriKey;
78 }
79
80 result
81 KeyPair::SetPublicKey(const ByteBuffer& pubKey)
82 {
83         result r = E_SUCCESS;
84
85         SysTryReturn(NID_SEC, static_cast< int >(pubKey.GetRemaining()) > 0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The input public key size should be positive.");
86
87         delete __pPubKey;
88
89         __pPubKey = new (std::nothrow) PublicKey();
90         SysTryReturn(NID_SEC, __pPubKey != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
91
92         r = __pPubKey->SetKey(pubKey);
93         SysTryReturn(NID_SEC, !IsFailed(r), r, r, "[%s] Failed to call set key operation.", GetErrorMessage(r));
94
95         return r;
96 }
97
98 result
99 KeyPair::SetPrivateKey(const ByteBuffer& privKey)
100 {
101         result r = E_SUCCESS;
102
103         SysTryReturn(NID_SEC, privKey.GetRemaining() > 0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The input private key size should be positive.");
104
105         delete __pPriKey;
106
107         __pPriKey = new (std::nothrow) PrivateKey();
108         SysTryReturn(NID_SEC, __pPriKey != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
109
110         r = __pPriKey->SetKey(privKey);
111         SysTryReturn(NID_SEC, !IsFailed(r), r, r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
112
113         return r;
114 }
115
116 } } //Tizen::Security