blob: e1318a8930d43461ba371643be263a4069704b0a [file] [log] [blame]
Adam Langley6e8184d2009-11-02 18:25:20 -08001// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package tls
6
7import (
8 "crypto/rsa";
9 "io";
10 "os";
11)
12
13const (
14 // maxTLSCiphertext is the maximum length of a plaintext payload.
15 maxTLSPlaintext = 16384;
16 // maxTLSCiphertext is the maximum length payload after compression and encryption.
Robert Griesemerbaba2922009-11-09 21:13:17 -080017 maxTLSCiphertext = 16384 + 2048;
Adam Langley6e8184d2009-11-02 18:25:20 -080018 // maxHandshakeMsg is the largest single handshake message that we'll buffer.
19 maxHandshakeMsg = 65536;
Adam Langley6e0842d2009-11-21 15:53:03 -080020 // defaultMajor and defaultMinor are the maximum TLS version that we support.
21 defaultMajor = 3;
22 defaultMinor = 2;
Adam Langley6e8184d2009-11-02 18:25:20 -080023)
24
25
26// TLS record types.
27type recordType uint8
28
29const (
30 recordTypeChangeCipherSpec recordType = 20;
31 recordTypeAlert recordType = 21;
32 recordTypeHandshake recordType = 22;
33 recordTypeApplicationData recordType = 23;
34)
35
36// TLS handshake message types.
37const (
38 typeClientHello uint8 = 1;
39 typeServerHello uint8 = 2;
40 typeCertificate uint8 = 11;
41 typeServerHelloDone uint8 = 14;
42 typeClientKeyExchange uint8 = 16;
43 typeFinished uint8 = 20;
44)
45
46// TLS cipher suites.
47var (
48 TLS_RSA_WITH_RC4_128_SHA uint16 = 5;
49)
50
51// TLS compression types.
52var (
53 compressionNone uint8 = 0;
54)
55
56type ConnectionState struct {
57 HandshakeComplete bool;
58 CipherSuite string;
59 Error alertType;
60}
61
62// A Config structure is used to configure a TLS client or server. After one
63// has been passed to a TLS function it must not be modified.
64type Config struct {
65 // Rand provides the source of entropy for nonces and RSA blinding.
66 Rand io.Reader;
67 // Time returns the current time as the number of seconds since the epoch.
68 Time func() int64;
69 Certificates []Certificate;
Adam Langley6e0842d2009-11-21 15:53:03 -080070 RootCAs *CASet;
Adam Langley6e8184d2009-11-02 18:25:20 -080071}
72
73type Certificate struct {
74 Certificate [][]byte;
75 PrivateKey *rsa.PrivateKey;
76}
77
78// A TLS record.
79type record struct {
80 contentType recordType;
81 major, minor uint8;
82 payload []byte;
83}
84
85type handshakeMessage interface {
86 marshal() []byte;
87}
88
89type encryptor interface {
90 // XORKeyStream xors the contents of the slice with bytes from the key stream.
91 XORKeyStream(buf []byte);
92}
93
94// mutualVersion returns the protocol version to use given the advertised
95// version of the peer.
96func mutualVersion(theirMajor, theirMinor uint8) (major, minor uint8, ok bool) {
97 // We don't deal with peers < TLS 1.0 (aka version 3.1).
98 if theirMajor < 3 || theirMajor == 3 && theirMinor < 1 {
Robert Griesemer40621d52009-11-09 12:07:39 -080099 return 0, 0, false
Adam Langley6e8184d2009-11-02 18:25:20 -0800100 }
101 major = 3;
102 minor = 2;
103 if theirMinor < minor {
Robert Griesemer40621d52009-11-09 12:07:39 -0800104 minor = theirMinor
Adam Langley6e8184d2009-11-02 18:25:20 -0800105 }
106 ok = true;
107 return;
108}
109
110// A nop implements the NULL encryption and MAC algorithms.
111type nop struct{}
112
Robert Griesemer368f8cb2009-11-06 14:24:38 -0800113func (nop) XORKeyStream(buf []byte) {}
Adam Langley6e8184d2009-11-02 18:25:20 -0800114
Robert Griesemer368f8cb2009-11-06 14:24:38 -0800115func (nop) Write(buf []byte) (int, os.Error) { return len(buf), nil }
Adam Langley6e8184d2009-11-02 18:25:20 -0800116
Robert Griesemer368f8cb2009-11-06 14:24:38 -0800117func (nop) Sum() []byte { return nil }
Adam Langley6e8184d2009-11-02 18:25:20 -0800118
Robert Griesemer368f8cb2009-11-06 14:24:38 -0800119func (nop) Reset() {}
Adam Langley6e8184d2009-11-02 18:25:20 -0800120
Robert Griesemer368f8cb2009-11-06 14:24:38 -0800121func (nop) Size() int { return 0 }