1 /*
2 * framework-pkcs15.c: PKCS#15 framework and related objects
3 *
4 * Copyright (C) 2002 Timo Teräs <timo.teras@iki.fi>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but 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 library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "sc-pkcs11.h"
27 #ifdef USE_PKCS15_INIT
28 #include "pkcs15init/pkcs15-init.h"
29
30 /*
31 * Deal with uninitialized cards
32 */
pkcs15init_bind(struct sc_pkcs11_card * p11card,struct sc_app_info * app_info)33 static CK_RV pkcs15init_bind(struct sc_pkcs11_card *p11card, struct sc_app_info *app_info)
34 {
35 struct sc_card *card;
36 struct sc_profile *profile;
37 int rc;
38
39 if (!p11card)
40 return CKR_TOKEN_NOT_RECOGNIZED;
41 card = p11card->card;
42 rc = sc_pkcs15init_bind(card, "pkcs15", NULL, NULL, &profile);
43 if (rc == 0)
44 p11card->fws_data[0] = profile;
45 return sc_to_cryptoki_error(rc, NULL);
46 }
47
pkcs15init_unbind(struct sc_pkcs11_card * p11card)48 static CK_RV pkcs15init_unbind(struct sc_pkcs11_card *p11card)
49 {
50 struct sc_profile *profile;
51
52 if (!p11card)
53 return CKR_TOKEN_NOT_RECOGNIZED;
54 profile = (struct sc_profile *) p11card->fws_data[0];
55 sc_pkcs15init_unbind(profile);
56 return CKR_OK;
57 }
58
59
60 static CK_RV
pkcs15init_create_tokens(struct sc_pkcs11_card * p11card,struct sc_app_info * app_info)61 pkcs15init_create_tokens(struct sc_pkcs11_card *p11card, struct sc_app_info *app_info)
62 {
63 struct sc_profile *profile;
64 struct sc_pkcs11_slot *slot;
65 CK_RV rc;
66
67 if (!p11card)
68 return CKR_TOKEN_NOT_RECOGNIZED;
69 profile = (struct sc_profile *) p11card->fws_data[0];
70
71 rc = slot_allocate(&slot, p11card);
72 if (rc == CKR_OK) {
73 CK_TOKEN_INFO_PTR pToken = &slot->token_info;
74 const char *string;
75
76 slot->slot_info.flags |= CKF_TOKEN_PRESENT;
77
78 strcpy_bp(pToken->model, "PKCS #15 SCard", 16);
79 sc_pkcs15init_get_manufacturer(profile, &string);
80 if (!string)
81 string = "Unknown";
82 strcpy_bp(pToken->manufacturerID, string, 32);
83 sc_pkcs15init_get_serial(profile, &string);
84 if (!string)
85 string = "";
86 strcpy_bp(pToken->serialNumber, string, 16);
87 pToken->ulMaxSessionCount = CK_EFFECTIVELY_INFINITE;
88 pToken->ulSessionCount = 0; /* FIXME */
89 pToken->ulMaxRwSessionCount = CK_EFFECTIVELY_INFINITE;
90 pToken->ulRwSessionCount = 0; /* FIXME */
91 pToken->ulTotalPublicMemory = CK_UNAVAILABLE_INFORMATION;
92 pToken->ulFreePublicMemory = CK_UNAVAILABLE_INFORMATION;
93 pToken->ulTotalPrivateMemory = CK_UNAVAILABLE_INFORMATION;
94 pToken->ulFreePrivateMemory = CK_UNAVAILABLE_INFORMATION;
95 pToken->hardwareVersion.major = 0;
96 pToken->hardwareVersion.minor = 0;
97 pToken->firmwareVersion.major = 0;
98 pToken->firmwareVersion.minor = 0;
99 }
100
101 return CKR_OK;
102 }
103
104 static CK_RV
pkcs15init_release_token(struct sc_pkcs11_card * p11card,void * ptr)105 pkcs15init_release_token(struct sc_pkcs11_card *p11card, void *ptr)
106 {
107 return CKR_OK;
108 }
109
110 static CK_RV
pkcs15init_login(struct sc_pkcs11_slot * slot,CK_USER_TYPE user,CK_CHAR_PTR pin,CK_ULONG pinLength)111 pkcs15init_login(struct sc_pkcs11_slot *slot,
112 CK_USER_TYPE user, CK_CHAR_PTR pin, CK_ULONG pinLength)
113 {
114 return CKR_CRYPTOKI_NOT_INITIALIZED;
115 }
116
117 static CK_RV
pkcs15init_logout(struct sc_pkcs11_slot * slot)118 pkcs15init_logout(struct sc_pkcs11_slot *slot)
119 {
120 return CKR_CRYPTOKI_NOT_INITIALIZED;
121 }
122
123 static CK_RV
pkcs15init_change_pin(struct sc_pkcs11_slot * slot,CK_CHAR_PTR oldPin,CK_ULONG oldPinLength,CK_CHAR_PTR newPin,CK_ULONG newPinLength)124 pkcs15init_change_pin(struct sc_pkcs11_slot *slot,
125 CK_CHAR_PTR oldPin, CK_ULONG oldPinLength,
126 CK_CHAR_PTR newPin, CK_ULONG newPinLength)
127 {
128 return CKR_CRYPTOKI_NOT_INITIALIZED;
129 }
130
131 static CK_RV
pkcs15init_initialize(struct sc_pkcs11_slot * pslot,void * ptr,CK_UTF8CHAR_PTR pPin,CK_ULONG ulPinLen,CK_UTF8CHAR_PTR pLabel)132 pkcs15init_initialize(struct sc_pkcs11_slot *pslot, void *ptr,
133 CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen,
134 CK_UTF8CHAR_PTR pLabel)
135 {
136 struct sc_pkcs11_card *p11card = pslot->p11card;
137 struct sc_profile *profile;
138 struct sc_pkcs15init_initargs args;
139 struct sc_pkcs11_slot *slot;
140 CK_RV rv;
141 int rc, id;
142
143 if (!p11card)
144 return CKR_TOKEN_NOT_RECOGNIZED;
145 profile = (struct sc_profile *) p11card->fws_data[0];
146 memset(&args, 0, sizeof(args));
147 args.so_pin = pPin;
148 args.so_pin_len = ulPinLen;
149 args.so_puk = pPin;
150 args.so_puk_len = ulPinLen;
151 args.label = (const char *) pLabel;
152 rc = sc_pkcs15init_add_app(p11card->card, profile, &args);
153 if (rc < 0)
154 return sc_to_cryptoki_error(rc, NULL);
155
156 /* Change the binding from the pkcs15init framework
157 * to the pkcs15 framework on the fly.
158 * First, try to bind pkcs15 framework */
159 if ((rv = framework_pkcs15.bind(p11card, NULL)) != CKR_OK) {
160 /* whoops, bad */
161 p11card->fws_data[0] = profile;
162 return rv;
163 }
164
165 /* Change the function vector to the standard pkcs15 ops */
166 p11card->framework = &framework_pkcs15;
167
168 /* Loop over all slots belonging to this card, and fix up
169 * the flags.
170 */
171 for (id = 0; slot_get_slot(id, &slot) == CKR_OK; id++) {
172 if (slot->p11card == p11card)
173 slot->token_info.flags |= CKF_TOKEN_INITIALIZED;
174 if (slot->p11card->card->caps & SC_CARD_CAP_RNG)
175 slot->token_info.flags |= CKF_RNG;
176 }
177
178 sc_pkcs15init_unbind(profile);
179 return CKR_OK;
180 }
181
182 struct sc_pkcs11_framework_ops framework_pkcs15init = {
183 pkcs15init_bind,
184 pkcs15init_unbind,
185 pkcs15init_create_tokens,
186 pkcs15init_release_token,
187 pkcs15init_login,
188 pkcs15init_logout,
189 pkcs15init_change_pin,
190 pkcs15init_initialize,
191 NULL, /* init_pin */
192 NULL, /* create_object */
193 NULL, /* gen_keypair */
194 NULL /* get_random */
195 };
196
197 #else /* ifdef USE_PKCS15_INIT */
198
199 struct sc_pkcs11_framework_ops framework_pkcs15init = {
200 NULL, /* bind */
201 NULL, /* unbind */
202 NULL, /* create_tokens */
203 NULL, /* release_tokens */
204 NULL, /* login */
205 NULL, /* logout */
206 NULL, /* change_pin */
207 NULL, /* inti_token */
208 NULL, /* init_pin */
209 NULL, /* create_object */
210 NULL, /* gen_keypair */
211 NULL /* get_random */
212 };
213
214 #endif
215