-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Oskar Sharipov
committed
Jul 22, 2022
1 parent
8c9dfa4
commit f899175
Showing
6 changed files
with
150 additions
and
127 deletions.
There are no files selected for viewing
This file contains 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,77 @@ | ||
package minitrust | ||
|
||
import ( | ||
"errors" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/jedisct1/go-minisign" | ||
) | ||
|
||
const ( | ||
trustedDirPerm = 0700 | ||
trustedKeyPerm = 0600 | ||
) | ||
|
||
type Base struct { | ||
trustedDir string | ||
} | ||
|
||
func New(trustedDir string) Base { | ||
return Base{trustedDir: trustedDir} | ||
} | ||
|
||
func (b *Base) ensureTrustedDir() error { | ||
err := os.MkdirAll(b.trustedDir, trustedDirPerm) | ||
if err != nil && !os.IsExist(err) { | ||
return err | ||
} | ||
return nil | ||
} | ||
func (b *Base) getKeyPath(keyID [8]byte) string { | ||
return filepath.Join(b.trustedDir, EncodeID(keyID)+".pub") | ||
} | ||
|
||
// SearchTrustedPubKey returns public key and untrusted comment. | ||
func (b *Base) SearchTrustedPubKey(sigFile string) (minisign.PublicKey, string, error) { | ||
if err := b.ensureTrustedDir(); err != nil { | ||
return minisign.PublicKey{}, "", errors.New("minitrust: can't create trusted directory.") | ||
} | ||
|
||
signature, err := minisign.NewSignatureFromFile(sigFile) | ||
if err != nil { | ||
return minisign.PublicKey{}, "", err | ||
} | ||
|
||
key, comment, err := readKeyFile(b.getKeyPath(signature.KeyId)) | ||
if err != nil { | ||
return minisign.PublicKey{}, "", err | ||
} | ||
|
||
return key, comment, nil | ||
} | ||
|
||
func (b *Base) AddTrustedPubKey(rawPubKey, comment string) error { | ||
if err := b.ensureTrustedDir(); err != nil { | ||
return errors.New("minitrust: can't create trusted directory.") | ||
} | ||
if strings.Count(comment, "\n") != 0 { | ||
return errors.New("minitrust: comment must be one-lined.") | ||
} | ||
|
||
pk, err := minisign.NewPublicKey(rawPubKey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
content := strings.Join( | ||
[]string{ | ||
commentPrefix + comment, | ||
EncodePublicKey(pk), | ||
}, | ||
"\n", | ||
) | ||
return ioutil.WriteFile(b.getKeyPath(pk.KeyId), []byte(content), trustedKeyPerm) | ||
} |
This file contains 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,40 @@ | ||
package minitrust | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
const rawPubKey = "RWRWfuqg9DPmJzteqVmj5xSm7z1V0ZTNA66UGpF+5vdkUe8llEMWkC6n" | ||
const pubKeyID = "27E633F4A0EA7E56" | ||
|
||
func mkdirTempHome(t *testing.T) string { | ||
dir, err := os.MkdirTemp("", "test-minitrust-*") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return dir | ||
} | ||
|
||
func TestEnsureTrustedDir(t *testing.T) { | ||
dir := mkdirTempHome(t) | ||
defer os.RemoveAll(dir) | ||
|
||
for _, test := range []string{ | ||
dir, | ||
filepath.Join(dir, "whataboutthis"), | ||
filepath.Join(dir, "foo", "bar"), | ||
filepath.Join(dir, "exists"), | ||
filepath.Join(dir, "exists"), | ||
} { | ||
b := New(test) | ||
if err := b.ensureTrustedDir(); err != nil { | ||
t.Fatal(err) | ||
} | ||
_, err := os.Stat(test) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
} |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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,29 @@ | ||
package minitrust | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/jedisct1/go-minisign" | ||
) | ||
|
||
func TestEncodePublicKey(t *testing.T) { | ||
pk, err := minisign.NewPublicKey(rawPubKey) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
got := EncodePublicKey(pk) | ||
if got != rawPubKey { | ||
t.Fatalf("encoded string doesn't match: got %v, expected %v.", got, rawPubKey) | ||
} | ||
} | ||
|
||
func TestEncodeID(t *testing.T) { | ||
pk, err := minisign.NewPublicKey(rawPubKey) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
got := EncodeID(pk.KeyId) | ||
if got != pubKeyID { | ||
t.Fatalf("encoded ID doesn't match: got %v, expected %v.", got, pubKeyID) | ||
} | ||
} |