summaryrefslogtreecommitdiff
path: root/mobicore/daemon/Registry/Registry.cpp
blob: a900529fb8cc3421bd2ac7ea183ca0a760917037 (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
/** Mobicore Driver Registry Interface
 *
 * Implements the MobiCore registry interface for the ROOT-PA
 *
 * @file
 * @ingroup MCD_MCDIMPL_DAEMON_REG
 */

/*
 * Copyright (c) 2013 TRUSTONIC LIMITED
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the TRUSTONIC LIMITED nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <sys/stat.h>
#include <assert.h>
#include <string>
#include <cstring>
#include <cstddef>
#include "mcLoadFormat.h"
#include "mcSpid.h"
#include "mcVersionHelper.h"

#include "log.h"

#include "MobiCoreRegistry.h"
#include "MobiCoreDriverCmd.h"

#include "Connection.h"

#define DAEMON_TIMEOUT 30000

using namespace std;


static mcResult_t writeBlobData(void *buff, uint32_t len)
{
    Connection con;
mcDrvResponseHeader_t rsp = { responseId :
                                  MC_DRV_ERR_INVALID_PARAMETER
                                };
    if (!con.connect(SOCK_PATH)) {
        LOG_E("Failed to connect to daemon!");
        return MC_DRV_ERR_DAEMON_SOCKET;
    }

    if (con.writeData(buff, len) <= 0) {
        LOG_E("Failed to send daemon to data!");
        return MC_DRV_ERR_DAEMON_SOCKET;
    }

    if (con.readData(&rsp, sizeof(rsp), DAEMON_TIMEOUT) <= 0) {
        LOG_E("Failed to get answer from daemon!");
        return MC_DRV_ERR_DAEMON_SOCKET;
    }

    return rsp.responseId;
}

static mcResult_t readBlobData(void *buff, uint32_t len, void *rbuff, uint32_t *rlen)
{
    Connection con;
    int32_t size;
mcDrvResponseHeader_t rsp = { responseId :
                                  MC_DRV_ERR_INVALID_PARAMETER
                                };
    if (*rlen == 0) {
        LOG_E("Invalid buffer length!");
        return MC_DRV_ERR_DAEMON_SOCKET;
    }

    if (!con.connect(SOCK_PATH)) {
        LOG_E("Failed to connect to daemon!");
        return MC_DRV_ERR_DAEMON_SOCKET;
    }

    if (con.writeData(buff, len) <= 0) {
        LOG_E("Failed to send daemon to data!");
        return MC_DRV_ERR_DAEMON_SOCKET;
    }

    // First read the response
    if (con.readData(&rsp, sizeof(rsp), DAEMON_TIMEOUT) <= 0) {
        LOG_E("Failed to get answer from daemon!");
        return MC_DRV_ERR_DAEMON_SOCKET;
    }

    //Then read the actual data
    size = con.readData(rbuff, *rlen, DAEMON_TIMEOUT);
    if (size <= 0) {
        LOG_E("Failed to get answer from daemon!");
        return MC_DRV_ERR_DAEMON_SOCKET;
    }
    // Return also the read buf size
    *rlen = (uint32_t)size;

    return rsp.responseId;
}

//------------------------------------------------------------------------------
mcResult_t mcRegistryStoreAuthToken(void *so, uint32_t size)
{
    typedef struct {
        uint32_t commandId;
        uint32_t soSize;
        uint8_t so;
    } storeCmd;

    mcResult_t ret;
    storeCmd *cmd = (storeCmd *)malloc(sizeof(storeCmd) + size - 1);
    if (cmd == NULL) {
        LOG_E("Allocation failure");
        return MC_DRV_ERR_NO_FREE_MEMORY;
    }

    cmd->commandId = MC_DRV_REG_STORE_AUTH_TOKEN;
    cmd->soSize = size;
    memcpy(&cmd->so, so, size);
    ret = writeBlobData(cmd, sizeof(storeCmd) + size - 1);
    free(cmd);
    return ret;
}


//------------------------------------------------------------------------------
mcResult_t mcRegistryReadAuthToken(void *so, uint32_t *size)
{
mcDrvCommandHeader_t cmd = { commandId :
                                 MC_DRV_REG_READ_AUTH_TOKEN
                               };
    uint32_t rsize;
    mcResult_t ret;
    // we expect to max read what the user has allocated
    rsize = *size;
    ret = readBlobData(&cmd, sizeof(cmd), so, &rsize);
    // return the actual read size
    *size = rsize;
    return ret;
}

//------------------------------------------------------------------------------
mcResult_t mcRegistryDeleteAuthToken(void)
{
mcDrvCommandHeader_t cmd = { commandId :
                                 MC_DRV_REG_DELETE_AUTH_TOKEN
                               };
    return writeBlobData(&cmd, sizeof(cmd));
}


//------------------------------------------------------------------------------
mcResult_t mcRegistryStoreRoot(void *so, uint32_t size)
{
    typedef struct {
        uint32_t commandId;
        uint32_t soSize;
        uint8_t so;
    } storeCmd;
    mcResult_t ret;
    storeCmd *cmd = (storeCmd *)malloc(sizeof(storeCmd) + size - 1);
    if (cmd == NULL) {
        LOG_E("Allocation failure");
        return MC_DRV_ERR_NO_FREE_MEMORY;
    }

    cmd->commandId = MC_DRV_REG_WRITE_ROOT_CONT;
    cmd->soSize = size;
    memcpy(&cmd->so, so, size);
    ret = writeBlobData(cmd, sizeof(storeCmd) + size - 1);
    free(cmd);
    return ret;
}


//------------------------------------------------------------------------------
mcResult_t mcRegistryReadRoot(void *so, uint32_t *size)
{
mcDrvCommandHeader_t cmd = { commandId :
                                 MC_DRV_REG_READ_ROOT_CONT
                               };
    uint32_t rsize;
    mcResult_t ret;

    rsize = *size;
    ret = readBlobData(&cmd, sizeof(cmd), so, &rsize);
    *size = rsize;
    return ret;
}

//------------------------------------------------------------------------------
mcResult_t mcRegistryCleanupRoot(void)
{
mcDrvCommandHeader_t cmd = { commandId :
                                 MC_DRV_REG_DELETE_ROOT_CONT
                               };
    return writeBlobData(&cmd, sizeof(cmd));
}

//------------------------------------------------------------------------------
mcResult_t mcRegistryStoreSp(mcSpid_t spid, void *so, uint32_t size)
{
    typedef struct {
        uint32_t commandId;
        uint32_t soSize;
        mcSpid_t spid;
        uint8_t so;
    } storeCmd;

    mcResult_t ret;
    storeCmd *cmd = (storeCmd *)malloc(sizeof(storeCmd) + size - 1);
    if (cmd == NULL) {
        LOG_E("Allocation failure");
        return MC_DRV_ERR_NO_FREE_MEMORY;
    }

    cmd->commandId = MC_DRV_REG_WRITE_SP_CONT;
    cmd->soSize = size;
    cmd->spid = spid;
    memcpy(&cmd->so, so, size);

    ret = writeBlobData(cmd, sizeof(storeCmd) + size - 1);
    free(cmd);
    return ret;
}


//------------------------------------------------------------------------------
mcResult_t mcRegistryReadSp(mcSpid_t spid, void *so, uint32_t *size)
{
    struct {
        uint32_t commandId;
        mcSpid_t spid;
    } cmd;
    uint32_t rsize;
    mcResult_t ret;
    cmd.commandId = MC_DRV_REG_READ_SP_CONT;
    cmd.spid = spid;

    rsize = *size;
    ret = readBlobData(&cmd, sizeof(cmd), so, &rsize);
    *size = rsize;

    return ret;
}

//------------------------------------------------------------------------------
mcResult_t mcRegistryCleanupSp(mcSpid_t spid)
{
    struct {
        uint32_t commandId;
        mcSpid_t spid;
    } cmd;

    cmd.commandId = MC_DRV_REG_DELETE_SP_CONT;
    cmd.spid = spid;

    return writeBlobData(&cmd, sizeof(cmd));
}

//------------------------------------------------------------------------------
mcResult_t mcRegistryStoreTrustletCon(const mcUuid_t *uuid, mcSpid_t spid, void *so, uint32_t size)
{
    typedef struct {
        uint32_t commandId;
        uint32_t soSize;
        mcUuid_t uuid;
        mcSpid_t spid;
        uint8_t so;
    } storeCmd;

    mcResult_t ret;
    storeCmd *cmd = (storeCmd *)malloc(sizeof(storeCmd) + size - 1);
    if (cmd == NULL) {
        LOG_E("Allocation failure");
        return MC_DRV_ERR_NO_FREE_MEMORY;
    }

    cmd->commandId = MC_DRV_REG_WRITE_TL_CONT;
    cmd->soSize = size;
    cmd->spid = spid;
    memcpy(&cmd->uuid, uuid, sizeof(mcUuid_t));
    memcpy(&cmd->so, so, size);

    ret = writeBlobData(cmd, sizeof(storeCmd) + size - 1);
    free(cmd);
    return ret;
}

//------------------------------------------------------------------------------
mcResult_t mcRegistryStoreTABlob(mcSpid_t spid, void *blob, uint32_t size)
{
    typedef struct {
        uint32_t commandId;
        uint32_t blobSize;
        mcSpid_t spid;
        uint8_t blob[];
    } storeCmd;

    mcResult_t ret;
    storeCmd *cmd = (storeCmd *)malloc(sizeof(storeCmd) + size);
    if (cmd == NULL) {
        LOG_E("Allocation failure");
        return MC_DRV_ERR_NO_FREE_MEMORY;
    }

    cmd->commandId = MC_DRV_REG_STORE_TA_BLOB;
    cmd->blobSize = size;
    cmd->spid = spid;
    memcpy(&cmd->blob, blob, size);

    ret = writeBlobData(cmd, sizeof(storeCmd) + size);
    free(cmd);
    return ret;
}

//------------------------------------------------------------------------------
mcResult_t mcRegistryReadTrustletCon(const mcUuid_t *uuid, mcSpid_t spid, void *so, uint32_t *size)
{
    struct {
        uint32_t commandId;
        mcUuid_t uuid;
        mcSpid_t spid;
    } cmd;
    mcResult_t ret;
    uint32_t rsize;
    cmd.commandId = MC_DRV_REG_READ_TL_CONT;
    cmd.spid = spid;
    memcpy(&cmd.uuid, uuid, sizeof(mcUuid_t));

    rsize = *size;
    ret = readBlobData(&cmd, sizeof(cmd), so, &rsize);
    *size = rsize;
    return ret;
}

//------------------------------------------------------------------------------
mcResult_t mcRegistryCleanupTrustlet(const mcUuid_t *uuid, const mcSpid_t spid)
{
    struct {
        uint32_t commandId;
        mcUuid_t uuid;
        mcSpid_t spid;
    } cmd;

    if (uuid == NULL) {
        return MC_DRV_ERR_INVALID_PARAMETER;
    }

    cmd.commandId = MC_DRV_REG_DELETE_TL_CONT;
    cmd.spid = spid;
    memcpy(&cmd.uuid, uuid, sizeof(mcUuid_t));

    return writeBlobData(&cmd, sizeof(cmd));
}

//------------------------------------------------------------------------------
mcResult_t mcRegistryStoreData(void *so, uint32_t size)
{
    return MC_DRV_ERR_INVALID_PARAMETER;
}


//------------------------------------------------------------------------------
mcResult_t mcRegistryReadData(uint32_t context, const mcCid_t *cid, mcPid_t pid,
                              mcSoDataCont_t *so, uint32_t maxLen)
{
    return MC_DRV_ERR_INVALID_PARAMETER;
}

/** @} */