4
0

linkedca.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. package authority
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/sha256"
  6. "crypto/tls"
  7. "crypto/x509"
  8. "encoding/hex"
  9. "encoding/pem"
  10. "fmt"
  11. "net/url"
  12. "regexp"
  13. "strings"
  14. "time"
  15. "github.com/pkg/errors"
  16. "golang.org/x/crypto/ssh"
  17. "google.golang.org/grpc"
  18. "google.golang.org/grpc/credentials"
  19. "github.com/smallstep/linkedca"
  20. "go.step.sm/crypto/jose"
  21. "go.step.sm/crypto/keyutil"
  22. "go.step.sm/crypto/tlsutil"
  23. "go.step.sm/crypto/x509util"
  24. "github.com/smallstep/certificates/authority/admin"
  25. "github.com/smallstep/certificates/authority/provisioner"
  26. "github.com/smallstep/certificates/db"
  27. "github.com/smallstep/certificates/internal/cast"
  28. )
  29. const uuidPattern = "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
  30. type linkedCaClient struct {
  31. renewer *tlsutil.Renewer
  32. client linkedca.MajordomoClient
  33. authorityID string
  34. }
  35. // interface guard
  36. var _ admin.DB = (*linkedCaClient)(nil)
  37. type linkedCAClaims struct {
  38. jose.Claims
  39. SANs []string `json:"sans"`
  40. SHA string `json:"sha"`
  41. }
  42. func newLinkedCAClient(token string) (*linkedCaClient, error) {
  43. tok, err := jose.ParseSigned(token)
  44. if err != nil {
  45. return nil, errors.Wrap(err, "error parsing token")
  46. }
  47. var claims linkedCAClaims
  48. if err := tok.UnsafeClaimsWithoutVerification(&claims); err != nil {
  49. return nil, errors.Wrap(err, "error parsing token")
  50. }
  51. // Validate claims
  52. if len(claims.Audience) != 1 {
  53. return nil, errors.New("error parsing token: invalid aud claim")
  54. }
  55. if claims.SHA == "" {
  56. return nil, errors.New("error parsing token: invalid sha claim")
  57. }
  58. // Get linkedCA endpoint from audience.
  59. u, err := url.Parse(claims.Audience[0])
  60. if err != nil {
  61. return nil, errors.New("error parsing token: invalid aud claim")
  62. }
  63. // Get authority from SANs
  64. authority, err := getAuthority(claims.SANs)
  65. if err != nil {
  66. return nil, err
  67. }
  68. // Create csr to login with
  69. signer, err := keyutil.GenerateDefaultSigner()
  70. if err != nil {
  71. return nil, err
  72. }
  73. csr, err := x509util.CreateCertificateRequest(claims.Subject, claims.SANs, signer)
  74. if err != nil {
  75. return nil, err
  76. }
  77. // Get and verify root certificate
  78. root, err := getRootCertificate(u.Host, claims.SHA)
  79. if err != nil {
  80. return nil, err
  81. }
  82. pool := x509.NewCertPool()
  83. pool.AddCert(root)
  84. // Login with majordomo and get certificates
  85. cert, tlsConfig, err := login(authority, token, csr, signer, u.Host, pool)
  86. if err != nil {
  87. return nil, err
  88. }
  89. // Start TLS renewer and set the GetClientCertificate callback to it.
  90. renewer, err := tlsutil.NewRenewer(cert, tlsConfig, func() (*tls.Certificate, *tls.Config, error) {
  91. return login(authority, token, csr, signer, u.Host, pool)
  92. })
  93. if err != nil {
  94. return nil, err
  95. }
  96. tlsConfig.GetClientCertificate = renewer.GetClientCertificate
  97. // Start mTLS client
  98. conn, err := grpc.NewClient(u.Host, grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
  99. if err != nil {
  100. return nil, errors.Wrapf(err, "error connecting %s", u.Host)
  101. }
  102. return &linkedCaClient{
  103. renewer: renewer,
  104. client: linkedca.NewMajordomoClient(conn),
  105. authorityID: authority,
  106. }, nil
  107. }
  108. // IsLinkedCA is a sentinel function that can be used to
  109. // check if a linkedCaClient is the underlying type of an
  110. // admin.DB interface.
  111. func (c *linkedCaClient) IsLinkedCA() bool {
  112. return true
  113. }
  114. func (c *linkedCaClient) Run() {
  115. c.renewer.Run()
  116. }
  117. func (c *linkedCaClient) Stop() {
  118. c.renewer.Stop()
  119. }
  120. func (c *linkedCaClient) CreateProvisioner(ctx context.Context, prov *linkedca.Provisioner) error {
  121. resp, err := c.client.CreateProvisioner(ctx, &linkedca.CreateProvisionerRequest{
  122. Type: prov.Type,
  123. Name: prov.Name,
  124. Details: prov.Details,
  125. Claims: prov.Claims,
  126. X509Template: prov.X509Template,
  127. SshTemplate: prov.SshTemplate,
  128. })
  129. if err != nil {
  130. return errors.Wrap(err, "error creating provisioner")
  131. }
  132. prov.Id = resp.Id
  133. prov.AuthorityId = resp.AuthorityId
  134. return nil
  135. }
  136. func (c *linkedCaClient) GetProvisioner(ctx context.Context, id string) (*linkedca.Provisioner, error) {
  137. resp, err := c.client.GetProvisioner(ctx, &linkedca.GetProvisionerRequest{
  138. Id: id,
  139. })
  140. if err != nil {
  141. return nil, errors.Wrap(err, "error getting provisioners")
  142. }
  143. return resp, nil
  144. }
  145. func (c *linkedCaClient) GetProvisioners(ctx context.Context) ([]*linkedca.Provisioner, error) {
  146. resp, err := c.GetConfiguration(ctx)
  147. if err != nil {
  148. return nil, err
  149. }
  150. return resp.Provisioners, nil
  151. }
  152. func (c *linkedCaClient) GetConfiguration(ctx context.Context) (*linkedca.ConfigurationResponse, error) {
  153. resp, err := c.client.GetConfiguration(ctx, &linkedca.ConfigurationRequest{
  154. AuthorityId: c.authorityID,
  155. })
  156. if err != nil {
  157. return nil, errors.Wrap(err, "error getting configuration")
  158. }
  159. return resp, nil
  160. }
  161. func (c *linkedCaClient) UpdateProvisioner(ctx context.Context, prov *linkedca.Provisioner) error {
  162. _, err := c.client.UpdateProvisioner(ctx, &linkedca.UpdateProvisionerRequest{
  163. Id: prov.Id,
  164. Name: prov.Name,
  165. Details: prov.Details,
  166. Claims: prov.Claims,
  167. X509Template: prov.X509Template,
  168. SshTemplate: prov.SshTemplate,
  169. })
  170. return errors.Wrap(err, "error updating provisioner")
  171. }
  172. func (c *linkedCaClient) DeleteProvisioner(ctx context.Context, id string) error {
  173. _, err := c.client.DeleteProvisioner(ctx, &linkedca.DeleteProvisionerRequest{
  174. Id: id,
  175. })
  176. return errors.Wrap(err, "error deleting provisioner")
  177. }
  178. func (c *linkedCaClient) CreateAdmin(ctx context.Context, adm *linkedca.Admin) error {
  179. resp, err := c.client.CreateAdmin(ctx, &linkedca.CreateAdminRequest{
  180. Subject: adm.Subject,
  181. ProvisionerId: adm.ProvisionerId,
  182. Type: adm.Type,
  183. })
  184. if err != nil {
  185. return errors.Wrap(err, "error creating admin")
  186. }
  187. adm.Id = resp.Id
  188. adm.AuthorityId = resp.AuthorityId
  189. return nil
  190. }
  191. func (c *linkedCaClient) GetAdmin(ctx context.Context, id string) (*linkedca.Admin, error) {
  192. resp, err := c.client.GetAdmin(ctx, &linkedca.GetAdminRequest{
  193. Id: id,
  194. })
  195. if err != nil {
  196. return nil, errors.Wrap(err, "error getting admins")
  197. }
  198. return resp, nil
  199. }
  200. func (c *linkedCaClient) GetAdmins(ctx context.Context) ([]*linkedca.Admin, error) {
  201. resp, err := c.GetConfiguration(ctx)
  202. if err != nil {
  203. return nil, err
  204. }
  205. return resp.Admins, nil
  206. }
  207. func (c *linkedCaClient) UpdateAdmin(ctx context.Context, adm *linkedca.Admin) error {
  208. _, err := c.client.UpdateAdmin(ctx, &linkedca.UpdateAdminRequest{
  209. Id: adm.Id,
  210. Type: adm.Type,
  211. })
  212. return errors.Wrap(err, "error updating admin")
  213. }
  214. func (c *linkedCaClient) DeleteAdmin(ctx context.Context, id string) error {
  215. _, err := c.client.DeleteAdmin(ctx, &linkedca.DeleteAdminRequest{
  216. Id: id,
  217. })
  218. return errors.Wrap(err, "error deleting admin")
  219. }
  220. func (c *linkedCaClient) GetCertificateData(serial string) (*db.CertificateData, error) {
  221. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  222. defer cancel()
  223. resp, err := c.client.GetCertificate(ctx, &linkedca.GetCertificateRequest{
  224. Serial: serial,
  225. })
  226. if err != nil {
  227. return nil, err
  228. }
  229. var pd *db.ProvisionerData
  230. if p := resp.Provisioner; p != nil {
  231. pd = &db.ProvisionerData{
  232. ID: p.Id, Name: p.Name, Type: p.Type.String(),
  233. }
  234. }
  235. var raInfo *provisioner.RAInfo
  236. if p := resp.RaProvisioner; p != nil && p.Provisioner != nil {
  237. raInfo = &provisioner.RAInfo{
  238. AuthorityID: p.AuthorityId,
  239. ProvisionerID: p.Provisioner.Id,
  240. ProvisionerType: p.Provisioner.Type.String(),
  241. ProvisionerName: p.Provisioner.Name,
  242. }
  243. }
  244. return &db.CertificateData{
  245. Provisioner: pd,
  246. RaInfo: raInfo,
  247. }, nil
  248. }
  249. func (c *linkedCaClient) StoreCertificateChain(p provisioner.Interface, fullchain ...*x509.Certificate) error {
  250. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  251. defer cancel()
  252. raProvisioner, endpointID := createRegistrationAuthorityProvisioner(p)
  253. _, err := c.client.PostCertificate(ctx, &linkedca.CertificateRequest{
  254. PemCertificate: serializeCertificateChain(fullchain[0]),
  255. PemCertificateChain: serializeCertificateChain(fullchain[1:]...),
  256. Provisioner: createProvisionerIdentity(p),
  257. AttestationData: createAttestationData(p),
  258. RaProvisioner: raProvisioner,
  259. EndpointId: endpointID,
  260. })
  261. return errors.Wrap(err, "error posting certificate")
  262. }
  263. func (c *linkedCaClient) StoreRenewedCertificate(parent *x509.Certificate, fullchain ...*x509.Certificate) error {
  264. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  265. defer cancel()
  266. _, err := c.client.PostCertificate(ctx, &linkedca.CertificateRequest{
  267. PemCertificate: serializeCertificateChain(fullchain[0]),
  268. PemCertificateChain: serializeCertificateChain(fullchain[1:]...),
  269. PemParentCertificate: serializeCertificateChain(parent),
  270. })
  271. return errors.Wrap(err, "error posting renewed certificate")
  272. }
  273. func (c *linkedCaClient) StoreSSHCertificate(p provisioner.Interface, crt *ssh.Certificate) error {
  274. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  275. defer cancel()
  276. _, err := c.client.PostSSHCertificate(ctx, &linkedca.SSHCertificateRequest{
  277. Certificate: string(ssh.MarshalAuthorizedKey(crt)),
  278. Provisioner: createProvisionerIdentity(p),
  279. })
  280. return errors.Wrap(err, "error posting ssh certificate")
  281. }
  282. func (c *linkedCaClient) StoreRenewedSSHCertificate(p provisioner.Interface, parent, crt *ssh.Certificate) error {
  283. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  284. defer cancel()
  285. _, err := c.client.PostSSHCertificate(ctx, &linkedca.SSHCertificateRequest{
  286. Certificate: string(ssh.MarshalAuthorizedKey(crt)),
  287. ParentCertificate: string(ssh.MarshalAuthorizedKey(parent)),
  288. Provisioner: createProvisionerIdentity(p),
  289. })
  290. return errors.Wrap(err, "error posting renewed ssh certificate")
  291. }
  292. func (c *linkedCaClient) Revoke(crt *x509.Certificate, rci *db.RevokedCertificateInfo) error {
  293. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  294. defer cancel()
  295. _, err := c.client.RevokeCertificate(ctx, &linkedca.RevokeCertificateRequest{
  296. Serial: rci.Serial,
  297. PemCertificate: serializeCertificate(crt),
  298. Reason: rci.Reason,
  299. ReasonCode: linkedca.RevocationReasonCode(cast.Int32(rci.ReasonCode)),
  300. Passive: true,
  301. })
  302. return errors.Wrap(err, "error revoking certificate")
  303. }
  304. func (c *linkedCaClient) RevokeSSH(cert *ssh.Certificate, rci *db.RevokedCertificateInfo) error {
  305. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  306. defer cancel()
  307. _, err := c.client.RevokeSSHCertificate(ctx, &linkedca.RevokeSSHCertificateRequest{
  308. Serial: rci.Serial,
  309. Certificate: serializeSSHCertificate(cert),
  310. Reason: rci.Reason,
  311. ReasonCode: linkedca.RevocationReasonCode(cast.Int32(rci.ReasonCode)),
  312. Passive: true,
  313. })
  314. return errors.Wrap(err, "error revoking ssh certificate")
  315. }
  316. func (c *linkedCaClient) IsRevoked(serial string) (bool, error) {
  317. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  318. defer cancel()
  319. resp, err := c.client.GetCertificateStatus(ctx, &linkedca.GetCertificateStatusRequest{
  320. Serial: serial,
  321. })
  322. if err != nil {
  323. return false, errors.Wrap(err, "error getting certificate status")
  324. }
  325. return resp.Status != linkedca.RevocationStatus_ACTIVE, nil
  326. }
  327. func (c *linkedCaClient) IsSSHRevoked(serial string) (bool, error) {
  328. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  329. defer cancel()
  330. resp, err := c.client.GetSSHCertificateStatus(ctx, &linkedca.GetSSHCertificateStatusRequest{
  331. Serial: serial,
  332. })
  333. if err != nil {
  334. return false, errors.Wrap(err, "error getting certificate status")
  335. }
  336. return resp.Status != linkedca.RevocationStatus_ACTIVE, nil
  337. }
  338. func (c *linkedCaClient) CreateAuthorityPolicy(_ context.Context, _ *linkedca.Policy) error {
  339. return errors.New("not implemented yet")
  340. }
  341. func (c *linkedCaClient) GetAuthorityPolicy(context.Context) (*linkedca.Policy, error) {
  342. return nil, errors.New("not implemented yet")
  343. }
  344. func (c *linkedCaClient) UpdateAuthorityPolicy(_ context.Context, _ *linkedca.Policy) error {
  345. return errors.New("not implemented yet")
  346. }
  347. func (c *linkedCaClient) DeleteAuthorityPolicy(context.Context) error {
  348. return errors.New("not implemented yet")
  349. }
  350. func createProvisionerIdentity(p provisioner.Interface) *linkedca.ProvisionerIdentity {
  351. if p == nil {
  352. return nil
  353. }
  354. return &linkedca.ProvisionerIdentity{
  355. Id: p.GetID(),
  356. Type: linkedca.Provisioner_Type(cast.Int32(int(p.GetType()))),
  357. Name: p.GetName(),
  358. }
  359. }
  360. func createRegistrationAuthorityProvisioner(p provisioner.Interface) (*linkedca.RegistrationAuthorityProvisioner, string) {
  361. if rap, ok := p.(raProvisioner); ok {
  362. if info := rap.RAInfo(); info != nil {
  363. typ := linkedca.Provisioner_Type_value[strings.ToUpper(info.ProvisionerType)]
  364. return &linkedca.RegistrationAuthorityProvisioner{
  365. AuthorityId: info.AuthorityID,
  366. Provisioner: &linkedca.ProvisionerIdentity{
  367. Id: info.ProvisionerID,
  368. Type: linkedca.Provisioner_Type(typ),
  369. Name: info.ProvisionerName,
  370. },
  371. }, info.EndpointID
  372. }
  373. }
  374. return nil, ""
  375. }
  376. func createAttestationData(p provisioner.Interface) *linkedca.AttestationData {
  377. if ap, ok := p.(attProvisioner); ok {
  378. if data := ap.AttestationData(); data != nil {
  379. return &linkedca.AttestationData{
  380. PermanentIdentifier: data.PermanentIdentifier,
  381. }
  382. }
  383. }
  384. return nil
  385. }
  386. func serializeCertificate(crt *x509.Certificate) string {
  387. if crt == nil {
  388. return ""
  389. }
  390. return string(pem.EncodeToMemory(&pem.Block{
  391. Type: "CERTIFICATE",
  392. Bytes: crt.Raw,
  393. }))
  394. }
  395. func serializeCertificateChain(fullchain ...*x509.Certificate) string {
  396. var chain string
  397. for _, crt := range fullchain {
  398. chain += string(pem.EncodeToMemory(&pem.Block{
  399. Type: "CERTIFICATE",
  400. Bytes: crt.Raw,
  401. }))
  402. }
  403. return chain
  404. }
  405. func serializeSSHCertificate(crt *ssh.Certificate) string {
  406. if crt == nil {
  407. return ""
  408. }
  409. return string(ssh.MarshalAuthorizedKey(crt))
  410. }
  411. func getAuthority(sans []string) (string, error) {
  412. for _, s := range sans {
  413. if strings.HasPrefix(s, "urn:smallstep:authority:") {
  414. if regexp.MustCompile(uuidPattern).MatchString(s[24:]) {
  415. return s[24:], nil
  416. }
  417. }
  418. }
  419. return "", fmt.Errorf("error parsing token: invalid sans claim")
  420. }
  421. // getRootCertificate creates an insecure majordomo client and returns the
  422. // verified root certificate.
  423. func getRootCertificate(endpoint, fingerprint string) (*x509.Certificate, error) {
  424. conn, err := grpc.NewClient(endpoint, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
  425. //nolint:gosec // used in bootstrap protocol
  426. InsecureSkipVerify: true, // lgtm[go/disabled-certificate-check]
  427. })))
  428. if err != nil {
  429. return nil, errors.Wrapf(err, "error connecting %s", endpoint)
  430. }
  431. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  432. defer cancel()
  433. client := linkedca.NewMajordomoClient(conn)
  434. resp, err := client.GetRootCertificate(ctx, &linkedca.GetRootCertificateRequest{
  435. Fingerprint: fingerprint,
  436. })
  437. if err != nil {
  438. return nil, fmt.Errorf("error getting root certificate: %w", err)
  439. }
  440. var block *pem.Block
  441. b := []byte(resp.PemCertificate)
  442. for len(b) > 0 {
  443. block, b = pem.Decode(b)
  444. if block == nil {
  445. break
  446. }
  447. if block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
  448. continue
  449. }
  450. cert, err := x509.ParseCertificate(block.Bytes)
  451. if err != nil {
  452. return nil, fmt.Errorf("error parsing certificate: %w", err)
  453. }
  454. // verify the sha256
  455. sum := sha256.Sum256(cert.Raw)
  456. if !strings.EqualFold(fingerprint, hex.EncodeToString(sum[:])) {
  457. return nil, fmt.Errorf("error verifying certificate: SHA256 fingerprint does not match")
  458. }
  459. return cert, nil
  460. }
  461. return nil, fmt.Errorf("error getting root certificate: certificate not found")
  462. }
  463. // login creates a new majordomo client with just the root ca pool and returns
  464. // the signed certificate and tls configuration.
  465. func login(authority, token string, csr *x509.CertificateRequest, signer crypto.PrivateKey, endpoint string, rootCAs *x509.CertPool) (*tls.Certificate, *tls.Config, error) {
  466. conn, err := grpc.NewClient(endpoint, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
  467. MinVersion: tls.VersionTLS12,
  468. RootCAs: rootCAs,
  469. })))
  470. if err != nil {
  471. return nil, nil, errors.Wrapf(err, "error connecting %s", endpoint)
  472. }
  473. // Login to get the signed certificate
  474. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  475. defer cancel()
  476. client := linkedca.NewMajordomoClient(conn)
  477. resp, err := client.Login(ctx, &linkedca.LoginRequest{
  478. AuthorityId: authority,
  479. Token: token,
  480. PemCertificateRequest: string(pem.EncodeToMemory(&pem.Block{
  481. Type: "CERTIFICATE REQUEST",
  482. Bytes: csr.Raw,
  483. })),
  484. })
  485. if err != nil {
  486. return nil, nil, errors.Wrapf(err, "error logging in %s", endpoint)
  487. }
  488. // Parse login response
  489. var block *pem.Block
  490. var bundle []*x509.Certificate
  491. rest := []byte(resp.PemCertificateChain)
  492. for {
  493. block, rest = pem.Decode(rest)
  494. if block == nil {
  495. break
  496. }
  497. if block.Type != "CERTIFICATE" {
  498. return nil, nil, errors.New("error decoding login response: pemCertificateChain is not a certificate bundle")
  499. }
  500. crt, err := x509.ParseCertificate(block.Bytes)
  501. if err != nil {
  502. return nil, nil, errors.Wrap(err, "error parsing login response")
  503. }
  504. bundle = append(bundle, crt)
  505. }
  506. if len(bundle) == 0 {
  507. return nil, nil, errors.New("error decoding login response: pemCertificateChain should not be empty")
  508. }
  509. // Build tls.Certificate with PemCertificate and intermediates in the
  510. // PemCertificateChain
  511. cert := &tls.Certificate{
  512. PrivateKey: signer,
  513. }
  514. rest = []byte(resp.PemCertificate)
  515. for {
  516. block, rest = pem.Decode(rest)
  517. if block == nil {
  518. break
  519. }
  520. if block.Type == "CERTIFICATE" {
  521. leaf, err := x509.ParseCertificate(block.Bytes)
  522. if err != nil {
  523. return nil, nil, errors.Wrap(err, "error parsing pemCertificate")
  524. }
  525. cert.Certificate = append(cert.Certificate, block.Bytes)
  526. cert.Leaf = leaf
  527. }
  528. }
  529. // Add intermediates to the tls.Certificate
  530. last := len(bundle) - 1
  531. for i := 0; i < last; i++ {
  532. cert.Certificate = append(cert.Certificate, bundle[i].Raw)
  533. }
  534. // Add root to the pool if it's not there yet
  535. rootCAs.AddCert(bundle[last])
  536. return cert, &tls.Config{
  537. MinVersion: tls.VersionTLS12,
  538. RootCAs: rootCAs,
  539. }, nil
  540. }