ssize_t count = 0;
int ret = 0;
uint64_t keyid = 0;
- uint8_t fp[MAX_FINGERPRINT_LEN];
char *search = NULL;
struct openpgp_publickey *key = NULL;
struct openpgp_packet_list *packets = NULL;
struct openpgp_packet_list *list_end = NULL;
struct buffer_ctx storebuf;
struct skshash hash;
+ struct openpgp_fingerprint fingerprint;
/*
* Get the command from the client.
if (bytes > MAX_FINGERPRINT_LEN) {
ret = 1;
} else {
- read(fd, fp, bytes);
+ fingerprint.length = bytes;
+ read(fd, fingerprint.fp, bytes);
}
storebuf.offset = 0;
if (ret == 0) {
"Fetching by fingerprint"
", result: %d",
dbctx->fetch_key_fp(dbctx,
- fp, bytes,
+ &fingerprint,
&key, false));
if (key != NULL) {
storebuf.size = 8192;
#ifdef NEED_GET_FP
static int generic_fetch_key_fp(struct onak_dbctx *dbctx,
- uint8_t *fp, size_t fpsize,
+ struct openpgp_fingerprint *fingerprint,
struct openpgp_publickey **publickey, bool intrans)
{
uint64_t keyid;
int i;
- if (fpsize > MAX_FINGERPRINT_LEN) {
+ if (fingerprint->length > MAX_FINGERPRINT_LEN) {
return 0;
}
* if the backend can't do it we're going to fail anyway.
*/
keyid = 0;
- for (i = (fpsize - 8); i < fpsize; i++) {
- keyid = (keyid << 8) + fp[i];
+ for (i = (fingerprint->length - 8); i < fingerprint->length; i++) {
+ keyid = (keyid << 8) + fingerprint->fp[i];
}
return dbctx->fetch_key_id(dbctx, keyid, publickey, intrans);
* are using.
*/
int (*fetch_key_fp)(struct onak_dbctx *,
- uint8_t *fp,
- size_t fpsize,
+ struct openpgp_fingerprint *fingerprint,
struct openpgp_publickey **publickey,
bool intrans);
}
static int dynamic_fetch_key_fp(struct onak_dbctx *dbctx,
- uint8_t *fp, size_t fpsize,
+ struct openpgp_fingerprint *fingerprint,
struct openpgp_publickey **publickey, bool intrans)
{
struct onak_dynamic_dbctx *privctx =
(struct onak_dynamic_dbctx *) dbctx->priv;
return privctx->loadeddbctx->fetch_key_fp(privctx->loadeddbctx,
- fp, fpsize, publickey, intrans);
+ fingerprint, publickey, intrans);
}
static int dynamic_fetch_key_text(struct onak_dbctx *dbctx,
* hkp_fetch_key_fp - Given a fingerprint fetch the key from HKP server.
*/
static int hkp_fetch_key_fp(struct onak_dbctx *dbctx,
- uint8_t *fp, size_t fpsize,
+ struct openpgp_fingerprint *fingerprint,
struct openpgp_publickey **publickey,
bool intrans)
{
char keyurl[1024];
int i, ofs;
- if (fpsize > MAX_FINGERPRINT_LEN) {
+ if (fingerprint->length > MAX_FINGERPRINT_LEN) {
return 0;
}
ofs = snprintf(keyurl, sizeof(keyurl),
"%s/lookup?op=get&search=0x", privctx->hkpbase);
- if ((ofs + fpsize * 2 + 1)> sizeof(keyurl)) {
+ if ((ofs + fingerprint->length * 2 + 1)> sizeof(keyurl)) {
return 0;
}
- for (i = 0; i < fpsize; i++) {
- ofs += sprintf(&keyurl[ofs], "%02X", fp[i]);
+ for (i = 0; i < fingerprint->length; i++) {
+ ofs += sprintf(&keyurl[ofs], "%02X", fingerprint->fp[i]);
}
return (hkp_fetch_key_url(dbctx, keyurl, publickey, intrans));
}
static int keyd_fetch_key_fp(struct onak_dbctx *dbctx,
- uint8_t *fp, size_t fpsize,
+ struct openpgp_fingerprint *fingerprint,
struct openpgp_publickey **publickey,
bool intrans)
{
ssize_t count = 0;
uint8_t size;
- if (fpsize > MAX_FINGERPRINT_LEN) {
+ if (fingerprint->length > MAX_FINGERPRINT_LEN) {
return 0;
}
write(keyd_fd, &cmd, sizeof(cmd));
read(keyd_fd, &cmd, sizeof(cmd));
if (cmd == KEYD_REPLY_OK) {
- size = fpsize;
+ size = fingerprint->length;
write(keyd_fd, &size, sizeof(size));
- write(keyd_fd, fp, size);
+ write(keyd_fd, fingerprint->fp, size);
keybuf.offset = 0;
read(keyd_fd, &keybuf.size, sizeof(keybuf.size));
if (keybuf.size > 0) {
* which we've returned.
*/
onak_status_t get_fingerprint(struct openpgp_packet *packet,
- unsigned char *fingerprint,
- size_t *len)
+ struct openpgp_fingerprint *fingerprint)
{
struct sha1_ctx sha_ctx;
struct md5_ctx md5_context;
if (fingerprint == NULL)
return ONAK_E_INVALID_PARAM;
- if (len == NULL)
- return ONAK_E_INVALID_PARAM;
- *len = 0;
+ fingerprint->length = 0;
switch (packet->data[0]) {
case 2:
packet->data[11+modlen] + 7) >> 3;
md5_update(&md5_context, explen, &packet->data[12 + modlen]);
- *len = 16;
- md5_digest(&md5_context, *len, fingerprint);
+ fingerprint->length = 16;
+ md5_digest(&md5_context, fingerprint->length, fingerprint->fp);
break;
sha1_update(&sha_ctx, sizeof(c), &c);
sha1_update(&sha_ctx, packet->length,
packet->data);
- *len = 20;
- sha1_digest(&sha_ctx, *len, fingerprint);
+ fingerprint->length = 20;
+ sha1_digest(&sha_ctx, fingerprint->length, fingerprint->fp);
break;
default:
{
int offset = 0;
int i = 0;
- size_t length = 0;
- unsigned char buff[20];
+ struct openpgp_fingerprint fingerprint;
#ifdef NETTLE_WITH_RIPEMD160
struct ripemd160_ctx ripemd160_context;
uint8_t data;
ripemd160_digest(&ripemd160_context,
RIPEMD160_DIGEST_SIZE,
- buff);
+ fingerprint.fp);
for (*keyid = 0, i = 12; i < 20; i++) {
*keyid <<= 8;
- *keyid += buff[i];
+ *keyid += fingerprint.fp[i];
}
return ONAK_E_OK;
}
break;
case 4:
- get_fingerprint(packet, buff, &length);
+ get_fingerprint(packet, &fingerprint);
for (*keyid = 0, i = 12; i < 20; i++) {
*keyid <<= 8;
- *keyid += buff[i];
+ *keyid += fingerprint.fp[i];
}
break;
/**
* 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).
- * @len: The length of the returned fingerprint.
+ * @fingerprint: The fingerprint structure to store the result in
*
* 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.
*/
onak_status_t get_fingerprint(struct openpgp_packet *packet,
- unsigned char *fingerprint,
- size_t *len);
+ struct openpgp_fingerprint *fingerprint);
/**
* get_packetid - Given a PGP packet returns the keyid.
void display_fingerprint(struct openpgp_publickey *key)
{
int i = 0;
- size_t length = 0;
- unsigned char fp[20];
+ struct openpgp_fingerprint fingerprint;
- get_fingerprint(key->publickey, fp, &length);
+ get_fingerprint(key->publickey, &fingerprint);
printf(" Key fingerprint =");
- for (i = 0; i < length; i++) {
- if ((length == 16) ||
+ for (i = 0; i < fingerprint.length; i++) {
+ if ((fingerprint.length == 16) ||
(i % 2 == 0)) {
printf(" ");
}
- if (length == 20 && (i * 2) == length) {
+ if (fingerprint.length == 20 &&
+ (i * 2) == fingerprint.length) {
/* Extra space in the middle of a SHA1 fingerprint */
printf(" ");
}
- printf("%02X", fp[i]);
+ printf("%02X", fingerprint.fp[i]);
}
printf("\n");
int type = 0;
int length = 0;
int i = 0;
- size_t fplength = 0;
- unsigned char fp[20];
int c;
uint64_t keyid;
+ struct openpgp_fingerprint fingerprint;
while (keys != NULL) {
created_time = (keys->publickey->data[1] << 24) +
type = keys->publickey->data[7];
break;
case 4:
- (void) get_fingerprint(keys->publickey, fp, &fplength);
+ (void) get_fingerprint(keys->publickey, &fingerprint);
- for (i = 0; i < fplength; i++) {
- printf("%02X", fp[i]);
+ for (i = 0; i < fingerprint.length; i++) {
+ printf("%02X", fingerprint.fp[i]);
}
type = keys->publickey->data[5];
/* v3 MD5 fingerprint is 16 characters, v4 SHA-1 fingerprint is 20 */
#define MAX_FINGERPRINT_LEN 20
+/**
+ * @brief Stores the fingerprint of an OpenPGP key
+ */
+struct openpgp_fingerprint {
+ /** Length of fingerprint. 16 bytes for v3, 20 for v4 */
+ size_t length;
+ /** Fingerprint data. Only the first length bytes are valid */
+ uint8_t fp[MAX_FINGERPRINT_LEN];
+};
+
/**
* @brief Stores an OpenPGP packet.
*
#define OP_HGET 5
void find_keys(struct onak_dbctx *dbctx,
- char *search, uint64_t keyid, uint8_t *fp, size_t fpsize,
- bool ishex, bool isfp, bool fingerprint, bool skshash,
+ char *search, uint64_t keyid,
+ struct openpgp_fingerprint *fingerprint,
+ bool ishex, bool isfp, bool dispfp, bool skshash,
bool exact, bool verbose, bool mrhkp)
{
struct openpgp_publickey *publickey = NULL;
count = dbctx->fetch_key_id(dbctx, keyid, &publickey,
false);
} else if (isfp) {
- count = dbctx->fetch_key_fp(dbctx, fp, fpsize, &publickey,
+ count = dbctx->fetch_key_fp(dbctx, fingerprint, &publickey,
false);
} else {
count = dbctx->fetch_key_text(dbctx, search, &publickey);
printf("info:1:%d\n", count);
mrkey_index(publickey);
} else {
- key_index(dbctx, publickey, verbose, fingerprint,
+ key_index(dbctx, publickey, verbose, dispfp,
skshash, true);
}
free_publickey(publickey);
int op = OP_UNKNOWN;
int i;
int indx = 0;
- bool fingerprint = false;
+ bool dispfp = false;
bool skshash = false;
bool exact = false;
bool ishex = false;
bool isfp = false;
bool mrhkp = false;
uint64_t keyid = 0;
- uint8_t fp[MAX_FINGERPRINT_LEN];
+ struct openpgp_fingerprint fingerprint;
char *search = NULL;
char *end = NULL;
struct openpgp_publickey *publickey = NULL;
params[i+1] = NULL;
if (search != NULL && strlen(search) == 42 &&
search[0] == '0' && search[1] == 'x') {
+ fingerprint.length = MAX_FINGERPRINT_LEN;
for (i = 0; i < MAX_FINGERPRINT_LEN; i++) {
- fp[i] = (hex2bin(search[2 + i * 2])
+ fingerprint.fp[i] = (hex2bin(
+ search[2 + i * 2])
<< 4) +
hex2bin(search[3 + i * 2]);
}
indx = atoi(params[i+1]);
} else if (!strcmp(params[i], "fingerprint")) {
if (!strcmp(params[i+1], "on")) {
- fingerprint = true;
+ dispfp = true;
}
} else if (!strcmp(params[i], "hash")) {
if (!strcmp(params[i+1], "on")) {
result = dbctx->fetch_key_id(dbctx, keyid,
&publickey, false);
} else if (isfp) {
- result = dbctx->fetch_key_fp(dbctx, fp,
- MAX_FINGERPRINT_LEN, &publickey, false);
+ result = dbctx->fetch_key_fp(dbctx,
+ &fingerprint, &publickey, false);
} else {
result = dbctx->fetch_key_text(dbctx,
search,
}
break;
case OP_INDEX:
- find_keys(dbctx, search, keyid, fp, MAX_FINGERPRINT_LEN,
- ishex, isfp, fingerprint, skshash,
+ find_keys(dbctx, search, keyid, &fingerprint,
+ ishex, isfp, dispfp, skshash,
exact, false, mrhkp);
break;
case OP_VINDEX:
- find_keys(dbctx, search, keyid, fp, MAX_FINGERPRINT_LEN,
- ishex, isfp, fingerprint, skshash,
+ find_keys(dbctx, search, keyid, &fingerprint,
+ ishex, isfp, dispfp, skshash,
exact, true, mrhkp);
break;
case OP_PHOTO:
if (isfp) {
- dbctx->fetch_key_fp(dbctx, fp,
- MAX_FINGERPRINT_LEN,
+ dbctx->fetch_key_fp(dbctx, &fingerprint,
&publickey, false);
} else {
dbctx->fetch_key_id(dbctx, keyid,
#include "version.h"
void find_keys(struct onak_dbctx *dbctx,
- char *search, uint64_t keyid, uint8_t *fp, bool ishex,
- bool isfp, bool fingerprint, bool skshash, bool exact,
- bool verbose)
+ char *search, uint64_t keyid,
+ struct openpgp_fingerprint *fingerprint,
+ bool ishex, bool isfp, bool dispfp, bool skshash,
+ bool exact, bool verbose)
{
struct openpgp_publickey *publickey = NULL;
int count = 0;
count = dbctx->fetch_key_id(dbctx, keyid, &publickey,
false);
} else if (isfp) {
- count = dbctx->fetch_key_fp(dbctx, fp, MAX_FINGERPRINT_LEN,
+ count = dbctx->fetch_key_fp(dbctx, fingerprint,
&publickey, false);
} else {
count = dbctx->fetch_key_text(dbctx, search, &publickey);
}
if (publickey != NULL) {
- key_index(dbctx, publickey, verbose, fingerprint, skshash,
+ key_index(dbctx, publickey, verbose, dispfp, skshash,
false);
free_publickey(publickey);
} else if (count == 0) {
char *search = NULL;
char *end = NULL;
uint64_t keyid = 0;
- uint8_t fp[MAX_FINGERPRINT_LEN];
int i;
bool ishex = false;
bool isfp = false;
bool update = false;
bool binary = false;
- bool fingerprint = false;
+ bool dispfp = false;
bool skshash = false;
int optchar;
struct dump_ctx dumpstate;
struct skshash hash;
struct onak_dbctx *dbctx;
+ struct openpgp_fingerprint fingerprint;
while ((optchar = getopt(argc, argv, "bc:fsuv")) != -1 ) {
switch (optchar) {
configfile = strdup(optarg);
break;
case 'f':
- fingerprint = true;
+ dispfp = true;
break;
case 's':
skshash = true;
search = argv[optind+1];
if (search != NULL && strlen(search) == 42 &&
search[0] == '0' && search[1] == 'x') {
+ fingerprint.length = MAX_FINGERPRINT_LEN;
for (i = 0; i < MAX_FINGERPRINT_LEN; i++) {
- fp[i] = (hex2bin(search[2 + i * 2]) << 4) +
+ fingerprint.fp[i] =
+ (hex2bin(search[2 + i * 2]) << 4) +
hex2bin(search[3 + i * 2]);
}
isfp = true;
}
dbctx = config.dbinit(false);
if (!strcmp("index", argv[optind])) {
- find_keys(dbctx, search, keyid, fp, ishex, isfp,
- fingerprint, skshash,
+ find_keys(dbctx, search, keyid, &fingerprint, ishex,
+ isfp, dispfp, skshash,
false, false);
} else if (!strcmp("vindex", argv[optind])) {
- find_keys(dbctx, search, keyid, fp, ishex, isfp,
- fingerprint, skshash,
+ find_keys(dbctx, search, keyid, &fingerprint, ishex,
+ isfp, dispfp, skshash,
false, true);
} else if (!strcmp("getphoto", argv[optind])) {
if (!ishex) {
" You must supply a keyid / "
"fingerprint.");
} else if ((isfp &&
- dbctx->fetch_key_fp(dbctx, fp,
- MAX_FINGERPRINT_LEN,
+ dbctx->fetch_key_fp(dbctx,
+ &fingerprint,
&keys, false)) ||
(ishex &&
dbctx->fetch_key_id(dbctx, keyid,