Added handling of Certificate encoding and decoding.

This commit is contained in:
Simone Bordet 2012-06-01 15:56:41 +02:00
parent 395d49ba71
commit 91105910ca
5 changed files with 42 additions and 6 deletions

View File

@ -18,10 +18,13 @@ package org.eclipse.jetty.spdy.generator;
import java.nio.ByteBuffer;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.spdy.ByteBufferPool;
import org.eclipse.jetty.spdy.SessionException;
import org.eclipse.jetty.spdy.api.SessionStatus;
import org.eclipse.jetty.spdy.frames.ControlFrame;
import org.eclipse.jetty.spdy.frames.CredentialFrame;
@ -65,7 +68,16 @@ public class CredentialGenerator extends ControlFrameGenerator
private List<byte[]> serializeCertificates(Certificate[] certificates)
{
// TODO
return new ArrayList<>();
try
{
List<byte[]> result = new ArrayList<>(certificates.length);
for (Certificate certificate : certificates)
result.add(certificate.getEncoded());
return result;
}
catch (CertificateEncodingException x)
{
throw new SessionException(SessionStatus.PROTOCOL_ERROR, x);
}
}
}

View File

@ -16,8 +16,11 @@
package org.eclipse.jetty.spdy.parser;
import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -229,8 +232,15 @@ public class CredentialBodyParser extends ControlFrameBodyParser
private Certificate deserializeCertificate(byte[] bytes)
{
// TODO
return null;
try
{
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
return certificateFactory.generateCertificate(new ByteArrayInputStream(bytes));
}
catch (CertificateException x)
{
throw new SessionException(SessionStatus.PROTOCOL_ERROR, x);
}
}
private void onCredential()

View File

@ -16,7 +16,9 @@
package org.eclipse.jetty.spdy.frames;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.security.KeyStore;
import java.security.cert.Certificate;
import org.eclipse.jetty.spdy.StandardByteBufferPool;
@ -24,6 +26,7 @@ import org.eclipse.jetty.spdy.StandardCompressionFactory;
import org.eclipse.jetty.spdy.api.SPDY;
import org.eclipse.jetty.spdy.generator.Generator;
import org.eclipse.jetty.spdy.parser.Parser;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.Assert;
import org.junit.Test;
@ -34,7 +37,10 @@ public class CredentialGenerateParseTest
{
short slot = 1;
byte[] proof = new byte[]{0, 1, 2};
Certificate[] certificates = new Certificate[0]; // TODO
Certificate[] temp = loadCertificates();
Certificate[] certificates = new Certificate[temp.length * 2];
System.arraycopy(temp, 0, certificates, 0, temp.length);
System.arraycopy(temp, 0, certificates, temp.length, temp.length);
CredentialFrame frame1 = new CredentialFrame(SPDY.V3, slot, proof, certificates);
Generator generator = new Generator(new StandardByteBufferPool(), new StandardCompressionFactory().newCompressor());
ByteBuffer buffer = generator.control(frame1);
@ -62,7 +68,7 @@ public class CredentialGenerateParseTest
{
short slot = 1;
byte[] proof = new byte[]{0, 1, 2};
Certificate[] certificates = new Certificate[0]; // TODO
Certificate[] certificates = loadCertificates();
CredentialFrame frame1 = new CredentialFrame(SPDY.V3, slot, proof, certificates);
Generator generator = new Generator(new StandardByteBufferPool(), new StandardCompressionFactory().newCompressor());
ByteBuffer buffer = generator.control(frame1);
@ -85,4 +91,12 @@ public class CredentialGenerateParseTest
Assert.assertArrayEquals(proof, credential.getProof());
Assert.assertArrayEquals(certificates, credential.getCertificateChain());
}
private Certificate[] loadCertificates() throws Exception
{
KeyStore keyStore = KeyStore.getInstance("JKS");
InputStream keyStoreStream = Resource.newResource("src/test/resources/keystore.jks").getInputStream();
keyStore.load(keyStoreStream, "storepwd".toCharArray());
return keyStore.getCertificateChain("mykey");
}
}

Binary file not shown.

Binary file not shown.