ISO/IEC 7816-4
Identification cards — Integrated circuit cards — Part 4: Organization, security and commands for interchange
Cartes d’identification — Cartes à circuit intégré — Partie 4: Organisation, sécurité et commandes pour les échanges

MANAGE SECURITY ENVIRONMENT command
The command prepares secure messaging (see 6) and security commands (e.g., EXTERNAL, INTERNAL and GENERAL AUTHENTICATE, see also PERFORM SECURITY OPERATION in ISO/IEC 7816-8[4]). The command supports the following functions:
–SET, i.e., setting or replacing one component of the current SE;
–STORE, i.e., saving the current SE under the SEID byte in P2;
–RESTORE, i.e., replacing the current SE by a SE stored in the card and identified by the SEID byte in P2;
–ERASE, i.e., erasing a SE stored in the card and identified by the SEID byte in P2. 

Table 77 — MANAGE SECURITY ENVIRONMENT command-response pair

 
CLA INS P1 P2 As defined in 5.1.1 ’22′ See Table 78 See Table 79
Lc field Absent for encoding Nc = 0, present for encoding Nc > 0
Data field Absent (STORE, RESTORE and ERASE), or concatenation of control reference data objects (SET)
Le field Absent for encoding Ne = 0

Table 78 — P1

 
Data field Absent
SW1-SW2 See Tables 5 and 6 when relevant, e.g., ’6600′, ’6987′, ’6988, ’6A88′ (see 7.5.1)

Table 79 — P2

 
b8 b7 b6 b5 b4 b3 b2 b1 Meaning
—1 –1 –1 –1 — —————- Secure messaging in command data field Secure messaging in response data field Computation, decipherment, internal authentication and key agreement Verification, encipherment, external authentication and key agreement
—- 0 0 0 1 SET
1 1 1 1 0 0 1 0 STORE
1 1 1 1 0 0 1 1 RESTORE
1 1 1 1 0 1 0 0 ERASE
 Any other value is reserved for future use by ISO/IEC JTC 1/SC 17.
Value Meaning
‘XX’ SEID byte in the cases of STORE, RESTORE and ERASE (set to ’00′ in the case of GET SE)
‘A4′ ‘A6′ ‘AA’ ‘B4′ ‘B6′ ‘B8′ Tag of the control reference template present in the command data field in the cases of SET, or GET CRT — Control reference template for authentication (AT) — Control reference template for key agreement (KAT) — Control reference template for hash-code (HT) — Control reference template for cryptographic checksum (CCT) — Control reference template for digital signature (DST) — Control reference template for confidentiality (CT)
⎯ Any other value is reserved for future use by ISO/IEC JTC 1/SC 17.

KEY DERIVATION function — The usage of a master key concept may require the derivation of a key in the card containing the master key. Table 80 shows the usage of the MANAGE SECURITY ENVIRONMENT command for deriving a key. It is assumed that the master key and the algorithm are implicitly selected in the card (otherwise, the MANAGE SECURITY ENVIRONMENT command can additionally select a key and an algorithm).

NOTE Depending on the algorithm reference, the data for deriving a key from a master key may be part of the input data of the subsequent command (e.g., EXTERNAL AUTHENTICATE). In this case the usage of the MANAGE SECURITY ENVIRONMENT command for deriving the key is not necessary.

Table 80 — Command-response pair for KEY DERIVATION function

 
CLA INS P1 P2 As defined in 5.1.1 ’22′ ‘X1′ (SET, see Table 78) CRT tag (e.g., ‘A4′ if an EXTERNAL AUTHENTICATE follows, or ‘B4′ if a VERIFY CRYPTOGRAPHIC CHECKSUM follows)
Lc field Present for encoding Nc > 0
Data field {’94′ – L – Data for deriving a key (mandatory)}; SM data objects may be present
Le field Absent for encoding Ne = 0
Data field Absent
SW1-SW2 See Tables 5 and 6 when relevant, e.g., ’6600′, ’6987′, ’6988, ’6A88′ (reference data not found)

Transmission handling

GET RESPONSE command

The command transmits [part of] response APDUs that otherwise could not be transmitted by the available transmission protocol. See examples in ISO/IEC 7816-3.

If the Le field contains only bytes set to ’00′, then all the available bytes should be returned within the limit of 256 for a short Le field, or 65 536 for an extended Le field.

Table 81 — GET RESPONSE command-response pair

CLA INS P1-P2 As defined in 5.1.1 ‘C0′ ’0000′ (any other value is reserved for future use)
Lc field Absent for encoding Nc = 0
Data field Absent
Le field Present for encoding Ne > 0
Data field Absent in any error case, or [Part of] a response APDU according to Ne
SW1-SW2 See Tables 5 and 6 when relevant, e.g., ’61XX’ (‘XX’ encodes the number of extra bytes still available by a subsequent GET RESPONSE), ’6281′, ’6700′, ’6A81′, ’6A82′, ’6A86′, ’6CXX’
ENVELOPE command

The command transmits [part of] either a command APDU or a BER-TLV data object that otherwise could not be transmitted by the available transmission protocol. See examples in ISO/IEC 7816-3.

NOTE Annex B shows the usage of the ENVELOPE command for secure messaging.

Table 82 — ENVELOPE command-response pair

CLA INS P1-P2 As defined in 5.1.1 ‘C2′, ‘C3′ ’0000′ (any other value is reserved for future use)
Lc field Present for encoding Nc > 0
Data field [Part of] a command APDU (INS = ‘C2′), or [part of] a BER-TLV data object (INS = ‘C3′)
Le field Absent for encoding Ne = 0, present for encoding Ne > 0
Data field [Part of] a response APDU (INS = ‘C2′), or absent
SW1-SW2 See Tables 5 and 6 when relevant, e.g., ’6700′