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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <time.h>
29 #include <syslog.h>
30 #include <ipxe/uaccess.h>
31 #include <ipxe/image.h>
32 #include <ipxe/cms.h>
33 #include <ipxe/validator.h>
34 #include <ipxe/monojob.h>
35 #include <usr/imgtrust.h>
36 
37 /** @file
38  *
39  * Image trust management
40  *
41  */
42 
43 /**
44  * Verify image using downloaded signature
45  *
46  * @v image Image to verify
47  * @v signature Image containing signature
48  * @v name Required common name, or NULL to allow any name
49  * @ret rc Return status code
50  */
51 int imgverify ( struct image *image, struct image *signature,
52  const char *name ) {
53  struct cms_message *cms;
54  struct cms_participant *part;
55  time_t now;
56  int rc;
57 
58  /* Parse signature */
59  if ( ( rc = cms_message ( signature, &cms ) ) != 0 )
60  goto err_parse;
61 
62  /* Complete all certificate chains */
63  list_for_each_entry ( part, &cms->participants, list ) {
64  if ( ( rc = create_validator ( &monojob, part->chain,
65  NULL ) ) != 0 )
66  goto err_create_validator;
67  if ( ( rc = monojob_wait ( NULL, 0 ) ) != 0 )
68  goto err_validator_wait;
69  }
70 
71  /* Use signature to verify image */
72  now = time ( NULL );
73  if ( ( rc = cms_verify ( cms, image, name, now, NULL, NULL ) ) != 0 )
74  goto err_verify;
75 
76  /* Drop reference to message */
77  cms_put ( cms );
78  cms = NULL;
79 
80  /* Record signature verification */
81  syslog ( LOG_NOTICE, "Image \"%s\" signature OK\n", image->name );
82 
83  return 0;
84 
85  err_verify:
86  err_validator_wait:
87  err_create_validator:
88  cms_put ( cms );
89  err_parse:
90  syslog ( LOG_ERR, "Image \"%s\" signature bad: %s\n",
91  image->name, strerror ( rc ) );
92  return rc;
93 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1984
int imgverify(struct image *image, struct image *signature, const char *name)
Verify image using downloaded signature.
Definition: imgtrust.c:51
int monojob_wait(const char *string, unsigned long timeout)
Wait for single foreground job to complete.
Definition: monojob.c:81
Error codes.
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:854
An executable image.
Definition: image.h:24
#define LOG_ERR
Error: error conditions.
Definition: syslog.h:35
Access to external ("user") memory.
A CMS message.
Definition: cms.h:55
Single foreground job.
CMS participant information.
Definition: cms.h:39
int create_validator(struct interface *job, struct x509_chain *chain, struct x509_root *root)
Instantiate a certificate validator.
Definition: validator.c:759
Certificate validator.
Executable images.
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
Image trust management.
struct interface monojob
Definition: monojob.c:56
System logger.
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
Date and time.
static void cms_put(struct cms_message *cms)
Drop reference to CMS message.
Definition: cms.h:94
struct list_head participants
List of participant information blocks.
Definition: cms.h:66
struct list_head list
List of participant information blocks.
Definition: cms.h:41
#define syslog(priority, fmt,...)
Write message to system log.
Definition: syslog.h:93
struct x509_chain * chain
Certificate chain.
Definition: cms.h:43
int64_t time_t
Seconds since the Epoch.
Definition: time.h:18
u8 signature
CPU signature.
Definition: CIB_PRM.h:35
Cryptographic Message Syntax (PKCS #7)
char * name
Name.
Definition: image.h:34
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define LOG_NOTICE
Notice: normal but significant conditions.
Definition: syslog.h:41