92c24b01813cd30ba7e5af5f35e4f6b85aa78b11
[platform/core/security/key-manager.git] / src / manager / common / protocols.cpp
1 /*
2  *  Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Bumjin Im <bj.im@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  *
18  * @file        protocols.cpp
19  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
20  * @author      Zofia Abramowska (z.abramowska@samsung.com)
21  * @version     1.0
22  * @brief       List of all protocols supported by Central Key Manager.
23  */
24
25 #include <protocols.h>
26
27 #include <dpl/serialization.h>
28 #include <ckm/ckm-type.h>
29
30 namespace CKM {
31
32 char const * const SERVICE_SOCKET_ECHO = "/tmp/.central-key-manager-echo.sock";
33 char const * const SERVICE_SOCKET_CKM_CONTROL = "/tmp/.central-key-manager-api-control.sock";
34 char const * const SERVICE_SOCKET_CKM_STORAGE = "/tmp/.central-key-manager-api-storage.sock";
35 char const * const SERVICE_SOCKET_OCSP = "/tmp/.central-key-manager-api-ocsp.sock";
36 char const * const LABEL_NAME_SEPARATOR = " ";
37 char const * const LABEL_SYSTEM_DB = "/";
38
39
40 PKCS12Serializable::PKCS12Serializable() {}
41 PKCS12Serializable::PKCS12Serializable(const PKCS12 &pkcs)
42     : PKCS12Impl(pkcs)
43 {}
44
45 PKCS12Serializable::PKCS12Serializable(IStream &stream)
46 {
47     // key
48     size_t numKeys;
49     Deserialization::Deserialize(stream, numKeys);
50     if(numKeys > 0) {
51         int keyType;
52         RawBuffer keyData;
53         Deserialization::Deserialize(stream, keyType);
54         Deserialization::Deserialize(stream, keyData);
55         m_pkey = CKM::Key::create(keyData);
56     }
57
58     // cert
59     size_t numCerts;
60     Deserialization::Deserialize(stream, numCerts);
61     if(numCerts > 0) {
62         RawBuffer certData;
63         Deserialization::Deserialize(stream, certData);
64         m_cert = CKM::Certificate::create(certData, DataFormat::FORM_DER);
65     }
66
67     // CA chain
68     size_t num_CA;
69     Deserialization::Deserialize(stream, num_CA);
70     for(size_t i=0; i<num_CA; i++)
71     {
72         RawBuffer CAcertData;
73         Deserialization::Deserialize(stream, CAcertData);
74         m_ca.push_back(CKM::Certificate::create(CAcertData, DataFormat::FORM_DER));
75     }
76 }
77 PKCS12Serializable::PKCS12Serializable(const KeyShPtr &privKey, const CertificateShPtr &cert, const CertificateShPtrVector &chainCerts)
78 {
79     m_pkey = privKey;
80     m_cert = cert;
81     m_ca = chainCerts;
82 }
83
84 void PKCS12Serializable::Serialize(IStream &stream) const
85 {
86     // key
87     Key *keyPtr = getKey().get();
88     bool isAnyKeyPresent = (getKey().get()!=NULL);
89
90     // logics if PKCS is correct or not is on the service side.
91     // sending number of keys and certificates to allow proper parsing on the service side.
92     // (what if no key or cert present? attempt to deserialize a not present key/cert would
93     // throw an error and close the connection).
94     Serialization::Serialize(stream, static_cast<size_t>(isAnyKeyPresent?1:0));
95     if(keyPtr) {
96         Serialization::Serialize(stream, DataType(keyPtr->getType()));
97         Serialization::Serialize(stream, keyPtr->getDER());
98     }
99
100     bool isAnyCertPresent = (getCertificate().get()!=NULL);
101     Serialization::Serialize(stream, static_cast<size_t>(isAnyCertPresent?1:0));
102     if(isAnyCertPresent) {
103         Serialization::Serialize(stream, getCertificate().get()->getDER());
104     }
105
106     // CA chain
107     Serialization::Serialize(stream, getCaCertificateShPtrVector().size());
108     for(auto it : getCaCertificateShPtrVector())
109         Serialization::Serialize(stream, it->getDER());
110 };
111
112
113 CryptoAlgorithmSerializable::CryptoAlgorithmSerializable() {}
114 CryptoAlgorithmSerializable::CryptoAlgorithmSerializable(CryptoAlgorithm &&algo) :
115         CryptoAlgorithm(std::move(algo))
116 {
117 }
118
119 CryptoAlgorithmSerializable::CryptoAlgorithmSerializable(IStream &stream)
120 {
121     size_t plen = 0;
122     int type;
123     Deserializer<int,size_t>::Deserialize(stream, type, plen);
124     m_type = static_cast<AlgoType>(type);
125     while(plen) {
126         ParamName name;
127         uint64_t integer;
128         RawBuffer buffer;
129         int tmpName;
130         Deserializer<int>::Deserialize(stream, tmpName);
131         name = static_cast<ParamName>(tmpName);
132         switch (name) {
133         case ParamName::ED_IV:
134         case ParamName::ED_CTR:
135         case ParamName::ED_AAD:
136         case ParamName::ED_LABEL:
137             Deserializer<RawBuffer>::Deserialize(stream, buffer);
138             m_params.emplace(name, BufferParam::create(buffer));
139             break;
140
141         case ParamName::ED_CTR_LEN:
142         case ParamName::ED_TAG_LEN:
143         case ParamName::GEN_KEY_LEN:
144         case ParamName::GEN_EC:
145         case ParamName::SV_HASH_ALGO:
146         case ParamName::SV_RSA_PADDING:
147             Deserializer<uint64_t>::Deserialize(stream, integer);
148             m_params.emplace(name, IntParam::create(integer));
149             break;
150
151         default:
152             ThrowMsg(UnsupportedParam, "Unsupported param name");
153         }
154         plen--;
155     }
156 }
157
158 void CryptoAlgorithmSerializable::Serialize(IStream &stream) const
159 {
160     Serializer<int,size_t>::Serialize(stream, static_cast<int>(m_type), m_params.size());
161     for(const auto& it : m_params) {
162         Serializer<int>::Serialize(stream, static_cast<int>(it.first));
163         uint64_t integer;
164         RawBuffer buffer;
165         if (CKM_API_SUCCESS == it.second->getInt(integer))
166             Serializer<uint64_t>::Serialize(stream, integer);
167         else if (CKM_API_SUCCESS == it.second->getBuffer(buffer))
168             Serializer<RawBuffer>::Serialize(stream, buffer);
169         else
170             ThrowMsg(UnsupportedParam, "Unsupported param type");
171     }
172
173 }
174
175 } // namespace CKM
176