2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
30 #include <ipxe/uaccess.h>
31 #include <ipxe/image.h>
33 #include <ipxe/validator.h>
34 #include <ipxe/monojob.h>
35 #include <usr/imgtrust.h>
39 * Image trust management
44 * Verify image using downloaded signature
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
51 int imgverify ( struct image
*image
, struct image
*signature
,
55 struct cms_signature
*sig
;
56 struct cms_signer_info
*info
;
60 /* Mark image as untrusted */
61 image_untrust ( image
);
63 /* Copy signature to internal memory */
65 data
= malloc ( len
);
70 copy_from_user ( data
, signature
->data
, 0, len
);
73 if ( ( rc
= cms_signature ( data
, len
, &sig
) ) != 0 )
76 /* Free internal copy of signature */
80 /* Complete all certificate chains */
81 list_for_each_entry ( info
, &sig
->info
, list
) {
82 if ( ( rc
= create_validator ( &monojob
, info
->chain
) ) != 0 )
83 goto err_create_validator
;
84 if ( ( rc
= monojob_wait ( NULL
, 0 ) ) != 0 )
85 goto err_validator_wait
;
88 /* Use signature to verify image */
90 if ( ( rc
= cms_verify ( sig
, image
->data
, image
->len
,
91 name
, now
, NULL
, NULL
) ) != 0 )
94 /* Drop reference to signature */
98 /* Mark image as trusted */
99 image_trust ( image
);
100 syslog ( LOG_NOTICE
, "Image \"%s\" signature OK\n", image
->name
);
106 err_create_validator
:
111 syslog ( LOG_ERR
, "Image \"%s\" signature bad: %s\n",
112 image
->name
, strerror ( rc
) );