blob: eb72f4356c28c309da0a0c20d58d19779e20fffb [file] [log] [blame]
Asim Shankarae8d4c52014-10-08 13:03:31 -07001package security
2
3import (
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -07004 "crypto/ecdsa"
5 "crypto/elliptic"
6 "crypto/rand"
Asim Shankarae8d4c52014-10-08 13:03:31 -07007 "io/ioutil"
8 "os"
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -07009 "path"
Asim Shankarae8d4c52014-10-08 13:03:31 -070010 "testing"
11)
12
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070013func TestLoadPersistentPrincipal(t *testing.T) {
14 // If the directory does not exist want os.IsNotExists.
15 _, err := LoadPersistentPrincipal("/tmp/fake/path/", nil)
16 if !os.IsNotExist(err) {
17 t.Errorf("invalid path should return does not exist error, instead got %v", err)
18 }
19 // If the key file exists and is unencrypted we should succeed.
20 dir := generatePEMFile(nil)
21 if _, err = LoadPersistentPrincipal(dir, nil); err != nil {
22 t.Errorf("unencrypted LoadPersistentPrincipal should have succeeded: %v", err)
23 }
24 os.RemoveAll(dir)
25
26 // If the private key file exists and is encrypted we should succeed with correct passphrase.
27 passphrase := []byte("passphrase")
28 incorrect_passphrase := []byte("incorrect_passphrase")
29 dir = generatePEMFile(passphrase)
30 if _, err = LoadPersistentPrincipal(dir, passphrase); err != nil {
31 t.Errorf("encrypted LoadPersistentPrincipal should have succeeded: %v", err)
32 }
33 // and fail with an incorrect passphrase.
34 if _, err = LoadPersistentPrincipal(dir, incorrect_passphrase); err == nil {
35 t.Errorf("encrypted LoadPersistentPrincipal with incorrect passphrase should fail")
36 }
Suharsh Sivakumar4684f4e2014-10-24 13:42:06 -070037 // and return PassphraseError if the passphrase is nil.
38 if _, err = LoadPersistentPrincipal(dir, nil); err != PassphraseErr {
39 t.Errorf("encrypted LoadPersistentPrincipal with nil passphrase should return PassphraseErr: %v", err)
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070040 }
41 os.RemoveAll(dir)
42}
43
Ankuree0aa812014-11-14 10:56:52 -080044// This Test checks that the all changes made to the principal's serialization
45// format stay backwards compatible.
46//
47// The 'testdata' directory used by this test was generated using the
48// principal tool as part of CL #6820.
49// $VEYRON_BIN/principal create testdata test
50func TestLoadPersistentPrincipalBackwardsCompatibility(t *testing.T) {
51 if _, err := LoadPersistentPrincipal("./testdata", nil); err != nil {
52 t.Fatal("LoadPersistentPrincipal is not backwards compatible: failed to read serialized principal data from CL #6820")
53 }
54}
55
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070056func TestCreatePersistentPrincipal(t *testing.T) {
57 tests := []struct {
58 Message, Passphrase []byte
59 }{
60 {[]byte("unencrypted"), nil},
61 {[]byte("encrypted"), []byte("passphrase")},
62 }
63 for _, test := range tests {
64 testCreatePersistentPrincipal(t, test.Message, test.Passphrase)
65 }
66}
67
68func testCreatePersistentPrincipal(t *testing.T, message, passphrase []byte) {
Asim Shankarae8d4c52014-10-08 13:03:31 -070069 // Persistence of the BlessingRoots and BlessingStore objects is
70 // tested in other files. Here just test the persistence of the key.
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070071 dir, err := ioutil.TempDir("", "TestCreatePersistentPrincipal")
Asim Shankarae8d4c52014-10-08 13:03:31 -070072 if err != nil {
73 t.Fatal(err)
74 }
75 defer os.RemoveAll(dir)
76
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070077 p, err := CreatePersistentPrincipal(dir, passphrase)
Asim Shankarae8d4c52014-10-08 13:03:31 -070078 if err != nil {
79 t.Fatal(err)
80 }
gauthamtb7bb39b2014-11-10 11:40:41 -080081 _, err = CreatePersistentPrincipal(dir, passphrase)
Ankur4704f5f2014-10-23 12:40:54 -070082 if err == nil {
83 t.Error("CreatePersistentPrincipal passed unexpectedly")
84 }
Ankur4704f5f2014-10-23 12:40:54 -070085
Asim Shankarae8d4c52014-10-08 13:03:31 -070086 sig, err := p.Sign(message)
87 if err != nil {
88 t.Fatal(err)
89 }
90
Ankur4704f5f2014-10-23 12:40:54 -070091 p2, err := LoadPersistentPrincipal(dir, passphrase)
Asim Shankarae8d4c52014-10-08 13:03:31 -070092 if err != nil {
Suharsh Sivakumar8a7fba42014-10-27 12:40:48 -070093 t.Fatalf("%s failed: %v", message, err)
Asim Shankarae8d4c52014-10-08 13:03:31 -070094 }
Ankur4704f5f2014-10-23 12:40:54 -070095 if !sig.Verify(p2.PublicKey(), message) {
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070096 t.Errorf("%s failed: p.PublicKey=%v, p2.PublicKey=%v", message, p.PublicKey(), p2.PublicKey())
Asim Shankarae8d4c52014-10-08 13:03:31 -070097 }
98}
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070099
100func generatePEMFile(passphrase []byte) (dir string) {
101 dir, err := ioutil.TempDir("", "TestLoadPersistentPrincipal")
102 if err != nil {
103 panic(err)
104 }
105 key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
106 if err != nil {
107 panic(err)
108 }
109 f, err := os.Create(path.Join(dir, privateKeyFile))
110 if err != nil {
111 panic(err)
112 }
113 defer f.Close()
Ankur73e7a932014-10-24 15:57:03 -0700114 if err = SavePEMKey(f, key, passphrase); err != nil {
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -0700115 panic(err)
116 }
117 return dir
118}