1 /*
2  * gnome-keyring
3  *
4  * Copyright (C) 2008 Stefan Walter
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "config.h"
22 
23 #include "gkm-attributes.h"
24 #include "gkm-null-mechanism.h"
25 #include "gkm-null-key.h"
26 #include "gkm-session.h"
27 #include "gkm-util.h"
28 
29 #include "pkcs11/pkcs11.h"
30 #include "pkcs11/pkcs11i.h"
31 
32 struct _GkmNullKey {
33 	GkmSecretKey parent;
34 };
35 
36 G_DEFINE_TYPE (GkmNullKey, gkm_null_key, GKM_TYPE_SECRET_KEY);
37 
38 /* -----------------------------------------------------------------------------
39  * INTERNAL
40  */
41 
42 static GkmObject*
factory_create_null_key(GkmSession * session,GkmTransaction * transaction,CK_ATTRIBUTE_PTR attrs,CK_ULONG n_attrs)43 factory_create_null_key (GkmSession *session, GkmTransaction *transaction,
44                          CK_ATTRIBUTE_PTR attrs, CK_ULONG n_attrs)
45 {
46 	GkmNullKey *key;
47 	GkmManager *manager;
48 
49 	manager = gkm_manager_for_template (attrs, n_attrs, session);
50 	key = g_object_new (GKM_TYPE_NULL_KEY,
51 	                    "module", gkm_session_get_module (session),
52 	                    "manager", manager,
53 	                    NULL);
54 
55 	gkm_session_complete_object_creation (session, transaction, GKM_OBJECT (key),
56 	                                      TRUE, attrs, n_attrs);
57 	return GKM_OBJECT (key);
58 }
59 
60 /* -----------------------------------------------------------------------------
61  * OBJECT
62  */
63 
64 static CK_RV
gkm_null_key_real_get_attribute(GkmObject * base,GkmSession * session,CK_ATTRIBUTE * attr)65 gkm_null_key_real_get_attribute (GkmObject *base, GkmSession *session, CK_ATTRIBUTE *attr)
66 {
67 	switch (attr->type)
68 	{
69 	case CKA_KEY_TYPE:
70 		return gkm_attribute_set_ulong (attr, CKK_G_NULL);
71 
72 	case CKA_UNWRAP:
73 	case CKA_WRAP:
74 		return gkm_attribute_set_bool (attr, CK_TRUE);
75 
76 	case CKA_VALUE:
77 		return gkm_attribute_set_empty (attr);
78 
79 	case CKA_VALUE_LEN:
80 		return gkm_attribute_set_ulong (attr, 0);
81 
82 	case CKA_CHECK_VALUE:
83 		return gkm_attribute_set_data (attr, "\0\0\0", 3);
84 
85 	case CKA_ALLOWED_MECHANISMS:
86 		return gkm_attribute_set_data (attr, (CK_VOID_PTR)GKM_NULL_MECHANISMS,
87 		                               sizeof (GKM_NULL_MECHANISMS));
88 	};
89 
90 	return GKM_OBJECT_CLASS (gkm_null_key_parent_class)->get_attribute (base, session, attr);
91 }
92 
93 static void
gkm_null_key_init(GkmNullKey * self)94 gkm_null_key_init (GkmNullKey *self)
95 {
96 
97 }
98 
99 static void
gkm_null_key_class_init(GkmNullKeyClass * klass)100 gkm_null_key_class_init (GkmNullKeyClass *klass)
101 {
102 	GkmObjectClass *gkm_class = GKM_OBJECT_CLASS (klass);
103 
104 	gkm_null_key_parent_class = g_type_class_peek_parent (klass);
105 	gkm_class->get_attribute = gkm_null_key_real_get_attribute;
106 }
107 
108 /* -----------------------------------------------------------------------------
109  * PUBLIC
110  */
111 
112 GkmFactory*
gkm_null_key_get_factory(void)113 gkm_null_key_get_factory (void)
114 {
115 	static CK_OBJECT_CLASS klass = CKO_SECRET_KEY;
116 	static CK_KEY_TYPE type = CKK_G_NULL;
117 
118 	static CK_ATTRIBUTE attributes[] = {
119 		{ CKA_CLASS, &klass, sizeof (klass) },
120 		{ CKA_KEY_TYPE, &type, sizeof (type) }
121 	};
122 
123 	static GkmFactory factory = {
124 		attributes,
125 		G_N_ELEMENTS (attributes),
126 		factory_create_null_key
127 	};
128 
129 	return &factory;
130 }
131