1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

show how to handle private keys with passphrases in openssl example

This commit is contained in:
Rob Ede 2023-05-22 14:06:04 +01:00
parent aae806b42f
commit 48565526e7
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
4 changed files with 137 additions and 80 deletions

View File

@ -2,31 +2,40 @@
## Usage ## Usage
### Certificate ### Generating Trusted Certificate
We put the self-signed certificate in this directory as an example but your browser would complain that it isn't secure. So we recommend to use [`mkcert`] to trust it. To use local CA, you should run: We put self-signed certificate in this directory as an example but your browser will complain that connections to the server aren't secure. We recommend to use [`mkcert`] to trust it. To use a local CA, you should run:
```sh ```sh
mkcert -install mkcert -install
``` ```
If you want to generate your own cert/private key file, then run: If you want to generate your own private key/certificate pair, then run:
```sh ```sh
mkcert 127.0.0.1 mkcert 127.0.0.1 localhost
```
A new `key.pem` and `cert.pem` will be saved to the current directory. You will then need to modify `main.rs` where indicated.
### Running Server
```console
$ cd security/openssl
$ cargo run # (or `cargo watch -x run`)
starting HTTPS server at 127.0.0.1:8443
```
### Using Client
- curl: `curl -vk https://127.0.0.1:8443`
- curl (forced HTTP/1.1): `curl -vk --http1.1 https://127.0.0.1:8443`
- browser: <https://127.0.0.1:8443>
## Self-Signed Encrypted Private Key Command
```sh
openssl req -x509 -newkey rsa:4096 -keyout key-pass.pem -out cert-pass.pem -sha256 -days 365
``` ```
[`mkcert`]: https://github.com/FiloSottile/mkcert [`mkcert`]: https://github.com/FiloSottile/mkcert
### server
```sh
cd security/openssl
cargo run (or ``cargo watch -x run``)
# Started http server: 127.0.0.1:8443
```
### web client
- curl: `curl -v https://127.0.0.1:8443/index.html --compressed -k`
- browser: [https://127.0.0.1:8443/index.html](https://127.0.0.1:8443/index.html)

View File

@ -1,24 +1,29 @@
-----BEGIN CERTIFICATE----- -----BEGIN CERTIFICATE-----
MIIEDDCCAnSgAwIBAgIQLu2TV80hCgYgZe18ovEhmzANBgkqhkiG9w0BAQsFADBZ MIIE5jCCAs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hVHOP7JdVNA+6cewROyPL7ElLs66ujE9hsRvs6eXLjgLZrHOZShnoQxK Ud4EtL//DJbQpWpYbvd1NmB3Bt6XZVzcShizDn8wEem+xzhSehyTEXlGV9Shoj3G
JJv8UfoE90FX1uDt9w9i3raig/O3oePNkU263kJlR+J1rdVdYV+pCCb7L4Vk+1l3 f86nNWO2qsz6UpmaW4kYG1Y03ZulHWcx01XjAiuyTSoYIlaBhnrWFBLd7u5+f7oU
S4VFVGVHN8x35dISCJwZrtnqPlfpCiLjtEJOu1zJUEY2Q0n7Km3z3zQcs6iCeOQi raLpLyMN5T3a4ixF8vq4Go74h78le+jnWAok9xoUL0a20eEI38nWu5EuJ/ULDE4h
O9MVJ4aiALdNvyCG7lL4+AJ/kWbwHFM6wOAKSrkpZ20msMuEgIlhCOi8PgYlKb+b HQArH1cHl0G+/e0Dr5cMYrU8iXIWZy1/j2Rsc4JeNnYbYfGpV69kMhMh28WGPFqP
V/lV6IJPVrAOOclgcvtfZ/LdsTxn15yLIieqgR0Lf/s= vWQo2BrQLSqeQRKPul348OmSwiSIqTGOZ08K+9yRQVsMEtQc3L2z+a4CTdOF7C+U
exEeHCtpGgcvJOPL63qNoOsARFzuEgjPgghSjWWuPvRsro8408sDt8LGgeCVIcln
KAXsPOADRBKzafS5M70vPj0uvhG9CulHngChBRP6Mt+X9fQhzTP89GS3V58/c8cL
JY8AM0zsHbKuVzVr53cCd4feHmwhNh9ybIfkPPPoTx5WOId03sLgD1YeBXdSJDnY
+qjcQtTcROwhrUW3lH3n6LmgiIAd6xxVg6fRaaWEmfYIGWAUiKTKlj7qTxBbtrZI
5CAz3ZrF2CJVOg==
-----END CERTIFICATE----- -----END CERTIFICATE-----

View File

@ -1,28 +1,54 @@
-----BEGIN PRIVATE KEY----- -----BEGIN ENCRYPTED PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDl9FQhsS/YOMGY MIIJnzBJBgkqhkiG9w0BBQ0w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SLrdft9nyhilGekpqiTz8407ckzbIosc0
TxEI9Q93Zjj3kMC1MAtwUCCK3N/7QNQw0vft1SZ/cKNn2zZ7Slzc7HWo3NJkMfAB
39o/M+I80XGL0HUO4uWXrkBh5O9wJpnKcz+jarY5H52if/+NCFdgLo/xDCyKGcr0
c02C3NieX4ZjakKv21yxH57SEVlLr6CDpZJ/YxmMiUdFdg93xp2X3Cwl8tNuwbOG
+KfEbFnzqCxb9BZAT5isezfryXcPyOdcpeERdg8VkcvqYkhS0Rf1uOPYNY5cX16K
Wv0piAT/VbNw/wF4KsB5xIJXeiGG1730grCtQlCRXIlK9LDzm+hmHZ5cwcfgr8RG
TX3gRP9CJn9jJP7Ftq1I2V1sPD4C2Jga5Wr1HomzMRwS36hHBd2wqh29fstYnZZY
BKJnjiF3ep3sPGZ/IB9R1gLIvRcym+o3f40dU3KWII+68BOJz/j6x72cJgZWmGIu
tX90atqzCqFUwgZCtlzzN4BxjlsKLsLOxDE/VlOjlhAcKu7k7CgxBHHFoauMG1l9
HRReC0LmrD91l8GASZ4NVzAGlA==
-----END ENCRYPTED PRIVATE KEY-----

View File

@ -1,25 +1,39 @@
use std::io; use std::{
fs::File,
io::{self, Read as _},
};
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer}; use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod}; use openssl::{
pkey::{PKey, Private},
ssl::{SslAcceptor, SslMethod},
};
/// simple handle /// simple handle
async fn index(req: HttpRequest) -> Result<HttpResponse, Error> { async fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
println!("{req:?}"); println!("{req:?}");
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok()
.content_type("text/plain") .content_type("text/plain")
.body("Welcome!")) .body("Hello HTTPS World!"))
} }
#[actix_web::main] #[actix_web::main]
async fn main() -> io::Result<()> { async fn main() -> io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
// load TLS keys // build TLS config from files
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap(); let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
// set the encrypted private key
builder builder
.set_private_key_file("key.pem", SslFiletype::PEM) .set_private_key(&load_encrypted_private_key())
.unwrap(); .unwrap();
// set the unencrypted private key
// (uncomment if you generate your own key+cert with `mkcert`, and also remove the statement above)
// builder.set_private_key_file("key.pem").unwrap();
// set the certificate chain file location
builder.set_certificate_chain_file("cert.pem").unwrap(); builder.set_certificate_chain_file("cert.pem").unwrap();
log::info!("starting HTTPS server at http://localhost:8443"); log::info!("starting HTTPS server at http://localhost:8443");
@ -28,16 +42,19 @@ async fn main() -> io::Result<()> {
App::new() App::new()
// enable logger // enable logger
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
// register simple handler, handle all methods // simple root handler
.service(web::resource("/index.html").to(index)) .service(web::resource("/").route(web::get().to(index)))
// with path parameters
.service(web::resource("/").route(web::get().to(|| async {
HttpResponse::Found()
.append_header(("LOCATION", "/index.html"))
.finish()
})))
}) })
.bind_openssl("127.0.0.1:8443", builder)? .bind_openssl("127.0.0.1:8443", builder)?
.workers(2)
.run() .run()
.await .await
} }
fn load_encrypted_private_key() -> PKey<Private> {
let mut file = File::open("key.pem").unwrap();
let mut buffer = Vec::new();
file.read_to_end(&mut buffer).expect("Failed to read file");
PKey::private_key_from_pem_passphrase(&buffer, b"password").unwrap()
}