-
Notifications
You must be signed in to change notification settings - Fork 33
tools/sec: Add certificates to key-list, add cert-export #181
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
06ebcac
a5d8840
c1d6ed7
05725ed
630250c
1ce6189
29cb0c3
bf7b3d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -26,7 +26,7 @@ func (t *ToolKeychain) configure(cmd *cobra.Command) { | |||||||||||||||||||||||
| cmd.AddCommand(&cobra.Command{ | ||||||||||||||||||||||||
| GroupID: "keychain", | ||||||||||||||||||||||||
| Use: "key-list KEYCHAIN-URI", | ||||||||||||||||||||||||
| Short: "List keys in a keychain", | ||||||||||||||||||||||||
| Short: "List keys and certs in a keychain", | ||||||||||||||||||||||||
| Run: t.List, | ||||||||||||||||||||||||
| Args: cobra.ExactArgs(1), | ||||||||||||||||||||||||
| Example: ` ndnd sec key-list dir:///safe/keys`, | ||||||||||||||||||||||||
|
|
@@ -74,9 +74,18 @@ and the default key of the identity will be exported.`, | |||||||||||||||||||||||
| Example: ` ndnd sec key-export dir:///safe/keys /alice`, | ||||||||||||||||||||||||
| Run: t.Export, | ||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| cmd.AddCommand(&cobra.Command{ | ||||||||||||||||||||||||
| GroupID: "keychain", | ||||||||||||||||||||||||
| Use: "cert-export KEYCHAIN-URI CERT-NAME", | ||||||||||||||||||||||||
| Short: "Export a certificate from a keychain", | ||||||||||||||||||||||||
| Args: cobra.ExactArgs(2), | ||||||||||||||||||||||||
| Example: ` ndnd sec cert-export dir:///safe/keys /alice/KEY/~%E8t%A5%A3V%88%81/NA/v=0`, | ||||||||||||||||||||||||
| Run: t.ExportCert, | ||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // (AI GENERATED DESCRIPTION): Lists all identities and their keys in the keychain at the given path, printing each identity name followed by the names of its keys. | ||||||||||||||||||||||||
| // Lists all identities, their keys, and the associated certs in the keychain at the given URI, printing each identity name followed by the names of its keys, then its names of its certs | ||||||||||||||||||||||||
| func (*ToolKeychain) List(_ *cobra.Command, args []string) { | ||||||||||||||||||||||||
| kc, err := keychain.NewKeyChain(args[0], storage.NewMemoryStore()) | ||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||
|
|
@@ -89,11 +98,14 @@ func (*ToolKeychain) List(_ *cobra.Command, args []string) { | |||||||||||||||||||||||
| fmt.Printf("%s\n", id.Name()) | ||||||||||||||||||||||||
| for _, key := range id.Keys() { | ||||||||||||||||||||||||
| fmt.Printf("==> %s\n", key.KeyName()) | ||||||||||||||||||||||||
| for _, cert := range key.UniqueCerts() { | ||||||||||||||||||||||||
| fmt.Printf(" ==> %s\n", cert) | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
zjkmxy marked this conversation as resolved.
|
||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // (AI GENERATED DESCRIPTION): Imports keychain entries from standard input into the keychain named by the first argument, storing them in a memory-based keychain. | ||||||||||||||||||||||||
| // Imports keychain entries from standard input into the keychain named by the first argument | ||||||||||||||||||||||||
| func (*ToolKeychain) Import(_ *cobra.Command, args []string) { | ||||||||||||||||||||||||
| kc, err := keychain.NewKeyChain(args[0], storage.NewMemoryStore()) | ||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||
|
|
@@ -117,7 +129,7 @@ func (*ToolKeychain) Import(_ *cobra.Command, args []string) { | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // (AI GENERATED DESCRIPTION): Exports a specified key (or an identity’s default key) from a keychain, PEM‑encodes its secret key, and writes it to standard output. | ||||||||||||||||||||||||
| // Exports a specified key (or an identity’s default key) from a keychain, PEM‑encodes its secret key, and writes it to standard output. | ||||||||||||||||||||||||
| func (*ToolKeychain) Export(_ *cobra.Command, args []string) { | ||||||||||||||||||||||||
| name, err := enc.NameFromStr(args[1]) | ||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||
|
|
@@ -205,6 +217,37 @@ func (*ToolKeychain) DeleteKey(_ *cobra.Command, args []string) { | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Exports a specified certificate from a keychain, PEM‑encodes it, and writes it to standard output. | ||||||||||||||||||||||||
| func (*ToolKeychain) ExportCert(_ *cobra.Command, args []string) { | ||||||||||||||||||||||||
| name, err := enc.NameFromStr(args[1]) | ||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||
| fmt.Fprintf(os.Stderr, "Invalid certificate name: %s\n", args[1]) | ||||||||||||||||||||||||
| os.Exit(1) | ||||||||||||||||||||||||
| return | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| kc, err := keychain.NewKeyChain(args[0], storage.NewMemoryStore()) | ||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||
| fmt.Fprintf(os.Stderr, "Failed to open keychain: %s\n", err) | ||||||||||||||||||||||||
| os.Exit(1) | ||||||||||||||||||||||||
| return | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| wire, err := kc.Store().Get(name.Prefix(-1), true) | ||||||||||||||||||||||||
| if err != nil || wire == nil { | ||||||||||||||||||||||||
| fmt.Fprintf(os.Stderr, "Certificate not found: %s\n", name) | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
| wire, err := kc.Store().Get(name.Prefix(-1), true) | |
| if err != nil || wire == nil { | |
| fmt.Fprintf(os.Stderr, "Certificate not found: %s\n", name) | |
| wire, err := kc.Store().Get(name, false) | |
| if err == nil && wire == nil { | |
| wire, err = kc.Store().Get(name, true) | |
| } | |
| if err != nil || wire == nil { | |
| fmt.Fprintf(os.Stderr, "Certificate not found: %s\n", name) | |
| os.Exit(1) | |
| return |
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.
Why you remove the last component of the certificate?
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.
UniqueCerts() always returns names with v=0 so it would fail if that was passed to it, I updated so that it works for all 3 cases (v=0, prefix, or specific cert).
Should I change key-list to get the full cert names instead of output of UniqueCerts() with v=0? I didn't before because no function currently exists in the Keychain or Store interface to make it possible to list every version of a unique certificate
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.
I already forgot about the keystore. @tianyuan129 What do you think?
Uh oh!
There was an error while loading. Please reload this page.