1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.spdy.generator;
15
16 import java.nio.ByteBuffer;
17 import java.security.cert.Certificate;
18 import java.security.cert.CertificateEncodingException;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.eclipse.jetty.spdy.ByteBufferPool;
23 import org.eclipse.jetty.spdy.SessionException;
24 import org.eclipse.jetty.spdy.api.SessionStatus;
25 import org.eclipse.jetty.spdy.frames.ControlFrame;
26 import org.eclipse.jetty.spdy.frames.CredentialFrame;
27
28 public class CredentialGenerator extends ControlFrameGenerator
29 {
30 public CredentialGenerator(ByteBufferPool bufferPool)
31 {
32 super(bufferPool);
33 }
34
35 @Override
36 public ByteBuffer generate(ControlFrame frame)
37 {
38 CredentialFrame credential = (CredentialFrame)frame;
39
40 byte[] proof = credential.getProof();
41
42 List<byte[]> certificates = serializeCertificates(credential.getCertificateChain());
43 int certificatesLength = 0;
44 for (byte[] certificate : certificates)
45 certificatesLength += certificate.length;
46
47 int frameBodyLength = 2 + 4 + proof.length + certificates.size() * 4 + certificatesLength;
48
49 int totalLength = ControlFrame.HEADER_LENGTH + frameBodyLength;
50 ByteBuffer buffer = getByteBufferPool().acquire(totalLength, true);
51 generateControlFrameHeader(credential, frameBodyLength, buffer);
52
53 buffer.putShort(credential.getSlot());
54 buffer.putInt(proof.length);
55 buffer.put(proof);
56 for (byte[] certificate : certificates)
57 {
58 buffer.putInt(certificate.length);
59 buffer.put(certificate);
60 }
61
62 buffer.flip();
63 return buffer;
64 }
65
66 private List<byte[]> serializeCertificates(Certificate[] certificates)
67 {
68 try
69 {
70 List<byte[]> result = new ArrayList<>(certificates.length);
71 for (Certificate certificate : certificates)
72 result.add(certificate.getEncoded());
73 return result;
74 }
75 catch (CertificateEncodingException x)
76 {
77 throw new SessionException(SessionStatus.PROTOCOL_ERROR, x);
78 }
79 }
80 }