Primitive implementation of CertificateService.

A primitive implementation of CertificateService. Also includes a
modified version of irc-api that supports getting a custom SSL context
from the server parameters.
I have been able to confirm its workings by connecting to
'swiftco.dal.net' (certificate has a different CN.)
fix-message-formatting
Danny van Heumen 12 years ago
parent b1f4dd6490
commit 1178b51255

@ -8,6 +8,7 @@
import java.util.*;
import net.java.sip.communicator.service.certificate.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.resources.*;
import net.java.sip.communicator.util.*;
@ -37,10 +38,15 @@ public class IrcActivator
/**
* The currently valid bundle context.
*/
public static BundleContext bundleContext = null;
private static BundleContext bundleContext = null;
private static ResourceManagementService resourceService;
/**
* Certificate Service instance.
*/
private static CertificateService certiticateService;
/**
* Called when this bundle is started. In here we'll export the
* IRC ProtocolProviderFactory implementation so that it could be
@ -112,4 +118,33 @@ public static ResourceManagementService getResources()
= ResourceManagementServiceUtils.getService(bundleContext);
return resourceService;
}
/**
* Bundle Context
*
* @return returns bundle context
*/
public static BundleContext getBundleContext()
{
return bundleContext;
}
/**
* Return the certificate verification service impl.
*
* @return the CertificateVerification service.
*/
public static CertificateService getCertificateVerificationService()
{
if(certiticateService == null)
{
ServiceReference guiVerifyReference
= IrcActivator.getBundleContext().getServiceReference(
CertificateService.class.getName());
if(guiVerifyReference != null)
certiticateService = (CertificateService)
IrcActivator.getBundleContext().getService(
guiVerifyReference);
}
return certiticateService;
}
}

