AWS Key Management Service (AWS KMS) allows you to use keys under your control to encrypt data at rest stored in Amazon S3. The two primary methods for implementing this encryption are server-side encryption (SSE) and client-side encryption (CSE). Each method offers multiple interfaces and API options to choose from. In this blog post, I will show you how to use the AWS REST APIs to secure S3 objects using KMS keys via SSE. Using the native REST APIs vs. the AWS SDKs may be a better option for you if you are looking to develop cross-platform code or want more control over API usage within your applications.
One critical aspect of using any AWS REST API call is the signature used to authenticate the request. This signature is vital if you wish to allow only authenticated users to access your data. There is an option to allow anonymous access that does not require a signature, but this makes your data publicly available to the world. For security, AWS recommends that you use only authenticated requests with a signature. This allows for verification of the requestor’s identity, in-transit data integrity protection, and protection against reuse of the signed portions of the request for replay attempts. I will describe the generic approach to signing AWS REST APIs, and then I will show how to use that technique to request that the S3 service encrypt objects on your behalf, using keys in your account that you manage using KMS.
Signatures are calculated by concatenating request elements to form a string such as a POST policy from an HTTP POST request. This is called the string to sign. Next, a signing key is used to calculate the keyed-hash method authentication code (HMAC) of the string to sign. In AWS Signature Version 4 signing, your secret access key is used to create the signing key. When S3 receives the request, it recreates the signature by using the authentication information provided to ensure a match. If it matches, it will accept/return your data, if not, an error is returned.
The encryption key you use is an important factor in deciding to use SSE or CSE encryption. Some factors you should take into account when determining the best option for you are cost, flexibility, security, and maintenance. The most important factor, however, is access control. To have the proper security levels, you want to ensure that only permitted users are able to access the encryption keys needed to decrypt data. You control these access permissions in KMS by using policies.
With KMS, you also can see when, where, and by whom your customer managed keys (CMK) are used, because all API calls are logged by AWS CloudTrail. These logs provide you with full audit capabilities for your keys. There is also one default CMK for each account and service integrated with KMS. This key is referred to as the default service key and will always be listed with an alias of aws/[servicename] (for example, aws/s3) in your KMS console. This default service key is used if you choose not to create your own custom keys in KMS.
The first time you add a SSE-KMS encrypted object to a S3 bucket in a specific region, a default service key is created for you, which only allows users in your account the permission to use the key from S3. However, creating your own CMKs does give you more flexibility with access control, auditing, rotation, and deletion. It also allows you to use CMKs across multiple AWS services and from within your own applications.
Default service CMKs are assigned a default key policy that you cannot change. You can see the policy yourself by running the following AWS CLI command. (Replace the placeholder values with your own values.)
The following policy example is the default key policy assigned to the default aws/s3 CMK. What this policy states is that all AWS Principals in the account can perform the Actions listed against S3 resources as long as they have the proper S3 permissions in their own IAM policies. This also gives permission to the account itself in order to pull the metadata of the key (account ID, ARN, description, and key state).
With a custom CMK, however, you can limit who can manage and use the key, and who is permitted to allow AWS resources to access KMS on your behalf. The following policy is an example of a policy assigned during custom CMK creation.
With the preceding custom policy, you can be restrictive about the users, roles, and services that can have access to the key. This is one of the benefits of using a CMK in KMS. For more information about KMS key policies, see Key Policies.
When making all GET and PUT requests for an S3 object protected by KMS, you must use Signature Version 4 signing because Signature Version 2 signing is not supported. Signature Version 4 signing provides authentication using some or all of the following, depending on how you choose to sign the request:
Request headers are a series of lines in the HTTP request that contain a name/value pair. These lines can be in any order and are used to pass information about the original request. Some examples of common request headers are: Content-Length, Content-Type, Date, Host, and x-amz-security-token. The following is an example of a basic request including headers:
Some request headers are necessary for SSE encryption. The main required header is x-amz-server-side-encryption, which is used to request SSE-KMS with aws:kms used as the encryption format for object uploads. You must also use the x-amz-server-side-encryption-aws-kms-key-id header, because this specifies the ID of the KMS CMK you want to use. To add an extra layer of security that forces recipient to provide matching contextual information about the object, you would use x-amz-server-side-encryption-context. For more information, see the KMS documentation on Encryption Context and our blog post on the topic .
One of the easier ways to make REST API calls is to use a browser with a RESTful add-on. Two options to choose from are Postman – REST Client for Google Chrome and RESTClient for Mozilla Firefox. After you have installed the add-on, you can then use your browser to make REST API calls against your S3 objects. This is a fast and efficient way to access your data without having to program an application.
For this post, I am using Firefox and the RESTClient add-on, which you can see in the following screenshot.
Now that I have covered the main components of S3 with SSE-KMS and making REST API calls, I can begin the process of using the REST API to secure S3 objects with SSE-KMS.
The authorization signature is used to authenticate the API requests to S3 so that your stored data is available only to authenticated users. The signature is created by combining data of the object and hash values created from your secret access key with other metadata.
Creating your signature can be done in various programming languages, but the examples I include in this post use JavaScript. The code snippets will show you how to create your string to sign, create the hash of your signing key, and provide the complete signature for API call authentication.
The JavaScript code I used to create the hashes for my signing key is forge.bundle.js. You can use the language your prefer, including Ruby, .NET, Python, and others. This will create the HMAC that is used to then create the final signature.
The “heavy lifter” of the group is the downloadable Presigned URL JavaScript code for Browser-based JavaScript POST script. This script creates your signature value by combining the hash values created by the forge.bundle.js script, the information for the file, and the KMS keys used for encryption. This signature value will be placed in the REST API call under the Authorization section in the body of your request.
You now have a completed signature to authenticate your REST API requests to S3. The next step focuses on creating the PUT REST API call by combining elements of the object and CMK for your request.
I will use a PUT request instead of POST because PUT is for creating or updating, and POST is only for creating. This request prevents duplicate objects. The PUT request places your data or object on your server for retrieval at a later date.
To upload data to S3 using the REST API, you need to create a PUT request. To do this, set the Method to PUT, specify the URL as an S3 URL (for example, https://s3-us-east-1.amazonaws.com), and then place the body of the request in the Body box. Be sure to include the x-amz headers to specify aws:kms as the method of encryption, and the key ID of the KMS CMK you wish to use for the actual data encryption. An example PUT REST API call follows.
You should receive a response that resembles the following (the following screenshot shows how the response appears in the browser add-on).
A GET request is used to recall data that has already been placed on a server. Think of it as opening a file or downloading an image. It is a way to retrieve information that was previously stored.
When you want to retrieve your data from S3, you must create a GET request. This request depends on your user having READ access to the objects that were uploaded. This will allow the user to open or download the requested object. Because this call is for an object encrypted with KMS, you will also need to provide the signature created previously for authorization. You follow the same steps as with the PUT request, but use the GET API instead. An example GET REST API call follows.
In this post, I have explained why you use signatures to authenticate REST API calls, what a signature includes, and how you create signatures. Using signatures for API authentication raises the security of your application. I also covered how SSE-KMS can add extra levels of security with access policies, encryption context, and request header specifications. Walking through how to add the signature value to your REST API requests should give you an idea of how easy this process can be—the most involved part of this process is the signature creation. From there, you are on your way to making REST API calls to quickly and efficiently place or retrieve your data securely.
If you have comments, submit them in the “Comments” section below. If you have questions, please start a new thread on the IAM forum.