Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

common.go 2.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. package tls
  5. import (
  6. "crypto/rsa";
  7. "io";
  8. "os";
  9. )
  10. const (
  11. // maxTLSCiphertext is the maximum length of a plaintext payload.
  12. maxTLSPlaintext = 16384;
  13. // maxTLSCiphertext is the maximum length payload after compression and encryption.
  14. maxTLSCiphertext = 16384+2048;
  15. // maxHandshakeMsg is the largest single handshake message that we'll buffer.
  16. maxHandshakeMsg = 65536;
  17. )
  18. // TLS record types.
  19. type recordType uint8
  20. const (
  21. recordTypeChangeCipherSpec recordType = 20;
  22. recordTypeAlert recordType = 21;
  23. recordTypeHandshake recordType = 22;
  24. recordTypeApplicationData recordType = 23;
  25. )
  26. // TLS handshake message types.
  27. const (
  28. typeClientHello uint8 = 1;
  29. typeServerHello uint8 = 2;
  30. typeCertificate uint8 = 11;
  31. typeServerHelloDone uint8 = 14;
  32. typeClientKeyExchange uint8 = 16;
  33. typeFinished uint8 = 20;
  34. )
  35. // TLS cipher suites.
  36. var (
  37. TLS_RSA_WITH_RC4_128_SHA uint16 = 5;
  38. )
  39. // TLS compression types.
  40. var (
  41. compressionNone uint8 = 0;
  42. )
  43. type ConnectionState struct {
  44. HandshakeComplete bool;
  45. CipherSuite string;
  46. Error alertType;
  47. }
  48. // A Config structure is used to configure a TLS client or server. After one
  49. // has been passed to a TLS function it must not be modified.
  50. type Config struct {
  51. // Rand provides the source of entropy for nonces and RSA blinding.
  52. Rand io.Reader;
  53. // Time returns the current time as the number of seconds since the epoch.
  54. Time func() int64;
  55. Certificates []Certificate;
  56. }
  57. type Certificate struct {
  58. Certificate [][]byte;
  59. PrivateKey *rsa.PrivateKey;
  60. }
  61. // A TLS record.
  62. type record struct {
  63. contentType recordType;
  64. major, minor uint8;
  65. payload []byte;
  66. }
  67. type handshakeMessage interface {
  68. marshal() []byte;
  69. }
  70. type encryptor interface {
  71. // XORKeyStream xors the contents of the slice with bytes from the key stream.
  72. XORKeyStream(buf []byte);
  73. }
  74. // mutualVersion returns the protocol version to use given the advertised
  75. // version of the peer.
  76. func mutualVersion(theirMajor, theirMinor uint8) (major, minor uint8, ok bool) {
  77. // We don't deal with peers < TLS 1.0 (aka version 3.1).
  78. if theirMajor < 3 || theirMajor == 3 && theirMinor < 1 {
  79. return 0, 0, false;
  80. }
  81. major = 3;
  82. minor = 2;
  83. if theirMinor < minor {
  84. minor = theirMinor;
  85. }
  86. ok = true;
  87. return;
  88. }
  89. // A nop implements the NULL encryption and MAC algorithms.
  90. type nop struct{}
  91. func (nop) XORKeyStream(buf []byte) {}
  92. func (nop) Write(buf []byte) (int, os.Error) {
  93. return len(buf), nil;
  94. }
  95. func (nop) Sum() []byte {
  96. return nil;
  97. }
  98. func (nop) Reset() {}
  99. func (nop) Size() int {
  100. return 0;
  101. }