internal/format: switch armor to PEM

It's with a heavy heart that I admit using the ASCII header as part of
the armor was clever, and you know what we think about being clever
around here.

Still, PEM is so lax, we target a subset without headers, and without
garbage before and after the markers.

-----BEGIN AGE ENCRYPTED FILE-----
VGhpcyBpcyBhIGZpbGUgZW5jcnlwdGVkIHdpdGggYWdlLXRvb2wuY29tLCB2ZXJz
aW9uIDEKLT4gWDI1NTE5IGozWWtNTWtaVGNDc0tKVGtMN29aam9NT2FUaGpBTVdU
Y1k5ZHVNdWJhUlkKb0F5d2N4ZW1lSTM1SkZiWHIxcHRFWW0rMjNzK3RuOTg1OHpN
L0ZkVzNCTQotLS0gQWZqdXFFaXNhbmYxbGpPRVZsSS9QM0wyM0RrTHRWWElsQnFu
ejFmRW4zdwq1FMc+yjVJBDuBUZSPMi0nCAtELIObQOHHQlQnvhk6BCITceOD5DbN
S7b6oumB8i/hEJvTtsOLgTBofzqzB90iAQ==
-----END AGE ENCRYPTED FILE-----

AGE-SECRET-KEY-1Y77J4M9R7GEKMZHR6YFDLDWV74VK2YQV4C7SR2H7SSVVJ05HQS4Q7NNMS3
This commit is contained in:
Filippo Valsorda
2019-12-27 17:13:20 +01:00
committed by Filippo Valsorda
parent b142e0fd01
commit c434eee6e8
4 changed files with 122 additions and 87 deletions
+10 -27
View File
@@ -18,7 +18,6 @@ import (
)
type Header struct {
Armor bool
Recipients []*Recipient
MAC []byte
}
@@ -45,8 +44,6 @@ const columnsPerLine = 64
const bytesPerLine = columnsPerLine / 4 * 3
const intro = "This is a file encrypted with age-tool.com, version 1\n"
const introWithArmor = "This is an armored file encrypted with age-tool.com, version 1\n"
const introWithArmorCRLF = "This is an armored file encrypted with age-tool.com, version 1\r\n"
var recipientPrefix = []byte("->")
var footerPrefix = []byte("---")
@@ -75,14 +72,8 @@ func (r *Recipient) Marshal(w io.Writer) error {
}
func (h *Header) MarshalWithoutMAC(w io.Writer) error {
if h.Armor {
if _, err := io.WriteString(w, introWithArmor); err != nil {
return err
}
} else {
if _, err := io.WriteString(w, intro); err != nil {
return err
}
if _, err := io.WriteString(w, intro); err != nil {
return err
}
for _, r := range h.Recipients {
if err := r.Marshal(w); err != nil {
@@ -118,19 +109,18 @@ func Parse(input io.Reader) (*Header, io.Reader, error) {
h := &Header{}
rr := bufio.NewReader(input)
// TODO: find a way to communicate to the caller that the file was armored,
// as they might not appreciate the malleability.
if start, _ := rr.Peek(len(armorPreamble)); string(start) == armorPreamble {
input = ArmoredReader(rr)
rr = bufio.NewReader(input)
}
line, err := rr.ReadString('\n')
if err != nil {
return nil, nil, errorf("failed to read intro: %v", err)
}
var normalizeCRLF bool
switch line {
case intro:
case introWithArmor:
h.Armor = true
case introWithArmorCRLF:
h.Armor = true
normalizeCRLF = true
default:
if line != intro {
return nil, nil, errorf("unexpected intro: %q", line)
}
@@ -140,13 +130,6 @@ func Parse(input io.Reader) (*Header, io.Reader, error) {
if err != nil {
return nil, nil, errorf("failed to read header: %v", err)
}
if normalizeCRLF {
if !bytes.HasSuffix(line, []byte("\r\n")) {
return nil, nil, errorf("unexpected LF in CRLF input")
}
line[len(line)-2] = '\n'
line = line[:len(line)-1]
}
if bytes.HasPrefix(line, footerPrefix) {
prefix, args := splitArgs(line)