91 lines
2.2 KiB
Markdown
91 lines
2.2 KiB
Markdown
# selfsign_ca
|
|
|
|
CA-gestützte Erstellung selbst-signierter X.509-Zertifikate mit Python `cryptography`.
|
|
|
|
## Funktionsweise
|
|
|
|
Beim ersten Aufruf wird automatisch eine Root-CA erstellt (`crt/ca.key` + `crt/ca.crt`). Alle weiteren Zertifikate werden von dieser CA signiert. Existiert die CA bereits, wird sie wiederverwendet.
|
|
|
|
Jedes Zertifikat wird als Trio abgelegt:
|
|
|
|
| Datei | Inhalt |
|
|
|---|---|
|
|
| `crt/<name>.key` | RSA-2048 Private Key (PEM) |
|
|
| `crt/<name>.crt` | X.509-Zertifikat, signiert von der CA |
|
|
| `crt/<name>-chain.crt` | Server-Zertifikat + CA-Zertifikat (Chain) |
|
|
|
|
## Abhängigkeiten
|
|
|
|
```bash
|
|
pip install cryptography
|
|
```
|
|
|
|
## Verwendung
|
|
|
|
### Direktaufruf
|
|
|
|
```bash
|
|
python selfsign_ca.py
|
|
```
|
|
|
|
Erzeugt eine CA (falls nicht vorhanden) und das Zertifikat `test.*` mit den im `__main__`-Block hinterlegten Defaults.
|
|
|
|
### Als Modul
|
|
|
|
```python
|
|
from selfsign_ca import generate_and_save_cert
|
|
|
|
generate_and_save_cert(
|
|
cert_country="DE",
|
|
cert_state="Berlin",
|
|
cert_organization="Meine GmbH",
|
|
cert_locality="Berlin",
|
|
cert_organizational_unit="IT",
|
|
cert_common_name="myserver.local",
|
|
valid_days=365,
|
|
serial_number=100,
|
|
cert_name="myserver",
|
|
)
|
|
```
|
|
|
|
### CA-Parameter (optional)
|
|
|
|
Die CA-Daten werden nur beim ersten Erstellen der CA verwendet. Überschreiben via:
|
|
|
|
```python
|
|
generate_and_save_cert(
|
|
...,
|
|
ca_country="DE",
|
|
ca_state="Sachsen",
|
|
ca_organization="IchAG",
|
|
ca_locality="Dresden",
|
|
ca_common_name="My Root CA",
|
|
)
|
|
```
|
|
|
|
## Zertifikat prüfen
|
|
|
|
```bash
|
|
openssl verify -CAfile crt/ca.crt crt/<name>.crt
|
|
openssl x509 -in crt/<name>.crt -text -noout
|
|
```
|
|
|
|
## CA auf Clients importieren (Vertrauen)
|
|
|
|
Damit Clients dem Zertifikat vertrauen, muss `crt/ca.crt` im Trust-Store hinterlegt werden:
|
|
|
|
```bash
|
|
# Arch/Manjaro
|
|
sudo trust anchor crt/ca.crt
|
|
|
|
# Debian/Ubuntu
|
|
sudo cp crt/ca.crt /usr/local/share/ca-certificates/my-ca.crt
|
|
sudo update-ca-certificates
|
|
```
|
|
|
|
## Technische Details
|
|
|
|
- **CA**: RSA 4096 Bit, SHA256, 10 Jahre gültig, `basicConstraints: CA:TRUE`
|
|
- **Endzertifikat**: RSA 2048 Bit, SHA256, Gültigkeit nach `valid_days`, `basicConstraints: CA:FALSE`
|
|
- X.509 v3 Extensions: `BasicConstraints`, `KeyUsage`, `SubjectKeyIdentifier`, `AuthorityKeyIdentifier`
|
|
- Alle Dateien landen im Unterverzeichnis `crt/` (wird automatisch angelegt) |