Skip to content

demo: fix UnusedBit panic on signing error - #262

Merged
davidben merged 1 commit into
ietf-plants-wg:mainfrom
Vaisman:fix-unusedbit-panic
Jun 29, 2026
Merged

demo: fix UnusedBit panic on signing error#262
davidben merged 1 commit into
ietf-plants-wg:mainfrom
Vaisman:fix-unusedbit-panic

Conversation

@Vaisman

@Vaisman Vaisman commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

Fixes an operator-precedence bug in CreateCertificate's UnusedBit handling.

&& binds tighter than ||, so sig[len(sig)-1] could be evaluated even when certSig.Bytes() returned an error. Add parentheses so the signature bytes are only inspected when Bytes() succeeds.

Tested:

go test ./...

Comment thread demo/encode.go Outdated
if sig, err := certSig.Bytes(); err == nil && len(sig) == 0 || sig[len(sig)-1]&1 != 0 {
certSig.SetError(errors.New("last bit in signature with not zero, unable to encode as unused"))
sig, err := certSig.Bytes()
if err == nil && (len(sig) == 0 || sig[len(sig)-1]&1 != 0) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason this changed the structure to pull the assignment out of the if?

Comment thread demo/encode_unusedbit_test.go Outdated
if r := recover(); r != nil {
t.Fatalf("CreateCertificate panicked instead of returning an error: %v", r)
}
}()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this for? Go's test framework will already catch panics.

Comment thread demo/encode_unusedbit_test.go Outdated
@@ -0,0 +1,85 @@
package main

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I'm not sure how much it's worth bothering with a test here. It seems mostly scar tissue. At the least, let's put this in encode_test.go.

Comment thread demo/encode_unusedbit_test.go Outdated
if !ok {
t.Fatalf("could not make issuer trust anchor ID")
}
cosignerID, ok := TrustAnchorIDFromString("32473.2")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same ID as the issuer is fine. We expect to have at least one cosigner that's the same name as the issuer anyway.

Comment thread demo/encode_unusedbit_test.go Outdated
0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9, 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01,
0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18, 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56,
0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16, 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1,
0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22, 0xc1,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect this test does not actually need you to put anything here, but if you put this in encode_test.go, it can at least share a constant.

Comment thread demo/encode_unusedbit_test.go Outdated
SignatureAlgorithm: SignatureAlgorithmEd25519,
Signer: failingSigner{},
SignerOpts: crypto.Hash(0),
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I am not thrilled about breaking the Cosigner abstraction here. It's something we'll have to remember to update every time we update that struct.

Instead of all this, I think you could get the same coverage by just passing an invalid subtree to CreateCertificate.

@Vaisman
Vaisman force-pushed the fix-unusedbit-panic branch from 87afb57 to a6888e8 Compare June 28, 2026 05:37
@Vaisman

Vaisman commented Jun 28, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, that makes sense. Reduced this to the minimal one-line fix and dropped the regression test.

@davidben
davidben merged commit 56cc9c3 into ietf-plants-wg:main Jun 29, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants