File: keydb.c

package info (click to toggle)
ekeyd 1.1.3-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 628 kB
  • ctags: 859
  • sloc: ansic: 5,189; sh: 271; makefile: 199; perl: 148
file content (194 lines) | stat: -rw-r--r-- 4,117 bytes parent folder | download
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
/* daemon/keydb.c
 *
 * Entropy key keys database handling.
 *
 * Copyright 2009 Simtec Electronics
 *
 * For licence terms refer to the COPYING file.
 */

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

#include "keydb.h"
#include "pem.h"

/** Keyring entry. */
struct snum_to_key_s {
    struct snum_to_key_s *next; /**< Next entry. */
    uint8_t snum[12]; /**< Serial number. */
    uint8_t ltkey[32]; /**< Long term session key. */
};

static struct snum_to_key_s *ents = NULL;

/** Find a long term saession key from a serial number.
 *
 * @param snum The serial number to find the key for.
 * @return The keyring entry or NULL if no matching serial number is found.
 */
static struct snum_to_key_s *
find_ltkey(const uint8_t *snum)
{
    struct snum_to_key_s *ent = ents;

    while (ent != NULL) {
        if (memcmp(ent->snum, snum, 12) == 0)
            break;
        ent = ent->next;
    }
    return ent;
}

/* exported interface documented in keydb.h */
uint8_t *
snum_to_ltkey(const uint8_t *snum)
{
    struct snum_to_key_s *ent;
    uint8_t *key = NULL;

    ent = find_ltkey(snum);
    if (ent != NULL) {
        key = malloc(32);
        if (key != NULL) {
            memcpy(key, ent->ltkey, 32);
        }
    }
    return key;
}

/* exported interface documented in keydb.h */
int
output_key(FILE *fh, const uint8_t *snum, const uint8_t *ltkey)
{
    uint8_t data[128];

    pem64_encode_bytes(snum, 12, (char *)data);
    data[16] = ' ';
    pem64_encode_bytes(ltkey, 32, (char *)data + 17);
    data[61] = 0;
    return fprintf(fh, "%s\n", data);
}

/* exported interface documented in keydb.h */
int
add_ltkey(const uint8_t *snum, const uint8_t *ltkey)
{
    struct snum_to_key_s *ent;

    ent = find_ltkey(snum);
    if (ent == NULL) {
        /* no current entry for that serial number */
        ent = calloc(1, sizeof(struct snum_to_key_s));
        memcpy(ent->snum, snum, 12);
        ent->next = ents;
        ents = ent;
    }

    memcpy(ent->ltkey, ltkey, 32);

    return 0;
}

/* exported interface documented in keydb.h */
int
write_keyring(const char *fname)
{
    int fd;
    FILE *fh;
    struct snum_to_key_s *base = ents;
    char *fname2 = malloc(strlen(fname) + 5);
    
    strcpy(fname2, fname);
    strcat(fname2, ".tmp");
    
    fd = open(fname2, O_CREAT | O_EXCL | O_WRONLY, 0600);
    if (fd == -1) {
        /* Unable to open keyring */
        perror("open");
        free(fname2);
        return -1;
    }
    
    fh = fdopen(fd, "w");
    if (fh == NULL) {
        free(fname2);
        close(fd);
        return -1;
    }
    
    fprintf(fh, "# Do not edit this directly, this file is managed by ekey-setkey\n");

    while (base) {
        output_key(fh, base->snum, base->ltkey);
        base = base->next;
    }

    fflush(fh);
    fclose(fh);
    
    if (rename(fname2, fname) == -1) {
        perror("rename");
        free(fname2);
        unlink(fname2);
        return -1;
    }
    
    free(fname2);
    
    return 0;
}

/* exported interface documented in keydb.h */
int
read_keyring(const char *fname)
{
    FILE *fh;
    char line[512];
    char snumpem[17];
    char ltkeypem[45];
    int res;
    int keys = 0;
    uint8_t snum[12];
    uint8_t ltkey[33];
    struct snum_to_key_s *base = ents;

    fh = fopen(fname, "r");
    if (fh == NULL) {
        /* Unable to open keyring */
        return -1;
    }

    /* Blow the old keyring out of the water */
    while (base) {
        struct snum_to_key_s *next = base->next;
        free(base);
        base = next;
    }

    ents = NULL;

    while (fgets(line, 512, fh) != NULL) {
        res = sscanf(line,
                     " %16[A-Za-z0-9+/=] %44[A-Za-z0-9+/=]",
                     snumpem,
                     ltkeypem);

        if (res == 2) {
            pem64_decode_bytes(snumpem, 16, snum);
            pem64_decode_bytes(ltkeypem, 44, ltkey);
            add_ltkey(snum, ltkey);
            keys++;
        }
    }

    fclose(fh);

    return keys;
}