Debian Codegraph @

testcases/login/digest_init.c

raw
1 /*
2 * COPYRIGHT (c) International Business Machines Corp. 2005-2017
3 *
4 * This program is provided under the terms of the Common Public License,
5 * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
6 * software constitutes recipient's acceptance of CPL-1.0 terms which can be
7 * found in the file LICENSE file or at
8 * https://opensource.org/licenses/cpl1.0.php
9 */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <memory.h>
15
16 #include <dlfcn.h>
17
18 #include "pkcs11types.h"
19
20 #include "regress.h"
21 #include "common.c"
22
23 int do_digestInit(CK_FUNCTION_LIST * funcs, CK_SLOT_ID slot_id,
24 CK_USER_TYPE userType, char *pass)
25 {
26 CK_RV rc;
27 CK_SESSION_HANDLE session;
28 CK_FLAGS flags = CKF_SERIAL_SESSION;
29 CK_MECHANISM mech;
30
31 mech.mechanism = CKM_SHA_1;
32 mech.pParameter = NULL;
33 mech.ulParameterLen = 0;
34
35 flags |= CKF_RW_SESSION;
36
37 rc = funcs->C_OpenSession(slot_id, flags, NULL, NULL, &session);
38 if (rc != CKR_OK) {
39 show_error("C_OpenSession", rc);
40 return rc;
41 }
42
43 rc = funcs->C_Login(session, userType, (CK_CHAR_PTR) pass, strlen(pass));
44 if (rc != CKR_OK) {
45 show_error("C_Login", rc);
46 return rc;
47 }
48
49 printf("Logged in successfully, calling C_DigestInit...\n");
50
51 rc = funcs->C_DigestInit(session, &mech);
52 if (rc != CKR_OK) {
53 show_error("C_DigestInit", rc);
54 funcs->C_Logout(session);
55 funcs->C_CloseSession(session);
56 return rc;
57 }
58
59 printf("Success.\n");
60
61 rc = funcs->C_Logout(session);
62 if (rc != CKR_OK) {
63 show_error("C_Logout", rc);
64 return rc;
65 }
66
67 printf("Logged out.\n");
68
69 rc = funcs->C_CloseSession(session);
70 if (rc != CKR_OK) {
71 show_error("C_CloseSession", rc);
72 return rc;
73 }
74
75 return rc;
76 }
77
78 void digest_init_usage(char *argv0)
79 {
80 printf("usage: %s [-slot <num>] [-h] [-user|-so] -pass pass\n\n", argv0);
81 printf("By default, Slot #%lu is used, as user\n\n", SLOT_ID);
82 exit(-1);
83 }
84
85 //
86 //
87 int main(int argc, char **argv)
88 {
89 CK_C_INITIALIZE_ARGS cinit_args;
90 CK_USER_TYPE userType = CKU_USER;
91 CK_RV rc = 0;
92 CK_SLOT_ID slot_id = 0;
93 char *pass = NULL;
94 int i;
95
96 for (i = 1; i < argc; i++) {
97 if (strcmp(argv[i], "-pass") == 0) {
98 ++i;
99 if (i >= argc) {
100 printf("PIN argument missing\n");
101 return -1;
102 }
103 pass = argv[i];
104 } else if (strcmp(argv[i], "-slot") == 0) {
105 ++i;
106 if (i >= argc) {
107 printf("Slot number missing\n");
108 return -1;
109 }
110 slot_id = atoi(argv[i]);
111 } else if (strcmp(argv[i], "-so") == 0) {
112 userType = CKU_SO;
113 } else if (strcmp(argv[i], "-user") == 0) {
114 continue;
115 } else {
116 digest_init_usage(argv[0]);
117 }
118 }
119
120 if (!pass)
121 digest_init_usage(argv[0]);
122
123 if (slot_id != SLOT_ID)
124 printf("Using user specified slot %lu.\n", slot_id);
125
126 rc = do_GetFunctionList();
127 if (funcs == NULL)
128 return -1;
129
130 memset(&cinit_args, 0, sizeof(cinit_args));
131 cinit_args.flags = CKF_OS_LOCKING_OK;
132
133 rc = funcs->C_Initialize(&cinit_args);
134 if (rc != CKR_OK) {
135 show_error("C_Initialize", rc);
136 return -1;
137 }
138
139 rc = do_digestInit(funcs, slot_id, userType, pass);
140
141 funcs->C_Finalize(NULL);
142
143 return rc;
144 }