2008 2011 Ericsson AB, All Rights Reserved The contents of this file are subject to the Erlang Public License, Version 1.1, (the "License"); you may not use this file except in compliance with the License. You should have received a copy of the Erlang Public License along with this software. If not, it can be retrieved online at http://www.erlang.org/. Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Initial Developer of the Original Code is Ericsson AB. public_key Ingela Anderton Andin
public_key API module for public key infrastructure.

This module provides functions to handle public key infrastructure from RFC 5280 - X.509 certificates and some parts of the PKCS-standard.

COMMON DATA TYPES

All records used in this manual are generated from ASN.1 specifications and are documented in the User's Guide. See Public key records and X.509 Certificate records.

Use the following include directive to get access to the records and constant macros described here and in the User's Guide.

-include_lib("public_key/include/public_key.hrl").

Data Types

boolean() = true | false

string = [bytes()]

pki_asn1_type() = 'Certificate' | 'RSAPrivateKey'| 'RSAPublicKey' 'DSAPrivateKey' | 'DSAPublicKey' | 'DHParameter' | 'SubjectPublicKeyInfo'| 'PrivateKeyInfo'

pem_entry () = {pki_asn1_type(), binary(), %% DER or encrypted DER not_encrypted | cipher_info()}

cipher_info() = {"RC2-CBC | "DES-CBC" | "DES-EDE3-CBC", crypto:rand_bytes(8)} | 'PBES2-params'}

rsa_public_key() = #'RSAPublicKey'{}

rsa_private_key() = #'RSAPrivateKey'{}

dsa_public_key() = {integer(), #'Dss-Parms'{}}

dsa_private_key() = #'DSAPrivateKey'{}

public_crypt_options() = [{rsa_pad, rsa_padding()}].

rsa_padding() = 'rsa_pkcs1_padding' | 'rsa_pkcs1_oaep_padding' | 'rsa_no_padding'

rsa_digest_type() = 'md5' | 'sha'

dss_digest_type() = 'none' | 'sha'

ssh_file() = openssh_public_key | rfc4716_public_key | known_hosts | auth_keys

decrypt_private(CipherText, Key) -> binary() decrypt_private(CipherText, Key, Options) -> binary() Public key decryption. CipherText = binary() Key = rsa_private_key() Options = public_crypt_options()

Public key decryption using the private key.

decrypt_public(CipherText, Key) - > binary() decrypt_public(CipherText, Key, Options) - > binary() CipherText = binary() Key = rsa_public_key() Options = public_crypt_options()

Public key decryption using the public key.

der_decode(Asn1type, Der) -> term() Decodes a public key asn1 der encoded entity. Asn1Type = atom() ASN.1 type present in the public_key applications asn1 specifications. Der = der_encoded()

Decodes a public key ASN.1 der encoded entity.

der_encode(Asn1Type, Entity) -> der_encoded() Encodes a public key entity with asn1 DER encoding. Asn1Type = atom() Asn1 type present in the public_key applications ASN.1 specifications. Entity = term() The erlang representation of Asn1Type

Encodes a public key entity with ASN.1 DER encoding.

pem_decode(PemBin) -> [pem_entry()] Decode PEM binary data and return entries as ASN.1 der encoded entities. PemBin = binary() Example {ok, PemBin} = file:read_file("cert.pem").

Decode PEM binary data and return entries as ASN.1 der encoded entities.

pem_encode(PemEntries) -> binary() Creates a PEM binary PemEntries = [pem_entry()]

Creates a PEM binary

pem_entry_decode(PemEntry) -> term() pem_entry_decode(PemEntry, Password) -> term() Decodes a pem entry. PemEntry = pem_entry() Password = string()

Decodes a pem entry. pem_decode/1 returns a list of pem entries. Note that if the pem entry is of type 'SubjectPublickeyInfo' it will be further decoded to an rsa_public_key() or dsa_public_key().

pem_entry_encode(Asn1Type, Entity) -> pem_entry() pem_entry_encode(Asn1Type, Entity, {CipherInfo, Password}) -> pem_entry() Creates a pem entry that can be fed to pem_encode/1. Asn1Type = pki_asn1_type() Entity = term() The Erlang representation of Asn1Type. If Asn1Type is 'SubjectPublicKeyInfo' then Entity must be either an rsa_public_key() or a dsa_public_key() and this function will create the appropriate 'SubjectPublicKeyInfo' entry. CipherInfo = cipher_info() Password = string()

Creates a pem entry that can be feed to pem_encode/1.

encrypt_private(PlainText, Key) -> binary() Public key encryption using the private key. PlainText = binary() Key = rsa_private_key()

Public key encryption using the private key.

encrypt_public(PlainText, Key) -> binary() Public key encryption using the public key. PlainText = binary() Key = rsa_public_key()

Public key encryption using the public key.

pkix_decode_cert(Cert, otp|plain) -> #'Certificate'{} | #'OTPCertificate'{} Decodes an ASN.1 der encoded pkix x509 certificate. Cert = der_encoded()

Decodes an ASN.1 der encoded pkix certificate. The otp option will use the customized ASN.1 specification OTP-PKIX.asn1 for decoding and also recursively decode most of the standard parts.

pkix_encode(Asn1Type, Entity, otp | plain) -> der_encoded() Der encodes a pkix x509 certificate or part of such a certificate. Asn1Type = atom() The ASN.1 type can be 'Certificate', 'OTPCertificate' or a subtype of either .

Der encodes a pkix x509 certificate or part of such a certificate. This function must be used for encoding certificates or parts of certificates that are decoded/created in the otp format, whereas for the plain format this function will directly call der_encode/2.

pkix_is_issuer(Cert, IssuerCert) -> boolean() Checks if IssuerCert issued Cert Cert = der_encode() | #'OTPCertificate'{} IssuerCert = der_encode() | #'OTPCertificate'{}

Checks if IssuerCert issued Cert

pkix_is_fixed_dh_cert(Cert) -> boolean() Checks if a Certificate is a fixed Diffie-Hellman Cert. Cert = der_encode() | #'OTPCertificate'{}

Checks if a Certificate is a fixed Diffie-Hellman Cert.

pkix_is_self_signed(Cert) -> boolean() Checks if a Certificate is self signed. Cert = der_encode() | #'OTPCertificate'{}

Checks if a Certificate is self signed.

pkix_issuer_id(Cert, IssuedBy) -> {ok, IssuerID} | {error, Reason} Returns the issuer id. Cert = der_encode() | #'OTPCertificate'{} IssuedBy = self | other IssuerID = {integer(), {rdnSequence, [#'AttributeTypeAndValue'{}]}} The issuer id consists of the serial number and the issuers name. Reason = term()

Returns the issuer id.

pkix_normalize_name(Issuer) -> Normalized Normalizes a issuer name so that it can be easily compared to another issuer name. Issuer = {rdnSequence,[#'AttributeTypeAndValue'{}]} Normalized = {rdnSequence, [#'AttributeTypeAndValue'{}]}

Normalizes a issuer name so that it can be easily compared to another issuer name.

pkix_sign(#'OTPTBSCertificate'{}, Key) -> der_encode() Signs certificate. Key = rsa_public_key() | dsa_public_key()

Signs a 'OTPTBSCertificate'. Returns the corresponding der encoded certificate.

pkix_verify(Cert, Key) -> boolean() Verify pkix x.509 certificate signature. Cert = der_encode() Key = rsa_public_key() | dsa_public_key()

Verify pkix x.509 certificate signature.

sign(Msg, DigestType, Key) -> binary() Create digital signature. Msg = binary() The msg is either the binary "plain text" data to be signed or in the case that digest type is none it is the hashed value of "plain text" i.e. the digest. DigestType = rsa_digest_type() | dsa_digest_type() Key = rsa_private_key() | dsa_private_key()

Creates a digital signature.

ssh_decode(SshBin, Type) -> [{public_key(), Attributes::list()}] Decodes a ssh file-binary. SshBin = binary() Example {ok, SshBin} = file:read_file("known_hosts"). Type = public_key | ssh_file() If Type is public_key the binary may be either a rfc4716 public key or a openssh public key.

Decodes a ssh file-binary. In the case of know_hosts or auth_keys the binary may include one or more lines of the file. Returns a list of public keys and their attributes, possible attribute values depends on the file type represented by the binary.

rfc4716 attributes - see RFC 4716 {headers, [{string(), utf8_string()}]} auth_key attributes - see man sshd {comment, string()} {options, [string()]} {bits, integer()} - In ssh version 1 files known_host attributes - see man sshd {hostnames, [string()]} {comment, string()} {bits, integer()} - In ssh version 1 files
ssh_encode([{Key, Attributes}], Type) -> binary() Encodes a list of ssh file entries to a binary. Key = public_key() Attributes = list() Type = ssh_file()

Encodes a list of ssh file entries (public keys and attributes) to a binary. Possible attributes depends on the file type, see ssh_decode/2

verify(Msg, DigestType, Signature, Key) -> boolean() Verifies a digital signature. Msg = binary() The msg is either the binary "plain text" data or in the case that digest type is none it is the hashed value of "plain text" i.e. the digest. DigestType = rsa_digest_type() | dsa_digest_type() Signature = binary() Key = rsa_public_key() | dsa_public_key()

Verifies a digital signature