blob: 8c3293e57c0669637a3bdc2c0ec4ffa83544138e [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium 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
Tilak Sharmad6ade0e2014-08-20 16:28:32 -07005package security
6
7import (
8 "bytes"
Ankur021e38e2014-09-26 10:26:45 -07009 "crypto/ecdsa"
10 "crypto/elliptic"
11 "crypto/rand"
Tilak Sharmad6ade0e2014-08-20 16:28:32 -070012 "reflect"
13 "testing"
Mike Burrows7f7088d2015-03-25 13:05:00 -070014
15 "v.io/v23/verror"
Tilak Sharmad6ade0e2014-08-20 16:28:32 -070016)
17
Ankur021e38e2014-09-26 10:26:45 -070018func TestLoadSavePEMKey(t *testing.T) {
19 key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
20 if err != nil {
21 t.Fatalf("Failed ecdsa.GenerateKey: %v", err)
22 }
23
24 var buf bytes.Buffer
Ankur73e7a932014-10-24 15:57:03 -070025 if err := SavePEMKey(&buf, key, nil); err != nil {
Ankur021e38e2014-09-26 10:26:45 -070026 t.Fatalf("Failed to save ECDSA private key: %v", err)
27 }
28
Ankur73e7a932014-10-24 15:57:03 -070029 loadedKey, err := LoadPEMKey(&buf, nil)
Suharsh Sivakumar0f359042014-10-01 22:53:45 -070030 if !reflect.DeepEqual(loadedKey, key) {
31 t.Fatalf("Got key %v, but want %v", loadedKey, key)
32 }
33}
34
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070035func TestLoadSavePEMKeyWithPassphrase(t *testing.T) {
Suharsh Sivakumar0f359042014-10-01 22:53:45 -070036 pass := []byte("openSesame")
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070037 incorrect_pass := []byte("wrongPassphrase")
Suharsh Sivakumar0f359042014-10-01 22:53:45 -070038 key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
39 if err != nil {
40 t.Fatalf("Failed ecdsa.GenerateKey: %v", err)
41 }
42 var buf bytes.Buffer
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070043
44 // Test incorrect passphrase.
Ankur73e7a932014-10-24 15:57:03 -070045 if err := SavePEMKey(&buf, key, pass); err != nil {
Suharsh Sivakumar0f359042014-10-01 22:53:45 -070046 t.Fatalf("Failed to save ECDSA private key: %v", err)
47 }
Ankur73e7a932014-10-24 15:57:03 -070048 loadedKey, err := LoadPEMKey(&buf, incorrect_pass)
Suharsh Sivakumar6ceb4302014-10-23 15:33:17 -070049 if loadedKey != nil && err != nil {
50 t.Errorf("expected (nil, err != nil) received (%v,%v)", loadedKey, err)
Suharsh Sivakumar0f359042014-10-01 22:53:45 -070051 }
52
53 // Test correct password.
Ankur73e7a932014-10-24 15:57:03 -070054 if err := SavePEMKey(&buf, key, pass); err != nil {
Suharsh Sivakumar0f359042014-10-01 22:53:45 -070055 t.Fatalf("Failed to save ECDSA private key: %v", err)
56 }
Ankur73e7a932014-10-24 15:57:03 -070057 loadedKey, err = LoadPEMKey(&buf, pass)
Ankur021e38e2014-09-26 10:26:45 -070058 if !reflect.DeepEqual(loadedKey, key) {
59 t.Fatalf("Got key %v, but want %v", loadedKey, key)
60 }
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070061
62 // Test nil passphrase.
Ankur73e7a932014-10-24 15:57:03 -070063 if err := SavePEMKey(&buf, key, pass); err != nil {
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070064 t.Fatalf("Failed to save ECDSA private key: %v", err)
65 }
Mike Burrows7f7088d2015-03-25 13:05:00 -070066 if loadedKey, err = LoadPEMKey(&buf, nil); loadedKey != nil || verror.ErrorID(err) != ErrBadPassphrase.ID {
67 t.Fatalf("expected(nil, ErrBadPassphrase), instead got (%v, %v)", loadedKey, err)
Suharsh Sivakumaraca1c322014-10-21 11:27:32 -070068 }
Ankur021e38e2014-09-26 10:26:45 -070069}