| 1 |
/* |
| 2 |
Copyright 2023 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package pkcs11 |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"crypto/cipher" |
| 22 |
"crypto/rand" |
| 23 |
"fmt" |
| 24 |
|
| 25 |
crypot11 "github.com/ThalesIgnite/crypto11" |
| 26 |
|
| 27 |
"k8s.io/kms/pkg/service" |
| 28 |
) |
| 29 |
|
| 30 |
const ( |
| 31 |
mockAnnotationKey = "version.encryption.remote.io" |
| 32 |
) |
| 33 |
|
| 34 |
var _ service.Service = &pkcs11RemoteService{} |
| 35 |
|
| 36 |
type pkcs11RemoteService struct { |
| 37 |
keyID string |
| 38 |
aead cipher.AEAD |
| 39 |
} |
| 40 |
|
| 41 |
// NewPKCS11RemoteService creates a new PKCS11 remote service with SoftHSMv2 configuration file and keyID |
| 42 |
func NewPKCS11RemoteService(configFilePath, keyID string) (service.Service, error) { |
| 43 |
ctx, err := crypot11.ConfigureFromFile(configFilePath) |
| 44 |
if err != nil { |
| 45 |
return nil, err |
| 46 |
} |
| 47 |
|
| 48 |
if len(keyID) == 0 { |
| 49 |
return nil, fmt.Errorf("invalid keyID") |
| 50 |
} |
| 51 |
|
| 52 |
remoteService := &pkcs11RemoteService{ |
| 53 |
keyID: keyID, |
| 54 |
} |
| 55 |
|
| 56 |
key, err := ctx.FindKey(nil, []byte(keyID)) |
| 57 |
if err != nil { |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
if key == nil { |
| 61 |
return nil, fmt.Errorf("key not found") |
| 62 |
} |
| 63 |
if remoteService.aead, err = key.NewGCM(); err != nil { |
| 64 |
return nil, err |
| 65 |
} |
| 66 |
|
| 67 |
return remoteService, nil |
| 68 |
} |
| 69 |
|
| 70 |
func (s *pkcs11RemoteService) Encrypt(ctx context.Context, uid string, plaintext []byte) (*service.EncryptResponse, error) { |
| 71 |
nonceSize := s.aead.NonceSize() |
| 72 |
result := make([]byte, nonceSize+s.aead.Overhead()+len(plaintext)) |
| 73 |
n, err := rand.Read(result[:nonceSize]) |
| 74 |
if err != nil { |
| 75 |
return nil, err |
| 76 |
} |
| 77 |
if n != nonceSize { |
| 78 |
return nil, fmt.Errorf("unable to read sufficient random bytes") |
| 79 |
} |
| 80 |
cipherText := s.aead.Seal(result[nonceSize:nonceSize], result[:nonceSize], plaintext, []byte(s.keyID)) |
| 81 |
|
| 82 |
return &service.EncryptResponse{ |
| 83 |
Ciphertext: result[:nonceSize+len(cipherText)], |
| 84 |
KeyID: s.keyID, |
| 85 |
Annotations: map[string][]byte{ |
| 86 |
mockAnnotationKey: []byte("1"), |
| 87 |
}, |
| 88 |
}, nil |
| 89 |
} |
| 90 |
|
| 91 |
func (s *pkcs11RemoteService) Decrypt(ctx context.Context, uid string, req *service.DecryptRequest) ([]byte, error) { |
| 92 |
if len(req.Annotations) != 1 { |
| 93 |
return nil, fmt.Errorf("invalid annotations") |
| 94 |
} |
| 95 |
if v, ok := req.Annotations[mockAnnotationKey]; !ok || string(v) != "1" { |
| 96 |
return nil, fmt.Errorf("invalid version in annotations") |
| 97 |
} |
| 98 |
|
| 99 |
if req.KeyID != s.keyID { |
| 100 |
return nil, fmt.Errorf("invalid keyID") |
| 101 |
} |
| 102 |
|
| 103 |
nonceSize := s.aead.NonceSize() |
| 104 |
data := req.Ciphertext |
| 105 |
if len(data) < nonceSize { |
| 106 |
return nil, fmt.Errorf("the stored data was shorter than the required size") |
| 107 |
} |
| 108 |
|
| 109 |
return s.aead.Open(nil, data[:nonceSize], data[nonceSize:], []byte(s.keyID)) |
| 110 |
} |
| 111 |
|
| 112 |
func (s *pkcs11RemoteService) Status(ctx context.Context) (*service.StatusResponse, error) { |
| 113 |
return &service.StatusResponse{ |
| 114 |
Version: "v2", |
| 115 |
Healthz: "ok", |
| 116 |
KeyID: s.keyID, |
| 117 |
}, nil |
| 118 |
} |