160 lines
3.2 KiB
Markdown
160 lines
3.2 KiB
Markdown
# Dummy CA - Certificate Authority in Go
|
|
|
|
Eine einfache Certificate Authority (CA) in Go, die über eine REST-API Certificate Signing Requests (CSR) entgegennimmt, signiert und Zertifikate bereitstellt.
|
|
|
|
## Features
|
|
|
|
- Automatische Generierung eines Root-Zertifikats beim Start
|
|
- REST-API zum Einreichen und Signieren von CSRs
|
|
- Abruf von signierten Zertifikaten über GET-Request
|
|
- Abruf des Root-Zertifikats
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
go mod download
|
|
```
|
|
|
|
## Starten
|
|
|
|
```bash
|
|
go run main.go
|
|
```
|
|
|
|
Der Server läuft standardmäßig auf Port 8088.
|
|
|
|
## API-Endpunkte
|
|
|
|
### POST /csr
|
|
Reicht einen CSR ein und lässt ihn signieren.
|
|
|
|
**Request Body:**
|
|
```json
|
|
{
|
|
"csr": "BASE64_ENCODED_CSR_PEM",
|
|
"action": "sign",
|
|
"validity_days": 365
|
|
}
|
|
```
|
|
|
|
**Response:**
|
|
```json
|
|
{
|
|
"id": "zertifikat-id",
|
|
"status": "success",
|
|
"message": "CSR erfolgreich signiert",
|
|
"certificate": "-----BEGIN CERTIFICATE-----\n..."
|
|
}
|
|
```
|
|
|
|
### GET /certificate/{id}
|
|
Ruft ein signiertes Zertifikat anhand der ID ab.
|
|
|
|
**Response:**
|
|
```json
|
|
{
|
|
"id": "zertifikat-id",
|
|
"certificate": "-----BEGIN CERTIFICATE-----\n...",
|
|
"created_at": "2024-01-01T12:00:00Z"
|
|
}
|
|
```
|
|
|
|
### GET /root
|
|
Ruft das Root-Zertifikat der CA ab (PEM-Format).
|
|
|
|
### GET /health
|
|
Health-Check-Endpunkt.
|
|
|
|
## Beispiel: CSR erstellen und einreichen
|
|
|
|
### 1. CSR erstellen
|
|
|
|
```bash
|
|
# Private Key generieren
|
|
openssl genrsa -out private.key 2048
|
|
|
|
# CSR erstellen
|
|
openssl req -new -key private.key -out request.csr -subj "/CN=example.com"
|
|
```
|
|
|
|
### 2. CSR in Base64 kodieren
|
|
|
|
```bash
|
|
# CSR in Base64 kodieren
|
|
CSR_B64=$(cat request.csr | base64 -w 0)
|
|
```
|
|
|
|
### 3. CSR an die CA senden
|
|
|
|
```bash
|
|
curl -X POST http://localhost:8088/csr \
|
|
-H "Content-Type: application/json" \
|
|
-d "{
|
|
\"csr\": \"$CSR_B64\",
|
|
\"action\": \"sign\",
|
|
\"validity_days\": 365
|
|
}"
|
|
```
|
|
|
|
### 4. Zertifikat abrufen
|
|
|
|
```bash
|
|
# Mit der ID aus der vorherigen Antwort
|
|
curl http://localhost:8088/certificate/{id}
|
|
```
|
|
|
|
### 5. Root-Zertifikat abrufen
|
|
|
|
```bash
|
|
curl http://localhost:8088/root > root.crt
|
|
```
|
|
|
|
## Beispiel-Skript
|
|
|
|
Ein Beispiel-Skript zum Testen der API:
|
|
|
|
```bash
|
|
#!/bin/bash
|
|
|
|
# 1. Private Key und CSR erstellen
|
|
openssl genrsa -out test.key 2048
|
|
openssl req -new -key test.key -out test.csr -subj "/CN=test.example.com"
|
|
|
|
# 2. CSR kodieren und einreichen
|
|
CSR_B64=$(cat test.csr | base64 -w 0)
|
|
RESPONSE=$(curl -s -X POST http://localhost:8088/csr \
|
|
-H "Content-Type: application/json" \
|
|
-d "{
|
|
\"csr\": \"$CSR_B64\",
|
|
\"action\": \"sign\",
|
|
\"validity_days\": 365
|
|
}")
|
|
|
|
# 3. Zertifikat-ID extrahieren
|
|
CERT_ID=$(echo $RESPONSE | jq -r '.id')
|
|
echo "Zertifikat-ID: $CERT_ID"
|
|
|
|
# 4. Zertifikat abrufen
|
|
curl -s http://localhost:8088/certificate/$CERT_ID | jq -r '.certificate' > test.crt
|
|
|
|
# 5. Zertifikat verifizieren
|
|
openssl x509 -in test.crt -text -noout
|
|
```
|
|
|
|
## Technische Details
|
|
|
|
- Root-Zertifikat: 2048-bit RSA, 10 Jahre Gültigkeit
|
|
- Signierte Zertifikate: Standardmäßig 365 Tage Gültigkeit (konfigurierbar)
|
|
- Zertifikat-Speicher: In-Memory (verloren nach Neustart)
|
|
- Serialnummern: Automatisch inkrementiert
|
|
|
|
## Sicherheitshinweise
|
|
|
|
⚠️ **Diese CA ist nur für Test- und Entwicklungszwecke gedacht!**
|
|
|
|
- Keine Persistierung der Zertifikate
|
|
- Keine Authentifizierung der API
|
|
- Keine Validierung der CSR-Inhalte
|
|
- Nicht für Produktionsumgebungen geeignet
|
|
|