AWS KMS: Import a PFX Private Key into a New Signing Key
See more AWS KMS Examples
Uses the current AWS KMS API. Configure the region and keep credentials in private qa_data/aws/kms.json, with nonempty string fields access_key and secret_key. The examples do not obtain or refresh credentials. Keep diagnostics private; they may contain credentials or sensitive key data. Create the output directory before running file-writing examples.
This convenience-method example configures an access-key/secret-key pair. It explicitly rejects a supplied session_token instead of silently omitting it: token support for this helper configuration has not been established here. Prefer temporary role credentials in production using a documented token-capable integration. The separate Rest examples support a session token. No credentials or private key material are printed.
Loads the certificate's private key from qa_data/pfx/myCertAndKey.pfx using string field pfx_password from the private configuration. Supports RSA 2048/3072/4096 and NIST P-256/P-384/P-521 in this sample. Keep a protected backup of the original material: KMS cannot export it. RSA material must satisfy AWS's current key-material requirements. Tags are visible metadata; do not put passwords or sensitive certificate identity data in them.
The UploadToCloud convenience method creates a new external-origin KMS key and imports the private key, with KeyUsage=SIGN_VERIFY. Rerunning can create additional keys and charges. On failure, inspect KMS/CloudTrail before deciding whether to retry or clean up a pending key. Required permissions include kms:CreateKey, kms:TagResource, kms:GetParametersForImport, and kms:ImportKeyMaterial, subject to IAM and key policy. The certificate itself stays local. Record the returned KeyId; inspect DescribeKey for state and expiration policy. Use the explicit low-level example when you need control of an existing destination key and import settings.
AWS ImportKeyMaterial reference
Chilkat Objective-C Downloads
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoCert.h>
#import <CkoPrivateKey.h>
#import <CkoPublicKey.h>
BOOL success = NO;
CkoStringBuilder *stringCompare = [[CkoStringBuilder alloc] init];
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this credentials file outside public web content and source control.
CkoJsonObject *credentials = [[CkoJsonObject alloc] init];
credentials.Strict = YES;
success = [credentials LoadFile: @"qa_data/aws/kms.json"];
if (success != YES) {
NSLog(@"%@",@"Could not load private KMS configuration.");
return;
}
[stringCompare SetString: [credentials StringOf: @"access_key"]];
BOOL stringMatch1 = [stringCompare ContentsEqual: @"" caseSensitive: YES];
if (([[credentials JsonTypeOf: @"access_key"] intValue] != 1) || (stringMatch1 == YES)) {
NSLog(@"%@",@"Missing or invalid access_key.");
return;
}
[stringCompare SetString: [credentials StringOf: @"secret_key"]];
BOOL stringMatch2 = [stringCompare ContentsEqual: @"" caseSensitive: YES];
if (([[credentials JsonTypeOf: @"secret_key"] intValue] != 1) || (stringMatch2 == YES)) {
NSLog(@"%@",@"Missing or invalid secret_key.");
return;
}
NSString *region = @"us-west-2";
// These convenience-method examples use an access-key/secret-key pair.
// Do not silently discard the token belonging to temporary credentials.
if ([credentials HasMember: @"session_token"] == YES) {
NSLog(@"%@",@"This convenience-method sample does not configure temporary credentials. Use a documented token-capable signing/import integration.");
return;
}
// pfx_password is a string; an empty string is allowed for an unprotected PFX.
if ([[credentials JsonTypeOf: @"pfx_password"] intValue] != 1) {
NSLog(@"%@",@"Missing or invalid pfx_password.");
return;
}
CkoCert *cert = [[CkoCert alloc] init];
success = [cert LoadPfxFile: @"qa_data/pfx/myCertAndKey.pfx" password: [credentials StringOf: @"pfx_password"]];
if (success != YES) {
NSLog(@"%@",@"Could not load PFX; inspect diagnostics privately.");
return;
}
CkoPrivateKey *privKey = [[CkoPrivateKey alloc] init];
success = [cert GetPrivateKey: privKey];
if (success != YES) {
NSLog(@"%@",@"PFX does not contain a usable private key.");
return;
}
// Only inspect the PUBLIC JWK to identify a supported key type and curve.
CkoPublicKey *sourcePublic = [[CkoPublicKey alloc] init];
success = [privKey ToPublicKey: sourcePublic];
if (success != YES) {
NSLog(@"%@",@"Could not get the public key.");
return;
}
NSString *publicJwk = [sourcePublic GetJwk];
if (sourcePublic.LastMethodSuccess != YES) {
NSLog(@"%@",@"Could not inspect public key type.");
return;
}
CkoJsonObject *keyInfo = [[CkoJsonObject alloc] init];
keyInfo.Strict = YES;
success = [keyInfo Load: publicJwk];
if (success != YES) {
NSLog(@"%@",@"Could not parse public key description.");
return;
}
NSString *keySpec = @"";
[stringCompare SetString: [keyInfo StringOf: @"kty"]];
BOOL stringMatch3 = [stringCompare ContentsEqual: @"RSA" caseSensitive: YES];
if (stringMatch3 == YES) {
if ([privKey.BitLength intValue] == 2048) {
keySpec = @"RSA_2048";
}
if ([privKey.BitLength intValue] == 3072) {
keySpec = @"RSA_3072";
}
if ([privKey.BitLength intValue] == 4096) {
keySpec = @"RSA_4096";
}
}
[stringCompare SetString: [keyInfo StringOf: @"kty"]];
BOOL stringMatch4 = [stringCompare ContentsEqual: @"EC" caseSensitive: YES];
if (stringMatch4 == YES) {
[stringCompare SetString: [keyInfo StringOf: @"crv"]];
BOOL stringMatch5 = [stringCompare ContentsEqual: @"P-256" caseSensitive: YES];
if (stringMatch5 == YES) {
keySpec = @"ECC_NIST_P256";
}
[stringCompare SetString: [keyInfo StringOf: @"crv"]];
BOOL stringMatch6 = [stringCompare ContentsEqual: @"P-384" caseSensitive: YES];
if (stringMatch6 == YES) {
keySpec = @"ECC_NIST_P384";
}
[stringCompare SetString: [keyInfo StringOf: @"crv"]];
BOOL stringMatch7 = [stringCompare ContentsEqual: @"P-521" caseSensitive: YES];
if (stringMatch7 == YES) {
keySpec = @"ECC_NIST_P521";
}
}
[stringCompare SetString: keySpec];
BOOL stringMatch8 = [stringCompare ContentsEqual: @"" caseSensitive: YES];
if (stringMatch8 == YES) {
NSLog(@"%@",@"Use RSA 2048/3072/4096 or a NIST P-256/P-384/P-521 EC private key.");
return;
}
CkoJsonObject *options = [[CkoJsonObject alloc] init];
success = [options UpdateString: @"service" value: @"aws-kms"];
if (success != YES) {
NSLog(@"%@",@"Could not set service.");
return;
}
success = [options UpdateString: @"auth.access_key" value: [credentials StringOf: @"access_key"]];
if (success != YES) {
NSLog(@"%@",@"Could not set auth.access_key.");
return;
}
success = [options UpdateString: @"auth.secret_key" value: [credentials StringOf: @"secret_key"]];
if (success != YES) {
NSLog(@"%@",@"Could not set auth.secret_key.");
return;
}
success = [options UpdateString: @"auth.region" value: region];
if (success != YES) {
NSLog(@"%@",@"Could not set auth.region.");
return;
}
success = [options UpdateString: @"description" value: @"Imported certificate signing key"];
if (success != YES) {
NSLog(@"%@",@"Could not set description.");
return;
}
success = [options UpdateString: @"tags[0].key" value: @"Purpose"];
if (success != YES) {
NSLog(@"%@",@"Could not set tags[0].key.");
return;
}
success = [options UpdateString: @"tags[0].value" value: @"DocumentSigning"];
if (success != YES) {
NSLog(@"%@",@"Could not set tags[0].value.");
return;
}
success = [options UpdateString: @"keyUsage" value: @"SIGN_VERIFY"];
if (success != YES) {
NSLog(@"%@",@"Could not set keyUsage.");
return;
}
CkoJsonObject *imported = [[CkoJsonObject alloc] init];
// Creates a NEW KMS key. Do not blindly rerun after an uncertain failure.
success = [privKey UploadToCloud: options jsonOut: imported];
if (success != YES) {
NSLog(@"%@",@"Import failed or its outcome is uncertain. Check KMS/CloudTrail before retrying; inspect diagnostics privately.");
return;
}
[stringCompare SetString: [imported StringOf: @"KeyId"]];
BOOL stringMatch9 = [stringCompare ContentsEqual: @"" caseSensitive: YES];
if (([[imported JsonTypeOf: @"KeyId"] intValue] != 1) || (stringMatch9 == YES)) {
NSLog(@"%@",@"Missing or invalid KeyId.");
return;
}
NSLog(@"%@%@",@"Imported KMS key ID: ",[imported StringOf: @"KeyId"]);
// Save this identifier securely for subsequent signing and lifecycle management.