Skip to content

Commit 8b78d6c

Browse files
committed
std: simplify trust-config with new API
1 parent 2b92a08 commit 8b78d6c

File tree

5 files changed

+45
-44
lines changed

5 files changed

+45
-44
lines changed

std/ndn/engine.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ type ExpressCallbackArgs struct {
8484
NackReason uint64
8585
// Error, if the result is InterestResultError.
8686
Error error
87+
// IsLocal indicates if a local copy of the Data was found.
88+
// e.g. returned by ExpressR when used with TryStore.
89+
IsLocal bool
8790
}
8891

8992
// InterestHandler represents the callback function for an Interest handler.

std/object/client_trust.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func (c *Client) ValidateExt(args ndn.ValidateExtArgs) {
5252
Config: config,
5353
Retries: 3,
5454
Callback: callback,
55+
TryStore: c.store,
5556
})
5657
},
5758
})

std/object/expressr.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func ExpressR(engine ndn.Engine, args ndn.ExpressRArgs) {
3636
Data: data,
3737
RawData: wire,
3838
SigCovered: sigCov,
39+
IsLocal: true,
3940
})
4041
})
4142
return

std/security/trust_config.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/named-data/ndnd/std/ndn"
1010
spec "github.com/named-data/ndnd/std/ndn/spec_2022"
1111
"github.com/named-data/ndnd/std/security/signer"
12+
"github.com/named-data/ndnd/std/utils"
1213
)
1314

1415
// TrustConfig is the configuration of the trust module.
@@ -85,6 +86,7 @@ type TrustConfigValidateArgs struct {
8586
DataSigCov enc.Wire
8687

8788
// Fetch is the fetch function to use for fetching certificates.
89+
// The fetcher MUST check the store for the certificate before fetching.
8890
Fetch func(enc.Name, *ndn.InterestConfig, ndn.ExpressCallbackFunc)
8991
// Callback is the callback to call when validation is done.
9092
Callback func(bool, error)
@@ -253,33 +255,6 @@ func (tc *TrustConfig) Validate(args TrustConfigValidateArgs) {
253255
return
254256
}
255257

256-
// Attempt to get cert from store.
257-
if storeCertBytes, err := tc.keychain.Store().Get(keyLocator, true); err != nil { // store is broken (panic)
258-
log.Error(tc, "Store returned error", "err", err)
259-
args.Callback(false, err)
260-
return
261-
} else if len(storeCertBytes) > 0 {
262-
// Attempt to parse the certificate
263-
storeCert, storeCertCov, err := spec.Spec{}.ReadData(enc.NewBufferView(storeCertBytes))
264-
if err != nil {
265-
// This is not supposed to happen, misconfiguration likely
266-
log.Warn(tc, "Failed to parse certificate in store", "err", err)
267-
} else if CertIsExpired(storeCert) {
268-
// No log, this will happen often.
269-
// Try to fetch a fresh cert from network.
270-
} else {
271-
// The store is not trusted, we must validate the cert
272-
args.cert = storeCert
273-
args.certSigCov = storeCertCov
274-
args.certRaw = nil // don't re-store
275-
args.certIsValid = false
276-
277-
// Continue validation with store cert
278-
tc.Validate(args)
279-
return
280-
}
281-
}
282-
283258
// Cert not found, attempt to fetch from network
284259
args.Fetch(keyLocator, &ndn.InterestConfig{
285260
CanBePrefix: true,
@@ -312,7 +287,7 @@ func (tc *TrustConfig) Validate(args TrustConfigValidateArgs) {
312287
// Call again with the fetched cert
313288
args.cert = res.Data
314289
args.certSigCov = res.SigCovered
315-
args.certRaw = res.RawData
290+
args.certRaw = utils.If(!res.IsLocal, res.RawData, nil) // prevent double insert
316291
args.certIsValid = false
317292

318293
// Continue validation with fetched cert

std/security/trust_config_test.go

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,23 +186,39 @@ func testTrustConfig(t *testing.T, keychain ndn.KeyChain, schema ndn.TrustSchema
186186
// Simulate fetch from network using engine
187187
fetchCount := 0
188188
fetch := func(name enc.Name, _ *ndn.InterestConfig, callback ndn.ExpressCallbackFunc) {
189-
fetchCount++
190-
for certName, certWire := range network {
191-
if strings.HasPrefix(certName, name.String()) {
192-
data, sigCov, err := spec.Spec{}.ReadData(enc.NewWireView(certWire))
193-
callback(ndn.ExpressCallbackArgs{
194-
Result: ndn.InterestResultData,
195-
Data: data,
196-
RawData: certWire,
197-
SigCovered: sigCov,
198-
Error: err,
199-
})
200-
return
189+
var certWire enc.Wire = nil
190+
var isLocal bool = false
191+
192+
// Fetch functions are required to check the store first
193+
if buf, _ := keychain.Store().Get(name, true); buf != nil {
194+
certWire = enc.Wire{buf}
195+
isLocal = true
196+
} else {
197+
// Simulate fetch from network
198+
fetchCount++
199+
for netName, netWire := range network {
200+
if strings.HasPrefix(netName, name.String()) {
201+
certWire = netWire
202+
break
203+
}
201204
}
202205
}
203-
callback(ndn.ExpressCallbackArgs{
204-
Result: ndn.InterestResultNack,
205-
})
206+
207+
if certWire != nil {
208+
data, sigCov, err := spec.Spec{}.ReadData(enc.NewWireView(certWire))
209+
callback(ndn.ExpressCallbackArgs{
210+
Result: ndn.InterestResultData,
211+
Data: data,
212+
RawData: certWire,
213+
SigCovered: sigCov,
214+
Error: err,
215+
IsLocal: isLocal,
216+
})
217+
} else {
218+
callback(ndn.ExpressCallbackArgs{
219+
Result: ndn.InterestResultNack,
220+
})
221+
}
206222
}
207223

208224
// Create trust config
@@ -247,10 +263,15 @@ func testTrustConfig(t *testing.T, keychain ndn.KeyChain, schema ndn.TrustSchema
247263
require.True(t, validateSync("/test/bob/data1", bobSigner))
248264
require.Equal(t, 1, fetchCount) // fetch bob's certificate
249265
require.True(t, validateSync("/test/bob/data2", bobSigner))
250-
require.Equal(t, 1, fetchCount) // cert in store
266+
require.Equal(t, 1, fetchCount) // cert in cache
251267
require.True(t, validateSync("/test/cathy/data1", cathySigner))
252268
require.Equal(t, 1, fetchCount) // have all certificates
253269

270+
// Make sure that bob's cert was inserted into the store
271+
if buf, _ := keychain.Store().Get(bobCertData.Name(), false); buf == nil {
272+
t.Error("bob's cert not in store")
273+
}
274+
254275
// Signing with admin key
255276
require.True(t, validateSync("/test/admin/alice/data1", aliceAdminSigner))
256277

0 commit comments

Comments
 (0)