KMFCryptoOperation - Scoped, Global
The KMFCryptoOperation class provides methods for performing cryptographic operations, including JSON Web Token (JWT) signing and verification, using the Key Management Framework (KMF) cryptographic module or a Field Encryption encryption module.
To use this API, you must have already created and configured a KMF cryptographic module or a Field Encryption module. This module must have one or more cryptographic specifications and you must create or import its associated key. For JWT signing and verification operations, the cryptographic module must be configured with an asymmetric key specification. For details, see Cryptographic modules.
The KMFCryptoOperation object generated using this API represents a cryptographic operation, such as Symmetric Encryption, JWT_SIGN, or JWT_VERIFY. Use the KMFCryptoOperations() method to create this object, the builder methods to set properties on the object, and the doOperation() method to execute the operation.
For JWT Sign operations, the KMF cryptographic module manages the private key used to sign the token. The corresponding public key is used for JWT Verify operations. Because the private key is held on the instance by the cryptographic module, customers do not need to upload or manage their own keystore.
You can use this API in both scoped and global applications. You must always specify the sn_kmf_ns namespace when calling this API.
Parent Topic:Server API reference
KMFCryptoOperation - KMFCryptoOperation(String cryptoModuleName, String operationName)
Creates a KMFCryptoOperation object for the specified module and operation.
This API leverages builder methods. Builder methods update properties on the KMFCryptoOperation object, such as changing the desired output format of the data. Not all builder methods are valid for all operations. The builder methods available for each operation are noted in the parameters table below.
The following builder methods are valid for all operation types:
Important: Base64 input values used in this document must be URL safe (contains only A-Z, a-z, 0-9, dash( - ) and underscore( _ ) characters).
| Name | Type | Description |
|---|---|---|
| cryptoModuleName | String | Name of the Key Management Framework \(KMF\) cryptographic module or Field Encryption encryption module to use. You must create the module before calling this method. For details, see Cryptographic module overview. |
| operationName | String | Name of the operation to perform.Valid values \(not case-sensitive\): - ASYMMETRIC\_DECRYPTION: Data decryption using an asymmetric-key algorithm. Requires a KMF cryptographic module with an Asymmetric Data Decryption cryptographic purpose. - Additional builder methods: withAdditionalInput\(\) - Default input format: Formatted - Formatted to the KMF specifications - Default output format: KMFBase64 - Base64 encoded - Default output type: String - ASYMMETRIC\_ENCRYPTION: Data encryption using an asymmetric-key algorithm. Requires a KMF cryptographic module with an Asymmetric Data Encryption cryptographic purpose. - Additional builder methods: withAdditionalInput\(\) - Default input format: KMFBase64 - Base64 encoded - Default output format: Formatted - Formatted to the KMF specifications - Default output type: String . Output can also be an KMFEncryptionPayload object. RSA and EC-IES are compatible with both. For additional information on the KMFEncryptionPayload object, see withAdditionalInput(). - ASYMMETRIC\_UNWRAPPING: Key unwrapping using an asymmetric-key algorithm. Requires a KMF cryptographic module with an Asymmetric Key Unwrapping cryptographic purpose. - Additional builder methods: withAlgorithm\(\) - Default input format: Formatted - Formatted to the KMF specifications - Default output format: KMFBase64 - Base64 encoded - Default output type: String - ASYMMETRIC\_WRAPPING: Key wrapping using an asymmetric-key algorithm. Requires a KMF cryptographic module with an Asymmetric Key Wrapping cryptographic purpose. - Additional builder methods: withAlgorithm\(\), withSysId\(\) - Default input format: KMFBase64 - Base64 encoded - Default output format: Formatted - Formatted to the KMF specifications - Default output type: String - JWT\_SIGN: Generate a JSON Web Token \(JWT\) signature using an asymmetric-key algorithm. Requires a KMF cryptographic module with a Signature Generation cryptographic purpose. The JWT header must be passed via the withAdditionalInput\(\) method. The generated JWT can be verified by external systems that have access to the public key material. - Additional builder methods:withAdditionalInput\(\) - Default input format: String, JWT payload in JSON format - Default output format: String, a complete JWT token \(header.payload.signature\) - Default output type: String - JWT\_VERIFY: JSON Web Token \(JWT\) signature verification using an asymmetric-key algorithm. Requires a KMF cryptographic module with a Signature Verification cryptographic purpose. - Additional builder methods: None. - Default input format: String, a complete JWT token \(header.payload.signature\) - Default output format: String, containing validity status, decoded header, and decoded payload - Default output type: String. Returns a JSON object with signature validity \(true/false\) and decoded JWT components if verification succeeds. Can verify JWTs generated both within and outside the ServiceNow instance. - MAC\_GENERATION: Generation of a Message Authentication Code \(MAC\). Symmetric-key algorithm based to provides data integrity and authentication. Requires a KMF cryptographic module with a Symmetric Authenticity cryptographic purpose. - Additional builder methods: None - Default input format: KMFBase64 - Base64 encoded - Default output format: Formatted - Formatted to the KMF specifications - Default output type: String - MAC\_VERIFICATION: Verification of a MAC. Symmetric-key algorithm based to provide data integrity and authentication. Requires a KMF cryptographic module with a Symmetric Authenticity cryptographic purpose. - Additional builder methods: withAdditionalInput\(\) - Default input format: KMFBase64 - Base64 encoded - Default output format: KMFNone - No decoding - Default output type: Boolean - SIGNATURE\_GENERATION: Generation of a digital signature. Asymmetric-key algorithm based to provide data integrity and authentication. Requires a KMF cryptographic module with a Signature Generation cryptographic purpose. - Additional builder methods: None - Default input format: KMFBase64 - Base64 encoded - Default output format: Formatted - Formatted to the KMF specifications - Default output type: String - SIGNATURE\_VERIFICATION: Verification of a digital signature. Asymmetric-key algorithm based to provide data integrity and authentication. Requires a KMF cryptographic module with a Signature Verification cryptographic purpose. - Additional builder methods: withAdditionalInput\(\) - Default input format: KMFBase64 - Base64 encoded - Default output format: KMFNone - No decoding - Default output type: Boolean - SYMMETRIC\_ENCRYPTION: Data encryption using a symmetric-key algorithm. If the algorithm is not equality preserving, only formatted output is allowed. Requires a KMF cryptographic module with a Symmetric Data Encryption/Decryption cryptographic purpose. - Additional builder methods: None - Default input format: KMFBase64 - Base64 encoded - Default output format: Formatted - Formatted to the KMF specifications - Default output type: String - SYMMETRIC\_DECRYPTION: Data decryption using a symmetric-key algorithm. If the algorithm is not equality preserving, KMFBase64 input is allowed. Requires a KMF cryptographic module with a Symmetric Data Encryption/Decryption cryptographic purpose. - Additional builder methods: None - Default input format: Formatted - Formatted to the KMF specifications - Default output format: KMFBase64 - Base64 encoded - Default output type: String - SYMMETRIC\_WRAPPING: Key wrapping using a symmetric-key algorithm. If the algorithm is not equality preserving, only formatted output is allowed. Requires a KMF cryptographic module with a Symmetric Key Wrapping/Unwrapping cryptographic purpose. - Additional builder methods: withAlgorithm\(\) and withSysId\(\) - Default input format: KMFBase64 - Base64 encoded - Default output format: Formatted - Formatted to the KMF specifications - Default output type: String - SYMMETRIC\_UNWRAPPING: Key unwrapping using a symmetric-key algorithm. If the algorithm is not equality preserving, KMFBase64 input is allowed. Requires a KMF cryptographic module with a Symmetric Key Wrapping/Unwrapping cryptographic purpose. - Additional builder methods: withAlgorithm\(\) - Default input format: Formatted - Formatted to the KMF specifications - Default output format: KMFBase64 - Base64 encoded - Default output type: String |
This example instantiates a KMFCryptoOperation object for the module global.sj_cm to perform a Symmetric Encryption operation. You must include the namespace for both global and scoped applications.
var op = new sn_kmf_ns.KMFCryptoOperation("global.sj_cm","SYMMETRIC_ENCRYPTION");
This example shows how to specify options to update the default output type and output format.
var op = new sn_kmf_ns.KMFCryptoOperation("global.sj_cm","SYMMETRIC_ENCRYPTION")
.withOutputType("STRING").withOutputFormat("FORMATTED");
var cipherText=op.doOperation("hi");
This example shows how to perform an Asymmetric Encryption operation using an Integrated Encryption Scheme (EC-IES). Note that long values, such as signature, have been truncated and replaced with an ellipse for readability.
var op = new sn_kmf_ns.KMFCryptoOperation("global.sj_cm","ASYMMETRIC_ENCRYPTION")
.withInputFormat("KMFNONE")
.withOutputType("PAYLOAD");
var cipherText = op.doOperation("hi");
/*
cipherText contains an object similar to this JSON: {
"signature": "pkg…",
"ephemeral_key": "BDi…",
"ciphertext": "afFS…"
}
*/
This example shows how to perform an Asymmetric Decryption operation using EC-IES.
var op = new
sn_kmf_ns.KMFCryptoOperation("global.sj_cm","ASYMMETRIC_DECRYPTION")
.withAdditionalInput({
"signature": "pkg… ",
"ephemeral_key": "BDi…"
})
.withOutputFormat("KMFNONE");
var clearText = op.doOperation("afFS…");
This example shows how to generate and verify a JWT signature.
// Validating from instance
var moduleName = 'global.jwtimport'; // must have signature generation spec
var header = '{ "alg": "P5512","typ": "JWT"}'; // your header JSON as a string
var payload = '{"sub": "1234567890", "name": "John Doe", "admin": true, "iat": 1516239022)'; // your payload JSON as a string
// SIGN
var api = new sn_kmf_ns.KMFCryptoOperation(moduleName, "JWT_SIGN").withAdditionalInput(header);
var token = api.doOperation(payload);
gs.info("sign = " + token);
// VERIFY
api = new sn_kmf_ns.KMFCrypto0peration(moduleName, "JWT_VERIFY");
var verifyoutput = api.doOperation(token);
gs.info("verify = " + verifyOutput);
KMFCryptoOperation - doOperation(Object data)
Performs the cryptographic operation defined by the current KMFCryptoOperation object on the supplied data and returns the result.
| Name | Type | Description |
|---|---|---|
| data | Object | Required except if the withSysId() builder method has previously been called on the associated KMFCryptoOperation object. Input data on which to perform the cryptographic operation. |
| Type | Description |
|---|---|
| Depends on the operation type. - MAC_VERIFICATION and SIGNATURE_VERIFICATION: Boolean - All others: String | Data results after performing the operation specified in the associated KMFCryptoOperation object. |
This example uses the doOperation() to create a MAC.
var op = new sn_kmf_ns.KMFCryptoOperation("global.sj_cm","MAC_GENERATION");
var data = "aGk=";
var mac = op.doOperation(data);
KMFCryptoOperation - withAdditionalInput(Object additionalInput)
Sets the additional input needed to perform the cryptographic operation.
For example, during a Message Authentication Code (MAC) verification, use this method to pass in the generated MAC tag. Similarly, during signature verification, use it to pass in the signature. You can also use this method to pass additional data, a KMFEncryptionPayload object, when performing an asymmetric operation with an integrated cipher, such as Elliptic Curve Integrated Encryption Scheme (EC-IES.)
Note: The additional input does not have to be in the same format that is currently set on the KMFCryptoOperation object.
| Name | Type | Description |
|---|---|---|
| additionalInput | String or Object | Optional, except for Asymmetric Decryption operations when using EC-IES. Additional input data needed to perform the cryptographic operation specified in the KMFCryptoOperation object.Supported string formats: - FORMATTED: Formatted to the Key Management Framework \(KMF\) specifications. - KMFBASE64: Base64 encoded. KMFEncryptionPayload object format: ``` { "ciphertext": String, "derivation_secret": String, "ephemeral_key": String, "ephemeral_key_format": String, "signature": String } |