certificate.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  30. )
  31. // GenerateWebServerCertificate creates a self-signed web server certificate,
  32. // using the specified host name (commonName).
  33. // This is primarily intended for use by MeekServer to generate on-the-fly,
  34. // self-signed TLS certificates for fronted HTTPS mode. In this case, the nature
  35. // of the certificate is non-circumvention; it only has to be acceptable to the
  36. // front CDN making connections to meek.
  37. // The same certificates are used for unfronted HTTPS meek. In this case, the
  38. // certificates may be a fingerprint used to detect Psiphon servers or traffic.
  39. // TODO: more effort to mitigate fingerprinting these certificates.
  40. //
  41. // In addition, GenerateWebServerCertificate is used by GenerateConfig to create
  42. // Psiphon web server certificates for test/example configurations. If these Psiphon
  43. // web server certificates are used in production, the same caveats about
  44. // fingerprints apply.
  45. func GenerateWebServerCertificate(commonName string) (string, string, error) {
  46. // Based on https://golang.org/src/crypto/tls/generate_cert.go
  47. // TODO: use other key types: anti-fingerprint by varying params
  48. rsaKey, err := rsa.GenerateKey(rand.Reader, 2048)
  49. if err != nil {
  50. return "", "", errors.Trace(err)
  51. }
  52. // Validity period is 1 or 2 years, starting 1 to 6 months ago.
  53. validityPeriodYears := 1
  54. delta, err := rand.Int(rand.Reader, big.NewInt(2))
  55. if err != nil {
  56. return "", "", errors.Trace(err)
  57. }
  58. validityPeriodYears += int(delta.Int64())
  59. retroactiveMonths := 1
  60. delta, err = rand.Int(rand.Reader, big.NewInt(6))
  61. if err != nil {
  62. return "", "", errors.Trace(err)
  63. }
  64. retroactiveMonths += int(delta.Int64())
  65. notBefore := time.Now().Truncate(time.Hour).UTC().AddDate(0, -retroactiveMonths, 0)
  66. notAfter := notBefore.AddDate(validityPeriodYears, 0, 0)
  67. serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
  68. serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
  69. if err != nil {
  70. return "", "", errors.Trace(err)
  71. }
  72. publicKeyBytes, err := x509.MarshalPKIXPublicKey(rsaKey.Public())
  73. if err != nil {
  74. return "", "", errors.Trace(err)
  75. }
  76. // as per RFC3280 sec. 4.2.1.2
  77. subjectKeyID := sha1.Sum(publicKeyBytes)
  78. var subject pkix.Name
  79. if commonName != "" {
  80. subject = pkix.Name{CommonName: commonName}
  81. }
  82. template := x509.Certificate{
  83. SerialNumber: serialNumber,
  84. Subject: subject,
  85. NotBefore: notBefore,
  86. NotAfter: notAfter,
  87. KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
  88. ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
  89. BasicConstraintsValid: true,
  90. IsCA: true,
  91. SubjectKeyId: subjectKeyID[:],
  92. MaxPathLen: 1,
  93. Version: 2,
  94. }
  95. derCert, err := x509.CreateCertificate(
  96. rand.Reader,
  97. &template,
  98. &template,
  99. rsaKey.Public(),
  100. rsaKey)
  101. if err != nil {
  102. return "", "", errors.Trace(err)
  103. }
  104. webServerCertificate := pem.EncodeToMemory(
  105. &pem.Block{
  106. Type: "CERTIFICATE",
  107. Bytes: derCert,
  108. },
  109. )
  110. webServerPrivateKey := pem.EncodeToMemory(
  111. &pem.Block{
  112. Type: "RSA PRIVATE KEY",
  113. Bytes: x509.MarshalPKCS1PrivateKey(rsaKey),
  114. },
  115. )
  116. return string(webServerCertificate), string(webServerPrivateKey), nil
  117. }