1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.conscrypt;
18
19import java.security.Principal;
20import java.security.cert.Certificate;
21import java.security.cert.X509Certificate;
22import java.util.ArrayList;
23import java.util.List;
24import javax.net.ssl.SSLPeerUnverifiedException;
25import javax.net.ssl.SSLSessionContext;
26
27/**
28 * A snapshot of the content of another {@link ConscryptSession}. This copies everything over
29 * except for the certificates.
30 */
31final class SessionSnapshot implements ConscryptSession {
32    private final SSLSessionContext sessionContext;
33    private final byte[] id;
34    private final String requestedServerName;
35    private final List<byte[]> statusResponses;
36    private final byte[] peerTlsSctData;
37    private final long creationTime;
38    private final long lastAccessedTime;
39    private final String cipherSuite;
40    private final String protocol;
41    private final String peerHost;
42    private final int peerPort;
43
44    SessionSnapshot(ConscryptSession session) {
45        sessionContext = session.getSessionContext();
46        id = session.getId();
47        requestedServerName = session.getRequestedServerName();
48        statusResponses = session.getStatusResponses();
49        peerTlsSctData = session.getPeerSignedCertificateTimestamp();
50        creationTime = session.getCreationTime();
51        lastAccessedTime = session.getLastAccessedTime();
52        cipherSuite = session.getCipherSuite();
53        protocol = session.getProtocol();
54        peerHost = session.getPeerHost();
55        peerPort = session.getPeerPort();
56    }
57
58    @Override
59    public String getRequestedServerName() {
60        return requestedServerName;
61    }
62
63    @Override
64    public List<byte[]> getStatusResponses() {
65        List<byte[]> ret = new ArrayList<byte[]>(statusResponses.size());
66        for (byte[] resp : statusResponses) {
67            ret.add(resp.clone());
68        }
69        return ret;
70    }
71
72    @Override
73    public byte[] getPeerSignedCertificateTimestamp() {
74        return peerTlsSctData != null ? peerTlsSctData.clone() : null;
75    }
76
77    @Override
78    public byte[] getId() {
79        return id;
80    }
81
82    @Override
83    public SSLSessionContext getSessionContext() {
84        return sessionContext;
85    }
86
87    @Override
88    public long getCreationTime() {
89        return creationTime;
90    }
91
92    @Override
93    public long getLastAccessedTime() {
94        return lastAccessedTime;
95    }
96
97    @Override
98    public void invalidate() {
99        // Do nothing.
100    }
101
102    @Override
103    public boolean isValid() {
104        return false;
105    }
106
107    @Override
108    public void putValue(String s, Object o) {
109        throw new UnsupportedOperationException(
110                "All calls to this method should be intercepted by ProvidedSessionDecorator.");
111    }
112
113    @Override
114    public Object getValue(String s) {
115        throw new UnsupportedOperationException(
116                "All calls to this method should be intercepted by ProvidedSessionDecorator.");
117    }
118
119    @Override
120    public void removeValue(String s) {
121        throw new UnsupportedOperationException(
122                "All calls to this method should be intercepted by ProvidedSessionDecorator.");
123    }
124
125    @Override
126    public String[] getValueNames() {
127        throw new UnsupportedOperationException(
128                "All calls to this method should be intercepted by ProvidedSessionDecorator.");
129    }
130
131    @Override
132    public X509Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException {
133        throw new SSLPeerUnverifiedException("No peer certificates");
134    }
135
136    @Override
137    public Certificate[] getLocalCertificates() {
138        return null;
139    }
140
141    @Override
142    public javax.security.cert.X509Certificate[] getPeerCertificateChain()
143        throws SSLPeerUnverifiedException {
144        throw new SSLPeerUnverifiedException("No peer certificates");
145    }
146
147    @Override
148    public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
149        throw new SSLPeerUnverifiedException("No peer certificates");
150    }
151
152    @Override
153    public Principal getLocalPrincipal() {
154        return null;
155    }
156
157    @Override
158    public String getCipherSuite() {
159        return cipherSuite;
160    }
161
162    @Override
163    public String getProtocol() {
164        return protocol;
165    }
166
167    @Override
168    public String getPeerHost() {
169        return peerHost;
170    }
171
172    @Override
173    public int getPeerPort() {
174        return peerPort;
175    }
176
177    @Override
178    public int getPacketBufferSize() {
179        return NativeConstants.SSL3_RT_MAX_PACKET_SIZE;
180    }
181
182    @Override
183    public int getApplicationBufferSize() {
184        return NativeConstants.SSL3_RT_MAX_PLAIN_LENGTH;
185    }
186}
187