This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / crypto / signature / ksign.c
1 /* ksign.c: signature checker
2  *
3  * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #include <linux/kernel.h>
13 #include <asm/errno.h>
14 #include "local.h"
15
16 #if 0
17 #define _debug(FMT, ...) printk(KERN_DEBUG FMT, ##__VA_ARGS__)
18 #else
19 #define _debug(FMT, ...) do { ; } while (0)
20 #endif
21
22 /*****************************************************************************/
23 /*
24  * check the signature which is contained in SIG.
25  */
26 static int ksign_signature_check(const struct ksign_signature *sig,
27                                  struct crypto_tfm *sha1_tfm)
28 {
29         struct ksign_public_key *pk;
30         uint8_t sha1[SHA1_DIGEST_SIZE];
31         MPI result = NULL;
32         int rc = 0, i;
33
34         pk = ksign_get_public_key(sig->keyid);
35         if (!pk) {
36                 printk("ksign: module signed with unknown public key\n");
37                 printk("- signature keyid: %08x%08x ver=%u\n",
38                        sig->keyid[0], sig->keyid[1], sig->version);
39                 return -EPERM;
40         }
41
42         if (pk->timestamp > sig->timestamp)
43                 printk("ksign:"
44                        " public key is %lu seconds newer than the signature\n",
45                        pk->timestamp - sig->timestamp);
46
47         /* complete the digest */
48         if (sig->version >= 4)
49                 SHA1_putc(sha1_tfm, sig->version);
50         SHA1_putc(sha1_tfm, sig->sig_class);
51
52         if (sig->version < 4) {
53                 u32 a = sig->timestamp;
54                 SHA1_putc(sha1_tfm, (a >> 24) & 0xff);
55                 SHA1_putc(sha1_tfm, (a >> 16) & 0xff);
56                 SHA1_putc(sha1_tfm, (a >>  8) & 0xff);
57                 SHA1_putc(sha1_tfm, (a >>  0) & 0xff);
58         }
59         else {
60                 uint8_t buf[6];
61                 size_t n;
62                 SHA1_putc(sha1_tfm, PUBKEY_ALGO_DSA);
63                 SHA1_putc(sha1_tfm, DIGEST_ALGO_SHA1);
64                 if (sig->hashed_data) {
65                         n = (sig->hashed_data[0] << 8) | sig->hashed_data[1];
66                         SHA1_write(sha1_tfm, sig->hashed_data, n + 2);
67                         n += 6;
68                 }
69                 else {
70                         n = 6;
71                 }
72
73                 /* add some magic */
74                 buf[0] = sig->version;
75                 buf[1] = 0xff;
76                 buf[2] = n >> 24;
77                 buf[3] = n >> 16;
78                 buf[4] = n >>  8;
79                 buf[5] = n;
80                 SHA1_write(sha1_tfm, buf, 6);
81         }
82
83         crypto_digest_final(sha1_tfm, sha1);
84         crypto_free_tfm(sha1_tfm);
85
86
87
88
89
90
91         rc = -ENOMEM;
92         result = mpi_alloc((SHA1_DIGEST_SIZE + BYTES_PER_MPI_LIMB - 1) / BYTES_PER_MPI_LIMB);
93         if (!result)
94                 goto cleanup;
95
96         rc = mpi_set_buffer(result, sha1, SHA1_DIGEST_SIZE, 0);
97         if (rc < 0)
98                 goto cleanup;
99
100         rc = DSA_verify(result, sig->data, pk->pkey);
101
102  cleanup:
103         mpi_free(result);
104         ksign_put_public_key(pk);
105
106         return rc;
107 } /* end ksign_signature_check() */
108
109 /*****************************************************************************/
110 /*
111  * examine the signatures that are parsed out of the signature data - we keep
112  * the first one that's appropriate and ignore the rest
113  * - return 0 if signature of interest (sig not freed by caller)
114  * - return 1 if no interest (caller frees)
115  */
116 static int ksign_grab_signature(struct ksign_signature *sig, void *fnxdata)
117 {
118         struct ksign_signature **_sig = fnxdata;
119
120         if (sig->sig_class != 0x00) {
121                 _debug("ksign: standalone signature of class 0x%02x\n",
122                        sig->sig_class);
123                 return 1;
124         }
125
126         if (*_sig)
127                 return 1;
128
129         *_sig = sig;
130         return 0;
131 } /* end ksign_grab_signature() */
132
133 /*****************************************************************************/
134 /*
135  * verify the signature of some data with one of the kernel's known public keys
136  * - the SHA1 context should be currently open with the signed data digested
137  *   into it so that more data can be appended
138  * - the SHA1 context is finalised and freed before returning
139  */
140 int ksign_verify_signature(const char *sigdata, unsigned sig_size,
141                            struct crypto_tfm *sha1)
142 {
143         struct ksign_signature *sig = NULL;
144         int retval;
145
146         /* parse the signature data to get the actual signature */
147         retval = ksign_parse_packets(sigdata, sig_size,
148                                      &ksign_grab_signature, NULL, NULL,
149                                      &sig);
150         if (retval < 0)
151                 goto cleanup;
152
153         if (!sig) {
154                 printk("Couldn't find valid DSA signature in module\n");
155                 return -ENOENT;
156         }
157
158         _debug("signature keyid: %08x%08x ver=%u\n",
159                sig->keyid[0], sig->keyid[1], sig->version);
160
161         /* check the data SHA1 transformation against the public key */
162         retval = ksign_signature_check(sig, sha1);
163         if (retval == 0) {
164                 _debug("ksign: Signature check succeeded\n");
165         }
166         else if (retval != -ENOMEM) {
167                 _debug("ksign: Signature check failed\n");
168                 retval = -EPERM;
169         }
170         else {
171                 _debug("ksign: Signature check ENOMEM\n");
172         }
173
174  cleanup:
175         if (sig)
176                 ksign_free_signature(sig);
177
178         return retval;
179 } /* end ksign_verify_signature() */