File size: 6,669 Bytes
0e0fe0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
## HTTPS Certificate Management Configuration Guide

### Overview
This application now includes enterprise-grade HTTPS certificate management with automatic renewal capabilities. The system can:

- ✅ Automatically create self-signed certificates for development
- ✅ Validate certificates on startup
- ✅ Automatically renew certificates before expiration
- ✅ Backup old certificates before renewal
- ✅ Expose certificate status via API endpoints
- ✅ Support custom certificate paths for production environments
- ✅ Provide detailed logging for certificate operations

### Configuration

The HTTPS certificate settings are configured in `appsettings.json`:

```json

{

  "HttpSettings": {

    "HttpPort": 5293,

    "HttpsPort": 7145

  },

  "HttpsCertificate": {

    "Enabled": true,

    "CertificatePath": "Data/certificates",

    "CertificateFileName": "port-watcher-cert",

    "CertificateFormat": "pfx",

    "SubjectName": "CN=localhost",

    "ValidityDays": 365,

    "AutoRenewThresholdDays": 30,

    "CheckIntervalHours": 24,

    "UseProductionCert": false,

    "ProductionCertPath": "",

    "EnableAutoRenewal": true

  }

}

```

### Configuration Options

| Option | Default | Description |
|--------|---------|-------------|
| `Enabled` | true | Enable/disable certificate management |
| `CertificatePath` | Data/certificates | Relative path where certificates are stored |
| `CertificateFileName` | port-watcher-cert | Name of the certificate file (without extension) |
| `CertificateFormat` | pfx | Certificate format (pfx/pem/cer) |
| `SubjectName` | CN=localhost | Certificate subject name |
| `ValidityDays` | 365 | Validity period for new certificates |
| `AutoRenewThresholdDays` | 30 | Days before expiration to trigger renewal |
| `CheckIntervalHours` | 24 | Interval for automatic renewal checks |
| `UseProductionCert` | false | Use production certificate instead of self-signed |
| `ProductionCertPath` | "" | Path to production certificate file |
| `EnableAutoRenewal` | true | Enable automatic certificate renewal |

### Development Environment

For development (appsettings.Development.json):
```json

{

  "HttpsCertificate": {

    "UseProductionCert": false,

    "EnableAutoRenewal": false,

    "SubjectName": "CN=localhost"

  }

}

```

### Production Environment

For production, you have two options:

#### Option 1: Use Self-Signed Certificates with Auto-Renewal
```json

{

  "HttpsCertificate": {

    "Enabled": true,

    "EnableAutoRenewal": true,

    "SubjectName": "CN=yourdomain.com",

    "AutoRenewThresholdDays": 30,

    "CheckIntervalHours": 24

  }

}

```

#### Option 2: Use Production Certificate (e.g., Let's Encrypt)
```json

{

  "HttpsCertificate": {

    "UseProductionCert": true,

    "ProductionCertPath": "C:\\path\\to\\your\\certificate.pfx",

    "EnableAutoRenewal": false

  }

}

```

### API Endpoints

The application exposes certificate diagnostics via REST API:

#### Get Certificate Status
```

GET /api/certificatediagnostics/status

```

Response:
```json

{

  "success": true,

  "data": {

    "subjectName": "CN=localhost",

    "issuedDate": "2026-03-03",

    "expirationDate": "2027-03-03",

    "daysUntilExpiration": 365,

    "isExpired": false,

    "needsRenewal": false,

    "thumbprint": "ABC123..."

  }

}

```

#### Validate and Auto-Renew
```

POST /api/certificatediagnostics/validate

```

#### Manual Certificate Renewal
```

POST /api/certificatediagnostics/renew

```

### Automatic Renewal Process

1. **Startup Check**: Certificate is validated when the application starts
2. **Background Task**: Certificate is checked every 24 hours (configurable)
3. **Renewal Trigger**: If certificate expires within the threshold (30 days), renewal is triggered
4. **Backup**: Old certificate is backed up with timestamp before renewal
5. **Logging**: All operations are logged for audit trail

### Certificate Files Location

Certificates are stored in: `Data/certificates/port-watcher-cert.pfx`

Backup certificates use the naming convention: `port-watcher-cert.pfx.backup-20260303-120000`

### Monitoring Certificate Status

Monitor the application logs for certificate-related messages:

```

[INF] HTTPS certificate found at Data/certificates/port-watcher-cert.pfx

[INF] Certificate Info: Subject=CN=localhost, Issued=2026-03-03, Expires=2027-03-03, DaysRemaining=365, Thumbprint=ABC123...

[INF] Running automatic certificate validation check...

[WRN] HTTPS certificate expires in 25 days. Auto-renewal will be triggered.

[INF] HTTPS certificate renewed successfully.

```

### Troubleshooting

#### HTTPS Port Not Determined Error
The warning "Failed to determine the https port for redirect" should now be fixed by:
- Explicit Kestrel configuration in Program.cs
- Proper port settings in appsettings.json
- Enhanced HSTS configuration

#### Certificate Not Found
- Check that `Data/certificates/` directory exists or will be created on startup
- Verify write permissions for the application process
- Check logs for detailed error messages

#### Cannot Load Certificate
- Ensure certificate file is valid PFX format
- Check file permissions
- Verify certificate hasn't been corrupted

#### Auto-Renewal Not Triggering
- Verify `EnableAutoRenewal` is set to `true`
- Check `AutoRenewThresholdDays` value (default: 30 days)
- Check application logs for renewal task status
- Manually trigger renewal via API: `POST /api/certificatediagnostics/renew`

### Security Considerations

1. **Certificate Storage**: Keep certificate files in a secure location with restricted access
2. **Backup Strategy**: Automatic backups are created during renewal - maintain offline backups for production
3. **Key Protection**: Private keys are stored in PFX format - ensure the process has correct permissions
4. **Logging**: Certificate operations are logged - review logs regularly for anomalies
5. **Production Certs**: Use proper SSL certificates from trusted CAs in production environments

### Integration with eSecSys/nCONNECT OS

For integration with enterprise certificate management systems:

1. Set `UseProductionCert` to `true`
2. Point `ProductionCertPath` to the provisioned certificate location
3. Configure your certificate management system to update this path
4. Set `EnableAutoRenewal` to `false` to allow external management

The API endpoints can be called by your enterprise certificate management system to validate certificate status and trigger renewals when needed.

---

**Last Updated**: March 3, 2026