#include "armor.h"
#include "keystructs.h"
-#include "log.h"
#include "onak-conf.h"
#include "parsekey.h"
#include "version.h"
} else if (c == 63) {
c = '/';
} else {
- log_assert(c < 64);
+ c = '?';
}
return c;
unsigned char t;
int i;
- log_assert(ctx != NULL);
state = (struct armor_context *) ctx;
switch (state->curoctet++) {
{
int i;
- log_assert(c != NULL);
for (i = 0; i < count; i++) {
armor_putchar_int(ctx, ((char *) c)[i]);
unsigned char tmpc;
int i;
- log_assert(ctx != NULL);
state = (struct dearmor_context *) ctx;
*c = 0;
struct openpgp_signedpacket_list *cursubkey = NULL;
uint64_t *subkeys = NULL;
int count = 0;
-
+
if (key != NULL && key->subkeys != NULL) {
subkeys = malloc((spsize(key->subkeys) + 1) *
sizeof (uint64_t));
cursubkey = key->subkeys;
while (cursubkey != NULL) {
- subkeys[count++] = get_packetid(cursubkey->packet);
+ get_packetid(cursubkey->packet, &subkeys[count++]);
cursubkey = cursubkey -> next;
}
subkeys[count] = 0;
struct buffer_ctx storebuf;
int ret = 0;
int *fd = (int *) ctx;
+ uint64_t keyid;
if (key != NULL) {
storebuf.offset = 0;
storebuf.size = 8192;
storebuf.buffer = malloc(8192);
+ get_keyid(key, &keyid);
logthing(LOGTHING_TRACE,
"Iterating over 0x%016" PRIX64 ".",
- get_keyid(key));
+ keyid);
flatten_publickey(key,
&packets,
if (keyid < 0x100000000LL) {
config.dbbackend->fetch_key(keyid, &publickey, false);
if (publickey != NULL) {
- keyid = get_keyid(publickey);
+ get_keyid(publickey, &keyid);
free_publickey(publickey);
publickey = NULL;
} else {
struct openpgp_publickey *prev = NULL;
int newkeys = 0;
bool intrans;
+ uint64_t keyid;
for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
intrans = config.dbbackend->starttrans();
+ get_keyid(curkey, &keyid);
logthing(LOGTHING_INFO,
"Fetching key 0x%" PRIX64 ", result: %d",
- get_keyid(curkey),
- config.dbbackend->fetch_key(get_keyid(curkey), &oldkey,
+ keyid,
+ config.dbbackend->fetch_key(keyid, &oldkey,
intrans));
/*
bool deadlock = false;
struct skshash hash;
- keyid = get_keyid(publickey);
+ get_keyid(publickey, &keyid);
if (!intrans) {
db4_starttrans();
if (keyid < 0x100000000LL) {
dynamic_fetch_key(keyid, &publickey, false);
if (publickey != NULL) {
- keyid = get_keyid(publickey);
+ get_keyid(publickey, &keyid);
free_publickey(publickey);
publickey = NULL;
} else {
struct openpgp_publickey *prev = NULL;
int newkeys = 0;
bool intrans;
+ uint64_t keyid;
if (loaded_backend == NULL) {
load_backend();
for (curkey = *keys; curkey != NULL; curkey = curkey->next) {
intrans = dynamic_starttrans();
+ get_keyid(curkey, &keyid);
logthing(LOGTHING_INFO,
"Fetching key 0x%" PRIX64 ", result: %d",
- get_keyid(curkey),
- dynamic_fetch_key(get_keyid(curkey), &oldkey, intrans));
+ keyid,
+ dynamic_fetch_key(keyid, &oldkey, intrans));
/*
* If we already have the key stored in the DB then merge it
struct openpgp_publickey *next = NULL;
char keyfile[1024];
int fd = -1;
+ uint64_t keyid;
+ get_keyid(publickey, &keyid);
snprintf(keyfile, 1023, "%s/0x%" PRIX64, config.db_dir,
- get_keyid(publickey) & 0xFFFFFFFF);
+ keyid & 0xFFFFFFFF);
fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK);
if (fd > -1) {
struct openpgp_packet_list *packets = NULL;
struct openpgp_packet_list *list_end = NULL;
struct openpgp_publickey *next = NULL;
- uint64_t keyid = get_keyid(publickey);
+ uint64_t keyid;
struct ll *wordlist = NULL, *wl = NULL;
struct skshash hash;
uint64_t *subkeyids = NULL;
uint32_t hashid;
int i = 0;
+ get_keyid(publickey, &keyid);
if (!intrans)
fs_starttrans();
uint32_t cmd = KEYD_CMD_STORE;
uint64_t keyid;
- keyid = get_keyid(publickey);
+ get_keyid(publickey, &keyid);
if (update) {
keyd_delete_key(keyid, false);
#include "config.h"
#include "keyid.h"
#include "keystructs.h"
-#include "log.h"
+#include "onak.h"
#include "parsekey.h"
#include "mem.h"
#include "merge.h"
* get_keyid - Given a public key returns the keyid.
* @publickey: The key to calculate the id for.
*/
-uint64_t get_keyid(struct openpgp_publickey *publickey)
+onak_status_t get_keyid(struct openpgp_publickey *publickey, uint64_t *keyid)
{
- return (get_packetid(publickey->publickey));
+ return (get_packetid(publickey->publickey, keyid));
}
/**
* get_fingerprint - Given a public key returns the fingerprint.
* @publickey: The key to calculate the id for.
- * @fingerprint: The fingerprint (must be at least 20 bytes of space).
+ * @fingerprint: The fingerprint (must be at least 20 bytes of space).
* @len: The length of the returned fingerprint.
*
* This function returns the fingerprint for a given public key. As Type 3
* fingerprints are 16 bytes and Type 4 are 20 the len field indicates
* which we've returned.
*/
-unsigned char *get_fingerprint(struct openpgp_packet *packet,
+onak_status_t get_fingerprint(struct openpgp_packet *packet,
unsigned char *fingerprint,
size_t *len)
{
unsigned char c;
size_t modlen, explen;
- log_assert(fingerprint != NULL);
- log_assert(len != NULL);
+ if (fingerprint == NULL)
+ return ONAK_E_INVALID_PARAM;
+ if (len == NULL)
+ return ONAK_E_INVALID_PARAM;
*len = 0;
break;
default:
- logthing(LOGTHING_ERROR, "Unknown key type: %d",
- packet->data[0]);
+ return ONAK_E_UNKNOWN_VER;
}
- return fingerprint;
+ return ONAK_E_OK;
}
* get_packetid - Given a PGP packet returns the keyid.
* @packet: The packet to calculate the id for.
*/
-uint64_t get_packetid(struct openpgp_packet *packet)
+onak_status_t get_packetid(struct openpgp_packet *packet, uint64_t *keyid)
{
- uint64_t keyid = 0;
int offset = 0;
int i = 0;
size_t length = 0;
unsigned char buff[20];
- log_assert(packet != NULL);
+ if (packet == NULL)
+ return ONAK_E_INVALID_PARAM;
switch (packet->data[0]) {
case 2:
packet->data[9];
offset = ((offset + 7) / 8) + 2;
- for (keyid = 0, i = 0; i < 8; i++) {
- keyid <<= 8;
- keyid += packet->data[offset++];
+ for (*keyid = 0, i = 0; i < 8; i++) {
+ *keyid <<= 8;
+ *keyid += packet->data[offset++];
}
/*
- * Check for an RSA key; if not then log but accept anyway.
+ * Check for an RSA key; if not return an error.
* 1 == RSA
* 2 == RSA Encrypt-Only
* 3 == RSA Sign-Only
*/
if (packet->data[7] < 1 || packet->data[7] > 3) {
- logthing(LOGTHING_NOTICE,
- "Type 2 or 3 key, but not RSA: %llx (type %d)",
- keyid,
- packet->data[7]);
+ return ONAK_E_INVALID_PKT;
}
break;
case 4:
get_fingerprint(packet, buff, &length);
- for (keyid = 0, i = 12; i < 20; i++) {
- keyid <<= 8;
- keyid += buff[i];
+ for (*keyid = 0, i = 12; i < 20; i++) {
+ *keyid <<= 8;
+ *keyid += buff[i];
}
break;
default:
- logthing(LOGTHING_ERROR, "Unknown key type: %d",
- packet->data[0]);
+ return ONAK_E_UNKNOWN_VER;
}
- return keyid;
+ return ONAK_E_OK;
}
static struct openpgp_packet_list *sortpackets(struct openpgp_packet_list
return sorted;
}
-void get_skshash(struct openpgp_publickey *key, struct skshash *hash)
+onak_status_t get_skshash(struct openpgp_publickey *key, struct skshash *hash)
{
struct openpgp_packet_list *packets = NULL, *list_end = NULL;
struct openpgp_packet_list *curpacket;
md5_digest(&md5_context, 16, (uint8_t *) &hash->hash);
free_packet_list(packets);
+
+ return ONAK_E_OK;
}
uint8_t hexdigit(char c)
#include <inttypes.h>
#include "keystructs.h"
+#include "onak.h"
/**
* get_keyid - Given a public key returns the keyid.
* @publickey: The key to calculate the id for.
+ * @keeyid: The returned keyid
*
* This function returns the key id for a given public key.
*/
-uint64_t get_keyid(struct openpgp_publickey *publickey);
+onak_status_t get_keyid(struct openpgp_publickey *publickey, uint64_t *keyid);
/**
* get_fingerprint - Given a public key returns the fingerprint.
* @publickey: The key to calculate the id for.
- * @fingerprint: The fingerprint (must be at least 20 bytes of space).
+ * @fingerprint: The fingerprint (must be at least 20 bytes of space).
* @len: The length of the returned fingerprint.
*
* This function returns the fingerprint for a given public key. As Type 3
* fingerprints are 16 bytes and Type 4 are 20 the len field indicates
* which we've returned.
*/
-unsigned char *get_fingerprint(struct openpgp_packet *packet,
+onak_status_t get_fingerprint(struct openpgp_packet *packet,
unsigned char *fingerprint,
size_t *len);
/**
* get_packetid - Given a PGP packet returns the keyid.
* @packet: The packet to calculate the id for.
+ * @keyid: The returned keyid
*
* This function returns the key id for a given PGP packet.
*/
-uint64_t get_packetid(struct openpgp_packet *packet);
+onak_status_t get_packetid(struct openpgp_packet *packet, uint64_t *keyid);
/**
* get_skshash - Given a public key returns the SKS hash for it.
* make up the key. The caller should allocate the memory for the
* hash.
*/
-void get_skshash(struct openpgp_publickey *publickey, struct skshash *hash);
+onak_status_t get_skshash(struct openpgp_publickey *publickey,
+ struct skshash *hash);
/**
* parse_skshash - Parse a string into an SKS hash structure.
#include "keyindex.h"
#include "keystructs.h"
#include "log.h"
+#include "onak.h"
#include "onak-conf.h"
#include "openpgp.h"
time_t created_time = 0;
int type = 0;
int length = 0;
+ uint64_t keyid = 0;
while (subkeys != NULL) {
if (subkeys->packet->tag == OPENPGP_PACKET_PUBLICSUBKEY) {
"Unknown key type: %d",
subkeys->packet->data[0]);
}
-
+
+ if (get_packetid(subkeys->packet,
+ &keyid) != ONAK_E_OK) {
+ logthing(LOGTHING_ERROR, "Couldn't get keyid.");
+ }
printf("sub %5d%c/%08X %04d/%02d/%02d\n",
length,
(type == OPENPGP_PKALGO_RSA) ? 'R' :
((type == OPENPGP_PKALGO_ELGAMAL_ENC) ? 'g' :
((type == OPENPGP_PKALGO_DSA) ? 'D' : '?')),
- (uint32_t) (get_packetid(subkeys->packet) &
- 0xFFFFFFFF),
+ (uint32_t) (keyid & 0xFFFFFFFF),
created->tm_year + 1900,
created->tm_mon + 1,
created->tm_mday);
keys->publickey->data[0]);
}
- keyid = get_keyid(keys);
+ if (get_keyid(keys, &keyid) != ONAK_E_OK) {
+ logthing(LOGTHING_ERROR, "Couldn't get keyid.");
+ }
switch (type) {
case OPENPGP_PKALGO_RSA:
size_t fplength = 0;
unsigned char fp[20];
int c;
+ uint64_t keyid;
while (keys != NULL) {
created_time = (keys->publickey->data[1] << 24) +
switch (keys->publickey->data[0]) {
case 2:
case 3:
- printf("%016" PRIX64, get_keyid(keys));
+ if (get_keyid(keys, &keyid) != ONAK_E_OK) {
+ logthing(LOGTHING_ERROR, "Couldn't get keyid");
+ }
+ printf("%016" PRIX64, keyid);
type = keys->publickey->data[7];
length = (keys->publickey->data[8] << 8) +
keys->publickey->data[9];
size_t length = 0;
if (getphoto(publickey, indx, &photo,
- &length)) {
+ &length) == ONAK_E_OK) {
fwrite(photo,
1,
length,
#include "keystructs.h"
#include "ll.h"
-#include "log.h"
#include "mem.h"
/**
{
struct openpgp_packet *newpacket = NULL;
- log_assert(packet != NULL);
+ if (packet == NULL)
+ return NULL;
newpacket = malloc(sizeof (struct openpgp_packet));
if (newpacket != NULL) {
struct openpgp_packet_list **list_end,
struct openpgp_packet_list *packet_list)
{
- log_assert(list != NULL);
- log_assert(list_end != NULL);
-
for (; packet_list != NULL; packet_list = packet_list->next) {
ADD_PACKET_TO_LIST((*list_end),
packet_dup(packet_list->packet));
* including the data part.
*/
void free_packet(struct openpgp_packet *packet) {
- log_assert(packet != NULL);
-
if (packet->data != NULL) {
free(packet->data);
packet->data = NULL;
struct openpgp_packet_list *curpacket = NULL;
struct openpgp_packet_list *lastpacket = NULL;
struct openpgp_packet_list *nextpacket = NULL;
+ uint64_t keya, keyb;
if (a == NULL || b == NULL) {
/*
* Do nothing.
*/
- rc = 1;
- } else if (get_keyid(a) != get_keyid(b)) {
+ return 1;
+ }
+
+ if (get_keyid(a, &keya) != ONAK_E_OK) {
+ return 1;
+ } else if (get_keyid(b, &keyb) != ONAK_E_OK) {
+ return 1;
+ } else if (keya != keyb) {
/*
* Key IDs are different.
*/
unsigned char *photo = NULL;
size_t length = 0;
- if (getphoto(keys, 0, &photo, &length)) {
+ if (getphoto(keys, 0, &photo,
+ &length) == ONAK_E_OK) {
fwrite(photo,
1,
length,
#include "keyid.h"
#include "keystructs.h"
#include "ll.h"
-#include "log.h"
#include "mem.h"
+#include "onak.h"
#include "openpgp.h"
#include "parsekey.h"
* It's a signature packet. Add it to either the public
* key, to the current UID or the current subkey.
*/
- log_assert(curkey != NULL);
+ if (curkey == NULL)
+ return ONAK_E_INVALID_PARAM;
if (curkey->subkeys != NULL) {
ADD_PACKET_TO_LIST_END(curkey->last_subkey,
sig,
/*
* It's a UID packet (or a photo id, which is similar).
*/
- log_assert(curkey != NULL);
- log_assert(curkey->subkeys == NULL);
+ if (curkey == NULL)
+ return ONAK_E_INVALID_PARAM;
+ if (curkey->subkeys != NULL)
+ return ONAK_E_INVALID_PARAM;
ADD_PACKET_TO_LIST_END(curkey,
uid,
packet_dup(packets->packet));
/*
* It's a subkey packet.
*/
- log_assert(curkey != NULL);
+ if (curkey == NULL)
+ return ONAK_E_INVALID_PARAM;
ADD_PACKET_TO_LIST_END(curkey,
subkey,
packet_dup(packets->packet));
*/
break;
default:
- logthing(LOGTHING_ERROR,
- "Unsupported packet type: %d",
- packets->packet->tag);
+ /* Unsupported packet. Do what? Ignore for now. */
+ break;
}
packets = packets->next;
}
* packet stream and reads the packets into a linked list of packets
* ready for parsing as a public key or whatever.
*/
-int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
+onak_status_t read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
void *c),
void *ctx,
struct openpgp_packet_list **packets,
{
unsigned char curchar = 0;
struct openpgp_packet_list *curpacket = NULL;
- int rc = 0;
+ onak_status_t rc = ONAK_E_OK;
int keys = 0;
bool inpacket = false;
- log_assert(packets != NULL);
+ if (packets == NULL)
+ return ONAK_E_INVALID_PARAM;
+
curpacket = *packets;
if (curpacket != NULL) {
while (curpacket->next != NULL) {
curpacket->packet->length += 192;
} else if (curpacket->packet->length > 223 &&
curpacket->packet->length < 255) {
- logthing(LOGTHING_NOTICE,
- "Partial length;"
- " not supported.");
+ return ONAK_E_UNSUPPORTED_FEATURE;
} else if (curpacket->packet->length == 255) {
/*
* 5 byte length; ie 255 followed by 3
curpacket->packet->length += curchar;
break;
case 3:
- logthing(LOGTHING_ERROR,
- "Unsupported length type 3.");
+ rc = ONAK_E_UNSUPPORTED_FEATURE;
curpacket->packet->length = 0;
curpacket->packet->data = NULL;
- rc = -1;
break;
}
}
malloc(curpacket->packet->length *
sizeof(unsigned char));
if (curpacket->packet->data == NULL) {
- logthing(LOGTHING_ERROR,
- "Can't allocate memory for "
- "packet!");
- rc = -1;
+ rc = ONAK_E_NOMEM;
} else {
rc = getchar_func(ctx,
curpacket->packet->length,
}
inpacket = false;
} else {
- logthing(LOGTHING_ERROR, "Unexpected character: 0x%X",
- curchar);
- rc = 1;
+ rc = ONAK_E_INVALID_PKT;
}
}
* This function uses putchar_func to write characters to an OpenPGP
* packet stream from a linked list of packets.
*/
-int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
+onak_status_t write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
void *c),
void *ctx,
struct openpgp_packet_list *packets)
putchar_func(ctx, 1, &curchar);
} else if (packets->packet->length > 8382 &&
packets->packet->length < 0xFFFFFFFF) {
- logthing(LOGTHING_DEBUG,
- "Writing 5 byte length");
curchar = 255;
putchar_func(ctx, 1, &curchar);
curchar &= 0xFF;
putchar_func(ctx, 1, &curchar);
} else {
- logthing(LOGTHING_ERROR,
- "Unsupported new format length.");
+ return ONAK_E_UNSUPPORTED_FEATURE;
}
} else {
curchar |= (packets->packet->tag << 2);
packets->packet->data);
packets = packets->next;
}
- return 0;
+
+ return ONAK_E_OK;
}
/**
#define __PARSEKEY_H__
#include "keystructs.h"
+#include "onak.h"
/**
* parse_keys - Process a stream of packets for public keys + sigs.
* then only the public key component of the last key will be returned,
* none of the other packets of the key will be read.
*/
-int read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
+onak_status_t read_openpgp_stream(int (*getchar_func)(void *ctx, size_t count,
void *c),
void *ctx,
struct openpgp_packet_list **packets,
* This function uses putchar_func to write characters to an OpenPGP
* packet stream from a linked list of packets.
*/
-int write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
+onak_status_t write_openpgp_stream(int (*putchar_func)(void *ctx, size_t count,
void *c),
void *ctx,
struct openpgp_packet_list *packets);
#include "keyid.h"
#include "keyindex.h"
#include "keystructs.h"
-#include "log.h"
+#include "onak.h"
#include "photoid.h"
/**
* photo id NULL is returned. The returned data pointer refers to the key
* data supplied rather than a copy of it.
*/
-int getphoto(struct openpgp_publickey *key, int index, unsigned char **photo,
- size_t *length)
+onak_status_t getphoto(struct openpgp_publickey *key, int index,
+ unsigned char **photo, size_t *length)
{
struct openpgp_signedpacket_list *curuid = NULL;
int i = 0;
int j = 0;
- log_assert(key != NULL);
- log_assert(photo != NULL);
- log_assert(length != NULL);
+ if (key == NULL || photo == NULL || length == NULL)
+ return ONAK_E_INVALID_PARAM;
*photo = NULL;
*length <<= 8;
*length += curuid->packet->data[j++];
}
- logthing(LOGTHING_DEBUG, "Got photo, size %d",
- *length);
j++;
*length -= 17;
*photo = &(curuid->packet->data[j+16]);
curuid = curuid->next;
}
- return (*photo != NULL);
+ return *photo == NULL ? ONAK_E_NOT_FOUND : ONAK_E_OK;
}
* photo id NULL is returned. The returned data pointer refers to the key
* data supplied rather than a copy of it.
*/
-int getphoto(struct openpgp_publickey *key, int index, unsigned char **photo,
- size_t *length);
+onak_status_t getphoto(struct openpgp_publickey *key, int index,
+ unsigned char **photo, size_t *length);
#endif /* __PHOTOID_H__ */
cleankeys( keys );
/* Iterate over the keys... */
for( key = keys; key; key = key->next ) {
- uint64_t keyid = get_keyid( key );
+ uint64_t keyid;
+ get_keyid( key, &keyid );
for( uid = key->uids; uid; uid = uid->next ) {
REPEATTHISUID:
for( sig = uid->sigs, prevsig = NULL;