author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 59024 | b046ba510bbc |
permissions | -rw-r--r-- |
2 | 1 |
/* |
59024
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.security.sasl; |
|
27 |
||
28 |
import javax.security.sasl.*; |
|
29 |
||
59024
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
30 |
import static java.nio.charset.StandardCharsets.UTF_8; |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
31 |
|
2 | 32 |
/** |
33 |
* Implements the PLAIN SASL client mechanism. |
|
34 |
* (<A |
|
35 |
* HREF="http://ftp.isi.edu/in-notes/rfc2595.txt">RFC 2595</A>) |
|
36 |
* |
|
37 |
* @author Rosanna Lee |
|
38 |
*/ |
|
39 |
final class PlainClient implements SaslClient { |
|
40 |
private boolean completed = false; |
|
41 |
private byte[] pw; |
|
42 |
private String authorizationID; |
|
43 |
private String authenticationID; |
|
44 |
private static byte SEP = 0; // US-ASCII <NUL> |
|
45 |
||
46 |
/** |
|
47 |
* Creates a SASL mechanism with client credentials that it needs |
|
48 |
* to participate in Plain authentication exchange with the server. |
|
49 |
* |
|
50 |
* @param authorizationID A possibly null string representing the principal |
|
51 |
* for which authorization is being granted; if null, same as |
|
52 |
* authenticationID |
|
53 |
* @param authenticationID A non-null string representing the principal |
|
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25859
diff
changeset
|
54 |
* being authenticated. pw is associated with this principal. |
2 | 55 |
* @param pw A non-null byte[] containing the password. |
56 |
*/ |
|
57 |
PlainClient(String authorizationID, String authenticationID, byte[] pw) |
|
58 |
throws SaslException { |
|
59 |
if (authenticationID == null || pw == null) { |
|
60 |
throw new SaslException( |
|
61 |
"PLAIN: authorization ID and password must be specified"); |
|
62 |
} |
|
63 |
||
64 |
this.authorizationID = authorizationID; |
|
65 |
this.authenticationID = authenticationID; |
|
66 |
this.pw = pw; // caller should have already cloned |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Retrieves this mechanism's name for to initiate the PLAIN protocol |
|
71 |
* exchange. |
|
72 |
* |
|
73 |
* @return The string "PLAIN". |
|
74 |
*/ |
|
75 |
public String getMechanismName() { |
|
76 |
return "PLAIN"; |
|
77 |
} |
|
78 |
||
79 |
public boolean hasInitialResponse() { |
|
80 |
return true; |
|
81 |
} |
|
82 |
||
83 |
public void dispose() throws SaslException { |
|
84 |
clearPassword(); |
|
85 |
} |
|
86 |
||
87 |
/** |
|
88 |
* Retrieves the initial response for the SASL command, which for |
|
89 |
* PLAIN is the concatenation of authorization ID, authentication ID |
|
90 |
* and password, with each component separated by the US-ASCII <NUL> byte. |
|
91 |
* |
|
92 |
* @param challengeData Ignored |
|
93 |
* @return A non-null byte array containing the response to be sent to the server. |
|
59024
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
94 |
* @throws IllegalStateException if authentication already completed |
2 | 95 |
*/ |
59024
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
96 |
public byte[] evaluateChallenge(byte[] challengeData) { |
2 | 97 |
if (completed) { |
98 |
throw new IllegalStateException( |
|
99 |
"PLAIN authentication already completed"); |
|
100 |
} |
|
101 |
completed = true; |
|
59024
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
102 |
byte[] authz = (authorizationID != null) |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
103 |
? authorizationID.getBytes(UTF_8) |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
104 |
: null; |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
105 |
byte[] auth = authenticationID.getBytes(UTF_8); |
2 | 106 |
|
59024
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
107 |
byte[] answer = new byte[pw.length + auth.length + 2 + |
2 | 108 |
(authz == null ? 0 : authz.length)]; |
109 |
||
59024
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
110 |
int pos = 0; |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
111 |
if (authz != null) { |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
112 |
System.arraycopy(authz, 0, answer, 0, authz.length); |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
113 |
pos = authz.length; |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
114 |
} |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
115 |
answer[pos++] = SEP; |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
116 |
System.arraycopy(auth, 0, answer, pos, auth.length); |
2 | 117 |
|
59024
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
118 |
pos += auth.length; |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
119 |
answer[pos++] = SEP; |
2 | 120 |
|
59024
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
121 |
System.arraycopy(pw, 0, answer, pos, pw.length); |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
122 |
|
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
123 |
clearPassword(); |
b046ba510bbc
8233884: Avoid looking up standard charsets in security libraries
igerasim
parents:
47216
diff
changeset
|
124 |
return answer; |
2 | 125 |
} |
126 |
||
127 |
/** |
|
128 |
* Determines whether this mechanism has completed. |
|
129 |
* Plain completes after returning one response. |
|
130 |
* |
|
131 |
* @return true if has completed; false otherwise; |
|
132 |
*/ |
|
133 |
public boolean isComplete() { |
|
134 |
return completed; |
|
135 |
} |
|
136 |
||
137 |
/** |
|
138 |
* Unwraps the incoming buffer. |
|
139 |
* |
|
140 |
* @throws SaslException Not applicable to this mechanism. |
|
141 |
*/ |
|
142 |
public byte[] unwrap(byte[] incoming, int offset, int len) |
|
143 |
throws SaslException { |
|
144 |
if (completed) { |
|
145 |
throw new SaslException( |
|
146 |
"PLAIN supports neither integrity nor privacy"); |
|
147 |
} else { |
|
148 |
throw new IllegalStateException("PLAIN authentication not completed"); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Wraps the outgoing buffer. |
|
154 |
* |
|
155 |
* @throws SaslException Not applicable to this mechanism. |
|
156 |
*/ |
|
157 |
public byte[] wrap(byte[] outgoing, int offset, int len) throws SaslException { |
|
158 |
if (completed) { |
|
159 |
throw new SaslException( |
|
160 |
"PLAIN supports neither integrity nor privacy"); |
|
161 |
} else { |
|
162 |
throw new IllegalStateException("PLAIN authentication not completed"); |
|
163 |
} |
|
164 |
} |
|
165 |
||
166 |
/** |
|
167 |
* Retrieves the negotiated property. |
|
168 |
* This method can be called only after the authentication exchange has |
|
32275
17eeb583a331
8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents:
28059
diff
changeset
|
169 |
* completed (i.e., when {@code isComplete()} returns true); otherwise, a |
17eeb583a331
8133802: replace some <tt> tags (obsolete in html5) in security-libs docs
avstepan
parents:
28059
diff
changeset
|
170 |
* {@code SaslException} is thrown. |
2 | 171 |
* |
172 |
* @return value of property; only QOP is applicable to PLAIN. |
|
173 |
* @exception IllegalStateException if this authentication exchange |
|
174 |
* has not completed |
|
175 |
*/ |
|
176 |
public Object getNegotiatedProperty(String propName) { |
|
177 |
if (completed) { |
|
178 |
if (propName.equals(Sasl.QOP)) { |
|
179 |
return "auth"; |
|
180 |
} else { |
|
181 |
return null; |
|
182 |
} |
|
183 |
} else { |
|
184 |
throw new IllegalStateException("PLAIN authentication not completed"); |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
private void clearPassword() { |
|
189 |
if (pw != null) { |
|
190 |
// zero out password |
|
191 |
for (int i = 0; i < pw.length; i++) { |
|
192 |
pw[i] = (byte)0; |
|
193 |
} |
|
194 |
pw = null; |
|
195 |
} |
|
196 |
} |
|
197 |
||
44534 | 198 |
@SuppressWarnings("deprecation") |
2 | 199 |
protected void finalize() { |
200 |
clearPassword(); |
|
201 |
} |
|
202 |
} |