Difference between revisions of "OpenSSL: Essentials"

From OnnoWiki
Jump to navigation Jump to search
Line 129: Line 129:
 
===Generate a Self-Signed Certificate from an Existing Private Key and CSR===
 
===Generate a Self-Signed Certificate from an Existing Private Key and CSR===
  
Use this method if you already have a private key and CSR, and you want to generate a self-signed certificate with them.
+
Gunakan metoda ini jika anda sudah mempunyai private key dan CSR, dan anda ingin membuat self-signed certificate.
  
This command creates a self-signed certificate (domain.crt) from an existing private key (domain.key) and (domain.csr):
+
Perintah ini membuat self-signed certificate (domain.crt) dari private key (domain.key) dan (domain.csr) yang ada:
  
 
  openssl x509 \
 
  openssl x509 \

Revision as of 06:41, 9 June 2017

sumber: https://www.digitalocean.com/community/tutorials/openssl-essentials-working-with-ssl-certificates-private-keys-and-csrs


Introduction

OpenSSL is a versatile command line tool that can be used for a large variety of tasks related to Public Key Infrastructure (PKI) and HTTPS (HTTP over TLS). This cheat sheet style guide provides a quick reference to OpenSSL commands that are useful in common, everyday scenarios. This includes OpenSSL examples of generating private keys, certificate signing requests, and certificate format conversion. It does not cover all of the uses of OpenSSL.

Cara menggunakan panduan ini:

  • Jika Anda tidak terbiasa dengan Certificate Signing Request (CSR), baca bagian pertama.
  • Selain bagian pertama, panduan ini ada dalam format cheat sheet sederhana -- cuplikan contoh perintah
  • Loncat ke menu yang anda butuhkan
  • Kebanyakan perintah yang digunakan adalah satu kalimat. Di expand menjadi banyak kalimat menggunakan simbol \ untuk memudahkan dalam membaca.

Tentang Certificate Signing Request (CSR)

Jika anda ingin mendapatkan sertifikat SSL dari otoritas sertifikat (CA), anda harus membuat Certificate Signing Request (CSR). CSR terdiri dari kunci publik dari pasangan kunci, dan beberapa informasi tambahan. Kedua komponen ini dimasukkan ke dalam sertifikat saat ditandatangani.

Kapan pun anda membuat CSR, anda akan diminta untuk memberikan informasi mengenai sertifikat tersebut. Informasi ini dikenal dengan nama Distinguised Name (DN). Bagian penting di DN adalah Common Name (CN), yang seharusnya merupakan Fully Qualified Domain Name (FQDN) dari host yang ingin anda gunakan dengan sertifikat. Juga memungkinkan untuk melewatkan petunjuk interaktif saat membuat CSR dengan menyampaikan informasi melalui baris perintah atau dari sebuah file..

Parameter lainnya di DN memberikan informasi tambahan tentang bisnis atau organisasi anda. Jika anda membeli sertifikat SSL dari otoritas sertifikat, sering diperlukan agar bidang tambahan ini, seperti "Organisasi", mencerminkan secara akurat rincian organisasi anda.

Berikut adalah contoh dari apa informasi CSR akan terlihat seperti:

---
Country Name (2 letter code) [AU]:US
State or Province Name (full name) [Some-State]:New York
Locality Name (eg, city) []:Brooklyn
Organization Name (eg, company) [Internet Widgits Pty Ltd]:Example Brooklyn Company
Organizational Unit Name (eg, section) []:Technology Division
Common Name (e.g. server FQDN or YOUR name) []:examplebrooklyn.com
Email Address []:

Jika anda ingin secara noninteraktif menjawab permintaan informasi CSR, anda dapat melakukannya dengan menambahkan opsi -subj ke perintah OpenSSL yang meminta informasi CSR. Berikut adalah contoh pilihannya, dengan menggunakan informasi yang sama yang ditampilkan di blok kode di atas:

-subj "/C=US/ST=New York/L=Brooklyn/O=Example Brooklyn Company/CN=examplebrooklyn.com"

Generating CSR

Bakan ini akan membahas tentang perintah OpenSSL yang berhubungan dengan generating CSR (dan private key, jika belum ada). CSR digunakan untuk permohonan certificate SSL dari Certificate Authority (CA).

Jika anda ingin memasukan informasi CSR secara non-interaktif dapat menggunakan opsi -subj

Generate Private Key dan CSR

Gunakan metoda ini jika anda ingin menggunakan HTTPS (HTTP over TLS) untuk mengamankan Apache HTTP atau Nginx web server, dan anda menginginkan untuk menggunakan Certificate Authority (CA) untuk issue SSL certificate. CSR yang dihasilkan dapat dikirim ke CA untuk meminta penerbitan sertifikat SSL yang ditandatangani CA. Jika CA anda mendukung SHA-2, tambahkan opsi -sha256 untuk menandatangani CSR dengan SHA-2.

Perintah berikut akan membuat 2048-bit private key (domain.key) dan CSR (domain.csr) dari nol:

openssl req \
       -newkey rsa:2048 -nodes -keyout domain.key \
       -out domain.csr

Jawab informasi yang dibutuhkan CSR untuk menyelesaikan proses.

  • -newkey rsa:2048 mensyaratkan agar key menggunakan 2048-bit, dan dibuat menggunakan algoritma RSA .
  • -nodes mensyaratkan bahwa private key tidak di enkripsi dengan pass phrase.
  • -new - tidak di lakukan dalam contoh di atas, mengindikasikan bahwa CSR harus dibuat.

Generate a CSR from an Existing Private Key

Gunakan metoda ini jika anda sudah memiliki private key yang ingin anda gunakan untuk request sebuah certificate dari CA.

Perintah ini akan membuat CSR baru (domain.csr) berbasis pada private key (domain.key) yang sudah ada:

openssl req \
       -key domain.key \
       -new -out domain.csr

Jawab informasi yang dibutuhkan CSR untuk menyelesaikan proses.

  • -key mensyaratkan private key (domain.key) yang ada akan digunakan untuk membuat CSR baru.
  • -new mengindikasikan bahwa CSR harus dibuat.

Generate a CSR from an Existing Certificate and Private Key

Gunakan metoda ini jika anda ingin me-renew certificate yang ada tapi anda & CA anda tidak mempunyai CSR yang asli karena satu dan lain hal. Ini pada dasarnya akan memudahkan untuk tidak memasukan ulang informasi CSR, karena dia akan mengekstrak dari certificate yang ada.

Perintah ini pada dasarnya membuat CSR baru (domain.csr) berbasis pada certificate yang ada (domain.crt) dan private key (domain.key):

openssl x509 \
       -in domain.crt \
       -signkey domain.key \
       -x509toreq -out domain.csr

Opsi -x509toreq mengindikasikan kalau kita menggunakan certificate X509 untuk membuat CSR.

Generating SSL Certificates

Jika anda ingin menggunakan sertifikat SSL untuk mendapatkan layanan namun anda tidak memerlukan sertifikat yang ditandatangani oleh CA, solusi yang sah (dan gratis) adalah menandatangani sertifikat anda sendiri.

Jenis sertifikat yang umum yang dapat anda gunakan sendiri adalah sertifikat yang ditandatangani sendiri. Sertifikat yang ditandatangani sendiri adalah sertifikat yang ditandatangani dengan kunci privatnya sendiri. Sertifikat yang ditandatangani sendiri dapat digunakan untuk mengenkripsi data sama seperti sertifikat yang ditandatangani oleh CA, namun pengguna Anda akan ditampilkan peringatan yang mengatakan bahwa sertifikat tersebut tidak dipercaya oleh komputer atau browser mereka. Oleh karena itu, sertifikat yang ditandatangani sendiri hanya boleh digunakan jika anda tidak perlu membuktikan identitas layanan anda kepada penggunanya (misalnya server non-produksi atau non-publik).

Bagian ini mencakup perintah OpenSSL yang terkait dengan pembuatan sertifikat self-signed.

Generate a Self-Signed Certificate

Gunakan metode ini jika anda ingin menggunakan HTTPS (HTTP over TLS) untuk mengamankan server web Apache HTTP atau Nginx anda, dan anda tidak mengharuskan sertifikat anda ditandatangani oleh CA.

Perintah ini akan membuat 2048-bit private key (domain.key) dan self-signed certificate (domain.crt) dari NOL:

openssl req \
       -newkey rsa:2048 -nodes -keyout domain.key \
       -x509 -days 365 -out domain.crt

Jawab informasi yang dibutuhkan CSR untuk menyelesaikan proses.

  • Opsi -x509 memberitahukan req untuk membuat self-signed cerificate.
  • Opsi -days 365 mensyaratkan certificate akan valid untuk 365 days.
  • Temporary CSR akan dibuat untuk mengumpulkan informasi terkait dengan certificate.

Generate a Self-Signed Certificate from an Existing Private Key

Gunakan metoda ini jika anda sudah mempunyai private key yang ingin digunakan untuk membuat self-signed certificate .

Perintah berikut akan membuat self-signed certificate (domain.crt) dari private key (domain.key) yang ada:

openssl req \
       -key domain.key \
       -new \
       -x509 -days 365 -out domain.crt

Jawab informasi yang dibutuhkan CSR untuk menyelesaikan proses.

  • Opsi -x509 memberitahukan req untuk membuat self-signed cerificate.
  • Opsi -days 365 mensyaratkan certificate akan valid untuk 365 days.
  • Opsi -new mengaktifkan CSR information prompt.

Generate a Self-Signed Certificate from an Existing Private Key and CSR

Gunakan metoda ini jika anda sudah mempunyai private key dan CSR, dan anda ingin membuat self-signed certificate.

Perintah ini membuat self-signed certificate (domain.crt) dari private key (domain.key) dan (domain.csr) yang ada:

openssl x509 \
       -signkey domain.key \
       -in domain.csr \
       -req -days 365 -out domain.crt

The -days 365 option specifies that the certificate will be valid for 365 days.

View Certificates

Certificate and CSR files are encoded in PEM format, which is not readily human-readable.

This section covers OpenSSL commands that will output the actual entries of PEM-encoded files.

View CSR Entries

This command allows you to view and verify the contents of a CSR (domain.csr) in plain text:

openssl req -text -noout -verify -in domain.csr

View Certificate Entries

This command allows you to view the contents of a certificate (domain.crt) in plain text:

openssl x509 -text -noout -in domain.crt

Verify a Certificate was Signed by a CA

Use this command to verify that a certificate (domain.crt) was signed by a specific CA certificate (ca.crt):

openssl verify -verbose -CAFile ca.crt domain.crt


Private Keys

This section covers OpenSSL commands that are specific to creating and verifying private keys.

Create a Private Key

Use this command to create a password-protected, 2048-bit private key (domain.key):

openssl genrsa -des3 -out domain.key 2048

Enter a password when prompted to complete the process.

Verify a Private Key

Use this command to check that a private key (domain.key) is a valid key:

openssl rsa -check -in domain.key

If your private key is encrypted, you will be prompted for its pass phrase. Upon success, the unencrypted key will be output on the terminal.

Verify a Private Key Matches a Certificate and CSR

Use these commands to verify if a private key (domain.key) matches a certificate (domain.crt) and CSR (domain.csr):

openssl rsa -noout -modulus -in domain.key | openssl md5
openssl x509 -noout -modulus -in domain.crt | openssl md5
openssl req -noout -modulus -in domain.csr | openssl md5

If the output of each command is identical there is an extremely high probability that the private key, certificate, and CSR are related.

Encrypt a Private Key

This takes an unencrypted private key (unencrypted.key) and outputs an encrypted version of it (encrypted.key):

openssl rsa -des3 \
       -in unencrypted.key \
       -out encrypted.key

Enter your desired pass phrase, to encrypt the private key with.

Decrypt a Private Key

This takes an encrypted private key (encrypted.key) and outputs a decrypted version of it (decrypted.key):

openssl rsa \
       -in encrypted.key \
       -out decrypted.key

Enter the pass phrase for the encrypted key when prompted.

Convert Certificate Formats

