sign_supported.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. //go:build windows
  4. // darwin,cgo is also supported by certstore but untested, so it is not enabled.
  5. package controlclient
  6. import (
  7. "crypto"
  8. "crypto/rsa"
  9. "crypto/x509"
  10. "errors"
  11. "fmt"
  12. "time"
  13. "github.com/tailscale/certstore"
  14. "tailscale.com/tailcfg"
  15. "tailscale.com/types/key"
  16. "tailscale.com/util/syspolicy/pkey"
  17. "tailscale.com/util/syspolicy/policyclient"
  18. )
  19. // getMachineCertificateSubject returns the exact name of a Subject that needs
  20. // to be present in an identity's certificate chain to sign a RegisterRequest,
  21. // formatted as per pkix.Name.String(). The Subject may be that of the identity
  22. // itself, an intermediate CA or the root CA.
  23. //
  24. // If getMachineCertificateSubject() returns "" then no lookup will occur and
  25. // each RegisterRequest will be unsigned.
  26. //
  27. // Example: "CN=Tailscale Inc Test Root CA,OU=Tailscale Inc Test Certificate Authority,O=Tailscale Inc,ST=ON,C=CA"
  28. func getMachineCertificateSubject(polc policyclient.Client) string {
  29. machineCertSubject, _ := polc.GetString(pkey.MachineCertificateSubject, "")
  30. return machineCertSubject
  31. }
  32. var (
  33. errNoMatch = errors.New("no matching certificate")
  34. errBadRequest = errors.New("malformed request")
  35. )
  36. func isSupportedCertificate(cert *x509.Certificate) bool {
  37. return cert.PublicKeyAlgorithm == x509.RSA
  38. }
  39. func isSubjectInChain(subject string, chain []*x509.Certificate) bool {
  40. if len(chain) == 0 || chain[0] == nil {
  41. return false
  42. }
  43. for _, c := range chain {
  44. if c == nil {
  45. continue
  46. }
  47. if c.Subject.String() == subject {
  48. return true
  49. }
  50. }
  51. return false
  52. }
  53. func selectIdentityFromSlice(subject string, ids []certstore.Identity, now time.Time) (certstore.Identity, []*x509.Certificate) {
  54. var bestCandidate struct {
  55. id certstore.Identity
  56. chain []*x509.Certificate
  57. }
  58. for _, id := range ids {
  59. chain, err := id.CertificateChain()
  60. if err != nil {
  61. continue
  62. }
  63. if len(chain) < 1 {
  64. continue
  65. }
  66. if !isSupportedCertificate(chain[0]) {
  67. continue
  68. }
  69. if now.Before(chain[0].NotBefore) || now.After(chain[0].NotAfter) {
  70. // Certificate is not valid at this time
  71. continue
  72. }
  73. if !isSubjectInChain(subject, chain) {
  74. continue
  75. }
  76. // Select the most recently issued certificate. If there is a tie, pick
  77. // one arbitrarily.
  78. if len(bestCandidate.chain) > 0 && bestCandidate.chain[0].NotBefore.After(chain[0].NotBefore) {
  79. continue
  80. }
  81. bestCandidate.id = id
  82. bestCandidate.chain = chain
  83. }
  84. return bestCandidate.id, bestCandidate.chain
  85. }
  86. // findIdentity locates an identity from the Windows or Darwin certificate
  87. // store. It returns the first certificate with a matching Subject anywhere in
  88. // its certificate chain, so it is possible to search for the leaf certificate,
  89. // intermediate CA or root CA. If err is nil then the returned identity will
  90. // never be nil (if no identity is found, the error errNoMatch will be
  91. // returned). If an identity is returned then its certificate chain is also
  92. // returned.
  93. func findIdentity(subject string, st certstore.Store) (certstore.Identity, []*x509.Certificate, error) {
  94. ids, err := st.Identities()
  95. if err != nil {
  96. return nil, nil, err
  97. }
  98. selected, chain := selectIdentityFromSlice(subject, ids, clock.Now())
  99. for _, id := range ids {
  100. if id != selected {
  101. id.Close()
  102. }
  103. }
  104. if selected == nil {
  105. return nil, nil, errNoMatch
  106. }
  107. return selected, chain, nil
  108. }
  109. // signRegisterRequest looks for a suitable machine identity from the local
  110. // system certificate store, and if one is found, signs the RegisterRequest
  111. // using that identity's public key. In addition to the signature, the full
  112. // certificate chain is included so that the control server can validate the
  113. // certificate from a copy of the root CA's certificate.
  114. func signRegisterRequest(polc policyclient.Client, req *tailcfg.RegisterRequest, serverURL string, serverPubKey, machinePubKey key.MachinePublic) (err error) {
  115. defer func() {
  116. if err != nil {
  117. err = fmt.Errorf("signRegisterRequest: %w", err)
  118. }
  119. }()
  120. if req.Timestamp == nil {
  121. return errBadRequest
  122. }
  123. machineCertificateSubject := getMachineCertificateSubject(polc)
  124. if machineCertificateSubject == "" {
  125. return errCertificateNotConfigured
  126. }
  127. st, err := certstore.Open(certstore.System)
  128. if err != nil {
  129. return fmt.Errorf("open cert store: %w", err)
  130. }
  131. defer st.Close()
  132. id, chain, err := findIdentity(machineCertificateSubject, st)
  133. if err != nil {
  134. return fmt.Errorf("find identity: %w", err)
  135. }
  136. defer id.Close()
  137. signer, err := id.Signer()
  138. if err != nil {
  139. return fmt.Errorf("create signer: %w", err)
  140. }
  141. cl := 0
  142. for _, c := range chain {
  143. cl += len(c.Raw)
  144. }
  145. req.DeviceCert = make([]byte, 0, cl)
  146. for _, c := range chain {
  147. req.DeviceCert = append(req.DeviceCert, c.Raw...)
  148. }
  149. req.SignatureType = tailcfg.SignatureV2
  150. h, err := HashRegisterRequest(req.SignatureType, req.Timestamp.UTC(), serverURL, req.DeviceCert, serverPubKey, machinePubKey)
  151. if err != nil {
  152. return fmt.Errorf("hash: %w", err)
  153. }
  154. req.Signature, err = signer.Sign(nil, h, &rsa.PSSOptions{
  155. SaltLength: rsa.PSSSaltLengthEqualsHash,
  156. Hash: crypto.SHA256,
  157. })
  158. if err != nil {
  159. return fmt.Errorf("sign: %w", err)
  160. }
  161. return nil
  162. }