-
-
Notifications
You must be signed in to change notification settings - Fork 579
age: add ExtractHeader, DecryptHeader, and NewInjectedFileKeyIdentity #631
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
Open
FiloSottile
wants to merge
1
commit into
main
Choose a base branch
from
filippo/detached
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ | |
| package age | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "crypto/hmac" | ||
| "crypto/rand" | ||
| "errors" | ||
|
|
@@ -207,22 +208,37 @@ func (*NoIdentityMatchError) Error() string { | |
| // If no identity matches the encrypted file, the returned error will be of type | ||
| // [NoIdentityMatchError]. | ||
| func Decrypt(src io.Reader, identities ...Identity) (io.Reader, error) { | ||
| if len(identities) == 0 { | ||
| return nil, errors.New("no identities specified") | ||
| } | ||
|
|
||
| hdr, payload, err := format.Parse(src) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read header: %w", err) | ||
| } | ||
|
|
||
| fileKey, err := decryptHdr(hdr, identities...) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| nonce := make([]byte, streamNonceSize) | ||
| if _, err := io.ReadFull(payload, nonce); err != nil { | ||
| return nil, fmt.Errorf("failed to read nonce: %w", err) | ||
| } | ||
|
|
||
| return stream.NewReader(streamKey(fileKey, nonce), payload) | ||
| } | ||
|
|
||
| func decryptHdr(hdr *format.Header, identities ...Identity) ([]byte, error) { | ||
| if len(identities) == 0 { | ||
| return nil, errors.New("no identities specified") | ||
| } | ||
|
|
||
| stanzas := make([]*Stanza, 0, len(hdr.Recipients)) | ||
| for _, s := range hdr.Recipients { | ||
| stanzas = append(stanzas, (*Stanza)(s)) | ||
| } | ||
| errNoMatch := &NoIdentityMatchError{} | ||
| var fileKey []byte | ||
| for _, id := range identities { | ||
| var err error | ||
| fileKey, err = id.Unwrap(stanzas) | ||
| if errors.Is(err, ErrIncorrectIdentity) { | ||
| errNoMatch.Errors = append(errNoMatch.Errors, err) | ||
|
|
@@ -244,12 +260,7 @@ func Decrypt(src io.Reader, identities ...Identity) (io.Reader, error) { | |
| return nil, errors.New("bad header MAC") | ||
| } | ||
|
|
||
| nonce := make([]byte, streamNonceSize) | ||
| if _, err := io.ReadFull(payload, nonce); err != nil { | ||
| return nil, fmt.Errorf("failed to read nonce: %w", err) | ||
| } | ||
|
|
||
| return stream.NewReader(streamKey(fileKey, nonce), payload) | ||
| return fileKey, nil | ||
| } | ||
|
|
||
| // multiUnwrap is a helper that implements Identity.Unwrap in terms of a | ||
|
|
@@ -270,3 +281,49 @@ func multiUnwrap(unwrap func(*Stanza) ([]byte, error), stanzas []*Stanza) ([]byt | |
| } | ||
| return nil, ErrIncorrectIdentity | ||
| } | ||
|
|
||
| // ExtractHeader returns a detched header from the src file. | ||
|
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. Typo: s/detched/detached/ |
||
| // | ||
| // The detached header can be decrypted with [DecryptHeader] and then the file | ||
| // key can be used with [NewInjectedFileKeyIdentity]. | ||
| func ExtractHeader(src io.Reader) ([]byte, error) { | ||
| hdr, _, err := format.Parse(src) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read header: %w", err) | ||
| } | ||
| buf := &bytes.Buffer{} | ||
| if err := hdr.Marshal(buf); err != nil { | ||
| return nil, fmt.Errorf("failed to serialize header: %w", err) | ||
| } | ||
| return buf.Bytes(), nil | ||
| } | ||
|
|
||
| // DecryptHeader decrypts a detached header and returns a file key. | ||
| // | ||
| // The detached header can be produced by [ExtractHeader], and the | ||
| // returned file key can be used with [NewInjectedFileKeyIdentity]. | ||
| // | ||
| // It is the caller's responsibility to keep track of what file the | ||
| // returned file key decrypts, and to ensure the file key is not used | ||
| // for any other purpose. | ||
| func DecryptHeader(header []byte, identities ...Identity) ([]byte, error) { | ||
| hdr, _, err := format.Parse(bytes.NewReader(header)) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read header: %w", err) | ||
| } | ||
| return decryptHdr(hdr, identities...) | ||
| } | ||
|
|
||
| type injectedFileKeyIdentity struct { | ||
| fileKey []byte | ||
| } | ||
|
|
||
| // NewInjectedFileKeyIdentity returns an [Identity] that always produces | ||
| // a fixed file key, such as one returned by [DecryptHeader]. | ||
| func NewInjectedFileKeyIdentity(fileKey []byte) Identity { | ||
| return injectedFileKeyIdentity{fileKey} | ||
| } | ||
|
Comment on lines
+317
to
+325
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. The name isn't immediately clear imo, maybe |
||
|
|
||
| func (i injectedFileKeyIdentity) Unwrap(stanzas []*Stanza) (fileKey []byte, err error) { | ||
| return i.fileKey, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is an error that might be worth exporting to allow library users to easily spot it (
errors.Is(err, age.ErrNoIdentity)) and issue proper guidance to their users.