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 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/* Copyright 2012 Red Hat, Inc.
* All rights reserved.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* Author(s): Peter Jones <pjones@redhat.com>
*/
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <popt.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "efitypes.h"
#include "siglist.h"
struct hash_param {
char *name;
const efi_guid_t *guid;
int size;
};
static struct hash_param hash_params[] = {
{.name = "sha256",
.guid = &efi_guid_sha256,
.size = 32,
},
{.name = "sha1",
.guid = &efi_guid_sha1,
.size = 20,
},
};
static int n_hash_params = sizeof (hash_params) / sizeof (hash_params[0]);
int
set_hash_parameters(char *value __attribute__((__unused__)),
char *name, int *hash_number)
{
if (strcmp(name, "help")) {
for (int i = 0; i < n_hash_params; i++) {
if (!strcmp(name, hash_params[i].name)) {
*hash_number = i;
return 0;
}
}
} else {
printf("Supported hashes:");
for (int i = 0; i < n_hash_params; i++)
printf(" %s", hash_params[i].name);
printf("\n");
return 0;
}
return -1;
}
static int8_t hexchar_to_bin(char hex)
{
if (hex >= '0' && hex <= '9')
return hex - '0';
if (hex >= 'A' && hex <= 'F')
return hex - 'A' + 10;
if (hex >= 'a' && hex <= 'f')
return hex - 'a' + 10;
return -1;
}
static uint8_t *
hex_to_bin(char *hex, size_t size)
{
uint8_t *ret = calloc(1, size+1);
if (!ret)
return NULL;
for (unsigned int i = 0, j = 0; i < size*2; i+= 2, j++) {
uint8_t val;
val = hexchar_to_bin(hex[i]);
if (val > 15) {
out_of_range:
free(ret);
errno = ERANGE;
return NULL;
}
ret[j] = (val & 0xf) << 4;
val = hexchar_to_bin(hex[i+1]);
if (val > 15)
goto out_of_range;
ret[j] |= val & 0xf;
};
return ret;
}
int
main(int argc, char *argv[])
{
poptContext optCon;
efi_guid_t owner = efi_guid_redhat_2;
int rc;
char *infile = NULL;
char *outfile = NULL;
char *hash = NULL;
char *hash_type = "sha256";
char *certfile = NULL;
int certfd = -1;
void *cert_data = NULL;
size_t cert_size = 0;
int add = 1;
struct poptOption options[] = {
{.argInfo = POPT_ARG_INTL_DOMAIN,
.arg = "pesign" },
{.longName = "infile",
.shortName = 'i',
.argInfo = POPT_ARG_STRING,
.arg = &infile,
.descrip = "input database",
.argDescrip = "<infile>" },
{.longName = "outfile",
.shortName = 'o',
.argInfo = POPT_ARG_STRING,
.arg = &outfile,
.descrip = "output filename",
.argDescrip = "<outfile>" },
{.longName = "add",
.shortName = 'a',
.argInfo = POPT_ARG_VAL,
.arg = &add,
.val = 1,
.descrip = "add hash or certificate to list" },
{.longName = "remove",
.shortName = 'r',
.argInfo = POPT_ARG_VAL,
.arg = &add,
.descrip = "remove hash or certificate from list" },
{.longName = "hash",
.shortName = 'h',
.argInfo = POPT_ARG_STRING,
.arg = &hash,
.descrip = "hash value to add",
.argDescrip = "<hash>" },
{.longName = "hash-type",
.shortName = 't',
.argInfo = POPT_ARG_STRING|POPT_ARGFLAG_SHOW_DEFAULT,
.arg = &hash_type,
.descrip = "hash type to add",
.argDescrip = "<hash-type>" },
{.longName = "certificate",
.shortName = 'c',
.argInfo = POPT_ARG_STRING,
.arg = &certfile,
.descrip = "certificate to add",
.argDescrip = "<certfile>" },
POPT_AUTOALIAS
POPT_AUTOHELP
POPT_TABLEEND
};
optCon = poptGetContext("pesign", argc, (const char **)argv, options,0);
rc = poptReadDefaultConfig(optCon, 0);
if (rc < 0 && !(rc == POPT_ERROR_ERRNO && errno == ENOENT)) {
fprintf(stderr,
"efisiglist: poptReadDefaultConfig failed: %s\n",
poptStrerror(rc));
exit(1);
}
while ((rc = poptGetNextOpt(optCon)) > 0)
;
if (rc < -1) {
fprintf(stderr, "efisiglist: Invalid argument: %s: %s\n",
poptBadOption(optCon, 0), poptStrerror(rc));
exit(1);
}
if (poptPeekArg(optCon)) {
fprintf(stderr, "efisiglist: Invalid Argument: \"%s\"\n",
poptPeekArg(optCon));
exit(1);
}
if (hash && certfile) {
fprintf(stderr, "efisiglist: hash and certfile cannot be "
"specified at the same time\n");
exit(1);
}
int infd = -1;
if (infile) {
infd = open(infile, O_RDONLY);
if (infd < 0)
err(1, "could not open \"%s\"", infile);
}
int outfd = -1;
if (!outfile) {
fprintf(stderr, "efisiglist: no output file specified\n");
exit(1);
}
outfd = open(outfile, O_RDWR|O_APPEND|O_CREAT, 0644);
if (outfd < 0) {
fprintf(stderr, "efisiglist: could not open \"%s\": %m\n",
outfile);
exit(1);
}
int hash_index = -1;
if (hash) {
rc = set_hash_parameters(hash, hash_type, &hash_index);
if (rc < 0) {
fprintf(stderr, "efisiglist: invalid hash type: "
"\"%s\"\n", hash_type);
set_hash_parameters(hash, "help", &hash_index);
exit(1);
} else if (rc == 0 && hash_index == -1) {
exit(0);
}
int x = strlen(hash);
if (x != hash_params[hash_index].size * 2) {
fprintf(stderr, "efisiglist: hash \"%s\" requires "
"a %d-bit value, but supplied value is "
"%d bits\n", hash_params[hash_index].name,
hash_params[hash_index].size * 8, x * 4);
exit(1);
}
} else if (certfile) {
certfd = open(certfile, O_RDONLY, 0644);
if (certfd < 0) {
fprintf(stderr, "efisiglist: could not open \"%s\": "
"%m\n", certfile);
exit(1);
}
struct stat sb;
if (fstat(certfd, &sb) < 0) {
fprintf(stderr, "efisiglist: could not get the size "
"of \"%s\": %m\n", certfile);
exit(1);
}
cert_size = sb.st_size;
cert_data = mmap(NULL, cert_size, PROT_READ, MAP_PRIVATE,
certfd, 0);
if (cert_data == MAP_FAILED) {
fprintf(stderr, "efisiglist: could not map \"%s\": "
"%m\n", certfile);
exit(1);
}
}
if (add) {
if (hash) {
signature_list *sl = signature_list_new(
hash_params[hash_index].guid);
if (!sl) {
fprintf(stderr, "efisiglist: could not "
"allocate signature list: %m\n");
unlink(outfile);
exit(1);
}
uint8_t *binary_hash = hex_to_bin(hash,
hash_params[hash_index].size);
if (!binary_hash) {
fprintf(stderr, "efisiglist: could not "
"parse hash \"%s\": %m\n", hash);
unlink(outfile);
exit(1);
}
rc = signature_list_add_sig(sl, owner, binary_hash,
hash_params[hash_index].size);
if (rc < 0) {
fprintf(stderr,"efisiglist: could not add "
"hash to list: %m\n");
unlink(outfile);
exit(1);
}
void *blah;
size_t size = 0;
rc = signature_list_realize(sl, &blah, &size);
if (rc < 0) {
fprintf(stderr, "efisiglist: Could not realize "
"signature list: %m\n");
unlink(outfile);
exit(1);
}
rc = write(outfd, blah, size);
if (rc < 0) {
fprintf(stderr, "efisiglist: Could not write "
"signature list: %m\n");
unlink(outfile);
exit(1);
}
close(outfd);
exit(0);
} else if (certfile) {
efi_guid_t sig_type = efi_guid_x509_cert;
signature_list *sl = signature_list_new(&sig_type);
if (!sl) {
fprintf(stderr, "efisiglist: could not "
"allocate signature list: %m\n");
unlink(outfile);
exit(1);
}
rc = signature_list_add_sig(sl, owner, cert_data,
cert_size);
if (rc < 0) {
fprintf(stderr,"efisiglist: could not add "
"cert to list: %m\n");
unlink(outfile);
exit(1);
}
void *blah;
size_t size = 0;
rc = signature_list_realize(sl, &blah, &size);
if (rc < 0) {
fprintf(stderr, "efisiglist: Could not realize "
"signature list: %m\n");
unlink(outfile);
exit(1);
}
rc = write(outfd, blah, size);
if (rc < 0) {
fprintf(stderr, "efisiglist: Could not write "
"signature list: %m\n");
unlink(outfile);
exit(1);
}
munmap(cert_data, cert_size);
close(certfd);
close(outfd);
exit(0);
}
}
exit(1);
return 0;
}
|