Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Create JWK Set Containing Certificates

See more Certificates Examples

Demonstrates how to create a JWK Set containing N certificates.

Chilkat Rust Downloads

Rust

// This example creates the following JWK Set from two certificates:

// {
//   "keys": [
//     {
//       "kty": "RSA",
//       "use": "sig",
//       "kid": "BB8CeFVqyaGrGNuehJIiL4dfjzw",
//       "x5t": "BB8CeFVqyaGrGNuehJIiL4dfjzw",
//       "n": "nYf1jpn7cFdQ...9Iw",
//       "e": "AQAB",
//       "x5c": [
//         "MIIDBTCCAe2...Z+NTZo"
//       ]
//     },
//     {
//       "kty": "RSA",
//       "use": "sig",
//       "kid": "M6pX7RHoraLsprfJeRCjSxuURhc",
//       "x5t": "M6pX7RHoraLsprfJeRCjSxuURhc",
//       "n": "xHScZMPo8F...EO4QQ",
//       "e": "AQAB",
//       "x5c": [
//         "MIIC8TCCAdmgA...Vt5432GA=="
//       ]
//     }
//   ]
// }

// First get two certificates from files.
let cert1 = chilkat::Cert::new();
if cert1.load_from_file("qa_data/certs/brasil_cert.pem").is_err() {
    println!("{}", cert1.last_error_text());
    return;
}

let cert2 = chilkat::Cert::new();
if cert2.load_from_file("qa_data/certs/testCert.cer").is_err() {
    println!("{}", cert2.last_error_text());
    return;
}

// We'll need this crypt object re-encode the SHA1 thumbprint from hex to base64.
let crypt = chilkat::Crypt2::new();

let json = chilkat::JsonObject::new();

// Let's begin with the 1st cert:
json.set_i(0);
let _ = json.update_string("keys[i].kty", "RSA");
let _ = json.update_string("keys[i].use", "sig");

let mut hex_thumbprint = cert1.sha1_thumbprint();
let mut base64_thumbprint = crypt.re_encode(&hex_thumbprint, "hex", "base64").unwrap_or_default();
let _ = json.update_string("keys[i].kid", &base64_thumbprint);
let _ = json.update_string("keys[i].x5t", &base64_thumbprint);

// (We're assuming these are RSA certificates)
// To get the modulus (n) and exponent (e), we need to get the cert's public key and then get its JWK.
let pub_key = chilkat::PublicKey::new();
let _ = cert1.get_public_key(&pub_key);

let pub_key_jwk = chilkat::JsonObject::new();
let _ = pub_key_jwk.load(&pub_key.get_jwk().unwrap_or_default());
let _ = json.update_string("keys[i].n", &pub_key_jwk.string_of("n").unwrap_or_default());
let _ = json.update_string("keys[i].e", &pub_key_jwk.string_of("e").unwrap_or_default());

// Now add the entire X.509 certificate 
let _ = json.update_string("keys[i].x5c[0]", &cert1.get_encoded().unwrap_or_default());

// Now do the same for cert2..
json.set_i(1);

let _ = json.update_string("keys[i].kty", "RSA");
let _ = json.update_string("keys[i].use", "sig");

hex_thumbprint = cert2.sha1_thumbprint();
base64_thumbprint = crypt.re_encode(&hex_thumbprint, "hex", "base64").unwrap_or_default();
let _ = json.update_string("keys[i].kid", &base64_thumbprint);
let _ = json.update_string("keys[i].x5t", &base64_thumbprint);
let _ = cert2.get_public_key(&pub_key);

let _ = pub_key_jwk.load(&pub_key.get_jwk().unwrap_or_default());
let _ = json.update_string("keys[i].n", &pub_key_jwk.string_of("n").unwrap_or_default());
let _ = json.update_string("keys[i].e", &pub_key_jwk.string_of("e").unwrap_or_default());

// Now add the entire X.509 certificate 
let _ = json.update_string("keys[i].x5c[0]", &cert2.get_encoded().unwrap_or_default());

// Emit the JSON..
json.set_emit_compact(false);
println!("{}", json.emit().unwrap_or_default());