@ -7,9 +7,13 @@
package net.java.sip.communicator.impl.protocol.irc;
import java.io.*;
import java.security.*;
import java.util.*;
import javax.net.ssl.*;
import net.java.sip.communicator.impl.protocol.irc.ModeParser.ModeEntry;
import net.java.sip.communicator.service.certificate.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
@ -133,15 +137,11 @@ public void connect(String host, int port, String password,
this.joined.clear();
this.irc = new IRCApiImpl(true);
// FIXME Currently, the secure connection is created by
// explicitly creating an SSLContext for 'SSL'. According
// to Ingo (in a mailing list conversation) it is better to
// use the CertificateService for this. This should be
// implemented in the irc-api library, though.
this.params.setServer(new IRCServer(host, port, password,
secureConnection));
synchronized (this.irc)
{
this.params.setServer(new IRCServer(host, port, password,
secureConnection));
this.params.setCustomContext(getCustomSSLContext(host));
this.irc.addListener(new ServerListener());
connectSynchronized();
}
@ -232,6 +232,30 @@ public void onFailure(Exception e)
}
}
/**
* Create a custom SSL context for this particular server.
*
* @return returns a customized SSL context or <tt>null</tt> if one cannot
* be created.
*/
private SSLContext getCustomSSLContext(String hostname)
{
SSLContext context = null;
try
{
CertificateService cs =
IrcActivator.getCertificateVerificationService();
X509TrustManager tm =
cs.getTrustManager(hostname);
context = cs.getSSLContext(tm);
}
catch (GeneralSecurityException e)
{
LOGGER.error("failed to create custom SSL context", e);
}
return context;
}
/**
* Disconnect from the IRC server
*/
@ -666,7 +690,9 @@ public void command(ChatRoomIrcImpl chatroom, String command)
int endOfNick = command.indexOf(' ');
if (endOfNick == -1)
{
throw new IllegalArgumentException("Invalid private message format. Message was not sent.");
throw new IllegalArgumentException(
"Invalid private message format. "
+ "Message was not sent.");
}
target = command.substring(0, endOfNick);
command = command.substring(endOfNick + 1);
@ -703,8 +729,8 @@ private static ChatRoomMemberRole convertMemberMode(char modeSymbol)
}
/**
* A listener for server-level messages (any messages that are related to the
* server, the connection, that are not related to any chatroom in
* A listener for server-level messages (any messages that are related to
* the server, the connection, that are not related to any chatroom in
* particular) or that are personal message from user to local user.
*/
private class ServerListener
@ -744,8 +770,7 @@ public void onServerNumericMessage(ServerNumericMessage msg)
@Override
public void onError(ErrorMessage msg)
{
LOGGER.debug("ERROR: " + msg.getSource() + ": "
+ msg.getText());
LOGGER.debug("ERROR: " + msg.getSource() + ": " + msg.getText());
}
/**
@ -780,8 +805,8 @@ public void onUserPrivMessage(UserPrivMsg msg)
* @param user the source user
* @param text the message
*/
private void deliverReceivedMessageToPrivateChat(ChatRoomIrcImpl chatroom,
String user, String text)
private void deliverReceivedMessageToPrivateChat(
ChatRoomIrcImpl chatroom, String user, String text)
{
ChatRoomMember member = chatroom.getChatRoomMember(user);
MessageIrcImpl message =
@ -924,9 +949,10 @@ public void onChannelPart(ChanPartMessage msg)
}
catch (NullPointerException e)
{
System.err
.println("This should not have happened. Please report this as it is a bug.");
e.printStackTrace();
LOGGER
.warn(
"This should not have happened. Please report this as it is a bug.",
e);
}
}
}
@ -959,8 +985,7 @@ public void onChannelKick(ChannelKick msg)
{
ChatRoomMember kicker =
this.chatroom.getChatRoomMember(user);
this.chatroom.fireMemberPresenceEvent(kickedMember,
kicker,
this.chatroom.fireMemberPresenceEvent(kickedMember, kicker,
ChatRoomMemberPresenceChangeEvent.MEMBER_KICKED,
msg.getText());
}
@ -1214,9 +1239,9 @@ sourceMember, new Date(),
ChatRoomMessageReceivedEvent.SYSTEM_MESSAGE_RECEIVED);
break;
case UNKNOWN:
LOGGER.info("Unknown mode: "
+ (mode.isAdded() ? "+" : "-") + mode.getParams()[0]
+ ". Original mode string: '" + msg.getModeStr() + "'");
LOGGER.info("Unknown mode: " + (mode.isAdded() ? "+" : "-")
+ mode.getParams()[0] + ". Original mode string: '"
+ msg.getModeStr() + "'");
break;
default:
LOGGER.info("Unsupported mode '"
@ -1395,16 +1420,43 @@ private static class ServerParameters
implements IServerParameters
{
/**
* Nick name.
*/
private String nick;
/**
* Alternative nick names.
*/
private List<String> alternativeNicks = new ArrayList<String>();
/**
* Real name.
*/
private String real;
/**
* Ident.
*/
private String ident;
/**
* IRC server.
*/
private IRCServer server;
/**
* Custom SSL Context.
*/
private SSLContext sslContext = null;
/**
* Construct ServerParameters instance.
* @param nickName nick name
* @param realName real name
* @param ident ident
* @param server IRC server instance
*/
private ServerParameters(String nickName, String realName,
String ident, IRCServer server)
{
@ -1415,54 +1467,128 @@ private ServerParameters(String nickName, String realName,
this.server = server;
}
/**
* Get nick name.
*
* @return returns nick name
*/
@Override
public String getNickname()
{
return this.nick;
}
/**
* Set new nick name.
*
* @param nick nick name
*/
public void setNickname(String nick)
{
this.nick = checkNick(nick);
}
/**
* Verify nick name.
*
* @param nick nick name
* @return returns nick name
* @throws IllegalArgumentException throws
* <tt>IllegalArgumentException</tt> if an invalid nick name
* is provided.
*/
private String checkNick(String nick)
{
if (nick == null)
throw new IllegalArgumentException("a nick name must be provided");
throw new IllegalArgumentException(
"a nick name must be provided");
if (nick.startsWith("#"))
throw new IllegalArgumentException("the nick name must not start with '#' since this is reserved for IRC channels");
throw new IllegalArgumentException(
"the nick name must not start with '#' "
+ "since this is reserved for IRC channels");
return nick;
}
/**
* Get alternative nick names.
*
* @return returns list of alternatives
*/
@Override
public List<String> getAlternativeNicknames()
{
return this.alternativeNicks;
}
/**
* Get ident string.
*
* @return returns ident
*/
@Override
public String getIdent()
{
return this.ident;
}
/**
* Get real name
*
* @return returns real name
*/
@Override
public String getRealname()
{
return this.real;
}
/**
* Get server
*
* @return returns server instance
*/
@Override
public IRCServer getServer()
{
return this.server;
}
/**
* Set server instance.
*
* @param server IRC server instance
*/
public void setServer(IRCServer server)
{
if (server == null)
throw new IllegalArgumentException("server cannot be null");
this.server = server;
}
/**
* Get the SSL Context.
*
* Returns the custom SSLContext or null in case there is no
* custom implementation.
*
* @return returns the SSLContext or null
*/
@Override
public SSLContext getCustomContext()
{
return this.sslContext;
}
/**
* Set custom SSLContext.
*
* @param context the custom SSLContext
*/
public void setCustomContext(SSLContext context)
{
this.sslContext = context;
}
}
/**

@ -24,7 +24,7 @@ public class ProtocolProviderFactoryIrcImpl
{
public ProtocolProviderFactoryIrcImpl()
{
super(IrcActivator.bundleContext, ProtocolNames.IRC);
super(IrcActivator.getBundleContext(), ProtocolNames.IRC);
}
/**
@ -42,7 +42,7 @@ public ProtocolProviderFactoryIrcImpl()
public AccountID installAccount( String userIDStr,
Map<String, String> accountProperties)
{
BundleContext context = IrcActivator.bundleContext;
BundleContext context = IrcActivator.getBundleContext();
if (context == null)
throw new NullPointerException(

@ -6,6 +6,7 @@
*/
package net.java.sip.communicator.impl.protocol.irc;
import net.java.sip.communicator.service.certificate.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
@ -25,7 +26,7 @@ public class ProtocolProviderServiceIrcImpl
/**
* The irc server.
*/
private IrcStack ircStack;
private IrcStack ircStack = null;
/**
* The id of the account that this protocol provider represents.
@ -151,6 +152,7 @@ public RegistrationState getRegistrationState()
return currentRegistrationState;
}
/**
* Starts the registration process.
*

@ -9,6 +9,7 @@ Import-Package: org.osgi.framework,
org.jitsi.service.resources,
net.java.sip.communicator.service.resources,
net.java.sip.communicator.util,
net.java.sip.communicator.service.certificate,
net.java.sip.communicator.service.protocol,
net.java.sip.communicator.service.protocol.event,
org.slf4j,

Loading…
Cancel
Save