summaryrefslogtreecommitdiff
path: root/src/manager/service/ckm-service.cpp
blob: 6773c478e7a1de0aabab5edce8069e69535dbf8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
 *  Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 *
 *
 * @file        ckm-service.cpp
 * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
 * @version     1.0
 * @brief       CKM service implementation.
 */

#include <protocols.h>

#include <dpl/serialization.h>
#include <dpl/log/log.h>

#include <ckm-service.h>
#include <ckm-logic.h>
#include <initial-value-loader.h>

namespace {
const CKM::InterfaceID SOCKET_ID_CONTROL = 0;
const CKM::InterfaceID SOCKET_ID_STORAGE = 1;
} // namespace anonymous

namespace CKM {

CKMService::CKMService() :
    m_logic(new CKMLogic)
{
    InitialValues::LoadFiles(*m_logic);
}

CKMService::~CKMService()
{
    delete m_logic;
}

void CKMService::Start()
{
    Create();
}

void CKMService::Stop()
{
    Join();
}

GenericSocketService::ServiceDescriptionVector CKMService::GetServiceDescription()
{
    return ServiceDescriptionVector {
        {SERVICE_SOCKET_CKM_CONTROL, "http://tizen.org/privilege/keymanager.admin", SOCKET_ID_CONTROL},
        {SERVICE_SOCKET_CKM_STORAGE, "http://tizen.org/privilege/keymanager", SOCKET_ID_STORAGE}
    };
}

void CKMService::SetCommManager(CommMgr *manager)
{
    ThreadService::SetCommManager(manager);
    Register(*manager);
}

// CKMService does not support security check
// so 3rd parameter is not used
bool CKMService::ProcessOne(
    const ConnectionID &conn,
    ConnectionInfo &info,
    bool /*allowed*/)
{
    LogDebug("process One");
    RawBuffer response;

    Try {
        if (!info.buffer.Ready())
            return false;

        if (info.interfaceID == SOCKET_ID_CONTROL)
            response = ProcessControl(info.buffer);
        else
            response = ProcessStorage(info.credentials, info.buffer);

        m_serviceManager->Write(conn, response);

        return true;
    } Catch(MessageBuffer::Exception::Base) {
        LogError("Broken protocol. Closing socket.");
    } Catch(Exception::BrokenProtocol) {
        LogError("Broken protocol. Closing socket.");
    } catch (const DataType::Exception::Base &e) {
        LogError("Closing socket. DBDataType::Exception: " << e.DumpToString());
    } catch (const std::string &e) {
        LogError("String exception(" << e << "). Closing socket");
    } catch (const std::exception &e) {
        LogError("Std exception:: " << e.what());
    } catch (...) {
        LogError("Unknown exception. Closing socket.");
    }

    m_serviceManager->Close(conn);
    return false;
}

RawBuffer CKMService::ProcessControl(MessageBuffer &buffer)
{
    int command = 0;
    uid_t user = 0;
    ControlCommand cc;
    Password newPass, oldPass;
    Label smackLabel;

    buffer.Deserialize(command);

    LogDebug("Process control. Command: " << command);

    cc = static_cast<ControlCommand>(command);

    switch (cc) {
    case ControlCommand::UNLOCK_USER_KEY:
        buffer.Deserialize(user, newPass);
        return m_logic->unlockUserKey(user, newPass);
    case ControlCommand::LOCK_USER_KEY:
        buffer.Deserialize(user);
        return m_logic->lockUserKey(user);
    case ControlCommand::REMOVE_USER_DATA:
        buffer.Deserialize(user);
        return m_logic->removeUserData(user);
    case ControlCommand::CHANGE_USER_PASSWORD:
        buffer.Deserialize(user, oldPass, newPass);
        return m_logic->changeUserPassword(user, oldPass, newPass);
    case ControlCommand::RESET_USER_PASSWORD:
        buffer.Deserialize(user, newPass);
        return m_logic->resetUserPassword(user, newPass);
    case ControlCommand::REMOVE_APP_DATA:
        buffer.Deserialize(smackLabel);
        return m_logic->removeApplicationData(smackLabel);
    case ControlCommand::UPDATE_CC_MODE:
        return m_logic->updateCCMode();
    case ControlCommand::SET_PERMISSION:
    {
        Name name;
        Label label;
        Label accessorLabel;
        PermissionMask permissionMask = 0;

        buffer.Deserialize(user, name, label, accessorLabel, permissionMask);

        Credentials cred(user, label);
        return m_logic->setPermission(
            cred,
            command,
            0, // dummy
            name,
            label,
            accessorLabel,
            permissionMask);
    }
    default:
        Throw(Exception::BrokenProtocol);
    }
}

RawBuffer CKMService::ProcessStorage(Credentials &cred, MessageBuffer &buffer)
{
    int command = 0;
    int msgID = 0;
    int tmpDataType = 0;
    Name name;
    Label label, accessorLabel;

    buffer.Deserialize(command);
    buffer.Deserialize(msgID);

    // This is a workaround solution for locktype=None in Tizen 2.2.1
    // When locktype is None, lockscreen app doesn't interfere with unlocking process.
    // Therefor lockscreen app cannot notify unlock events to key-manager when locktype is None.
    // So, to unlock user data when lock type is None, key-manager always try to unlock user data with null password.
    // Even if the result is fail, it will be ignored.
    Password nullPassword("");
    m_logic->unlockUserKey(cred.clientUid, nullPassword);

    LogDebug("Process storage. Command: " << command);

    switch (static_cast<LogicCommand>(command)) {
    case LogicCommand::SAVE:
    {
        RawBuffer rawData;
        PolicySerializable policy;
        buffer.Deserialize(tmpDataType, name, label, rawData, policy);
        return m_logic->saveData(
            cred,
            msgID,
            name,
            label,
            Crypto::Data(DataType(tmpDataType), std::move(rawData)),
            policy);
    }
    case LogicCommand::SAVE_PKCS12:
    {
        RawBuffer rawData;
        PKCS12Serializable pkcs;
        PolicySerializable keyPolicy, certPolicy;
        buffer.Deserialize(name, label, pkcs, keyPolicy, certPolicy);
        return m_logic->savePKCS12(
            cred,
            msgID,
            name,
            label,
            pkcs,
            keyPolicy,
            certPolicy);
    }
    case LogicCommand::REMOVE:
    {
        buffer.Deserialize(name, label);
        return m_logic->removeData(
            cred,
            msgID,
            name,
            label);
    }
    case LogicCommand::GET:
    {
        Password password;
        buffer.Deserialize(tmpDataType, name, label, password);
        return m_logic->getData(
            cred,
            msgID,
            DataType(tmpDataType),
            name,
            label,
            password);
    }
    case LogicCommand::GET_PKCS12:
    {
        Password passKey;
        Password passCert;
        buffer.Deserialize(
            name,
            label,
            passKey,
            passCert);
        return m_logic->getPKCS12(
            cred,
            msgID,
            name,
            label,
            passKey,
            passCert);
    }
    case LogicCommand::GET_LIST:
    {
        buffer.Deserialize(tmpDataType);
        return m_logic->getDataList(
            cred,
            msgID,
            DataType(tmpDataType));
    }
    case LogicCommand::CREATE_KEY_AES:
    {
        int size = 0;
        Name keyName;
        Label keyLabel;
        PolicySerializable policyKey;
        buffer.Deserialize(
            size,
            policyKey,
            keyName,
            keyLabel);
        return m_logic->createKeyAES(
            cred,
            msgID,
            size,
            keyName,
            keyLabel,
            policyKey);
    }
    case LogicCommand::CREATE_KEY_PAIR:
    {
        CryptoAlgorithmSerializable keyGenAlgorithm;
        Name privateKeyName;
        Label privateKeyLabel;
        Name publicKeyName;
        Label publicKeyLabel;
        PolicySerializable policyPrivateKey;
        PolicySerializable policyPublicKey;
        buffer.Deserialize(keyGenAlgorithm,
                           policyPrivateKey,
                           policyPublicKey,
                           privateKeyName,
                           privateKeyLabel,
                           publicKeyName,
                           publicKeyLabel);
        return m_logic->createKeyPair(
            cred,
            msgID,
            keyGenAlgorithm,
            privateKeyName,
            privateKeyLabel,
            publicKeyName,
            publicKeyLabel,
            policyPrivateKey,
            policyPublicKey);
    }
    case LogicCommand::GET_CHAIN_CERT:
    {
        RawBuffer certificate;
        RawBufferVector untrustedVector;
        RawBufferVector trustedVector;
        bool systemCerts = false;
        buffer.Deserialize(certificate, untrustedVector, trustedVector, systemCerts);
        return m_logic->getCertificateChain(
            cred,
            msgID,
            certificate,
            untrustedVector,
            trustedVector,
            systemCerts);
    }
    case LogicCommand::GET_CHAIN_ALIAS:
    {
        RawBuffer certificate;
        LabelNameVector untrustedVector;
        LabelNameVector trustedVector;
        bool systemCerts = false;
        buffer.Deserialize(certificate, untrustedVector, trustedVector, systemCerts);
        return m_logic->getCertificateChain(
            cred,
            msgID,
            certificate,
            untrustedVector,
            trustedVector,
            systemCerts);
    }
    case LogicCommand::CREATE_SIGNATURE:
    {
        Password password;        // password for private_key
        RawBuffer message;

        CryptoAlgorithmSerializable cAlgorithm;
        buffer.Deserialize(name, label, password, message, cAlgorithm);

        return m_logic->createSignature(
              cred,
              msgID,
              name,
              label,
              password,           // password for private_key
              message,
              cAlgorithm);
    }
    case LogicCommand::VERIFY_SIGNATURE:
    {
        Password password;           // password for public_key (optional)
        RawBuffer message;
        RawBuffer signature;
        CryptoAlgorithmSerializable cAlg;

        buffer.Deserialize(name,
                           label,
                           password,
                           message,
                           signature,
                           cAlg);

        return m_logic->verifySignature(
            cred,
            msgID,
            name,
            label,
            password,           // password for public_key (optional)
            message,
            signature,
            cAlg);
    }
    case LogicCommand::SET_PERMISSION:
    {
        PermissionMask permissionMask = 0;
        buffer.Deserialize(name, label, accessorLabel, permissionMask);
        return m_logic->setPermission(
            cred,
            command,
            msgID,
            name,
            label,
            accessorLabel,
            permissionMask);
    }
    default:
        Throw(Exception::BrokenProtocol);
    }
}

void CKMService::ProcessMessage(MsgKeyRequest msg)
{
    Crypto::GObjShPtr key;
    int ret = m_logic->getKeyForService(msg.cred,
                                        msg.name,
                                        msg.label,
                                        msg.password,
                                        key);
    MsgKeyResponse kResp(msg.id, key, ret);
    try {
        if (!m_commMgr->SendMessage(kResp))
            LogError("No listener found"); // can't do much more
    } catch (...) {
        LogError("Uncaught exception in SendMessage. Check listeners.");
    }
}

void CKMService::CustomHandle(const ReadEvent &event)
{
    LogDebug("Read event");
    auto &info = m_connectionInfoMap[event.connectionID.counter];
    info.buffer.Push(event.rawBuffer);
    while (ProcessOne(event.connectionID, info, true));
}

void CKMService::CustomHandle(const SecurityEvent & /*event*/)
{
    LogError("This should not happend! SecurityEvent was called on CKMService!");
}

} // namespace CKM