xref: /illumos-gate/usr/src/lib/pkcs11/libpkcs11/common/pkcs11Rand.c (revision f37b3cbb6f67aaea5eec1c335bdc7bf432867d64)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <security/cryptoki.h>
28 #include "pkcs11Global.h"
29 #include "pkcs11Session.h"
30 #include "pkcs11Slot.h"
31 
32 /*
33  * C_SeedRandom will verify that the session handle is valid within
34  * the framework, that random numbers are not disabled for the slot
35  * associated with this session, and then redirect to the underlying
36  * provider.
37  */
38 CK_RV
39 C_SeedRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pSeed, CK_ULONG ulSeedLen)
40 {
41 	CK_RV rv;
42 	pkcs11_session_t *sessp;
43 	CK_SLOT_ID slotid;
44 
45 	/* Check for a fastpath */
46 	if (purefastpath || policyfastpath) {
47 		/* Check if random number functions are allowed */
48 		if (policyfastpath &&
49 		    slottable->st_slots[fast_slot]->sl_norandom) {
50 			return (CKR_FUNCTION_FAILED);
51 		}
52 		return (fast_funcs->C_SeedRandom(hSession, pSeed, ulSeedLen));
53 	}
54 
55 	if (!pkcs11_initialized) {
56 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
57 	}
58 
59 	/* Obtain the session pointer */
60 	HANDLE2SESSION(hSession, sessp, rv);
61 
62 	if (rv != CKR_OK) {
63 		return (rv);
64 	}
65 
66 	slotid = sessp->se_slotid;
67 
68 	/* Check if random number functions are allowed */
69 	if (slottable->st_slots[slotid]->sl_norandom)
70 		return (CKR_FUNCTION_FAILED);
71 
72 	/* Pass data to the provider */
73 	rv = FUNCLIST(slotid)->C_SeedRandom(sessp->se_handle, pSeed,
74 	    ulSeedLen);
75 
76 	/* Present consistent interface to the application */
77 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
78 		return (CKR_FUNCTION_FAILED);
79 	}
80 
81 	return (rv);
82 }
83 
84 /*
85  * C_GenerateRandom will verify that the session handle is valid within
86  * the framework, that random numbers are not disabled for the slot
87  * associated with this session, and then redirect to the underlying
88  * provider.
89  */
90 CK_RV
91 C_GenerateRandom(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pRandomData,
92     CK_ULONG ulRandomLen)
93 {
94 	CK_RV rv;
95 	pkcs11_session_t *sessp;
96 	CK_SLOT_ID slotid;
97 
98 	/* Check for a fastpath */
99 	if (purefastpath || policyfastpath) {
100 		/* Check if random number functions are allowed */
101 		if (policyfastpath &&
102 		    slottable->st_slots[fast_slot]->sl_norandom) {
103 			return (CKR_FUNCTION_FAILED);
104 		}
105 		return (fast_funcs->C_GenerateRandom(hSession, pRandomData,
106 			    ulRandomLen));
107 	}
108 
109 	if (!pkcs11_initialized) {
110 		return (CKR_CRYPTOKI_NOT_INITIALIZED);
111 	}
112 
113 	/* Obtain the session pointer */
114 	HANDLE2SESSION(hSession, sessp, rv);
115 
116 	if (rv != CKR_OK) {
117 		return (rv);
118 	}
119 
120 	slotid = sessp->se_slotid;
121 
122 	/* Check if random number functions are allowed */
123 	if (slottable->st_slots[slotid]->sl_norandom)
124 		return (CKR_FUNCTION_FAILED);
125 
126 	/* Pass data to the provider */
127 	rv = FUNCLIST(sessp->se_slotid)->C_GenerateRandom(sessp->se_handle,
128 	    pRandomData, ulRandomLen);
129 
130 	/* Present consistent interface to the application */
131 	if (rv == CKR_FUNCTION_NOT_SUPPORTED) {
132 		return (CKR_FUNCTION_FAILED);
133 	}
134 
135 	return (rv);
136 }
137