Merge "Fix compilation warnning for locales." into devel_3.0_main
[platform/framework/native/appfw.git] / src / security / FSecKeyPair.cpp
1 //
2 // Copyright (c) 2012 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 /**
18  * @file                FSecKeyPair.cpp
19  * @brief               This is the implementation file for KeyPair class.
20  */
21 #include <FBaseResult.h>
22 #include <FBaseErrors.h>
23 #include <FSecKeyPair.h>
24 #include <FSecPublicKey.h>
25 #include <FSecPrivateKey.h>
26 #include <FBaseSysLog.h>
27
28 using namespace Tizen::Base;
29 using namespace Tizen::Security;
30
31
32 namespace Tizen { namespace Security
33 {
34
35 KeyPair::KeyPair(void)
36         : __pPubKey(null)
37         , __pPriKey(null)
38         , __pKeyPairImpl(null)
39 {
40 }
41
42 KeyPair::~KeyPair(void)
43 {
44         delete __pPubKey;
45         delete __pPriKey;
46 }
47
48 result
49 KeyPair::Construct(IPublicKey& publicKey, IPrivateKey& privateKey)
50 {
51         result r = E_SUCCESS;
52
53         SysAssertf(__pPubKey == null && __pPriKey == null, "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class");
54
55         __pPubKey = new (std::nothrow) PublicKey();
56         SysTryReturn(NID_SEC, __pPubKey != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
57
58         __pPriKey = new (std::nothrow) PrivateKey();
59         SysTryReturn(NID_SEC, __pPriKey != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
60
61         *__pPubKey = publicKey;
62         *__pPriKey = privateKey;
63
64         return r;
65 }
66
67 IPublicKey*
68 KeyPair::GetPublicKey(void) const
69 {
70         return __pPubKey;
71 }
72
73 IPrivateKey*
74 KeyPair::GetPrivateKey(void) const
75 {
76         return __pPriKey;
77 }
78
79 result
80 KeyPair::SetPublicKey(const ByteBuffer& pubKey)
81 {
82         result r = E_SUCCESS;
83
84         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.");
85
86         delete __pPubKey;
87
88         __pPubKey = new (std::nothrow) PublicKey();
89         SysTryReturn(NID_SEC, __pPubKey != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
90
91         r = __pPubKey->SetKey(pubKey);
92         SysTryReturn(NID_SEC, !IsFailed(r), r, r, "[%s] Failed to call set key operation.", GetErrorMessage(r));
93
94         return r;
95 }
96
97 result
98 KeyPair::SetPrivateKey(const ByteBuffer& privKey)
99 {
100         result r = E_SUCCESS;
101
102         SysTryReturn(NID_SEC, privKey.GetRemaining() > 0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The input private key size should be positive.");
103
104         delete __pPriKey;
105
106         __pPriKey = new (std::nothrow) PrivateKey();
107         SysTryReturn(NID_SEC, __pPriKey != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
108
109         r = __pPriKey->SetKey(privKey);
110         SysTryReturn(NID_SEC, !IsFailed(r), r, r, "[%s] Failed to do set key operation.", GetErrorMessage(r));
111
112         return r;
113 }
114
115 } } //Tizen::Security