1 /*
2  * This program is provided under the terms of the Common Public License,
3  * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
4  * software constitutes recipient's acceptance of CPL-1.0 terms which can be
5  * found in the file LICENSE file or at
6  * https://opensource.org/licenses/cpl1.0.php
7  */
8 
9 /* (C) COPYRIGHT Google Inc. 2013 */
10 
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdio.h>
14 
15 #include "slotmgr.h"
16 #include "log.h"
17 #include "pkcsslotd.h"
18 
PopulateCKInfo(CK_INFO_PTR_64 ckinf)19 void PopulateCKInfo(CK_INFO_PTR_64 ckinf)
20 {
21     CK_VERSION_PTR ckver;
22     char *package_version_tmp;
23     char *tok_str;
24     CK_BYTE lib_major;
25     CK_BYTE lib_minor;
26 
27     ckver = &(ckinf->cryptokiVersion);
28 
29     ckver->major = CRYPTOKI_API_MAJOR_V;
30     ckver->minor = CRYPTOKI_API_MINOR_V;
31 
32     memset(ckinf->manufacturerID, ' ', sizeof(ckinf->manufacturerID));
33     memset(ckinf->libraryDescription, ' ', sizeof(ckinf->libraryDescription));
34 
35     memcpy(ckinf->manufacturerID, MFG, strlen(MFG));
36     memcpy(ckinf->libraryDescription, LIB, strlen(LIB));
37 
38     ckver = &(ckinf->libraryVersion);
39 
40     ckver->major = LIB_MAJOR_V;
41     ckver->minor = LIB_MINOR_V;
42 
43 #ifdef PACKAGE_VERSION
44     package_version_tmp = malloc(strlen(PACKAGE_VERSION) + 1);
45     if (package_version_tmp) {
46         strcpy(package_version_tmp, PACKAGE_VERSION);
47         tok_str = strtok(package_version_tmp, ".");
48         if (tok_str) {
49             lib_major = (CK_BYTE) atoi(tok_str);
50             tok_str = strtok(NULL, ".");
51             if (tok_str) {
52                 lib_minor = (CK_BYTE) atoi(tok_str);
53                 ckver->major = lib_major;
54                 ckver->minor = lib_minor;
55             }
56         }
57         free(package_version_tmp);
58     }
59 #endif
60 
61 }
62 
PopulateSlotInfo(Slot_Info_t_64 * slot_info,unsigned int * processed)63 void PopulateSlotInfo(Slot_Info_t_64 *slot_info, unsigned int *processed)
64 {
65     CK_SLOT_ID id;
66     unsigned int slot_count = 0;
67 
68     /*
69      *  populate the Slot entries...
70      */
71 
72     for (id = 0; id < NUMBER_SLOTS_MANAGED; id++) {
73 
74         if (sinfo[id].present == FALSE) {
75             /* skip empty slots and just note the slot number */
76             slot_info[id].slot_number = id;
77         } else {
78             slot_info[id].slot_number = sinfo[id].slot_number;
79             slot_info[id].present = sinfo[id].present;
80             slot_info[id].pk_slot.flags = sinfo[id].pk_slot.flags;
81 
82             memcpy(slot_info[id].dll_location,
83                    sinfo[id].dll_location, strlen(sinfo[id].dll_location));
84 
85             memcpy(slot_info[id].confname, sinfo[id].confname,
86                    strlen(sinfo[id].confname));
87 
88             memcpy(slot_info[id].tokname, sinfo[id].tokname,
89                    strlen(sinfo[id].tokname));
90 
91             memcpy(slot_info[id].pk_slot.slotDescription,
92                    sinfo[id].pk_slot.slotDescription,
93                    sizeof(sinfo[id].pk_slot.slotDescription));
94 
95             memcpy(slot_info[id].pk_slot.manufacturerID,
96                    sinfo[id].pk_slot.manufacturerID,
97                    sizeof(sinfo[id].pk_slot.manufacturerID));
98 
99             memcpy(&slot_info[id].pk_slot.hardwareVersion,
100                    &sinfo[id].pk_slot.hardwareVersion,
101                    sizeof(sinfo[id].pk_slot.hardwareVersion));
102 
103             memcpy(&slot_info[id].pk_slot.firmwareVersion,
104                    &sinfo[id].pk_slot.firmwareVersion,
105                    sizeof(sinfo[id].pk_slot.firmwareVersion));
106 
107             slot_count++;
108         }
109     }
110     *processed = slot_count;
111 }
112