Esempio n. 1
0
func TestErrors(t *testing.T) {
	// Encrypted data is too small.
	_, err := keymgr.FromEncrypted([]byte{0x00}, []byte{0x00})
	if err == nil {
		t.Error("didn't get error on small ciphertext")
	}

	// Decryption failure.
	_, err = keymgr.FromEncrypted(bytes.Repeat([]byte{0x00}, 100), []byte("pass"))
	if err == nil {
		t.Error("decryption failure did not give error")
	}
}
Esempio n. 2
0
// openDatabases returns an instance of keymgr.Manager, and store.Store based on
// the configuration.
func openDatabases(cfg *Config) (*keymgr.Manager, *store.Store, *store.PKRequests, error) {

	// Read key file.
	keyFile, err := ioutil.ReadFile(cfg.keyfilePath)
	if err != nil {
		return nil, nil, nil, err
	}

	var kmgr *keymgr.Manager

	if cfg.NoPass { // If allowed, check for plaintext key file.
		// Attempt to load unencrypted key file.
		kmgr, err = keymgr.FromPlaintext(bytes.NewBuffer(keyFile))
		if err != nil {
			return nil, nil, nil, err
		}
	}

	if kmgr == nil {

		// Read key file passphrase from console.
		keyfilePass, err := promptKeyfilePassPhrase()
		if err != nil {
			return nil, nil, nil, err
		}

		// Create an instance of key manager.
		kmgr, err = keymgr.FromEncrypted(keyFile, keyfilePass)
		if err != nil {
			return nil, nil, nil, fmt.Errorf("Failed to create key manager: %v", err)
		}

		cfg.keyfilePass = keyfilePass
	}

	load, err := store.Open(cfg.storePath)
	if err != nil {
		return nil, nil, nil, err
	}

	var dstore *store.Store
	var pk *store.PKRequests

	if cfg.NoPass && !load.IsEncrypted() {
		dstore, pk, err = load.Construct(nil)
	} else {

		// Read store passphrase from console.
		storePass, err := promptStorePassPhrase()
		if err != nil {
			return nil, nil, nil, err
		}

		// Open store.
		dstore, pk, err = load.Construct(storePass)
		if err != nil {
			return nil, nil, nil, fmt.Errorf("Failed to open data store: %v", err)
		}
	}

	return kmgr, dstore, pk, nil
}
Esempio n. 3
0
func TestOperation(t *testing.T) {
	// Initialize a new key manager.
	seed := []byte("a secure psuedorandom seed (clearly not)")
	mgr, err := keymgr.New(seed)
	if err != nil {
		t.Fatal(err)
	}

	// Check that everything is correctly zeroed.
	if n := mgr.NumDeterministic(); n != 0 {
		t.Errorf("invalid latestIDIndex for no identity, expected %d got %d",
			0, n)
	}
	if n := mgr.NumImported(); n != 0 {
		t.Errorf("invalid numImported for no identity, expected %d got %d",
			0, n)
	}

	// Generate first identity and check if everything works as expected.
	gen1 := mgr.NewHDIdentity(1, "Seven Anvil")
	if gen1.IsChan != false {
		t.Error("generated identity not a channel")
	}
	if n := mgr.NumDeterministic(); n != 1 {
		t.Errorf("invalid latestIDIndex for no identity, expected %d got %d",
			1, n)
	}
	if n := mgr.NumImported(); n != 0 {
		t.Errorf("invalid numImported for no identity, expected %d got %d",
			0, n)
	}

	// Generate second identity and check if everything works as expected.
	gen2 := mgr.NewHDIdentity(1, "Intelligent Glue")
	if gen2.IsChan != false {
		t.Error("generated identity not a channel")
	}
	if n := mgr.NumDeterministic(); n != 2 {
		t.Errorf("invalid latestIDIndex for no identity, expected %d got %d",
			1, n)
	}
	if n := mgr.NumImported(); n != 0 {
		t.Errorf("invalid numImported for no identity, expected %d got %d",
			0, n)
	}

	// Import a channel and check if it's imported as expected.
	ids, _ := identity.NewDeterministic("privacy", 1, 1)
	privacyChan := &keymgr.PrivateID{
		Private: *ids[0],
		IsChan:  true,
		Name:    "Hyperluminous",
	}
	// Create an address (export fails without this).
	privacyChan.CreateAddress(4, 1)

	mgr.ImportIdentity(*privacyChan)
	if n := mgr.NumDeterministic(); n != 2 {
		t.Errorf("invalid latestIDIndex for no identity, expected %d got %d",
			1, n)
	}
	if n := mgr.NumImported(); n != 1 {
		t.Errorf("invalid numImported for no identity, expected %d got %d",
			1, n)
	}

	// Try to retrieve private identity from address.
	privacyAddr := privacyChan.Address()
	privacyRetrieved := mgr.LookupByAddress(privacyAddr)
	if privacyRetrieved == nil {
		t.Errorf("LookupByAddress returned nil address")
	}
	if !bytes.Equal(privacyRetrieved.Private.Address.Ripe[:], privacyChan.Private.Address.Ripe[:]) {
		t.Errorf("got different ripe, expected %v got %v",
			privacyChan.Private.Address.Ripe, privacyRetrieved.Private.Address.Ripe)
	}

	// Save and encrypt the private keys held by the key manager.
	pass := []byte("a very nice and secure password for my keyfile")
	encData, err := mgr.ExportEncrypted(pass)
	if err != nil {
		t.Fatal(err)
	}

	// Create a new key manager from the encrypted data and check if it's like
	// the original.
	mgr1, err := keymgr.FromEncrypted(encData, pass)
	if err != nil {
		t.Fatal(err)
	}
	if mgr.NumImported() != mgr1.NumImported() {
		t.Errorf("invalid number of imported keys, expected %d got %d",
			mgr.NumImported(), mgr1.NumImported())
	}
	if mgr.NumDeterministic() != mgr1.NumDeterministic() {
		t.Errorf("invalid number of deterministic keys, expected %d got %d",
			mgr.NumDeterministic(), mgr1.NumDeterministic())
	}

	// Remove an imported key and check if the operation is successful.
	/*mgr1.RemoveImported(privacyChan.Address())
	if n := mgr1.NumImported(); n != 0 {
		t.Errorf("invalid numImported for no identity, expected %d got %d",
			0, n)
	}
	err = mgr1.ForEach(func(id keymgr.PrivateID) error {
		if bytes.Equal(id.Tag(), privacyChan.Tag()) {
			return errors.New("should not happen")
		}
		return nil
	})
	if err != nil {
		t.Error("imported key not removed from database")
	}*/

	// Try to remove a key that doesn't exist in the database.
	// Should not crash the program. (function removed)
	//mgr1.RemoveImported(privacyChan.Address())

	// Try to retrieve non-existant private identity from address.
	privacyRetrieved = mgr1.LookupByAddress("BM-2cUfDTJXLeMxAVe7pWXBEneBjDuQ783VSq")
	if privacyRetrieved != nil {
		t.Errorf("expected nil id")
	}
}