xref: /onnv-gate/usr/src/lib/pkcs11/pkcs11_tpm/common/sess_mgr.c (revision 9126:6acea8ac53c8)
1*9126SWyllys.Ingersoll@Sun.COM /*
2*9126SWyllys.Ingersoll@Sun.COM  *		Common Public License Version 0.5
3*9126SWyllys.Ingersoll@Sun.COM  *
4*9126SWyllys.Ingersoll@Sun.COM  *		THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF
5*9126SWyllys.Ingersoll@Sun.COM  *		THIS COMMON PUBLIC LICENSE ("AGREEMENT"). ANY USE,
6*9126SWyllys.Ingersoll@Sun.COM  *		REPRODUCTION OR DISTRIBUTION OF THE PROGRAM CONSTITUTES
7*9126SWyllys.Ingersoll@Sun.COM  *		RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
8*9126SWyllys.Ingersoll@Sun.COM  *
9*9126SWyllys.Ingersoll@Sun.COM  *		1. DEFINITIONS
10*9126SWyllys.Ingersoll@Sun.COM  *
11*9126SWyllys.Ingersoll@Sun.COM  *		"Contribution" means:
12*9126SWyllys.Ingersoll@Sun.COM  *		      a) in the case of the initial Contributor, the
13*9126SWyllys.Ingersoll@Sun.COM  *		      initial code and documentation distributed under
14*9126SWyllys.Ingersoll@Sun.COM  *		      this Agreement, and
15*9126SWyllys.Ingersoll@Sun.COM  *
16*9126SWyllys.Ingersoll@Sun.COM  *		      b) in the case of each subsequent Contributor:
17*9126SWyllys.Ingersoll@Sun.COM  *		      i) changes to the Program, and
18*9126SWyllys.Ingersoll@Sun.COM  *		      ii) additions to the Program;
19*9126SWyllys.Ingersoll@Sun.COM  *
20*9126SWyllys.Ingersoll@Sun.COM  *		      where such changes and/or additions to the Program
21*9126SWyllys.Ingersoll@Sun.COM  *		      originate from and are distributed by that
22*9126SWyllys.Ingersoll@Sun.COM  *		      particular Contributor. A Contribution 'originates'
23*9126SWyllys.Ingersoll@Sun.COM  *		      from a Contributor if it was added to the Program
24*9126SWyllys.Ingersoll@Sun.COM  *		      by such Contributor itself or anyone acting on such
25*9126SWyllys.Ingersoll@Sun.COM  *		      Contributor's behalf. Contributions do not include
26*9126SWyllys.Ingersoll@Sun.COM  *		      additions to the Program which: (i) are separate
27*9126SWyllys.Ingersoll@Sun.COM  *		      modules of software distributed in conjunction with
28*9126SWyllys.Ingersoll@Sun.COM  *		      the Program under their own license agreement, and
29*9126SWyllys.Ingersoll@Sun.COM  *		      (ii) are not derivative works of the Program.
30*9126SWyllys.Ingersoll@Sun.COM  *
31*9126SWyllys.Ingersoll@Sun.COM  *
32*9126SWyllys.Ingersoll@Sun.COM  *		"Contributor" means any person or entity that distributes
33*9126SWyllys.Ingersoll@Sun.COM  *		the Program.
34*9126SWyllys.Ingersoll@Sun.COM  *
35*9126SWyllys.Ingersoll@Sun.COM  *		"Licensed Patents " mean patent claims licensable by a
36*9126SWyllys.Ingersoll@Sun.COM  *		Contributor which are necessarily infringed by the use or
37*9126SWyllys.Ingersoll@Sun.COM  *		sale of its Contribution alone or when combined with the
38*9126SWyllys.Ingersoll@Sun.COM  *		Program.
39*9126SWyllys.Ingersoll@Sun.COM  *
40*9126SWyllys.Ingersoll@Sun.COM  *		"Program" means the Contributions distributed in
41*9126SWyllys.Ingersoll@Sun.COM  *		accordance with this Agreement.
42*9126SWyllys.Ingersoll@Sun.COM  *
43*9126SWyllys.Ingersoll@Sun.COM  *		"Recipient" means anyone who receives the Program under
44*9126SWyllys.Ingersoll@Sun.COM  *		this Agreement, including all Contributors.
45*9126SWyllys.Ingersoll@Sun.COM  *
46*9126SWyllys.Ingersoll@Sun.COM  *		2. GRANT OF RIGHTS
47*9126SWyllys.Ingersoll@Sun.COM  *
48*9126SWyllys.Ingersoll@Sun.COM  *		      a) Subject to the terms of this Agreement, each
49*9126SWyllys.Ingersoll@Sun.COM  *		      Contributor hereby grants Recipient a
50*9126SWyllys.Ingersoll@Sun.COM  *		      no - exclusive, worldwide, royalt - free copyright
51*9126SWyllys.Ingersoll@Sun.COM  *		      license to reproduce, prepare derivative works of,
52*9126SWyllys.Ingersoll@Sun.COM  *		      publicly display, publicly perform, distribute and
53*9126SWyllys.Ingersoll@Sun.COM  *		      sublicense the Contribution of such Contributor, if
54*9126SWyllys.Ingersoll@Sun.COM  *		      any, and such derivative works, in source code and
55*9126SWyllys.Ingersoll@Sun.COM  *		      object code form.
56*9126SWyllys.Ingersoll@Sun.COM  *
57*9126SWyllys.Ingersoll@Sun.COM  *		      b) Subject to the terms of this Agreement, each
58*9126SWyllys.Ingersoll@Sun.COM  *		      Contributor hereby grants Recipient a
59*9126SWyllys.Ingersoll@Sun.COM  *		      no - exclusive, worldwide, royalt - free patent
60*9126SWyllys.Ingersoll@Sun.COM  *		      license under Licensed Patents to make, use, sell,
61*9126SWyllys.Ingersoll@Sun.COM  *		      offer to sell, import and otherwise transfer the
62*9126SWyllys.Ingersoll@Sun.COM  *		      Contribution of such Contributor, if any, in source
63*9126SWyllys.Ingersoll@Sun.COM  *		      code and object code form. This patent license
64*9126SWyllys.Ingersoll@Sun.COM  *		      shall apply to the combination of the Contribution
65*9126SWyllys.Ingersoll@Sun.COM  *		      and the Program if, at the time the Contribution is
66*9126SWyllys.Ingersoll@Sun.COM  *		      added by the Contributor, such addition of the
67*9126SWyllys.Ingersoll@Sun.COM  *		      Contribution causes such combination to be covered
68*9126SWyllys.Ingersoll@Sun.COM  *		      by the Licensed Patents. The patent license shall
69*9126SWyllys.Ingersoll@Sun.COM  *		      not apply to any other combinations which include
70*9126SWyllys.Ingersoll@Sun.COM  *		      the Contribution. No hardware per se is licensed
71*9126SWyllys.Ingersoll@Sun.COM  *		      hereunder.
72*9126SWyllys.Ingersoll@Sun.COM  *
73*9126SWyllys.Ingersoll@Sun.COM  *		      c) Recipient understands that although each
74*9126SWyllys.Ingersoll@Sun.COM  *		      Contributor grants the licenses to its
75*9126SWyllys.Ingersoll@Sun.COM  *		      Contributions set forth herein, no assurances are
76*9126SWyllys.Ingersoll@Sun.COM  *		      provided by any Contributor that the Program does
77*9126SWyllys.Ingersoll@Sun.COM  *		      not infringe the patent or other intellectual
78*9126SWyllys.Ingersoll@Sun.COM  *		      property rights of any other entity. Each
79*9126SWyllys.Ingersoll@Sun.COM  *		      Contributor disclaims any liability to Recipient
80*9126SWyllys.Ingersoll@Sun.COM  *		      for claims brought by any other entity based on
81*9126SWyllys.Ingersoll@Sun.COM  *		      infringement of intellectual property rights or
82*9126SWyllys.Ingersoll@Sun.COM  *		      otherwise. As a condition to exercising the rights
83*9126SWyllys.Ingersoll@Sun.COM  *		      and licenses granted hereunder, each Recipient
84*9126SWyllys.Ingersoll@Sun.COM  *		      hereby assumes sole responsibility to secure any
85*9126SWyllys.Ingersoll@Sun.COM  *		      other intellectual property rights needed, if any.
86*9126SWyllys.Ingersoll@Sun.COM  *
87*9126SWyllys.Ingersoll@Sun.COM  *		      For example, if a third party patent license is
88*9126SWyllys.Ingersoll@Sun.COM  *		      required to allow Recipient to distribute the
89*9126SWyllys.Ingersoll@Sun.COM  *		      Program, it is Recipient's responsibility to
90*9126SWyllys.Ingersoll@Sun.COM  *		      acquire that license before distributing the
91*9126SWyllys.Ingersoll@Sun.COM  *		      Program.
92*9126SWyllys.Ingersoll@Sun.COM  *
93*9126SWyllys.Ingersoll@Sun.COM  *		      d) Each Contributor represents that to its
94*9126SWyllys.Ingersoll@Sun.COM  *		      knowledge it has sufficient copyright rights in its
95*9126SWyllys.Ingersoll@Sun.COM  *		      Contribution, if any, to grant the copyright
96*9126SWyllys.Ingersoll@Sun.COM  *		      license set forth in this Agreement.
97*9126SWyllys.Ingersoll@Sun.COM  *
98*9126SWyllys.Ingersoll@Sun.COM  *		3. REQUIREMENTS
99*9126SWyllys.Ingersoll@Sun.COM  *
100*9126SWyllys.Ingersoll@Sun.COM  *		A Contributor may choose to distribute the Program in
101*9126SWyllys.Ingersoll@Sun.COM  *		object code form under its own license agreement, provided
102*9126SWyllys.Ingersoll@Sun.COM  *		that:
103*9126SWyllys.Ingersoll@Sun.COM  *		      a) it complies with the terms and conditions of
104*9126SWyllys.Ingersoll@Sun.COM  *		      this Agreement; and
105*9126SWyllys.Ingersoll@Sun.COM  *
106*9126SWyllys.Ingersoll@Sun.COM  *		      b) its license agreement:
107*9126SWyllys.Ingersoll@Sun.COM  *		      i) effectively disclaims on behalf of all
108*9126SWyllys.Ingersoll@Sun.COM  *		      Contributors all warranties and conditions, express
109*9126SWyllys.Ingersoll@Sun.COM  *		      and implied, including warranties or conditions of
110*9126SWyllys.Ingersoll@Sun.COM  *		      title and no - infringement, and implied warranties
111*9126SWyllys.Ingersoll@Sun.COM  *		      or conditions of merchantability and fitness for a
112*9126SWyllys.Ingersoll@Sun.COM  *		      particular purpose;
113*9126SWyllys.Ingersoll@Sun.COM  *
114*9126SWyllys.Ingersoll@Sun.COM  *		      ii) effectively excludes on behalf of all
115*9126SWyllys.Ingersoll@Sun.COM  *		      Contributors all liability for damages, including
116*9126SWyllys.Ingersoll@Sun.COM  *		      direct, indirect, special, incidental and
117*9126SWyllys.Ingersoll@Sun.COM  *		      consequential damages, such as lost profits;
118*9126SWyllys.Ingersoll@Sun.COM  *
119*9126SWyllys.Ingersoll@Sun.COM  *		      iii) states that any provisions which differ from
120*9126SWyllys.Ingersoll@Sun.COM  *		      this Agreement are offered by that Contributor
121*9126SWyllys.Ingersoll@Sun.COM  *		      alone and not by any other party; and
122*9126SWyllys.Ingersoll@Sun.COM  *
123*9126SWyllys.Ingersoll@Sun.COM  *		      iv) states that source code for the Program is
124*9126SWyllys.Ingersoll@Sun.COM  *		      available from such Contributor, and informs
125*9126SWyllys.Ingersoll@Sun.COM  *		      licensees how to obtain it in a reasonable manner
126*9126SWyllys.Ingersoll@Sun.COM  *		      on or through a medium customarily used for
127*9126SWyllys.Ingersoll@Sun.COM  *		      software exchange.
128*9126SWyllys.Ingersoll@Sun.COM  *
129*9126SWyllys.Ingersoll@Sun.COM  *		When the Program is made available in source code form:
130*9126SWyllys.Ingersoll@Sun.COM  *		      a) it must be made available under this Agreement;
131*9126SWyllys.Ingersoll@Sun.COM  *		      and
132*9126SWyllys.Ingersoll@Sun.COM  *		      b) a copy of this Agreement must be included with
133*9126SWyllys.Ingersoll@Sun.COM  *		      each copy of the Program.
134*9126SWyllys.Ingersoll@Sun.COM  *
135*9126SWyllys.Ingersoll@Sun.COM  *		Contributors may not remove or alter any copyright notices
136*9126SWyllys.Ingersoll@Sun.COM  *		contained within the Program.
137*9126SWyllys.Ingersoll@Sun.COM  *
138*9126SWyllys.Ingersoll@Sun.COM  *		Each Contributor must identify itself as the originator of
139*9126SWyllys.Ingersoll@Sun.COM  *		its Contribution, if any, in a manner that reasonably
140*9126SWyllys.Ingersoll@Sun.COM  *		allows subsequent Recipients to identify the originator of
141*9126SWyllys.Ingersoll@Sun.COM  *		the Contribution.
142*9126SWyllys.Ingersoll@Sun.COM  *
143*9126SWyllys.Ingersoll@Sun.COM  *
144*9126SWyllys.Ingersoll@Sun.COM  *		4. COMMERCIAL DISTRIBUTION
145*9126SWyllys.Ingersoll@Sun.COM  *
146*9126SWyllys.Ingersoll@Sun.COM  *		Commercial distributors of software may accept certain
147*9126SWyllys.Ingersoll@Sun.COM  *		responsibilities with respect to end users, business
148*9126SWyllys.Ingersoll@Sun.COM  *		partners and the like. While this license is intended to
149*9126SWyllys.Ingersoll@Sun.COM  *		facilitate the commercial use of the Program, the
150*9126SWyllys.Ingersoll@Sun.COM  *		Contributor who includes the Program in a commercial
151*9126SWyllys.Ingersoll@Sun.COM  *		product offering should do so in a manner which does not
152*9126SWyllys.Ingersoll@Sun.COM  *		create potential liability for other Contributors.
153*9126SWyllys.Ingersoll@Sun.COM  *		Therefore, if a Contributor includes the Program in a
154*9126SWyllys.Ingersoll@Sun.COM  *		commercial product offering, such Contributor ("Commercial
155*9126SWyllys.Ingersoll@Sun.COM  *		Contributor") hereby agrees to defend and indemnify every
156*9126SWyllys.Ingersoll@Sun.COM  *		other Contributor ("Indemnified Contributor") against any
157*9126SWyllys.Ingersoll@Sun.COM  *		losses, damages and costs (collectively "Losses") arising
158*9126SWyllys.Ingersoll@Sun.COM  *		from claims, lawsuits and other legal actions brought by a
159*9126SWyllys.Ingersoll@Sun.COM  *		third party against the Indemnified Contributor to the
160*9126SWyllys.Ingersoll@Sun.COM  *		extent caused by the acts or omissions of such Commercial
161*9126SWyllys.Ingersoll@Sun.COM  *		Contributor in connection with its distribution of the
162*9126SWyllys.Ingersoll@Sun.COM  *		Program in a commercial product offering. The obligations
163*9126SWyllys.Ingersoll@Sun.COM  *		in this section do not apply to any claims or Losses
164*9126SWyllys.Ingersoll@Sun.COM  *		relating to any actual or alleged intellectual property
165*9126SWyllys.Ingersoll@Sun.COM  *		infringement. In order to qualify, an Indemnified
166*9126SWyllys.Ingersoll@Sun.COM  *		Contributor must: a) promptly notify the Commercial
167*9126SWyllys.Ingersoll@Sun.COM  *		Contributor in writing of such claim, and b) allow the
168*9126SWyllys.Ingersoll@Sun.COM  *		Commercial Contributor to control, and cooperate with the
169*9126SWyllys.Ingersoll@Sun.COM  *		Commercial Contributor in, the defense and any related
170*9126SWyllys.Ingersoll@Sun.COM  *		settlement negotiations. The Indemnified Contributor may
171*9126SWyllys.Ingersoll@Sun.COM  *		participate in any such claim at its own expense.
172*9126SWyllys.Ingersoll@Sun.COM  *
173*9126SWyllys.Ingersoll@Sun.COM  *
174*9126SWyllys.Ingersoll@Sun.COM  *		For example, a Contributor might include the Program in a
175*9126SWyllys.Ingersoll@Sun.COM  *		commercial product offering, Product X. That Contributor
176*9126SWyllys.Ingersoll@Sun.COM  *		is then a Commercial Contributor. If that Commercial
177*9126SWyllys.Ingersoll@Sun.COM  *		Contributor then makes performance claims, or offers
178*9126SWyllys.Ingersoll@Sun.COM  *		warranties related to Product X, those performance claims
179*9126SWyllys.Ingersoll@Sun.COM  *		and warranties are such Commercial Contributor's
180*9126SWyllys.Ingersoll@Sun.COM  *		responsibility alone. Under this section, the Commercial
181*9126SWyllys.Ingersoll@Sun.COM  *		Contributor would have to defend claims against the other
182*9126SWyllys.Ingersoll@Sun.COM  *		Contributors related to those performance claims and
183*9126SWyllys.Ingersoll@Sun.COM  *		warranties, and if a court requires any other Contributor
184*9126SWyllys.Ingersoll@Sun.COM  *		to pay any damages as a result, the Commercial Contributor
185*9126SWyllys.Ingersoll@Sun.COM  *		must pay those damages.
186*9126SWyllys.Ingersoll@Sun.COM  *
187*9126SWyllys.Ingersoll@Sun.COM  *
188*9126SWyllys.Ingersoll@Sun.COM  *		5. NO WARRANTY
189*9126SWyllys.Ingersoll@Sun.COM  *
190*9126SWyllys.Ingersoll@Sun.COM  *		EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE
191*9126SWyllys.Ingersoll@Sun.COM  *		PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT
192*9126SWyllys.Ingersoll@Sun.COM  *		WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
193*9126SWyllys.Ingersoll@Sun.COM  *		IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR
194*9126SWyllys.Ingersoll@Sun.COM  *		CONDITIONS OF TITLE, NO - INFRINGEMENT, MERCHANTABILITY OR
195*9126SWyllys.Ingersoll@Sun.COM  *		FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely
196*9126SWyllys.Ingersoll@Sun.COM  *		responsible for determining the appropriateness of using
197*9126SWyllys.Ingersoll@Sun.COM  *		and distributing the Program and assumes all risks
198*9126SWyllys.Ingersoll@Sun.COM  *		associated with its exercise of rights under this
199*9126SWyllys.Ingersoll@Sun.COM  *		Agreement, including but not limited to the risks and
200*9126SWyllys.Ingersoll@Sun.COM  *		costs of program errors, compliance with applicable laws,
201*9126SWyllys.Ingersoll@Sun.COM  *		damage to or loss of data, programs or equipment, and
202*9126SWyllys.Ingersoll@Sun.COM  *		unavailability or interruption of operations.
203*9126SWyllys.Ingersoll@Sun.COM  *
204*9126SWyllys.Ingersoll@Sun.COM  *		6. DISCLAIMER OF LIABILITY
205*9126SWyllys.Ingersoll@Sun.COM  *		EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER
206*9126SWyllys.Ingersoll@Sun.COM  *		RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY
207*9126SWyllys.Ingersoll@Sun.COM  *		FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
208*9126SWyllys.Ingersoll@Sun.COM  *		OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION
209*9126SWyllys.Ingersoll@Sun.COM  *		LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF
210*9126SWyllys.Ingersoll@Sun.COM  *		LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
211*9126SWyllys.Ingersoll@Sun.COM  *		(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
212*9126SWyllys.Ingersoll@Sun.COM  *		OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE
213*9126SWyllys.Ingersoll@Sun.COM  *		OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE
214*9126SWyllys.Ingersoll@Sun.COM  *		POSSIBILITY OF SUCH DAMAGES.
215*9126SWyllys.Ingersoll@Sun.COM  *
216*9126SWyllys.Ingersoll@Sun.COM  *		7. GENERAL
217*9126SWyllys.Ingersoll@Sun.COM  *
218*9126SWyllys.Ingersoll@Sun.COM  *		If any provision of this Agreement is invalid or
219*9126SWyllys.Ingersoll@Sun.COM  *		unenforceable under applicable law, it shall not affect
220*9126SWyllys.Ingersoll@Sun.COM  *		the validity or enforceability of the remainder of the
221*9126SWyllys.Ingersoll@Sun.COM  *		terms of this Agreement, and without further action by the
222*9126SWyllys.Ingersoll@Sun.COM  *		parties hereto, such provision shall be reformed to the
223*9126SWyllys.Ingersoll@Sun.COM  *		minimum extent necessary to make such provision valid and
224*9126SWyllys.Ingersoll@Sun.COM  *		enforceable.
225*9126SWyllys.Ingersoll@Sun.COM  *
226*9126SWyllys.Ingersoll@Sun.COM  *
227*9126SWyllys.Ingersoll@Sun.COM  *		If Recipient institutes patent litigation against a
228*9126SWyllys.Ingersoll@Sun.COM  *		Contributor with respect to a patent applicable to
229*9126SWyllys.Ingersoll@Sun.COM  *		software (including a cros - claim or counterclaim in a
230*9126SWyllys.Ingersoll@Sun.COM  *		lawsuit), then any patent licenses granted by that
231*9126SWyllys.Ingersoll@Sun.COM  *		Contributor to such Recipient under this Agreement shall
232*9126SWyllys.Ingersoll@Sun.COM  *		terminate as of the date such litigation is filed. In
233*9126SWyllys.Ingersoll@Sun.COM  *		addition, If Recipient institutes patent litigation
234*9126SWyllys.Ingersoll@Sun.COM  *		against any entity (including a cros - claim or
235*9126SWyllys.Ingersoll@Sun.COM  *		counterclaim in a lawsuit) alleging that the Program
236*9126SWyllys.Ingersoll@Sun.COM  *		itself (excluding combinations of the Program with other
237*9126SWyllys.Ingersoll@Sun.COM  *		software or hardware) infringes such Recipient's
238*9126SWyllys.Ingersoll@Sun.COM  *		patent(s), then such Recipient's rights granted under
239*9126SWyllys.Ingersoll@Sun.COM  *		Section 2(b) shall terminate as of the date such
240*9126SWyllys.Ingersoll@Sun.COM  *		litigation is filed.
241*9126SWyllys.Ingersoll@Sun.COM  *
242*9126SWyllys.Ingersoll@Sun.COM  *		All Recipient's rights under this Agreement shall
243*9126SWyllys.Ingersoll@Sun.COM  *		terminate if it fails to comply with any of the material
244*9126SWyllys.Ingersoll@Sun.COM  *		terms or conditions of this Agreement and does not cure
245*9126SWyllys.Ingersoll@Sun.COM  *		such failure in a reasonable period of time after becoming
246*9126SWyllys.Ingersoll@Sun.COM  *		aware of such noncompliance. If all Recipient's rights
247*9126SWyllys.Ingersoll@Sun.COM  *		under this Agreement terminate, Recipient agrees to cease
248*9126SWyllys.Ingersoll@Sun.COM  *		use and distribution of the Program as soon as reasonably
249*9126SWyllys.Ingersoll@Sun.COM  *		practicable. However, Recipient's obligations under this
250*9126SWyllys.Ingersoll@Sun.COM  *		Agreement and any licenses granted by Recipient relating
251*9126SWyllys.Ingersoll@Sun.COM  *		to the Program shall continue and survive.
252*9126SWyllys.Ingersoll@Sun.COM  *
253*9126SWyllys.Ingersoll@Sun.COM  *		Everyone is permitted to copy and distribute copies of
254*9126SWyllys.Ingersoll@Sun.COM  *		this Agreement, but in order to avoid inconsistency the
255*9126SWyllys.Ingersoll@Sun.COM  *		Agreement is copyrighted and may only be modified in the
256*9126SWyllys.Ingersoll@Sun.COM  *		following manner. The Agreement Steward reserves the right
257*9126SWyllys.Ingersoll@Sun.COM  *		to publish new versions (including revisions) of this
258*9126SWyllys.Ingersoll@Sun.COM  *		Agreement from time to time. No one other than the
259*9126SWyllys.Ingersoll@Sun.COM  *		Agreement Steward has the right to modify this Agreement.
260*9126SWyllys.Ingersoll@Sun.COM  *
261*9126SWyllys.Ingersoll@Sun.COM  *		IBM is the initial Agreement Steward. IBM may assign the
262*9126SWyllys.Ingersoll@Sun.COM  *		responsibility to serve as the Agreement Steward to a
263*9126SWyllys.Ingersoll@Sun.COM  *		suitable separate entity. Each new version of the
264*9126SWyllys.Ingersoll@Sun.COM  *		Agreement will be given a distinguishing version number.
265*9126SWyllys.Ingersoll@Sun.COM  *		The Program (including Contributions) may always be
266*9126SWyllys.Ingersoll@Sun.COM  *		distributed subject to the version of the Agreement under
267*9126SWyllys.Ingersoll@Sun.COM  *		which it was received. In addition, after a new version of
268*9126SWyllys.Ingersoll@Sun.COM  *		the Agreement is published, Contributor may elect to
269*9126SWyllys.Ingersoll@Sun.COM  *		distribute the Program (including its Contributions) under
270*9126SWyllys.Ingersoll@Sun.COM  *		the new version. Except as expressly stated in Sections
271*9126SWyllys.Ingersoll@Sun.COM  *		2(a) and 2(b) above, Recipient receives no rights or
272*9126SWyllys.Ingersoll@Sun.COM  *		licenses to the intellectual property of any Contributor
273*9126SWyllys.Ingersoll@Sun.COM  *		under this Agreement, whether expressly, by implication,
274*9126SWyllys.Ingersoll@Sun.COM  *		estoppel or otherwise. All rights in the Program not
275*9126SWyllys.Ingersoll@Sun.COM  *		expressly granted under this Agreement are reserved.
276*9126SWyllys.Ingersoll@Sun.COM  *
277*9126SWyllys.Ingersoll@Sun.COM  *
278*9126SWyllys.Ingersoll@Sun.COM  *		This Agreement is governed by the laws of the State of New
279*9126SWyllys.Ingersoll@Sun.COM  *		York and the intellectual property laws of the United
280*9126SWyllys.Ingersoll@Sun.COM  *		States of America. No party to this Agreement will bring a
281*9126SWyllys.Ingersoll@Sun.COM  *		legal action under this Agreement more than one year after
282*9126SWyllys.Ingersoll@Sun.COM  *		the cause of action arose. Each party waives its rights to
283*9126SWyllys.Ingersoll@Sun.COM  *		a jury trial in any resulting litigation.
284*9126SWyllys.Ingersoll@Sun.COM  *
285*9126SWyllys.Ingersoll@Sun.COM  *
286*9126SWyllys.Ingersoll@Sun.COM  *
287*9126SWyllys.Ingersoll@Sun.COM  * (C) COPYRIGHT International Business Machines Corp. 2001, 2002
288*9126SWyllys.Ingersoll@Sun.COM  */
289*9126SWyllys.Ingersoll@Sun.COM /*
290*9126SWyllys.Ingersoll@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
291*9126SWyllys.Ingersoll@Sun.COM  * Use is subject to license terms.
292*9126SWyllys.Ingersoll@Sun.COM  */
293*9126SWyllys.Ingersoll@Sun.COM 
294*9126SWyllys.Ingersoll@Sun.COM #include "tpmtok_int.h"
295*9126SWyllys.Ingersoll@Sun.COM 
296*9126SWyllys.Ingersoll@Sun.COM SESSION *
session_mgr_find(CK_SESSION_HANDLE handle)297*9126SWyllys.Ingersoll@Sun.COM session_mgr_find(CK_SESSION_HANDLE handle)
298*9126SWyllys.Ingersoll@Sun.COM {
299*9126SWyllys.Ingersoll@Sun.COM 	DL_NODE  * node   = NULL;
300*9126SWyllys.Ingersoll@Sun.COM 	SESSION  * result = NULL;
301*9126SWyllys.Ingersoll@Sun.COM 	CK_RV	rc;
302*9126SWyllys.Ingersoll@Sun.COM 
303*9126SWyllys.Ingersoll@Sun.COM 	rc = pthread_mutex_lock(&sess_list_mutex);
304*9126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
305*9126SWyllys.Ingersoll@Sun.COM 		return (NULL);
306*9126SWyllys.Ingersoll@Sun.COM 	}
307*9126SWyllys.Ingersoll@Sun.COM 	node = sess_list;
308*9126SWyllys.Ingersoll@Sun.COM 
309*9126SWyllys.Ingersoll@Sun.COM 	while (node) {
310*9126SWyllys.Ingersoll@Sun.COM 		SESSION *s = (SESSION *)node->data;
311*9126SWyllys.Ingersoll@Sun.COM 
312*9126SWyllys.Ingersoll@Sun.COM 		if (s->handle == handle) {
313*9126SWyllys.Ingersoll@Sun.COM 			result = s;
314*9126SWyllys.Ingersoll@Sun.COM 			break;
315*9126SWyllys.Ingersoll@Sun.COM 		}
316*9126SWyllys.Ingersoll@Sun.COM 
317*9126SWyllys.Ingersoll@Sun.COM 		node = node->next;
318*9126SWyllys.Ingersoll@Sun.COM 	}
319*9126SWyllys.Ingersoll@Sun.COM 
320*9126SWyllys.Ingersoll@Sun.COM 	(void) pthread_mutex_unlock(&sess_list_mutex);
321*9126SWyllys.Ingersoll@Sun.COM 	return (result);
322*9126SWyllys.Ingersoll@Sun.COM }
323*9126SWyllys.Ingersoll@Sun.COM 
324*9126SWyllys.Ingersoll@Sun.COM 
325*9126SWyllys.Ingersoll@Sun.COM /*
326*9126SWyllys.Ingersoll@Sun.COM  * session_mgr_new()
327*9126SWyllys.Ingersoll@Sun.COM  *
328*9126SWyllys.Ingersoll@Sun.COM  * creates a new session structure and adds it to the process's list
329*9126SWyllys.Ingersoll@Sun.COM  * of sessions
330*9126SWyllys.Ingersoll@Sun.COM  *
331*9126SWyllys.Ingersoll@Sun.COM  * Args:  CK_ULONG	flags : session flags		   (INPUT)
332*9126SWyllys.Ingersoll@Sun.COM  *	SESSION **	sess : new session pointer		(OUTPUT)
333*9126SWyllys.Ingersoll@Sun.COM  *
334*9126SWyllys.Ingersoll@Sun.COM  * Returns:  CK_RV
335*9126SWyllys.Ingersoll@Sun.COM  */
336*9126SWyllys.Ingersoll@Sun.COM CK_RV
session_mgr_new(CK_ULONG flags,SESSION ** sess)337*9126SWyllys.Ingersoll@Sun.COM session_mgr_new(CK_ULONG flags, SESSION **sess)
338*9126SWyllys.Ingersoll@Sun.COM {
339*9126SWyllys.Ingersoll@Sun.COM 	SESSION  * new_session  = NULL;
340*9126SWyllys.Ingersoll@Sun.COM 	SESSION  * s	    = NULL;
341*9126SWyllys.Ingersoll@Sun.COM 	DL_NODE  * node	 = NULL;
342*9126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL   user_session = FALSE;
343*9126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL   so_session   = FALSE;
344*9126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL   pkcs_locked  = TRUE;
345*9126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL   sess_locked  = TRUE;
346*9126SWyllys.Ingersoll@Sun.COM 	CK_RV	rc;
347*9126SWyllys.Ingersoll@Sun.COM 
348*9126SWyllys.Ingersoll@Sun.COM 	new_session = (SESSION *)malloc(sizeof (SESSION));
349*9126SWyllys.Ingersoll@Sun.COM 	if (! new_session) {
350*9126SWyllys.Ingersoll@Sun.COM 		rc = CKR_HOST_MEMORY;
351*9126SWyllys.Ingersoll@Sun.COM 		goto done;
352*9126SWyllys.Ingersoll@Sun.COM 	}
353*9126SWyllys.Ingersoll@Sun.COM 
354*9126SWyllys.Ingersoll@Sun.COM 	(void) memset(new_session, 0x0, sizeof (SESSION));
355*9126SWyllys.Ingersoll@Sun.COM 
356*9126SWyllys.Ingersoll@Sun.COM 	rc = pthread_mutex_lock(&pkcs_mutex);
357*9126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
358*9126SWyllys.Ingersoll@Sun.COM 		return (rc);
359*9126SWyllys.Ingersoll@Sun.COM 	}
360*9126SWyllys.Ingersoll@Sun.COM 	pkcs_locked = TRUE;
361*9126SWyllys.Ingersoll@Sun.COM 
362*9126SWyllys.Ingersoll@Sun.COM 	do {
363*9126SWyllys.Ingersoll@Sun.COM 		s = session_mgr_find(next_session_handle);
364*9126SWyllys.Ingersoll@Sun.COM 		if (s != NULL)
365*9126SWyllys.Ingersoll@Sun.COM 			next_session_handle++;
366*9126SWyllys.Ingersoll@Sun.COM 		else
367*9126SWyllys.Ingersoll@Sun.COM 			new_session->handle = next_session_handle++;
368*9126SWyllys.Ingersoll@Sun.COM 	} while (s != NULL);
369*9126SWyllys.Ingersoll@Sun.COM 
370*9126SWyllys.Ingersoll@Sun.COM 	(void) pthread_mutex_unlock(&pkcs_mutex);
371*9126SWyllys.Ingersoll@Sun.COM 	pkcs_locked = FALSE;
372*9126SWyllys.Ingersoll@Sun.COM 
373*9126SWyllys.Ingersoll@Sun.COM 
374*9126SWyllys.Ingersoll@Sun.COM 	new_session->session_info.slotID	= 1;
375*9126SWyllys.Ingersoll@Sun.COM 	new_session->session_info.flags	 = flags;
376*9126SWyllys.Ingersoll@Sun.COM 	new_session->session_info.ulDeviceError = 0;
377*9126SWyllys.Ingersoll@Sun.COM 
378*9126SWyllys.Ingersoll@Sun.COM 	rc = pthread_mutex_lock(&sess_list_mutex);
379*9126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
380*9126SWyllys.Ingersoll@Sun.COM 		return (rc);
381*9126SWyllys.Ingersoll@Sun.COM 	}
382*9126SWyllys.Ingersoll@Sun.COM 	sess_locked = TRUE;
383*9126SWyllys.Ingersoll@Sun.COM 
384*9126SWyllys.Ingersoll@Sun.COM 	node = sess_list;
385*9126SWyllys.Ingersoll@Sun.COM 	while (node) {
386*9126SWyllys.Ingersoll@Sun.COM 		SESSION *s = (SESSION *)node->data;
387*9126SWyllys.Ingersoll@Sun.COM 		if (s->session_info.state == CKS_RW_SO_FUNCTIONS) {
388*9126SWyllys.Ingersoll@Sun.COM 			so_session = TRUE;
389*9126SWyllys.Ingersoll@Sun.COM 			break;
390*9126SWyllys.Ingersoll@Sun.COM 		}
391*9126SWyllys.Ingersoll@Sun.COM 
392*9126SWyllys.Ingersoll@Sun.COM 		if ((s->session_info.state == CKS_RO_USER_FUNCTIONS) ||
393*9126SWyllys.Ingersoll@Sun.COM 		    (s->session_info.state == CKS_RW_USER_FUNCTIONS)) {
394*9126SWyllys.Ingersoll@Sun.COM 			user_session = TRUE;
395*9126SWyllys.Ingersoll@Sun.COM 			break;
396*9126SWyllys.Ingersoll@Sun.COM 		}
397*9126SWyllys.Ingersoll@Sun.COM 
398*9126SWyllys.Ingersoll@Sun.COM 		node = node->next;
399*9126SWyllys.Ingersoll@Sun.COM 	}
400*9126SWyllys.Ingersoll@Sun.COM 
401*9126SWyllys.Ingersoll@Sun.COM 	if (global_login_state == CKS_RW_SO_FUNCTIONS) {
402*9126SWyllys.Ingersoll@Sun.COM 		so_session = TRUE;
403*9126SWyllys.Ingersoll@Sun.COM 	}
404*9126SWyllys.Ingersoll@Sun.COM 	if ((global_login_state == CKS_RO_USER_FUNCTIONS) ||
405*9126SWyllys.Ingersoll@Sun.COM 	    (global_login_state == CKS_RW_USER_FUNCTIONS)) {
406*9126SWyllys.Ingersoll@Sun.COM 		user_session = TRUE;
407*9126SWyllys.Ingersoll@Sun.COM 	}
408*9126SWyllys.Ingersoll@Sun.COM 
409*9126SWyllys.Ingersoll@Sun.COM 	if (user_session) {
410*9126SWyllys.Ingersoll@Sun.COM 		if (new_session->session_info.flags & CKF_RW_SESSION)
411*9126SWyllys.Ingersoll@Sun.COM 			new_session->session_info.state = CKS_RW_USER_FUNCTIONS;
412*9126SWyllys.Ingersoll@Sun.COM 		else
413*9126SWyllys.Ingersoll@Sun.COM 			new_session->session_info.state = CKS_RO_USER_FUNCTIONS;
414*9126SWyllys.Ingersoll@Sun.COM 		} else if (so_session) {
415*9126SWyllys.Ingersoll@Sun.COM 
416*9126SWyllys.Ingersoll@Sun.COM 		new_session->session_info.state = CKS_RW_SO_FUNCTIONS;
417*9126SWyllys.Ingersoll@Sun.COM 	} else {
418*9126SWyllys.Ingersoll@Sun.COM 		if (new_session->session_info.flags & CKF_RW_SESSION)
419*9126SWyllys.Ingersoll@Sun.COM 			new_session->session_info.state = CKS_RW_PUBLIC_SESSION;
420*9126SWyllys.Ingersoll@Sun.COM 		else
421*9126SWyllys.Ingersoll@Sun.COM 			new_session->session_info.state = CKS_RO_PUBLIC_SESSION;
422*9126SWyllys.Ingersoll@Sun.COM 	}
423*9126SWyllys.Ingersoll@Sun.COM 
424*9126SWyllys.Ingersoll@Sun.COM 	sess_list = dlist_add_as_first(sess_list, new_session);
425*9126SWyllys.Ingersoll@Sun.COM 	*sess = new_session;
426*9126SWyllys.Ingersoll@Sun.COM 
427*9126SWyllys.Ingersoll@Sun.COM 	done:
428*9126SWyllys.Ingersoll@Sun.COM 	if (pkcs_locked)
429*9126SWyllys.Ingersoll@Sun.COM 		(void) pthread_mutex_unlock(&pkcs_mutex);
430*9126SWyllys.Ingersoll@Sun.COM 
431*9126SWyllys.Ingersoll@Sun.COM 	if (sess_locked)
432*9126SWyllys.Ingersoll@Sun.COM 		(void) pthread_mutex_unlock(&sess_list_mutex);
433*9126SWyllys.Ingersoll@Sun.COM 
434*9126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK && new_session != NULL) {
435*9126SWyllys.Ingersoll@Sun.COM 		free(new_session);
436*9126SWyllys.Ingersoll@Sun.COM 	}
437*9126SWyllys.Ingersoll@Sun.COM 	return (rc);
438*9126SWyllys.Ingersoll@Sun.COM }
439*9126SWyllys.Ingersoll@Sun.COM 
440*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL
session_mgr_so_session_exists(void)441*9126SWyllys.Ingersoll@Sun.COM session_mgr_so_session_exists(void)
442*9126SWyllys.Ingersoll@Sun.COM {
443*9126SWyllys.Ingersoll@Sun.COM 	DL_NODE *node = NULL;
444*9126SWyllys.Ingersoll@Sun.COM 	CK_RV    rc;
445*9126SWyllys.Ingersoll@Sun.COM 
446*9126SWyllys.Ingersoll@Sun.COM 	rc = pthread_mutex_lock(&sess_list_mutex);
447*9126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
448*9126SWyllys.Ingersoll@Sun.COM 		return (rc);
449*9126SWyllys.Ingersoll@Sun.COM 	}
450*9126SWyllys.Ingersoll@Sun.COM 	node = sess_list;
451*9126SWyllys.Ingersoll@Sun.COM 	while (node) {
452*9126SWyllys.Ingersoll@Sun.COM 		SESSION *s = (SESSION *)node->data;
453*9126SWyllys.Ingersoll@Sun.COM 		if (s->session_info.state == CKS_RW_SO_FUNCTIONS) {
454*9126SWyllys.Ingersoll@Sun.COM 			rc = TRUE;
455*9126SWyllys.Ingersoll@Sun.COM 			goto done;
456*9126SWyllys.Ingersoll@Sun.COM 		}
457*9126SWyllys.Ingersoll@Sun.COM 
458*9126SWyllys.Ingersoll@Sun.COM 		node = node->next;
459*9126SWyllys.Ingersoll@Sun.COM 	}
460*9126SWyllys.Ingersoll@Sun.COM 
461*9126SWyllys.Ingersoll@Sun.COM 	rc = FALSE;
462*9126SWyllys.Ingersoll@Sun.COM 
463*9126SWyllys.Ingersoll@Sun.COM done:
464*9126SWyllys.Ingersoll@Sun.COM 	(void) pthread_mutex_unlock(&sess_list_mutex);
465*9126SWyllys.Ingersoll@Sun.COM 	return (rc);
466*9126SWyllys.Ingersoll@Sun.COM }
467*9126SWyllys.Ingersoll@Sun.COM 
468*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL
session_mgr_user_session_exists(void)469*9126SWyllys.Ingersoll@Sun.COM session_mgr_user_session_exists(void)
470*9126SWyllys.Ingersoll@Sun.COM {
471*9126SWyllys.Ingersoll@Sun.COM 	DL_NODE *node = NULL;
472*9126SWyllys.Ingersoll@Sun.COM 	CK_RV    rc;
473*9126SWyllys.Ingersoll@Sun.COM 
474*9126SWyllys.Ingersoll@Sun.COM 	rc = pthread_mutex_lock(&sess_list_mutex);
475*9126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
476*9126SWyllys.Ingersoll@Sun.COM 		return (rc);
477*9126SWyllys.Ingersoll@Sun.COM 	}
478*9126SWyllys.Ingersoll@Sun.COM 	node = sess_list;
479*9126SWyllys.Ingersoll@Sun.COM 	while (node) {
480*9126SWyllys.Ingersoll@Sun.COM 		SESSION *s = (SESSION *)node->data;
481*9126SWyllys.Ingersoll@Sun.COM 		if ((s->session_info.state == CKS_RO_USER_FUNCTIONS) ||
482*9126SWyllys.Ingersoll@Sun.COM 		    (s->session_info.state == CKS_RW_USER_FUNCTIONS)) {
483*9126SWyllys.Ingersoll@Sun.COM 			rc = TRUE;
484*9126SWyllys.Ingersoll@Sun.COM 			goto done;
485*9126SWyllys.Ingersoll@Sun.COM 		}
486*9126SWyllys.Ingersoll@Sun.COM 
487*9126SWyllys.Ingersoll@Sun.COM 		node = node->next;
488*9126SWyllys.Ingersoll@Sun.COM 	}
489*9126SWyllys.Ingersoll@Sun.COM 
490*9126SWyllys.Ingersoll@Sun.COM 	rc = FALSE;
491*9126SWyllys.Ingersoll@Sun.COM 
492*9126SWyllys.Ingersoll@Sun.COM done:
493*9126SWyllys.Ingersoll@Sun.COM 	(void) pthread_mutex_unlock(&sess_list_mutex);
494*9126SWyllys.Ingersoll@Sun.COM 	return (rc);
495*9126SWyllys.Ingersoll@Sun.COM }
496*9126SWyllys.Ingersoll@Sun.COM 
497*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL
session_mgr_public_session_exists(void)498*9126SWyllys.Ingersoll@Sun.COM session_mgr_public_session_exists(void)
499*9126SWyllys.Ingersoll@Sun.COM {
500*9126SWyllys.Ingersoll@Sun.COM 	DL_NODE *node = NULL;
501*9126SWyllys.Ingersoll@Sun.COM 	CK_RV    rc;
502*9126SWyllys.Ingersoll@Sun.COM 
503*9126SWyllys.Ingersoll@Sun.COM 	rc = pthread_mutex_lock(&sess_list_mutex);
504*9126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
505*9126SWyllys.Ingersoll@Sun.COM 		return (rc);
506*9126SWyllys.Ingersoll@Sun.COM 	}
507*9126SWyllys.Ingersoll@Sun.COM 	node = sess_list;
508*9126SWyllys.Ingersoll@Sun.COM 	while (node) {
509*9126SWyllys.Ingersoll@Sun.COM 		SESSION *s = (SESSION *)node->data;
510*9126SWyllys.Ingersoll@Sun.COM 		if ((s->session_info.state == CKS_RO_PUBLIC_SESSION) ||
511*9126SWyllys.Ingersoll@Sun.COM 		    (s->session_info.state == CKS_RW_PUBLIC_SESSION)) {
512*9126SWyllys.Ingersoll@Sun.COM 			rc = TRUE;
513*9126SWyllys.Ingersoll@Sun.COM 			goto done;
514*9126SWyllys.Ingersoll@Sun.COM 		}
515*9126SWyllys.Ingersoll@Sun.COM 
516*9126SWyllys.Ingersoll@Sun.COM 		node = node->next;
517*9126SWyllys.Ingersoll@Sun.COM 	}
518*9126SWyllys.Ingersoll@Sun.COM 
519*9126SWyllys.Ingersoll@Sun.COM 	rc = FALSE;
520*9126SWyllys.Ingersoll@Sun.COM 
521*9126SWyllys.Ingersoll@Sun.COM done:
522*9126SWyllys.Ingersoll@Sun.COM 	(void) pthread_mutex_unlock(&sess_list_mutex);
523*9126SWyllys.Ingersoll@Sun.COM 	return (rc);
524*9126SWyllys.Ingersoll@Sun.COM }
525*9126SWyllys.Ingersoll@Sun.COM 
526*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL
session_mgr_readonly_exists(void)527*9126SWyllys.Ingersoll@Sun.COM session_mgr_readonly_exists(void)
528*9126SWyllys.Ingersoll@Sun.COM {
529*9126SWyllys.Ingersoll@Sun.COM 	DL_NODE *node = NULL;
530*9126SWyllys.Ingersoll@Sun.COM 	CK_RV    rc;
531*9126SWyllys.Ingersoll@Sun.COM 
532*9126SWyllys.Ingersoll@Sun.COM 	rc = pthread_mutex_lock(&sess_list_mutex);
533*9126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
534*9126SWyllys.Ingersoll@Sun.COM 		return (rc);
535*9126SWyllys.Ingersoll@Sun.COM 	}
536*9126SWyllys.Ingersoll@Sun.COM 	node = sess_list;
537*9126SWyllys.Ingersoll@Sun.COM 	while (node) {
538*9126SWyllys.Ingersoll@Sun.COM 		SESSION *s = (SESSION *)node->data;
539*9126SWyllys.Ingersoll@Sun.COM 		if ((s->session_info.flags & CKF_RW_SESSION) == 0) {
540*9126SWyllys.Ingersoll@Sun.COM 			rc = TRUE;
541*9126SWyllys.Ingersoll@Sun.COM 			goto done;
542*9126SWyllys.Ingersoll@Sun.COM 		}
543*9126SWyllys.Ingersoll@Sun.COM 
544*9126SWyllys.Ingersoll@Sun.COM 		node = node->next;
545*9126SWyllys.Ingersoll@Sun.COM 	}
546*9126SWyllys.Ingersoll@Sun.COM 
547*9126SWyllys.Ingersoll@Sun.COM 	rc = FALSE;
548*9126SWyllys.Ingersoll@Sun.COM 
549*9126SWyllys.Ingersoll@Sun.COM done:
550*9126SWyllys.Ingersoll@Sun.COM 	(void) pthread_mutex_unlock(&sess_list_mutex);
551*9126SWyllys.Ingersoll@Sun.COM 	return (rc);
552*9126SWyllys.Ingersoll@Sun.COM }
553*9126SWyllys.Ingersoll@Sun.COM 
554*9126SWyllys.Ingersoll@Sun.COM CK_RV
session_mgr_close_session(SESSION * sess)555*9126SWyllys.Ingersoll@Sun.COM session_mgr_close_session(SESSION *sess)
556*9126SWyllys.Ingersoll@Sun.COM {
557*9126SWyllys.Ingersoll@Sun.COM 	DL_NODE  * node = NULL;
558*9126SWyllys.Ingersoll@Sun.COM 	CK_RV	rc = CKR_OK;
559*9126SWyllys.Ingersoll@Sun.COM 
560*9126SWyllys.Ingersoll@Sun.COM 	if (! sess)
561*9126SWyllys.Ingersoll@Sun.COM 		return (FALSE);
562*9126SWyllys.Ingersoll@Sun.COM 	rc = pthread_mutex_lock(&sess_list_mutex);
563*9126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
564*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
565*9126SWyllys.Ingersoll@Sun.COM 	}
566*9126SWyllys.Ingersoll@Sun.COM 	node = dlist_find(sess_list, sess);
567*9126SWyllys.Ingersoll@Sun.COM 	if (! node) {
568*9126SWyllys.Ingersoll@Sun.COM 		rc = CKR_FUNCTION_FAILED;
569*9126SWyllys.Ingersoll@Sun.COM 		goto done;
570*9126SWyllys.Ingersoll@Sun.COM 	}
571*9126SWyllys.Ingersoll@Sun.COM 
572*9126SWyllys.Ingersoll@Sun.COM 	(void) object_mgr_purge_session_objects(sess, ALL);
573*9126SWyllys.Ingersoll@Sun.COM 
574*9126SWyllys.Ingersoll@Sun.COM 	if (sess->find_list)
575*9126SWyllys.Ingersoll@Sun.COM 		free(sess->find_list);
576*9126SWyllys.Ingersoll@Sun.COM 
577*9126SWyllys.Ingersoll@Sun.COM 	if (sess->encr_ctx.context)
578*9126SWyllys.Ingersoll@Sun.COM 		free(sess->encr_ctx.context);
579*9126SWyllys.Ingersoll@Sun.COM 
580*9126SWyllys.Ingersoll@Sun.COM 	if (sess->encr_ctx.mech.pParameter)
581*9126SWyllys.Ingersoll@Sun.COM 		free(sess->encr_ctx.mech.pParameter);
582*9126SWyllys.Ingersoll@Sun.COM 
583*9126SWyllys.Ingersoll@Sun.COM 	if (sess->decr_ctx.context)
584*9126SWyllys.Ingersoll@Sun.COM 		free(sess->decr_ctx.context);
585*9126SWyllys.Ingersoll@Sun.COM 
586*9126SWyllys.Ingersoll@Sun.COM 	if (sess->decr_ctx.mech.pParameter)
587*9126SWyllys.Ingersoll@Sun.COM 		free(sess->decr_ctx.mech.pParameter);
588*9126SWyllys.Ingersoll@Sun.COM 
589*9126SWyllys.Ingersoll@Sun.COM 	if (sess->digest_ctx.context.ref)
590*9126SWyllys.Ingersoll@Sun.COM 		free(sess->digest_ctx.context.ref);
591*9126SWyllys.Ingersoll@Sun.COM 
592*9126SWyllys.Ingersoll@Sun.COM 	if (sess->digest_ctx.mech.pParameter)
593*9126SWyllys.Ingersoll@Sun.COM 		free(sess->digest_ctx.mech.pParameter);
594*9126SWyllys.Ingersoll@Sun.COM 
595*9126SWyllys.Ingersoll@Sun.COM 	if (sess->sign_ctx.context)
596*9126SWyllys.Ingersoll@Sun.COM 		free(sess->sign_ctx.context);
597*9126SWyllys.Ingersoll@Sun.COM 
598*9126SWyllys.Ingersoll@Sun.COM 	if (sess->sign_ctx.mech.pParameter)
599*9126SWyllys.Ingersoll@Sun.COM 		free(sess->sign_ctx.mech.pParameter);
600*9126SWyllys.Ingersoll@Sun.COM 
601*9126SWyllys.Ingersoll@Sun.COM 	if (sess->verify_ctx.context)
602*9126SWyllys.Ingersoll@Sun.COM 		free(sess->verify_ctx.context);
603*9126SWyllys.Ingersoll@Sun.COM 
604*9126SWyllys.Ingersoll@Sun.COM 	if (sess->verify_ctx.mech.pParameter)
605*9126SWyllys.Ingersoll@Sun.COM 		free(sess->verify_ctx.mech.pParameter);
606*9126SWyllys.Ingersoll@Sun.COM 
607*9126SWyllys.Ingersoll@Sun.COM 	if (sess->hContext)
608*9126SWyllys.Ingersoll@Sun.COM 		(void) Tspi_Context_Close(sess->hContext);
609*9126SWyllys.Ingersoll@Sun.COM 
610*9126SWyllys.Ingersoll@Sun.COM 	free(sess);
611*9126SWyllys.Ingersoll@Sun.COM 
612*9126SWyllys.Ingersoll@Sun.COM 	sess_list = dlist_remove_node(sess_list, node);
613*9126SWyllys.Ingersoll@Sun.COM 
614*9126SWyllys.Ingersoll@Sun.COM 	if (sess_list == NULL) {
615*9126SWyllys.Ingersoll@Sun.COM 		TSS_HCONTEXT hContext;
616*9126SWyllys.Ingersoll@Sun.COM 		if (open_tss_context(&hContext) == 0) {
617*9126SWyllys.Ingersoll@Sun.COM 			(void) object_mgr_purge_private_token_objects(hContext);
618*9126SWyllys.Ingersoll@Sun.COM 			(void) Tspi_Context_Close(hContext);
619*9126SWyllys.Ingersoll@Sun.COM 		}
620*9126SWyllys.Ingersoll@Sun.COM 
621*9126SWyllys.Ingersoll@Sun.COM 		global_login_state = 0;
622*9126SWyllys.Ingersoll@Sun.COM 
623*9126SWyllys.Ingersoll@Sun.COM 		(void) pthread_mutex_lock(&obj_list_mutex);
624*9126SWyllys.Ingersoll@Sun.COM 		(void) object_mgr_purge_map((SESSION *)0xFFFF, PRIVATE);
625*9126SWyllys.Ingersoll@Sun.COM 		(void) pthread_mutex_unlock(&obj_list_mutex);
626*9126SWyllys.Ingersoll@Sun.COM 	}
627*9126SWyllys.Ingersoll@Sun.COM 
628*9126SWyllys.Ingersoll@Sun.COM 	done:
629*9126SWyllys.Ingersoll@Sun.COM 	(void) pthread_mutex_unlock(&sess_list_mutex);
630*9126SWyllys.Ingersoll@Sun.COM 	return (rc);
631*9126SWyllys.Ingersoll@Sun.COM }
632*9126SWyllys.Ingersoll@Sun.COM 
633*9126SWyllys.Ingersoll@Sun.COM // session_mgr_close_all_sessions()
634*9126SWyllys.Ingersoll@Sun.COM //
635*9126SWyllys.Ingersoll@Sun.COM // removes all sessions from the specified process
636*9126SWyllys.Ingersoll@Sun.COM //
637*9126SWyllys.Ingersoll@Sun.COM CK_RV
session_mgr_close_all_sessions(void)638*9126SWyllys.Ingersoll@Sun.COM session_mgr_close_all_sessions(void)
639*9126SWyllys.Ingersoll@Sun.COM {
640*9126SWyllys.Ingersoll@Sun.COM 	CK_RV   rc = CKR_OK;
641*9126SWyllys.Ingersoll@Sun.COM 
642*9126SWyllys.Ingersoll@Sun.COM 	rc = pthread_mutex_lock(&sess_list_mutex);
643*9126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
644*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
645*9126SWyllys.Ingersoll@Sun.COM 	}
646*9126SWyllys.Ingersoll@Sun.COM 	while (sess_list) {
647*9126SWyllys.Ingersoll@Sun.COM 		SESSION *sess = (SESSION *)sess_list->data;
648*9126SWyllys.Ingersoll@Sun.COM 
649*9126SWyllys.Ingersoll@Sun.COM 		(void) object_mgr_purge_session_objects(sess, ALL);
650*9126SWyllys.Ingersoll@Sun.COM 
651*9126SWyllys.Ingersoll@Sun.COM 		if (sess->find_list)
652*9126SWyllys.Ingersoll@Sun.COM 			free(sess->find_list);
653*9126SWyllys.Ingersoll@Sun.COM 
654*9126SWyllys.Ingersoll@Sun.COM 		if (sess->encr_ctx.context)
655*9126SWyllys.Ingersoll@Sun.COM 			free(sess->encr_ctx.context);
656*9126SWyllys.Ingersoll@Sun.COM 
657*9126SWyllys.Ingersoll@Sun.COM 		if (sess->encr_ctx.mech.pParameter)
658*9126SWyllys.Ingersoll@Sun.COM 			free(sess->encr_ctx.mech.pParameter);
659*9126SWyllys.Ingersoll@Sun.COM 
660*9126SWyllys.Ingersoll@Sun.COM 		if (sess->decr_ctx.context)
661*9126SWyllys.Ingersoll@Sun.COM 			free(sess->decr_ctx.context);
662*9126SWyllys.Ingersoll@Sun.COM 
663*9126SWyllys.Ingersoll@Sun.COM 		if (sess->decr_ctx.mech.pParameter)
664*9126SWyllys.Ingersoll@Sun.COM 			free(sess->decr_ctx.mech.pParameter);
665*9126SWyllys.Ingersoll@Sun.COM 
666*9126SWyllys.Ingersoll@Sun.COM 		if (sess->digest_ctx.context.ref)
667*9126SWyllys.Ingersoll@Sun.COM 			free(sess->digest_ctx.context.ref);
668*9126SWyllys.Ingersoll@Sun.COM 
669*9126SWyllys.Ingersoll@Sun.COM 		if (sess->digest_ctx.mech.pParameter)
670*9126SWyllys.Ingersoll@Sun.COM 			free(sess->digest_ctx.mech.pParameter);
671*9126SWyllys.Ingersoll@Sun.COM 
672*9126SWyllys.Ingersoll@Sun.COM 		if (sess->sign_ctx.context)
673*9126SWyllys.Ingersoll@Sun.COM 			free(sess->sign_ctx.context);
674*9126SWyllys.Ingersoll@Sun.COM 
675*9126SWyllys.Ingersoll@Sun.COM 		if (sess->sign_ctx.mech.pParameter)
676*9126SWyllys.Ingersoll@Sun.COM 			free(sess->sign_ctx.mech.pParameter);
677*9126SWyllys.Ingersoll@Sun.COM 
678*9126SWyllys.Ingersoll@Sun.COM 		if (sess->verify_ctx.context)
679*9126SWyllys.Ingersoll@Sun.COM 			free(sess->verify_ctx.context);
680*9126SWyllys.Ingersoll@Sun.COM 
681*9126SWyllys.Ingersoll@Sun.COM 		if (sess->verify_ctx.mech.pParameter)
682*9126SWyllys.Ingersoll@Sun.COM 			free(sess->verify_ctx.mech.pParameter);
683*9126SWyllys.Ingersoll@Sun.COM 
684*9126SWyllys.Ingersoll@Sun.COM 		if (sess->hContext)
685*9126SWyllys.Ingersoll@Sun.COM 			(void) Tspi_Context_Close(sess->hContext);
686*9126SWyllys.Ingersoll@Sun.COM 
687*9126SWyllys.Ingersoll@Sun.COM 		free(sess);
688*9126SWyllys.Ingersoll@Sun.COM 
689*9126SWyllys.Ingersoll@Sun.COM 		sess_list = dlist_remove_node(sess_list, sess_list);
690*9126SWyllys.Ingersoll@Sun.COM 	}
691*9126SWyllys.Ingersoll@Sun.COM 
692*9126SWyllys.Ingersoll@Sun.COM 	(void) pthread_mutex_unlock(&sess_list_mutex);
693*9126SWyllys.Ingersoll@Sun.COM 	return (CKR_OK);
694*9126SWyllys.Ingersoll@Sun.COM }
695*9126SWyllys.Ingersoll@Sun.COM 
696*9126SWyllys.Ingersoll@Sun.COM // session_mgr_login_all()
697*9126SWyllys.Ingersoll@Sun.COM //
698*9126SWyllys.Ingersoll@Sun.COM // changes the login status of all sessions in the token
699*9126SWyllys.Ingersoll@Sun.COM //
700*9126SWyllys.Ingersoll@Sun.COM // Arg:  CK_USER_TYPE  user_type : USER or SO
701*9126SWyllys.Ingersoll@Sun.COM //
702*9126SWyllys.Ingersoll@Sun.COM CK_RV
session_mgr_login_all(CK_USER_TYPE user_type)703*9126SWyllys.Ingersoll@Sun.COM session_mgr_login_all(CK_USER_TYPE user_type) {
704*9126SWyllys.Ingersoll@Sun.COM 	DL_NODE  * node = NULL;
705*9126SWyllys.Ingersoll@Sun.COM 	CK_RV	rc = CKR_OK;
706*9126SWyllys.Ingersoll@Sun.COM 
707*9126SWyllys.Ingersoll@Sun.COM 	rc = pthread_mutex_lock(&sess_list_mutex);
708*9126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
709*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
710*9126SWyllys.Ingersoll@Sun.COM 	}
711*9126SWyllys.Ingersoll@Sun.COM 	node = sess_list;
712*9126SWyllys.Ingersoll@Sun.COM 	while (node) {
713*9126SWyllys.Ingersoll@Sun.COM 		SESSION *s = (SESSION *)node->data;
714*9126SWyllys.Ingersoll@Sun.COM 
715*9126SWyllys.Ingersoll@Sun.COM 		if (s->session_info.flags & CKF_RW_SESSION) {
716*9126SWyllys.Ingersoll@Sun.COM 			if (user_type == CKU_USER)
717*9126SWyllys.Ingersoll@Sun.COM 				s->session_info.state = CKS_RW_USER_FUNCTIONS;
718*9126SWyllys.Ingersoll@Sun.COM 			else
719*9126SWyllys.Ingersoll@Sun.COM 				s->session_info.state = CKS_RW_SO_FUNCTIONS;
720*9126SWyllys.Ingersoll@Sun.COM 		} else {
721*9126SWyllys.Ingersoll@Sun.COM 			if (user_type == CKU_USER)
722*9126SWyllys.Ingersoll@Sun.COM 				s->session_info.state = CKS_RO_USER_FUNCTIONS;
723*9126SWyllys.Ingersoll@Sun.COM 		}
724*9126SWyllys.Ingersoll@Sun.COM 
725*9126SWyllys.Ingersoll@Sun.COM 		global_login_state = s->session_info.state;
726*9126SWyllys.Ingersoll@Sun.COM 		node = node->next;
727*9126SWyllys.Ingersoll@Sun.COM 	}
728*9126SWyllys.Ingersoll@Sun.COM 
729*9126SWyllys.Ingersoll@Sun.COM 	(void) pthread_mutex_unlock(&sess_list_mutex);
730*9126SWyllys.Ingersoll@Sun.COM 	return (CKR_OK);
731*9126SWyllys.Ingersoll@Sun.COM }
732*9126SWyllys.Ingersoll@Sun.COM 
733*9126SWyllys.Ingersoll@Sun.COM CK_RV
session_mgr_logout_all(void)734*9126SWyllys.Ingersoll@Sun.COM session_mgr_logout_all(void) {
735*9126SWyllys.Ingersoll@Sun.COM 	DL_NODE  * node = NULL;
736*9126SWyllys.Ingersoll@Sun.COM 	SESSION  * s    = NULL;
737*9126SWyllys.Ingersoll@Sun.COM 	CK_RV	rc   = CKR_OK;
738*9126SWyllys.Ingersoll@Sun.COM 
739*9126SWyllys.Ingersoll@Sun.COM 	rc = pthread_mutex_lock(&sess_list_mutex);
740*9126SWyllys.Ingersoll@Sun.COM 	if (rc != CKR_OK) {
741*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
742*9126SWyllys.Ingersoll@Sun.COM 	}
743*9126SWyllys.Ingersoll@Sun.COM 	node = sess_list;
744*9126SWyllys.Ingersoll@Sun.COM 	while (node) {
745*9126SWyllys.Ingersoll@Sun.COM 		s = (SESSION *)node->data;
746*9126SWyllys.Ingersoll@Sun.COM 
747*9126SWyllys.Ingersoll@Sun.COM 		(void) object_mgr_purge_session_objects(s, PRIVATE);
748*9126SWyllys.Ingersoll@Sun.COM 
749*9126SWyllys.Ingersoll@Sun.COM 		if (s->session_info.flags & CKF_RW_SESSION)
750*9126SWyllys.Ingersoll@Sun.COM 			s->session_info.state = CKS_RW_PUBLIC_SESSION;
751*9126SWyllys.Ingersoll@Sun.COM 		else
752*9126SWyllys.Ingersoll@Sun.COM 			s->session_info.state = CKS_RO_PUBLIC_SESSION;
753*9126SWyllys.Ingersoll@Sun.COM 
754*9126SWyllys.Ingersoll@Sun.COM 		global_login_state = s->session_info.state; // SAB
755*9126SWyllys.Ingersoll@Sun.COM 
756*9126SWyllys.Ingersoll@Sun.COM 		node = node->next;
757*9126SWyllys.Ingersoll@Sun.COM 	}
758*9126SWyllys.Ingersoll@Sun.COM 
759*9126SWyllys.Ingersoll@Sun.COM 	(void) pthread_mutex_unlock(&sess_list_mutex);
760*9126SWyllys.Ingersoll@Sun.COM 	return (CKR_OK);
761*9126SWyllys.Ingersoll@Sun.COM }
762*9126SWyllys.Ingersoll@Sun.COM 
763*9126SWyllys.Ingersoll@Sun.COM CK_RV
session_mgr_get_op_state(SESSION * sess,CK_BBOOL length_only,CK_BYTE * data,CK_ULONG * data_len)764*9126SWyllys.Ingersoll@Sun.COM session_mgr_get_op_state(SESSION   *sess,
765*9126SWyllys.Ingersoll@Sun.COM 	CK_BBOOL   length_only,
766*9126SWyllys.Ingersoll@Sun.COM 	CK_BYTE   *data,
767*9126SWyllys.Ingersoll@Sun.COM 	CK_ULONG  *data_len) {
768*9126SWyllys.Ingersoll@Sun.COM 	OP_STATE_DATA  *op_data = NULL;
769*9126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	op_data_len = 0;
770*9126SWyllys.Ingersoll@Sun.COM 	CK_ULONG	offset;
771*9126SWyllys.Ingersoll@Sun.COM 	void		*dptr = data;
772*9126SWyllys.Ingersoll@Sun.COM 
773*9126SWyllys.Ingersoll@Sun.COM 	if (! sess) {
774*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
775*9126SWyllys.Ingersoll@Sun.COM 	}
776*9126SWyllys.Ingersoll@Sun.COM 
777*9126SWyllys.Ingersoll@Sun.COM 	if (sess->find_active == TRUE) {
778*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_STATE_UNSAVEABLE);
779*9126SWyllys.Ingersoll@Sun.COM 	}
780*9126SWyllys.Ingersoll@Sun.COM 	if (sess->encr_ctx.active == TRUE) {
781*9126SWyllys.Ingersoll@Sun.COM 		if (op_data != NULL) {
782*9126SWyllys.Ingersoll@Sun.COM 			return (CKR_STATE_UNSAVEABLE);
783*9126SWyllys.Ingersoll@Sun.COM 		}
784*9126SWyllys.Ingersoll@Sun.COM 		op_data_len = sizeof (OP_STATE_DATA)	+
785*9126SWyllys.Ingersoll@Sun.COM 		    sizeof (ENCR_DECR_CONTEXT)  +
786*9126SWyllys.Ingersoll@Sun.COM 		    sess->encr_ctx.context_len +
787*9126SWyllys.Ingersoll@Sun.COM 		    sess->encr_ctx.mech.ulParameterLen;
788*9126SWyllys.Ingersoll@Sun.COM 
789*9126SWyllys.Ingersoll@Sun.COM 		if (length_only == FALSE) {
790*9126SWyllys.Ingersoll@Sun.COM 			op_data = (OP_STATE_DATA *)dptr;
791*9126SWyllys.Ingersoll@Sun.COM 
792*9126SWyllys.Ingersoll@Sun.COM 			op_data->data_len = op_data_len -
793*9126SWyllys.Ingersoll@Sun.COM 			    sizeof (OP_STATE_DATA);
794*9126SWyllys.Ingersoll@Sun.COM 			op_data->session_state = sess->session_info.state;
795*9126SWyllys.Ingersoll@Sun.COM 			op_data->active_operation = STATE_ENCR;
796*9126SWyllys.Ingersoll@Sun.COM 
797*9126SWyllys.Ingersoll@Sun.COM 			offset = sizeof (OP_STATE_DATA);
798*9126SWyllys.Ingersoll@Sun.COM 
799*9126SWyllys.Ingersoll@Sun.COM 			(void) (void) memcpy((CK_BYTE *)op_data + offset,
800*9126SWyllys.Ingersoll@Sun.COM 			&sess->encr_ctx,
801*9126SWyllys.Ingersoll@Sun.COM 			sizeof (ENCR_DECR_CONTEXT));
802*9126SWyllys.Ingersoll@Sun.COM 
803*9126SWyllys.Ingersoll@Sun.COM 			offset += sizeof (ENCR_DECR_CONTEXT);
804*9126SWyllys.Ingersoll@Sun.COM 
805*9126SWyllys.Ingersoll@Sun.COM 			if (sess->encr_ctx.context_len != 0) {
806*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy((CK_BYTE *)op_data + offset,
807*9126SWyllys.Ingersoll@Sun.COM 				sess->encr_ctx.context,
808*9126SWyllys.Ingersoll@Sun.COM 				sess->encr_ctx.context_len);
809*9126SWyllys.Ingersoll@Sun.COM 
810*9126SWyllys.Ingersoll@Sun.COM 				offset += sess->encr_ctx.context_len;
811*9126SWyllys.Ingersoll@Sun.COM 			}
812*9126SWyllys.Ingersoll@Sun.COM 
813*9126SWyllys.Ingersoll@Sun.COM 			if (sess->encr_ctx.mech.ulParameterLen != 0) {
814*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy((CK_BYTE *)op_data + offset,
815*9126SWyllys.Ingersoll@Sun.COM 				sess->encr_ctx.mech.pParameter,
816*9126SWyllys.Ingersoll@Sun.COM 				sess->encr_ctx.mech.ulParameterLen);
817*9126SWyllys.Ingersoll@Sun.COM 			}
818*9126SWyllys.Ingersoll@Sun.COM 		}
819*9126SWyllys.Ingersoll@Sun.COM 	}
820*9126SWyllys.Ingersoll@Sun.COM 
821*9126SWyllys.Ingersoll@Sun.COM 	if (sess->decr_ctx.active == TRUE) {
822*9126SWyllys.Ingersoll@Sun.COM 		if (op_data != NULL) {
823*9126SWyllys.Ingersoll@Sun.COM 			return (CKR_STATE_UNSAVEABLE);
824*9126SWyllys.Ingersoll@Sun.COM 		}
825*9126SWyllys.Ingersoll@Sun.COM 		op_data_len = sizeof (OP_STATE_DATA)	+
826*9126SWyllys.Ingersoll@Sun.COM 		    sizeof (ENCR_DECR_CONTEXT)  +
827*9126SWyllys.Ingersoll@Sun.COM 		    sess->decr_ctx.context_len +
828*9126SWyllys.Ingersoll@Sun.COM 		    sess->decr_ctx.mech.ulParameterLen;
829*9126SWyllys.Ingersoll@Sun.COM 
830*9126SWyllys.Ingersoll@Sun.COM 		if (length_only == FALSE) {
831*9126SWyllys.Ingersoll@Sun.COM 			op_data = (OP_STATE_DATA *)dptr;
832*9126SWyllys.Ingersoll@Sun.COM 
833*9126SWyllys.Ingersoll@Sun.COM 			op_data->data_len = op_data_len -
834*9126SWyllys.Ingersoll@Sun.COM 			    sizeof (OP_STATE_DATA);
835*9126SWyllys.Ingersoll@Sun.COM 			op_data->session_state    = sess->session_info.state;
836*9126SWyllys.Ingersoll@Sun.COM 			op_data->active_operation = STATE_DECR;
837*9126SWyllys.Ingersoll@Sun.COM 
838*9126SWyllys.Ingersoll@Sun.COM 			offset = sizeof (OP_STATE_DATA);
839*9126SWyllys.Ingersoll@Sun.COM 
840*9126SWyllys.Ingersoll@Sun.COM 			(void) memcpy((CK_BYTE *)op_data + offset,
841*9126SWyllys.Ingersoll@Sun.COM 			&sess->decr_ctx,
842*9126SWyllys.Ingersoll@Sun.COM 			sizeof (ENCR_DECR_CONTEXT));
843*9126SWyllys.Ingersoll@Sun.COM 
844*9126SWyllys.Ingersoll@Sun.COM 			offset += sizeof (ENCR_DECR_CONTEXT);
845*9126SWyllys.Ingersoll@Sun.COM 
846*9126SWyllys.Ingersoll@Sun.COM 			if (sess->decr_ctx.context_len != 0) {
847*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy((CK_BYTE *)op_data + offset,
848*9126SWyllys.Ingersoll@Sun.COM 				sess->decr_ctx.context,
849*9126SWyllys.Ingersoll@Sun.COM 				sess->decr_ctx.context_len);
850*9126SWyllys.Ingersoll@Sun.COM 
851*9126SWyllys.Ingersoll@Sun.COM 				offset += sess->decr_ctx.context_len;
852*9126SWyllys.Ingersoll@Sun.COM 			}
853*9126SWyllys.Ingersoll@Sun.COM 
854*9126SWyllys.Ingersoll@Sun.COM 			if (sess->decr_ctx.mech.ulParameterLen != 0) {
855*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy((CK_BYTE *)op_data + offset,
856*9126SWyllys.Ingersoll@Sun.COM 				sess->decr_ctx.mech.pParameter,
857*9126SWyllys.Ingersoll@Sun.COM 				sess->decr_ctx.mech.ulParameterLen);
858*9126SWyllys.Ingersoll@Sun.COM 			}
859*9126SWyllys.Ingersoll@Sun.COM 		}
860*9126SWyllys.Ingersoll@Sun.COM 	}
861*9126SWyllys.Ingersoll@Sun.COM 
862*9126SWyllys.Ingersoll@Sun.COM 	if (sess->digest_ctx.active == TRUE) {
863*9126SWyllys.Ingersoll@Sun.COM 		if (op_data != NULL) {
864*9126SWyllys.Ingersoll@Sun.COM 			return (CKR_STATE_UNSAVEABLE);
865*9126SWyllys.Ingersoll@Sun.COM 		}
866*9126SWyllys.Ingersoll@Sun.COM 		op_data_len = sizeof (OP_STATE_DATA) +
867*9126SWyllys.Ingersoll@Sun.COM 		    sizeof (DIGEST_CONTEXT) +
868*9126SWyllys.Ingersoll@Sun.COM 		    sess->digest_ctx.context_len +
869*9126SWyllys.Ingersoll@Sun.COM 		    sess->digest_ctx.mech.ulParameterLen;
870*9126SWyllys.Ingersoll@Sun.COM 
871*9126SWyllys.Ingersoll@Sun.COM 		if (length_only == FALSE) {
872*9126SWyllys.Ingersoll@Sun.COM 			op_data = (OP_STATE_DATA *)dptr;
873*9126SWyllys.Ingersoll@Sun.COM 
874*9126SWyllys.Ingersoll@Sun.COM 			op_data->data_len = op_data_len -
875*9126SWyllys.Ingersoll@Sun.COM 			    sizeof (OP_STATE_DATA);
876*9126SWyllys.Ingersoll@Sun.COM 			op_data->session_state    = sess->session_info.state;
877*9126SWyllys.Ingersoll@Sun.COM 			op_data->active_operation = STATE_DIGEST;
878*9126SWyllys.Ingersoll@Sun.COM 
879*9126SWyllys.Ingersoll@Sun.COM 			offset = sizeof (OP_STATE_DATA);
880*9126SWyllys.Ingersoll@Sun.COM 
881*9126SWyllys.Ingersoll@Sun.COM 			(void) memcpy((CK_BYTE *)op_data + offset,
882*9126SWyllys.Ingersoll@Sun.COM 			    &sess->digest_ctx, sizeof (DIGEST_CONTEXT));
883*9126SWyllys.Ingersoll@Sun.COM 
884*9126SWyllys.Ingersoll@Sun.COM 			offset += sizeof (DIGEST_CONTEXT);
885*9126SWyllys.Ingersoll@Sun.COM 
886*9126SWyllys.Ingersoll@Sun.COM 			if (sess->digest_ctx.context_len != 0) {
887*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy((CK_BYTE *)op_data + offset,
888*9126SWyllys.Ingersoll@Sun.COM 				    sess->digest_ctx.context.ref,
889*9126SWyllys.Ingersoll@Sun.COM 				    sess->digest_ctx.context_len);
890*9126SWyllys.Ingersoll@Sun.COM 
891*9126SWyllys.Ingersoll@Sun.COM 				offset += sess->digest_ctx.context_len;
892*9126SWyllys.Ingersoll@Sun.COM 			}
893*9126SWyllys.Ingersoll@Sun.COM 
894*9126SWyllys.Ingersoll@Sun.COM 			if (sess->digest_ctx.mech.ulParameterLen != 0) {
895*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy((CK_BYTE *)op_data + offset,
896*9126SWyllys.Ingersoll@Sun.COM 				    sess->digest_ctx.mech.pParameter,
897*9126SWyllys.Ingersoll@Sun.COM 				    sess->digest_ctx.mech.ulParameterLen);
898*9126SWyllys.Ingersoll@Sun.COM 			}
899*9126SWyllys.Ingersoll@Sun.COM 		}
900*9126SWyllys.Ingersoll@Sun.COM 	}
901*9126SWyllys.Ingersoll@Sun.COM 
902*9126SWyllys.Ingersoll@Sun.COM 	if (sess->sign_ctx.active == TRUE) {
903*9126SWyllys.Ingersoll@Sun.COM 		if (op_data != NULL) {
904*9126SWyllys.Ingersoll@Sun.COM 			return (CKR_STATE_UNSAVEABLE);
905*9126SWyllys.Ingersoll@Sun.COM 		}
906*9126SWyllys.Ingersoll@Sun.COM 		op_data_len = sizeof (OP_STATE_DATA) +
907*9126SWyllys.Ingersoll@Sun.COM 		    sizeof (SIGN_VERIFY_CONTEXT) +
908*9126SWyllys.Ingersoll@Sun.COM 		    sess->sign_ctx.context_len +
909*9126SWyllys.Ingersoll@Sun.COM 		    sess->sign_ctx.mech.ulParameterLen;
910*9126SWyllys.Ingersoll@Sun.COM 
911*9126SWyllys.Ingersoll@Sun.COM 		if (length_only == FALSE) {
912*9126SWyllys.Ingersoll@Sun.COM 			op_data = (OP_STATE_DATA *)dptr;
913*9126SWyllys.Ingersoll@Sun.COM 
914*9126SWyllys.Ingersoll@Sun.COM 			op_data->data_len = op_data_len -
915*9126SWyllys.Ingersoll@Sun.COM 			    sizeof (OP_STATE_DATA);
916*9126SWyllys.Ingersoll@Sun.COM 			op_data->session_state    = sess->session_info.state;
917*9126SWyllys.Ingersoll@Sun.COM 			op_data->active_operation = STATE_SIGN;
918*9126SWyllys.Ingersoll@Sun.COM 
919*9126SWyllys.Ingersoll@Sun.COM 			offset = sizeof (OP_STATE_DATA);
920*9126SWyllys.Ingersoll@Sun.COM 
921*9126SWyllys.Ingersoll@Sun.COM 			(void) memcpy((CK_BYTE *)op_data + offset,
922*9126SWyllys.Ingersoll@Sun.COM 			    &sess->sign_ctx, sizeof (SIGN_VERIFY_CONTEXT));
923*9126SWyllys.Ingersoll@Sun.COM 
924*9126SWyllys.Ingersoll@Sun.COM 			offset += sizeof (SIGN_VERIFY_CONTEXT);
925*9126SWyllys.Ingersoll@Sun.COM 
926*9126SWyllys.Ingersoll@Sun.COM 			if (sess->sign_ctx.context_len != 0) {
927*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy((CK_BYTE *)op_data + offset,
928*9126SWyllys.Ingersoll@Sun.COM 				    sess->sign_ctx.context,
929*9126SWyllys.Ingersoll@Sun.COM 				    sess->sign_ctx.context_len);
930*9126SWyllys.Ingersoll@Sun.COM 
931*9126SWyllys.Ingersoll@Sun.COM 				offset += sess->sign_ctx.context_len;
932*9126SWyllys.Ingersoll@Sun.COM 			}
933*9126SWyllys.Ingersoll@Sun.COM 
934*9126SWyllys.Ingersoll@Sun.COM 			if (sess->sign_ctx.mech.ulParameterLen != 0) {
935*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy((CK_BYTE *)op_data + offset,
936*9126SWyllys.Ingersoll@Sun.COM 				    sess->sign_ctx.mech.pParameter,
937*9126SWyllys.Ingersoll@Sun.COM 				    sess->sign_ctx.mech.ulParameterLen);
938*9126SWyllys.Ingersoll@Sun.COM 			}
939*9126SWyllys.Ingersoll@Sun.COM 		}
940*9126SWyllys.Ingersoll@Sun.COM 	}
941*9126SWyllys.Ingersoll@Sun.COM 
942*9126SWyllys.Ingersoll@Sun.COM 	if (sess->verify_ctx.active == TRUE) {
943*9126SWyllys.Ingersoll@Sun.COM 		if (op_data != NULL) {
944*9126SWyllys.Ingersoll@Sun.COM 			return (CKR_STATE_UNSAVEABLE);
945*9126SWyllys.Ingersoll@Sun.COM 		}
946*9126SWyllys.Ingersoll@Sun.COM 		op_data_len = sizeof (OP_STATE_DATA)	+
947*9126SWyllys.Ingersoll@Sun.COM 		    sizeof (SIGN_VERIFY_CONTEXT)  +
948*9126SWyllys.Ingersoll@Sun.COM 		    sess->verify_ctx.context_len +
949*9126SWyllys.Ingersoll@Sun.COM 		    sess->verify_ctx.mech.ulParameterLen;
950*9126SWyllys.Ingersoll@Sun.COM 
951*9126SWyllys.Ingersoll@Sun.COM 		if (length_only == FALSE) {
952*9126SWyllys.Ingersoll@Sun.COM 			op_data = (OP_STATE_DATA *)dptr;
953*9126SWyllys.Ingersoll@Sun.COM 
954*9126SWyllys.Ingersoll@Sun.COM 			op_data->data_len = op_data_len -
955*9126SWyllys.Ingersoll@Sun.COM 			    sizeof (OP_STATE_DATA);
956*9126SWyllys.Ingersoll@Sun.COM 			op_data->session_state    = sess->session_info.state;
957*9126SWyllys.Ingersoll@Sun.COM 			op_data->active_operation = STATE_SIGN;
958*9126SWyllys.Ingersoll@Sun.COM 
959*9126SWyllys.Ingersoll@Sun.COM 			offset = sizeof (OP_STATE_DATA);
960*9126SWyllys.Ingersoll@Sun.COM 
961*9126SWyllys.Ingersoll@Sun.COM 			(void) memcpy((CK_BYTE *)op_data + offset,
962*9126SWyllys.Ingersoll@Sun.COM 			    &sess->verify_ctx, sizeof (SIGN_VERIFY_CONTEXT));
963*9126SWyllys.Ingersoll@Sun.COM 
964*9126SWyllys.Ingersoll@Sun.COM 			offset += sizeof (SIGN_VERIFY_CONTEXT);
965*9126SWyllys.Ingersoll@Sun.COM 
966*9126SWyllys.Ingersoll@Sun.COM 			if (sess->verify_ctx.context_len != 0) {
967*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy((CK_BYTE *)op_data + offset,
968*9126SWyllys.Ingersoll@Sun.COM 				    sess->verify_ctx.context,
969*9126SWyllys.Ingersoll@Sun.COM 				    sess->verify_ctx.context_len);
970*9126SWyllys.Ingersoll@Sun.COM 
971*9126SWyllys.Ingersoll@Sun.COM 				offset += sess->verify_ctx.context_len;
972*9126SWyllys.Ingersoll@Sun.COM 			}
973*9126SWyllys.Ingersoll@Sun.COM 
974*9126SWyllys.Ingersoll@Sun.COM 			if (sess->verify_ctx.mech.ulParameterLen != 0) {
975*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy((CK_BYTE *)op_data + offset,
976*9126SWyllys.Ingersoll@Sun.COM 				    sess->verify_ctx.mech.pParameter,
977*9126SWyllys.Ingersoll@Sun.COM 				    sess->verify_ctx.mech.ulParameterLen);
978*9126SWyllys.Ingersoll@Sun.COM 			}
979*9126SWyllys.Ingersoll@Sun.COM 		}
980*9126SWyllys.Ingersoll@Sun.COM 	}
981*9126SWyllys.Ingersoll@Sun.COM 
982*9126SWyllys.Ingersoll@Sun.COM 	*data_len = op_data_len;
983*9126SWyllys.Ingersoll@Sun.COM 	return (CKR_OK);
984*9126SWyllys.Ingersoll@Sun.COM }
985*9126SWyllys.Ingersoll@Sun.COM 
986*9126SWyllys.Ingersoll@Sun.COM CK_RV
session_mgr_set_op_state(SESSION * sess,CK_OBJECT_HANDLE encr_key,CK_OBJECT_HANDLE auth_key,CK_BYTE * data)987*9126SWyllys.Ingersoll@Sun.COM session_mgr_set_op_state(
988*9126SWyllys.Ingersoll@Sun.COM 	SESSION	*sess,
989*9126SWyllys.Ingersoll@Sun.COM 	CK_OBJECT_HANDLE encr_key,
990*9126SWyllys.Ingersoll@Sun.COM 	CK_OBJECT_HANDLE auth_key,
991*9126SWyllys.Ingersoll@Sun.COM 	CK_BYTE	*data)
992*9126SWyllys.Ingersoll@Sun.COM {
993*9126SWyllys.Ingersoll@Sun.COM 	OP_STATE_DATA  *op_data = NULL;
994*9126SWyllys.Ingersoll@Sun.COM 	CK_BYTE	*mech_param = NULL;
995*9126SWyllys.Ingersoll@Sun.COM 	CK_BYTE	*context  = NULL;
996*9126SWyllys.Ingersoll@Sun.COM 	CK_BYTE	*ptr1	= NULL;
997*9126SWyllys.Ingersoll@Sun.COM 	CK_BYTE	*ptr2	= NULL;
998*9126SWyllys.Ingersoll@Sun.COM 	CK_BYTE	*ptr3	= NULL;
999*9126SWyllys.Ingersoll@Sun.COM 	CK_ULONG len;
1000*9126SWyllys.Ingersoll@Sun.COM 	void	*dptr = data;
1001*9126SWyllys.Ingersoll@Sun.COM 
1002*9126SWyllys.Ingersoll@Sun.COM 	if (! sess || ! data) {
1003*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_FUNCTION_FAILED);
1004*9126SWyllys.Ingersoll@Sun.COM 	}
1005*9126SWyllys.Ingersoll@Sun.COM 	op_data = (OP_STATE_DATA *)dptr;
1006*9126SWyllys.Ingersoll@Sun.COM 
1007*9126SWyllys.Ingersoll@Sun.COM 	if (sess->session_info.state != op_data->session_state) {
1008*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_SAVED_STATE_INVALID);
1009*9126SWyllys.Ingersoll@Sun.COM 	}
1010*9126SWyllys.Ingersoll@Sun.COM 	switch (op_data->active_operation) {
1011*9126SWyllys.Ingersoll@Sun.COM 		case STATE_ENCR:
1012*9126SWyllys.Ingersoll@Sun.COM 		case STATE_DECR:
1013*9126SWyllys.Ingersoll@Sun.COM 		{
1014*9126SWyllys.Ingersoll@Sun.COM 			void *cptr = data + sizeof (OP_STATE_DATA);
1015*9126SWyllys.Ingersoll@Sun.COM 			ENCR_DECR_CONTEXT *ctx = (ENCR_DECR_CONTEXT *)cptr;
1016*9126SWyllys.Ingersoll@Sun.COM 
1017*9126SWyllys.Ingersoll@Sun.COM 			len = sizeof (ENCR_DECR_CONTEXT) +
1018*9126SWyllys.Ingersoll@Sun.COM 			    ctx->context_len + ctx->mech.ulParameterLen;
1019*9126SWyllys.Ingersoll@Sun.COM 			if (len != op_data->data_len) {
1020*9126SWyllys.Ingersoll@Sun.COM 				return (CKR_SAVED_STATE_INVALID);
1021*9126SWyllys.Ingersoll@Sun.COM 			}
1022*9126SWyllys.Ingersoll@Sun.COM 			if (auth_key != 0) {
1023*9126SWyllys.Ingersoll@Sun.COM 				return (CKR_KEY_NOT_NEEDED);
1024*9126SWyllys.Ingersoll@Sun.COM 			}
1025*9126SWyllys.Ingersoll@Sun.COM 			if (encr_key == 0) {
1026*9126SWyllys.Ingersoll@Sun.COM 				return (CKR_KEY_NEEDED);
1027*9126SWyllys.Ingersoll@Sun.COM 			}
1028*9126SWyllys.Ingersoll@Sun.COM 			ptr1 = (CK_BYTE *)ctx;
1029*9126SWyllys.Ingersoll@Sun.COM 			ptr2 = ptr1 + sizeof (ENCR_DECR_CONTEXT);
1030*9126SWyllys.Ingersoll@Sun.COM 			ptr3 = ptr2 + ctx->context_len;
1031*9126SWyllys.Ingersoll@Sun.COM 
1032*9126SWyllys.Ingersoll@Sun.COM 			if (ctx->context_len) {
1033*9126SWyllys.Ingersoll@Sun.COM 				context = (CK_BYTE *)malloc(ctx->context_len);
1034*9126SWyllys.Ingersoll@Sun.COM 				if (! context) {
1035*9126SWyllys.Ingersoll@Sun.COM 					return (CKR_HOST_MEMORY);
1036*9126SWyllys.Ingersoll@Sun.COM 				}
1037*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy(context, ptr2, ctx->context_len);
1038*9126SWyllys.Ingersoll@Sun.COM 			}
1039*9126SWyllys.Ingersoll@Sun.COM 
1040*9126SWyllys.Ingersoll@Sun.COM 			if (ctx->mech.ulParameterLen) {
1041*9126SWyllys.Ingersoll@Sun.COM 				mech_param = (CK_BYTE *)malloc(
1042*9126SWyllys.Ingersoll@Sun.COM 				    ctx->mech.ulParameterLen);
1043*9126SWyllys.Ingersoll@Sun.COM 				if (! mech_param) {
1044*9126SWyllys.Ingersoll@Sun.COM 					if (context)
1045*9126SWyllys.Ingersoll@Sun.COM 						free(context);
1046*9126SWyllys.Ingersoll@Sun.COM 					return (CKR_HOST_MEMORY);
1047*9126SWyllys.Ingersoll@Sun.COM 				}
1048*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy(mech_param, ptr3,
1049*9126SWyllys.Ingersoll@Sun.COM 				    ctx->mech.ulParameterLen);
1050*9126SWyllys.Ingersoll@Sun.COM 			}
1051*9126SWyllys.Ingersoll@Sun.COM 		}
1052*9126SWyllys.Ingersoll@Sun.COM 		break;
1053*9126SWyllys.Ingersoll@Sun.COM 
1054*9126SWyllys.Ingersoll@Sun.COM 		case STATE_SIGN:
1055*9126SWyllys.Ingersoll@Sun.COM 		case STATE_VERIFY:
1056*9126SWyllys.Ingersoll@Sun.COM 		{
1057*9126SWyllys.Ingersoll@Sun.COM 			void *cptr = data + sizeof (OP_STATE_DATA);
1058*9126SWyllys.Ingersoll@Sun.COM 			SIGN_VERIFY_CONTEXT *ctx = (SIGN_VERIFY_CONTEXT *)cptr;
1059*9126SWyllys.Ingersoll@Sun.COM 
1060*9126SWyllys.Ingersoll@Sun.COM 			len = sizeof (SIGN_VERIFY_CONTEXT) +
1061*9126SWyllys.Ingersoll@Sun.COM 			    ctx->context_len + ctx->mech.ulParameterLen;
1062*9126SWyllys.Ingersoll@Sun.COM 			if (len != op_data->data_len) {
1063*9126SWyllys.Ingersoll@Sun.COM 				return (CKR_SAVED_STATE_INVALID);
1064*9126SWyllys.Ingersoll@Sun.COM 			}
1065*9126SWyllys.Ingersoll@Sun.COM 			if (auth_key == 0) {
1066*9126SWyllys.Ingersoll@Sun.COM 				return (CKR_KEY_NEEDED);
1067*9126SWyllys.Ingersoll@Sun.COM 			}
1068*9126SWyllys.Ingersoll@Sun.COM 			if (encr_key != 0) {
1069*9126SWyllys.Ingersoll@Sun.COM 				return (CKR_KEY_NOT_NEEDED);
1070*9126SWyllys.Ingersoll@Sun.COM 			}
1071*9126SWyllys.Ingersoll@Sun.COM 			ptr1 = (CK_BYTE *)ctx;
1072*9126SWyllys.Ingersoll@Sun.COM 			ptr2 = ptr1 + sizeof (SIGN_VERIFY_CONTEXT);
1073*9126SWyllys.Ingersoll@Sun.COM 			ptr3 = ptr2 + ctx->context_len;
1074*9126SWyllys.Ingersoll@Sun.COM 
1075*9126SWyllys.Ingersoll@Sun.COM 			if (ctx->context_len) {
1076*9126SWyllys.Ingersoll@Sun.COM 				context = (CK_BYTE *)malloc(ctx->context_len);
1077*9126SWyllys.Ingersoll@Sun.COM 				if (! context) {
1078*9126SWyllys.Ingersoll@Sun.COM 					return (CKR_HOST_MEMORY);
1079*9126SWyllys.Ingersoll@Sun.COM 				}
1080*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy(context, ptr2, ctx->context_len);
1081*9126SWyllys.Ingersoll@Sun.COM 			}
1082*9126SWyllys.Ingersoll@Sun.COM 
1083*9126SWyllys.Ingersoll@Sun.COM 			if (ctx->mech.ulParameterLen) {
1084*9126SWyllys.Ingersoll@Sun.COM 				mech_param = (CK_BYTE *)malloc(
1085*9126SWyllys.Ingersoll@Sun.COM 				    ctx->mech.ulParameterLen);
1086*9126SWyllys.Ingersoll@Sun.COM 				if (! mech_param) {
1087*9126SWyllys.Ingersoll@Sun.COM 					if (context)
1088*9126SWyllys.Ingersoll@Sun.COM 						free(context);
1089*9126SWyllys.Ingersoll@Sun.COM 					return (CKR_HOST_MEMORY);
1090*9126SWyllys.Ingersoll@Sun.COM 				}
1091*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy(mech_param, ptr3,
1092*9126SWyllys.Ingersoll@Sun.COM 				    ctx->mech.ulParameterLen);
1093*9126SWyllys.Ingersoll@Sun.COM 			}
1094*9126SWyllys.Ingersoll@Sun.COM 		}
1095*9126SWyllys.Ingersoll@Sun.COM 		break;
1096*9126SWyllys.Ingersoll@Sun.COM 
1097*9126SWyllys.Ingersoll@Sun.COM 		case STATE_DIGEST:
1098*9126SWyllys.Ingersoll@Sun.COM 		{
1099*9126SWyllys.Ingersoll@Sun.COM 			void *cptr = data + sizeof (OP_STATE_DATA);
1100*9126SWyllys.Ingersoll@Sun.COM 			DIGEST_CONTEXT *ctx = (DIGEST_CONTEXT *)cptr;
1101*9126SWyllys.Ingersoll@Sun.COM 
1102*9126SWyllys.Ingersoll@Sun.COM 			len = sizeof (DIGEST_CONTEXT) +
1103*9126SWyllys.Ingersoll@Sun.COM 			    ctx->context_len + ctx->mech.ulParameterLen;
1104*9126SWyllys.Ingersoll@Sun.COM 			if (len != op_data->data_len) {
1105*9126SWyllys.Ingersoll@Sun.COM 				return (CKR_SAVED_STATE_INVALID);
1106*9126SWyllys.Ingersoll@Sun.COM 			}
1107*9126SWyllys.Ingersoll@Sun.COM 			if (auth_key != 0) {
1108*9126SWyllys.Ingersoll@Sun.COM 				return (CKR_KEY_NOT_NEEDED);
1109*9126SWyllys.Ingersoll@Sun.COM 			}
1110*9126SWyllys.Ingersoll@Sun.COM 			if (encr_key != 0) {
1111*9126SWyllys.Ingersoll@Sun.COM 				return (CKR_KEY_NOT_NEEDED);
1112*9126SWyllys.Ingersoll@Sun.COM 			}
1113*9126SWyllys.Ingersoll@Sun.COM 			ptr1 = (CK_BYTE *)ctx;
1114*9126SWyllys.Ingersoll@Sun.COM 			ptr2 = ptr1 + sizeof (DIGEST_CONTEXT);
1115*9126SWyllys.Ingersoll@Sun.COM 			ptr3 = ptr2 + ctx->context_len;
1116*9126SWyllys.Ingersoll@Sun.COM 
1117*9126SWyllys.Ingersoll@Sun.COM 			if (ctx->context_len) {
1118*9126SWyllys.Ingersoll@Sun.COM 				context = (CK_BYTE *)malloc(ctx->context_len);
1119*9126SWyllys.Ingersoll@Sun.COM 				if (! context) {
1120*9126SWyllys.Ingersoll@Sun.COM 					return (CKR_HOST_MEMORY);
1121*9126SWyllys.Ingersoll@Sun.COM 				}
1122*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy(context, ptr2, ctx->context_len);
1123*9126SWyllys.Ingersoll@Sun.COM 			}
1124*9126SWyllys.Ingersoll@Sun.COM 
1125*9126SWyllys.Ingersoll@Sun.COM 			if (ctx->mech.ulParameterLen) {
1126*9126SWyllys.Ingersoll@Sun.COM 				mech_param = (CK_BYTE *)malloc(
1127*9126SWyllys.Ingersoll@Sun.COM 				    ctx->mech.ulParameterLen);
1128*9126SWyllys.Ingersoll@Sun.COM 				if (! mech_param) {
1129*9126SWyllys.Ingersoll@Sun.COM 					if (context)
1130*9126SWyllys.Ingersoll@Sun.COM 						free(context);
1131*9126SWyllys.Ingersoll@Sun.COM 					return (CKR_HOST_MEMORY);
1132*9126SWyllys.Ingersoll@Sun.COM 				}
1133*9126SWyllys.Ingersoll@Sun.COM 				(void) memcpy(mech_param, ptr3,
1134*9126SWyllys.Ingersoll@Sun.COM 				    ctx->mech.ulParameterLen);
1135*9126SWyllys.Ingersoll@Sun.COM 			}
1136*9126SWyllys.Ingersoll@Sun.COM 		}
1137*9126SWyllys.Ingersoll@Sun.COM 		break;
1138*9126SWyllys.Ingersoll@Sun.COM 
1139*9126SWyllys.Ingersoll@Sun.COM 		default:
1140*9126SWyllys.Ingersoll@Sun.COM 		return (CKR_SAVED_STATE_INVALID);
1141*9126SWyllys.Ingersoll@Sun.COM 	}
1142*9126SWyllys.Ingersoll@Sun.COM 
1143*9126SWyllys.Ingersoll@Sun.COM 
1144*9126SWyllys.Ingersoll@Sun.COM 	if (sess->encr_ctx.active)
1145*9126SWyllys.Ingersoll@Sun.COM 		(void) encr_mgr_cleanup(&sess->encr_ctx);
1146*9126SWyllys.Ingersoll@Sun.COM 
1147*9126SWyllys.Ingersoll@Sun.COM 	if (sess->decr_ctx.active)
1148*9126SWyllys.Ingersoll@Sun.COM 		(void) decr_mgr_cleanup(&sess->decr_ctx);
1149*9126SWyllys.Ingersoll@Sun.COM 
1150*9126SWyllys.Ingersoll@Sun.COM 	if (sess->digest_ctx.active)
1151*9126SWyllys.Ingersoll@Sun.COM 		(void) digest_mgr_cleanup(&sess->digest_ctx);
1152*9126SWyllys.Ingersoll@Sun.COM 
1153*9126SWyllys.Ingersoll@Sun.COM 	if (sess->sign_ctx.active)
1154*9126SWyllys.Ingersoll@Sun.COM 		(void) sign_mgr_cleanup(&sess->sign_ctx);
1155*9126SWyllys.Ingersoll@Sun.COM 
1156*9126SWyllys.Ingersoll@Sun.COM 	if (sess->verify_ctx.active)
1157*9126SWyllys.Ingersoll@Sun.COM 		(void) verify_mgr_cleanup(&sess->verify_ctx);
1158*9126SWyllys.Ingersoll@Sun.COM 
1159*9126SWyllys.Ingersoll@Sun.COM 	switch (op_data->active_operation) {
1160*9126SWyllys.Ingersoll@Sun.COM 		case STATE_ENCR:
1161*9126SWyllys.Ingersoll@Sun.COM 		(void) memcpy(&sess->encr_ctx, ptr1,
1162*9126SWyllys.Ingersoll@Sun.COM 		    sizeof (ENCR_DECR_CONTEXT));
1163*9126SWyllys.Ingersoll@Sun.COM 
1164*9126SWyllys.Ingersoll@Sun.COM 		sess->encr_ctx.key	= encr_key;
1165*9126SWyllys.Ingersoll@Sun.COM 		sess->encr_ctx.context	= context;
1166*9126SWyllys.Ingersoll@Sun.COM 		sess->encr_ctx.mech.pParameter = mech_param;
1167*9126SWyllys.Ingersoll@Sun.COM 		break;
1168*9126SWyllys.Ingersoll@Sun.COM 
1169*9126SWyllys.Ingersoll@Sun.COM 		case STATE_DECR:
1170*9126SWyllys.Ingersoll@Sun.COM 		(void) memcpy(&sess->decr_ctx, ptr1,
1171*9126SWyllys.Ingersoll@Sun.COM 		    sizeof (ENCR_DECR_CONTEXT));
1172*9126SWyllys.Ingersoll@Sun.COM 
1173*9126SWyllys.Ingersoll@Sun.COM 		sess->decr_ctx.key = encr_key;
1174*9126SWyllys.Ingersoll@Sun.COM 		sess->decr_ctx.context = context;
1175*9126SWyllys.Ingersoll@Sun.COM 		sess->decr_ctx.mech.pParameter = mech_param;
1176*9126SWyllys.Ingersoll@Sun.COM 		break;
1177*9126SWyllys.Ingersoll@Sun.COM 
1178*9126SWyllys.Ingersoll@Sun.COM 		case STATE_SIGN:
1179*9126SWyllys.Ingersoll@Sun.COM 		(void) memcpy(&sess->sign_ctx, ptr1,
1180*9126SWyllys.Ingersoll@Sun.COM 		    sizeof (SIGN_VERIFY_CONTEXT));
1181*9126SWyllys.Ingersoll@Sun.COM 
1182*9126SWyllys.Ingersoll@Sun.COM 		sess->sign_ctx.key	= auth_key;
1183*9126SWyllys.Ingersoll@Sun.COM 		sess->sign_ctx.context  = context;
1184*9126SWyllys.Ingersoll@Sun.COM 		sess->sign_ctx.mech.pParameter = mech_param;
1185*9126SWyllys.Ingersoll@Sun.COM 		break;
1186*9126SWyllys.Ingersoll@Sun.COM 
1187*9126SWyllys.Ingersoll@Sun.COM 		case STATE_VERIFY:
1188*9126SWyllys.Ingersoll@Sun.COM 		(void) memcpy(&sess->verify_ctx, ptr1,
1189*9126SWyllys.Ingersoll@Sun.COM 		    sizeof (SIGN_VERIFY_CONTEXT));
1190*9126SWyllys.Ingersoll@Sun.COM 
1191*9126SWyllys.Ingersoll@Sun.COM 		sess->verify_ctx.key = auth_key;
1192*9126SWyllys.Ingersoll@Sun.COM 		sess->verify_ctx.context = context;
1193*9126SWyllys.Ingersoll@Sun.COM 		sess->verify_ctx.mech.pParameter = mech_param;
1194*9126SWyllys.Ingersoll@Sun.COM 		break;
1195*9126SWyllys.Ingersoll@Sun.COM 
1196*9126SWyllys.Ingersoll@Sun.COM 		case STATE_DIGEST:
1197*9126SWyllys.Ingersoll@Sun.COM 		(void) memcpy(&sess->digest_ctx, ptr1,
1198*9126SWyllys.Ingersoll@Sun.COM 		    sizeof (DIGEST_CONTEXT));
1199*9126SWyllys.Ingersoll@Sun.COM 
1200*9126SWyllys.Ingersoll@Sun.COM 		sess->digest_ctx.context.ref = context;
1201*9126SWyllys.Ingersoll@Sun.COM 		sess->digest_ctx.mech.pParameter = mech_param;
1202*9126SWyllys.Ingersoll@Sun.COM 		break;
1203*9126SWyllys.Ingersoll@Sun.COM 	}
1204*9126SWyllys.Ingersoll@Sun.COM 
1205*9126SWyllys.Ingersoll@Sun.COM 	return (CKR_OK);
1206*9126SWyllys.Ingersoll@Sun.COM }
1207*9126SWyllys.Ingersoll@Sun.COM 
1208*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL
pin_expired(CK_SESSION_INFO * si,CK_FLAGS flags)1209*9126SWyllys.Ingersoll@Sun.COM pin_expired(CK_SESSION_INFO *si, CK_FLAGS flags)
1210*9126SWyllys.Ingersoll@Sun.COM {
1211*9126SWyllys.Ingersoll@Sun.COM 	// If this is an SO session
1212*9126SWyllys.Ingersoll@Sun.COM 	if ((flags & CKF_SO_PIN_TO_BE_CHANGED) &&
1213*9126SWyllys.Ingersoll@Sun.COM 	    (si->state == CKS_RW_SO_FUNCTIONS))
1214*9126SWyllys.Ingersoll@Sun.COM 		return (TRUE);
1215*9126SWyllys.Ingersoll@Sun.COM 	return ((flags & CKF_USER_PIN_TO_BE_CHANGED) &&
1216*9126SWyllys.Ingersoll@Sun.COM 	    ((si->state == CKS_RO_USER_FUNCTIONS) ||
1217*9126SWyllys.Ingersoll@Sun.COM 	    (si->state == CKS_RW_USER_FUNCTIONS)));
1218*9126SWyllys.Ingersoll@Sun.COM }
1219*9126SWyllys.Ingersoll@Sun.COM 
1220*9126SWyllys.Ingersoll@Sun.COM CK_BBOOL
pin_locked(CK_SESSION_INFO * si,CK_FLAGS flags)1221*9126SWyllys.Ingersoll@Sun.COM pin_locked(CK_SESSION_INFO *si, CK_FLAGS flags) {
1222*9126SWyllys.Ingersoll@Sun.COM 	// If this is an SO session
1223*9126SWyllys.Ingersoll@Sun.COM 	if ((flags & CKF_SO_PIN_LOCKED) &&
1224*9126SWyllys.Ingersoll@Sun.COM 	    (si->state == CKS_RW_SO_FUNCTIONS))
1225*9126SWyllys.Ingersoll@Sun.COM 		return (TRUE);
1226*9126SWyllys.Ingersoll@Sun.COM 
1227*9126SWyllys.Ingersoll@Sun.COM 	return ((flags & CKF_USER_PIN_LOCKED) &&
1228*9126SWyllys.Ingersoll@Sun.COM 	    ((si->state == CKS_RO_USER_FUNCTIONS) ||
1229*9126SWyllys.Ingersoll@Sun.COM 	    (si->state == CKS_RW_USER_FUNCTIONS)));
1230*9126SWyllys.Ingersoll@Sun.COM }
1231*9126SWyllys.Ingersoll@Sun.COM 
1232*9126SWyllys.Ingersoll@Sun.COM void
set_login_flags(CK_USER_TYPE userType,CK_FLAGS * flags)1233*9126SWyllys.Ingersoll@Sun.COM set_login_flags(CK_USER_TYPE userType, CK_FLAGS *flags) {
1234*9126SWyllys.Ingersoll@Sun.COM 	if (userType == CKU_USER) {
1235*9126SWyllys.Ingersoll@Sun.COM 		if (*flags & CKF_USER_PIN_FINAL_TRY) {
1236*9126SWyllys.Ingersoll@Sun.COM 			*flags |= CKF_USER_PIN_LOCKED;
1237*9126SWyllys.Ingersoll@Sun.COM 			*flags &= ~(CKF_USER_PIN_FINAL_TRY);
1238*9126SWyllys.Ingersoll@Sun.COM 		} else if (*flags & CKF_USER_PIN_COUNT_LOW) {
1239*9126SWyllys.Ingersoll@Sun.COM 			*flags |= CKF_USER_PIN_FINAL_TRY;
1240*9126SWyllys.Ingersoll@Sun.COM 			*flags &= ~(CKF_USER_PIN_COUNT_LOW);
1241*9126SWyllys.Ingersoll@Sun.COM 		} else {
1242*9126SWyllys.Ingersoll@Sun.COM 			*flags |= CKF_USER_PIN_COUNT_LOW;
1243*9126SWyllys.Ingersoll@Sun.COM 		}
1244*9126SWyllys.Ingersoll@Sun.COM 	} else {
1245*9126SWyllys.Ingersoll@Sun.COM 		if (*flags & CKF_SO_PIN_FINAL_TRY) {
1246*9126SWyllys.Ingersoll@Sun.COM 			*flags |= CKF_SO_PIN_LOCKED;
1247*9126SWyllys.Ingersoll@Sun.COM 			*flags &= ~(CKF_SO_PIN_FINAL_TRY);
1248*9126SWyllys.Ingersoll@Sun.COM 		} else if (*flags & CKF_SO_PIN_COUNT_LOW) {
1249*9126SWyllys.Ingersoll@Sun.COM 			*flags |= CKF_SO_PIN_FINAL_TRY;
1250*9126SWyllys.Ingersoll@Sun.COM 			*flags &= ~(CKF_SO_PIN_COUNT_LOW);
1251*9126SWyllys.Ingersoll@Sun.COM 		} else {
1252*9126SWyllys.Ingersoll@Sun.COM 			*flags |= CKF_SO_PIN_COUNT_LOW;
1253*9126SWyllys.Ingersoll@Sun.COM 		}
1254*9126SWyllys.Ingersoll@Sun.COM 	}
1255*9126SWyllys.Ingersoll@Sun.COM }
1256