View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.spdy.generator;
20  
21  import java.nio.ByteBuffer;
22  import java.security.cert.Certificate;
23  import java.security.cert.CertificateEncodingException;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.eclipse.jetty.io.ByteBufferPool;
28  import org.eclipse.jetty.spdy.SessionException;
29  import org.eclipse.jetty.spdy.api.SessionStatus;
30  import org.eclipse.jetty.spdy.frames.ControlFrame;
31  import org.eclipse.jetty.spdy.frames.CredentialFrame;
32  import org.eclipse.jetty.util.BufferUtil;
33  
34  public class CredentialGenerator extends ControlFrameGenerator
35  {
36      public CredentialGenerator(ByteBufferPool bufferPool)
37      {
38          super(bufferPool);
39      }
40  
41      @Override
42      public ByteBuffer generate(ControlFrame frame)
43      {
44          CredentialFrame credential = (CredentialFrame)frame;
45  
46          byte[] proof = credential.getProof();
47  
48          List<byte[]> certificates = serializeCertificates(credential.getCertificateChain());
49          int certificatesLength = 0;
50          for (byte[] certificate : certificates)
51              certificatesLength += certificate.length;
52  
53          int frameBodyLength = 2 + 4 + proof.length + certificates.size() * 4 + certificatesLength;
54  
55          int totalLength = ControlFrame.HEADER_LENGTH + frameBodyLength;
56          ByteBuffer buffer = getByteBufferPool().acquire(totalLength, Generator.useDirectBuffers);
57          BufferUtil.clearToFill(buffer);
58          generateControlFrameHeader(credential, frameBodyLength, buffer);
59  
60          buffer.putShort(credential.getSlot());
61          buffer.putInt(proof.length);
62          buffer.put(proof);
63          for (byte[] certificate : certificates)
64          {
65              buffer.putInt(certificate.length);
66              buffer.put(certificate);
67          }
68  
69          buffer.flip();
70          return buffer;
71      }
72  
73      private List<byte[]> serializeCertificates(Certificate[] certificates)
74      {
75          try
76          {
77              List<byte[]> result = new ArrayList<>(certificates.length);
78              for (Certificate certificate : certificates)
79                  result.add(certificate.getEncoded());
80              return result;
81          }
82          catch (CertificateEncodingException x)
83          {
84              throw new SessionException(SessionStatus.PROTOCOL_ERROR, x);
85          }
86      }
87  }