certificate.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2018, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package common
  20. import (
  21. "crypto/rand"
  22. "crypto/rsa"
  23. "crypto/sha1"
  24. "crypto/x509"
  25. "crypto/x509/pkix"
  26. "encoding/pem"
  27. "math/big"
  28. "time"
  29. )
  30. // GenerateWebServerCertificate creates a self-signed web server certificate,
  31. // using the specified host name (commonName).
  32. // This is primarily intended for use by MeekServer to generate on-the-fly,
  33. // self-signed TLS certificates for fronted HTTPS mode. In this case, the nature
  34. // of the certificate is non-circumvention; it only has to be acceptable to the
  35. // front CDN making connections to meek.
  36. // The same certificates are used for unfronted HTTPS meek. In this case, the
  37. // certificates may be a fingerprint used to detect Psiphon servers or traffic.
  38. // TODO: more effort to mitigate fingerprinting these certificates.
  39. //
  40. // In addition, GenerateWebServerCertificate is used by GenerateConfig to create
  41. // Psiphon web server certificates for test/example configurations. If these Psiphon
  42. // web server certificates are used in production, the same caveats about
  43. // fingerprints apply.
  44. func GenerateWebServerCertificate(commonName string) (string, string, error) {
  45. // Based on https://golang.org/src/crypto/tls/generate_cert.go
  46. // TODO: use other key types: anti-fingerprint by varying params
  47. rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
  48. if err != nil {
  49. return "", "", ContextError(err)
  50. }
  51. // Validity period is 1 or 2 years, starting 1 to 6 months ago.
  52. validityPeriodYears := 1
  53. delta, err := rand.Int(rand.Reader, big.NewInt(2))
  54. if err != nil {
  55. return "", "", ContextError(err)
  56. }
  57. validityPeriodYears += int(delta.Int64())
  58. retroactiveMonths := 1
  59. delta, err = rand.Int(rand.Reader, big.NewInt(6))
  60. if err != nil {
  61. return "", "", ContextError(err)
  62. }
  63. retroactiveMonths += int(delta.Int64())
  64. notBefore := time.Now().Truncate(time.Hour).UTC().AddDate(0, -retroactiveMonths, 0)
  65. notAfter := notBefore.AddDate(validityPeriodYears, 0, 0)
  66. serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
  67. serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
  68. if err != nil {
  69. return "", "", ContextError(err)
  70. }
  71. publicKeyBytes, err := x509.MarshalPKIXPublicKey(rsaKey.Public())
  72. if err != nil {
  73. return "", "", ContextError(err)
  74. }
  75. // as per RFC3280 sec. 4.2.1.2
  76. subjectKeyID := sha1.Sum(publicKeyBytes)
  77. var subject pkix.Name
  78. if commonName != "" {
  79. subject = pkix.Name{CommonName: commonName}
  80. }
  81. template := x509.Certificate{
  82. SerialNumber: serialNumber,
  83. Subject: subject,
  84. NotBefore: notBefore,
  85. NotAfter: notAfter,
  86. KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
  87. ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
  88. BasicConstraintsValid: true,
  89. IsCA: true,
  90. SubjectKeyId: subjectKeyID[:],
  91. MaxPathLen: 1,
  92. Version: 2,
  93. }
  94. derCert, err := x509.CreateCertificate(
  95. rand.Reader,
  96. &template,
  97. &template,
  98. rsaKey.Public(),
  99. rsaKey)
  100. if err != nil {
  101. return "", "", ContextError(err)
  102. }
  103. webServerCertificate := pem.EncodeToMemory(
  104. &pem.Block{
  105. Type: "CERTIFICATE",
  106. Bytes: derCert,
  107. },
  108. )
  109. webServerPrivateKey := pem.EncodeToMemory(
  110. &pem.Block{
  111. Type: "RSA PRIVATE KEY",
  112. Bytes: x509.MarshalPKCS1PrivateKey(rsaKey),
  113. },
  114. )
  115. return string(webServerCertificate), string(webServerPrivateKey), nil
  116. }