forked from mrmekon/libsimplepgp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeychain.c
More file actions
executable file
·112 lines (92 loc) · 2.7 KB
/
Copy pathkeychain.c
File metadata and controls
executable file
·112 lines (92 loc) · 2.7 KB
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
/*
* keychain.c
* simplepgp
*
* Created by Trevor Bentley on 11/8/11.
*
* Copyright 2011 Trevor Bentley
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "keychain.h"
#include "packet_private.h"
#define SPGP_KEYCHAIN_DEFAULT_SIZE 5
static pthread_mutex_t keychain_mtx;
static spgp_packet_t **keychain;
static uint32_t kc_count;
static uint32_t kc_used;
static uint32_t iter_idx;
uint8_t spgp_keychain_init(void) {
if (pthread_mutex_init(&keychain_mtx, NULL)) return -1;
keychain = malloc(sizeof(spgp_packet_t*) * SPGP_KEYCHAIN_DEFAULT_SIZE);
if (NULL == keychain) return -1;
kc_count = SPGP_KEYCHAIN_DEFAULT_SIZE;
kc_used = 0;
return 0;
}
uint8_t spgp_keychain_free(void) {
kc_count = 0;
kc_used = 0;
free(keychain);
keychain = NULL;
pthread_mutex_destroy(&keychain_mtx);
return 0;
}
uint8_t spgp_keychain_is_valid(void) {
if (keychain) return 1;
return 0;
}
uint8_t spgp_keychain_add_packet(spgp_packet_t *pkt) {
if (NULL == pkt) return -1;
pthread_mutex_lock(&keychain_mtx);
if (kc_used == kc_count) {
// Need to allocate more space
return -1; // for now, unsupported
}
keychain[kc_used] = pkt;
kc_used++;
// Potential feature:
// If we wanted to be really fuckin' fancy, we could use 'secure memory'
// to store secret keys. Use mlock() to get a page that will never be
// swapped to disk, and clear it when we exit.
Serial.printf("Added packet to keychain.");
pthread_mutex_unlock(&keychain_mtx);
return 0;
}
uint8_t spgp_keychain_del_packet(spgp_packet_t *pkt) {
pthread_mutex_lock(&keychain_mtx);
pthread_mutex_unlock(&keychain_mtx);
Serial.printf("PACKET DELETE UNIMPLEMENTED\n");
return -1;
}
uint8_t spgp_keychain_iter_start(void) {
pthread_mutex_lock(&keychain_mtx);
iter_idx = 0;
return 0;
}
uint8_t spgp_keychain_iter_end(void) {
pthread_mutex_unlock(&keychain_mtx);
return 0;
}
spgp_packet_t *spgp_keychain_iter_next(void) {
if (iter_idx < kc_used)
return keychain[iter_idx++];
return NULL;
}
spgp_packet_t *spgp_keychain_secret_key_with_id(uint8_t *keyid) {
pthread_mutex_lock(&keychain_mtx);
pthread_mutex_unlock(&keychain_mtx);
Serial.printf("SECRET KEY SEARCH UNIMPLEMENTED\n");
return NULL;
}