2024-10-17 13:40:17 -05:00
/ *
2024-10-18 09:25:31 -05:00
Copyright Docker attest authors
2024-10-17 13:40:17 -05:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2024-10-18 09:25:31 -05:00
2024-04-19 09:08:31 -05:00
package tlog
import (
"context"
"crypto/x509"
"encoding/pem"
"testing"
"time"
"github.com/docker/attest/internal/util"
2024-09-02 16:17:50 +01:00
"github.com/docker/attest/signerverifier"
2024-04-19 09:08:31 -05:00
"github.com/secure-systems-lab/go-securesystemslib/dsse"
"github.com/stretchr/testify/assert"
)
const (
2024-08-01 15:35:15 +01:00
// test artifacts.
2024-04-19 09:08:31 -05:00
TestPayload = "test"
TestPublicKey = "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAED4V+REhx+aqWH7ylMMDHahNMnMLS\nOJP/9kAm9lp+3mqYTAhURra6OD5Qx8Zbd+euPyPk9y+w/gWGDB9zn/Il1A==\n-----END PUBLIC KEY-----"
)
func TestCreateX509Cert ( t * testing . T ) {
// TODO - replace with mock KMS
// generate test signing keys
signer , err := signerverifier . GenKeyPair ( )
assert . NoError ( t , err )
// create x509 cert
cert , err := CreateX509Cert ( "test" , signer )
assert . NoError ( t , err )
p , _ := pem . Decode ( cert )
result , err := x509 . ParseCertificate ( p . Bytes )
assert . NoError ( t , err )
// test cert RawSubjectPublicKeyInfo field contains ephemeral public key
ecPub , err := x509 . MarshalPKIXPublicKey ( signer . Public ( ) )
assert . NoError ( t , err )
assert . Equalf ( t , string ( result . RawSubjectPublicKeyInfo ) , string ( ecPub ) , "certificate raw subject public key info does not match ephemeral public key" )
// test cert common name == subject
assert . Equalf ( t , result . Subject . CommonName , "test" , "cert common name does not equal subject id" )
}
func TestUploadAndVerifyLogEntry ( t * testing . T ) {
// message digest
payload := [ ] byte ( "test" )
2024-05-08 10:28:01 +01:00
hash := util . SHA256 ( payload )
2024-04-19 09:08:31 -05:00
// generate ephemeral keys to sign message digest
signer , err := signerverifier . GenKeyPair ( )
assert . NoError ( t , err )
sig , err := signer . Sign ( context . Background ( ) , hash )
assert . NoError ( t , err )
2024-09-18 13:34:10 +01:00
var tl TransparencyLog
2024-08-01 15:35:15 +01:00
if UseMockTL {
2024-09-18 13:34:10 +01:00
tl = & MockTransparencyLog {
UploadLogEntryFunc : func ( _ context . Context , _ string , _ [ ] byte , _ [ ] byte , _ dsse . SignerVerifier ) ( * DockerTLExtension , error ) {
return & DockerTLExtension {
Kind : RekorTLExtKind ,
Data : [ ] byte ( TestEntry ) ,
} , nil
2024-04-19 09:08:31 -05:00
} ,
2024-09-18 13:34:10 +01:00
VerifyLogEntryFunc : func ( _ context . Context , _ * DockerTLExtension , _ , _ [ ] byte ) ( time . Time , error ) {
2024-04-19 09:08:31 -05:00
return time . Time { } , nil
} ,
}
} else {
2024-09-18 13:34:10 +01:00
assert . NoError ( t , err )
2024-04-19 09:08:31 -05:00
}
// test upload log entry
2024-09-18 13:34:10 +01:00
ctx := context . Background ( )
entry , err := tl . UploadEntry ( ctx , "test" , payload , sig , signer )
2024-04-19 09:08:31 -05:00
assert . NoError ( t , err )
2024-09-18 13:34:10 +01:00
// verify TL entry
2024-04-19 09:08:31 -05:00
ecPub , err := x509 . MarshalPKIXPublicKey ( signer . Public ( ) )
assert . NoError ( t , err )
2024-09-18 13:34:10 +01:00
_ , err = tl . VerifyEntry ( ctx , entry , payload , ecPub )
2024-04-19 09:08:31 -05:00
assert . NoError ( t , err )
}