tls_info.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package starter
  15. import (
  16. "crypto/tls"
  17. "crypto/x509"
  18. "encoding/pem"
  19. "fmt"
  20. "io/ioutil"
  21. )
  22. // TLSInfo holds the SSL certificates paths.
  23. type TLSInfo struct {
  24. CertFile string `json:"CertFile"`
  25. KeyFile string `json:"KeyFile"`
  26. CAFile string `json:"CAFile"`
  27. }
  28. func (info TLSInfo) Scheme() string {
  29. if info.KeyFile != "" && info.CertFile != "" {
  30. return "https"
  31. } else {
  32. return "http"
  33. }
  34. }
  35. // Generates a tls.Config object for a server from the given files.
  36. func (info TLSInfo) ServerConfig() (*tls.Config, error) {
  37. // Both the key and cert must be present.
  38. if info.KeyFile == "" || info.CertFile == "" {
  39. return nil, fmt.Errorf("KeyFile and CertFile must both be present[key: %v, cert: %v]", info.KeyFile, info.CertFile)
  40. }
  41. var cfg tls.Config
  42. tlsCert, err := tls.LoadX509KeyPair(info.CertFile, info.KeyFile)
  43. if err != nil {
  44. return nil, err
  45. }
  46. cfg.Certificates = []tls.Certificate{tlsCert}
  47. if info.CAFile != "" {
  48. cfg.ClientAuth = tls.RequireAndVerifyClientCert
  49. cp, err := newCertPool(info.CAFile)
  50. if err != nil {
  51. return nil, err
  52. }
  53. cfg.RootCAs = cp
  54. cfg.ClientCAs = cp
  55. } else {
  56. cfg.ClientAuth = tls.NoClientCert
  57. }
  58. return &cfg, nil
  59. }
  60. // Generates a tls.Config object for a client from the given files.
  61. func (info TLSInfo) ClientConfig() (*tls.Config, error) {
  62. var cfg tls.Config
  63. if info.KeyFile == "" || info.CertFile == "" {
  64. return &cfg, nil
  65. }
  66. tlsCert, err := tls.LoadX509KeyPair(info.CertFile, info.KeyFile)
  67. if err != nil {
  68. return nil, err
  69. }
  70. cfg.Certificates = []tls.Certificate{tlsCert}
  71. if info.CAFile != "" {
  72. cp, err := newCertPool(info.CAFile)
  73. if err != nil {
  74. return nil, err
  75. }
  76. cfg.RootCAs = cp
  77. }
  78. return &cfg, nil
  79. }
  80. // newCertPool creates x509 certPool with provided CA file
  81. func newCertPool(CAFile string) (*x509.CertPool, error) {
  82. certPool := x509.NewCertPool()
  83. pemByte, err := ioutil.ReadFile(CAFile)
  84. if err != nil {
  85. return nil, err
  86. }
  87. for {
  88. var block *pem.Block
  89. block, pemByte = pem.Decode(pemByte)
  90. if block == nil {
  91. return certPool, nil
  92. }
  93. cert, err := x509.ParseCertificate(block.Bytes)
  94. if err != nil {
  95. return nil, err
  96. }
  97. certPool.AddCert(cert)
  98. }
  99. }