~ubuntu-branches/ubuntu/raring/ipxe/raring

« back to all changes in this revision

Viewing changes to src/hci/commands/image_trust_cmd.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-11-14 15:47:31 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20121114154731-jhuy5d1h2jw75qe9
Tags: 1.0.0+git-4.d6b0b76-0ubuntu1
* New upstream snapshot:
  - d/p/iscsi*.patch: Dropped - included in snapshot.
  - Refreshed all other patches.
* d/p/enable-https.patch: Enable HTTPS support (LP: #1025239).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
20
FILE_LICENCE ( GPL2_OR_LATER );
 
21
 
 
22
#include <stdint.h>
 
23
#include <stdio.h>
 
24
#include <getopt.h>
 
25
#include <ipxe/image.h>
 
26
#include <ipxe/command.h>
 
27
#include <ipxe/parseopt.h>
 
28
#include <usr/imgmgmt.h>
 
29
#include <usr/imgtrust.h>
 
30
 
 
31
/** @file
 
32
 *
 
33
 * Image trust management commands
 
34
 *
 
35
 */
 
36
 
 
37
/** "imgtrust" options */
 
38
struct imgtrust_options {
 
39
        /** Allow trusted images */
 
40
        int allow;
 
41
        /** Make trust requirement permanent */
 
42
        int permanent;
 
43
};
 
44
 
 
45
/** "imgtrust" option list */
 
46
static struct option_descriptor imgtrust_opts[] = {
 
47
        OPTION_DESC ( "allow", 'a', no_argument,
 
48
                      struct imgtrust_options, allow, parse_flag ),
 
49
        OPTION_DESC ( "permanent", 'p', no_argument,
 
50
                      struct imgtrust_options, permanent, parse_flag ),
 
51
};
 
52
 
 
53
/** "imgtrust" command descriptor */
 
54
static struct command_descriptor imgtrust_cmd =
 
55
        COMMAND_DESC ( struct imgtrust_options, imgtrust_opts, 0, 0,
 
56
                       "[--allow] [--permanent]" );
 
57
 
 
58
/**
 
59
 * The "imgtrust" command
 
60
 *
 
61
 * @v argc              Argument count
 
62
 * @v argv              Argument list
 
63
 * @ret rc              Return status code
 
64
 */
 
65
static int imgtrust_exec ( int argc, char **argv ) {
 
66
        struct imgtrust_options opts;
 
67
        int rc;
 
68
 
 
69
        /* Parse options */
 
70
        if ( ( rc = parse_options ( argc, argv, &imgtrust_cmd, &opts ) ) != 0 )
 
71
                return rc;
 
72
 
 
73
        /* Set trust requirement */
 
74
        if ( ( rc = image_set_trust ( ( ! opts.allow ),
 
75
                                      opts.permanent ) ) != 0 ) {
 
76
                printf ( "Could not set image trust requirement: %s\n",
 
77
                         strerror ( rc ) );
 
78
                return rc;
 
79
        }
 
80
 
 
81
        return 0;
 
82
}
 
83
 
 
84
/** "imgverify" options */
 
85
struct imgverify_options {
 
86
        /** Required signer common name */
 
87
        const char *signer;
 
88
        /** Keep signature after verification */
 
89
        int keep;
 
90
};
 
91
 
 
92
/** "imgverify" option list */
 
93
static struct option_descriptor imgverify_opts[] = {
 
94
        OPTION_DESC ( "signer", 's', required_argument,
 
95
                      struct imgverify_options, signer, parse_string ),
 
96
        OPTION_DESC ( "keep", 'k', no_argument,
 
97
                      struct imgverify_options, keep, parse_flag ),
 
98
};
 
99
 
 
100
/** "imgverify" command descriptor */
 
101
static struct command_descriptor imgverify_cmd =
 
102
        COMMAND_DESC ( struct imgverify_options, imgverify_opts, 2, 2,
 
103
                       "[--signer <signer>] [--keep] <uri|image> "
 
104
                       "<signature uri|image>" );
 
105
 
 
106
/**
 
107
 * The "imgverify" command
 
108
 *
 
109
 * @v argc              Argument count
 
110
 * @v argv              Argument list
 
111
 * @ret rc              Return status code
 
112
 */
 
113
static int imgverify_exec ( int argc, char **argv ) {
 
114
        struct imgverify_options opts;
 
115
        const char *image_name_uri;
 
116
        const char *signature_name_uri;
 
117
        struct image *image;
 
118
        struct image *signature;
 
119
        int rc;
 
120
 
 
121
        /* Parse options */
 
122
        if ( ( rc = parse_options ( argc, argv, &imgverify_cmd, &opts ) ) != 0 )
 
123
                return rc;
 
124
 
 
125
        /* Parse image name/URI string */
 
126
        image_name_uri = argv[optind];
 
127
 
 
128
        /* Parse signature name/URI string */
 
129
        signature_name_uri = argv[ optind + 1 ];
 
130
 
 
131
        /* Acquire the image */
 
132
        if ( ( rc = imgacquire ( image_name_uri, &image ) ) != 0 )
 
133
                goto err_acquire_image;
 
134
 
 
135
        /* Acquire the signature image */
 
136
        if ( ( rc = imgacquire ( signature_name_uri, &signature ) ) != 0 )
 
137
                goto err_acquire_signature;
 
138
 
 
139
        /* Verify image */
 
140
        if ( ( rc = imgverify ( image, signature, opts.signer ) ) != 0 ) {
 
141
                printf ( "Could not verify: %s\n", strerror ( rc ) );
 
142
                goto err_verify;
 
143
        }
 
144
 
 
145
        /* Success */
 
146
        rc = 0;
 
147
 
 
148
 err_verify:
 
149
        /* Discard signature unless --keep was specified */
 
150
        if ( ! opts.keep )
 
151
                unregister_image ( signature );
 
152
 err_acquire_signature:
 
153
 err_acquire_image:
 
154
        return rc;
 
155
}
 
156
 
 
157
/** Image trust management commands */
 
158
struct command image_trust_commands[] __command = {
 
159
        {
 
160
                .name = "imgtrust",
 
161
                .exec = imgtrust_exec,
 
162
        },
 
163
        {
 
164
                .name = "imgverify",
 
165
                .exec = imgverify_exec,
 
166
        },
 
167
};
 
168
 
 
169
/* Drag in objects typically required for signature verification */
 
170
REQUIRE_OBJECT ( rsa );
 
171
REQUIRE_OBJECT ( md5 );
 
172
REQUIRE_OBJECT ( sha1 );
 
173
REQUIRE_OBJECT ( sha256 );