@@ -97,58 +97,74 @@ func parseSigstoreKeys(entry string, out *pbtrustroot.TrustedRoot) error {
9797
9898// ConvertSigstoreKeys takes a source and converts into a SigstoreKeys suitable
9999// for serialization into a ConfigMap entry.
100- func ConvertSigstoreKeys (_ context.Context , source * v1alpha1.SigstoreKeys ) * SigstoreKeys {
101- sk : = & SigstoreKeys {}
100+ func ConvertSigstoreKeys (_ context.Context , source * v1alpha1.SigstoreKeys ) ( sk * SigstoreKeys , err error ) {
101+ sk = & SigstoreKeys {}
102102 sk .MediaType = "application/vnd.dev.sigstore.trustedroot+json;version=0.1"
103103 sk .CertificateAuthorities = make ([]* pbtrustroot.CertificateAuthority , len (source .CertificateAuthorities ))
104104 for i := range source .CertificateAuthorities {
105- sk .CertificateAuthorities [i ] = ConvertCertificateAuthority (source .CertificateAuthorities [i ])
105+ sk .CertificateAuthorities [i ], err = ConvertCertificateAuthority (source .CertificateAuthorities [i ])
106+ if err != nil {
107+ return nil , fmt .Errorf ("failed to convert certificate authority: %w" , err )
108+ }
106109 }
107110
108111 sk .Tlogs = make ([]* pbtrustroot.TransparencyLogInstance , len (source .TLogs ))
109112 for i := range source .TLogs {
110- sk .Tlogs [i ] = ConvertTransparencyLogInstance (source .TLogs [i ])
113+ sk .Tlogs [i ], err = ConvertTransparencyLogInstance (source .TLogs [i ])
114+ if err != nil {
115+ return nil , fmt .Errorf ("failed to convert transparency log instance: %w" , err )
116+ }
111117 }
112118
113119 sk .Ctlogs = make ([]* pbtrustroot.TransparencyLogInstance , len (source .CTLogs ))
114120 for i := range source .CTLogs {
115- sk .Ctlogs [i ] = ConvertTransparencyLogInstance (source .CTLogs [i ])
121+ sk .Ctlogs [i ], err = ConvertTransparencyLogInstance (source .CTLogs [i ])
122+ if err != nil {
123+ return nil , fmt .Errorf ("failed to convert ct log instance: %w" , err )
124+ }
116125 }
117126
118127 sk .TimestampAuthorities = make ([]* pbtrustroot.CertificateAuthority , len (source .TimeStampAuthorities ))
119128 for i := range source .TimeStampAuthorities {
120- sk .TimestampAuthorities [i ] = ConvertCertificateAuthority (source .TimeStampAuthorities [i ])
129+ sk .TimestampAuthorities [i ], err = ConvertCertificateAuthority (source .TimeStampAuthorities [i ])
130+ if err != nil {
131+ return nil , fmt .Errorf ("failed to convert timestamp authority: %w" , err )
132+ }
121133 }
122- return sk
134+ return sk , nil
123135}
124136
125137// ConvertCertificateAuthority converts public into private CertificateAuthority
126- func ConvertCertificateAuthority (source v1alpha1.CertificateAuthority ) * pbtrustroot.CertificateAuthority {
138+ func ConvertCertificateAuthority (source v1alpha1.CertificateAuthority ) (* pbtrustroot.CertificateAuthority , error ) {
139+ certChain , err := DeserializeCertChain (source .CertChain )
140+ if err != nil {
141+ return nil , err
142+ }
127143 return & pbtrustroot.CertificateAuthority {
128144 Subject : & pbcommon.DistinguishedName {
129145 Organization : source .Subject .Organization ,
130146 CommonName : source .Subject .CommonName ,
131147 },
132148 Uri : source .URI .String (),
133- CertChain : DeserializeCertChain ( source . CertChain ) ,
149+ CertChain : certChain ,
134150 ValidFor : & pbcommon.TimeRange {
135151 Start : & timestamppb.Timestamp {
136152 Seconds : 0 , // TODO: Add support for time range to v1alpha1.CertificateAuthority
137153 },
138154 },
139- }
155+ }, nil
140156}
141157
142158// ConvertTransparencyLogInstance converts public into private
143159// TransparencyLogInstance.
144- func ConvertTransparencyLogInstance (source v1alpha1.TransparencyLogInstance ) * pbtrustroot.TransparencyLogInstance {
160+ func ConvertTransparencyLogInstance (source v1alpha1.TransparencyLogInstance ) ( * pbtrustroot.TransparencyLogInstance , error ) {
145161 pbpk , pk , err := DeserializePublicKey (source .PublicKey )
146162 if err != nil {
147- return nil // TODO: log error? Add return error?
163+ return nil , err
148164 }
149165 logID , err := cosign .GetTransparencyLogID (pk )
150166 if err != nil {
151- return nil // TODO: log error? Add return error?
167+ return nil , err
152168 }
153169
154170 return & pbtrustroot.TransparencyLogInstance {
@@ -158,7 +174,7 @@ func ConvertTransparencyLogInstance(source v1alpha1.TransparencyLogInstance) *pb
158174 LogId : & pbcommon.LogId {
159175 KeyId : []byte (logID ),
160176 },
161- }
177+ }, nil
162178}
163179
164180func HashStringToHashAlgorithm (hash string ) pbcommon.HashAlgorithm {
@@ -195,17 +211,17 @@ func SerializePublicKey(publicKey *pbcommon.PublicKey) []byte {
195211 return pem .EncodeToMemory (block )
196212}
197213
198- func DeserializeCertChain (chain []byte ) * pbcommon.X509CertificateChain {
214+ func DeserializeCertChain (chain []byte ) ( * pbcommon.X509CertificateChain , error ) {
199215 var certs []* pbcommon.X509Certificate
200- for {
201- var block * pem. Block
216+ var block * pem. Block
217+ for len ( chain ) > 0 {
202218 block , chain = pem .Decode (chain )
203219 if block == nil {
204- break
220+ return nil , fmt . Errorf ( "failed to decode certificate chain PEM" )
205221 }
206222 certs = append (certs , & pbcommon.X509Certificate {RawBytes : block .Bytes })
207223 }
208- return & pbcommon.X509CertificateChain {Certificates : certs }
224+ return & pbcommon.X509CertificateChain {Certificates : certs }, nil
209225}
210226
211227func DeserializePublicKey (publicKey []byte ) (* pbcommon.PublicKey , crypto.PublicKey , error ) {
0 commit comments