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 "softSession.h"
34 
35 CK_RV
C_SeedRandom(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pSeed,CK_ULONG ulSeedLen)36 C_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen)
37 {
38 
39 	CK_RV	rv;
40 	soft_session_t	*session_p;
41 	boolean_t	lock_held = B_FALSE;
42 
43 	if (!softtoken_initialized)
44 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
45 
46 	/* Obtain the session pointer just for validity check. */
47 	rv = handle2session(hSession, &session_p);
48 	if (rv != CKR_OK)
49 		return (rv);
50 
51 	SES_REFRELE(session_p, lock_held);
52 
53 	if ((pSeed == NULL) || (ulSeedLen == 0)) {
54 		return (CKR_ARGUMENTS_BAD);
55 	}
56 
57 	if (pkcs11_seed_urandom(pSeed, ulSeedLen) < 0) {
58 		if (errno == EACCES)
59 			return (CKR_RANDOM_SEED_NOT_SUPPORTED);
60 		return (CKR_DEVICE_ERROR);
61 	}
62 	return (CKR_OK);
63 
64 }
65 
66 CK_RV
C_GenerateRandom(CK_SESSION_HANDLE hSession,CK_BYTE_PTR pRandomData,CK_ULONG ulRandomLen)67 C_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData,
68     CK_ULONG ulRandomLen)
69 {
70 
71 	CK_RV	rv;
72 	soft_session_t	*session_p;
73 	boolean_t	lock_held = B_FALSE;
74 
75 	if (!softtoken_initialized)
76 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
77 
78 	/* Obtain the session pointer just for validity check. */
79 	rv = handle2session(hSession, &session_p);
80 	if (rv != CKR_OK)
81 		return (rv);
82 
83 	SES_REFRELE(session_p, lock_held);
84 
85 	if ((pRandomData == NULL) || (ulRandomLen == 0)) {
86 		return (CKR_ARGUMENTS_BAD);
87 	}
88 
89 	if (pkcs11_get_urandom(pRandomData, ulRandomLen) < 0)
90 		return (CKR_DEVICE_ERROR);
91 	return (CKR_OK);
92 
93 }
94