Explorar o código

Enforce the use of id and label when generating objects.

Mariano Cano %!s(int64=5) %!d(string=hai) anos
pai
achega
f425a81d36
Modificáronse 2 ficheiros con 16 adicións e 1 borrados
  1. 7 0
      kms/pkcs11/pkcs11.go
  2. 9 1
      kms/pkcs11/pkcs11_test.go

+ 7 - 0
kms/pkcs11/pkcs11.go

@@ -262,6 +262,7 @@ func generateKey(ctx P11, req *apiv1.CreateKeyRequest) (crypto11.Signer, error)
 	if err != nil {
 		return nil, err
 	}
+
 	signer, err := ctx.FindKeyPair(id, object)
 	if err != nil {
 		return nil, err
@@ -272,6 +273,12 @@ func generateKey(ctx P11, req *apiv1.CreateKeyRequest) (crypto11.Signer, error)
 		}
 	}
 
+	// Enforce the use of both id and labels. This is not strictly necessary in
+	// PKCS #11, but it's a good practice.
+	if len(id) == 0 || len(object) == 0 {
+		return nil, errors.Errorf("key with uri %s is not valid, id and object are required", req.Name)
+	}
+
 	bits := req.Bits
 	if bits == 0 {
 		bits = DefaultRSASize

+ 9 - 1
kms/pkcs11/pkcs11_test.go

@@ -198,7 +198,6 @@ func TestPKCS11_CreateKey(t *testing.T) {
 		want    *apiv1.CreateKeyResponse
 		wantErr bool
 	}{
-		// SoftHSM2
 		{"default", args{&apiv1.CreateKeyRequest{
 			Name: testObject,
 		}}, &apiv1.CreateKeyResponse{
@@ -323,6 +322,15 @@ func TestPKCS11_CreateKey(t *testing.T) {
 		{"fail name", args{&apiv1.CreateKeyRequest{
 			Name: "",
 		}}, nil, true},
+		{"fail no id", args{&apiv1.CreateKeyRequest{
+			Name: "pkcs11:object=create-key",
+		}}, nil, true},
+		{"fail no object", args{&apiv1.CreateKeyRequest{
+			Name: "pkcs11:id=9999",
+		}}, nil, true},
+		{"fail schema", args{&apiv1.CreateKeyRequest{
+			Name: "pkcs12:id=9999;object=create-key",
+		}}, nil, true},
 		{"fail bits", args{&apiv1.CreateKeyRequest{
 			Name:               "pkcs11:id=9999;object=create-key",
 			Bits:               -1,