001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.daemon.authentication;
027:
028: import com.sshtools.daemon.platform.*;
029:
030: import com.sshtools.j2ssh.authentication.*;
031: import com.sshtools.j2ssh.io.*;
032:
033: import org.apache.commons.logging.*;
034:
035: import java.io.*;
036:
037: /**
038: *
039: *
040: * @author $author$
041: * @version $Revision: 1.13 $
042: */
043: public class PublicKeyAuthenticationServer extends
044: SshAuthenticationServer {
045: private static Class pkv = AuthorizationFileVerification.class;
046: private Log log = LogFactory
047: .getLog(PublicKeyAuthenticationServer.class);
048:
049: /**
050: * Creates a new PublicKeyAuthenticationServer object.
051: */
052: public PublicKeyAuthenticationServer() {
053: }
054:
055: /**
056: *
057: *
058: * @return
059: */
060: public String getMethodName() {
061: return "publickey";
062: }
063:
064: /**
065: *
066: *
067: * @param pkv
068: */
069: public static void setVerificationImpl(Class pkv) {
070: PublicKeyAuthenticationServer.pkv = pkv;
071: }
072:
073: /**
074: *
075: *
076: * @param authentication
077: * @param msg
078: *
079: * @return
080: *
081: * @throws IOException
082: */
083: public int authenticate(
084: AuthenticationProtocolServer authentication,
085: SshMsgUserAuthRequest msg) throws IOException { //, Map nativeSettings)
086:
087: ByteArrayReader bar = new ByteArrayReader(msg.getRequestData());
088:
089: // If check == 0 then authenticate, otherwise just inform that
090: // the authentication can continue with the key supplied
091: int check = bar.read();
092: String algorithm = bar.readString();
093: byte[] encoded = bar.readBinaryString();
094: byte[] signature = null;
095:
096: try {
097: PublicKeyVerification verify = (PublicKeyVerification) pkv
098: .newInstance();
099:
100: if (check == 0) {
101: // Verify that the public key can be used for authenticaiton
102: //boolean ok = SshKeyPairFactory.supportsKey(algorithm);
103: // Send the reply
104: if (verify.acceptKey(msg.getUsername(), algorithm,
105: encoded)) {
106: SshMsgUserAuthPKOK reply = new SshMsgUserAuthPKOK(
107: algorithm, encoded);
108: authentication.sendMessage(reply);
109:
110: return AuthenticationProtocolState.READY;
111: } else {
112: return AuthenticationProtocolState.FAILED;
113: }
114: } else {
115: signature = bar.readBinaryString();
116:
117: NativeAuthenticationProvider authProv = NativeAuthenticationProvider
118: .getInstance();
119:
120: if (authProv == null) {
121: log
122: .error("Authentication failed because no native authentication provider is available");
123:
124: return AuthenticationProtocolState.FAILED;
125: }
126:
127: if (!authProv.logonUser(msg.getUsername())) { //, nativeSettings)) {
128: log.info("Authentication failed because "
129: + msg.getUsername()
130: + " is not a valid username");
131:
132: return AuthenticationProtocolState.FAILED;
133: }
134:
135: try {
136: if (verify.verifyKeySignature(msg.getUsername(),
137: algorithm, encoded, msg.getServiceName(),
138: authentication.getSessionIdentifier(),
139: signature)) {
140: return AuthenticationProtocolState.COMPLETE;
141: }
142: } catch (Exception ex) {
143: log
144: .error(
145: "Failed to create an instance of the verification implementation",
146: ex);
147: }
148: }
149: } catch (Exception e) {
150: }
151:
152: return AuthenticationProtocolState.FAILED;
153: }
154: }
|