Skip to content

Commit b2fdd83

Browse files
committed
Provide output for all external commands in subsequent error messages
1 parent ff2b628 commit b2fdd83

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

fetcher/crypto_cmds.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,35 @@ import (
77

88
// Verify the signature of signedTopology
99
// using the CA bundle rootCertsBundlePath, and output the verified payload to verifiedTopology.
10-
func cmsVerifyOutput(ctx context.Context, signedTopology, rootCertsBundlePath, verifiedTopology string) (err error) {
10+
func cmsVerifyOutput(ctx context.Context, signedTopology, rootCertsBundlePath, verifiedTopology string) (output []byte, err error) {
1111
if !ctx.Value("nativeCrypto").(bool) {
1212
if err = checkExecutable("openssl"); err != nil {
1313
return
1414
}
1515
return opensslCMSVerifyOutput(ctx, signedTopology, rootCertsBundlePath, verifiedTopology)
1616
}
17-
return fmt.Errorf("not implemented crypto engine: nativeCrypto=%t", ctx.Value("nativeCrypto"))
17+
return []byte{}, fmt.Errorf("not implemented crypto engine: nativeCrypto=%t", ctx.Value("nativeCrypto"))
1818
}
1919

2020
// Detach the signature on signedTopology in the PKCS#7 format.
21-
func smimePk7out(ctx context.Context, signedTopology, detachedSignaturePath string) (err error) {
21+
func smimePk7out(ctx context.Context, signedTopology, detachedSignaturePath string) (output []byte, err error) {
2222
if !ctx.Value("nativeCrypto").(bool) {
2323
if err = checkExecutable("openssl"); err != nil {
2424
return
2525
}
2626
return opensslSMIMEPk7out(ctx, signedTopology, detachedSignaturePath)
2727
}
28-
return fmt.Errorf("not implemented crypto engine: nativeCrypto=%t", ctx.Value("nativeCrypto"))
28+
return []byte{}, fmt.Errorf("not implemented crypto engine: nativeCrypto=%t", ctx.Value("nativeCrypto"))
2929
}
3030

3131
// Extract the certificate bundle into asCertHumanChain
3232
// from the detached signature at detachedSignaturePath.
33-
func pkcs7Certs(ctx context.Context, detachedSignaturePath, asCertHumanChain string) (err error) {
33+
func pkcs7Certs(ctx context.Context, detachedSignaturePath, asCertHumanChain string) (output []byte, err error) {
3434
if !ctx.Value("nativeCrypto").(bool) {
3535
if err = checkExecutable("openssl"); err != nil {
3636
return
3737
}
3838
return opensslPKCS7Certs(ctx, detachedSignaturePath, asCertHumanChain)
3939
}
40-
return fmt.Errorf("not implemented crypto engine: nativeCrypto=%t", ctx.Value("nativeCrypto"))
40+
return []byte{}, fmt.Errorf("not implemented crypto engine: nativeCrypto=%t", ctx.Value("nativeCrypto"))
4141
}

fetcher/openssl_cmds.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ import (
77

88
// opensslCMSVerifyOutput uses the openssl cms module to verify the signature of signedTopology
99
// using the CA bundle rootCertsBundlePath, and outputs the verified payload to verifiedTopology.
10-
func opensslCMSVerifyOutput(ctx context.Context, signedTopology, rootCertsBundlePath, verifiedTopology string) error {
11-
return exec.CommandContext(ctx, "openssl", "cms", "-verify", "-in", signedTopology,
12-
"-CAfile", rootCertsBundlePath, "-purpose", "any", "-noout", "-text", "-out", verifiedTopology).Run()
10+
func opensslCMSVerifyOutput(ctx context.Context, signedTopo, rootCertsBundlePath, verifiedTopo string) ([]byte, error) {
11+
return exec.CommandContext(ctx, "openssl", "cms", "-verify", "-in", signedTopo,
12+
"-CAfile", rootCertsBundlePath, "-purpose", "any", "-noout", "-text", "-out", verifiedTopo).CombinedOutput()
1313
}
1414

1515
// opensslSMIMEPk7out uses the openssl smime module to detach the signature on signedTopology in the PKCS#7 format.
16-
func opensslSMIMEPk7out(ctx context.Context, signedTopology, detachedSignaturePath string) error {
16+
func opensslSMIMEPk7out(ctx context.Context, signedTopo, detachedSignaturePath string) ([]byte, error) {
1717
return exec.CommandContext(ctx, "openssl", "smime", "-pk7out",
18-
"-in", signedTopology, "-out", detachedSignaturePath).Run()
18+
"-in", signedTopo, "-out", detachedSignaturePath).CombinedOutput()
1919
}
2020

2121
// opensslPKCS7Certs uses the openssl pkcs7 module to extract the certificate bundle into asCertHumanChain
2222
// from the detached signature at detachedSignaturePath.
23-
func opensslPKCS7Certs(ctx context.Context, detachedSignaturePath, asCertHumanChain string) error {
23+
func opensslPKCS7Certs(ctx context.Context, detachedSignaturePath, asCertHumanChain string) ([]byte, error) {
2424
return exec.CommandContext(ctx, "openssl", "pkcs7", "-in", detachedSignaturePath,
25-
"-inform", "PEM", "-print_certs", "-out", asCertHumanChain).Run()
25+
"-inform", "PEM", "-print_certs", "-out", asCertHumanChain).CombinedOutput()
2626
}

fetcher/scion_cppki_verify.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func verifyTopologySignature(cfg *config.Config) error {
7171
// verify the AS certificate chain (but not the payload signature) back to the TRCs of the ISD follows the
7272
// SCION CP PKI rules about cert type, key usage:
7373
if stdoutStderr, err := spkiCertVerify(ctx, sortedTRCsPaths, asCertChainPath); err != nil {
74-
return fmt.Errorf("unable to validate certificate chain: %s %w", stdoutStderr, err)
74+
return fmt.Errorf("unable to validate certificate chain: %w: scion-pki output: %s", err, stdoutStderr)
7575
}
7676

7777
var unvalidatedTopologyPath string
@@ -149,13 +149,15 @@ func verifyWithRootBundle(ctx context.Context,
149149
_, trcFileName := filepath.Split(trustAnchorTRC)
150150
rootCertsBundlePath := filepath.Join(verifyPath, trcFileName+".certs.pem")
151151
// extract TRC certificates:
152-
if err = spkiTRCExtractCerts(ctx, trustAnchorTRC, rootCertsBundlePath); err != nil {
153-
return fmt.Errorf("unable to extract root certificates from TRC %s: %w",
154-
trustAnchorTRC, err)
152+
var stdoutStderr []byte
153+
if stdoutStderr, err = spkiTRCExtractCerts(ctx, trustAnchorTRC, rootCertsBundlePath); err != nil {
154+
return fmt.Errorf("unable to extract root certificates from TRC %s: %w: scion-pki output: %s",
155+
trustAnchorTRC, err, stdoutStderr)
155156
}
156157
// verify the signature and certificate chain back to a root certs bundle, write out the payload:
157-
if err = cmsVerifyOutput(ctx, signedTopology, rootCertsBundlePath, unvalidatedTopologyPath); err != nil {
158-
return fmt.Errorf("verifying and extracting signed payload failed: %w", err)
158+
if stdoutStderr, err = cmsVerifyOutput(ctx, signedTopology, rootCertsBundlePath, unvalidatedTopologyPath); err != nil {
159+
return fmt.Errorf("verifying and extracting signed payload failed: %w: Verification output: %s",
160+
err, stdoutStderr)
159161
}
160162
return
161163
}
@@ -167,17 +169,19 @@ func extractSignerInfo(ctx context.Context, signedTopology, verifyPath string) (
167169

168170
detachedSignaturePath := filepath.Join(verifyPath, "detached_signature.p7s")
169171
// detach signature for further validation:
170-
err = smimePk7out(ctx, signedTopology, detachedSignaturePath)
172+
var stdoutStderr []byte
173+
stdoutStderr, err = smimePk7out(ctx, signedTopology, detachedSignaturePath)
171174
if err != nil {
172-
err = fmt.Errorf("unable to detach signature: %w", err)
175+
err = fmt.Errorf("unable to detach signature: %w: Verification output: %s", err, stdoutStderr)
173176
return
174177
}
175178

176179
asCertHumanChain := filepath.Join(verifyPath, "as_cert_chain.human.pem")
177180
// collect included certificates from detached signature:
178-
err = pkcs7Certs(ctx, detachedSignaturePath, asCertHumanChain)
181+
stdoutStderr, err = pkcs7Certs(ctx, detachedSignaturePath, asCertHumanChain)
179182
if err != nil {
180-
err = fmt.Errorf("unable to gather included certificates from signature: %w", err)
183+
err = fmt.Errorf("unable to gather included certificates from signature: %w: Verification output: %s",
184+
err, stdoutStderr)
181185
return
182186
}
183187

@@ -256,9 +260,10 @@ func verifyTRCUpdateChain(outputPath, candidateTRCPath string, strict bool) erro
256260
}
257261
trcUpdateChainPaths := sortTRCsFiles(trcs).Paths()
258262
trcUpdateChainPaths = append(trcUpdateChainPaths, candidateTRCPath)
259-
err = spkiTRCVerify(ctx, trcUpdateChainPaths[0], trcUpdateChainPaths[1:])
263+
var stdoutStderr []byte
264+
stdoutStderr, err = spkiTRCVerify(ctx, trcUpdateChainPaths[0], trcUpdateChainPaths[1:])
260265
if err != nil {
261-
return fmt.Errorf("validating TRC update chain failed: %w", err)
266+
return fmt.Errorf("validating TRC update chain failed: %w: scion-pki output: %s", err, stdoutStderr)
262267
}
263268
return nil
264269
}

fetcher/scion_pki_tool_cmds.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
// scion-pki commands
1010

1111
// spkiTRCExtractCerts extracts the certificates contained in the TRC trustAnchorTRC into rootCertsBundlePath.
12-
func spkiTRCExtractCerts(ctx context.Context, trustAnchorTRC, rootCertsBundlePath string) error {
12+
func spkiTRCExtractCerts(ctx context.Context, trustAnchorTRC, rootCertsBundlePath string) ([]byte, error) {
1313
return exec.CommandContext(ctx, "scion-pki", "trc", "extract", "certificates",
14-
trustAnchorTRC, "-o", rootCertsBundlePath).Run()
14+
trustAnchorTRC, "-o", rootCertsBundlePath).CombinedOutput()
1515
}
1616

1717
// spkiCertVerify verifies the AS certificate asCertChainPath
@@ -22,9 +22,9 @@ func spkiCertVerify(ctx context.Context, trcsUpdateChain []string, asCertChainPa
2222
}
2323

2424
// spkiTRCVerify verifies the TRC update chain for candidateTRCPath anchored in the TRCs trcUpdateChainPaths
25-
func spkiTRCVerify(ctx context.Context, trcAnchorPath string, updateChainCandidatePaths []string) error {
25+
func spkiTRCVerify(ctx context.Context, trcAnchorPath string, updateChainCandidatePaths []string) ([]byte, error) {
2626
cmdArgs := []string{"trc", "verify", "--anchor"}
2727
cmdArgs = append(cmdArgs, trcAnchorPath)
2828
cmdArgs = append(cmdArgs, updateChainCandidatePaths...)
29-
return exec.CommandContext(ctx, "scion-pki", cmdArgs...).Run()
29+
return exec.CommandContext(ctx, "scion-pki", cmdArgs...).CombinedOutput()
3030
}

0 commit comments

Comments
 (0)