Documentation
¶
Index ¶
- func ErrStatusCode(err error) int
- func NewSigner(api SignatureApi, jwk types.JsonWebKey) (crypto.Signer, error)
- func WithContextHeader(ctx context.Context, key, value string) context.Context
- type Category
- type Client
- type DataKeyApi
- type DataKeyProvider
- func (sk *DataKeyProvider) DecryptDataKey(ctx context.Context, key []byte) ([]byte, error)
- func (sk *DataKeyProvider) DecryptGCM(ctx context.Context, cipherKey, cipherText, nonce, aad []byte) ([]byte, error)
- func (sk *DataKeyProvider) EncryptGCM(ctx context.Context, keyName string, data, aad []byte) (cipherText, cipherKey, nonce []byte, err error)
- func (sk *DataKeyProvider) GenerateDataKey(ctx context.Context, name string, size int) (plain, encrypted []byte, err error)
- type EncryptionApi
- type ErrorCode
- type InternalHttpClient
- type KeyIter
- type KmsError
- type Major
- type Minor
- type RestAPIClient
- func (client *RestAPIClient) ActivateServiceKey(ctx context.Context, keyId uuid.UUID) error
- func (client *RestAPIClient) CreateImportServiceKey(ctx context.Context, format *types.KeyFormats, ...) (*types.GetServiceKeyResponse, error)
- func (client *RestAPIClient) DeactivateServiceKey(ctx context.Context, keyId uuid.UUID, reason types.RevocationReasons) error
- func (client *RestAPIClient) Decrypt(ctx context.Context, keyId uuid.UUID, keyCtx, data string) ([]byte, error)
- func (client *RestAPIClient) DecryptDataKey(ctx context.Context, keyId uuid.UUID, encryptedKey string) ([]byte, error)
- func (client *RestAPIClient) DeleteServiceKey(ctx context.Context, keyId uuid.UUID) error
- func (client *RestAPIClient) Encrypt(ctx context.Context, keyId uuid.UUID, keyCtx string, data []byte) (string, error)
- func (client *RestAPIClient) GenerateDataKey(ctx context.Context, keyId uuid.UUID, name string, size int32) (plain []byte, encrypted string, err error)
- func (client *RestAPIClient) GetInternalClient() *InternalHttpClient
- func (client *RestAPIClient) GetServiceKey(ctx context.Context, keyId uuid.UUID, format *types.KeyFormats) (*types.GetServiceKeyResponse, error)
- func (client *RestAPIClient) ListAllServiceKeys(pageSize *int32, state *types.KeyStates) KeyIter
- func (client *RestAPIClient) ListServiceKeys(ctx context.Context, continuationToken *string, maxKeys *int32, ...) (*types.ListServiceKeysResponse, error)
- func (client *RestAPIClient) Sign(ctx context.Context, keyId uuid.UUID, alg types.DigitalSignatureAlgorithms, ...) (string, error)
- func (client *RestAPIClient) UpdateServiceKey(ctx context.Context, keyId uuid.UUID, body types.PatchServiceKeyRequest) (*types.GetServiceKeyResponse, error)
- func (client *RestAPIClient) Verify(ctx context.Context, keyId uuid.UUID, alg types.DigitalSignatureAlgorithms, ...) (bool, error)
- func (client *RestAPIClient) WithCustomHeader(key, value string) *RestAPIClient
- type ServiceKeyApi
- type SignatureApi
Examples ¶
- DataKeyProvider (Helpers)
- DataKeyProvider.DecryptDataKey
- DataKeyProvider.GenerateDataKey
- NewRestAPIClientWithHttp
- RestAPIClient.CreateImportServiceKey (GenerateAES)
- RestAPIClient.CreateImportServiceKey (GenerateECDSA)
- RestAPIClient.CreateImportServiceKey (GenerateRSA)
- RestAPIClient.Sign
- RestAPIClient.Verify
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrStatusCode ¶
ErrStatusCode returns the status code of the HTTP response that caused this error, if any. If err is nil, or if it was not caused by an http response (for example if it was in fact caused by a connectivity issue), then it returns 0.
The returned status code will be 0, or a value >= 400.
func NewSigner ¶
func NewSigner(api SignatureApi, jwk types.JsonWebKey) (crypto.Signer, error)
NewSigner creates a new crypto.Signer using the given public JsonWebKey and its remote private key.
NewSigner cannot be used with symetric keys.
Types ¶
type Client ¶
type Client interface {
// RandomApi
DataKeyApi
SignatureApi
EncryptionApi
ServiceKeyApi
}
Client is the interface abstracting the KMS clients methods.
type DataKeyApi ¶
type DataKeyApi interface {
// GenerateDataKey creates a new data key of the given size, protected by the service key with the ID `keyId`.
// It returns the plain datakey, and the JWE encrypted version of it, which can be decrypted by calling the DecryptDataKey method.
GenerateDataKey(ctx context.Context, keyId uuid.UUID, name string, size int32) (plain []byte, encrypted string, err error)
// DecryptDataKey decrypts a JWE encrypted data key protected by the service key with the ID `keyId`.
DecryptDataKey(ctx context.Context, keyId uuid.UUID, encryptedKey string) ([]byte, error)
}
DataKeyApi is the client interface used to create and retrieve KMS data keys using a remote symmetric key.
type DataKeyProvider ¶
type DataKeyProvider struct {
// contains filtered or unexported fields
}
DataKeyProvider is a helper provider that wraps an API client and provides hlpers functions to repeatedly generate or decrypt datakeys protected by the same service key.
It also provide helper functions to directly encrypt or decrypt data with a datakey.
Example (Helpers) ¶
package main
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/ovh/okms-sdk-go"
)
func main() {
var kmsClient *okms.RestAPIClient // Initialize client
data := "Hello World !!!" // Data to encrypt
dkProvider := okms.NewDataKeyProvider(kmsClient, uuid.MustParse("2dab95dc-d7d3-482b-a07b-6b4dfae89d58"))
// Unless you want to use another algorithm than AES-GCM 256 bits, you can use the 2 following helper methods:
encryptedData, encryptedKey, nonce, err := dkProvider.EncryptGCM(context.Background(), "Example DK", []byte(data), []byte("Some additional data"))
if err != nil {
panic(err)
}
plainData, err := dkProvider.DecryptGCM(context.Background(), encryptedKey, encryptedData, nonce, []byte("Some additional data"))
if err != nil {
panic(err)
}
fmt.Println("Decrypted:", string(plainData))
}
Output:
func NewDataKeyProvider ¶
func NewDataKeyProvider(api DataKeyApi, keyId uuid.UUID) *DataKeyProvider
NewDataKeyProvider creates a new datakey provider for the given service key, using the given DataKeyApi api client.
func (*DataKeyProvider) DecryptDataKey ¶
DecryptDataKey decrypts an encrypted datakey like ones returned by DataKeyProvider.GenerateDataKey.
Example ¶
package main
import (
"context"
"crypto/aes"
"crypto/cipher"
"fmt"
"github.com/google/uuid"
"github.com/ovh/okms-sdk-go"
)
func main() {
var kmsClient *okms.RestAPIClient // Initialize client
var encryptedData []byte // Some encrypted data
var encryptedKey []byte // Encrypted datakey
var nonce []byte // Nonce used for data encryption
dkProvider := okms.NewDataKeyProvider(kmsClient, uuid.MustParse("2dab95dc-d7d3-482b-a07b-6b4dfae89d58"))
// Decrypt data key
plain, err := dkProvider.DecryptDataKey(context.Background(), encryptedKey)
if err != nil {
panic(err)
}
// Initialize AES GCM cipher with the decrypted key
aesCipher, err := aes.NewCipher(plain)
if err != nil {
panic(err)
}
gcm, err := cipher.NewGCM(aesCipher)
if err != nil {
panic(err)
}
// Decrypt text
plainData, err := gcm.Open(nil, nonce, encryptedData, []byte("Some additional data"))
if err != nil {
panic(err)
}
fmt.Println("Decrypted:", string(plainData))
}
Output:
func (*DataKeyProvider) DecryptGCM ¶
func (sk *DataKeyProvider) DecryptGCM(ctx context.Context, cipherKey, cipherText, nonce, aad []byte) ([]byte, error)
DecryptGCM is a helper function that decrypts the given encrypted datakey, than decrypts the given cipherText using the reulting plain key with the given nonce and aad, using AES-GCM.
It's mostly used to decrypt data encrypted using DataKeyProvider.EncryptGCM.
func (*DataKeyProvider) EncryptGCM ¶
func (sk *DataKeyProvider) EncryptGCM(ctx context.Context, keyName string, data, aad []byte) (cipherText, cipherKey, nonce []byte, err error)
EncryptGCM is a helper function that creates a new 256 bits datakey, and encrypts the given data with AES-GCM.
On success, the function returns the encrypted data, the encrypted key, and the random nonce used. Those 3 values must later be passed to DataKeyProvider.DecryptGCM in order to decrypt the data.
func (*DataKeyProvider) GenerateDataKey ¶
func (sk *DataKeyProvider) GenerateDataKey(ctx context.Context, name string, size int) (plain, encrypted []byte, err error)
GenerateDataKey creates a new datakey of bitlen `size` and with name `name`, protected by the provider's service key. The `plain` and `encrypted` form of the datakey are returned.
Acceptable key sizes are 128, 192 and 256. The encrypted key can later be decrypted by calling DataKeyProvider.DecryptDataKey.
Example ¶
package main
import (
"context"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"github.com/google/uuid"
"github.com/ovh/okms-sdk-go"
)
func main() {
var kmsClient *okms.RestAPIClient // Initialize client
data := "Hello World !!!" // Data to encrypt
dkProvider := okms.NewDataKeyProvider(kmsClient, uuid.MustParse("2dab95dc-d7d3-482b-a07b-6b4dfae89d58"))
// Generate a new datakey
plain, encrypted, err := dkProvider.GenerateDataKey(context.Background(), "Example DK", 256)
if err != nil {
panic(err)
}
// Initialize AES GCM cipher with the plain key
aesCipher, err := aes.NewCipher(plain)
if err != nil {
panic(err)
}
gcm, err := cipher.NewGCM(aesCipher)
if err != nil {
panic(err)
}
// Generate random nonce
nonce := make([]byte, gcm.NonceSize())
if _, err := rand.Read(nonce); err != nil {
panic(err)
}
// Encrypt the data
encryptedData := gcm.Seal(nil, nonce, []byte(data), []byte("Some additional data"))
fmt.Println("EncryptedKey:", encrypted, ", Encrypted data:", base64.StdEncoding.EncodeToString(encryptedData))
}
Output:
type EncryptionApi ¶
type EncryptionApi interface {
// Encrypt encrypts `data` with the remote symmetric key having the ID `keyId`. Returns a JWE (Json Web Encryption) string.
Encrypt(ctx context.Context, keyId uuid.UUID, keyCtx string, data []byte) (string, error)
// Decrypt decrypts JWE `data` previously encrypted with the remote symmetric key having the ID `keyId`.
Decrypt(ctx context.Context, keyId uuid.UUID, keyCtx, data string) ([]byte, error)
}
EncryptionApi is the client interface used to encrypt and decrypt data using a remote symmetric key.
type InternalHttpClient ¶
InternalHttpClient is the low level, internal http client generated by oapi-codegen. It may be used as an escape hatch to some low level features. Use at your own risk.
type KeyIter ¶
type KeyIter struct {
// contains filtered or unexported fields
}
KeyIter is an iterator for service keys. It helps in iterating efficiently over multiple pages without having to deal with the pagination.
func (KeyIter) Iter ¶
Iter returns a go 1.23+ iterator that can be ranged over. The iterator yields 2 values which are the key and an error. The key is null if err is not, and err is null if key is not.
func (*KeyIter) Next ¶
Next checks if there is another key to return, and advance the cursor if so. It returns true if there is another key to get, false otherwise. Once the cursor has advanced, the newt key can be retrieved with a call to Value().
func (*KeyIter) Value ¶
func (it *KeyIter) Value() (*types.GetServiceKeyResponse, error)
Value returns the last key fetched by the iterator after calling Next(), or any error that has occurred internally during the fetch.
Calling Value() multiple times will return the same result unless Next() has been called and has returned true.
type RestAPIClient ¶
type RestAPIClient struct {
// contains filtered or unexported fields
}
RestAPIClient is the main client to the KMS rest api.
func NewRestAPIClientWithHttp ¶
func NewRestAPIClientWithHttp(endpoint string, c *http.Client) (*RestAPIClient, error)
NewRestAPIClientWithHttp creates and initialize a new HTTP connection to the KMS at url `endpoint` using the provided http.Client.
The client must be configured with an appropriate tls.Config using client TLS certificates for authentication.
Example ¶
package main
import (
"context"
"crypto/tls"
"net/http"
"os"
"github.com/google/uuid"
"github.com/ovh/okms-sdk-go"
)
func main() {
cert, err := tls.LoadX509KeyPair(os.Getenv("KMS_CLIENT_CERT_FILE"), os.Getenv("KMS_CLIENT_KEY_FILE"))
if err != nil {
panic(err)
}
httpClient := http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
}},
}
kmsClient, err := okms.NewRestAPIClientWithHttp("https://eu-west-rbx.okms.ovh.net", &httpClient)
if err != nil {
panic(err)
}
// Use KMS client, for example to activate a service key
if err := kmsClient.ActivateServiceKey(context.Background(), uuid.MustParse("2dab95dc-d7d3-482b-a07b-6b4dfae89d58")); err != nil {
panic(err)
}
}
Output:
func (*RestAPIClient) ActivateServiceKey ¶
ActivateServiceKey activates or re-activates a service key.
func (*RestAPIClient) CreateImportServiceKey ¶
func (client *RestAPIClient) CreateImportServiceKey(ctx context.Context, format *types.KeyFormats, body types.CreateImportServiceKeyRequest) (*types.GetServiceKeyResponse, error)
CreateImportServiceKey is used to either generate a new key securely in the KMS, or to import a plain key into the KMS domain.
Example (GenerateAES) ¶
Generate an 256 bits AES key
package main
import (
"context"
"fmt"
"github.com/ovh/okms-sdk-go"
"github.com/ovh/okms-sdk-go/types"
)
func main() {
var kmsClient *okms.RestAPIClient // Initialize client
kType := types.Oct
kSize := types.N256
ops := []types.CryptographicUsages{types.Encrypt, types.Decrypt, types.WrapKey, types.UnwrapKey}
// Create a new AES 256 key
respAes, err := kmsClient.CreateImportServiceKey(context.Background(), nil, types.CreateImportServiceKeyRequest{
Name: "AES key example",
Type: &kType,
Size: &kSize,
Operations: &ops,
})
if err != nil {
panic(err)
}
fmt.Println("AES KEY:", respAes.Id)
}
Output:
Example (GenerateECDSA) ¶
Generate an ECDSA key pair on the P-256 curve
package main
import (
"context"
"fmt"
"github.com/ovh/okms-sdk-go"
"github.com/ovh/okms-sdk-go/types"
)
func main() {
var kmsClient *okms.RestAPIClient // Initialize client
kType := types.EC
curve := types.P256
ops := []types.CryptographicUsages{types.Sign, types.Verify}
// Create a new ECDSA P-256 key-pair
respAes, err := kmsClient.CreateImportServiceKey(context.Background(), nil, types.CreateImportServiceKeyRequest{
Name: "ECDSA key-pair example",
Type: &kType,
Curve: &curve,
Operations: &ops,
})
if err != nil {
panic(err)
}
fmt.Println("ECDSA KEY:", respAes.Id)
}
Output:
Example (GenerateRSA) ¶
Generate a 2048 bits RSA key pair
package main
import (
"context"
"fmt"
"github.com/ovh/okms-sdk-go"
"github.com/ovh/okms-sdk-go/types"
)
func main() {
var kmsClient *okms.RestAPIClient // Initialize client
kType := types.RSA
kSize := types.N2048
ops := []types.CryptographicUsages{types.Sign, types.Verify}
// Create a new RSA 2048 key-pair
respAes, err := kmsClient.CreateImportServiceKey(context.Background(), nil, types.CreateImportServiceKeyRequest{
Name: "RSA key-pair example",
Type: &kType,
Size: &kSize,
Operations: &ops,
})
if err != nil {
panic(err)
}
fmt.Println("RSA KEY:", respAes.Id)
}
Output:
func (*RestAPIClient) DeactivateServiceKey ¶
func (client *RestAPIClient) DeactivateServiceKey(ctx context.Context, keyId uuid.UUID, reason types.RevocationReasons) error
DeactivateServiceKey deactivates a service key with the given deactivation reason.
func (*RestAPIClient) Decrypt ¶
func (client *RestAPIClient) Decrypt(ctx context.Context, keyId uuid.UUID, keyCtx, data string) ([]byte, error)
Decrypt decrypts JWE `data` previously encrypted with the remote symmetric key having the ID `keyId`.
func (*RestAPIClient) DecryptDataKey ¶
func (client *RestAPIClient) DecryptDataKey(ctx context.Context, keyId uuid.UUID, encryptedKey string) ([]byte, error)
DecryptDataKey decrypts a JWE encrypted data key protected by the service key with the ID `keyId`.
func (*RestAPIClient) DeleteServiceKey ¶
DeleteServiceKey deletes a deactivated service key. The key cannot be recovered after deletion. It will fail if the key is not deactivated.
func (*RestAPIClient) Encrypt ¶
func (client *RestAPIClient) Encrypt(ctx context.Context, keyId uuid.UUID, keyCtx string, data []byte) (string, error)
Encrypt encrypts `data` with the remote symmetric key having the ID `keyId`. Returns a JWE (Json Web Encryption) string.
func (*RestAPIClient) GenerateDataKey ¶
func (client *RestAPIClient) GenerateDataKey(ctx context.Context, keyId uuid.UUID, name string, size int32) (plain []byte, encrypted string, err error)
GenerateDataKey creates a new data key of the given size, protected by the service key with the ID `keyId`. It returns the plain datakey, and the JWE encrypted version of it, which can be decrypted by calling the DecryptDataKey method.
func (*RestAPIClient) GetInternalClient ¶
func (client *RestAPIClient) GetInternalClient() *InternalHttpClient
GetInternalClient returns the internal client wrapped. It is an escape hatch to some low level features. Use at your own risk.
func (*RestAPIClient) GetServiceKey ¶
func (client *RestAPIClient) GetServiceKey(ctx context.Context, keyId uuid.UUID, format *types.KeyFormats) (*types.GetServiceKeyResponse, error)
GetServiceKey returns a key metadata. If format is not nil, then the public key material is also returned.
func (*RestAPIClient) ListAllServiceKeys ¶
func (client *RestAPIClient) ListAllServiceKeys(pageSize *int32, state *types.KeyStates) KeyIter
ListAllServiceKeys returns an iterator to go through all the keys without having to deal with pagination.
func (*RestAPIClient) ListServiceKeys ¶
func (client *RestAPIClient) ListServiceKeys(ctx context.Context, continuationToken *string, maxKeys *int32, state *types.KeyStates) (*types.ListServiceKeysResponse, error)
ListServiceKeys returns a page of service keys. The response contains a continuationToken that must be passed to the subsequent calls in order to get the next page. The state parameter when no nil is used to query keys having a specific state.
func (*RestAPIClient) Sign ¶
func (client *RestAPIClient) Sign(ctx context.Context, keyId uuid.UUID, alg types.DigitalSignatureAlgorithms, preHashed bool, msg []byte) (string, error)
Sign signs the given message with the remote private key having the ID `keyId`. The message can be pre-hashed or not.
Example ¶
package main
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/ovh/okms-sdk-go"
"github.com/ovh/okms-sdk-go/types"
)
func main() {
var kmsClient *okms.RestAPIClient // Initialize client
data := "Hello World !!!" // Data to sign
signResponse, err := kmsClient.Sign(context.Background(), uuid.MustParse("2dab95dc-d7d3-482b-a07b-6b4dfae89d58"), types.ES256, false, []byte(data))
if err != nil {
panic(err)
}
fmt.Println("Signature:", signResponse)
}
Output:
func (*RestAPIClient) UpdateServiceKey ¶
func (client *RestAPIClient) UpdateServiceKey(ctx context.Context, keyId uuid.UUID, body types.PatchServiceKeyRequest) (*types.GetServiceKeyResponse, error)
UpdateServiceKey updates some service key metadata.
func (*RestAPIClient) Verify ¶
func (client *RestAPIClient) Verify(ctx context.Context, keyId uuid.UUID, alg types.DigitalSignatureAlgorithms, preHashed bool, msg []byte, sig string) (bool, error)
Verify checks the signature of given message against the remote public key having the ID `keyId`. The message can be pre-hashed or not.
Example ¶
package main
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/ovh/okms-sdk-go"
"github.com/ovh/okms-sdk-go/types"
)
func main() {
var kmsClient *okms.RestAPIClient // Initialize client
var signature string // Base64 encoded signature
data := "Hello World !!!" // Data to sign
result, err := kmsClient.Verify(context.Background(), uuid.MustParse("2dab95dc-d7d3-482b-a07b-6b4dfae89d58"), types.ES256, false, []byte(data), signature)
if err != nil {
panic(err)
}
fmt.Println("Is valid:", result)
}
Output:
func (*RestAPIClient) WithCustomHeader ¶
func (client *RestAPIClient) WithCustomHeader(key, value string) *RestAPIClient
WithCustomHeader adds additional HTTP headers that will be sent with every outgoing requests.
type ServiceKeyApi ¶
type ServiceKeyApi interface {
// ActivateServiceKey activates or re-activates a service key.
ActivateServiceKey(ctx context.Context, keyId uuid.UUID) error
// DeactivateServiceKey deactivates a service key with the given deactivation reason.
DeactivateServiceKey(ctx context.Context, keyId uuid.UUID, reason types.RevocationReasons) error
// CreateImportServiceKey is used to either generate a new key securely in the KMS, or to import a plain key into the KMS domain.
CreateImportServiceKey(ctx context.Context, format *types.KeyFormats, body types.CreateImportServiceKeyRequest) (*types.GetServiceKeyResponse, error)
// DeleteServiceKey deletes a deactivated service key. The key cannot be recovered after deletion.
// It will fail if the key is not deactivated.
DeleteServiceKey(ctx context.Context, keyId uuid.UUID) error
// GetServiceKey returns a key metadata. If format is not nil, then the public key material is also returned.
GetServiceKey(ctx context.Context, keyId uuid.UUID, format *types.KeyFormats) (*types.GetServiceKeyResponse, error)
// ListServiceKeys returns a page of service keys. The response contains a continuationToken that must be passed to the
// subsequent calls in order to get the next page. The state parameter when no nil is used to query keys having a specific state.
ListServiceKeys(ctx context.Context, continuationToken *string, maxKey *int32, state *types.KeyStates) (*types.ListServiceKeysResponse, error)
// ListAllServiceKeys returns an iterator to go through all the keys without having to deal with pagination.
ListAllServiceKeys(pageSize *int32, state *types.KeyStates) KeyIter
// UpdateServiceKey updates some service key metadata.
UpdateServiceKey(ctx context.Context, keyId uuid.UUID, body types.PatchServiceKeyRequest) (*types.GetServiceKeyResponse, error)
}
ServiceKeyApi is the client interface used to query and manipulate the remote service keys lifecycle.
type SignatureApi ¶
type SignatureApi interface {
// Sign signs the given message with the remote private key having the ID `keyId`. The message can be pre-hashed or not.
Sign(ctx context.Context, keyId uuid.UUID, alg types.DigitalSignatureAlgorithms, preHashed bool, msg []byte) (string, error)
// Verify checks the signature of given message against the remote public key having the ID `keyId`. The message can be pre-hashed or not.
Verify(ctx context.Context, keyId uuid.UUID, alg types.DigitalSignatureAlgorithms, preHashed bool, msg []byte, sig string) (bool, error)
}
SignatureApi is the client interface used to sign data and verify signatures using a remote asymmetric key-pair.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package internal provides primitives to interact with the openapi HTTP API.
|
Package internal provides primitives to interact with the openapi HTTP API. |
|
Package types provides primitives to interact with the openapi HTTP API.
|
Package types provides primitives to interact with the openapi HTTP API. |