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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/*
* decodekey.c - Routines to further decode an OpenPGP key.
*
* Copyright 2002-2008 Jonathan McDowell <noodles@earth.li>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; version 2 of the License.
*
* This program 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 General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "decodekey.h"
#include "hash.h"
#include "keyid.h"
#include "keystructs.h"
#include "ll.h"
#include "log.h"
#include "openpgp.h"
/*
* parse_subpackets - Parse the subpackets of a Type 4 signature.
* @data: The subpacket data.
* @keyid: A pointer to where we should return the keyid.
* @creationtime: A pointer to where we should return the creation time.
*
* This function parses the subkey data of a Type 4 signature and fills
* in the supplied variables. It also returns the length of the data
* processed. If the value of any piece of data is not desired a NULL
* can be passed instead of a pointer to a storage area for that value.
*/
int parse_subpackets(unsigned char *data, uint64_t *keyid, time_t *creation)
{
int offset = 0;
int length = 0;
int packetlen = 0;
log_assert(data != NULL);
length = (data[0] << 8) + data[1] + 2;
offset = 2;
while (offset < length) {
packetlen = data[offset++];
if (packetlen > 191 && packetlen < 255) {
packetlen = ((packetlen - 192) << 8) +
data[offset++] + 192;
} else if (packetlen == 255) {
packetlen = data[offset++];
packetlen <<= 8;
packetlen = data[offset++];
packetlen <<= 8;
packetlen = data[offset++];
packetlen <<= 8;
packetlen = data[offset++];
}
switch (data[offset] & 0x7F) {
case OPENPGP_SIGSUB_CREATION:
/*
* Signature creation time.
*/
if (creation != NULL) {
*creation = data[offset + packetlen - 4];
*creation <<= 8;
*creation = data[offset + packetlen - 3];
*creation <<= 8;
*creation = data[offset + packetlen - 2];
*creation <<= 8;
*creation = data[offset + packetlen - 1];
}
break;
/*
* Signature expiration time. Might want to output this?
*/
break;
case OPENPGP_SIGSUB_ISSUER:
if (keyid != NULL) {
*keyid = data[offset+packetlen - 8];
*keyid <<= 8;
*keyid += data[offset+packetlen - 7];
*keyid <<= 8;
*keyid += data[offset+packetlen - 6];
*keyid <<= 8;
*keyid += data[offset+packetlen - 5];
*keyid <<= 8;
*keyid += data[offset+packetlen - 4];
*keyid <<= 8;
*keyid += data[offset+packetlen - 3];
*keyid <<= 8;
*keyid += data[offset+packetlen - 2];
*keyid <<= 8;
*keyid += data[offset+packetlen - 1];
}
break;
case OPENPGP_SIGSUB_EXPIRY:
case OPENPGP_SIGSUB_EXPORTABLE:
case OPENPGP_SIGSUB_TRUSTSIG:
case OPENPGP_SIGSUB_REGEX:
case OPENPGP_SIGSUB_KEYEXPIRY:
case OPENPGP_SIGSUB_PREFSYM:
case OPENPGP_SIGSUB_NOTATION:
case OPENPGP_SIGSUB_PREFHASH:
case OPENPGP_SIGSUB_PREFCOMPRESS:
case OPENPGP_SIGSUB_KEYSERVER:
case OPENPGP_SIGSUB_PRIMARYUID:
case OPENPGP_SIGSUB_POLICYURI:
case OPENPGP_SIGSUB_KEYFLAGS:
/*
* Various subpacket types we know about, but don't
* currently handle. Some are candidates for being
* supported if we add signature checking support.
*/
break;
default:
/*
* We don't care about unrecognized packets unless bit
* 7 is set in which case we log a major error.
*/
if (data[offset] & 0x80) {
logthing(LOGTHING_CRITICAL,
"Critical subpacket type not parsed: 0x%X",
data[offset]);
}
}
offset += packetlen;
}
return length;
}
/**
* keysigs - Return the sigs on a given OpenPGP signature list.
* @curll: The current linked list. Can be NULL to create a new list.
* @sigs: The signature list we want the sigs on.
*
* Returns a linked list of stats_key elements containing the sigs on the
* supplied OpenPGP packet list.
*/
struct ll *keysigs(struct ll *curll,
struct openpgp_packet_list *sigs)
{
uint64_t keyid = 0;
while (sigs != NULL) {
keyid = sig_keyid(sigs->packet);
sigs = sigs->next;
curll = lladd(curll, createandaddtohash(keyid));
}
return curll;
}
/**
* sig_info - Get info on a given OpenPGP signature packet
* @packet: The signature packet
* @keyid: A pointer for where to return the signature keyid
* @creation: A pointer for where to return the signature creation time
*
* Gets any info about a signature packet; parses the subpackets for a v4
* key or pulls the data directly from v2/3. NULL can be passed for any
* values which aren't cared about.
*/
void sig_info(struct openpgp_packet *packet, uint64_t *keyid, time_t *creation)
{
int length = 0;
if (packet != NULL) {
switch (packet->data[0]) {
case 2:
case 3:
if (keyid != NULL) {
*keyid = packet->data[7];
*keyid <<= 8;
*keyid += packet->data[8];
*keyid <<= 8;
*keyid += packet->data[9];
*keyid <<= 8;
*keyid += packet->data[10];
*keyid <<= 8;
*keyid += packet->data[11];
*keyid <<= 8;
*keyid += packet->data[12];
*keyid <<= 8;
*keyid += packet->data[13];
*keyid <<= 8;
*keyid += packet->data[14];
}
if (creation != NULL) {
*creation = packet->data[3];
*creation <<= 8;
*creation = packet->data[4];
*creation <<= 8;
*creation = packet->data[5];
*creation <<= 8;
*creation = packet->data[6];
}
break;
case 4:
length = parse_subpackets(&packet->data[4],
keyid, creation);
parse_subpackets(&packet->data[length + 4],
keyid, creation);
/*
* Don't bother to look at the unsigned packets.
*/
break;
default:
break;
}
}
return;
}
/**
* sig_keyid - Return the keyid for a given OpenPGP signature packet.
* @packet: The signature packet.
*
* Returns the keyid for the supplied signature packet.
*/
uint64_t sig_keyid(struct openpgp_packet *packet)
{
uint64_t keyid = 0;
sig_info(packet, &keyid, NULL);
return keyid;
}
/*
* TODO: Abstract out; all our linked lists should be generic and then we can
* llsize them.
*/
int spsize(struct openpgp_signedpacket_list *list)
{
int size = 0;
struct openpgp_signedpacket_list *cur;
for (cur = list; cur != NULL; cur = cur->next, size++) ;
return size;
}
/**
* keyuids - Takes a key and returns an array of its UIDs
* @key: The key to get the uids of.
* @primary: A pointer to store the primary UID in.
*
* keyuids takes a public key structure and builds an array of the UIDs
* on the key. It also attempts to work out the primary UID and returns a
* separate pointer to that particular element of the array.
*/
char **keyuids(struct openpgp_publickey *key, char **primary)
{
struct openpgp_signedpacket_list *curuid = NULL;
char buf[1024];
char **uids = NULL;
int count = 0;
if (primary != NULL) {
*primary = NULL;
}
if (key != NULL && key->uids != NULL) {
uids = malloc((spsize(key->uids) + 1) * sizeof (char *));
curuid = key->uids;
while (curuid != NULL) {
buf[0] = 0;
if (curuid->packet->tag == OPENPGP_PACKET_UID) {
snprintf(buf, 1023, "%.*s",
(int) curuid->packet->length,
curuid->packet->data);
uids[count++] = strdup(buf);
}
curuid = curuid -> next;
}
uids[count] = NULL;
/*
* TODO: Parse subpackets for real primary ID (v4 keys)
*/
if (primary != NULL) {
*primary = uids[0];
}
}
return uids;
}
/**
* keysubkeys - Takes a key and returns an array of its subkey keyids.
* @key: The key to get the subkeys of.
*
* keysubkeys takes a public key structure and returns an array of the
* subkey keyids for that key.
*/
uint64_t *keysubkeys(struct openpgp_publickey *key)
{
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);
cursubkey = cursubkey -> next;
}
subkeys[count] = 0;
}
return subkeys;
}
|