@@ -32,7 +32,9 @@ import (
3232 "github.com/Fraunhofer-AISEC/cmc/internal"
3333)
3434
35- type LibApi struct {}
35+ type LibApi struct {
36+ cmc * cmc.Cmc
37+ }
3638
3739func init () {
3840 CmcApis ["libapi" ] = LibApi {}
@@ -41,17 +43,25 @@ func init() {
4143// Obtains attestation report from CMCd
4244func (a LibApi ) obtainAR (cc * CmcConfig , chbindings []byte , cached []string ) ([]byte , map [string ][]byte , []string , error ) {
4345
44- if cc == nil || cc . Cmc == nil {
46+ if cc == nil {
4547 return nil , nil , nil , errors .New ("internal error: cmc is nil" )
4648 }
4749
48- if len (cc .Cmc .Drivers ) == 0 {
50+ if a .cmc == nil {
51+ cmc , err := cmc .NewCmc (cc .LibApiConfig )
52+ if err != nil {
53+ return nil , nil , nil , fmt .Errorf ("failed to initialize CMC: %v" , err )
54+ }
55+ a .cmc = cmc
56+ }
57+
58+ if len (a .cmc .Drivers ) == 0 {
4959 return nil , nil , nil , errors .New ("no drivers configured" )
5060 }
5161
5262 log .Debug ("Prover: Generating Attestation Report with nonce: " , hex .EncodeToString (chbindings ))
5363
54- report , metadata , cacheMisses , err := cmc .Generate (chbindings , cached , cc . Cmc )
64+ report , metadata , cacheMisses , err := cmc .Generate (chbindings , cached , a . cmc )
5565 if err != nil {
5666 return nil , nil , nil , fmt .Errorf ("failed to generate attestation report: %w" , err )
5767 }
@@ -74,10 +84,18 @@ func (a LibApi) verifyAR(
7484 metadata map [string ][]byte ,
7585) error {
7686
77- if cc == nil || cc . Cmc == nil {
87+ if cc == nil {
7888 return errors .New ("internal error: cmc is nil" )
7989 }
8090
91+ if a .cmc == nil {
92+ cmc , err := cmc .NewCmc (cc .LibApiConfig )
93+ if err != nil {
94+ return fmt .Errorf ("failed to initialize CMC: %v" , err )
95+ }
96+ a .cmc = cmc
97+ }
98+
8199 req := & api.VerificationRequest {
82100 Nonce : nonce ,
83101 Report : report ,
@@ -89,7 +107,7 @@ func (a LibApi) verifyAR(
89107
90108 log .Debug ("Verifier: verifying attestation report" )
91109 result , err := cmc .Verify (req .Report , req .Nonce , req .Policies ,
92- req .Peer , req .CacheMisses , req .Metadata , cc . Cmc )
110+ req .Peer , req .CacheMisses , req .Metadata , a . cmc )
93111 if err != nil {
94112 return fmt .Errorf ("failed to verify: %w" , err )
95113 }
@@ -113,14 +131,22 @@ func (a LibApi) verifyAR(
113131
114132func (a LibApi ) fetchSignature (cc * CmcConfig , digest []byte , opts crypto.SignerOpts ) ([]byte , error ) {
115133
116- if cc == nil || cc . Cmc == nil {
134+ if cc == nil {
117135 return nil , errors .New ("internal error: cmc is nil" )
118136 }
119137
120- if len (cc .Cmc .Drivers ) == 0 {
138+ if a .cmc == nil {
139+ cmc , err := cmc .NewCmc (cc .LibApiConfig )
140+ if err != nil {
141+ return nil , fmt .Errorf ("failed to initialize CMC: %v" , err )
142+ }
143+ a .cmc = cmc
144+ }
145+
146+ if len (a .cmc .Drivers ) == 0 {
121147 return nil , errors .New ("no drivers configured" )
122148 }
123- d := cc . Cmc .Drivers [0 ]
149+ d := a . cmc .Drivers [0 ]
124150
125151 // Get key handle from (hardware) interface
126152 tlsKeyPriv , _ , err := d .GetKeyHandles (ar .IK )
@@ -140,14 +166,22 @@ func (a LibApi) fetchSignature(cc *CmcConfig, digest []byte, opts crypto.SignerO
140166
141167func (a LibApi ) fetchCerts (cc * CmcConfig ) ([][]byte , error ) {
142168
143- if cc == nil || cc . Cmc == nil {
169+ if cc == nil {
144170 return nil , errors .New ("internal error: cmc is nil" )
145171 }
146172
147- if len (cc .Cmc .Drivers ) == 0 {
173+ if a .cmc == nil {
174+ cmc , err := cmc .NewCmc (cc .LibApiConfig )
175+ if err != nil {
176+ return nil , fmt .Errorf ("failed to initialize CMC: %v" , err )
177+ }
178+ a .cmc = cmc
179+ }
180+
181+ if len (a .cmc .Drivers ) == 0 {
148182 return nil , errors .New ("no drivers configured" )
149183 }
150- d := cc . Cmc .Drivers [0 ]
184+ d := a . cmc .Drivers [0 ]
151185
152186 certChain , err := d .GetCertChain (ar .IK )
153187 if err != nil {
@@ -162,13 +196,21 @@ func (a LibApi) fetchCerts(cc *CmcConfig) ([][]byte, error) {
162196// Fetches the peer cache from the cmcd
163197func (a LibApi ) fetchPeerCache (cc * CmcConfig , fingerprint string ) ([]string , error ) {
164198
165- if cc == nil || cc . Cmc == nil {
199+ if cc == nil {
166200 return nil , errors .New ("internal error: cmc is nil" )
167201 }
168202
203+ if a .cmc == nil {
204+ cmc , err := cmc .NewCmc (cc .LibApiConfig )
205+ if err != nil {
206+ return nil , fmt .Errorf ("failed to initialize CMC: %v" , err )
207+ }
208+ a .cmc = cmc
209+ }
210+
169211 log .Debugf ("Fetching peer cache for peer: %v" , fingerprint )
170212
171- c , ok := cc . Cmc .CachedPeerMetadata [fingerprint ]
213+ c , ok := a . cmc .CachedPeerMetadata [fingerprint ]
172214 if ! ok {
173215 log .Tracef ("No data cached for peer %v" , fingerprint )
174216 return nil , nil
0 commit comments