summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPierrick MARIE2012-07-05 13:05:17 (EDT)
committer Benjamin Cabé2012-07-05 13:05:17 (EDT)
commit345c312def2b072327c3f2e0093751dec265e64f (patch)
tree9ba56c03c667b7cf598e22191456ca55059f66bd
parentf8bc3c519aba20dac8f592154d5c0d031f149004 (diff)
downloadorg.eclipse.koneki.protocols-345c312def2b072327c3f2e0093751dec265e64f.zip
org.eclipse.koneki.protocols-345c312def2b072327c3f2e0093751dec265e64f.tar.gz
org.eclipse.koneki.protocols-345c312def2b072327c3f2e0093751dec265e64f.tar.bz2
A new step to make the hmac authentication, it stil not work
-rw-r--r--omadm/bundles/org.eclipse.koneki.protocols.omadm.client.http/src/org/eclipse/koneki/protocols/omadm/client/http/internal/DMHttpClient.java29
-rw-r--r--omadm/bundles/org.eclipse.koneki.protocols.omadm.client/src/org/eclipse/koneki/protocols/omadm/client/basic/DMBasicClient.java3
-rw-r--r--omadm/bundles/org.eclipse.koneki.protocols.omadm.client/src/org/eclipse/koneki/protocols/omadm/client/basic/DMBasicSession.java129
3 files changed, 143 insertions, 18 deletions
diff --git a/omadm/bundles/org.eclipse.koneki.protocols.omadm.client.http/src/org/eclipse/koneki/protocols/omadm/client/http/internal/DMHttpClient.java b/omadm/bundles/org.eclipse.koneki.protocols.omadm.client.http/src/org/eclipse/koneki/protocols/omadm/client/http/internal/DMHttpClient.java
index 485713d..4687409 100644
--- a/omadm/bundles/org.eclipse.koneki.protocols.omadm.client.http/src/org/eclipse/koneki/protocols/omadm/client/http/internal/DMHttpClient.java
+++ b/omadm/bundles/org.eclipse.koneki.protocols.omadm.client.http/src/org/eclipse/koneki/protocols/omadm/client/http/internal/DMHttpClient.java
@@ -10,10 +10,12 @@
*******************************************************************************/
package org.eclipse.koneki.protocols.omadm.client.http.internal;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
+import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
@@ -25,6 +27,7 @@ import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
+import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.eclipse.koneki.protocols.omadm.client.DMClientException;
import org.eclipse.koneki.protocols.omadm.client.basic.DMBasicClient;
@@ -45,18 +48,34 @@ public class DMHttpClient extends DMBasicClient {
try {
final HttpPost post = new HttpPost(server);
+ final ByteArrayOutputStream myByteArray = new ByteArrayOutputStream();
+
+ messenger.writeMessage(myByteArray);
+
final EntityTemplate entity = new EntityTemplate(new ContentProducer() {
@Override
public void writeTo(final OutputStream out) throws IOException {
- try {
- messenger.writeMessage(out);
- } catch (final DMClientException e) {
- throw new IOException(e);
- }
+ // try {
+ // messenger.writeMessage(out);
+ // } catch (final DMClientException e) {
+ // throw new IOException(e);
+ // }
+
+ out.write(myByteArray.toByteArray());
+
}
});
+
+ /*
+ * TODO Call this method break the md5 authentication !
+ */
+ String test = messenger.getAuthenticationValue(myByteArray);
+
+ Header header = new BasicHeader("x-syncml-hmac", test);
+ post.addHeader(header);
+
entity.setChunked(false);
entity.setContentEncoding(encoding);
entity.setContentType("application/vnd.syncml.dm+xml"); //$NON-NLS-1$
diff --git a/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/src/org/eclipse/koneki/protocols/omadm/client/basic/DMBasicClient.java b/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/src/org/eclipse/koneki/protocols/omadm/client/basic/DMBasicClient.java
index 7e01aa8..4da6dc3 100644
--- a/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/src/org/eclipse/koneki/protocols/omadm/client/basic/DMBasicClient.java
+++ b/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/src/org/eclipse/koneki/protocols/omadm/client/basic/DMBasicClient.java
@@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.koneki.protocols.omadm.client.basic;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -96,6 +97,8 @@ public abstract class DMBasicClient implements DMClient {
void readMessage(final InputStream in) throws DMClientException;
+ String getAuthenticationValue(final ByteArrayOutputStream out) throws DMClientException;
+
}
protected abstract void sendAndReceiveMessage(final URI server, final String encoding, final DMMessenger messenger) throws IOException,
diff --git a/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/src/org/eclipse/koneki/protocols/omadm/client/basic/DMBasicSession.java b/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/src/org/eclipse/koneki/protocols/omadm/client/basic/DMBasicSession.java
index dc8e13d..43109fc 100644
--- a/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/src/org/eclipse/koneki/protocols/omadm/client/basic/DMBasicSession.java
+++ b/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/src/org/eclipse/koneki/protocols/omadm/client/basic/DMBasicSession.java
@@ -82,6 +82,7 @@ final class DMBasicSession implements Runnable {
private final DMAuthentication authentication;
private String nextNonce;
private boolean isAuthSessionContinue;
+ private int hmacMessageNumber;
public DMBasicSession(final DMBasicClient dmClient, final URI server, final DMAuthentication userAuth, final URI client, final String sessionId,
final DMNode[] devInfoNodes, final CommandHandler commandHandler, final ProtocolListener[] protocolLinsteners,
@@ -99,6 +100,7 @@ final class DMBasicSession implements Runnable {
this.statusManager = new DMStatusManager();
this.commandSends = new HashMap<String, Object[]>();
this.nextNonce = ""; //$NON-NLS-1$
+ this.hmacMessageNumber = 0;
}
@Override
@@ -125,6 +127,35 @@ final class DMBasicSession implements Runnable {
void sendPackageAndReceivePackage() throws IOException, DMClientException {
this.dmClient.sendAndReceiveMessage(this.server, ENCODING, new DMMessenger() {
+
+ private String headerAuthentication = "algorithm=MD5, username=\"" + authentication.getUser() + "\",mac=";
+
+ public String getAuthenticationValue(final ByteArrayOutputStream message) throws DMClientException {
+
+ try {
+
+ /*
+ * TODO <begin : improve>
+ */
+ /*
+ * Get the next nonce value is not secure : the method should parse all the message and don't find the next nonce node. So the
+ * next nonce is searched into a copy of the inputStream
+ */
+ /*
+ * TODO Remove the test for the tests
+ */
+ // if (authentication.getAuthenticationType() == AuthenticationType.HMAC) {
+
+ headerAuthentication += computeMACAuthentication(message.toString(ENCODING));
+ // }
+
+ } catch (final UnsupportedEncodingException e) {
+ throw new DMClientException(e);
+ }
+
+ return headerAuthentication;
+ }
+
@Override
public void writeMessage(final OutputStream out) throws DMClientException {
try {
@@ -178,12 +209,6 @@ final class DMBasicSession implements Runnable {
DMBasicSession.this.fireManagementPhaseEnd();
}
- // try {
- // in.close();
- // } catch (IOException e) {
- // // Auto-generated catch block
- // }
-
} catch (final XMLStreamException e) {
throw new DMClientException(e);
} catch (final UnsupportedEncodingException e) {
@@ -195,7 +220,12 @@ final class DMBasicSession implements Runnable {
private void writeAuthentication(final XMLStreamWriter writer) throws XMLStreamException {
- writer.writeStartElement("Cred"); //$NON-NLS-1$
+ /*
+ * There are no credentials with HMAC
+ */
+ if (authentication.getAuthenticationType() != AuthenticationType.HMAC) {
+ writer.writeStartElement("Cred"); //$NON-NLS-1$
+ }
writer.writeStartElement("Meta"); //$NON-NLS-1$
@@ -218,8 +248,9 @@ final class DMBasicSession implements Runnable {
writer.writeEndElement();
writer.writeStartElement("Data"); //$NON-NLS-1$
-
writer.writeCharacters(computeBasicAuthentication());
+ writer.writeEndElement();
+
break;
/*
* Add md5 authentication
@@ -232,17 +263,84 @@ final class DMBasicSession implements Runnable {
writer.writeEndElement();
writer.writeStartElement("Data"); //$NON-NLS-1$
-
writer.writeCharacters(computeMd5Authentication());
+ writer.writeEndElement();
break;
- }
+ /*
+ * Add hmac authentication
+ */
+ case HMAC:
- writer.writeEndElement();
+ writer.writeCharacters("syncml:auth-MAC"); //$NON-NLS-1$
+ writer.writeEndElement();
- writer.writeEndElement();
+ writer.writeEndElement();
+
+ // writer.writeStartElement("Data"); //$NON-NLS-1$
+ // writer.writeCharacters(computeMd5Authentication());
+
+ break;
+ }
+
+ /*
+ * There are no credential with HMAC
+ */
+ if (authentication.getAuthenticationType() != AuthenticationType.HMAC) {
+ writer.writeEndElement();
+ }
}
+ // private void writeAuthentication(final XMLStreamWriter writer) throws XMLStreamException {
+ //
+ // writer.writeStartElement("Cred"); //$NON-NLS-1$
+ //
+ // writer.writeStartElement("Meta"); //$NON-NLS-1$
+ //
+ // writer.writeStartElement("Format"); //$NON-NLS-1$
+ // writer.writeAttribute("xmlns", "syncml:metinf"); //$NON-NLS-1$ //$NON-NLS-2$
+ // writer.writeCharacters("b64"); //$NON-NLS-1$
+ // writer.writeEndElement();
+ //
+ // writer.writeStartElement("Type"); //$NON-NLS-1$
+ // writer.writeAttribute("xmlns", "syncml:metinf"); //$NON-NLS-1$ //$NON-NLS-2$
+ //
+ // switch (authentication.getAuthenticationType()) {
+ // /*
+ // * Add basic authentication
+ // */
+ // case BASIC:
+ // writer.writeCharacters("syncml:auth-basic"); //$NON-NLS-1$
+ // writer.writeEndElement();
+ //
+ // writer.writeEndElement();
+ //
+ // writer.writeStartElement("Data"); //$NON-NLS-1$
+ //
+ // writer.writeCharacters(computeBasicAuthentication());
+ // break;
+ // /*
+ // * Add md5 authentication
+ // */
+ // case MD5:
+ //
+ // writer.writeCharacters("syncml:auth-md5"); //$NON-NLS-1$
+ // writer.writeEndElement();
+ //
+ // writer.writeEndElement();
+ //
+ // writer.writeStartElement("Data"); //$NON-NLS-1$
+ //
+ // writer.writeCharacters(computeMd5Authentication());
+ //
+ // break;
+ // }
+ //
+ // writer.writeEndElement();
+ //
+ // writer.writeEndElement();
+ // }
+
private byte[] computeB64OfMd5OfUsernamePasswordPlusNonce() {
byte[] userNonce = null;
@@ -833,6 +931,7 @@ final class DMBasicSession implements Runnable {
// Performs the status
if (cmd.equals("SyncHdr")) { //$NON-NLS-1$
+ this.hmacMessageNumber++;
switch (data) {
case 212:
this.isClientAuthenticated = true;
@@ -842,13 +941,17 @@ final class DMBasicSession implements Runnable {
this.isClientAuthenticated = false;
if ((!nextNonce.equals("")) && (authentication.getAuthenticationType() == AuthenticationType.HMAC)) {
this.isAuthSessionContinue = true;
+ } else {
+ this.isAuthSessionContinue = false;
}
break;
case 401:
this.isClientAuthenticated = false;
if (authentication.getAuthenticationType() == AuthenticationType.MD5
- || ((!nextNonce.equals("")) && (authentication.getAuthenticationType() == AuthenticationType.HMAC))) {
+ || ((hmacMessageNumber < 2) && (authentication.getAuthenticationType() == AuthenticationType.HMAC))) {
this.isAuthSessionContinue = true;
+ } else {
+ this.isAuthSessionContinue = false;
}
break;
default: