This article describes the steps to take to generate your own keys and certificates for your system.
Since the 1.3 branch, mbed TLS also includes the core and applications for generating keys and certificates without relying on other libraries and applications, offering users a command-line alternative to OpenSSL for generating their keys and (self-signed) certificates.
This article assumes you have compiled (and optionally installed) the mbed TLS library on your system.
Generating a RSA key file
The first step for generating a self-signed certificate, is to generate a private/public keypair for the certificate.
For generating key files, mbed TLS includes the gen_key application (located in programs/pkey).
This key generation application accepts the following arguments:
usage: gen_key param=<>... acceptable parameters: type=rsa default: rsa rsa_keysize=%d default: 4096 filename=%s default: keyfile.key format=pem|der default: pem
The following command generates a 4096 bit RSA key file for us:
programs/pkey/gen_key type=rsa rsa_keysize=4096 filename=our_key.key
Generating a self-signed certificate
For generating and writing certificate files, mbed TLS includes the cert_write application (located in programs/x509).
Before generating the self-signed certificate we need to determine the different values that need to go in it.
In case of a self-signed certificate, we need to indicate so with
Key to use in the certificate
First and foremost a certificate binds a public-private keypair to an identity. To indicate which key to use to sign the certificate we use the
issuer_key argument, like so
Because this is a self-signed certificate the subject_key is automatically identical.
Subject name / Issuer name
Each certificate is identified with a subject name (the identity that is being signed) and an issuer name (the party doing the signing). In the case of a self-signed certificate only the issuer name is used.
In case we want to name the issuer 'myserver' from the organisation 'myorganisation' and the country 'NL', we should use
issuer_name=CN=myserver,O=myorganisation,C=NL on the command-line.
Note: If you want to use a space in one of the names you have to either escape it (
issuer_name=CN=my\ server) or contain the entire argument in quotes (
Note 2: Comas inside names need to be escaped with a backslash too. You need to protect the backslash from you shell, eg
The available items you can put in a subject_name (that we support) are:
- C = Country
- CN = Common Name
- L = Locality
- O = Organisation
- OU = Organisational Unit
- R = e-mail address
- ST = State
Each certificate contains within it, the validity period for that certificate indicated by the not_before and not_after values.
The applications accepts values of the format YYYYMMDDHHMMSS, so to indicate that a certificate is not valid before 2013, you can use
not_before=20130101000000 and not valid after 2015, you can use
Is it a CA certificate?
If we want this self-signed certificate to be a CA certificate (capable of signing other certificates) we can enable it with
We then have to decide if this CA certificate can only sign regular certificates (
max_pathlen=0) or that it is at the top of other intermediate CA certificates.
Command to generate a self-signed certificate
So the full command for generating a self-signed CA certificate that cannot sign other CA certificates for our_key.key with the name 'CN=myserver, O=myorganisation, C=NL' and valid only in the years 2013, 2014 and 2015 would be:
programs/x509/cert_write selfsign=1 issuer_key=our_key.key \ issuer_name=CN=myserver,O=myorganisation,C=NL \ not_before=20130101000000 not_after=20151231235959 \ is_ca=1 max_pathlen=0 output_file=my_crt.crt
And you are done!
The file my_crt.crt now contains your (PEM encoded) certificate.
Key usage and NS cert type extension
You can set the key usage extension and the NS Cert type extension in the certificate on the command-line as well with:
key_usage=%s default: (empty) Comma-separated-list of values: digital_signature non_repudiation key_encipherment data_encipherment key_agreement key_certificate_sign crl_sign ns_cert_type=%s default: (empty) Comma-separated-list of values: ssl_client ssl_server email object_signing ssl_ca email_ca object_signing_ca
Need to be able to set other certificate related values as well?
Let us know! We are adding new features as we go.