github.com/hyperledger/aries-framework-go@v0.3.2/pkg/doc/util/kmsdidkey/kmsdidkey_test.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  Copyright Avast Software. All Rights Reserved.
     4  
     5  SPDX-License-Identifier: Apache-2.0
     6  */
     7  
     8  package kmsdidkey
     9  
    10  import (
    11  	"encoding/json"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/require"
    15  
    16  	"github.com/hyperledger/aries-framework-go/pkg/crypto"
    17  	"github.com/hyperledger/aries-framework-go/pkg/kms"
    18  	"github.com/hyperledger/aries-framework-go/pkg/kms/localkms"
    19  	mockstorage "github.com/hyperledger/aries-framework-go/pkg/mock/storage"
    20  	"github.com/hyperledger/aries-framework-go/pkg/secretlock"
    21  	"github.com/hyperledger/aries-framework-go/pkg/secretlock/noop"
    22  	"github.com/hyperledger/aries-framework-go/spi/storage"
    23  )
    24  
    25  func TestBuildDIDKeyByKMSKeyType(t *testing.T) {
    26  	sp := mockstorage.NewMockStoreProvider()
    27  	k := newKMS(t, sp)
    28  
    29  	_, ed25519Key, err := k.CreateAndExportPubKeyBytes(kms.ED25519Type)
    30  	require.NoError(t, err)
    31  
    32  	_, bbsKey, err := k.CreateAndExportPubKeyBytes(kms.BLS12381G2Type)
    33  	require.NoError(t, err)
    34  
    35  	_, p256IEEEKey, err := k.CreateAndExportPubKeyBytes(kms.ECDSAP256TypeIEEEP1363)
    36  	require.NoError(t, err)
    37  
    38  	_, p256DERKey, err := k.CreateAndExportPubKeyBytes(kms.ECDSAP256TypeDER)
    39  	require.NoError(t, err)
    40  
    41  	_, p384IEEEKey, err := k.CreateAndExportPubKeyBytes(kms.ECDSAP384TypeIEEEP1363)
    42  	require.NoError(t, err)
    43  
    44  	_, p384DERKey, err := k.CreateAndExportPubKeyBytes(kms.ECDSAP384TypeDER)
    45  	require.NoError(t, err)
    46  
    47  	_, p521IEEEKey, err := k.CreateAndExportPubKeyBytes(kms.ECDSAP521TypeIEEEP1363)
    48  	require.NoError(t, err)
    49  
    50  	_, p521DERKey, err := k.CreateAndExportPubKeyBytes(kms.ECDSAP521TypeDER)
    51  	require.NoError(t, err)
    52  
    53  	_, x25519Key, err := k.CreateAndExportPubKeyBytes(kms.X25519ECDHKWType)
    54  	require.NoError(t, err)
    55  
    56  	_, p256KWKey, err := k.CreateAndExportPubKeyBytes(kms.NISTP256ECDHKW)
    57  	require.NoError(t, err)
    58  
    59  	_, p384KWKey, err := k.CreateAndExportPubKeyBytes(kms.NISTP384ECDHKWType)
    60  	require.NoError(t, err)
    61  
    62  	badP384KWKey := &crypto.PublicKey{}
    63  	err = json.Unmarshal(p384KWKey, badP384KWKey)
    64  	require.NoError(t, err)
    65  
    66  	badP384KWKey.Curve = "bad_curve"
    67  
    68  	badP384KWKeyBytes, err := json.Marshal(badP384KWKey)
    69  	require.NoError(t, err)
    70  
    71  	_, p521KWKey, err := k.CreateAndExportPubKeyBytes(kms.NISTP521ECDHKWType)
    72  	require.NoError(t, err)
    73  
    74  	tests := []struct {
    75  		name     string
    76  		keyBytes []byte
    77  		keyType  kms.KeyType
    78  	}{
    79  		{
    80  			name:     "test ED25519 key",
    81  			keyBytes: ed25519Key,
    82  			keyType:  kms.ED25519Type,
    83  		},
    84  		{
    85  			name:     "test BLS12381G2 key",
    86  			keyBytes: bbsKey,
    87  			keyType:  kms.BLS12381G2Type,
    88  		},
    89  		{
    90  			name:     "test ECDSAP256TypeIEEEP1363 key",
    91  			keyBytes: p256IEEEKey,
    92  			keyType:  kms.ECDSAP256TypeIEEEP1363,
    93  		},
    94  		{
    95  			name:     "test ECDSAP256TypeDER key",
    96  			keyBytes: p256DERKey,
    97  			keyType:  kms.ECDSAP256TypeDER,
    98  		},
    99  		{
   100  			name:     "test ECDSAP384TypeIEEEP1363 key",
   101  			keyBytes: p384IEEEKey,
   102  			keyType:  kms.ECDSAP384TypeIEEEP1363,
   103  		},
   104  		{
   105  			name:     "test ECDSAP384TypeDER key",
   106  			keyBytes: p384DERKey,
   107  			keyType:  kms.ECDSAP384TypeDER,
   108  		},
   109  		{
   110  			name:     "test ECDSAP521TypeIEEEP1363 key",
   111  			keyBytes: p521IEEEKey,
   112  			keyType:  kms.ECDSAP521TypeIEEEP1363,
   113  		},
   114  		{
   115  			name:     "test ECDSAP521TypeDER key",
   116  			keyBytes: p521DERKey,
   117  			keyType:  kms.ECDSAP521TypeDER,
   118  		},
   119  		{
   120  			name:     "test X25519ECDHKWType key",
   121  			keyBytes: x25519Key,
   122  			keyType:  kms.X25519ECDHKWType,
   123  		},
   124  		{
   125  			name:     "test NISTP256ECDHKW key",
   126  			keyBytes: p256KWKey,
   127  			keyType:  kms.NISTP256ECDHKW,
   128  		},
   129  		{
   130  			name:     "test NISTP384ECDHKW key",
   131  			keyBytes: p384KWKey,
   132  			keyType:  kms.NISTP384ECDHKW,
   133  		},
   134  		{
   135  			name:     "test NISTP521ECDHKW key",
   136  			keyBytes: p521KWKey,
   137  			keyType:  kms.NISTP521ECDHKW,
   138  		},
   139  		{
   140  			name:     "test invalid key",
   141  			keyBytes: []byte{},
   142  			keyType:  "undefined",
   143  		},
   144  		{
   145  			name:     "test invalid X25519 key",
   146  			keyBytes: []byte("wrongKey."),
   147  			keyType:  kms.X25519ECDHKWType,
   148  		},
   149  		{
   150  			name:     "test invalid NISTP256ECDHKW key",
   151  			keyBytes: []byte("wrongKey."),
   152  			keyType:  kms.NISTP256ECDHKW,
   153  		},
   154  		{
   155  			name:     "test invalid NISTP384ECDHKW marshalled Key",
   156  			keyBytes: badP384KWKeyBytes,
   157  			keyType:  kms.NISTP384ECDHKWType,
   158  		},
   159  	}
   160  
   161  	for _, tt := range tests {
   162  		tc := tt
   163  		t.Run(tc.name, func(t *testing.T) {
   164  			didKey, err := BuildDIDKeyByKeyType(tc.keyBytes, tc.keyType)
   165  			if tc.name == "test invalid key" {
   166  				require.EqualError(t, err, "keyType 'undefined' does not have a multi-base codec")
   167  
   168  				return
   169  			}
   170  
   171  			if tc.name == "test invalid X25519 key" {
   172  				require.EqualError(t, err, "buildDIDkeyByKMSKeyType failed to unmarshal key type X25519ECDHKW:"+
   173  					" invalid character 'w' looking for beginning of value")
   174  
   175  				return
   176  			}
   177  
   178  			if tc.name == "test invalid NISTP256ECDHKW key" {
   179  				require.EqualError(t, err, "buildDIDkeyByKMSKeyType failed to unmarshal key type NISTP256ECDHKW:"+
   180  					" invalid character 'w' looking for beginning of value")
   181  
   182  				return
   183  			}
   184  
   185  			if tc.name == "test invalid NISTP384ECDHKW marshalled Key" {
   186  				require.EqualError(t, err, "buildDIDkeyByKMSKeyType failed to unmarshal key type NISTP384ECDHKW:"+
   187  					" invalid curve 'bad_curve'")
   188  
   189  				return
   190  			}
   191  
   192  			require.NoError(t, err)
   193  			require.Contains(t, didKey, "did:key:z")
   194  		})
   195  	}
   196  }
   197  
   198  type kmsProvider struct {
   199  	store             kms.Store
   200  	secretLockService secretlock.Service
   201  }
   202  
   203  func (k *kmsProvider) StorageProvider() kms.Store {
   204  	return k.store
   205  }
   206  
   207  func (k *kmsProvider) SecretLock() secretlock.Service {
   208  	return k.secretLockService
   209  }
   210  
   211  func newKMS(t *testing.T, store storage.Provider) kms.KeyManager {
   212  	t.Helper()
   213  
   214  	kmsStore, err := kms.NewAriesProviderWrapper(store)
   215  	require.NoError(t, err)
   216  
   217  	kmsProv := &kmsProvider{
   218  		store:             kmsStore,
   219  		secretLockService: &noop.NoLock{},
   220  	}
   221  
   222  	customKMS, err := localkms.New("local-lock://primary/test/", kmsProv)
   223  	require.NoError(t, err)
   224  
   225  	return customKMS
   226  }
   227  
   228  func TestEncryptionPubKeyFromDIDKey(t *testing.T) {
   229  	didKeyED25519 := "did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH"
   230  	didKeyX25519 := "did:key:z6LSeu9HkTHSfLLeUs2nnzUSNedgDUevfNQgQjQC23ZCit6F"
   231  	didKeyP256 := "did:key:zDnaerDaTF5BXEavCrfRZEk316dpbLsfPDZ3WJ5hRTPFU2169"
   232  	didKeyP384 := "did:key:z82Lm1MpAkeJcix9K8TMiLd5NMAhnwkjjCBeWHXyu3U4oT2MVJJKXkcVBgjGhnLBn2Kaau9"
   233  	didKeyP521 := "did:key:z2J9gaYxrKVpdoG9A4gRnmpnRCcxU6agDtFVVBVdn1JedouoZN7SzcyREXXzWgt3gGiwpoHq7K68X4m32D8HgzG8wv3sY5j7"                                                                                                     //nolint:lll
   234  	didKeyP256Uncompressed := "did:key:zrurwcJZss4ruepVNu1H3xmSirvNbzgBk9qrCktB6kaewXnJAhYWwtP3bxACqBpzjZdN7TyHNzzGGSSH5qvZsSDir9z"                                                                                              //nolint:lll
   235  	didKeyP384Uncompressed := "did:key:zFwfeyrSyWdksRYykTGGtagWazFB5zS4CjQcxDMQSNmCTQB5QMqokx2VJz4vBB2hN1nUrYDTuYq3kd1BM5cUCfFD4awiNuzEBuoy6rZZTMCsZsdvWkDXY6832qcAnzE7YGw43KU"                                                  //nolint:lll
   236  	didKeyP521Uncmopressed := "did:key:zWGhj2NTyCiehTPioanYSuSrfB7RJKwZj6bBUDNojfGEA21nr5NcBsHme7hcVSbptpWKarJpTcw814J3X8gVU9gZmeKM27JpGA5wNMzt8JZwjDyf8EzCJg5ve5GR2Xfm7d9Djp73V7s35KPeKe7VHMzmL8aPw4XBniNej5sXapPFoBs5R8m195HK" //nolint:lll
   237  
   238  	tests := []struct {
   239  		name   string
   240  		didKey string
   241  	}{
   242  		{
   243  			name:   "test ED25519 key",
   244  			didKey: didKeyED25519,
   245  		},
   246  		{
   247  			name:   "test P-256 key",
   248  			didKey: didKeyP256,
   249  		},
   250  		{
   251  			name:   "test P-384 key",
   252  			didKey: didKeyP384,
   253  		},
   254  		{
   255  			name:   "test P-521 key",
   256  			didKey: didKeyP521,
   257  		},
   258  		{
   259  			name:   "test P-256 uncompressed key",
   260  			didKey: didKeyP256Uncompressed,
   261  		},
   262  		{
   263  			name:   "test P-384 uncompressed key",
   264  			didKey: didKeyP384Uncompressed,
   265  		},
   266  		{
   267  			name:   "test P-521 uncompressed key",
   268  			didKey: didKeyP521Uncmopressed,
   269  		},
   270  		{
   271  			name:   "test X25519 key",
   272  			didKey: didKeyX25519,
   273  		},
   274  		{
   275  			name:   "invalid did:key code",
   276  			didKey: "did:key:zabcd",
   277  		},
   278  		{
   279  			name:   "invalid did:key method",
   280  			didKey: "did:key:invalid",
   281  		},
   282  	}
   283  
   284  	for _, tt := range tests {
   285  		tc := tt
   286  		t.Run(tc.name, func(t *testing.T) {
   287  			pubKey, err := EncryptionPubKeyFromDIDKey(tc.didKey)
   288  			switch tc.name {
   289  			case "invalid did:key code":
   290  				require.EqualError(t, err, "encryptionPubKeyFromDIDKey: unsupported key multicodec code [0x64]")
   291  				require.Empty(t, pubKey)
   292  
   293  				return
   294  			case "invalid did:key method":
   295  				require.EqualError(t, err, "encryptionPubKeyFromDIDKey: extractRawKey: MethodIDFromDIDKey "+
   296  					"failure: not a valid did:key identifier (not a base58btc multicodec): did:key:invalid")
   297  				require.Empty(t, pubKey)
   298  
   299  				return
   300  			}
   301  
   302  			require.NoError(t, err)
   303  			require.NotEmpty(t, pubKey)
   304  		})
   305  	}
   306  }
   307  
   308  func TestGetBase58PubKeyFromDIDKey(t *testing.T) {
   309  	didKeyED25519 := "did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH"
   310  	pubKey := "B12NYF8RrR3h41TDCTJojY59usg3mbtbjnFs7Eud1Y6u"
   311  
   312  	tests := []struct {
   313  		name   string
   314  		didKey string
   315  	}{
   316  		{
   317  			name:   "test ED25519 key",
   318  			didKey: didKeyED25519,
   319  		},
   320  		{
   321  			name:   "invalid did:key code",
   322  			didKey: "did:key:zabcd",
   323  		},
   324  		{
   325  			name:   "invalid did:key method",
   326  			didKey: "did:key:invalid",
   327  		},
   328  	}
   329  
   330  	for _, tt := range tests {
   331  		tc := tt
   332  		t.Run(tc.name, func(t *testing.T) {
   333  			resultKey, err := GetBase58PubKeyFromDIDKey(tc.didKey)
   334  			switch tc.name {
   335  			case "invalid did:key code":
   336  				require.ErrorContains(t, err, "GetBase58PubKeyFromDIDKey: failed to parse public key bytes")
   337  				require.Empty(t, resultKey)
   338  
   339  				return
   340  			case "invalid did:key method":
   341  				require.ErrorContains(t, err,
   342  					"GetBase58PubKeyFromDIDKey: failed to parse public key bytes from did:key:invalid:")
   343  				require.Empty(t, resultKey)
   344  
   345  				return
   346  			}
   347  
   348  			require.NoError(t, err)
   349  			require.Equal(t, pubKey, resultKey)
   350  		})
   351  	}
   352  }