All of the certificates that we have been working with have been X.509 certificates that are ASCII PEM encoded. There are a variety of other certificate encoding and container types; some applications prefer certain formats over others. Also, many of these formats can contain multiple items, such as a private key, certificate, and CA certificate, in a single file.

OpenSSL can be used to convert certificates to and from a large variety of these formats. This section will cover a some of the possible conversions.

Convert PEM to DER

Use this command if you want to convert a PEM-encoded certificate (domain.crt) to a DER-encoded certificate (domain.der), a binary format:

openssl x509 \
       -in domain.crt \
       -outform der -out domain.der

The DER format is typically used with Java.

Convert DER to PEM

Use this command if you want to convert a DER-encoded certificate (domain.der) to a PEM-encoded certificate (domain.crt):

openssl x509 \
       -inform der -in domain.der \
       -out domain.crt

Convert PEM to PKCS7

Use this command if you want to add PEM certificates (domain.crt and ca-chain.crt) to a PKCS7 file (domain.p7b):

openssl crl2pkcs7 -nocrl \
       -certfile domain.crt \
       -certfile ca-chain.crt \
       -out domain.p7b

Note that you can use one or more -certfile options to specify which certificates to add to the PKCS7 file.

PKCS7 files, also known as P7B, are typically used in Java Keystores and Microsoft IIS (Windows). They are ASCII files which can contain certificates and CA certificates.

Convert PKCS7 to PEM

Use this command if you want to convert a PKCS7 file (domain.p7b) to a PEM file:

openssl pkcs7 \
       -in domain.p7b \
       -print_certs -out domain.crt

Note that if your PKCS7 file has multiple items in it (e.g. a certificate and a CA intermediate certificate), the PEM file that is created will contain all of the items in it.

Convert PEM to PKCS12

Use this command if you want to take a private key (domain.key) and a certificate (domain.crt), and combine them into a PKCS12 file (domain.pfx):

openssl pkcs12 \
       -inkey domain.key \
       -in domain.crt \
       -export -out domain.pfx

You will be prompted for export passwords, which you may leave blank. Note that you may add a chain of certificates to the PKCS12 file by concatenating the certificates together in a single PEM file (domain.crt) in this case.

PKCS12 files, also known as PFX files, are typically used for importing and exporting certificate chains in Micrsoft IIS (Windows).

Convert PKCS12 to PEM

Use this command if you want to convert a PKCS12 file (domain.pfx) and convert it to PEM format (domain.combined.crt):

openssl pkcs12 \
       -in domain.pfx \
       -nodes -out domain.combined.crt

Note that if your PKCS12 file has multiple items in it (e.g. a certificate and private key), the PEM file that is created will contain all of the items in it.

OpenSSL Version

The openssl version command can be used to check which version you are running. The version of OpenSSL that you are running, and the options it was compiled with affect the capabilities (and sometimes the command line options) that are available to you.

The following command displays the OpenSSL version that you are running, and all of the options that it was compiled with:

openssl version -a

This guide was written using an OpenSSL binary with the following details (the output of the previous command):

OpenSSL 1.0.2g-fips  1 Mar 2016
built on: reproducible build, date unspecified
platform: debian-amd64
options:  bn(64,64) rc4(16x,int) des(idx,cisc,16,int) blowfish(idx) 
compiler: cc -I. -I.. -I../include  -fPIC -DOPENSSL_PIC -DOPENSSL_THREADS -D_REENTRANT -DDSO_DLFCN -DHAVE_DLFCN_H -m64 -DL_ENDIAN -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -Wl,-Bsymbolic-functions -Wl,-z,relro -Wa,--noexecstack -Wall -DMD32_REG_T=int -DOPENSSL_IA32_SSE2 -DOPENSSL_BN_ASM_MONT -DOPENSSL_BN_ASM_MONT5 -DOPENSSL_BN_ASM_GF2m -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DMD5_ASM -DAES_ASM -DVPAES_ASM -DBSAES_ASM -DWHIRLPOOL_ASM -DGHASH_ASM -DECP_NISTZ256_ASM
OPENSSLDIR: "/usr/lib/ssl"

Referensi