iPXE
imgtrust.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
30#include <time.h>
31#include <syslog.h>
32#include <ipxe/image.h>
33#include <ipxe/cms.h>
34#include <ipxe/validator.h>
35#include <ipxe/monojob.h>
36#include <usr/imgtrust.h>
37
38/** @file
39 *
40 * Image trust management
41 *
42 */
43
44/**
45 * Verify image using downloaded signature
46 *
47 * @v image Image to verify
48 * @v signature Image containing signature
49 * @v name Required common name, or NULL to allow any name
50 * @ret rc Return status code
51 */
52int imgverify ( struct image *image, struct image *signature,
53 const char *name ) {
54 struct cms_message *cms;
55 struct cms_participant *part;
56 time_t now;
57 int rc;
58
59 /* Parse signature */
60 if ( ( rc = cms_message ( signature, &cms ) ) != 0 )
61 goto err_parse;
62
63 /* Complete all certificate chains */
64 list_for_each_entry ( part, &cms->participants, list ) {
65 if ( ( rc = create_validator ( &monojob, part->chain,
66 NULL ) ) != 0 )
67 goto err_create_validator;
68 if ( ( rc = monojob_wait ( NULL, 0 ) ) != 0 )
69 goto err_validator_wait;
70 }
71
72 /* Use signature to verify image */
73 now = time ( NULL );
74 if ( ( rc = cms_verify ( cms, image, name, now, NULL, NULL ) ) != 0 )
75 goto err_verify;
76
77 /* Drop reference to message */
78 cms_put ( cms );
79 cms = NULL;
80
81 /* Record signature verification */
82 syslog ( LOG_NOTICE, "Image \"%s\" signature OK\n", image->name );
83
84 return 0;
85
86 err_verify:
87 err_validator_wait:
88 err_create_validator:
89 cms_put ( cms );
90 err_parse:
91 syslog ( LOG_ERR, "Image \"%s\" signature bad: %s\n",
92 image->name, strerror ( rc ) );
93 return rc;
94}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
u8 signature
CPU signature.
Definition CIB_PRM.h:7
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
const char * name
Definition ath9k_hw.c:1986
int cms_verify(struct cms_message *cms, struct image *image, const char *name, time_t time, struct x509_chain *store, struct x509_root *root)
Verify CMS signature.
Definition cms.c:834
Cryptographic Message Syntax (PKCS #7)
static void cms_put(struct cms_message *cms)
Drop reference to CMS message.
Definition cms.h:94
Error codes.
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define LOG_ERR
Error: error conditions.
Definition syslog.h:36
#define LOG_NOTICE
Notice: normal but significant conditions.
Definition syslog.h:42
Executable images.
int imgverify(struct image *image, struct image *signature, const char *name)
Verify image using downloaded signature.
Definition imgtrust.c:52
Image trust management.
String functions.
int64_t time_t
Seconds since the Epoch.
Definition time.h:19
Date and time.
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition list.h:432
struct interface monojob
Definition monojob.c:57
int monojob_wait(const char *string, unsigned long timeout)
Wait for single foreground job to complete.
Definition monojob.c:82
Single foreground job.
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
A CMS message.
Definition cms.h:55
struct list_head participants
List of participant information blocks.
Definition cms.h:66
CMS participant information.
Definition cms.h:39
struct list_head list
List of participant information blocks.
Definition cms.h:41
struct x509_chain * chain
Certificate chain.
Definition cms.h:43
An executable image.
Definition image.h:24
char * name
Name.
Definition image.h:38
System logger.
#define syslog(priority, fmt,...)
Write message to system log.
Definition syslog.h:94
int create_validator(struct interface *job, struct x509_chain *chain, struct x509_root *root)
Instantiate a certificate validator.
Definition validator.c:760
Certificate validator.