Coverage Report

Created: 2026-01-09 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/src/pkcs11/framework-pkcs15init.c
Line
Count
Source
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
 */
33
static CK_RV pkcs15init_bind(struct sc_pkcs11_card *p11card, struct sc_app_info *app_info)
34
0
{
35
0
  struct sc_card  *card;
36
0
  struct sc_profile *profile;
37
0
  int   rc;
38
39
0
  if (!p11card)
40
0
    return CKR_TOKEN_NOT_RECOGNIZED;
41
0
  card = p11card->card;
42
0
  rc = sc_pkcs15init_bind(card, "pkcs15", NULL, NULL, &profile);
43
0
  if (rc == 0)
44
0
    p11card->fws_data[0] = profile;
45
0
  return sc_to_cryptoki_error(rc, NULL);
46
0
}
47
48
static CK_RV pkcs15init_unbind(struct sc_pkcs11_card *p11card)
49
0
{
50
0
  struct sc_profile *profile;
51
52
0
  if (!p11card)
53
0
    return CKR_TOKEN_NOT_RECOGNIZED;
54
0
  profile = (struct sc_profile *) p11card->fws_data[0];
55
0
  sc_pkcs15init_unbind(profile);
56
0
  return CKR_OK;
57
0
}
58
59
60
static CK_RV
61
pkcs15init_create_tokens(struct sc_pkcs11_card *p11card, struct sc_app_info *app_info)
62
0
{
63
0
  struct sc_profile *profile;
64
0
  struct sc_pkcs11_slot *slot;
65
0
  CK_RV rc;
66
67
0
  if (!p11card)
68
0
    return CKR_TOKEN_NOT_RECOGNIZED;
69
0
  profile = (struct sc_profile *) p11card->fws_data[0];
70
71
0
  rc = slot_allocate(&slot, p11card);
72
0
  if (rc == CKR_OK) {
73
0
    CK_TOKEN_INFO_PTR pToken = &slot->token_info;
74
0
    const char  *string;
75
76
0
    slot->slot_info.flags |= CKF_TOKEN_PRESENT;
77
78
0
    strcpy_bp(pToken->model, "PKCS #15 SCard", 16);
79
0
    sc_pkcs15init_get_manufacturer(profile, &string);
80
0
    if (!string)
81
0
      string = "Unknown";
82
0
    strcpy_bp(pToken->manufacturerID, string, 32);
83
0
    sc_pkcs15init_get_serial(profile, &string);
84
0
    if (!string)
85
0
      string = "";
86
0
    strcpy_bp(pToken->serialNumber, string, 16);
87
0
    pToken->ulMaxSessionCount = CK_EFFECTIVELY_INFINITE;
88
0
    pToken->ulSessionCount = 0; /* FIXME */
89
0
    pToken->ulMaxRwSessionCount = CK_EFFECTIVELY_INFINITE;
90
0
    pToken->ulRwSessionCount = 0; /* FIXME */
91
0
    pToken->ulTotalPublicMemory = CK_UNAVAILABLE_INFORMATION;
92
0
    pToken->ulFreePublicMemory = CK_UNAVAILABLE_INFORMATION;
93
0
    pToken->ulTotalPrivateMemory = CK_UNAVAILABLE_INFORMATION;
94
0
    pToken->ulFreePrivateMemory = CK_UNAVAILABLE_INFORMATION;
95
0
    pToken->hardwareVersion.major = 0;
96
0
    pToken->hardwareVersion.minor = 0;
97
0
    pToken->firmwareVersion.major = 0;
98
0
    pToken->firmwareVersion.minor = 0;
99
0
  }
100
101
0
  return CKR_OK;
102
0
}
103
104
static CK_RV
105
pkcs15init_release_token(struct sc_pkcs11_card *p11card, void *ptr)
106
0
{
107
0
  return CKR_OK;
108
0
}
109
110
static CK_RV
111
pkcs15init_login(struct sc_pkcs11_slot *slot,
112
    CK_USER_TYPE user, CK_CHAR_PTR pin, CK_ULONG pinLength)
113
0
{
114
0
  return CKR_CRYPTOKI_NOT_INITIALIZED;
115
0
}
116
117
static CK_RV
118
pkcs15init_logout(struct sc_pkcs11_slot *slot)
119
0
{
120
0
  return CKR_CRYPTOKI_NOT_INITIALIZED;
121
0
}
122
123
static CK_RV
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
0
{
128
0
  return CKR_CRYPTOKI_NOT_INITIALIZED;
129
0
}
130
131
static CK_RV
132
pkcs15init_initialize(struct sc_pkcs11_slot *pslot, void *ptr,
133
    CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen,
134
    CK_UTF8CHAR_PTR pLabel)
135
0
{
136
0
  struct sc_pkcs11_card *p11card = pslot->p11card;
137
0
  struct sc_profile *profile;
138
0
  struct sc_pkcs15init_initargs args;
139
0
  struct sc_pkcs11_slot *slot;
140
0
    CK_RV rv;
141
0
  int   rc, id;
142
143
0
  if (!p11card)
144
0
    return CKR_TOKEN_NOT_RECOGNIZED;
145
0
  profile = (struct sc_profile *) p11card->fws_data[0];
146
0
  memset(&args, 0, sizeof(args));
147
0
  args.so_pin = pPin;
148
0
  args.so_pin_len = ulPinLen;
149
0
  args.so_puk = pPin;
150
0
  args.so_puk_len = ulPinLen;
151
0
  args.label = (const char *) pLabel;
152
0
  rc = sc_pkcs15init_add_app(p11card->card, profile, &args);
153
0
  if (rc < 0)
154
0
    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
0
  if ((rv = framework_pkcs15.bind(p11card, NULL)) != CKR_OK) {
160
    /* whoops, bad */
161
0
    p11card->fws_data[0] = profile;
162
0
    return rv;
163
0
  }
164
165
  /* Change the function vector to the standard pkcs15 ops */
166
0
  p11card->framework = &framework_pkcs15;
167
168
  /* Loop over all slots belonging to this card, and fix up
169
   * the flags.
170
   */
171
0
  for (id = 0; slot_get_slot(id, &slot) == CKR_OK; id++) {
172
0
    if (slot->p11card == p11card)
173
0
      slot->token_info.flags |= CKF_TOKEN_INITIALIZED;
174
0
    if (slot->p11card->card->caps & SC_CARD_CAP_RNG)
175
0
      slot->token_info.flags |= CKF_RNG;
176
0
  }
177
178
0
  sc_pkcs15init_unbind(profile);
179
0
  return CKR_OK;
180
0
}
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, /* init_token */
208
  NULL, /* init_pin */
209
  NULL, /* create_object */
210
  NULL, /* gen_keypair */
211
  NULL  /* get_random */
212
};
213
214
#endif