-
Notifications
You must be signed in to change notification settings - Fork 4
tuf: Support importing root keys from a fioctl tarball #199
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,15 +4,171 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package main | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "archive/tar" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "compress/gzip" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "io" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "path/filepath" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/foundriesio/update-server/storage" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "github.com/foundriesio/update-server/storage/tuf" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type TufInitCmd struct{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // rootJsonSuffix is the suffix ota-tuf uses for root metadata files (e.g. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // "1.root.json"). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const rootJsonSuffix = ".root.json" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // tufKeyFileSuffix is the suffix fioctl/garage-sign use for private key files | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // stored in the offline keys tarball. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const tufKeyFileSuffix = ".sec" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type TufInitCmd struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ImportKeys string `arg:"--import-keys" help:"Path to a fioctl offline keys tarball with the root key(s) to sign the rotation; enables TUF root import (requires auth-init to have been run first)"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ImportRoots string `arg:"--import-roots" help:"Path to a gzipped tarball containing all root.json files to import"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (c TufInitCmd) Run(args CommonArgs) error { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fs, err := storage.NewFs(args.DataDir) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fs.Tuf.InitTuf() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if c.ImportKeys != "" || c.ImportRoots != "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if c.ImportKeys == "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("--import-keys is required to import TUF root metadata") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if c.ImportRoots == "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return fmt.Errorf("--import-roots is required to import TUF root metadata") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| roots, err := loadTufRootsArchive(c.ImportRoots) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| keys, err := loadTufKeysArchive(c.ImportKeys) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err := fs.Tuf.ImportTuf(roots, keys); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printRootKeyBackupNotice(fs) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err := fs.Tuf.InitTuf(); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| printRootKeyBackupNotice(fs) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // printRootKeyBackupNotice warns the operator that the newly created root key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // is irreplaceable and must be backed up along with the HMAC secret used to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // decrypt it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func printRootKeyBackupNotice(fs *storage.FsHandle) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rootKeyPath := filepath.Join(fs.Config.TufDir(), "keys", "root.key") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hmacPath := filepath.Join(fs.Config.AuthDir(), storage.HmacFile) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Println() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Println("TUF initialization completed successfully.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Println() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Println("IMPORTANT: A new root key was created at:") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Printf(" %s\n", rootKeyPath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Println() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Println("This key is encrypted at rest using the HMAC secret at:") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Printf(" %s\n", hmacPath) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Println() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Println("Make a backup copy of BOTH files and store them somewhere safe NOW.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Println("The root key is useless without the HMAC secret needed to decrypt it.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Println("If either file is lost it CANNOT be recovered, and you will permanently") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Println("lose the ability to rotate or manage your TUF root of trust.") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fmt.Println() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+72
to
+85
Member
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. Maybe, use a multi-line format, like this:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // loadTufKeysArchive opens a fioctl offline keys tarball and extracts the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // root key files it contains. Only private key files (those ending in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ".sec") are parsed; all other archive entries are ignored. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func loadTufKeysArchive(path string) ([]tuf.AtsKey, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f, err := os.Open(path) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("unable to open keys archive: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defer f.Close() // nolint:errcheck | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| gz, err := gzip.NewReader(f) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("unable to open keys archive (expected a gzipped tarball): %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defer gz.Close() // nolint:errcheck | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var keys []tuf.AtsKey | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tr := tar.NewReader(gz) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hdr, err := tr.Next() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err == io.EOF { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("unable to read keys archive: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if hdr.Typeflag == tar.TypeDir || !strings.HasSuffix(hdr.Name, tufKeyFileSuffix) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if data, err := io.ReadAll(tr); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("unable to read %s from keys archive: %w", hdr.Name, err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var key tuf.AtsKey | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err := json.Unmarshal(data, &key); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("unable to parse key file %s: %w", hdr.Name, err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if key.KeyType != "" && key.KeyValue.Private != "" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| keys = append(keys, key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(keys) == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("keys archive does not contain any valid private key files (expected .sec files with private key material)") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return keys, nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // loadTufRootsArchive reads a gzipped tar archive and returns the raw bytes of | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // every root.json file it contains. Only files ending in ".root.json" are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // extracted; all other archive entries are ignored. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func loadTufRootsArchive(path string) ([][]byte, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| f, err := os.Open(path) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("unable to open roots archive: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defer f.Close() // nolint:errcheck | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| gz, err := gzip.NewReader(f) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("unable to open roots archive (expected a gzipped tarball): %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| defer gz.Close() // nolint:errcheck | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var roots [][]byte | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tr := tar.NewReader(gz) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| hdr, err := tr.Next() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if err == io.EOF { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("unable to read roots archive: %w", err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if hdr.Typeflag == tar.TypeDir || !strings.HasSuffix(hdr.Name, rootJsonSuffix) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if data, err := io.ReadAll(tr); err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("unable to read %s from roots archive: %w", hdr.Name, err) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| roots = append(roots, data) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if len(roots) == 0 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return nil, fmt.Errorf("roots archive does not contain any root.json files (expected files ending in %q)", rootJsonSuffix) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return roots, 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
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 |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| // Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. | ||
| // SPDX-License-Identifier: BSD-3-Clause-Clear | ||
|
|
||
| package storage | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "slices" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/foundriesio/update-server/clock" | ||
| "github.com/foundriesio/update-server/storage/tuf" | ||
| ) | ||
|
|
||
| // importedRoot holds the imported root metadata along with its raw bytes. The | ||
| // raw bytes are stored verbatim so the original signatures remain verifiable. | ||
| type importedRoot struct { | ||
| raw []byte | ||
| root tuf.AtsTufRoot | ||
| } | ||
|
|
||
| // ImportTuf initializes TUF for this server by migrating from an existing | ||
| // fioctl/ota-tuf setup. | ||
| // | ||
| // rootJSONs are the raw bytes of every known root.json version. candidateKeys | ||
| // are the private keys extracted from a fioctl offline keys tarball, | ||
| // including the offline private key(s) for the root role. Every imported | ||
| // root.json is stored verbatim, fresh online keys are generated for every role, | ||
| // and a new root.json (version = highest imported version + 1) is created and | ||
| // signed by both the imported (old) root key(s) and the newly generated root | ||
| // key so that clients can verify the chain of trust from the previously | ||
| // trusted root. | ||
| // | ||
| // It fails if TUF data already exists. | ||
| func (h TufFsHandle) ImportTuf(rootJSONs [][]byte, candidateKeys []tuf.AtsKey) error { | ||
| if h.isInitialized() { | ||
| return ErrTufAlreadyInitialized | ||
| } | ||
|
|
||
| if len(rootJSONs) == 0 { | ||
| return fmt.Errorf("no root metadata was provided to import") | ||
| } | ||
| roots := make([]importedRoot, 0, len(rootJSONs)) | ||
| for _, raw := range rootJSONs { | ||
| var root tuf.AtsTufRoot | ||
| if err := json.Unmarshal(raw, &root); err != nil { | ||
| return fmt.Errorf("unable to parse root metadata: %w", err) | ||
| } | ||
| if len(root.Signed.Roles) == 0 { | ||
| return fmt.Errorf("provided metadata does not look like a TUF root.json") | ||
| } | ||
| roots = append(roots, importedRoot{raw: raw, root: root}) | ||
| } | ||
|
|
||
| return h.importTuf(roots, candidateKeys) | ||
| } | ||
|
|
||
| // importTuf performs the actual import once the root metadata and keys have | ||
| // been parsed. | ||
| func (h TufFsHandle) importTuf(roots []importedRoot, candidateKeys []tuf.AtsKey) error { | ||
| hmacSecret, err := h.auth.GetHmacSecret() | ||
| if err != nil { | ||
| return fmt.Errorf("unable to read HMAC secret (run auth-init first): %w", err) | ||
| } else if len(hmacSecret) == 0 { | ||
| return fmt.Errorf("HMAC secret is empty; run auth-init first") | ||
| } | ||
|
|
||
| // The highest-version imported root is the trust anchor to chain from. | ||
| slices.SortFunc(roots, func(a, b importedRoot) int { | ||
| return a.root.Signed.Version - b.root.Signed.Version | ||
| }) | ||
| base := roots[len(roots)-1] | ||
|
vkhoroz marked this conversation as resolved.
|
||
|
|
||
| // ensure we have all the root.json versions | ||
| for idx, root := range roots { | ||
| if idx+1 != root.root.Signed.Version { | ||
| return fmt.Errorf("missing %d.root.json version", idx+1) | ||
| } | ||
| } | ||
|
|
||
| rootThresh := base.root.Signed.Roles[tuf.RoleRoot].Threshold | ||
| if rootThresh > 1 { | ||
| return fmt.Errorf("unable to import TUF root. The signature threshold for the root role must be 1. Current value is: %d", rootThresh) | ||
| } | ||
|
|
||
| oldRootRole, ok := base.root.Signed.Roles[tuf.RoleRoot] | ||
| if !ok { | ||
| return fmt.Errorf("imported root.json (version %d) has no root role", base.root.Signed.Version) | ||
| } | ||
|
|
||
| oldSigner, err := findFirstRootSigner(oldRootRole.KeyIDs, candidateKeys) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := os.MkdirAll(h.keysDir(), defaultDirAccess); err != nil { | ||
| return fmt.Errorf("unable to create TUF keys directory: %w", err) | ||
| } | ||
|
|
||
| // Generate fresh online keys for every role owned by this server. | ||
| signers := make(map[tuf.RoleName]*tuf.Signer, len(tufRoles)) | ||
| for _, role := range tufRoles { | ||
| signer, err := tuf.NewSigner() | ||
| if err != nil { | ||
| return fmt.Errorf("unable to generate %s key: %w", role, err) | ||
| } | ||
| if err := h.writeKey(hmacSecret, role, signer); err != nil { | ||
| return err | ||
| } | ||
| signers[role] = signer | ||
| } | ||
|
|
||
| keys := make(map[string]tuf.AtsKey, len(signers)) | ||
| roles := make(map[tuf.RoleName]tuf.RootRole, len(signers)) | ||
| for _, role := range tufRoles { | ||
| signer := signers[role] | ||
| keys[signer.Id] = signer.PublicAtsKey() | ||
| roles[role] = tuf.RootRole{KeyIDs: []string{signer.Id}, Threshold: 1} | ||
| } | ||
| newRoot := tuf.AtsTufRoot{ | ||
| Signed: tuf.RootMeta{ | ||
| SignedCommon: tuf.SignedCommon{ | ||
| Type: tuf.RoleRoot.TufType(), | ||
| Expires: clock.Now().UTC().Add(h.RootExpiration).Truncate(time.Second), | ||
| Version: base.root.Signed.Version + 1, | ||
| }, | ||
| ConsistentSnapshot: false, | ||
| Keys: keys, | ||
| Roles: roles, | ||
| }, | ||
| } | ||
|
|
||
| // The new root must be signed by the new root key and one matching old | ||
| // root key so it chains from the previously trusted root. | ||
| newSigned, err := signers[tuf.RoleRoot].Sign(newRoot.Signed) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to sign new root metadata: %w", err) | ||
| } | ||
| oldSigned, err := oldSigner.Sign(newRoot.Signed) | ||
| if err != nil { | ||
| return fmt.Errorf("unable to sign new root metadata with imported key %s: %w", oldSigner.Id, err) | ||
| } | ||
| newRoot.Signatures = []tuf.Signature{newSigned, oldSigned} | ||
|
|
||
| // Persist every imported root verbatim, then the newly generated root. | ||
| if err := h.mkdirs(defaultDirAccess, true); err != nil { | ||
| return fmt.Errorf("unable to create TUF directory: %w", err) | ||
| } | ||
| for _, imp := range roots { | ||
| name := strconv.Itoa(imp.root.Signed.Version) + rootJsonSuffix | ||
| if err := h.writeFile(name, string(imp.raw), defaultFileAccess); err != nil { | ||
| return fmt.Errorf("unable to write %s: %w", name, err) | ||
| } | ||
| } | ||
| return h.writeRoot(newRoot) | ||
| } | ||
|
|
||
| func findFirstRootSigner(keyIDs []string, candidateKeys []tuf.AtsKey) (*tuf.ImportSigner, error) { | ||
| for _, key := range candidateKeys { | ||
| signer, err := tuf.ImportSignerFromAtsKey(key) | ||
| if err != nil { | ||
| // Not a usable signing key (e.g. public-only or unsupported type). | ||
| continue | ||
| } | ||
| if slices.Contains(keyIDs, signer.Id) { | ||
| return signer, nil | ||
| } | ||
| } | ||
| return nil, fmt.Errorf("unable to find root key signer for key IDs: %v", keyIDs) | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.