1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <errno.h> 27 #include <fcntl.h> 28 #include <sys/stat.h> 29 #include <sys/types.h> 30 #include <security/cryptoki.h> 31 #include <cryptoutil.h> 32 #include "softGlobal.h" 33 #include "softRandom.h" 34 #include "softSession.h" 35 36 CK_RV 37 C_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen) 38 { 39 40 CK_RV rv; 41 soft_session_t *session_p; 42 boolean_t lock_held = B_FALSE; 43 long nwrite; 44 45 if (!softtoken_initialized) 46 return (CKR_CRYPTOKI_NOT_INITIALIZED); 47 48 /* Obtain the session pointer just for validity check. */ 49 rv = handle2session(hSession, &session_p); 50 if (rv != CKR_OK) 51 return (rv); 52 53 SES_REFRELE(session_p, lock_held); 54 55 if ((pSeed == NULL) || (ulSeedLen == 0)) { 56 return (CKR_ARGUMENTS_BAD); 57 } 58 59 if (soft_urandom_seed_fd < 0) { 60 (void) pthread_mutex_lock(&soft_giant_mutex); 61 /* Check again holding the mutex */ 62 if (soft_urandom_seed_fd < 0) { 63 soft_urandom_seed_fd = open_nointr(DEV_URANDOM, 64 O_WRONLY); 65 if (soft_urandom_seed_fd < 0) { 66 (void) pthread_mutex_unlock(&soft_giant_mutex); 67 if (errno == EACCES) 68 return (CKR_RANDOM_SEED_NOT_SUPPORTED); 69 return (CKR_DEVICE_ERROR); 70 } 71 } 72 (void) pthread_mutex_unlock(&soft_giant_mutex); 73 } 74 75 nwrite = writen_nointr(soft_urandom_seed_fd, pSeed, ulSeedLen); 76 if (nwrite <= 0) { 77 return (CKR_DEVICE_ERROR); 78 } 79 80 return (CKR_OK); 81 82 } 83 84 CK_RV 85 C_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData, 86 CK_ULONG ulRandomLen) 87 { 88 89 CK_RV rv; 90 soft_session_t *session_p; 91 boolean_t lock_held = B_FALSE; 92 93 if (!softtoken_initialized) 94 return (CKR_CRYPTOKI_NOT_INITIALIZED); 95 96 /* Obtain the session pointer just for validity check. */ 97 rv = handle2session(hSession, &session_p); 98 if (rv != CKR_OK) 99 return (rv); 100 101 SES_REFRELE(session_p, lock_held); 102 103 if ((pRandomData == NULL) || (ulRandomLen == 0)) { 104 return (CKR_ARGUMENTS_BAD); 105 } 106 107 return (soft_random_generator(pRandomData, ulRandomLen, B_FALSE)); 108 109 } 110