-
Notifications
You must be signed in to change notification settings - Fork 395
API for providing untrusted data about unverified signatures #208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
12514e7
69464bd
e705530
596d319
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,9 +4,13 @@ package signature | |
|
|
||
| import ( | ||
| "bytes" | ||
| "errors" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "strings" | ||
|
|
||
| "github.com/mtrmac/gpgme" | ||
| "golang.org/x/crypto/openpgp" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this wont work for openshift, we will not build with gpgme :(
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not add any new uses of |
||
| ) | ||
|
|
||
| // SigningMechanism abstracts a way to sign binary blobs and verify their signatures. | ||
|
|
@@ -21,6 +25,12 @@ type SigningMechanism interface { | |
| Sign(input []byte, keyIdentity string) ([]byte, error) | ||
| // Verify parses unverifiedSignature and returns the content and the signer's identity | ||
| Verify(unverifiedSignature []byte) (contents []byte, keyIdentity string, err error) | ||
| // UntrustedSignatureContents returns UNTRUSTED contents of the signature WITHOUT ANY VERIFICATION, | ||
| // along with a short identifier of the key used for signing. | ||
| // WARNING: The short key identifier (which correponds to "Key ID" for OpenPGP keys) | ||
| // is NOT the same as a "key identity" used in other calls ot this interface, and | ||
| // the values may have no recognizable relationship if the public key is not available. | ||
| UntrustedSignatureContents(untrustedSignature []byte) (untrustedContents []byte, shortKeyIdentifier string, err error) | ||
| } | ||
|
|
||
| // A GPG/OpenPGP signing mechanism. | ||
|
|
@@ -119,3 +129,31 @@ func (m gpgSigningMechanism) Verify(unverifiedSignature []byte) (contents []byte | |
| } | ||
| return signedBuffer.Bytes(), sig.Fingerprint, nil | ||
| } | ||
|
|
||
| // UntrustedSignatureContents returns UNTRUSTED contents of the signature WITHOUT ANY VERIFICATION, | ||
| // along with a short identifier of the key used for signing. | ||
| // WARNING: The short key identifier (which correponds to "Key ID" for OpenPGP keys) | ||
| // is NOT the same as a "key identity" used in other calls ot this interface, and | ||
| // the values may have no recognizable relationship if the public key is not available. | ||
| func (m gpgSigningMechanism) UntrustedSignatureContents(untrustedSignature []byte) (untrustedContents []byte, shortKeyIdentifier string, err error) { | ||
| // This uses the Golang-native OpenPGP implementation instead of gpgme because we are not doing any cryptography. | ||
| md, err := openpgp.ReadMessage(bytes.NewReader(untrustedSignature), openpgp.EntityList{}, nil, nil) | ||
| if err != nil { | ||
| return nil, "", err | ||
| } | ||
| if !md.IsSigned { | ||
| return nil, "", errors.New("The input is not a signature") | ||
| } | ||
| content, err := ioutil.ReadAll(md.UnverifiedBody) | ||
| if err != nil { | ||
| // Coverage: An error during reading the body can happen only if | ||
| // 1) the message is encrypted, which is not our case (and we don’t give ReadMessage the key | ||
| // to decrypt the contents anyway), or | ||
| // 2) the message is signed AND we give ReadMessage a correspnding public key, which we don’t. | ||
| return nil, "", err | ||
| } | ||
|
|
||
| // Uppercase the key ID for minimal consistency with the gpgme-returned fingerprints | ||
| // (but note that key ID is a suffix of the fingerprint only for V4 keys, not V3)! | ||
| return content, strings.ToUpper(fmt.Sprintf("%016X", md.SignedByKeyId)), nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to return
-1here. the 0 is default for int64 when not set.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well it is returning -1, not 0. Anyway, there is no “good” undefined value to return here if the caller makes a mistake and ignores
err. -1 is perhaps slightly less likely to be valid than a non-negative number… but it really is anybody’s guess whether that makes it more likely to be noticed as invalid or more likely to have completely unpredicted effects. (Go’s error handling sucks, it would be nice to have something like optional checked exceptions, so that a caller can’t ignore an error by mistake without it being very visible.)It seems to me not to be worth it to bother with named return parameters so that we can skip explicitly naming a value returned in the error case—especially because that also makes a decision, namely to use 0, only that decision is not explicitly visible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mtrmac the called should never ignore "err", doing so is a programmer error and we can't prevent all human errors :-)
I'm fine with
-1, just pointing out it is something unusual to me as usually if the return value is not defined, we default to a default value of the type (which in case of int64 is '0').There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, and yet, we are in the business of engineering tools and interfaces to minimize errors anyway. (Ordinarily I wouldn’t bother, but the
signaturesubpackage brings out my worst pedantic tendencies.)