A REXX Productivity Tool for calling the IBM Common Cryptographic Architecture (CCA) and IBM PKCS #11 Callable Services on z/OS
This tool drammatically simplifies the process of calling the IBM CCA and PKCS #11 Callable Services from REXX by providing a REXX "host command environment" that understands how to call CCA and PKCS #11 verbs and only requires you to populate the required verb parameters, providing default values for the rest. It knows the data type and length of each parameter, so integer parameters can be provided as REXX integers, rather than having to convert them to 4-byte binary form. Similarly text parameters (e.g. key labels) can be provided as a string of any length, which ZCCREXX will automatically pad with spaces to the the required length (e.g. 64 characters for a key label).
To understand the value that ZCCREXX brings, let's look at an example of generating a random number using the CCA verb, CSNBRNGL (see: https://www.ibm.com/docs/en/zos/3.1.0?topic=keys-random-number-generate-csnbrng-csnerng-csnbrngl-csnerngl).
Without ZCCREXX, to call CSNBRNGL directly from REXX, the code would look something like this:
return_code = D2C(0, 4)
reason_code = D2C(0, 4)
exit_data_length = D2C(0, 4)
exit_data = ""
rule_array_count = D2C(1, 4)
rule_array = "ODD"
reserved_length = D2C(0, 4)
reserved = ""
random_number_length = D2C(24, 4)
random_number = COPIES('00'x, 24)
address LINKPGM "CSNBRNGL" ,
"return_code" ,
"reason_code" ,
"exit_data_length" ,
"exit_data" ,
"rule_array_count" ,
"rule_array" ,
"reserved_length" ,
"reserved" ,
"random_number_length" ,
"random_number"
say "CSNBRNGL rc="C2D(return_code)", reason="C2D(reason_code)
if C2D(return_code) = 0 then do
say "Random number:" C2X(random_number)
end
Whereas, ZCCREXX requires much less code:
rule_array_count = 1 /* Note: integer, not binary '00000001'x */
rule_array = "ODD" /* Note: no padding required */
random_number_length = 24
address ZCCREXX "CSNBRNGL" /* Note: no need to supply verb parameters */
/* Note: a result "banner" is automatically SAYed, displaying return_code and reason_code */
if return_code = 0 then do
say "Random number:" C2X(random_number)
end
Upload the file, zccrexx.xmit, to your z/OS system. Make sure the file transfer uses BINARY mode (i.e. no ASCII/EBCDIC translation). Also make sure that the destination data set on z/OS has LRECL=80 and RECFM=FB (or F).
Then use the TSO RECEIVE command to convert this data set to a PDSE. For example:
receive inda('zccrexx.xmit')
INMR901I Dataset ZCX100.SZCXLOAD from ADCDMST on S0W1
INMR154I The incoming data set is a 'PROGRAM LIBRARY'.
INMR906A Enter restore parameters or 'DELETE' or 'END' +
da('zccrexx.load')
Finally, make sure that this PDSE is in the STEPLIB concatenation (typically, or somewhere in the REXX external function search order. See: https://www.ibm.com/docs/en/zos/3.1.0?topic=subroutines-search-order) for the environment(s) where you are running your REXX programs.
To use ZCCREXX in your REXX program, you must install the ZCCREXX host command environment, and as a matter of good hygiene, you should remove it when no longer needed. Once installed, you can (optionally) make ZCCREXX the default command environment using the ADDRESS statement. For example:
call ZCCREXX(ON) /* Install the ZCCREXX host command environment */
address ZCCREXX /* Make ZCCREXX the default command environment */
rule_array_count = 1
rule_array = "ODD"
random_number_length = 24
"CSNBRNGL" /* This command is passed to ZCCREXX */
if return_code = 0 then do
say "Random number:" C2X(random_number)
end
call ZCCREXX(OFF) /* Remove the ZCCREXX host command environment */
To call a CCA verb consult the "CCA callable services" section, or to call a PKCS #11 verb consult the "PKCS #11 callable services" section of the "IBM z/OS Cryptographic Services ICSF Application Programmer's Guide" (see: https://www.ibm.com/docs/en/zos/3.1.0?topic=guide-cca-callable-services or https://www.ibm.com/docs/en/zos/3.1.0?topic=guide-pkcs-11-callable-services) for a description of the parameters passed to each verb. ZCCREXX expects you to populate REXX variables with the required inputs before invoking the verb. The REXX variables must be named exactly as the parameters are named in the IBM documentation. Note, however, that REXX variable names are not case-sensitive. You do not need to define variables for optional input parameters or output-only parameters - ZCCREXX automatically sets null values for these parameters and populates REXX variables for the output parameters on return from calling each verb.
If you place a question mark at the end of the verb when invoking it using ZCCREXX, the verb is not executed. Instead, ZCCREXX will SAY all the input and output parameters for that verb.
For example, this REXX program:
/* REXX */
trace o
ZCERRSAY = 0
call ZCCREXX(ON) /* Install the ZCCREXX host command environment */
"CSNBRNGL?" /* Add a question mark (?) to verb name */
call ZCCREXX(OFF) /* Remove the ZCCREXX host command environment */
produces the following output:
CSNBRNGL - Random Number Generate
Type Name
------- ----------------------------------------------------
Mandatory input parameters:
Integer rule_array_count
String rule_array
Integer random_number_length
Optional input parameters:
Integer exit_data_length
String exit_data
Integer reserved_length
String reserved
Output parameters:
Integer return_code
Integer reason_code
Integer exit_data_length
String exit_data
String random_number
A REXX program, zcchelp.rexx, is provided to facilitate displaying the parameters for each verb.
The samples folder contains a number of REXX programs which demonstrate how to use ZCCREXX. Feel free to use them as you see fit!
Several verb parameters, particularly the rule_array parameter, must be supplied as a concatenation of one or more fixed-length (typically 8-character) strings. To make your code more legible, and to avoid errors specifying such parameters, ZCCREXX supplies a built-in function called ZCPACK.
This function takes two arguments:
- (Mandatory) a space-delimited string comprising the words to be concatenated as fixed-length components of the output string.
- (Optional) the length of each fixed-length component of the output string. The default length of each component is 8 characters.
Words in the input string must be separated by a single space.
Here are some examples of ZCPACK in action:
ZCPACK("") -> ""
ZCPACK("quick brown fox") -> "quick brown fox "
ZCPACK("quick brown fox", 4) -> "quicbrowfox "
ZCPACK("quick brown fox") -> "quick brown fox "
/* |-------|-------|-------|------- */
Here's a typical example of using ZCPACK to populate the rule_array and rule_array_count parameters, leveraging the fact that ZCPACK will return a string that is suitably padded to a multiple of 8 characters:
rule_array = ZCPACK("DES INTERNAL DOUBLE NO-KEY KEY-PART")
rule_array_count = LENGTH(rule_array) / 8
Several of the sample REXX programs provided here demonstrate the use of the ZCPACK function.
Many PKCS #11 callable services require attribute lists to be supplied in a specific format (see: https://www.ibm.com/docs/en/zos/3.1.0?topic=services-attribute-list-format). To make your code more legible, and to avoid errors specifying such parameters, ZCCREXX supplies a built-in function called ZCALIST.
This function takes an even number of arguments. Each pair of arguments comprises a 4-byte name and a value. A binary string is returned, comprised of a 2-byte binary count of the number of attributes contained in the list, followed by that number of triplets, each comprised of a 4-byte binary name, 2-byte value length and the value.
The sample REXX program, smfkeys demonstrates the use of the ZCALIST function.
Some PKCS #11 attributes must be provided as a DER-encoded ASN.1 Object Identifier (OID). To generate a DER-encoded OID as a binary string, ZCCREXX supplies a built-in function called ZCOID.
This function takes a single argument. This argument is the OID to be encoded, formatted as dotted-decimal string or space-delimited list of decimal node values.
Here are some examples of ZCOID in action:
ZCOID("1.3.132.0.35") -> '06052B81040023'x
ZCOID("1 3 6 1 4 1 2 267 1 6 5") -> '060B2B0601040102820B010605'x
The sample REXX program, smfkeys demonstrates the use of the ZCOID function.
To initialize REXX variables for all the constants required by the IBM PKCS #11 callable services, ZCCREXX supplies a built-in function called ZCPDEFS.
This function takes no arguments.
Each constant is defined in the forward and reverse direction, which permits the name of a constant of a particular type to be retrieved, based on its value. For example:
CKK_IBM_DILITHIUM -> '80010023'x
CKK.X80010023 -> "CKK_IBM_DILITHIUM"
The sample REXX program, smfkeys demonstrates the use of the ZCPDEFS function.
ZCCREXX recognises a set of (optional) pre-defined REXX variables, which can be used to customise the behavior of, or retrieve information about, ZCCREXX processing.
This variable is automatically set to the build date and time of the running version of ZCCREXX.
This variable is automatically set to the name of the last verb processed by ZCCREXX.
This variable is automatically set to the title of the last verb processed by ZCCREXX. If the verb is not recognised by ZCCREXX, this variable is set to "?".
This stem variable contains the descriptive text (if any) corresponding to the return_code and reason_code for the last verb processed by ZCCREXX. ZCERRM.0 contains an integer count of the number of lines of descriptive text. The descriptive text is contained in variables, ZCERRM.1, ZCERRM.2, ..., ZCERRM.n, where n is the count contained in ZCERRM.0.
If you set this variable to 1, ZCCREXX uses MVS "write to operator" (WTO) calls to emit detailed trace information. It is recommended that you do not use this capability, unless directed to do so by the author while attempting to chase down a bug in ZCCREXX.
Use this variable to customise the manner in which ZCCREXX SAYs the results of processing a CCA verb. Depending on the value of ZCERRSAY, ZCCREXX will SAY
- a single-line "banner" showing the verb that was processed, and the resulting return_code and reason_code, AND/OR
- a multi-line description corresponding to the return_code and reason_code
The value of ZCERRSAY is a bit mask, with each bit controlling the circumstances under which a banner and/or descriptive text is displayed, as follows:
Bit
(as decimal) Meaning
------------ -----------------------------------------------------------------------------------------------------
1 Banner produced for any return_code/reason_code
2 Banner produced for return_code not zero OR reason_code not zero
4 Banner produced for return_code negative or return_code >= 8
8 Descriptive text produced for any return_code/reason_code
16 Descriptive text produced for return_code not zero OR reason_code not zero
32 Descriptive text produced for return_code negative or return_code >= 8
64 Help requested (RC=-100) is treated as a warning, otherwise treated as return_code = reason_code = 0
The default value of ZCERRSAY is 1 + 16 + 64 = 81. To suppress all SAYs of error information by ZCCREXX, set ZCERRSAY = 0.
Note that the setting of ZCCERRSAY has no effect on the stem variable, ZCCERRM. It is always populated with descriptive text corresponding to the value of return_code and reason_code.
Use this variable to set the maximum line length for descriptive text. The minimum line length is 55 characters, the default is 75 characters and the maximum line length is 255 characters. The value of this variable has no effect on the "banner" line, if ZCERRSAY is set so that a banner is produced.
Use this variable to control how many lines of space are SAYed after each (unformatted) line of descriptive text. The default is 0 = no line spacing and the maximum is 2 lines.
Note that the setting of ZCCERRLINESPACE has no effect on the stem variable, ZCCERRM.
Use this variable to set the number of spaces SAYed at the start of each line of descriptive text (if any).
Note that the setting of ZCCERRINDENT has no effect on the stem variable, ZCCERRM.
ZCCREXX implements the full gamut of (183) z/OS ICSF CCA and PKCS #11 verbs, with the following exceptions:
- Privileged verbs: CSFACEE and CSFWRP are not supported.
- CSNBxxx1 and CSNBxxx3 variants of data protection and integrity verbs (e.g. CSNBENC1, CSNBCTT3, etc), that accept an ALET for some parameters, are not supported. For those base functions that accept an ALET parameter (i.e. CSNBFLD and CSNBFLE), a non-zero ALET value is ignored, forcing all parameter values to be sourced from the primary address space.
- 64-bit verbs (i.e. CSFxxx6, CSNExxx and CSNFxxx) are not supported.