certificate.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 ~10 years, starting some number of ~months
  52. // back in the last year.
  53. age, err := MakeSecureRandomInt(12)
  54. if err != nil {
  55. return "", "", ContextError(err)
  56. }
  57. age += 1
  58. validityPeriod := 10 * 365 * 24 * time.Hour
  59. notBefore := time.Now().Add(time.Duration(-age) * 30 * 24 * time.Hour).UTC()
  60. notAfter := notBefore.Add(validityPeriod).UTC()
  61. serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
  62. serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
  63. if err != nil {
  64. return "", "", ContextError(err)
  65. }
  66. publicKeyBytes, err := x509.MarshalPKIXPublicKey(rsaKey.Public())
  67. if err != nil {
  68. return "", "", ContextError(err)
  69. }
  70. // as per RFC3280 sec. 4.2.1.2
  71. subjectKeyID := sha1.Sum(publicKeyBytes)
  72. var subject pkix.Name
  73. if commonName != "" {
  74. subject = pkix.Name{CommonName: commonName}
  75. }
  76. template := x509.Certificate{
  77. SerialNumber: serialNumber,
  78. Subject: subject,
  79. NotBefore: notBefore,
  80. NotAfter: notAfter,
  81. KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
  82. ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
  83. BasicConstraintsValid: true,
  84. IsCA: true,
  85. SubjectKeyId: subjectKeyID[:],
  86. MaxPathLen: 1,
  87. Version: 2,
  88. }
  89. derCert, err := x509.CreateCertificate(
  90. rand.Reader,
  91. &template,
  92. &template,
  93. rsaKey.Public(),
  94. rsaKey)
  95. if err != nil {
  96. return "", "", ContextError(err)
  97. }
  98. webServerCertificate := pem.EncodeToMemory(
  99. &pem.Block{
  100. Type: "CERTIFICATE",
  101. Bytes: derCert,
  102. },
  103. )
  104. webServerPrivateKey := pem.EncodeToMemory(
  105. &pem.Block{
  106. Type: "RSA PRIVATE KEY",
  107. Bytes: x509.MarshalPKCS1PrivateKey(rsaKey),
  108. },
  109. )
  110. return string(webServerCertificate), string(webServerPrivateKey), nil
  111. }