| 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 agescip-go gomod filippo.io/age . `filippo.io/age`/ |
| 6 | |
| 7 | import ( |
| 8 | "bufioscip-go gomod github.com/golang/go/src . bufio/" |
| 9 | "fmtscip-go gomod github.com/golang/go/src . fmt/" |
| 10 | "ioscip-go gomod github.com/golang/go/src . io/" |
| 11 | "stringsscip-go gomod github.com/golang/go/src . strings/" |
| 12 | "unicode/utf8scip-go gomod github.com/golang/go/src . `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 ParseIdentitiesscip-go gomod filippo.io/age . `filippo.io/age`/ParseIdentities().(f ioscip-go gomod github.com/golang/go/src . io/.Readerscip-go gomod github.com/golang/go/src . io/Reader#) ([]Identityscip-go gomod filippo.io/age . `filippo.io/age`/Identity#, error) { |
| 25 | const privateKeySizeLimit = 1 << 24 // 16 MiB |
| 26 | var ids []Identityscip-go gomod filippo.io/age . `filippo.io/age`/Identity# |
| 27 | lr := &ioscip-go gomod github.com/golang/go/src . io/.LimitedReaderscip-go gomod github.com/golang/go/src . io/LimitedReader#{Rscip-go gomod github.com/golang/go/src . io/LimitedReader#R.: f, Nscip-go gomod github.com/golang/go/src . io/LimitedReader#N.: privateKeySizeLimit + 1} |
| 28 | scanner := bufioscip-go gomod github.com/golang/go/src . bufio/.NewScannerscip-go gomod github.com/golang/go/src . bufio/NewScanner().(lr) |
| 29 | var n int |
| 30 | for scanner.Scanscip-go gomod github.com/golang/go/src . bufio/Scanner#Scan().() { |
| 31 | n++ |
| 32 | line := scanner.Textscip-go gomod github.com/golang/go/src . bufio/Scanner#Text().() |
| 33 | if stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(line, "#") || line == "" { |
| 34 | continue |
| 35 | } |
| 36 | if !utf8scip-go gomod github.com/golang/go/src . `unicode/utf8`/.ValidStringscip-go gomod github.com/golang/go/src . `unicode/utf8`/ValidString().(line) { |
| 37 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("identities file is not valid UTF-8") |
| 38 | } |
| 39 | i, err := parseIdentityscip-go gomod filippo.io/age . `filippo.io/age`/parseIdentity().(line) |
| 40 | if err != nil { |
| 41 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("error at line %d: %v", n, err) |
| 42 | } |
| 43 | ids = append(ids, i) |
| 44 | } |
| 45 | if err := scanner.Errscip-go gomod github.com/golang/go/src . bufio/Scanner#Err().(); err != nil { |
| 46 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to read identities file: %v", err) |
| 47 | } |
| 48 | if lr.Nscip-go gomod github.com/golang/go/src . io/LimitedReader#N. == 0 { |
| 49 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("identities file is too long") |
| 50 | } |
| 51 | if len(ids) == 0 { |
| 52 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("no identities found") |
| 53 | } |
| 54 | return ids, nil |
| 55 | } |
| 56 | |
| 57 | func parseIdentityscip-go gomod filippo.io/age . `filippo.io/age`/parseIdentity().(arg string) (Identityscip-go gomod filippo.io/age . `filippo.io/age`/Identity#, error) { |
| 58 | switch { |
| 59 | case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(arg, "AGE-SECRET-KEY-1"): |
| 60 | return ParseX25519Identityscip-go gomod filippo.io/age . `filippo.io/age`/ParseX25519Identity().(arg) |
| 61 | case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(arg, "AGE-SECRET-KEY-PQ-1"): |
| 62 | return ParseHybridIdentityscip-go gomod filippo.io/age . `filippo.io/age`/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, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . 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 ParseRecipientsscip-go gomod filippo.io/age . `filippo.io/age`/ParseRecipients().(f ioscip-go gomod github.com/golang/go/src . io/.Readerscip-go gomod github.com/golang/go/src . io/Reader#) ([]Recipientscip-go gomod filippo.io/age . `filippo.io/age`/Recipient#, error) { |
| 81 | const recipientFileSizeLimit = 1 << 24 // 16 MiB |
| 82 | var recs []Recipientscip-go gomod filippo.io/age . `filippo.io/age`/Recipient# |
| 83 | lr := &ioscip-go gomod github.com/golang/go/src . io/.LimitedReaderscip-go gomod github.com/golang/go/src . io/LimitedReader#{Rscip-go gomod github.com/golang/go/src . io/LimitedReader#R.: f, Nscip-go gomod github.com/golang/go/src . io/LimitedReader#N.: recipientFileSizeLimit + 1} |
| 84 | scanner := bufioscip-go gomod github.com/golang/go/src . bufio/.NewScannerscip-go gomod github.com/golang/go/src . bufio/NewScanner().(lr) |
| 85 | var n int |
| 86 | for scanner.Scanscip-go gomod github.com/golang/go/src . bufio/Scanner#Scan().() { |
| 87 | n++ |
| 88 | line := scanner.Textscip-go gomod github.com/golang/go/src . bufio/Scanner#Text().() |
| 89 | if stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(line, "#") || line == "" { |
| 90 | continue |
| 91 | } |
| 92 | if !utf8scip-go gomod github.com/golang/go/src . `unicode/utf8`/.ValidStringscip-go gomod github.com/golang/go/src . `unicode/utf8`/ValidString().(line) { |
| 93 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("recipients file is not valid UTF-8") |
| 94 | } |
| 95 | r, err := parseRecipientscip-go gomod filippo.io/age . `filippo.io/age`/parseRecipient().(line) |
| 96 | if err != nil { |
| 97 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("error at line %d: %v", n, err) |
| 98 | } |
| 99 | recs = append(recs, r) |
| 100 | } |
| 101 | if err := scanner.Errscip-go gomod github.com/golang/go/src . bufio/Scanner#Err().(); err != nil { |
| 102 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("failed to read recipients file: %v", err) |
| 103 | } |
| 104 | if lr.Nscip-go gomod github.com/golang/go/src . io/LimitedReader#N. == 0 { |
| 105 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("recipients file is too long") |
| 106 | } |
| 107 | if len(recs) == 0 { |
| 108 | return nil, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("no recipients found") |
| 109 | } |
| 110 | return recs, nil |
| 111 | } |
| 112 | |
| 113 | func parseRecipientscip-go gomod filippo.io/age . `filippo.io/age`/parseRecipient().(arg string) (Recipientscip-go gomod filippo.io/age . `filippo.io/age`/Recipient#, error) { |
| 114 | switch { |
| 115 | case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(arg, "age1pq1"): |
| 116 | return ParseHybridRecipientscip-go gomod filippo.io/age . `filippo.io/age`/ParseHybridRecipient().(arg) |
| 117 | case stringsscip-go gomod github.com/golang/go/src . strings/.HasPrefixscip-go gomod github.com/golang/go/src . strings/HasPrefix().(arg, "age1"): |
| 118 | return ParseX25519Recipientscip-go gomod filippo.io/age . `filippo.io/age`/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, fmtscip-go gomod github.com/golang/go/src . fmt/.Errorfscip-go gomod github.com/golang/go/src . fmt/Errorf().("unknown recipient type") |
| 123 | } |
| 124 | } |