-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.c
117 lines (95 loc) · 2.48 KB
/
interface.c
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
/*
* dsngctl - Control utility for DigSig-ng (digsig-ng.org)
*
* interface.c - functions for interfacing with the kernel module
*
* Copyright (c) 2013-2014, The DigSig-ng Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*/
#include "interface.h"
#include "extract.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
int dsng_start(char *pkey_path)
{
int ret = -1;
int pkey_file, mod_file;
pkey_file = open(pkey_path, O_RDONLY);
if (pkey_file < 0) {
fprintf(stderr, "dsngctl: %s: unable to open public key\n", __func__);
goto cleanup_done;
}
mod_file = open("/sys/digsig/key", O_WRONLY);
if (pkey_file < 0) {
fprintf(stderr, "dsngctl: %s: unable to open module char device\n",
__func__);
goto cleanup_pkey;
}
if (check_pubkey(pkey_file) != 0) {
fprintf(stderr, "dsngctl: %s: public key invalid\n", __func__);
goto cleanup_mod;
}
if (get_mpi(pkey_file, mod_file, 'n') != 0) {
fprintf(stderr, "dsngctl: %s: get_mpi(..., ..., 'n') failed\n",
__func__);
goto cleanup_mod;
}
if (get_mpi(pkey_file, mod_file, 'e') != 0) {
fprintf(stderr, "dsngctl: %s: get_mpi(..., ..., 'e') failed\n",
__func__);
goto cleanup_mod;
}
ret = 0;
cleanup_mod:
close(mod_file);
cleanup_pkey:
close(pkey_file);
cleanup_done:
return ret;
}
int digsig_is_loaded()
{
struct stat key_stat, revoke_stat;
if (stat("/sys/digsig/key", &key_stat) != 0) {
fprintf(stderr, "dsngctl: %s: could not stat /sys/digsig/key\n", __func__);
return 0;
}
if (stat("/sys/digsig/revoke", &revoke_stat) != 0) {
fprintf(stderr, "dsngctl: %s: could not stat /sys/digsig/revoke\n", __func__);
return 0;
}
return 1;
}
int digsig_is_initialized()
{
int status_fd;
int rcount;
char status[8];
if (!digsig_is_loaded())
return 0;
status_fd = open("/sys/digsig/status", O_RDONLY);
if (status_fd < 0) {
fprintf(stderr, "dsngctl: %s: could not open /sys/digsig/status\n", __func__);
return 0;
}
rcount = read(status_fd, status, 8); /* reading 8 bytes, we won't need more */
if (rcount < 0) {
fprintf(stderr, "dsngctl: %s: could not read /sys/digsig/status\n", __func__);
close(status_fd);
return 0;
}
if (strncmp(status, "1", 1) == 0) {
close(status_fd);
return 1;
}
if (status_fd > 0)
close(status_fd);
return 0;
}