class JOSE::JWK

Overview

Represents a JSON Web Key (JWK) for EC, RSA, oct, and OKP key types.

Defined in:

jose/jwk.cr

Constructors

Instance Method Summary

Constructor Detail

def self.from_binary(json : String) : JWK #

Parses json and returns the resulting JWK.


[View source]
def self.from_map(map : Hash(String, JSON::Any)) : JWK #

Constructs a JWK wrapping the given map.


[View source]
def self.from_map(map : Hash(String, JSON::Any), **overrides : String) : JWK #

Constructs a JWK wrapping map with overrides merged in. String overrides are automatically boxed into JSON::Any.

JOSE::JWK.from_map(k1.map, kid: "sig")

[View source]
def self.from_oct(key : Bytes) : JWK #

Creates an oct (symmetric) JWK from raw key bytes.


[View source]
def self.from_pem(pem : String) : JWK #

Loads an EC or RSA key from PEM and returns the corresponding JWK. Returns a JWK with private fields if the PEM contains a private key, or a public-only JWK if it contains only a public key.

Accepted PEM formats and the OpenSSL commands that produce them:

# EC private key  (SEC 1 / RFC 5915)
openssl ecparam -name prime256v1 -genkey -noout -out ec-p256.pem

# EC public key  (SubjectPublicKeyInfo)
openssl ec -in ec-p256.pem -pubout -out ec-p256-pub.pem

# RSA private key  (PKCS#1)
openssl genrsa -out rsa-2048.pem 2048

# RSA public key  (SubjectPublicKeyInfo)
openssl rsa -in rsa-2048.pem -pubout -out rsa-2048-pub.pem
jwk = JOSE::JWK.from_pem(File.read("ec-p256.pem"))

[View source]
def self.generate_key(params : Hash(String, JSON::Any)) : JWK #

Generates a new key according to params.

The params hash must contain at least "kty" and, depending on the key type, optional shape keys:

  • "EC""crv": curve name ("P-256""P-521"), default "P-256"
  • "RSA""bits": key size in bits, default 2048
  • "oct""size": key length in bytes, default 32
  • "OKP""crv": curve name ("Ed25519"), default "Ed25519"

NOTE RSA key generation is synchronous and may take a moment for large bit sizes. EC and OKP generation is near-instant.

Using OpenSSL to generate keys externally:

# EC
openssl ecparam -name prime256v1 -genkey -noout -out ec-p256.pem
# RSA
openssl genrsa -out rsa-2048.pem 2048

Load with JWK.from_pem(File.read("ec-p256.pem")).

Prefer the typed convenience wrappers .generate_key_ec, .generate_key_rsa, .generate_key_oct, and .generate_key_okp for common cases.

ec_key = JOSE::JWK.generate_key({"kty" => JSON::Any.new("EC"), "crv" => JSON::Any.new("P-384")})
rsa_key = JOSE::JWK.generate_key({"kty" => JSON::Any.new("RSA"), "bits" => JSON::Any.new(2048_i64)})
sym_key = JOSE::JWK.generate_key({"kty" => JSON::Any.new("oct"), "size" => JSON::Any.new(32_i64)})
okp_key = JOSE::JWK.generate_key({"kty" => JSON::Any.new("OKP"), "crv" => JSON::Any.new("Ed25519")})

[View source]
def self.generate_key_ec(crv : String = "P-256") : JWK #

Generates a new "EC" key. crv defaults to "P-256". See .generate_key for details.


[View source]
def self.generate_key_oct(size : Int32 = 32) : JWK #

Generates a new "oct" key. size defaults to 32. See .generate_key for details.


[View source]
def self.generate_key_okp(crv : String = "Ed25519") : JWK #

Generates a new "OKP" key. crv defaults to "Ed25519". See .generate_key for details.


[View source]
def self.generate_key_rsa(bits : Int32 = 2048) : JWK #

Generates a new "RSA" key. bits defaults to 2048. See .generate_key for details.


[View source]
def self.new(map : Hash(String, JSON::Any)) #

[View source]

Instance Method Detail

def ==(other : JWK) : Bool #

Returns true if both JWKs have identical key parameters.


[View source]
def [](key : String) : JSON::Any #

Returns the value for key from the key map.


[View source]
def []?(key : String) : JSON::Any | Nil #

Returns the value for key, or nil if absent.


[View source]
def block_decrypt(encrypted : String | EncryptedBinary) : String #

Decrypts encrypted using this key. Delegates to JWE.block_decrypt.


[View source]
def block_encrypt(plain_text : String, header = nil) : EncryptedBinary #

Encrypts plain_text using this key. Delegates to JWE.block_encrypt.


[View source]
def ec_private_key : LibCrypto::EC_KEY #

Returns an EC_KEY with both public and private key set.

NOTE Caller must free the returned key with LibCrypto.ec_key_free.


[View source]
def ec_public_key : LibCrypto::EC_KEY #

Returns an EC_KEY with only the public key set.

NOTE Caller must free the returned key with LibCrypto.ec_key_free.


[View source]
def ed25519_signing_key : Ed25519::SigningKey #

Returns the Ed25519 signing key for OKP private keys.


[View source]
def ed25519_verify_key : Ed25519::VerifyKey #

Returns the Ed25519 verify key for OKP keys.


[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


[View source]
def key_bytes : Bytes #

Returns the raw symmetric key bytes for oct keys.


[View source]
def kty : String #

Returns the key type ("EC", "RSA", "oct", or "OKP").


[View source]
def map : Hash(String, JSON::Any) #

The underlying JSON map holding all key parameters.


[View source]
def private? : Bool #

Returns true if this key contains private key material.


[View source]
def public? : Bool #

Returns true if this key contains no private key material.


[View source]
def rsa_raw_key : LibCryptoJose::RSA #

Returns the raw LibCryptoJose::RSA handle built from this key's parameters.

NOTE Caller must free the returned handle with LibCryptoJose.RSA_free.


[View source]
def sign(plain_text : String, header = nil) : SignedBinary #

Signs plain_text using this key. Delegates to JWS.sign.


[View source]
def to_binary : String #

Serializes this key to a JSON string.


[View source]
def to_map : Hash(String, JSON::Any) #

Returns the underlying JSON map.


[View source]
def to_pem : String #

Serializes this key to PEM format.


[View source]
def to_public : JWK #

Returns a copy of this JWK with all private fields removed.


[View source]
def verify(signed : String | SignedBinary) : Tuple(Bool, String) #

Verifies signed using this key. Delegates to JWS.verify.


[View source]
def with(**fields : String) : JWK #

Returns a copy of this JWK with the given string fields merged in. Existing fields are overwritten; all other fields are preserved.

jwk.with(kid: "sig")
jwk.with(kid: "sig", use: "sig", alg: "ES256")

[View source]