File: pkix.go

package info (click to toggle)
golang-github-foxboron-go-tpm-keyfiles 0.0~git20241207.04534a2-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 216 kB
  • sloc: makefile: 9
file content (47 lines) | stat: -rw-r--r-- 1,197 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package pkix

import (
	"crypto/ecdsa"
	"crypto/rsa"
	"crypto/x509"
	"encoding/pem"
	"fmt"

	"github.com/foxboron/go-tpm-keyfiles/template"
	"github.com/google/go-tpm/tpm2"
)

// ToTPMPublic takes a PKIX ASN.1 structure and transforms it into a
// tpm2.TPMTPublic structure
func ToTPMPublic(pKey []byte) (*tpm2.TPMTPublic, error) {
	block, _ := pem.Decode([]byte(pKey))

	if block == nil || block.Type != "PUBLIC KEY" {
		return nil, fmt.Errorf("failed to decode PEM block containing public key")
	}

	key, err := x509.ParsePKIXPublicKey(block.Bytes)
	if err != nil {
		return nil, fmt.Errorf("failed parsing pem key: %v", err)
	}

	switch p := key.(type) {
	case *ecdsa.PublicKey:
		return template.EcdsaToTPMTPublic(p, tpm2.TPMAlgSHA256), nil
	case *rsa.PublicKey:
		// TODO: Support other bit lengths
		return template.RSAToTPMTPublic(p, 2048), nil
	default:
		return nil, fmt.Errorf("unsupported keytype")
	}
}

// FromTPMPublic takes a tpm2.TPMTPublic struct and transform it into a PKIX
// ASN.1 structure
func FromTPMPublic(pub *tpm2.TPMTPublic) ([]byte, error) {
	pk, err := template.FromTPMPublicToPubkey(pub)
	if err != nil {
		return nil, err
	}
	return x509.MarshalPKIXPublicKey(pk)
}