17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 590e0e8c4Sizick * Common Development and Distribution License (the "License"). 690e0e8c4Sizick * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21*726fad2aSDina K Nimeh 227c478bd9Sstevel@tonic-gate /* 23c5866e1dSPeter Shoults * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <strings.h> 277c478bd9Sstevel@tonic-gate #include <cryptoutil.h> 287c478bd9Sstevel@tonic-gate #include <security/cryptoki.h> 2923c57df7Smcpowers #include <sys/crypto/common.h> 307c478bd9Sstevel@tonic-gate #include <arcfour.h> 317c478bd9Sstevel@tonic-gate #include "softGlobal.h" 327c478bd9Sstevel@tonic-gate #include "softSession.h" 337c478bd9Sstevel@tonic-gate #include <aes_impl.h> 34f66d273dSizick #include <blowfish_impl.h> 35f9fbec18Smcpowers #include <des_impl.h> 36f9fbec18Smcpowers #include <ecc_impl.h> 377c478bd9Sstevel@tonic-gate #include "softDH.h" 387c478bd9Sstevel@tonic-gate #include "softObject.h" 397c478bd9Sstevel@tonic-gate #include "softKeystore.h" 407c478bd9Sstevel@tonic-gate #include "softKeystoreUtil.h" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate static CK_MECHANISM_TYPE soft_mechanisms[] = { 447c478bd9Sstevel@tonic-gate CKM_DES_CBC, 457c478bd9Sstevel@tonic-gate CKM_DES_CBC_PAD, 467c478bd9Sstevel@tonic-gate CKM_DES_ECB, 477c478bd9Sstevel@tonic-gate CKM_DES_KEY_GEN, 487c478bd9Sstevel@tonic-gate CKM_DES_MAC_GENERAL, 497c478bd9Sstevel@tonic-gate CKM_DES_MAC, 507c478bd9Sstevel@tonic-gate CKM_DES3_CBC, 517c478bd9Sstevel@tonic-gate CKM_DES3_CBC_PAD, 527c478bd9Sstevel@tonic-gate CKM_DES3_ECB, 53436935a1SVladimir Kotal CKM_DES2_KEY_GEN, 547c478bd9Sstevel@tonic-gate CKM_DES3_KEY_GEN, 557c478bd9Sstevel@tonic-gate CKM_AES_CBC, 567c478bd9Sstevel@tonic-gate CKM_AES_CBC_PAD, 5723c57df7Smcpowers CKM_AES_CTR, 587c478bd9Sstevel@tonic-gate CKM_AES_ECB, 597c478bd9Sstevel@tonic-gate CKM_AES_KEY_GEN, 60f66d273dSizick CKM_BLOWFISH_CBC, 61f66d273dSizick CKM_BLOWFISH_KEY_GEN, 627c478bd9Sstevel@tonic-gate CKM_SHA_1, 637c478bd9Sstevel@tonic-gate CKM_SHA_1_HMAC, 647c478bd9Sstevel@tonic-gate CKM_SHA_1_HMAC_GENERAL, 65f66d273dSizick CKM_SHA256, 66f66d273dSizick CKM_SHA256_HMAC, 67f66d273dSizick CKM_SHA256_HMAC_GENERAL, 68f66d273dSizick CKM_SHA384, 69f66d273dSizick CKM_SHA384_HMAC, 70f66d273dSizick CKM_SHA384_HMAC_GENERAL, 71f66d273dSizick CKM_SHA512, 72f66d273dSizick CKM_SHA512_HMAC, 73f66d273dSizick CKM_SHA512_HMAC_GENERAL, 747c478bd9Sstevel@tonic-gate CKM_SSL3_SHA1_MAC, 757c478bd9Sstevel@tonic-gate CKM_MD5, 767c478bd9Sstevel@tonic-gate CKM_MD5_HMAC, 777c478bd9Sstevel@tonic-gate CKM_MD5_HMAC_GENERAL, 787c478bd9Sstevel@tonic-gate CKM_SSL3_MD5_MAC, 797c478bd9Sstevel@tonic-gate CKM_RC4, 807c478bd9Sstevel@tonic-gate CKM_RC4_KEY_GEN, 817c478bd9Sstevel@tonic-gate CKM_DSA, 827c478bd9Sstevel@tonic-gate CKM_DSA_SHA1, 837c478bd9Sstevel@tonic-gate CKM_DSA_KEY_PAIR_GEN, 847c478bd9Sstevel@tonic-gate CKM_RSA_PKCS, 857c478bd9Sstevel@tonic-gate CKM_RSA_PKCS_KEY_PAIR_GEN, 867c478bd9Sstevel@tonic-gate CKM_RSA_X_509, 877c478bd9Sstevel@tonic-gate CKM_MD5_RSA_PKCS, 887c478bd9Sstevel@tonic-gate CKM_SHA1_RSA_PKCS, 89f66d273dSizick CKM_SHA256_RSA_PKCS, 90f66d273dSizick CKM_SHA384_RSA_PKCS, 91f66d273dSizick CKM_SHA512_RSA_PKCS, 927c478bd9Sstevel@tonic-gate CKM_DH_PKCS_KEY_PAIR_GEN, 937c478bd9Sstevel@tonic-gate CKM_DH_PKCS_DERIVE, 947c478bd9Sstevel@tonic-gate CKM_MD5_KEY_DERIVATION, 957c478bd9Sstevel@tonic-gate CKM_SHA1_KEY_DERIVATION, 96f66d273dSizick CKM_SHA256_KEY_DERIVATION, 97f66d273dSizick CKM_SHA384_KEY_DERIVATION, 98f66d273dSizick CKM_SHA512_KEY_DERIVATION, 997c478bd9Sstevel@tonic-gate CKM_PBE_SHA1_RC4_128, 1007c478bd9Sstevel@tonic-gate CKM_PKCS5_PBKD2, 1017c478bd9Sstevel@tonic-gate CKM_SSL3_PRE_MASTER_KEY_GEN, 1027c478bd9Sstevel@tonic-gate CKM_TLS_PRE_MASTER_KEY_GEN, 1037c478bd9Sstevel@tonic-gate CKM_SSL3_MASTER_KEY_DERIVE, 1047c478bd9Sstevel@tonic-gate CKM_TLS_MASTER_KEY_DERIVE, 1057c478bd9Sstevel@tonic-gate CKM_SSL3_MASTER_KEY_DERIVE_DH, 1067c478bd9Sstevel@tonic-gate CKM_TLS_MASTER_KEY_DERIVE_DH, 1077c478bd9Sstevel@tonic-gate CKM_SSL3_KEY_AND_MAC_DERIVE, 10860722cc8Sizick CKM_TLS_KEY_AND_MAC_DERIVE, 109f9fbec18Smcpowers CKM_TLS_PRF, 110f9fbec18Smcpowers CKM_EC_KEY_PAIR_GEN, 111f9fbec18Smcpowers CKM_ECDSA, 112f9fbec18Smcpowers CKM_ECDSA_SHA1, 113f9fbec18Smcpowers CKM_ECDH1_DERIVE 1147c478bd9Sstevel@tonic-gate }; 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate /* 1177c478bd9Sstevel@tonic-gate * This is the table of CK_MECHANISM_INFO structs for the supported mechanisms. 1187c478bd9Sstevel@tonic-gate * The index for this table is the same as the one above for the same 1197c478bd9Sstevel@tonic-gate * mechanism. 1207c478bd9Sstevel@tonic-gate * The minimum and maximum sizes of the key for the mechanism can be measured 1217c478bd9Sstevel@tonic-gate * in bits or in bytes (i.e. mechanism-dependent). This table specifies the 1227c478bd9Sstevel@tonic-gate * supported range of key sizes in bytes; unless noted as in bits. 1237c478bd9Sstevel@tonic-gate */ 1247c478bd9Sstevel@tonic-gate static CK_MECHANISM_INFO soft_mechanism_info[] = { 1257c478bd9Sstevel@tonic-gate {DES_MINBYTES, DES_MAXBYTES, 1267c478bd9Sstevel@tonic-gate CKF_ENCRYPT|CKF_DECRYPT| 1277c478bd9Sstevel@tonic-gate CKF_WRAP|CKF_UNWRAP}, /* CKM_DES_CBC */ 1287c478bd9Sstevel@tonic-gate {DES_MINBYTES, DES_MAXBYTES, 1297c478bd9Sstevel@tonic-gate CKF_ENCRYPT|CKF_DECRYPT| 1307c478bd9Sstevel@tonic-gate CKF_WRAP|CKF_UNWRAP}, /* CKM_DES_CBC_PAD */ 1317c478bd9Sstevel@tonic-gate {DES_MINBYTES, DES_MAXBYTES, 1327c478bd9Sstevel@tonic-gate CKF_ENCRYPT|CKF_DECRYPT| 1337c478bd9Sstevel@tonic-gate CKF_WRAP|CKF_UNWRAP}, /* CKM_DES_ECB */ 1347c478bd9Sstevel@tonic-gate {DES_MINBYTES, DES_MAXBYTES, 1357c478bd9Sstevel@tonic-gate CKF_GENERATE}, /* CKM_DES_KEY_GEN */ 1367c478bd9Sstevel@tonic-gate {DES_MINBYTES, DES_MAXBYTES, 1377c478bd9Sstevel@tonic-gate CKF_SIGN|CKF_VERIFY}, /* CKM_DES_MAC_GENERAL */ 1387c478bd9Sstevel@tonic-gate {DES_MINBYTES, DES_MAXBYTES, 1397c478bd9Sstevel@tonic-gate CKF_SIGN|CKF_VERIFY}, /* CKM_DES_MAC */ 1407c478bd9Sstevel@tonic-gate {DES3_MINBYTES, DES3_MAXBYTES, 1417c478bd9Sstevel@tonic-gate CKF_ENCRYPT|CKF_DECRYPT| 1427c478bd9Sstevel@tonic-gate CKF_WRAP|CKF_UNWRAP}, /* CKM_DES3_CBC */ 1437c478bd9Sstevel@tonic-gate {DES3_MINBYTES, DES3_MAXBYTES, 1447c478bd9Sstevel@tonic-gate CKF_ENCRYPT|CKF_DECRYPT| 1457c478bd9Sstevel@tonic-gate CKF_WRAP|CKF_UNWRAP}, /* CKM_DES3_CBC_PAD */ 1467c478bd9Sstevel@tonic-gate {DES3_MINBYTES, DES3_MAXBYTES, 1477c478bd9Sstevel@tonic-gate CKF_ENCRYPT|CKF_DECRYPT| 1487c478bd9Sstevel@tonic-gate CKF_WRAP|CKF_UNWRAP}, /* CKM_DES3_ECB */ 149436935a1SVladimir Kotal {DES2_MAXBYTES, DES2_MAXBYTES, 150436935a1SVladimir Kotal CKF_GENERATE}, /* CKM_DES2_KEY_GEN */ 151436935a1SVladimir Kotal {DES3_MAXBYTES, DES3_MAXBYTES, /* CKK_DES3 only */ 1527c478bd9Sstevel@tonic-gate CKF_GENERATE}, /* CKM_DES3_KEY_GEN */ 1537c478bd9Sstevel@tonic-gate {AES_MINBYTES, AES_MAXBYTES, 1547c478bd9Sstevel@tonic-gate CKF_ENCRYPT|CKF_DECRYPT| 1557c478bd9Sstevel@tonic-gate CKF_WRAP|CKF_UNWRAP}, /* CKM_AES_CBC */ 1567c478bd9Sstevel@tonic-gate {AES_MINBYTES, AES_MAXBYTES, 1577c478bd9Sstevel@tonic-gate CKF_ENCRYPT|CKF_DECRYPT| 1587c478bd9Sstevel@tonic-gate CKF_WRAP|CKF_UNWRAP}, /* CKM_AES_CBC_PAD */ 1597c478bd9Sstevel@tonic-gate {AES_MINBYTES, AES_MAXBYTES, 1607c478bd9Sstevel@tonic-gate CKF_ENCRYPT|CKF_DECRYPT| 16123c57df7Smcpowers CKF_WRAP|CKF_UNWRAP}, /* CKM_AES_CTR */ 16223c57df7Smcpowers {AES_MINBYTES, AES_MAXBYTES, 16323c57df7Smcpowers CKF_ENCRYPT|CKF_DECRYPT| 1647c478bd9Sstevel@tonic-gate CKF_WRAP|CKF_UNWRAP}, /* CKM_AES_ECB */ 1657c478bd9Sstevel@tonic-gate {AES_MINBYTES, AES_MAXBYTES, 1667c478bd9Sstevel@tonic-gate CKF_GENERATE}, /* CKM_AES_KEY_GEN */ 167f66d273dSizick {BLOWFISH_MINBYTES, BLOWFISH_MAXBYTES, 168f66d273dSizick CKF_ENCRYPT|CKF_DECRYPT| 169f66d273dSizick CKF_WRAP|CKF_UNWRAP}, /* CKM_BLOWFISH_ECB */ 170f66d273dSizick {BLOWFISH_MINBYTES, BLOWFISH_MAXBYTES, 171f66d273dSizick CKF_GENERATE}, /* CKM_BLOWFISH_KEY_GEN */ 1727c478bd9Sstevel@tonic-gate {0, 0, CKF_DIGEST}, /* CKM_SHA_1 */ 1737c478bd9Sstevel@tonic-gate {1, 64, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA_1_HMAC */ 1747c478bd9Sstevel@tonic-gate {1, 64, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA_1_HMAC_GENERAL */ 175f66d273dSizick {0, 0, CKF_DIGEST}, /* CKM_SHA256 */ 176f66d273dSizick {1, 64, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA256_HMAC */ 177f66d273dSizick {1, 64, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA256_HMAC_GENERAL */ 178f66d273dSizick {0, 0, CKF_DIGEST}, /* CKM_SHA384 */ 179f66d273dSizick {1, 128, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA384_HMAC */ 180f66d273dSizick {1, 128, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA384_HMAC_GENERAL */ 181f66d273dSizick {0, 0, CKF_DIGEST}, /* CKM_SHA512 */ 182f66d273dSizick {1, 128, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA512_HMAC */ 183f66d273dSizick {1, 128, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA512_HMAC_GENERAL */ 1847c478bd9Sstevel@tonic-gate {1, 512, CKF_SIGN|CKF_VERIFY}, /* CKM_SSL3_SHA1_MAC */ 1857c478bd9Sstevel@tonic-gate {0, 0, CKF_DIGEST}, /* CKM_MD5 */ 1867c478bd9Sstevel@tonic-gate {1, 64, CKF_SIGN|CKF_VERIFY}, /* CKM_MD5_HMAC */ 1877c478bd9Sstevel@tonic-gate {1, 64, CKF_SIGN|CKF_VERIFY}, /* CKM_MD5_HMAC_GENERAL */ 1887c478bd9Sstevel@tonic-gate {1, 512, CKF_SIGN|CKF_VERIFY}, /* CKM_SSL3_MD5_MAC */ 1897c478bd9Sstevel@tonic-gate {8, ARCFOUR_MAX_KEY_BITS, CKF_ENCRYPT|CKF_DECRYPT}, /* CKM_RC4; */ 1907c478bd9Sstevel@tonic-gate /* in bits */ 1917c478bd9Sstevel@tonic-gate {8, ARCFOUR_MAX_KEY_BITS, CKF_GENERATE }, /* CKM_RC4_KEY_GEN; in bits */ 1927c478bd9Sstevel@tonic-gate {512, 1024, CKF_SIGN|CKF_VERIFY}, /* CKM_DSA; in bits */ 1937c478bd9Sstevel@tonic-gate {512, 1024, CKF_SIGN|CKF_VERIFY}, /* CKM_DSA_SHA1; in bits */ 1947c478bd9Sstevel@tonic-gate {512, 1024, CKF_GENERATE_KEY_PAIR}, /* CKM_DSA_KEY_PAIR_GEN; */ 1957c478bd9Sstevel@tonic-gate /* in bits */ 1967c478bd9Sstevel@tonic-gate {256, 4096, CKF_ENCRYPT|CKF_DECRYPT| 1977c478bd9Sstevel@tonic-gate CKF_SIGN|CKF_SIGN_RECOVER| 1987c478bd9Sstevel@tonic-gate CKF_WRAP|CKF_UNWRAP| 1997c478bd9Sstevel@tonic-gate CKF_VERIFY|CKF_VERIFY_RECOVER}, /* CKM_RSA_PKCS; in bits */ 2007c478bd9Sstevel@tonic-gate {256, 4096, CKF_GENERATE_KEY_PAIR}, /* CKM_RSA_PKCS_KEY_PAIR_GEN; */ 2017c478bd9Sstevel@tonic-gate /* in bits */ 2027c478bd9Sstevel@tonic-gate {256, 4096, CKF_ENCRYPT|CKF_DECRYPT| 2037c478bd9Sstevel@tonic-gate CKF_SIGN|CKF_SIGN_RECOVER| 2047c478bd9Sstevel@tonic-gate CKF_WRAP|CKF_UNWRAP| 2057c478bd9Sstevel@tonic-gate CKF_VERIFY|CKF_VERIFY_RECOVER}, /* CKM_RSA_X_509 in bits */ 2067c478bd9Sstevel@tonic-gate {256, 4096, CKF_SIGN|CKF_VERIFY}, /* CKM_MD5_RSA_PKCS in bits */ 2077c478bd9Sstevel@tonic-gate {256, 4096, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA1_RSA_PKCS in bits */ 208f66d273dSizick {256, 4096, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA256_RSA_PKCS in bits */ 209f66d273dSizick {256, 4096, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA384_RSA_PKCS in bits */ 210f66d273dSizick {256, 4096, CKF_SIGN|CKF_VERIFY}, /* CKM_SHA512_RSA_PKCS in bits */ 211*726fad2aSDina K Nimeh {DH_MIN_KEY_LEN, DH_MAX_KEY_LEN, CKF_GENERATE_KEY_PAIR}, 2127c478bd9Sstevel@tonic-gate /* CKM_DH_PKCS_KEY_PAIR_GEN */ 2137c478bd9Sstevel@tonic-gate /* in bits */ 214*726fad2aSDina K Nimeh {DH_MIN_KEY_LEN, DH_MAX_KEY_LEN, CKF_DERIVE}, 2157c478bd9Sstevel@tonic-gate /* CKM_DH_PKCS_DERIVE; */ 2167c478bd9Sstevel@tonic-gate /* in bits */ 2177c478bd9Sstevel@tonic-gate {1, 16, CKF_DERIVE}, /* CKM_MD5_KEY_DERIVATION */ 2187c478bd9Sstevel@tonic-gate {1, 20, CKF_DERIVE}, /* CKM_SHA1_KEY_DERIVATION */ 219f66d273dSizick {1, 32, CKF_DERIVE}, /* CKM_SHA256_KEY_DERIVATION */ 220f66d273dSizick {1, 48, CKF_DERIVE}, /* CKM_SHA384_KEY_DERIVATION */ 221f66d273dSizick {1, 64, CKF_DERIVE}, /* CKM_SHA512_KEY_DERIVATION */ 2227c478bd9Sstevel@tonic-gate {0, 0, CKF_GENERATE}, /* CKM_PBE_SHA1_RC4_128 */ 2237c478bd9Sstevel@tonic-gate {0, 0, CKF_GENERATE}, /* CKM_PKCS5_PBKD2 */ 2247c478bd9Sstevel@tonic-gate {48, 48, CKF_GENERATE}, /* CKM_SSL3_PRE_MASTER_KEY_GEN */ 2257c478bd9Sstevel@tonic-gate {48, 48, CKF_GENERATE}, /* CKM_TLS_PRE_MASTER_KEY_GEN */ 2267c478bd9Sstevel@tonic-gate {48, 48, CKF_DERIVE}, /* CKM_SSL3_MASTER_KEY_DERIVE */ 2277c478bd9Sstevel@tonic-gate {48, 48, CKF_DERIVE}, /* CKM_TLS_MASTER_KEY_DERIVE */ 2287c478bd9Sstevel@tonic-gate {48, 48, CKF_DERIVE}, /* CKM_SSL3_MASTER_KEY_DERIVE_DH */ 2297c478bd9Sstevel@tonic-gate {48, 48, CKF_DERIVE}, /* CKM_TLS_MASTER_KEY_DERIVE_DH */ 2307c478bd9Sstevel@tonic-gate {0, 0, CKF_DERIVE}, /* CKM_SSL3_KEY_AND_MAC_DERIVE */ 23160722cc8Sizick {0, 0, CKF_DERIVE}, /* CKM_TLS_KEY_AND_MAC_DERIVE */ 232f9fbec18Smcpowers {0, 0, CKF_DERIVE}, /* CKM_TLS_PRF */ 233f9fbec18Smcpowers {EC_MIN_KEY_LEN, EC_MAX_KEY_LEN, CKF_GENERATE_KEY_PAIR}, 234f9fbec18Smcpowers {EC_MIN_KEY_LEN, EC_MAX_KEY_LEN, CKF_SIGN|CKF_VERIFY}, 235f9fbec18Smcpowers {EC_MIN_KEY_LEN, EC_MAX_KEY_LEN, CKF_SIGN|CKF_VERIFY}, 236f9fbec18Smcpowers {EC_MIN_KEY_LEN, EC_MAX_KEY_LEN, CKF_DERIVE} 2377c478bd9Sstevel@tonic-gate }; 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate /* 2407c478bd9Sstevel@tonic-gate * Slot ID for softtoken is always 1. tokenPresent is ignored. 2417c478bd9Sstevel@tonic-gate * Also, only one slot is used. 2427c478bd9Sstevel@tonic-gate */ 2437c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 2447c478bd9Sstevel@tonic-gate CK_RV 2457c478bd9Sstevel@tonic-gate C_GetSlotList(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, 2467c478bd9Sstevel@tonic-gate CK_ULONG_PTR pulCount) 2477c478bd9Sstevel@tonic-gate { 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate CK_RV rv; 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate if (!softtoken_initialized) 2527c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED); 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate if (pulCount == NULL) { 2557c478bd9Sstevel@tonic-gate return (CKR_ARGUMENTS_BAD); 2567c478bd9Sstevel@tonic-gate } 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate if (pSlotList == NULL) { 2597c478bd9Sstevel@tonic-gate /* 2607c478bd9Sstevel@tonic-gate * Application only wants to know the number of slots. 2617c478bd9Sstevel@tonic-gate */ 2627c478bd9Sstevel@tonic-gate *pulCount = 1; 2637c478bd9Sstevel@tonic-gate return (CKR_OK); 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate if ((*pulCount < 1) && (pSlotList != NULL)) { 2677c478bd9Sstevel@tonic-gate rv = CKR_BUFFER_TOO_SMALL; 2687c478bd9Sstevel@tonic-gate } else { 2697c478bd9Sstevel@tonic-gate pSlotList[0] = SOFTTOKEN_SLOTID; 2707c478bd9Sstevel@tonic-gate rv = CKR_OK; 2717c478bd9Sstevel@tonic-gate } 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate *pulCount = 1; 2747c478bd9Sstevel@tonic-gate return (rv); 2757c478bd9Sstevel@tonic-gate } 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate CK_RV 2797c478bd9Sstevel@tonic-gate C_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo) 2807c478bd9Sstevel@tonic-gate { 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate if (!softtoken_initialized) 2837c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED); 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate if (pInfo == NULL) 2867c478bd9Sstevel@tonic-gate return (CKR_ARGUMENTS_BAD); 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate /* Make sure the slot ID is valid */ 2897c478bd9Sstevel@tonic-gate if (slotID != SOFTTOKEN_SLOTID) 2907c478bd9Sstevel@tonic-gate return (CKR_SLOT_ID_INVALID); 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate /* Provide information about the slot in the provided buffer */ 2937c478bd9Sstevel@tonic-gate (void) strncpy((char *)pInfo->slotDescription, SOFT_SLOT_DESCRIPTION, 2947c478bd9Sstevel@tonic-gate 64); 2957c478bd9Sstevel@tonic-gate (void) strncpy((char *)pInfo->manufacturerID, SOFT_MANUFACTURER_ID, 32); 2968cae6764SAnthony Scarpino pInfo->flags = CKF_TOKEN_PRESENT; 2977c478bd9Sstevel@tonic-gate pInfo->hardwareVersion.major = HARDWARE_VERSION_MAJOR; 2987c478bd9Sstevel@tonic-gate pInfo->hardwareVersion.minor = HARDWARE_VERSION_MINOR; 2997c478bd9Sstevel@tonic-gate pInfo->firmwareVersion.major = FIRMWARE_VERSION_MAJOR; 3007c478bd9Sstevel@tonic-gate pInfo->firmwareVersion.minor = FIRMWARE_VERSION_MINOR; 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate return (CKR_OK); 3037c478bd9Sstevel@tonic-gate } 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate CK_RV 3067c478bd9Sstevel@tonic-gate C_GetTokenInfo(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo) 3077c478bd9Sstevel@tonic-gate { 3081fa2a72aSPeter Shoults boolean_t pin_initialized = B_FALSE; 3091fa2a72aSPeter Shoults char *ks_cryptpin = NULL; 3101fa2a72aSPeter Shoults 3117c478bd9Sstevel@tonic-gate if (!softtoken_initialized) 3127c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED); 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate /* Make sure the slot ID is valid */ 3157c478bd9Sstevel@tonic-gate if (slotID != SOFTTOKEN_SLOTID) 3167c478bd9Sstevel@tonic-gate return (CKR_SLOT_ID_INVALID); 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate if (pInfo == NULL) 3197c478bd9Sstevel@tonic-gate return (CKR_ARGUMENTS_BAD); 3207c478bd9Sstevel@tonic-gate 3211fa2a72aSPeter Shoults /* 3221fa2a72aSPeter Shoults * It is intentional that we don't forward the error code 3231fa2a72aSPeter Shoults * returned from soft_keystore_pin_initialized() to the caller 3241fa2a72aSPeter Shoults */ 325c5866e1dSPeter Shoults pInfo->flags = SOFT_TOKEN_FLAGS; 326c5866e1dSPeter Shoults if (soft_slot.keystore_load_status == KEYSTORE_UNAVAILABLE) { 327c5866e1dSPeter Shoults pInfo->flags |= CKF_WRITE_PROTECTED; 3281fa2a72aSPeter Shoults } else { 3291fa2a72aSPeter Shoults if ((soft_keystore_pin_initialized(&pin_initialized, 3301fa2a72aSPeter Shoults &ks_cryptpin, B_FALSE) == CKR_OK) && !pin_initialized) 3311fa2a72aSPeter Shoults pInfo->flags |= CKF_USER_PIN_TO_BE_CHANGED; 332c5866e1dSPeter Shoults } 3331fa2a72aSPeter Shoults 3341fa2a72aSPeter Shoults if (ks_cryptpin) 3351fa2a72aSPeter Shoults free(ks_cryptpin); 3361fa2a72aSPeter Shoults 3377c478bd9Sstevel@tonic-gate /* Provide information about a token in the provided buffer */ 3387c478bd9Sstevel@tonic-gate (void) strncpy((char *)pInfo->label, SOFT_TOKEN_LABEL, 32); 3397c478bd9Sstevel@tonic-gate (void) strncpy((char *)pInfo->manufacturerID, SOFT_MANUFACTURER_ID, 32); 3407c478bd9Sstevel@tonic-gate (void) strncpy((char *)pInfo->model, TOKEN_MODEL, 16); 3417c478bd9Sstevel@tonic-gate (void) strncpy((char *)pInfo->serialNumber, SOFT_TOKEN_SERIAL, 16); 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate pInfo->ulMaxSessionCount = CK_EFFECTIVELY_INFINITE; 3447c478bd9Sstevel@tonic-gate pInfo->ulSessionCount = soft_session_cnt; 3457c478bd9Sstevel@tonic-gate pInfo->ulMaxRwSessionCount = CK_EFFECTIVELY_INFINITE; 3467c478bd9Sstevel@tonic-gate pInfo->ulRwSessionCount = soft_session_rw_cnt; 3477c478bd9Sstevel@tonic-gate pInfo->ulMaxPinLen = MAX_PIN_LEN; 3487c478bd9Sstevel@tonic-gate pInfo->ulMinPinLen = MIN_PIN_LEN; 3497c478bd9Sstevel@tonic-gate pInfo->ulTotalPublicMemory = CK_UNAVAILABLE_INFORMATION; 3507c478bd9Sstevel@tonic-gate pInfo->ulFreePublicMemory = CK_UNAVAILABLE_INFORMATION; 3517c478bd9Sstevel@tonic-gate pInfo->ulTotalPrivateMemory = CK_UNAVAILABLE_INFORMATION; 3527c478bd9Sstevel@tonic-gate pInfo->ulFreePrivateMemory = CK_UNAVAILABLE_INFORMATION; 3537c478bd9Sstevel@tonic-gate pInfo->hardwareVersion.major = HARDWARE_VERSION_MAJOR; 3547c478bd9Sstevel@tonic-gate pInfo->hardwareVersion.minor = HARDWARE_VERSION_MINOR; 3557c478bd9Sstevel@tonic-gate pInfo->firmwareVersion.major = FIRMWARE_VERSION_MAJOR; 3567c478bd9Sstevel@tonic-gate pInfo->firmwareVersion.minor = FIRMWARE_VERSION_MINOR; 3577c478bd9Sstevel@tonic-gate (void) memset(pInfo->utcTime, ' ', 16); 3587c478bd9Sstevel@tonic-gate 3597c478bd9Sstevel@tonic-gate return (CKR_OK); 3607c478bd9Sstevel@tonic-gate } 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3637c478bd9Sstevel@tonic-gate CK_RV 3647c478bd9Sstevel@tonic-gate C_WaitForSlotEvent(CK_FLAGS flags, CK_SLOT_ID_PTR pSlot, CK_VOID_PTR pReserved) 3657c478bd9Sstevel@tonic-gate { 3667c478bd9Sstevel@tonic-gate if (!softtoken_initialized) 3677c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED); 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate /* 3707c478bd9Sstevel@tonic-gate * This is currently not implemented, however we could cause this 3717c478bd9Sstevel@tonic-gate * to wait for the token files to appear if soft_token_present is 3727c478bd9Sstevel@tonic-gate * false. 3737c478bd9Sstevel@tonic-gate * However there is currently no polite and portable way to do that 3747c478bd9Sstevel@tonic-gate * because we might not even be able to get to an fd to the 3757c478bd9Sstevel@tonic-gate * parent directory, so instead we don't support any slot events. 3767c478bd9Sstevel@tonic-gate */ 3777c478bd9Sstevel@tonic-gate return (CKR_FUNCTION_NOT_SUPPORTED); 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate CK_RV 3827c478bd9Sstevel@tonic-gate C_GetMechanismList(CK_SLOT_ID slotID, CK_MECHANISM_TYPE_PTR pMechanismList, 3837c478bd9Sstevel@tonic-gate CK_ULONG_PTR pulCount) 3847c478bd9Sstevel@tonic-gate { 3857c478bd9Sstevel@tonic-gate 3867c478bd9Sstevel@tonic-gate ulong_t i; 3877c478bd9Sstevel@tonic-gate ulong_t mechnum; 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate if (!softtoken_initialized) 3907c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED); 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate if (slotID != SOFTTOKEN_SLOTID) 3937c478bd9Sstevel@tonic-gate return (CKR_SLOT_ID_INVALID); 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate mechnum = sizeof (soft_mechanisms) / sizeof (CK_MECHANISM_TYPE); 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate if (pMechanismList == NULL) { 3987c478bd9Sstevel@tonic-gate /* 3997c478bd9Sstevel@tonic-gate * Application only wants to know the number of 4007c478bd9Sstevel@tonic-gate * supported mechanism types. 4017c478bd9Sstevel@tonic-gate */ 4027c478bd9Sstevel@tonic-gate *pulCount = mechnum; 4037c478bd9Sstevel@tonic-gate return (CKR_OK); 4047c478bd9Sstevel@tonic-gate } 4057c478bd9Sstevel@tonic-gate 4067c478bd9Sstevel@tonic-gate if (*pulCount < mechnum) { 4077c478bd9Sstevel@tonic-gate *pulCount = mechnum; 4087c478bd9Sstevel@tonic-gate return (CKR_BUFFER_TOO_SMALL); 4097c478bd9Sstevel@tonic-gate } 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate for (i = 0; i < mechnum; i++) { 4127c478bd9Sstevel@tonic-gate pMechanismList[i] = soft_mechanisms[i]; 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate 4157c478bd9Sstevel@tonic-gate *pulCount = mechnum; 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate return (CKR_OK); 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate 4217c478bd9Sstevel@tonic-gate CK_RV 4227c478bd9Sstevel@tonic-gate C_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type, 4237c478bd9Sstevel@tonic-gate CK_MECHANISM_INFO_PTR pInfo) 4247c478bd9Sstevel@tonic-gate { 4257c478bd9Sstevel@tonic-gate 4267c478bd9Sstevel@tonic-gate ulong_t i; 4277c478bd9Sstevel@tonic-gate ulong_t mechnum; 4287c478bd9Sstevel@tonic-gate 4297c478bd9Sstevel@tonic-gate if (!softtoken_initialized) 4307c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED); 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate if (slotID != SOFTTOKEN_SLOTID) 4337c478bd9Sstevel@tonic-gate return (CKR_SLOT_ID_INVALID); 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate if (pInfo == NULL) { 4367c478bd9Sstevel@tonic-gate return (CKR_ARGUMENTS_BAD); 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate mechnum = sizeof (soft_mechanisms) / sizeof (CK_MECHANISM_TYPE); 4407c478bd9Sstevel@tonic-gate for (i = 0; i < mechnum; i++) { 4417c478bd9Sstevel@tonic-gate if (soft_mechanisms[i] == type) 4427c478bd9Sstevel@tonic-gate break; 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate 4457c478bd9Sstevel@tonic-gate if (i == mechnum) 4467c478bd9Sstevel@tonic-gate /* unsupported mechanism */ 4477c478bd9Sstevel@tonic-gate return (CKR_MECHANISM_INVALID); 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate pInfo->ulMinKeySize = soft_mechanism_info[i].ulMinKeySize; 4507c478bd9Sstevel@tonic-gate pInfo->ulMaxKeySize = soft_mechanism_info[i].ulMaxKeySize; 4517c478bd9Sstevel@tonic-gate pInfo->flags = soft_mechanism_info[i].flags; 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate return (CKR_OK); 4547c478bd9Sstevel@tonic-gate } 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4587c478bd9Sstevel@tonic-gate CK_RV 4597c478bd9Sstevel@tonic-gate C_InitToken(CK_SLOT_ID slotID, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen, 4607c478bd9Sstevel@tonic-gate CK_UTF8CHAR_PTR pLabel) 4617c478bd9Sstevel@tonic-gate { 4627c478bd9Sstevel@tonic-gate if (!softtoken_initialized) 4637c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED); 4647c478bd9Sstevel@tonic-gate 465c5866e1dSPeter Shoults if (create_keystore() != 0) 466c5866e1dSPeter Shoults return (CKR_FUNCTION_FAILED); 467c5866e1dSPeter Shoults 468c5866e1dSPeter Shoults return (CKR_OK); 4697c478bd9Sstevel@tonic-gate } 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 4727c478bd9Sstevel@tonic-gate CK_RV 4737c478bd9Sstevel@tonic-gate C_InitPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen) 4747c478bd9Sstevel@tonic-gate { 4757c478bd9Sstevel@tonic-gate if (!softtoken_initialized) 4767c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED); 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate return (CKR_FUNCTION_NOT_SUPPORTED); 4797c478bd9Sstevel@tonic-gate } 4807c478bd9Sstevel@tonic-gate 4817c478bd9Sstevel@tonic-gate 4827c478bd9Sstevel@tonic-gate CK_RV 4837c478bd9Sstevel@tonic-gate C_SetPIN(CK_SESSION_HANDLE hSession, CK_UTF8CHAR_PTR pOldPin, 4847c478bd9Sstevel@tonic-gate CK_ULONG ulOldPinLen, CK_UTF8CHAR_PTR pNewPin, CK_ULONG ulNewPinLen) 4857c478bd9Sstevel@tonic-gate { 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate soft_session_t *session_p; 4887c478bd9Sstevel@tonic-gate CK_RV rv; 4897c478bd9Sstevel@tonic-gate boolean_t lock_held = B_FALSE; 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate if (!softtoken_initialized) 4927c478bd9Sstevel@tonic-gate return (CKR_CRYPTOKI_NOT_INITIALIZED); 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate /* 4957c478bd9Sstevel@tonic-gate * Obtain the session pointer. Also, increment the session 4967c478bd9Sstevel@tonic-gate * reference count. 4977c478bd9Sstevel@tonic-gate */ 4987c478bd9Sstevel@tonic-gate rv = handle2session(hSession, &session_p); 4997c478bd9Sstevel@tonic-gate if (rv != CKR_OK) 5007c478bd9Sstevel@tonic-gate return (rv); 5017c478bd9Sstevel@tonic-gate 502c5866e1dSPeter Shoults if (!soft_keystore_status(KEYSTORE_LOAD)) { 5037c478bd9Sstevel@tonic-gate SES_REFRELE(session_p, lock_held); 5047c478bd9Sstevel@tonic-gate return (CKR_DEVICE_REMOVED); 5057c478bd9Sstevel@tonic-gate } 5067c478bd9Sstevel@tonic-gate 5077c478bd9Sstevel@tonic-gate if ((ulOldPinLen < MIN_PIN_LEN) || (ulOldPinLen > MAX_PIN_LEN) || 5087c478bd9Sstevel@tonic-gate (ulNewPinLen < MIN_PIN_LEN) ||(ulNewPinLen > MAX_PIN_LEN)) { 5097c478bd9Sstevel@tonic-gate SES_REFRELE(session_p, lock_held); 5107c478bd9Sstevel@tonic-gate return (CKR_PIN_LEN_RANGE); 5117c478bd9Sstevel@tonic-gate } 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate if ((pOldPin == NULL_PTR) || (pNewPin == NULL_PTR)) { 5147c478bd9Sstevel@tonic-gate /* 5157c478bd9Sstevel@tonic-gate * We don't support CKF_PROTECTED_AUTHENTICATION_PATH 5167c478bd9Sstevel@tonic-gate */ 5177c478bd9Sstevel@tonic-gate SES_REFRELE(session_p, lock_held); 5187c478bd9Sstevel@tonic-gate return (CKR_ARGUMENTS_BAD); 5197c478bd9Sstevel@tonic-gate } 5207c478bd9Sstevel@tonic-gate 5217c478bd9Sstevel@tonic-gate /* check the state of the session */ 5227c478bd9Sstevel@tonic-gate if ((session_p->state != CKS_RW_PUBLIC_SESSION) && 5237c478bd9Sstevel@tonic-gate (session_p->state != CKS_RW_USER_FUNCTIONS)) { 5247c478bd9Sstevel@tonic-gate SES_REFRELE(session_p, lock_held); 5257c478bd9Sstevel@tonic-gate return (CKR_SESSION_READ_ONLY); 5267c478bd9Sstevel@tonic-gate } 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate rv = soft_setpin(pOldPin, ulOldPinLen, pNewPin, ulNewPinLen); 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate SES_REFRELE(session_p, lock_held); 5317c478bd9Sstevel@tonic-gate return (rv); 5327c478bd9Sstevel@tonic-gate } 533