github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/protocol/didexchange/keys_test.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package didexchange
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/mediator"
    15  	"github.com/hyperledger/aries-framework-go/pkg/doc/did"
    16  	"github.com/hyperledger/aries-framework-go/pkg/kms"
    17  	"github.com/hyperledger/aries-framework-go/pkg/mock/didcomm/protocol"
    18  	mockroute "github.com/hyperledger/aries-framework-go/pkg/mock/didcomm/protocol/mediator"
    19  	mockkms "github.com/hyperledger/aries-framework-go/pkg/mock/kms"
    20  	mockstorage "github.com/hyperledger/aries-framework-go/pkg/mock/storage"
    21  )
    22  
    23  func TestCreateNewKeyAndVM(t *testing.T) {
    24  	k := newKMS(t, mockstorage.NewMockStoreProvider())
    25  
    26  	p, err := New(&protocol.MockProvider{
    27  		ServiceMap: map[string]interface{}{
    28  			mediator.Coordination: &mockroute.MockMediatorSvc{},
    29  		},
    30  		CustomKMS: k,
    31  	})
    32  	require.NoError(t, err)
    33  
    34  	t.Run("createNewKeyAndVM success", func(t *testing.T) {
    35  		didDoc := &did.Doc{}
    36  
    37  		p.ctx.keyType = kms.ED25519
    38  		p.ctx.keyAgreementType = kms.X25519ECDHKWType
    39  
    40  		err = p.ctx.createNewKeyAndVM(didDoc)
    41  		require.NoError(t, err)
    42  		require.Equal(t, ed25519VerificationKey2018, didDoc.VerificationMethod[0].Type)
    43  		require.Equal(t, x25519KeyAgreementKey2019, didDoc.KeyAgreement[0].VerificationMethod.Type)
    44  	})
    45  
    46  	t.Run("createNewKeyAndVM invalid keyType export signing key", func(t *testing.T) {
    47  		didDoc := &did.Doc{}
    48  
    49  		p.ctx.keyType = kms.HMACSHA256Tag256Type // invalid signing key
    50  		p.ctx.keyAgreementType = kms.X25519ECDHKWType
    51  
    52  		err = p.ctx.createNewKeyAndVM(didDoc)
    53  		require.EqualError(t, err, "createSigningVM: createAndExportPubKeyBytes: failed to export new public key bytes: "+
    54  			"exportPubKeyBytes: failed to export marshalled key: exportPubKeyBytes: failed to get public keyset "+
    55  			"handle: keyset.Handle: keyset.Handle: keyset contains a non-private key")
    56  		require.Empty(t, didDoc.VerificationMethod)
    57  		require.Empty(t, didDoc.KeyAgreement)
    58  	})
    59  }
    60  
    61  func TestCreateSigningVM(t *testing.T) {
    62  	k := newKMS(t, mockstorage.NewMockStoreProvider())
    63  
    64  	p, err := New(&protocol.MockProvider{
    65  		ServiceMap: map[string]interface{}{
    66  			mediator.Coordination: &mockroute.MockMediatorSvc{},
    67  		},
    68  		CustomKMS: k,
    69  	})
    70  	require.NoError(t, err)
    71  
    72  	t.Run("createSigningVM success", func(t *testing.T) {
    73  		p.ctx.keyType = kms.ED25519
    74  
    75  		svm, err := p.ctx.createSigningVM()
    76  		require.NoError(t, err)
    77  		require.NotEmpty(t, svm)
    78  	})
    79  
    80  	t.Run("createSigningVM with empty vmType", func(t *testing.T) {
    81  		p.ctx.keyType = ""
    82  
    83  		svm, err := p.ctx.createSigningVM()
    84  		require.EqualError(t, err, "createSigningVM: createAndExportPubKeyBytes: failed to create new key: "+
    85  			"failed to create new key, missing key type")
    86  		require.Empty(t, svm)
    87  	})
    88  
    89  	t.Run("createSigningVM with unsupported keyType", func(t *testing.T) {
    90  		p.ctx.keyType = kms.X25519ECDHKW
    91  
    92  		svm, err := p.ctx.createSigningVM()
    93  		require.EqualError(t, err, "createSigningVM: unsupported verification method: 'X25519KeyAgreementKey2019'")
    94  		require.Empty(t, svm)
    95  	})
    96  
    97  	t.Run("createSigningVM failed with invalid jsonWebkey2020 key value", func(t *testing.T) {
    98  		p.ctx.keyType = kms.ECDSAP256TypeDER
    99  		p.ctx.kms = &mockkms.KeyManager{
   100  			CrAndExportPubKeyValue: []byte("a"),
   101  		}
   102  
   103  		svm, err := p.ctx.createSigningVM()
   104  		require.EqualError(t, err, "createSigningVM: failed to convert public key to JWK for VM: asn1: syntax "+
   105  			"error: truncated tag or length")
   106  		require.Empty(t, svm)
   107  	})
   108  }
   109  
   110  func TestCreateEncryptionVM(t *testing.T) {
   111  	k := newKMS(t, mockstorage.NewMockStoreProvider())
   112  
   113  	p, err := New(&protocol.MockProvider{
   114  		ServiceMap: map[string]interface{}{
   115  			mediator.Coordination: &mockroute.MockMediatorSvc{},
   116  		},
   117  		CustomKMS: k,
   118  	})
   119  	require.NoError(t, err)
   120  
   121  	t.Run("createEncryptionVM success", func(t *testing.T) {
   122  		p.ctx.keyAgreementType = kms.X25519ECDHKW
   123  
   124  		evm, err := p.ctx.createEncryptionVM()
   125  		require.NoError(t, err)
   126  		require.NotEmpty(t, evm)
   127  
   128  		p.ctx.keyAgreementType = kms.NISTP521ECDHKWType
   129  
   130  		evm, err = p.ctx.createEncryptionVM()
   131  		require.NoError(t, err)
   132  		require.NotEmpty(t, evm)
   133  	})
   134  
   135  	t.Run("createEncryptionVM with empty keyAgreementType", func(t *testing.T) {
   136  		p.ctx.keyAgreementType = ""
   137  
   138  		evm, err := p.ctx.createEncryptionVM()
   139  		require.EqualError(t, err, "createEncryptionVM: createAndExportPubKeyBytes: failed to create new key: "+
   140  			"failed to create new key, missing key type")
   141  		require.Empty(t, evm)
   142  	})
   143  
   144  	t.Run("createEncryptionVM with unsupported keyType", func(t *testing.T) {
   145  		p.ctx.keyAgreementType = kms.ED25519Type
   146  
   147  		evm, err := p.ctx.createEncryptionVM()
   148  		require.EqualError(t, err, "unsupported verification method for KeyAgreement: 'Ed25519VerificationKey2018'")
   149  		require.Empty(t, evm)
   150  	})
   151  
   152  	t.Run("createSigningVM failed with invalid jsonWebkey2020 key value", func(t *testing.T) {
   153  		p.ctx.keyAgreementType = kms.NISTP384ECDHKWType
   154  		p.ctx.kms = &mockkms.KeyManager{
   155  			CrAndExportPubKeyValue: []byte("a"),
   156  		}
   157  
   158  		evm, err := p.ctx.createEncryptionVM()
   159  		require.EqualError(t, err, "createEncryptionVM: failed to unmarshal JWK for KeyAgreement: invalid "+
   160  			"character 'a' looking for beginning of value")
   161  		require.Empty(t, evm)
   162  	})
   163  }