@@ -636,6 +636,23 @@ func generateCsrBytes(options CsrOptions) []byte {
636636}
637637
638638func TestParseTCGCSRIDevIDContent (t * testing.T ) {
639+ u := DefaultTPM20Utils {}
640+ privKey , err := rsa .GenerateKey (rand .Reader , 2048 )
641+ if err != nil {
642+ t .Fatalf ("Failed to generate RSA key for testing: %v" , err )
643+ }
644+ tpmtPub , err := u .RSAEKPublicKeyToTPMTPublic (& privKey .PublicKey )
645+ if err != nil {
646+ t .Fatalf ("Failed to create TPMT Public for testing: %v" , err )
647+ }
648+ tpmtPubPEM , err := u .TPMTPublicToPEM (tpmtPub )
649+ if err != nil {
650+ t .Fatalf ("Failed to convert TPMT Public to PEM for testing: %v" , err )
651+ }
652+ tpmtPubBytes := tpm20 .Marshal (tpmtPub )
653+
654+ validCSRWithTPMTPub := * validCSR
655+ validCSRWithTPMTPub .EKCert = tpmtPubPEM
639656 // Define test cases
640657 tests := []struct {
641658 name string
@@ -736,7 +753,7 @@ func TestParseTCGCSRIDevIDContent(t *testing.T) {
736753 {
737754 name : "Invalid EK Cert" ,
738755 csrBytes : generateCsrBytes (CsrOptions {EKCert : []byte ("invalid-ek-cert" )}),
739- expectedError : errors .New ("failed to convert EK Cert to PEM " ),
756+ expectedError : errors .New ("failed to parse ekCert as X509 certificate " ),
740757 },
741758 {
742759 name : "Invalid Attest Pub Bytes" ,
@@ -763,6 +780,12 @@ func TestParseTCGCSRIDevIDContent(t *testing.T) {
763780 csrBytes : generateCsrBytes (CsrOptions {AddExtraBytesToEnd : true }),
764781 expectedError : errors .New ("leftover bytes in TCG_CSR_IDEVID_CONTENT block after parsing" ),
765782 },
783+ {
784+ name : "Valid CSR bytes: PPK in EkCert field as TPMTPublic" ,
785+ csrBytes : generateCsrBytes (CsrOptions {EKCert : tpmtPubBytes }),
786+ expectedError : nil ,
787+ expectedResult : & validCSRWithTPMTPub ,
788+ },
766789 }
767790
768791 for _ , tc := range tests {
0 commit comments