...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt/mongocrypt_kms_context.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/mongocrypt

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  //go:build cse
     8  // +build cse
     9  
    10  package mongocrypt
    11  
    12  // #include <mongocrypt.h>
    13  import "C"
    14  
    15  // KmsContext represents a mongocrypt_kms_ctx_t handle.
    16  type KmsContext struct {
    17  	wrapped *C.mongocrypt_kms_ctx_t
    18  }
    19  
    20  // newKmsContext creates a KmsContext wrapper around the given C type.
    21  func newKmsContext(wrapped *C.mongocrypt_kms_ctx_t) *KmsContext {
    22  	return &KmsContext{
    23  		wrapped: wrapped,
    24  	}
    25  }
    26  
    27  // HostName gets the host name of the KMS.
    28  func (kc *KmsContext) HostName() (string, error) {
    29  	var hostname *C.char // out param for mongocrypt function to fill in hostname
    30  	if ok := C.mongocrypt_kms_ctx_endpoint(kc.wrapped, &hostname); !ok {
    31  		return "", kc.createErrorFromStatus()
    32  	}
    33  	return C.GoString(hostname), nil
    34  }
    35  
    36  // KMSProvider gets the KMS provider of the KMS context.
    37  func (kc *KmsContext) KMSProvider() string {
    38  	kmsProvider := C.mongocrypt_kms_ctx_get_kms_provider(kc.wrapped, nil)
    39  	return C.GoString(kmsProvider)
    40  }
    41  
    42  // Message returns the message to send to the KMS.
    43  func (kc *KmsContext) Message() ([]byte, error) {
    44  	msgBinary := newBinary()
    45  	defer msgBinary.close()
    46  
    47  	if ok := C.mongocrypt_kms_ctx_message(kc.wrapped, msgBinary.wrapped); !ok {
    48  		return nil, kc.createErrorFromStatus()
    49  	}
    50  	return msgBinary.toBytes(), nil
    51  }
    52  
    53  // BytesNeeded returns the number of bytes that should be received from the KMS.
    54  // After sending the message to the KMS, this message should be called in a loop until the number returned is 0.
    55  func (kc *KmsContext) BytesNeeded() int32 {
    56  	return int32(C.mongocrypt_kms_ctx_bytes_needed(kc.wrapped))
    57  }
    58  
    59  // FeedResponse feeds the bytes received from the KMS to mongocrypt.
    60  func (kc *KmsContext) FeedResponse(response []byte) error {
    61  	responseBinary := newBinaryFromBytes(response)
    62  	defer responseBinary.close()
    63  
    64  	if ok := C.mongocrypt_kms_ctx_feed(kc.wrapped, responseBinary.wrapped); !ok {
    65  		return kc.createErrorFromStatus()
    66  	}
    67  	return nil
    68  }
    69  
    70  // createErrorFromStatus creates a new Error from the status of the KmsContext instance.
    71  func (kc *KmsContext) createErrorFromStatus() error {
    72  	status := C.mongocrypt_status_new()
    73  	defer C.mongocrypt_status_destroy(status)
    74  	C.mongocrypt_kms_ctx_status(kc.wrapped, status)
    75  	return errorFromStatus(status)
    76  }
    77  

View as plain text