Produced by Araxis Merge on 9/25/2018 2:13:24 PM Central Daylight Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
# | Location | File | Last Modified |
---|---|---|---|
1 | build 3.zip\build 3\MHLTH_YS_137_Source\JavaScript\resources\javaJDF-1.8.0\src\sun\security\pkcs11 | Token.java | Mon Jan 22 14:46:54 2018 UTC |
2 | build 3.zip\build 3\MHLTH_YS_137_Source\JavaScript\resources\javaJDF-1.8.0\src\sun\security\pkcs11 | Token.java | Wed Sep 12 17:53:24 2018 UTC |
Description | Between Files 1 and 2 |
|
---|---|---|
Text Blocks | Lines | |
Unchanged | 5 | 890 |
Changed | 4 | 10 |
Inserted | 0 | 0 |
Removed | 0 | 0 |
Whitespace | |
---|---|
Character case | Differences in character case are significant |
Line endings | Differences in line endings (CR and LF characters) are ignored |
CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
1 | /* | |
2 | * Copyrig ht (c) 200 3, 2013, O racle and/ or its aff iliates. A ll rights reserved. | |
3 | * DO NOT ALTER OR R EMOVE COPY RIGHT NOTI CES OR THI S FILE HEA DER. | |
4 | * | |
5 | * This co de is free software; you can r edistribut e it and/o r modify i t | |
6 | * under t he terms o f the GNU General Pu blic Licen se version 2 only, a s | |
7 | * publish ed by the Free Softw are Founda tion. Ora cle design ates this | |
8 | * particu lar file a s subject to the "Cl asspath" e xception a s provided | |
9 | * by Orac le in the LICENSE fi le that ac companied this code. | |
10 | * | |
11 | * This co de is dist ributed in the hope that it wi ll be usef ul, but WI THOUT | |
12 | * ANY WAR RANTY; wit hout even the implie d warranty of MERCHA NTABILITY or | |
13 | * FITNESS FOR A PAR TICULAR PU RPOSE. Se e the GNU General Pu blic Licen se | |
14 | * version 2 for mor e details (a copy is included in the LIC ENSE file that | |
15 | * accompa nied this code). | |
16 | * | |
17 | * You sho uld have r eceived a copy of th e GNU Gene ral Public License v ersion | |
18 | * 2 along with this work; if not, write to the Fr ee Softwar e Foundati on, | |
19 | * Inc., 5 1 Franklin St, Fifth Floor, Bo ston, MA 0 2110-1301 USA. | |
20 | * | |
21 | * Please contact Or acle, 500 Oracle Par kway, Redw ood Shores , CA 94065 USA | |
22 | * or visi t www.orac le.com if you need a dditional informatio n or have any | |
23 | * questio ns. | |
24 | */ | |
25 | ||
26 | package su n.security .pkcs11; | |
27 | ||
28 | import jav a.util.*; | |
29 | import jav a.util.con current.Co ncurrentHa shMap; | |
30 | import jav a.io.*; | |
31 | import jav a.lang.ref .*; | |
32 | ||
33 | import jav a.security .*; | |
34 | import jav ax.securit y.auth.log in.LoginEx ception; | |
35 | ||
36 | import sun .security. jca.JCAUti l; | |
37 | ||
38 | import sun .security. pkcs11.wra pper.*; | |
39 | import sta tic sun.se curity.pkc s11.Templa teManager. *; | |
40 | import sta tic sun.se curity.pkc s11.wrappe r.PKCS11Co nstants.*; | |
41 | ||
42 | /** | |
43 | * PKCS#11 token. | |
44 | * | |
45 | * @author Andreas Sterbenz | |
46 | * @since 1.5 | |
47 | */ | |
48 | class Toke n implemen ts Seriali zable { | |
49 | ||
50 | // nee d to be se rializable to allow SecureRand om to be s erialized | |
51 | privat e static f inal long serialVers ionUID = 2 5415276491 00571747L; | |
52 | ||
53 | // how often to check if t he token i s still pr esent (in ms) | |
54 | // thi s is diffe rent from checking i f a token has been i nserted, | |
55 | // tha t is done in SunPKCS 11. Curren tly 50 ms. | |
56 | privat e final st atic long CHECK_INTE RVAL = 50; | |
57 | ||
58 | final SunPKCS11 provider; | |
59 | ||
60 | final PKCS11 p11 ; | |
61 | ||
62 | final Config con fig; | |
63 | ||
64 | final CK_TOKEN_I NFO tokenI nfo; | |
65 | ||
66 | // ses sion manag er to pool sessions | |
67 | final SessionMan ager sessi onManager; | |
68 | ||
69 | // tem plate mana ger to cus tomize the attribute s used whe n creating objects | |
70 | privat e final Te mplateMana ger templa teManager; | |
71 | ||
72 | // fla g indicati ng whether we need t o explicit ly cancel operations | |
73 | // we started on the token . If false , we assum e operatio ns are | |
74 | // aut omatically cancelled once we s tart anoth er one | |
75 | final boolean ex plicitCanc el; | |
76 | ||
77 | // transla tion cache for PW keys | |
78 | final KeyC ache PW Cache; | |
79 | ||
80 | // tra nslation c ache for a symmetric keys (publ ic and pri vate) | |
81 | final KeyCache p rivateCach e; | |
82 | ||
83 | // cac hed instan ces of the various k ey factori es, initia lized on d emand | |
84 | privat e volatile P11KeyFac tory rsaFa ctory, dsa Factory, d hFactory, ecFactory; | |
85 | ||
86 | // tab le which m aps mechan isms to th e correspo nding cach ed | |
87 | // Mec hanismInfo objects | |
88 | privat e final Ma p<Long, CK _MECHANISM _INFO> mec hInfoMap; | |
89 | ||
90 | // sin gle Secure RandomSpi instance w e use per token | |
91 | // ini tialized o n demand ( if support ed) | |
92 | privat e volatile P11Secure Random sec ureRandom; | |
93 | ||
94 | // sin gle KeySto reSpi inst ance we us e per prov ider | |
95 | // ini tialized o n demand | |
96 | privat e volatile P11KeySto re keyStor e; | |
97 | ||
98 | // whe ther this token is a removable token | |
99 | privat e final bo olean remo vable; | |
100 | ||
101 | // for removable tokens: w hether thi s token is valid or has been r emoved | |
102 | privat e volatile boolean v alid; | |
103 | ||
104 | // for removable tokens: t ime last c hecked for token pre sence | |
105 | privat e long las tPresentCh eck; | |
106 | ||
107 | // uni que token id, used f or seriali zation onl y | |
108 | privat e byte[] t okenId; | |
109 | ||
110 | // fla g indicati ng whether the token is write protected | |
111 | privat e boolean writeProte cted; | |
112 | ||
113 | // fla g indicati ng whether we are lo gged in | |
114 | privat e volatile boolean l oggedIn; | |
115 | ||
116 | // tim e we last checked lo gin status | |
117 | privat e long las tLoginChec k; | |
118 | ||
119 | // mut ex for tok en-present -check | |
120 | privat e final st atic Objec t CHECK_LO CK = new O bject(); | |
121 | ||
122 | // obj ect for in dicating u nsupported mechanism in 'mechI nfoMap' | |
123 | privat e final st atic CK_ME CHANISM_IN FO INVALID _MECH = | |
124 | ne w CK_MECHA NISM_INFO( 0, 0, 0); | |
125 | ||
126 | // flag in dicating w hether the token sup ports raw PW key materi al import | |
127 | privat e Boolean supportsRa wSecretKey Import; | |
128 | ||
129 | Token( SunPKCS11 provider) throws PKC S11Excepti on { | |
130 | th is.provide r = provid er; | |
131 | th is.removab le = provi der.remova ble; | |
132 | th is.valid = true; | |
133 | p1 1 = provid er.p11; | |
134 | co nfig = pro vider.conf ig; | |
135 | to kenInfo = p11.C_GetT okenInfo(p rovider.sl otID); | |
136 | wr iteProtect ed = (toke nInfo.flag s & CKF_WR ITE_PROTEC TED) != 0; | |
137 | // create se ssion mana ger and op en a test session | |
138 | Se ssionManag er session Manager; | |
139 | tr y { | |
140 | sessionM anager = n ew Session Manager(th is); | |
141 | Session s = sessio nManager.g etOpSessio n(); | |
142 | sessionM anager.rel easeSessio n(s); | |
143 | } catch (PKC S11Excepti on e) { | |
144 | if (writ eProtected ) { | |
145 | thro w e; | |
146 | } | |
147 | // token might not permit RW sessions even thoug h | |
148 | // CKF_W RITE_PROTE CTED is no t set | |
149 | writePro tected = t rue; | |
150 | sessionM anager = n ew Session Manager(th is); | |
151 | Session s = sessio nManager.g etOpSessio n(); | |
152 | sessionM anager.rel easeSessio n(s); | |
153 | } | |
154 | th is.session Manager = sessionMan ager; | |
155 | PW Cache = ne w KeyCache (); | |
156 | pr ivateCache = new Key Cache(); | |
157 | te mplateMana ger = conf ig.getTemp lateManage r(); | |
158 | ex plicitCanc el = confi g.getExpli citCancel( ); | |
159 | me chInfoMap = | |
160 | new Conc urrentHash Map<Long, CK_MECHANI SM_INFO>(1 0); | |
161 | } | |
162 | ||
163 | boolea n isWriteP rotected() { | |
164 | re turn write Protected; | |
165 | } | |
166 | ||
167 | // return whether th e token su pports raw PW key materi al import | |
168 | boolea n supports RawSecretK eyImport() { | |
169 | if (supports RawSecretK eyImport = = null) { | |
170 | SecureRa ndom rando m = JCAUti l.getSecur eRandom(); | |
171 | byte[] e ncoded = n ew byte[48 ]; | |
172 | random.n extBytes(e ncoded); | |
173 | ||
174 | CK_ATTRI BUTE[] att ributes = new CK_ATT RIBUTE[3]; | |
175 | attribut es[0] = ne w CK_ATTRI BUTE(CKA_C LASS, CKO_ SECRET_KEY ); | |
176 | attribut es[1] = ne w CK_ATTRI BUTE(CKA_K EY_TYPE, C KK_GENERIC _SECRET); | |
177 | attribut es[2] = ne w CK_ATTRI BUTE(CKA_V ALUE, enco ded); | |
178 | ||
179 | Session session = null; | |
180 | try { | |
181 | attr ibutes = g etAttribut es(O_IMPOR T, | |
182 | CKO_SE CRET_KEY, CKK_GENERI C_SECRET, attributes ); | |
183 | sess ion = getO bjSession( ); | |
184 | long keyID = p 11.C_Creat eObject(se ssion.id() , attribut es); | |
185 | ||
186 | supp ortsRawSec retKeyImpo rt = Boole an.TRUE; | |
187 | } catch (PKCS11Exc eption e) { | |
188 | supp ortsRawSec retKeyImpo rt = Boole an.FALSE; | |
189 | } finall y { | |
190 | rele aseSession (session); | |
191 | } | |
192 | } | |
193 | ||
194 | re turn suppo rtsRawSecr etKeyImpor t; | |
195 | } | |
196 | ||
197 | // ret urn whethe r we are l ogged in | |
198 | // use s cached r esult if c urrent. se ssion is o ptional an d may be n ull | |
199 | boolea n isLogged In(Session session) throws PKC S11Excepti on { | |
200 | // volatile load first | |
201 | bo olean logg edIn = thi s.loggedIn ; | |
202 | lo ng time = System.cur rentTimeMi llis(); | |
203 | if (time - l astLoginCh eck > CHEC K_INTERVAL ) { | |
204 | loggedIn = isLogge dInNow(ses sion); | |
205 | lastLogi nCheck = t ime; | |
206 | } | |
207 | re turn logge dIn; | |
208 | } | |
209 | ||
210 | // ret urn whethe r we are l ogged in n ow | |
211 | // doe s not use cache | |
212 | boolea n isLogged InNow(Sess ion sessio n) throws PKCS11Exce ption { | |
213 | bo olean allo cSession = (session == null); | |
214 | tr y { | |
215 | if (allo cSession) { | |
216 | sess ion = getO pSession() ; | |
217 | } | |
218 | CK_SESSI ON_INFO in fo = p11.C _GetSessio nInfo(sess ion.id()); | |
219 | boolean loggedIn = (info.sta te == CKS_ RO_USER_FU NCTIONS) | | | |
220 | (info.st ate == CKS _RW_USER_F UNCTIONS); | |
221 | this.log gedIn = lo ggedIn; | |
222 | return l oggedIn; | |
223 | } finally { | |
224 | if (allo cSession) { | |
225 | rele aseSession (session); | |
226 | } | |
227 | } | |
228 | } | |
229 | ||
230 | // ens ure that w e are logg ed in | |
231 | // cal l provider .login() i f not | |
232 | void e nsureLogge dIn(Sessio n session) throws PK CS11Except ion, Login Exception { | |
233 | if (isLogged In(session ) == false ) { | |
234 | provider .login(nul l, null); | |
235 | } | |
236 | } | |
237 | ||
238 | // ret urn whethe r this tok en object is valid ( i.e. token not remov ed) | |
239 | // ret urns value from last check, do es not per form new c heck | |
240 | boolea n isValid( ) { | |
241 | if (removabl e == false ) { | |
242 | return t rue; | |
243 | } | |
244 | re turn valid ; | |
245 | } | |
246 | ||
247 | void e nsureValid () { | |
248 | if (isValid( ) == false ) { | |
249 | throw ne w Provider Exception( "Token has been remo ved"); | |
250 | } | |
251 | } | |
252 | ||
253 | // ret urn whethe r a token is present (i.e. tok en not rem oved) | |
254 | // ret urns cache d value if current, otherwise performs n ew check | |
255 | boolea n isPresen t(long ses sionID) { | |
256 | if (removabl e == false ) { | |
257 | return t rue; | |
258 | } | |
259 | if (valid == false) { | |
260 | return f alse; | |
261 | } | |
262 | lo ng time = System.cur rentTimeMi llis(); | |
263 | if ((time - lastPresen tCheck) >= CHECK_INT ERVAL) { | |
264 | synchron ized (CHEC K_LOCK) { | |
265 | if ( (time - la stPresentC heck) >= C HECK_INTER VAL) { | |
266 | boolean ok = false; | |
267 | try { | |
268 | // che ck if toke n still pr esent | |
269 | CK_SLO T_INFO slo tInfo = | |
270 | provider .p11.C_Get SlotInfo(p rovider.sl otID); | |
271 | if ((s lotInfo.fl ags & CKF_ TOKEN_PRES ENT) != 0) { | |
272 | // if the to ken has be en removed and re-in serted, | |
273 | // the token should re turn an er ror | |
274 | CK _SESSION_I NFO sessIn fo = | |
275 | prov ider.p11.C _GetSessio nInfo | |
276 | (ses sionID); | |
277 | ok = true; | |
278 | } | |
279 | } catch (P KCS11Excep tion e) { | |
280 | // emp ty | |
281 | } | |
282 | valid = ok ; | |
283 | lastPresen tCheck = S ystem.curr entTimeMil lis(); | |
284 | if (ok == false) { | |
285 | destro y(); | |
286 | } | |
287 | } | |
288 | } | |
289 | } | |
290 | re turn valid ; | |
291 | } | |
292 | ||
293 | void d estroy() { | |
294 | va lid = fals e; | |
295 | pr ovider.uni nitToken(t his); | |
296 | } | |
297 | ||
298 | Sessio n getObjSe ssion() th rows PKCS1 1Exception { | |
299 | re turn sessi onManager. getObjSess ion(); | |
300 | } | |
301 | ||
302 | Sessio n getOpSes sion() thr ows PKCS11 Exception { | |
303 | re turn sessi onManager. getOpSessi on(); | |
304 | } | |
305 | ||
306 | Sessio n releaseS ession(Ses sion sessi on) { | |
307 | re turn sessi onManager. releaseSes sion(sessi on); | |
308 | } | |
309 | ||
310 | Sessio n killSess ion(Sessio n session) { | |
311 | re turn sessi onManager. killSessio n(session) ; | |
312 | } | |
313 | ||
314 | CK_ATT RIBUTE[] g etAttribut es(String op, long t ype, long alg, | |
315 | CK_ATTRI BUTE[] att rs) throws PKCS11Exc eption { | |
316 | CK _ATTRIBUTE [] newAttr s = | |
317 | templateMa nager.getA ttributes( op, type, alg, attrs ); | |
318 | fo r (CK_ATTR IBUTE attr : newAttr s) { | |
319 | if (attr .type == C KA_TOKEN) { | |
320 | if ( attr.getBo olean()) { | |
321 | try { | |
322 | ensure LoggedIn(n ull); | |
323 | } catch (L oginExcept ion e) { | |
324 | throw new Provid erExceptio n("Login f ailed", e) ; | |
325 | } | |
326 | } | |
327 | // b reak once we have fo und a CKA_ TOKEN attr ibute | |
328 | brea k; | |
329 | } | |
330 | } | |
331 | re turn newAt trs; | |
332 | } | |
333 | ||
334 | P11Key Factory ge tKeyFactor y(String a lgorithm) { | |
335 | P1 1KeyFactor y f; | |
336 | if (algorith m.equals(" RSA")) { | |
337 | f = rsaF actory; | |
338 | if (f == null) { | |
339 | f = new P11RSA KeyFactory (this, alg orithm); | |
340 | rsaF actory = f ; | |
341 | } | |
342 | } else if (a lgorithm.e quals("DSA ")) { | |
343 | f = dsaF actory; | |
344 | if (f == null) { | |
345 | f = new P11DSA KeyFactory (this, alg orithm); | |
346 | dsaF actory = f ; | |
347 | } | |
348 | } else if (a lgorithm.e quals("DH" )) { | |
349 | f = dhFa ctory; | |
350 | if (f == null) { | |
351 | f = new P11DHK eyFactory( this, algo rithm); | |
352 | dhFa ctory = f; | |
353 | } | |
354 | } else if (a lgorithm.e quals("EC" )) { | |
355 | f = ecFa ctory; | |
356 | if (f == null) { | |
357 | f = new P11ECK eyFactory( this, algo rithm); | |
358 | ecFa ctory = f; | |
359 | } | |
360 | } else { | |
361 | throw ne w Provider Exception( "Unknown a lgorithm " + algorit hm); | |
362 | } | |
363 | re turn f; | |
364 | } | |
365 | ||
366 | P11Sec ureRandom getRandom( ) { | |
367 | if (secureRa ndom == nu ll) { | |
368 | secureRa ndom = new P11Secure Random(thi s); | |
369 | } | |
370 | re turn secur eRandom; | |
371 | } | |
372 | ||
373 | P11Key Store getK eyStore() { | |
374 | if (keyStore == null) { | |
375 | keyStore = new P11 KeyStore(t his); | |
376 | } | |
377 | re turn keySt ore; | |
378 | } | |
379 | ||
380 | CK_MEC HANISM_INF O getMecha nismInfo(l ong mechan ism) throw s PKCS11Ex ception { | |
381 | CK _MECHANISM _INFO resu lt = mechI nfoMap.get (mechanism ); | |
382 | if (result = = null) { | |
383 | try { | |
384 | resu lt = p11.C _GetMechan ismInfo(pr ovider.slo tID, | |
385 | me chanism); | |
386 | mech InfoMap.pu t(mechanis m, result) ; | |
387 | } catch (PKCS11Exc eption e) { | |
388 | if ( e.getError Code() != PKCS11Cons tants.CKR_ MECHANISM_ INVALID) { | |
389 | throw e; | |
390 | } el se { | |
391 | mechInfoMa p.put(mech anism, INV ALID_MECH) ; | |
392 | } | |
393 | } | |
394 | } else if (r esult == I NVALID_MEC H) { | |
395 | result = null; | |
396 | } | |
397 | re turn resul t; | |
398 | } | |
399 | ||
400 | privat e synchron ized byte[ ] getToken Id() { | |
401 | if (tokenId == null) { | |
402 | SecureRa ndom rando m = JCAUti l.getSecur eRandom(); | |
403 | tokenId = new byte [20]; | |
404 | random.n extBytes(t okenId); | |
405 | serializ edTokens.a dd(new Wea kReference <Token>(th is)); | |
406 | } | |
407 | re turn token Id; | |
408 | } | |
409 | ||
410 | // lis t of all t okens that have been serialize d within t his VM | |
411 | // NOT E that ele ments are never remo ved from t his list | |
412 | // the assumptio n is that the number of tokens that are serialized | |
413 | // is relatively small | |
414 | privat e static f inal List< Reference< Token>> se rializedTo kens = | |
415 | ne w ArrayLis t<Referenc e<Token>>( ); | |
416 | ||
417 | privat e Object w riteReplac e() throws ObjectStr eamExcepti on { | |
418 | if (isValid( ) == false ) { | |
419 | throw ne w NotSeria lizableExc eption("To ken has be en removed "); | |
420 | } | |
421 | re turn new T okenRep(th is); | |
422 | } | |
423 | ||
424 | // ser ialized re presentati on of a to ken | |
425 | // tok ens can on ly be de-s erialized within the same VM i nvocation | |
426 | // and if the to ken has no t been rem oved in th e meantime | |
427 | privat e static c lass Token Rep implem ents Seria lizable { | |
428 | ||
429 | pr ivate stat ic final l ong serial VersionUID = 3503721 1682182198 07L; | |
430 | ||
431 | pr ivate fina l byte[] t okenId; | |
432 | ||
433 | To kenRep(Tok en token) { | |
434 | tokenId = token.ge tTokenId() ; | |
435 | } | |
436 | ||
437 | pr ivate Obje ct readRes olve() thr ows Object StreamExce ption { | |
438 | for (Ref erence<Tok en> tokenR ef : seria lizedToken s) { | |
439 | Toke n token = tokenRef.g et(); | |
440 | if ( (token != null) && t oken.isVal id()) { | |
441 | if (Arrays .equals(to ken.getTok enId(), to kenId)) { | |
442 | return token; | |
443 | } | |
444 | } | |
445 | } | |
446 | throw ne w NotSeria lizableExc eption("Co uld not fi nd token") ; | |
447 | } | |
448 | } | |
449 | ||
450 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.