These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / usr / imgtrust.c
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         size_t len;
54         void *data;
55         struct cms_signature *sig;
56         struct cms_signer_info *info;
57         time_t now;
58         int rc;
59
60         /* Mark image as untrusted */
61         image_untrust ( image );
62
63         /* Copy signature to internal memory */
64         len = signature->len;
65         data = malloc ( len );
66         if ( ! data ) {
67                 rc = -ENOMEM;
68                 goto err_alloc;
69         }
70         copy_from_user ( data, signature->data, 0, len );
71
72         /* Parse signature */
73         if ( ( rc = cms_signature ( data, len, &sig ) ) != 0 )
74                 goto err_parse;
75
76         /* Free internal copy of signature */
77         free ( data );
78         data = NULL;
79
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;
86         }
87
88         /* Use signature to verify image */
89         now = time ( NULL );
90         if ( ( rc = cms_verify ( sig, image->data, image->len,
91                                  name, now, NULL, NULL ) ) != 0 )
92                 goto err_verify;
93
94         /* Drop reference to signature */
95         cms_put ( sig );
96         sig = NULL;
97
98         /* Mark image as trusted */
99         image_trust ( image );
100         syslog ( LOG_NOTICE, "Image \"%s\" signature OK\n", image->name );
101
102         return 0;
103
104  err_verify:
105  err_validator_wait:
106  err_create_validator:
107         cms_put ( sig );
108  err_parse:
109         free ( data );
110  err_alloc:
111         syslog ( LOG_ERR, "Image \"%s\" signature bad: %s\n",
112                  image->name, strerror ( rc ) );
113         return rc;
114 }