2022-11-12 23:18:06 +01:00
|
|
|
# OpenSSL Cheatsheet
|
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
## Read X.509 Certificate
|
|
|
|
|
2024-02-18 21:09:29 +01:00
|
|
|
A certificate can be read via
|
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
```sh
|
|
|
|
openssl x509 -in $CERT -text
|
|
|
|
```
|
|
|
|
|
|
|
|
## Generate CSR
|
|
|
|
|
2024-02-18 21:09:29 +01:00
|
|
|
A Certificate Signing Request needs a private alongside the request for a cert.
|
2023-02-09 21:31:25 +01:00
|
|
|
This is done in the following way
|
2024-02-18 21:09:29 +01:00
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
```sh
|
|
|
|
openssl req -new -nodes -newkey rsa:4096 -keyout $PRIVATE_KEY -out $CERT_CSR
|
|
|
|
```
|
|
|
|
|
|
|
|
## Create an X.509 Certificate
|
|
|
|
|
2024-02-18 21:09:29 +01:00
|
|
|
Create a X.509 certificate via
|
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
```sh
|
|
|
|
openssl x509 -newkey -nodes rsa:4096 -keyout $PRIVATE_KEY -out $CERT -sha256 -days 365
|
|
|
|
openssl req -new -x509 -keyout cert.pem -out cert.pem -days 365 -nodes
|
|
|
|
```
|
|
|
|
|
|
|
|
## Extract Keys from PFX Cert
|
2022-11-12 23:18:06 +01:00
|
|
|
|
2024-02-18 21:09:29 +01:00
|
|
|
Key and cert form PFX
|
|
|
|
|
2022-11-12 23:18:06 +01:00
|
|
|
```sh
|
|
|
|
openssl pkcs12 -in cert.pfx -nocerts -out key.pem -nodes
|
|
|
|
openssl pkcs12 -in cert.pfx -out cert.pem -clcerts -nokeys
|
|
|
|
```
|
|
|
|
|
|
|
|
## Extract & Repack PFX Cert
|
|
|
|
|
2024-02-18 21:09:29 +01:00
|
|
|
Extract & Repack with another password, e.g. from `mimikatz` to `cqure`
|
|
|
|
|
2022-11-12 23:18:06 +01:00
|
|
|
```sh
|
|
|
|
openssl pkcs12 -in *.pfx -out temp.pem -nodes
|
|
|
|
openssl pkcs12 -export -out *.pfx -in temp.pem
|
|
|
|
```
|
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
## RSA
|
2022-11-12 23:18:06 +01:00
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
### Read Parameters of a RSA Key
|
|
|
|
|
2024-02-18 21:09:29 +01:00
|
|
|
Show parameters of the private key
|
|
|
|
|
2022-11-12 23:18:06 +01:00
|
|
|
```sh
|
2023-02-09 21:31:25 +01:00
|
|
|
openssl rsa -in $PRIVATE_KEY -text -noout
|
|
|
|
```
|
|
|
|
|
|
|
|
### Create RSA Key
|
|
|
|
|
2024-02-18 21:09:29 +01:00
|
|
|
Generate an OpenSSL RSA key via
|
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
```sh
|
|
|
|
openssl genrsa -out $PRIVATE_KEY 4096
|
|
|
|
```
|
|
|
|
|
2024-02-18 21:09:29 +01:00
|
|
|
Generate an OpenSSl RSA public key from a private key
|
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
```sh
|
|
|
|
openssl rsa -in $PRIVATE_KEY -pubout -out public-key.pem
|
|
|
|
```
|
|
|
|
|
|
|
|
### Encrypt RSA
|
|
|
|
|
2024-02-18 21:09:29 +01:00
|
|
|
Encrypt RSA current and deprecated
|
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
```sh
|
|
|
|
openssl pkeyutl -encrypt -in $CLEAR_TEXT -out $CLEAR_TEXT -pubin -inkey $PUBLIC_KEY
|
|
|
|
openssl rsautl -encrypt -in $CLEAR_TEXT -out $ENCRYPTED -pubin -inkey $PUBLIC_KEY
|
2022-11-12 23:18:06 +01:00
|
|
|
```
|
2023-01-02 20:28:19 +01:00
|
|
|
|
|
|
|
### Decrypt RSA
|
|
|
|
|
2024-02-18 21:09:29 +01:00
|
|
|
Decrypt a RSA cipher with the private key
|
|
|
|
|
2023-01-02 20:28:19 +01:00
|
|
|
```sh
|
2023-02-09 21:31:25 +01:00
|
|
|
openssl pkeyutl -decrypt -in $CIPHER -out $PLAIN_TEXT -inkey $PRIVATE_KEY
|
2023-01-02 20:28:19 +01:00
|
|
|
```
|
|
|
|
|
2024-02-18 21:09:29 +01:00
|
|
|
Deprecated version of RSA decryption is the following
|
|
|
|
|
2023-01-02 20:28:19 +01:00
|
|
|
```sh
|
2023-02-09 21:31:25 +01:00
|
|
|
openssl rsautl -decrypt -in $CIPHER -out $PLAIN_TEXT -inkey $PRIVATE_KEY
|
2023-01-02 20:28:19 +01:00
|
|
|
```
|
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
## Diffie-Hellman
|
2023-01-02 20:28:19 +01:00
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
### Read Parameters of a DH Keys
|
|
|
|
|
2024-02-18 21:23:15 +01:00
|
|
|
Output of a DH key is done the following way
|
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
```sh
|
|
|
|
openssl dhparam -in $PRIVATE_KEY -text -noout
|
|
|
|
```
|
|
|
|
|
|
|
|
### Create DH Key
|
|
|
|
|
2024-02-18 21:23:15 +01:00
|
|
|
A Diffie-Hellman key can be created via
|
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
```sh
|
|
|
|
openssl dhparam -out $PRIVATE_KEY 4096
|
|
|
|
```
|
|
|
|
|
|
|
|
## AES
|
|
|
|
|
|
|
|
### Encrypt AES
|
|
|
|
|
2024-02-18 21:23:15 +01:00
|
|
|
Encrypt AES
|
2023-02-09 21:31:25 +01:00
|
|
|
|
|
|
|
```sh
|
|
|
|
openssl aes-256-cbc -e -in $PLAIN_TEXT -out $CIPHER
|
|
|
|
```
|
|
|
|
|
|
|
|
### Decrypt AES
|
|
|
|
|
2024-02-18 21:23:15 +01:00
|
|
|
Decrypt AES
|
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
```sh
|
|
|
|
openssl aes-256-cbc -d -in $CIPHER -out $PLAIN_TEXT
|
|
|
|
```
|
|
|
|
|
|
|
|
## PBKDF2
|
|
|
|
|
|
|
|
### Encrypt PBKDF2
|
|
|
|
|
2024-02-18 21:23:15 +01:00
|
|
|
Encrypt file via PBKDF2 with 128000 iterations
|
|
|
|
|
2023-02-09 21:31:25 +01:00
|
|
|
```sh
|
|
|
|
openssl aes-256-cbc -pbkdf2 -iter 128000 -e -in $PLAIN_TEXT -out $CIPHER
|
|
|
|
```
|
|
|
|
|
|
|
|
### Decrypt PBKDF2
|
|
|
|
|
2024-02-18 21:23:15 +01:00
|
|
|
Decrypt file via PBKDF2 with an iteration of 128000
|
|
|
|
|
2023-01-02 20:28:19 +01:00
|
|
|
```sh
|
2023-02-09 21:31:25 +01:00
|
|
|
openssl aes-256-cbc -pbkdf2 -iter 128000 -d -in $CIPHER -out $PLAIN_TEXT
|
2023-01-02 20:28:19 +01:00
|
|
|
```
|
2023-02-16 23:15:47 +01:00
|
|
|
|
|
|
|
## ECPoint (EC)
|
|
|
|
|
|
|
|
* RFC5480
|
|
|
|
|
|
|
|
### Read PEM Public Key
|
|
|
|
|
|
|
|
```sh
|
|
|
|
openssl ec -pubin -in publickey.pem -noout -text
|
|
|
|
```
|