This commit was manufactured by cvs2svn to create branch 'vserver'.
[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 static int add_keyblock_key(struct ksign_public_key *pk, void *data)
30 {
31         printk("- Added public key %X%X\n", pk->keyid[0], pk->keyid[1]);
32
33         if (pk->expiredate && pk->expiredate < xtime.tv_sec)
34                 printk("  - public key has expired\n");
35
36         if (pk->timestamp > xtime.tv_sec )
37                 printk("  - key was been created %lu seconds in future\n",
38                        pk->timestamp - xtime.tv_sec);
39
40         atomic_inc(&pk->count);
41
42         down_write(&keyring_sem);
43         list_add_tail(&pk->link, &keyring);
44         up_write(&keyring_sem);
45
46         return 0;
47 }
48
49 static int add_keyblock_uid(struct ksign_user_id *uid, void *data)
50 {
51         printk("- User ID: %s\n", uid->name);
52         return 1;
53 }
54
55 /*****************************************************************************/
56 /*
57  *
58  */
59 int ksign_load_keyring_from_buffer(const void *buffer, size_t size)
60 {
61     printk("Loading keyring\n");
62
63     return ksign_parse_packets((const uint8_t *) buffer,
64                                size,
65                                NULL,
66                                add_keyblock_key,
67                                add_keyblock_uid,
68                                NULL);
69 } /* end ksign_load_keyring_from_buffer() */
70
71 /*****************************************************************************/
72 /*
73  *
74  */
75 struct ksign_public_key *ksign_get_public_key(const uint32_t *keyid)
76 {
77         struct ksign_public_key *pk;
78
79         down_read(&keyring_sem);
80
81         list_for_each_entry(pk, &keyring, link) {
82                 if (memcmp(pk->keyid, keyid, sizeof(pk->keyid)) == 0) {
83                         atomic_inc(&pk->count);
84                         goto found;
85                 }
86         }
87
88  found:
89         up_read(&keyring_sem);
90
91         return pk;
92 } /* end ksign_get_public_key() */
93
94 /*****************************************************************************/
95 /*
96  * clear the public key keyring
97  */
98 void ksign_clear_keyring(void)
99 {
100         struct ksign_public_key *pk;
101
102         down_write(&keyring_sem);
103
104         while (!list_empty(&keyring)) {
105                 pk = list_entry(keyring.next, struct ksign_public_key, link);
106                 list_del(&pk->link);
107
108                 ksign_put_public_key(pk);
109         }
110
111         up_write(&keyring_sem);
112 } /* end ksign_clear_keyring() */