1*12720SWyllys.Ingersoll@Sun.COM /*
2*12720SWyllys.Ingersoll@Sun.COM * CDDL HEADER START
3*12720SWyllys.Ingersoll@Sun.COM *
4*12720SWyllys.Ingersoll@Sun.COM * The contents of this file are subject to the terms of the
5*12720SWyllys.Ingersoll@Sun.COM * Common Development and Distribution License (the "License").
6*12720SWyllys.Ingersoll@Sun.COM * You may not use this file except in compliance with the License.
7*12720SWyllys.Ingersoll@Sun.COM *
8*12720SWyllys.Ingersoll@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*12720SWyllys.Ingersoll@Sun.COM * or http://www.opensolaris.org/os/licensing.
10*12720SWyllys.Ingersoll@Sun.COM * See the License for the specific language governing permissions
11*12720SWyllys.Ingersoll@Sun.COM * and limitations under the License.
12*12720SWyllys.Ingersoll@Sun.COM *
13*12720SWyllys.Ingersoll@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
14*12720SWyllys.Ingersoll@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*12720SWyllys.Ingersoll@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
16*12720SWyllys.Ingersoll@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
17*12720SWyllys.Ingersoll@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
18*12720SWyllys.Ingersoll@Sun.COM *
19*12720SWyllys.Ingersoll@Sun.COM * CDDL HEADER END
20*12720SWyllys.Ingersoll@Sun.COM */
21*12720SWyllys.Ingersoll@Sun.COM /*
22*12720SWyllys.Ingersoll@Sun.COM * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
23*12720SWyllys.Ingersoll@Sun.COM */
24*12720SWyllys.Ingersoll@Sun.COM
25*12720SWyllys.Ingersoll@Sun.COM #include <strings.h>
26*12720SWyllys.Ingersoll@Sun.COM #include <errno.h>
27*12720SWyllys.Ingersoll@Sun.COM #include <security/cryptoki.h>
28*12720SWyllys.Ingersoll@Sun.COM #include <cryptoutil.h>
29*12720SWyllys.Ingersoll@Sun.COM #include "kmsGlobal.h"
30*12720SWyllys.Ingersoll@Sun.COM #include "kmsSession.h"
31*12720SWyllys.Ingersoll@Sun.COM #include "kmsObject.h"
32*12720SWyllys.Ingersoll@Sun.COM #include "kmsKeystoreUtil.h"
33*12720SWyllys.Ingersoll@Sun.COM
34*12720SWyllys.Ingersoll@Sun.COM static CK_RV
kms_generate_softkey(kms_object_t * keyp)35*12720SWyllys.Ingersoll@Sun.COM kms_generate_softkey(kms_object_t *keyp)
36*12720SWyllys.Ingersoll@Sun.COM {
37*12720SWyllys.Ingersoll@Sun.COM if ((OBJ_SEC_VALUE(keyp) = malloc(OBJ_SEC_VALUE_LEN(keyp))) == NULL)
38*12720SWyllys.Ingersoll@Sun.COM return (CKR_HOST_MEMORY);
39*12720SWyllys.Ingersoll@Sun.COM
40*12720SWyllys.Ingersoll@Sun.COM if (pkcs11_get_urandom(OBJ_SEC_VALUE(keyp),
41*12720SWyllys.Ingersoll@Sun.COM OBJ_SEC_VALUE_LEN(keyp)) < 0)
42*12720SWyllys.Ingersoll@Sun.COM return (CKR_DEVICE_ERROR);
43*12720SWyllys.Ingersoll@Sun.COM
44*12720SWyllys.Ingersoll@Sun.COM return (CKR_OK);
45*12720SWyllys.Ingersoll@Sun.COM }
46*12720SWyllys.Ingersoll@Sun.COM
47*12720SWyllys.Ingersoll@Sun.COM CK_RV
C_GenerateKey(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pMechanism,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulCount,CK_OBJECT_HANDLE_PTR phKey)48*12720SWyllys.Ingersoll@Sun.COM C_GenerateKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
49*12720SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_OBJECT_HANDLE_PTR phKey)
50*12720SWyllys.Ingersoll@Sun.COM {
51*12720SWyllys.Ingersoll@Sun.COM CK_RV rv = CKR_OK;
52*12720SWyllys.Ingersoll@Sun.COM kms_session_t *session_p;
53*12720SWyllys.Ingersoll@Sun.COM kms_object_t *new_objp = NULL;
54*12720SWyllys.Ingersoll@Sun.COM kms_slot_t *pslot;
55*12720SWyllys.Ingersoll@Sun.COM boolean_t ses_lock_held = B_FALSE;
56*12720SWyllys.Ingersoll@Sun.COM
57*12720SWyllys.Ingersoll@Sun.COM if (!kms_initialized)
58*12720SWyllys.Ingersoll@Sun.COM return (CKR_CRYPTOKI_NOT_INITIALIZED);
59*12720SWyllys.Ingersoll@Sun.COM
60*12720SWyllys.Ingersoll@Sun.COM /* Obtain the session pointer */
61*12720SWyllys.Ingersoll@Sun.COM rv = handle2session(hSession, &session_p);
62*12720SWyllys.Ingersoll@Sun.COM if (rv != CKR_OK)
63*12720SWyllys.Ingersoll@Sun.COM return (rv);
64*12720SWyllys.Ingersoll@Sun.COM
65*12720SWyllys.Ingersoll@Sun.COM if ((pMechanism == NULL) || (phKey == NULL)) {
66*12720SWyllys.Ingersoll@Sun.COM rv = CKR_ARGUMENTS_BAD;
67*12720SWyllys.Ingersoll@Sun.COM goto failed_exit;
68*12720SWyllys.Ingersoll@Sun.COM }
69*12720SWyllys.Ingersoll@Sun.COM
70*12720SWyllys.Ingersoll@Sun.COM if ((pTemplate == NULL) && (ulCount != 0)) {
71*12720SWyllys.Ingersoll@Sun.COM rv = CKR_ARGUMENTS_BAD;
72*12720SWyllys.Ingersoll@Sun.COM goto failed_exit;
73*12720SWyllys.Ingersoll@Sun.COM }
74*12720SWyllys.Ingersoll@Sun.COM
75*12720SWyllys.Ingersoll@Sun.COM switch (pMechanism->mechanism) {
76*12720SWyllys.Ingersoll@Sun.COM case CKM_AES_KEY_GEN:
77*12720SWyllys.Ingersoll@Sun.COM break;
78*12720SWyllys.Ingersoll@Sun.COM default:
79*12720SWyllys.Ingersoll@Sun.COM rv = CKR_MECHANISM_INVALID;
80*12720SWyllys.Ingersoll@Sun.COM goto failed_exit;
81*12720SWyllys.Ingersoll@Sun.COM break;
82*12720SWyllys.Ingersoll@Sun.COM }
83*12720SWyllys.Ingersoll@Sun.COM
84*12720SWyllys.Ingersoll@Sun.COM /* Create an object record */
85*12720SWyllys.Ingersoll@Sun.COM new_objp = kms_new_object();
86*12720SWyllys.Ingersoll@Sun.COM if (new_objp == NULL)
87*12720SWyllys.Ingersoll@Sun.COM return (CKR_HOST_MEMORY);
88*12720SWyllys.Ingersoll@Sun.COM
89*12720SWyllys.Ingersoll@Sun.COM new_objp->mechanism = pMechanism->mechanism;
90*12720SWyllys.Ingersoll@Sun.COM rv = kms_build_object(pTemplate, ulCount, new_objp);
91*12720SWyllys.Ingersoll@Sun.COM if (rv != CKR_OK)
92*12720SWyllys.Ingersoll@Sun.COM goto failed_exit;
93*12720SWyllys.Ingersoll@Sun.COM
94*12720SWyllys.Ingersoll@Sun.COM /*
95*12720SWyllys.Ingersoll@Sun.COM * Generate the KMS key.
96*12720SWyllys.Ingersoll@Sun.COM *
97*12720SWyllys.Ingersoll@Sun.COM * This will put the AES key value from the KMS key into the
98*12720SWyllys.Ingersoll@Sun.COM * key object record.
99*12720SWyllys.Ingersoll@Sun.COM */
100*12720SWyllys.Ingersoll@Sun.COM if (new_objp->bool_attr_mask & TOKEN_BOOL_ON)
101*12720SWyllys.Ingersoll@Sun.COM rv = KMS_GenerateKey(session_p, new_objp);
102*12720SWyllys.Ingersoll@Sun.COM else
103*12720SWyllys.Ingersoll@Sun.COM rv = kms_generate_softkey(new_objp);
104*12720SWyllys.Ingersoll@Sun.COM
105*12720SWyllys.Ingersoll@Sun.COM if (rv != CKR_OK)
106*12720SWyllys.Ingersoll@Sun.COM goto failed_exit;
107*12720SWyllys.Ingersoll@Sun.COM
108*12720SWyllys.Ingersoll@Sun.COM if (new_objp->bool_attr_mask & TOKEN_BOOL_ON) {
109*12720SWyllys.Ingersoll@Sun.COM pslot = get_slotinfo();
110*12720SWyllys.Ingersoll@Sun.COM if (pslot == NULL) {
111*12720SWyllys.Ingersoll@Sun.COM rv = CKR_GENERAL_ERROR;
112*12720SWyllys.Ingersoll@Sun.COM goto failed_exit;
113*12720SWyllys.Ingersoll@Sun.COM }
114*12720SWyllys.Ingersoll@Sun.COM kms_add_token_object_to_slot(new_objp, pslot);
115*12720SWyllys.Ingersoll@Sun.COM } else {
116*12720SWyllys.Ingersoll@Sun.COM kms_add_object_to_session(new_objp, session_p);
117*12720SWyllys.Ingersoll@Sun.COM }
118*12720SWyllys.Ingersoll@Sun.COM
119*12720SWyllys.Ingersoll@Sun.COM *phKey = (CK_OBJECT_HANDLE)new_objp;
120*12720SWyllys.Ingersoll@Sun.COM REFRELE(session_p, ses_lock_held);
121*12720SWyllys.Ingersoll@Sun.COM return (rv);
122*12720SWyllys.Ingersoll@Sun.COM
123*12720SWyllys.Ingersoll@Sun.COM failed_exit:
124*12720SWyllys.Ingersoll@Sun.COM if (new_objp != NULL)
125*12720SWyllys.Ingersoll@Sun.COM (void) free(new_objp);
126*12720SWyllys.Ingersoll@Sun.COM
127*12720SWyllys.Ingersoll@Sun.COM REFRELE(session_p, ses_lock_held);
128*12720SWyllys.Ingersoll@Sun.COM return (rv);
129*12720SWyllys.Ingersoll@Sun.COM }
130*12720SWyllys.Ingersoll@Sun.COM
131*12720SWyllys.Ingersoll@Sun.COM /*ARGSUSED*/
132*12720SWyllys.Ingersoll@Sun.COM CK_RV
C_GenerateKeyPair(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pMechanism,CK_ATTRIBUTE_PTR pPublicKeyTemplate,CK_ULONG ulPublicKeyAttributeCount,CK_ATTRIBUTE_PTR pPrivateKeyTemplate,CK_ULONG ulPrivateKeyAttributeCount,CK_OBJECT_HANDLE_PTR phPublicKey,CK_OBJECT_HANDLE_PTR phPrivateKey)133*12720SWyllys.Ingersoll@Sun.COM C_GenerateKeyPair(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
134*12720SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE_PTR pPublicKeyTemplate, CK_ULONG ulPublicKeyAttributeCount,
135*12720SWyllys.Ingersoll@Sun.COM CK_ATTRIBUTE_PTR pPrivateKeyTemplate, CK_ULONG ulPrivateKeyAttributeCount,
136*12720SWyllys.Ingersoll@Sun.COM CK_OBJECT_HANDLE_PTR phPublicKey, CK_OBJECT_HANDLE_PTR phPrivateKey)
137*12720SWyllys.Ingersoll@Sun.COM {
138*12720SWyllys.Ingersoll@Sun.COM if (!kms_initialized)
139*12720SWyllys.Ingersoll@Sun.COM return (CKR_CRYPTOKI_NOT_INITIALIZED);
140*12720SWyllys.Ingersoll@Sun.COM
141*12720SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_NOT_SUPPORTED);
142*12720SWyllys.Ingersoll@Sun.COM }
143*12720SWyllys.Ingersoll@Sun.COM
144*12720SWyllys.Ingersoll@Sun.COM /*ARGSUSED*/
145*12720SWyllys.Ingersoll@Sun.COM CK_RV
C_WrapKey(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pMechanism,CK_OBJECT_HANDLE hWrappingKey,CK_OBJECT_HANDLE hKey,CK_BYTE_PTR pWrappedKey,CK_ULONG_PTR pulWrappedKeyLen)146*12720SWyllys.Ingersoll@Sun.COM C_WrapKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
147*12720SWyllys.Ingersoll@Sun.COM CK_OBJECT_HANDLE hWrappingKey, CK_OBJECT_HANDLE hKey,
148*12720SWyllys.Ingersoll@Sun.COM CK_BYTE_PTR pWrappedKey, CK_ULONG_PTR pulWrappedKeyLen)
149*12720SWyllys.Ingersoll@Sun.COM {
150*12720SWyllys.Ingersoll@Sun.COM if (!kms_initialized)
151*12720SWyllys.Ingersoll@Sun.COM return (CKR_CRYPTOKI_NOT_INITIALIZED);
152*12720SWyllys.Ingersoll@Sun.COM
153*12720SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_NOT_SUPPORTED);
154*12720SWyllys.Ingersoll@Sun.COM }
155*12720SWyllys.Ingersoll@Sun.COM
156*12720SWyllys.Ingersoll@Sun.COM /*ARGSUSED*/
157*12720SWyllys.Ingersoll@Sun.COM CK_RV
C_UnwrapKey(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pMechanism,CK_OBJECT_HANDLE hUnwrappingKey,CK_BYTE_PTR pWrappedKey,CK_ULONG ulWrappedKeyLen,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulAttributeCount,CK_OBJECT_HANDLE_PTR phKey)158*12720SWyllys.Ingersoll@Sun.COM C_UnwrapKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
159*12720SWyllys.Ingersoll@Sun.COM CK_OBJECT_HANDLE hUnwrappingKey, CK_BYTE_PTR pWrappedKey,
160*12720SWyllys.Ingersoll@Sun.COM CK_ULONG ulWrappedKeyLen, CK_ATTRIBUTE_PTR pTemplate,
161*12720SWyllys.Ingersoll@Sun.COM CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
162*12720SWyllys.Ingersoll@Sun.COM {
163*12720SWyllys.Ingersoll@Sun.COM if (!kms_initialized)
164*12720SWyllys.Ingersoll@Sun.COM return (CKR_CRYPTOKI_NOT_INITIALIZED);
165*12720SWyllys.Ingersoll@Sun.COM
166*12720SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_NOT_SUPPORTED);
167*12720SWyllys.Ingersoll@Sun.COM }
168*12720SWyllys.Ingersoll@Sun.COM
169*12720SWyllys.Ingersoll@Sun.COM /*ARGSUSED*/
170*12720SWyllys.Ingersoll@Sun.COM CK_RV
C_DeriveKey(CK_SESSION_HANDLE hSession,CK_MECHANISM_PTR pMechanism,CK_OBJECT_HANDLE hBaseKey,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG ulAttributeCount,CK_OBJECT_HANDLE_PTR phKey)171*12720SWyllys.Ingersoll@Sun.COM C_DeriveKey(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism,
172*12720SWyllys.Ingersoll@Sun.COM CK_OBJECT_HANDLE hBaseKey, CK_ATTRIBUTE_PTR pTemplate,
173*12720SWyllys.Ingersoll@Sun.COM CK_ULONG ulAttributeCount, CK_OBJECT_HANDLE_PTR phKey)
174*12720SWyllys.Ingersoll@Sun.COM {
175*12720SWyllys.Ingersoll@Sun.COM if (!kms_initialized)
176*12720SWyllys.Ingersoll@Sun.COM return (CKR_CRYPTOKI_NOT_INITIALIZED);
177*12720SWyllys.Ingersoll@Sun.COM
178*12720SWyllys.Ingersoll@Sun.COM return (CKR_FUNCTION_NOT_SUPPORTED);
179*12720SWyllys.Ingersoll@Sun.COM }
180