fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / crypto / signature / ksign-keyring.c
1 /* ksign-keyring.c: public key cache
2  *
3  * Copyright (C) 2001 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This file is derived from part of GnuPG.
7  *
8  * GnuPG is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * GnuPG is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21  */
22
23 #include <linux/rwsem.h>
24 #include "local.h"
25
26 static LIST_HEAD(keyring);
27 static DECLARE_RWSEM(keyring_sem);
28
29 /*
30  * handle a public key element parsed from the keyring blob
31  */
32 static int add_keyblock_key(struct ksign_public_key *pk, void *data)
33 {
34         printk("- Added public key %X%X\n", pk->keyid[0], pk->keyid[1]);
35
36         if (pk->expiredate && pk->expiredate < xtime.tv_sec)
37                 printk("  - public key has expired\n");
38
39         if (pk->timestamp > xtime.tv_sec )
40                 printk("  - key was been created %lu seconds in future\n",
41                        pk->timestamp - xtime.tv_sec);
42
43         atomic_inc(&pk->count);
44
45         down_write(&keyring_sem);
46         list_add_tail(&pk->link, &keyring);
47         up_write(&keyring_sem);
48
49         return 0;
50 }
51
52 /*
53  * handle a user ID element parsed from the keyring blob
54  */
55 static int add_keyblock_uid(struct ksign_user_id *uid, void *data)
56 {
57         printk("- User ID: %s\n", uid->name);
58         return 1;
59 }
60
61 /*
62  * add the keys from a ASN.1 encoded blob into the keyring
63  */
64 int ksign_load_keyring_from_buffer(const void *buffer, size_t size)
65 {
66     printk("Loading keyring\n");
67
68     return ksign_parse_packets((const uint8_t *) buffer,
69                                size,
70                                NULL,
71                                add_keyblock_key,
72                                add_keyblock_uid,
73                                NULL);
74 }
75
76 /*
77  * find a public key by ID
78  */
79 struct ksign_public_key *ksign_get_public_key(const uint32_t *keyid)
80 {
81         struct ksign_public_key *pk;
82
83         down_read(&keyring_sem);
84
85         list_for_each_entry(pk, &keyring, link) {
86                 if (memcmp(pk->keyid, keyid, sizeof(pk->keyid)) == 0) {
87                         atomic_inc(&pk->count);
88                         goto found;
89                 }
90         }
91
92         pk = NULL;
93
94 found:
95         up_read(&keyring_sem);
96         return pk;
97 }
98
99 /*
100  * clear the public-key keyring
101  */
102 void ksign_clear_keyring(void)
103 {
104         struct ksign_public_key *pk;
105
106         down_write(&keyring_sem);
107
108         while (!list_empty(&keyring)) {
109                 pk = list_entry(keyring.next, struct ksign_public_key, link);
110                 list_del(&pk->link);
111
112                 ksign_put_public_key(pk);
113         }
114
115         up_write(&keyring_sem);
116 }