1/*
2 * Copyright (C) 2016 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 libcore.tlswire.handshake;
18
19import libcore.tlswire.util.IoUtils;
20import java.io.ByteArrayInputStream;
21import java.io.DataInputStream;
22import java.io.IOException;
23import java.util.ArrayList;
24import java.util.List;
25
26/**
27 * {@code elliptic_curves} {@link HelloExtension} from RFC 4492 section 5.1.1.
28 */
29public class EllipticCurvesHelloExtension extends HelloExtension {
30    public List<EllipticCurve> supported;
31    public boolean wellFormed;
32
33    @Override
34    protected void parseData() throws IOException {
35        byte[] ellipticCurvesListBytes = IoUtils.readTlsVariableLengthByteVector(
36                new DataInputStream(new ByteArrayInputStream(data)), 0xffff);
37        ByteArrayInputStream ellipticCurvesListIn = new ByteArrayInputStream(ellipticCurvesListBytes);
38        DataInputStream in = new DataInputStream(ellipticCurvesListIn);
39        wellFormed = (ellipticCurvesListIn.available() % 2) == 0;
40        supported = new ArrayList<EllipticCurve>(ellipticCurvesListIn.available() / 2);
41        while (ellipticCurvesListIn.available() >= 2) {
42            int curve_id = in.readUnsignedShort();
43            supported.add(EllipticCurve.fromIdentifier(curve_id));
44        }
45    }
46
47    @Override
48    public String toString() {
49        StringBuilder sb = new StringBuilder("HelloExtension{type: elliptic_curves, wellFormed: ");
50        sb.append(wellFormed);
51        sb.append(", supported: ");
52        sb.append(supported);
53        sb.append('}');
54        return sb.toString();
55    }
56}
57