Adam Langley | 6e8184d | 2009-11-02 18:25:20 -0800 | [diff] [blame] | 1 | // 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 | |
| 5 | package tls |
| 6 | |
| 7 | import ( |
| 8 | "crypto/rsa"; |
| 9 | "io"; |
| 10 | "os"; |
| 11 | ) |
| 12 | |
| 13 | const ( |
| 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 Griesemer | baba292 | 2009-11-09 21:13:17 -0800 | [diff] [blame] | 17 | maxTLSCiphertext = 16384 + 2048; |
Adam Langley | 6e8184d | 2009-11-02 18:25:20 -0800 | [diff] [blame] | 18 | // maxHandshakeMsg is the largest single handshake message that we'll buffer. |
| 19 | maxHandshakeMsg = 65536; |
Adam Langley | 6e0842d | 2009-11-21 15:53:03 -0800 | [diff] [blame] | 20 | // defaultMajor and defaultMinor are the maximum TLS version that we support. |
| 21 | defaultMajor = 3; |
| 22 | defaultMinor = 2; |
Adam Langley | 6e8184d | 2009-11-02 18:25:20 -0800 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | |
| 26 | // TLS record types. |
| 27 | type recordType uint8 |
| 28 | |
| 29 | const ( |
| 30 | recordTypeChangeCipherSpec recordType = 20; |
| 31 | recordTypeAlert recordType = 21; |
| 32 | recordTypeHandshake recordType = 22; |
| 33 | recordTypeApplicationData recordType = 23; |
| 34 | ) |
| 35 | |
| 36 | // TLS handshake message types. |
| 37 | const ( |
| 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. |
| 47 | var ( |
| 48 | TLS_RSA_WITH_RC4_128_SHA uint16 = 5; |
| 49 | ) |
| 50 | |
| 51 | // TLS compression types. |
| 52 | var ( |
| 53 | compressionNone uint8 = 0; |
| 54 | ) |
| 55 | |
| 56 | type 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. |
| 64 | type 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 Langley | 6e0842d | 2009-11-21 15:53:03 -0800 | [diff] [blame] | 70 | RootCAs *CASet; |
Adam Langley | 6e8184d | 2009-11-02 18:25:20 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | type Certificate struct { |
| 74 | Certificate [][]byte; |
| 75 | PrivateKey *rsa.PrivateKey; |
| 76 | } |
| 77 | |
| 78 | // A TLS record. |
| 79 | type record struct { |
| 80 | contentType recordType; |
| 81 | major, minor uint8; |
| 82 | payload []byte; |
| 83 | } |
| 84 | |
| 85 | type handshakeMessage interface { |
| 86 | marshal() []byte; |
| 87 | } |
| 88 | |
| 89 | type 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. |
| 96 | func 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 Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 99 | return 0, 0, false |
Adam Langley | 6e8184d | 2009-11-02 18:25:20 -0800 | [diff] [blame] | 100 | } |
| 101 | major = 3; |
| 102 | minor = 2; |
| 103 | if theirMinor < minor { |
Robert Griesemer | 40621d5 | 2009-11-09 12:07:39 -0800 | [diff] [blame] | 104 | minor = theirMinor |
Adam Langley | 6e8184d | 2009-11-02 18:25:20 -0800 | [diff] [blame] | 105 | } |
| 106 | ok = true; |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // A nop implements the NULL encryption and MAC algorithms. |
| 111 | type nop struct{} |
| 112 | |
Robert Griesemer | 368f8cb | 2009-11-06 14:24:38 -0800 | [diff] [blame] | 113 | func (nop) XORKeyStream(buf []byte) {} |
Adam Langley | 6e8184d | 2009-11-02 18:25:20 -0800 | [diff] [blame] | 114 | |
Robert Griesemer | 368f8cb | 2009-11-06 14:24:38 -0800 | [diff] [blame] | 115 | func (nop) Write(buf []byte) (int, os.Error) { return len(buf), nil } |
Adam Langley | 6e8184d | 2009-11-02 18:25:20 -0800 | [diff] [blame] | 116 | |
Robert Griesemer | 368f8cb | 2009-11-06 14:24:38 -0800 | [diff] [blame] | 117 | func (nop) Sum() []byte { return nil } |
Adam Langley | 6e8184d | 2009-11-02 18:25:20 -0800 | [diff] [blame] | 118 | |
Robert Griesemer | 368f8cb | 2009-11-06 14:24:38 -0800 | [diff] [blame] | 119 | func (nop) Reset() {} |
Adam Langley | 6e8184d | 2009-11-02 18:25:20 -0800 | [diff] [blame] | 120 | |
Robert Griesemer | 368f8cb | 2009-11-06 14:24:38 -0800 | [diff] [blame] | 121 | func (nop) Size() int { return 0 } |