This document describes the most common Public Key Infrastructure (PKI) data formats and encodings.
Cisco recommends that you have knowledge of these topics:
This document is not restricted to specific software and hardware versions.
The information in this document was created from the devices in a specific lab environment. All of the devices used in this document started with a cleared (default) configuration. If your network is live, make sure that you understand the potential impact of any command.
Refer to Cisco Technical Tips Conventions for information on document conventions.
Abstract Syntax Notation One (ASN.1) is a formal language for the definition of data types and values, and how those data types and values are used and combined in various data structures. The goal of the standard is to define the abstract syntax of information without constraining how the information is encoded for transmission.
Here is an example excerpted from the X.509 RFC:
Version ::= INTEGER { v1(0), v2(1), v3(2) }
CertificateSerialNumber ::= INTEGER
Validity ::= SEQUENCE {
notBefore Time,
notAfter Time }
Time ::= CHOICE {
utcTime UTCTime,
generalTime GeneralizedTime }
Refer to these documents from the International Telecommunication Union (ITU-T) standards sites:
ITU-T recommendations search - Search for X.509 in Rec. or standard with Language set to ASN.1.
The ITU-T has defined a standard way of encoding data structures described in ASN.1 into binary data. X.690 defines Basic Encoding Rules (BER) and its two subsets, Canonical Encoding Rules (CER) and Distinguished Encoding Rules (DER). All three are based on type-length-value data fields packed in a hierarchical structure, which is built from SEQUENCEs, SETs, and CHOICEs, with these differences:
Example: In DER, the 20-bit value 1010 1011 1100 1101 1110 is encoded as :
0x03
(bitstring)0x04
(bytes)0x04ABCDE0
0x030404ABCDE0
The leading 04
means that the last 4 bits (equals the trailing 0
digit) of the encoded value must be discarded because the encoded value does not end on a byte boundary.
Refer to these documents from the TU-T standards site:
From the Wikipedia site, refer to these documents:
Cisco IOS, Adaptive Security Appliance (ASA), and other devices display DER content as a hex dump with the show running-config command. Here is the output:
crypto pki certificate chain root
certificate ca 01
30820213 3082017C A0030201 02020101 300D0609 2A864886 F70D0101 04050030
1D310C30 0A060355 040B1303 54414331 0D300B06 03550403 1304726F 6F74301E
170D3039 30373235 31313436 33325A17 0D313230 37323431 31343633 325A301D
...
This kind of hex dump can be converted back to DER in various ways. For instance, you can remove the space characters and pipe it to the xxd program:
$ cat ca.hex | tr -d ' ' | xxd -r -p -c 32 | openssl x509 -inform der -text -noout
Another easy way is to use this Perl script :
#!/usr/bin/perl
foreach (<>) {
s/[^a-fA-F0-9]//g;
print join("", pack("H*", $_));
}
$ perl hex2der.pl < hex-file.txt > der-file.der
In addition, a compact way to convert cert dumps, each one previously manually copied to a file with extension .hex, from a bash command line as shown here:
for hex in *.hex; do
b="${hex%.hex}"
hex2der.pl < "$hex" > "$b".der
openssl x509 -inform der -in "$b".der > "$b".pem
openssl x509 -in "$b".pem -text -noout > "$b".txt
done
Each file results in:
Base64 encoding represents the binary data with only 64 printable characters (A-Za-z0-9+/
) similarly to uuencode. In the conversion from binary to Base64, every 6-bit block of the original data is encoded into an 8-bit printable ASCII character with a translation table. Therefore, the size of the data after encoding has increased by 33 percent (data times 8 divided by 6 bits, equals 1.333).
A 24-bit buffer is used for translation of three (3) groups of eight (8) bits into four (4) groups of six (6) bits. Therefore one (1) or two (2) bytes of padding might be required at the end of the input data stream. Padding is indicated at the end of the Base64-encoded data, by one equals (=)
sign for each group of eight (8) padding bits added to the input during encoding.
Refer to this example from Wikipedia.
Refer to the most recent information in RFC 4648: The Base16, Base32, and Base64 Data Encodings.
Privacy Enhanced Mail (PEM) is a full Internet Engineering Task Force (IETF) PKI standard in order to exchange secure messages. It is no longer widely used as such, but its encapsulation syntax has been widely borrowed in order to format and exchange Base64-encoded PKI-related data.
The PEM RFC 1421, section 4.4: Encapsulation Mechanism, defines PEM messages as delimited by Encapsulation Boundaries (EBs), which are based on RFC 934, with this format:
-----BEGIN PRIVACY-ENHANCED MESSAGE-----
Header: value
Header: value
...
Base64-encoded data
...
-----END PRIVACY-ENHANCED MESSAGE-----
In practice today, when PEM-formatted data is distributed, this boundary format is used:
-----BEGIN type-----
...
-----END type-----
type can be with other keys or certificates such as:
RSA PRIVATE KEY
ENCRYPTED PRIVATE KEY
CERTIFICATE
CERTIFICATE REQUEST
X509 CRL
Note: Although the RFCs do not make this mandatory, the number of leading and trailing dashes (-
) in the EBs is significant and should always be five (5). Otherwise, some applications, such as OpenSSL, choke on the input. On the other hand, other applications, such as Cisco IOS, do not require EBs at all.
Refer to these most recent RFCs for further information:
X.509 is a subset of X.500, which is an extended ITU specification about Open Systems Interconnection. It deals specifically with certificates and public keys and has been adapted as an Internet standard by the IETF. X.509 provides a structure and syntax, expressed in the RFC with ASN.1 Notation, in order to store certificate information and certificate revocation lists.
In an X.509 PKI, a CA issues a certificate that binds a public key, for example: a Rivest-Shamir-Adleman (RSA) or Digital Signature Algorithm (DSA) key to a particular Distinguished Name (DN), or to an alternative name such as an email address or fully qualified domain name (FQDN). The DN follows the structure in the X.500 standards. Here is an example:
CN=common-name,OU=organizational-unit,O=organization,L=location,C=country
Because of the ASN.1 definition, X.509 data can be encoded into DER in order to be exchanged in binary form, and optionally, converted to Base64/PEM for text-based means of communication, such as copy-paste on a terminal.
The Public-Key Cryptography Standards (PKCS) are specifications from RSA Labs that have partly evolved into industry standards. Those most often encountered, deal with these topics; however, not all of them deal with data formats.
PKCS#1 (RFC 3347) - Covers the implementation aspects of RSA-based cryptography (crypto primitives, encryption/signature schemes, ASN.1 syntax).
PKCS#5 (RFC 2898) - Covers password-based key derivation.
PKCS#7 (RFC 2315) and S/MIME RFC 3852 - Defines a message syntax to transmit signed and encrypted data and related certificates. Often used simply as a container for X.509 certificates.
PKCS#8- Defines a message syntax to transport cleartext or encrypted RSA keypairs.
PKCS#9 (RFC 2985) - Defines additional object classes and identity attributes.
PKCS#10 (RFC 2986) - Defines a message syntax for Certificate Signing Requests (CSRs). A CSR is sent by an entity to a CA and contains the information to be signed by the CA, such as public key information, identity, and additional attributes.
PKCS#12 - Defines a container for packaging related PKI data (typically, entity keypair + entity cert + root and intermediate CA certificates) within a single file. It is an evolution of Microsoft's Personal Information Exchange (PFX) format.
Refer to these resources: