1 /*
2  * COPYRIGHT (c) International Business Machines Corp. 2016-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 <unistd.h>
14 #include <string.h>
15 #include <memory.h>
16 
17 #include "pkcs11types.h"
18 #include "regress.h"
19 #include "common.c"
20 
21 
22 /* API Routines exercised:
23  * C_CreateObject
24  * C_GetObjectSize
25  * C_DestroyObject
26  *
27  * 2 TestCases
28  * Setup: Create a key object.
29  * Get the object size
30  * Destroy the object
31  * Get the object size
32  */
do_GetObjectSize(void)33 CK_RV do_GetObjectSize(void)
34 {
35     CK_FLAGS flags;
36     CK_SESSION_HANDLE session;
37     CK_RV rc = 0;
38     CK_BYTE user_pin[PKCS11_MAX_PIN_LEN];
39     CK_ULONG user_pin_len;
40 
41     CK_OBJECT_HANDLE keyobj = CK_INVALID_HANDLE;
42 
43     CK_BBOOL false = FALSE;
44     CK_KEY_TYPE aes_type = CKK_AES;
45     CK_OBJECT_CLASS key_class = CKO_SECRET_KEY;
46     CK_CHAR aes_value[] = "This is a fake aes key.";
47     CK_ATTRIBUTE aes_tmpl[] = {
48         {CKA_CLASS, &key_class, sizeof(key_class)},
49         {CKA_KEY_TYPE, &aes_type, sizeof(aes_type)},
50         {CKA_VALUE, &aes_value, sizeof(aes_value)},
51         {CKA_SENSITIVE, &false, sizeof(false)}
52     };
53 
54     CK_ULONG obj_size = 0;
55 
56     // Do some setup and login to the token
57     testcase_begin("starting...");
58     testcase_rw_session();
59     testcase_user_login();
60 
61     // Create an AES Key Object.
62     rc = funcs->C_CreateObject(session, aes_tmpl, 4, &keyobj);
63     if (rc != CKR_OK) {
64         testcase_error("C_CreateObject() rc = %s", p11_get_ckr(rc));
65         goto testcase_cleanup;
66     }
67 
68     /* now, get the size of the object */
69     rc = funcs->C_GetObjectSize(session, keyobj, &obj_size);
70     if (rc != CKR_OK) {
71         testcase_fail("C_GetObjectSize() rc = %s", p11_get_ckr(rc));
72         return rc;
73     }
74 
75     printf("\nSize of object = %lu\n", obj_size);
76 
77         /** Destroy the object */
78     rc = funcs->C_DestroyObject(session, keyobj);
79     if (rc != CKR_OK) {
80         testcase_fail("C_DestroyObject() rc = %s", p11_get_ckr(rc));
81         return rc;
82     }
83 
84     /* now, get the size of a non-existent object */
85     rc = funcs->C_GetObjectSize(session, keyobj, &obj_size);
86     if (rc != CKR_OBJECT_HANDLE_INVALID) {
87         testcase_fail("C_GetObjectSize () rc = %s (expected "
88                       "CKR_OBJECT_HANDLE_INVALID)", p11_get_ckr(rc));
89         return rc;
90     }
91 
92     printf("C_GetObjectSize test passed\n");
93 
94 testcase_cleanup:
95     funcs->C_DestroyObject(session, keyobj);
96 
97     testcase_user_logout();
98     rc = funcs->C_CloseSession(session);
99     if (rc != CKR_OK) {
100         testcase_error("C_CloseSessions rc=%s", p11_get_ckr(rc));
101     }
102 
103     return rc;
104 }
105 
main(int argc,char ** argv)106 int main(int argc, char **argv)
107 {
108     int rc;
109     CK_C_INITIALIZE_ARGS cinit_args;
110     CK_RV rv = 0;
111 
112     rc = do_ParseArgs(argc, argv);
113     if (rc != 1)
114         return rc;
115 
116     printf("Using slot #%lu...\n\n", SLOT_ID);
117 
118     rc = do_GetFunctionList();
119     if (!rc) {
120         testcase_error("do_getFunctionList(), rc=%s", p11_get_ckr(rc));
121         return rc;
122     }
123 
124     memset(&cinit_args, 0x0, sizeof(cinit_args));
125     cinit_args.flags = CKF_OS_LOCKING_OK;
126 
127     funcs->C_Initialize(&cinit_args);
128 
129     {
130         CK_SESSION_HANDLE hsess = 0;
131 
132         rc = funcs->C_GetFunctionStatus(hsess);
133         if (rc != CKR_FUNCTION_NOT_PARALLEL)
134             return rc;
135 
136         rc = funcs->C_CancelFunction(hsess);
137         if (rc != CKR_FUNCTION_NOT_PARALLEL)
138             return rc;
139     }
140 
141     rc = do_GetObjectSize();
142 
143     funcs->C_Finalize(NULL);
144 
145     /* make sure we return non-zero if rv is non-zero */
146     return ((rv == 0) || (rv % 256) ? (int)rv : -1);
147 }
148