Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.security.sasl/share/classes/com/sun/security/sasl/CramMD5Client.java
41161 views
1
/*
2
* Copyright (c) 1999, 2020, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
package com.sun.security.sasl;
27
28
import javax.security.sasl.*;
29
import java.security.NoSuchAlgorithmException;
30
31
import java.util.logging.Logger;
32
import java.util.logging.Level;
33
34
import static java.nio.charset.StandardCharsets.UTF_8;
35
36
/**
37
* Implements the CRAM-MD5 SASL client-side mechanism.
38
* (<A HREF="http://www.ietf.org/rfc/rfc2195.txt">RFC 2195</A>).
39
* CRAM-MD5 has no initial response. It receives bytes from
40
* the server as a challenge, which it hashes by using MD5 and the password.
41
* It concatenates the authentication ID with this result and returns it
42
* as the response to the challenge. At that point, the exchange is complete.
43
*
44
* @author Vincent Ryan
45
* @author Rosanna Lee
46
*/
47
final class CramMD5Client extends CramMD5Base implements SaslClient {
48
private String username;
49
50
/**
51
* Creates a SASL mechanism with client credentials that it needs
52
* to participate in CRAM-MD5 authentication exchange with the server.
53
*
54
* @param authID A non-null string representing the principal
55
* being authenticated.
56
*
57
* @param pw A non-null String or byte[]
58
* containing the password. If it is an array, it is first cloned.
59
*/
60
CramMD5Client(String authID, byte[] pw) throws SaslException {
61
if (authID == null || pw == null) {
62
throw new SaslException(
63
"CRAM-MD5: authentication ID and password must be specified");
64
}
65
66
username = authID;
67
this.pw = pw; // caller should have already cloned
68
}
69
70
/**
71
* CRAM-MD5 has no initial response.
72
*/
73
public boolean hasInitialResponse() {
74
return false;
75
}
76
77
/**
78
* Processes the challenge data.
79
*
80
* The server sends a challenge data using which the client must
81
* compute an MD5-digest with its password as the key.
82
*
83
* @param challengeData A non-null byte array containing the challenge
84
* data from the server.
85
* @return A non-null byte array containing the response to be sent to
86
* the server.
87
* @throws SaslException if platform does not have MD5 support
88
* @throws IllegalStateException if this method is invoked more than once.
89
*/
90
public byte[] evaluateChallenge(byte[] challengeData)
91
throws SaslException {
92
93
// See if we've been here before
94
if (completed) {
95
throw new IllegalStateException(
96
"CRAM-MD5 authentication already completed");
97
}
98
99
if (aborted) {
100
throw new IllegalStateException(
101
"CRAM-MD5 authentication previously aborted due to error");
102
}
103
104
// generate a keyed-MD5 digest from the user's password and challenge.
105
try {
106
if (logger.isLoggable(Level.FINE)) {
107
logger.log(Level.FINE, "CRAMCLNT01:Received challenge: {0}",
108
new String(challengeData, UTF_8));
109
}
110
111
String digest = HMAC_MD5(pw, challengeData);
112
113
// clear it when we no longer need it
114
clearPassword();
115
116
// response is username + " " + digest
117
String resp = username + " " + digest;
118
119
logger.log(Level.FINE, "CRAMCLNT02:Sending response: {0}", resp);
120
121
completed = true;
122
123
return resp.getBytes(UTF_8);
124
} catch (java.security.NoSuchAlgorithmException e) {
125
aborted = true;
126
throw new SaslException("MD5 algorithm not available on platform", e);
127
}
128
}
129
}
130
131