tizen 2.3 release
[framework/system/deviced.git] / src / icd / icd-integrity.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <sys/ioctl.h>
26 #include <sys/stat.h>
27
28 #include "sha2.h"
29 #include "core/log.h"
30
31 #define ICD_EXEC_PATH           "/usr/bin/icd"
32
33 #define INTEGRITY_NOT_COMPROMISED       1
34 #define INTEGRITY_COMPROMISED           0
35 #define ERR_FILE_READ                   -1
36
37 #define TZIC_IOC_MAGIC          0x9E
38 #define TZIC_IOCTL_SET_FUSE_REQ _IO(TZIC_IOC_MAGIC, 1)
39
40 static int write_file(const char *path, const char *value)
41 {
42         int fd, ret, len;
43
44         fd = open(path, O_WRONLY|O_CREAT, 0622);
45         if (fd < 0)
46                 return -errno;
47
48         len = strlen(value);
49
50         do {
51                 ret = write(fd, value, len);
52         } while (ret < 0 && errno == EINTR);
53
54         close(fd);
55
56         if (ret < 0)
57                 return -errno;
58
59         return 0;
60 }
61
62 static int check_file_hash(const char *filename)
63 {
64         int fd;
65         struct stat info;
66         int fsize = 0;
67         int result = INTEGRITY_COMPROMISED;
68         SECKM_SHA256_CTX ctx;
69         unsigned char digest[SECKM_SHA256_DIGEST_LENGTH];
70         unsigned char *input = 0;
71
72         /* icd digest */
73         unsigned char hashed[] =
74         "\x08\x01\x77\xd8\x5e\xdf\xa2\xe3\x9c\x34\xe7\xd6\xdd\x86\xae\x88\xeb\x19\x1b\xc9\xb6\xdd\x3d\xa2\x80\xd1\xaa\xf5\x1e\x29\x41\x14";
75
76         fd = open(filename, O_RDONLY);
77         if (fd < 0)
78                 goto out_err;
79
80         if (fstat(fd, &info) < 0)
81                 goto out;
82
83         fsize = info.st_size;
84
85         input = (unsigned char *)malloc(fsize);
86         if (!input)
87                 goto out;
88
89         result = read(fd, input, fsize);
90         if (result != fsize) {
91                 result = ERR_FILE_READ;
92                 goto out;
93         }
94
95         SECKM_SHA256_Init((SECKM_SHA256_CTX*) &ctx);
96         SECKM_SHA256_Update((SECKM_SHA256_CTX*) &ctx, input, fsize);
97         SECKM_SHA256_Final((SECKM_SHA256_CTX*) &ctx, digest);
98
99         if ((memcmp(hashed, digest, SECKM_SHA256_DIGEST_LENGTH) == 0))
100                 result = INTEGRITY_NOT_COMPROMISED;
101 out:
102         close(fd);
103 out_err:
104         /*
105          * FIXME: temporarily skip a tamper flag setting
106          * icd package not working
107          */
108 #if 0
109         if (result != INTEGRITY_NOT_COMPROMISED) {
110                 fd = open("/dev/tzic", O_RDWR);
111                 if (fd > 0) {
112                         ioctl(fd, TZIC_IOCTL_SET_FUSE_REQ, &result);
113                         close(fd);
114                 }
115         }
116 #endif
117         if (input)
118                 free(input);
119
120         return result;
121 }
122
123 void icd_check_integrity(void)
124 {
125         int ret;
126         int check;
127
128         check = check_file_hash(ICD_EXEC_PATH);
129         if (check == INTEGRITY_NOT_COMPROMISED)
130                 ret = write_file("/dev/icd", "1");
131         else
132                 ret = write_file("/dev/icd", "0");
133         _I("icd status %d %d", check, ret);
134 }