-
Notifications
You must be signed in to change notification settings - Fork 2
/
show-key-id.c
163 lines (131 loc) · 3.16 KB
/
show-key-id.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
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
/*
* dvpn, a multipoint vpn implementation
* Copyright (C) 2015, 2016 Lennert Buytenhek
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version
* 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 2.1 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License version 2.1 along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <gnutls/abstract.h>
#include <gnutls/x509.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "util.h"
#include "x509.h"
static int try_privkey(uint8_t *id, uint8_t *buf, int size)
{
gnutls_x509_privkey_t privkey;
gnutls_datum_t datum;
int ret;
ret = gnutls_x509_privkey_init(&privkey);
if (ret) {
fprintf(stderr, "gnutls_x509_privkey_init: ");
gnutls_perror(ret);
return -1;
}
datum.data = buf;
datum.size = size;
ret = gnutls_x509_privkey_import(privkey, &datum, GNUTLS_X509_FMT_PEM);
if (ret) {
gnutls_x509_privkey_deinit(privkey);
return -1;
}
ret = x509_get_privkey_id(id, privkey);
gnutls_x509_privkey_deinit(privkey);
return ret;
}
static int try_pubkey(uint8_t *id, uint8_t *buf, int size)
{
gnutls_pubkey_t pubkey;
gnutls_datum_t datum;
int ret;
ret = gnutls_pubkey_init(&pubkey);
if (ret) {
fprintf(stderr, "gnutls_pubkey_init: ");
gnutls_perror(ret);
return -1;
}
datum.data = buf;
datum.size = size;
ret = gnutls_pubkey_import(pubkey, &datum, GNUTLS_X509_FMT_PEM);
if (ret) {
gnutls_pubkey_deinit(pubkey);
return -1;
}
ret = get_pubkey_id(id, pubkey);
gnutls_pubkey_deinit(pubkey);
return ret;
}
static int read_key_id(uint8_t *id, const char *file)
{
int fd;
uint8_t buf[65536];
int size;
int ret;
fd = open(file, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "error opening %s: %s\n", file,
strerror(errno));
return -1;
}
size = read(fd, buf, sizeof(buf));
if (size < 0) {
perror("read_key_id: read");
close(fd);
return -1;
}
close(fd);
gnutls_global_init();
ret = try_privkey(id, buf, size);
if (ret)
ret = try_pubkey(id, buf, size);
gnutls_global_deinit();
return !!ret;
}
int show_key_id(const char *file)
{
uint8_t keyid[NODE_ID_LEN];
int ret;
if (file == NULL) {
fprintf(stderr, "usage: show-key-id <key.pem>\n");
return 1;
}
ret = read_key_id(keyid, file);
if (ret == 0) {
print_fingerprint(stdout, keyid);
printf("\n");
}
return ret;
}
int show_key_id_hex(const char *file)
{
uint8_t keyid[NODE_ID_LEN];
int ret;
if (file == NULL) {
fprintf(stderr, "usage: show-key-id-hex <key.pem>\n");
return 1;
}
ret = read_key_id(keyid, file);
if (ret == 0) {
int i;
for (i = 0; i < NODE_ID_LEN; i++) {
printf("%.2x%c", keyid[i],
(i < NODE_ID_LEN - 1) ? ':' : '\n');
}
}
return ret;
}