From 91105910ca9124caea28a9b1812f3f03d1d61fba Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Fri, 1 Jun 2012 15:56:41 +0200 Subject: [PATCH] Added handling of Certificate encoding and decoding. --- .../spdy/generator/CredentialGenerator.java | 16 ++++++++++++++-- .../spdy/parser/CredentialBodyParser.java | 14 ++++++++++++-- .../frames/CredentialGenerateParseTest.java | 18 ++++++++++++++++-- .../spdy-core/src/test/resources/keystore.jks | Bin 0 -> 2206 bytes .../src/test/resources/truststore.jks | Bin 0 -> 916 bytes 5 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 jetty-spdy/spdy-core/src/test/resources/keystore.jks create mode 100644 jetty-spdy/spdy-core/src/test/resources/truststore.jks diff --git a/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/generator/CredentialGenerator.java b/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/generator/CredentialGenerator.java index cb7bf319ec2..f46d39313fb 100644 --- a/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/generator/CredentialGenerator.java +++ b/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/generator/CredentialGenerator.java @@ -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 serializeCertificates(Certificate[] certificates) { - // TODO - return new ArrayList<>(); + try + { + List 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); + } } } diff --git a/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/parser/CredentialBodyParser.java b/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/parser/CredentialBodyParser.java index c39af7b6b0d..c79129ce163 100644 --- a/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/parser/CredentialBodyParser.java +++ b/jetty-spdy/spdy-core/src/main/java/org/eclipse/jetty/spdy/parser/CredentialBodyParser.java @@ -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() diff --git a/jetty-spdy/spdy-core/src/test/java/org/eclipse/jetty/spdy/frames/CredentialGenerateParseTest.java b/jetty-spdy/spdy-core/src/test/java/org/eclipse/jetty/spdy/frames/CredentialGenerateParseTest.java index 2b210b940d6..0678c768ebc 100644 --- a/jetty-spdy/spdy-core/src/test/java/org/eclipse/jetty/spdy/frames/CredentialGenerateParseTest.java +++ b/jetty-spdy/spdy-core/src/test/java/org/eclipse/jetty/spdy/frames/CredentialGenerateParseTest.java @@ -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"); + } } diff --git a/jetty-spdy/spdy-core/src/test/resources/keystore.jks b/jetty-spdy/spdy-core/src/test/resources/keystore.jks new file mode 100644 index 0000000000000000000000000000000000000000..428ba54776ede2fdcdeedd879edb927c2abd9953 GIT binary patch literal 2206 zcmcgt`9Bkm8{cNkoMUp6gmShKn!AQX*(l6Nj(i=TnQPOKYtv{*Wg>ItE=Q!pRYH8a z$Sp#S#2lYw#aw;$y9u4T}83H*%lp zAKZay0sy=q1Qoo85aAQh;$ zD(c2EIN#D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-spdy/spdy-core/src/test/resources/truststore.jks b/jetty-spdy/spdy-core/src/test/resources/truststore.jks new file mode 100644 index 0000000000000000000000000000000000000000..839cb8c35151c2b7c64afca24b6b72caad070a05 GIT binary patch literal 916 zcmezO_TO6u1_mY|W(3o$xs} zE~X|%Muz1J{3AIFGbaABoD&*5saD@gH|APIn|qhRGl}gsUzm=o9G*UXZaLfkb^*)o zjA*-gTf)`m_MQJYE&gJ}p^PHkrj!4^W|XX5a=N7A{;n#yaON&k_bHloe-^*hm?Z91 zlB>xeD=<(C>yn{9D54u}krkl}HQ(Uscha(++qf!T9y+xaEfnXd1O zi0)T?voO%;QH9LK;*_O3mBblqm)!31vU@hm;^%>mh5U@y3R%l0gzi`2yxH!+?kPOi zt!Tnsz1x9B3U2~8STZp)GB6^C5HPs_Lx_=~O<3xi>MmQ;D_g$D<_pdct`+TyzWTQ= zW5Finm(sGEe;ty^>vg$!cV)t>;H#Mev23$*WWBpyJ}Ir;RW+Htrt6{Pk&qz&-XG2@ z8@{&Lu%DX7m47Uny+-3w`=4V611q#Ub(U`xZCtSK^2LO^3(s|HW&N14dV4@A&(kX% z*S_eUPs-bSWRp>avt;CP@7K+G&3=b&1eO-s3f`;Cf91p#$)FW&xME3L8sEBQQDVCvfG>mdwqnk+GXd2ihXqpv z;usF(WoYYmu8DZZa4%1z=+hI+*gpkUykAy5tj#grb*gH!M6TqIcifYBGVe^&T#-2O K*=+x>r_BKeJV|!| literal 0 HcmV?d00001