Debian Codegraph @

parse.go

raw symbols SCIP
1 // Copyright 2021 The age Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package age
6
7 import (
8 "bufio"
9 "fmt"
10 "io"
11 "strings"
12 "unicode/utf8"
13 )
14
15 // ParseIdentities parses a file with one or more private key encodings, one per
16 // line. Empty lines and lines starting with "#" are ignored.
17 //
18 // This is the same syntax as the private key files accepted by the CLI, except
19 // the CLI also accepts SSH private keys, which are not recommended for the
20 // average application, and plugins, which involve invoking external programs.
21 //
22 // Currently, all returned values are of type *[X25519Identity] or
23 // *[HybridIdentity], but different types might be returned in the future.
24 func ParseIdentities(f io.Reader) ([]Identity, error) {
25 const privateKeySizeLimit = 1 << 24 // 16 MiB
26 var ids []Identity
27 lr := &io.LimitedReader{R: f, N: privateKeySizeLimit + 1}
28 scanner := bufio.NewScanner(lr)
29 var n int
30 for scanner.Scan() {
31 n++
32 line := scanner.Text()
33 if strings.HasPrefix(line, "#") || line == "" {
34 continue
35 }
36 if !utf8.ValidString(line) {
37 return nil, fmt.Errorf("identities file is not valid UTF-8")
38 }
39 i, err := parseIdentity(line)
40 if err != nil {
41 return nil, fmt.Errorf("error at line %d: %v", n, err)
42 }
43 ids = append(ids, i)
44 }
45 if err := scanner.Err(); err != nil {
46 return nil, fmt.Errorf("failed to read identities file: %v", err)
47 }
48 if lr.N == 0 {
49 return nil, fmt.Errorf("identities file is too long")
50 }
51 if len(ids) == 0 {
52 return nil, fmt.Errorf("no identities found")
53 }
54 return ids, nil
55 }
56
57 func parseIdentity(arg string) (Identity, error) {
58 switch {
59 case strings.HasPrefix(arg, "AGE-SECRET-KEY-1"):
60 return ParseX25519Identity(arg)
61 case strings.HasPrefix(arg, "AGE-SECRET-KEY-PQ-1"):
62 return ParseHybridIdentity(arg)
63 default:
64 // Don't include arg in the error: it may contain private key material,
65 // and callers print these errors.
66 return nil, fmt.Errorf("unknown identity type")
67 }
68 }
69
70 // ParseRecipients parses a file with one or more public key encodings, one per
71 // line. Empty lines and lines starting with "#" are ignored.
72 //
73 // This is the same syntax as the recipients files accepted by the CLI, except
74 // the CLI also accepts SSH recipients, which are not recommended for the
75 // average application, tagged recipients, which have different privacy
76 // properties, and plugins, which involve invoking external programs.
77 //
78 // Currently, all returned values are of type *[X25519Recipient] or
79 // *[HybridRecipient] but different types might be returned in the future.
80 func ParseRecipients(f io.Reader) ([]Recipient, error) {
81 const recipientFileSizeLimit = 1 << 24 // 16 MiB
82 var recs []Recipient
83 lr := &io.LimitedReader{R: f, N: recipientFileSizeLimit + 1}
84 scanner := bufio.NewScanner(lr)
85 var n int
86 for scanner.Scan() {
87 n++
88 line := scanner.Text()
89 if strings.HasPrefix(line, "#") || line == "" {
90 continue
91 }
92 if !utf8.ValidString(line) {
93 return nil, fmt.Errorf("recipients file is not valid UTF-8")
94 }
95 r, err := parseRecipient(line)
96 if err != nil {
97 return nil, fmt.Errorf("error at line %d: %v", n, err)
98 }
99 recs = append(recs, r)
100 }
101 if err := scanner.Err(); err != nil {
102 return nil, fmt.Errorf("failed to read recipients file: %v", err)
103 }
104 if lr.N == 0 {
105 return nil, fmt.Errorf("recipients file is too long")
106 }
107 if len(recs) == 0 {
108 return nil, fmt.Errorf("no recipients found")
109 }
110 return recs, nil
111 }
112
113 func parseRecipient(arg string) (Recipient, error) {
114 switch {
115 case strings.HasPrefix(arg, "age1pq1"):
116 return ParseHybridRecipient(arg)
117 case strings.HasPrefix(arg, "age1"):
118 return ParseX25519Recipient(arg)
119 default:
120 // It might be a private key from an identities file accidentally used
121 // as a recipients file, so don't include arg in the error.
122 return nil, fmt.Errorf("unknown recipient type")
123 }
124 }