-
Notifications
You must be signed in to change notification settings - Fork 104
2195 lines (1969 loc) · 96.9 KB
/
windows-cert-store-test.yml
File metadata and controls
2195 lines (1969 loc) · 96.9 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
name: Windows Certificate Store Test
# This workflow tests MS Certificate Store integration for WolfSSH
# It tests 4 combinations:
# 1. Server using cert store key, Client using cert store key
# 2. Server using cert store key, Client using file-based key (interop)
# 3. Server using file-based key, Client using cert store key (interop)
# 4. Server using file-based key, Client using file-based key (baseline)
on:
push:
branches: [ 'master', 'main', 'release/**' ]
pull_request:
branches: [ '*' ]
env:
WOLFSSL_SOLUTION_FILE_PATH: wolfssl64.sln
SOLUTION_FILE_PATH: wolfssh.sln
USER_SETTINGS_H_NEW: wolfssh/ide/winvs/user_settings.h
USER_SETTINGS_H: wolfssl/IDE/WIN/user_settings.h
INCLUDE_DIR: wolfssh
WOLFSSL_BUILD_CONFIGURATION: Release
WOLFSSH_BUILD_CONFIGURATION: Release
BUILD_PLATFORM: x64
TARGET_PLATFORM: 10
TEST_PORT: 22222
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
with:
repository: wolfssl/wolfssl
path: wolfssl
- uses: actions/checkout@v4
with:
path: wolfssh
- name: Add MSBuild to PATH
uses: microsoft/setup-msbuild@v1
- name: Restore wolfSSL NuGet packages
working-directory: ${{ github.workspace }}\wolfssl
run: nuget restore ${{env.WOLFSSL_SOLUTION_FILE_PATH}}
- name: updated user_settings.h for sshd and x509
working-directory: ${{env.GITHUB_WORKSPACE}}
shell: bash
run: |
# Enable SSHD, SFTP, and X509 support (including WOLFSSH_NO_FPKI)
sed -i 's/#if 0/#if 1/g' ${{env.USER_SETTINGS_H_NEW}}
cp ${{env.USER_SETTINGS_H_NEW}} ${{env.USER_SETTINGS_H}}
# Verify WOLFSSH_NO_FPKI will be defined
if grep -q "WOLFSSH_NO_FPKI" ${{env.USER_SETTINGS_H}}; then
echo "WOLFSSH_NO_FPKI found in user_settings.h"
else
echo "WARNING: WOLFSSH_NO_FPKI not found in user_settings.h"
fi
- name: Build wolfssl library
working-directory: ${{ github.workspace }}\wolfssl
run: msbuild /m /p:PlatformToolset=v142 /p:Platform=${{env.BUILD_PLATFORM}} /p:Configuration=${{env.WOLFSSL_BUILD_CONFIGURATION}} /t:wolfssl ${{env.WOLFSSL_SOLUTION_FILE_PATH}}
- name: Upload wolfSSL build artifacts
uses: actions/upload-artifact@v4
with:
name: wolfssl-windows-build
if-no-files-found: warn
retention-days: 1
path: |
wolfssl/IDE/WIN/${{env.WOLFSSL_BUILD_CONFIGURATION}}/${{env.BUILD_PLATFORM}}/**
wolfssl/IDE/WIN/${{env.WOLFSSL_BUILD_CONFIGURATION}}/**
wolfssl/${{env.WOLFSSL_BUILD_CONFIGURATION}}/${{env.BUILD_PLATFORM}}/**
wolfssl/${{env.WOLFSSL_BUILD_CONFIGURATION}}/**
- name: Restore NuGet packages
working-directory: ${{ github.workspace }}\wolfssh\ide\winvs
run: nuget restore ${{env.SOLUTION_FILE_PATH}}
- name: Build wolfssh
working-directory: ${{ github.workspace }}\wolfssh\ide\winvs
run: msbuild /m /p:PlatformToolset=v142 /p:Platform=${{env.BUILD_PLATFORM}} /p:WindowsTargetPlatformVersion=${{env.TARGET_PLATFORM}} /p:Configuration=${{env.WOLFSSH_BUILD_CONFIGURATION}} ${{env.SOLUTION_FILE_PATH}}
- name: Upload wolfSSH build artifacts
uses: actions/upload-artifact@v4
with:
name: wolfssh-windows-build
if-no-files-found: error
path: |
wolfssh/ide/winvs/**/Release/**
- name: Create PowerShell script to import cert to store
working-directory: ${{ github.workspace }}\wolfssh
run: |
@"
# Import certificate and key to Windows Certificate Store
param(
[string]$CertPath,
[string]$KeyPath,
[string]$StoreName = "My",
[string]$SubjectName,
[string]$StoreLocation = "CurrentUser"
)
`$ErrorActionPreference = "Stop"
# Convert DER to Base64 for import
`$certBytes = [System.IO.File]::ReadAllBytes(`$CertPath)
`$certBase64 = [System.Convert]::ToBase64String(`$certBytes)
# Create certificate object
`$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
`$cert.Import([System.Convert]::FromBase64String(`$certBase64))
# If subject name not provided, use CN from certificate
if ([string]::IsNullOrEmpty(`$SubjectName)) {
`$SubjectName = `$cert.Subject
}
# Determine store location
`$storeLocationEnum = [System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser
if (`$StoreLocation -eq "LocalMachine") {
`$storeLocationEnum = [System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine
}
# Open the certificate store
`$store = New-Object System.Security.Cryptography.X509Certificates.X509Store(`$StoreName, `$storeLocationEnum)
`$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
try {
# Remove existing certificate with same subject if present
`$existingCerts = `$store.Certificates.Find([System.Security.Cryptography.X509Certificates.X509FindType]::FindBySubjectName, `$SubjectName, `$false)
foreach (`$existingCert in `$existingCerts) {
`$store.Remove(`$existingCert)
}
# Import the certificate
# Note: For private key import, we need to use certutil or other methods
# This is a simplified version - in practice, you may need to use
# certutil -importPFX or other methods to import with private key
`$store.Add(`$cert)
Write-Host "Certificate imported successfully to `$StoreName store"
Write-Host "Subject: `$SubjectName"
Write-Host "Thumbprint: `$(`$cert.Thumbprint)"
}
finally {
`$store.Close()
}
return `$cert.Thumbprint
"@ | Out-File -FilePath import-cert.ps1 -Encoding UTF8
- name: Build import script
working-directory: ${{ github.workspace }}\wolfssh
shell: pwsh
run: |
# Note: This step is informational - actual import happens in test job
Write-Host "Keys will be imported to cert store in test job"
test:
needs: build
runs-on: windows-latest
strategy:
fail-fast: false
matrix:
include:
- server_key_source: file
client_key_source: x509
test_name: "Server-File-Client-X509"
- server_key_source: store
client_key_source: x509
test_name: "Server-Store-Client-X509"
- server_key_source: file
client_key_source: store
test_name: "Server-File-Client-Store"
- server_key_source: store
client_key_source: store
test_name: "Server-Store-Client-Store"
steps:
- uses: actions/checkout@v4
with:
path: wolfssh
- name: Download wolfSSH build artifacts
uses: actions/download-artifact@v4
with:
name: wolfssh-windows-build
path: .
- name: Download wolfSSL build artifacts
uses: actions/download-artifact@v4
with:
name: wolfssl-windows-build
path: .
- name: Set up test environment - ${{ matrix.test_name }}
working-directory: ${{ github.workspace }}\wolfssh
shell: bash
env:
# Disable MSYS path conversion - Git Bash converts /C=US/... to C:/Program Files/Git/C=US/...
MSYS_NO_PATHCONV: 1
MSYS2_ARG_CONV_EXCL: "*"
run: |
echo "=== Test Configuration ==="
echo "Server key source: ${{ matrix.server_key_source }}"
echo "Client key source: ${{ matrix.client_key_source }}"
echo "========================="
# Create X509 certificate for testuser using renewcerts.sh (like sshd_x509_test.sh does)
if [[ "${{ matrix.client_key_source }}" == "x509" || "${{ matrix.client_key_source }}" == "store" ]]; then
echo "Creating X509 certificate for testuser using renewcerts.sh..."
# Check required files exist
if [[ ! -f "keys/renewcerts.sh" ]]; then
echo "ERROR: renewcerts.sh not found at keys/renewcerts.sh"
exit 1
fi
if [[ ! -f "keys/fred-key.pem" ]]; then
echo "ERROR: fred-key.pem not found at keys/fred-key.pem"
exit 1
fi
if [[ ! -f "keys/ca-key-ecc.pem" ]]; then
echo "ERROR: ca-key-ecc.pem not found at keys/ca-key-ecc.pem"
exit 1
fi
# Run renewcerts.sh with testuser argument (this creates testuser-cert.der and testuser-key.der)
cd keys
bash renewcerts.sh testuser
cd ..
# Verify certificates were created
if [[ -f "keys/testuser-cert.der" && -f "keys/testuser-key.der" ]]; then
echo "Created testuser-cert.der and testuser-key.der"
# Verify the certificate has the correct CN
certText=$(openssl x509 -in keys/testuser-cert.der -inform DER -text -noout 2>&1)
if echo "$certText" | grep -q "CN.*=.*testuser"; then
echo "Certificate CN verified: testuser"
else
echo "WARNING: Certificate CN may not match testuser"
echo "Certificate subject:"
echo "$certText" | grep "Subject:"
fi
echo "CLIENT_CERT_FILE=keys/testuser-cert.der" >> $GITHUB_ENV
echo "CLIENT_KEY_FILE=keys/testuser-key.der" >> $GITHUB_ENV
else
echo "ERROR: Failed to create certificate files"
echo "Expected: keys/testuser-cert.der and keys/testuser-key.der"
ls -la keys/
exit 1
fi
fi
- name: Set up cert store certificates
working-directory: ${{ github.workspace }}\wolfssh
shell: pwsh
run: |
# For testing, we'll create self-signed certificates in the cert store
# In a production scenario, you would import existing DER keys/certs
# using certutil or other tools that can handle private key import
# Create server certificate in cert store with exportable key
# For server: use LocalMachine so service (LocalSystem) can access it
# For client: use CurrentUser (accessed by testuser)
if ("${{ matrix.server_key_source }}" -eq "store") {
$serverCert = New-SelfSignedCertificate `
-Subject "CN=wolfSSH-Test-Server" `
-KeyAlgorithm RSA `
-KeyLength 2048 `
-CertStoreLocation "Cert:\LocalMachine\My" `
-KeyExportPolicy Exportable `
-NotAfter (Get-Date).AddYears(1) `
-KeyUsage DigitalSignature, KeyEncipherment
Write-Host "Server cert created in LocalMachine: $($serverCert.Subject)"
# Grant LocalSystem (NT AUTHORITY\SYSTEM) access to the private key.
# This is required for the wolfsshd service to access the key when running
# as LocalSystem. Without this, CryptAcquireCertificatePrivateKey fails.
Write-Host "=== Granting LocalSystem access to private key ==="
Write-Host "Certificate thumbprint: $($serverCert.Thumbprint)"
Write-Host "Certificate has private key: $($serverCert.HasPrivateKey)"
# Try multiple methods to get the private key info
$keyFound = $false
# Method 1: Try RSACertificateExtensions
try {
Write-Host "Trying RSACertificateExtensions.GetRSAPrivateKey..."
$rsaKey = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($serverCert)
if ($rsaKey) {
Write-Host " Got RSA key object: $($rsaKey.GetType().FullName)"
# Try to get the key name from CngKey
if ($rsaKey.Key) {
$keyName = $rsaKey.Key.UniqueName
Write-Host " Key unique name: $keyName"
} elseif ($rsaKey -is [System.Security.Cryptography.RSACng]) {
$cngKey = $rsaKey.Key
if ($cngKey) {
$keyName = $cngKey.UniqueName
Write-Host " CNG Key unique name: $keyName"
}
}
}
} catch {
Write-Host " RSACertificateExtensions method failed: $_"
}
# Method 2: Try PrivateKey property (older API)
if (-not $keyName) {
try {
Write-Host "Trying PrivateKey property..."
if ($serverCert.PrivateKey) {
$pk = $serverCert.PrivateKey
Write-Host " Got private key: $($pk.GetType().FullName)"
if ($pk.CspKeyContainerInfo) {
$keyName = $pk.CspKeyContainerInfo.UniqueKeyContainerName
Write-Host " CSP Key container name: $keyName"
}
}
} catch {
Write-Host " PrivateKey property method failed: $_"
}
}
# Search for key file in known locations
if ($keyName) {
$keyPaths = @(
"$env:ProgramData\Microsoft\Crypto\Keys\$keyName",
"$env:ProgramData\Microsoft\Crypto\RSA\MachineKeys\$keyName",
"$env:ProgramData\Microsoft\Crypto\SystemKeys\$keyName"
)
foreach ($keyPath in $keyPaths) {
if (Test-Path $keyPath) {
Write-Host "Found key file at: $keyPath"
# Show current ACL
$acl = Get-Acl $keyPath
Write-Host "Current ACL:"
$acl.Access | ForEach-Object { Write-Host " $($_.IdentityReference): $($_.FileSystemRights)" }
# Add SYSTEM access
$permission = "NT AUTHORITY\SYSTEM", "FullControl", "Allow"
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permission
$acl.SetAccessRule($accessRule)
Set-Acl $keyPath $acl
Write-Host "Granted SYSTEM FullControl to: $keyPath"
# Verify new ACL
$newAcl = Get-Acl $keyPath
Write-Host "New ACL:"
$newAcl.Access | ForEach-Object { Write-Host " $($_.IdentityReference): $($_.FileSystemRights)" }
$keyFound = $true
break
}
}
if (-not $keyFound) {
Write-Host "WARNING: Key file not found in any expected location"
Write-Host "Searching for key files..."
Get-ChildItem "$env:ProgramData\Microsoft\Crypto" -Recurse -File 2>$null | ForEach-Object {
if ($_.Name -like "*$($keyName.Substring(0, [Math]::Min(8, $keyName.Length)))*") {
Write-Host " Possible match: $($_.FullName)"
}
}
}
} else {
Write-Host "WARNING: Could not determine private key name"
}
if (-not $keyFound) {
Write-Host "WARNING: Could not grant SYSTEM access to private key - service may fail"
}
} else {
# Still create it for consistency, but in CurrentUser (not used for server)
$serverCert = New-SelfSignedCertificate `
-Subject "CN=wolfSSH-Test-Server" `
-KeyAlgorithm RSA `
-KeyLength 2048 `
-CertStoreLocation "Cert:\CurrentUser\My" `
-KeyExportPolicy Exportable `
-NotAfter (Get-Date).AddYears(1) `
-KeyUsage DigitalSignature, KeyEncipherment
Write-Host "Server cert created in CurrentUser: $($serverCert.Subject)"
}
Write-Host "Server cert thumbprint: $($serverCert.Thumbprint)"
Write-Host "Server cert full subject: $($serverCert.Subject)"
# Extract just the CN value without "CN=" prefix for CertFindCertificateInStore
# CertFindCertificateInStore with CERT_FIND_SUBJECT_STR searches the formatted name
# which may not include the "CN=" prefix
$subjectForSearch = $serverCert.Subject
if ($subjectForSearch -match "^CN=(.+)$") {
$subjectForSearch = $matches[1]
Write-Host "Using CN value for search: $subjectForSearch"
}
Add-Content -Path $env:GITHUB_ENV -Value "SERVER_CERT_SUBJECT=$subjectForSearch"
Add-Content -Path $env:GITHUB_ENV -Value "SERVER_CERT_STORE=${{ matrix.server_key_source }}"
# Create/import client certificate based on client_key_source
if ("${{ matrix.client_key_source }}" -eq "store") {
# For cert store: import testuser certificate (signed by CA) into cert store
# This ensures the cert is signed by the CA that the server trusts and has CN=testuser
$userCertPath = $env:CLIENT_CERT_FILE
$userKeyPath = $env:CLIENT_KEY_FILE
if ([string]::IsNullOrEmpty($userCertPath)) {
$userCertPath = Join-Path "${{ github.workspace }}" "wolfssh\keys\testuser-cert.der"
}
if ([string]::IsNullOrEmpty($userKeyPath)) {
$userKeyPath = Join-Path "${{ github.workspace }}" "wolfssh\keys\testuser-key.der"
}
# Fallback to fred if testuser certs don't exist
if (-not (Test-Path $userCertPath)) {
Write-Host "WARNING: testuser-cert.der not found, trying fred-cert.der"
$userCertPath = Join-Path "${{ github.workspace }}" "wolfssh\keys\fred-cert.der"
}
if (-not (Test-Path $userKeyPath)) {
Write-Host "WARNING: testuser-key.der not found, trying fred-key.der"
$userKeyPath = Join-Path "${{ github.workspace }}" "wolfssh\keys\fred-key.der"
}
if (-not (Test-Path $userCertPath) -or -not (Test-Path $userKeyPath)) {
Write-Host "ERROR: Client cert or key not found: $userCertPath or $userKeyPath"
exit 1
}
# Convert DER cert+key to PFX for import into cert store using openssl
$pfxPath = Join-Path $env:TEMP "testuser-client.pfx"
$pfxPassword = "TempP@ss123"
# Check if openssl is available
$opensslPath = Get-Command openssl -ErrorAction SilentlyContinue
if ($opensslPath) {
Write-Host "Converting testuser-cert.der + testuser-key.der to PFX using openssl..."
# Convert DER to PEM first, then to PFX
$userCertPem = Join-Path $env:TEMP "testuser-cert.pem"
$userKeyPem = Join-Path $env:TEMP "testuser-key.pem"
# Convert certificate DER to PEM
$certConvert = & openssl x509 -inform DER -in $userCertPath -out $userCertPem 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "ERROR: Failed to convert cert DER to PEM: $certConvert"
$importedCert = $null
} else {
# Try to convert key - first try as RSA, then as ECC
$keyConvert = & openssl rsa -inform DER -in $userKeyPath -out $userKeyPem 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "Key is not RSA, trying ECC..."
$keyConvert = & openssl ec -inform DER -in $userKeyPath -out $userKeyPem 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "ERROR: Failed to convert key DER to PEM (tried RSA and ECC): $keyConvert"
$importedCert = $null
} else {
Write-Host "Successfully converted ECC key to PEM"
}
} else {
Write-Host "Successfully converted RSA key to PEM"
}
if ($importedCert -eq $null -and (Test-Path $userKeyPem)) {
# Create PFX
$pfxConvert = & openssl pkcs12 -export -out $pfxPath -inkey $userKeyPem -in $userCertPem -password "pass:$pfxPassword" -nodes 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Host "ERROR: Failed to create PFX: $pfxConvert"
$importedCert = $null
} elseif (Test-Path $pfxPath) {
Write-Host "PFX created, importing into cert store..."
# Import PFX into cert store
try {
Import-PfxCertificate -FilePath $pfxPath -CertStoreLocation "Cert:\CurrentUser\My" -Password (ConvertTo-SecureString -String $pfxPassword -Force -AsPlainText) -ErrorAction Stop | Out-Null
# Get the imported cert - look for "testuser" in subject or check most recent
$importedCert = Get-ChildItem -Path "Cert:\CurrentUser\My" | Where-Object { $_.Subject -match "testuser" } | Select-Object -First 1
if (-not $importedCert) {
Write-Host "WARNING: Cert imported but not found by subject search, checking most recent..."
# Get the most recently added cert
$importedCert = Get-ChildItem -Path "Cert:\CurrentUser\My" | Sort-Object NotBefore -Descending | Select-Object -First 1
}
if ($importedCert) {
Write-Host "Successfully imported testuser cert: $($importedCert.Subject)"
}
} catch {
Write-Host "ERROR: Failed to import PFX: $_"
$importedCert = $null
}
# Cleanup temp files
Remove-Item -Path $pfxPath, $userCertPem, $userKeyPem -ErrorAction SilentlyContinue
} else {
Write-Host "WARNING: PFX file was not created"
$importedCert = $null
}
}
}
} else {
Write-Host "WARNING: openssl not found, cannot import testuser cert. Creating self-signed cert (may not work with CA verification)"
$importedCert = $null
}
if (-not $importedCert) {
# Fallback: create self-signed cert (won't work with CA verification, but allows test to proceed)
Write-Host "Creating self-signed client cert as fallback..."
$clientCert = New-SelfSignedCertificate `
-Subject "CN=wolfSSH-Test-Client" `
-KeyAlgorithm RSA `
-KeyLength 2048 `
-CertStoreLocation "Cert:\CurrentUser\My" `
-KeyExportPolicy Exportable `
-NotAfter (Get-Date).AddYears(1) `
-KeyUsage DigitalSignature, KeyEncipherment
$importedCert = $clientCert
}
Write-Host "Client cert in store: $($importedCert.Subject)"
Write-Host "Client cert thumbprint: $($importedCert.Thumbprint)"
# Extract CN from the subject for use as a cert store search string.
# The full X.500 DN contains commas which break command-line argument
# parsing, but CertFindCertificateInStore does substring matching so
# the CN alone is sufficient.
$cn = $importedCert.Subject
if ($cn -match 'CN=([^,]+)') {
$cn = $matches[1].Trim()
}
Write-Host "Client cert CN for store lookup: '$cn'"
Add-Content -Path $env:GITHUB_ENV -Value "CLIENT_CERT_SUBJECT=$cn"
} else {
# For file/x509: create a placeholder cert (not used, but keeps env var consistent)
$clientCert = New-SelfSignedCertificate `
-Subject "CN=wolfSSH-Test-Client" `
-KeyAlgorithm RSA `
-KeyLength 2048 `
-CertStoreLocation "Cert:\CurrentUser\My" `
-KeyExportPolicy Exportable `
-NotAfter (Get-Date).AddYears(1) `
-KeyUsage DigitalSignature, KeyEncipherment
Write-Host "Client cert created (placeholder for non-store tests): $($clientCert.Subject)"
Add-Content -Path $env:GITHUB_ENV -Value "CLIENT_CERT_SUBJECT=$($clientCert.Subject)"
}
- name: Create Windows user testuser and authorized_keys
shell: pwsh
run: |
$homeDir = "C:\Users\testuser"
$sshDir = "$homeDir\.ssh"
$authKeysFile = "$sshDir\authorized_keys"
# Password: <=14 chars to avoid net user "Windows 2000" prompt; mixed case, number, special.
# This is a test user and not a sensitive password.
$pw = 'T3stP@ss!xY9'
# Create home dir and .ssh for testuser (default: .ssh/authorized_keys)
New-Item -ItemType Directory -Path $homeDir -Force | Out-Null
New-Item -ItemType Directory -Path $sshDir -Force | Out-Null
Write-Host "Created $homeDir and $sshDir"
# Create local user testuser (net user avoids New-LocalUser password policy issues in CI)
$o = net user testuser $pw /add /homedir:$homeDir 2>&1
if ($LASTEXITCODE -ne 0) {
if ($o -match "already exists") {
Write-Host "User testuser already exists"
net user testuser /homedir:$homeDir 2>$null
} else {
Write-Host "net user failed: $o"
exit 1
}
} else {
Write-Host "Created user testuser"
}
Add-Content -Path $env:GITHUB_ENV -Value "TESTUSER_PASSWORD=$pw"
# For X509: no authorized_keys needed (server verifies cert against CA)
Write-Host "X509 certificate auth (source: ${{ matrix.client_key_source }}): authorized_keys not needed (server uses CA verification)"
# Create empty file - X509 doesn't use authorized_keys, but file should exist
"" | Out-File -FilePath $authKeysFile -Encoding ASCII -NoNewline
icacls $authKeysFile /grant "testuser:R" /q
Write-Host "Created $authKeysFile (empty - X509 uses CA verification)"
# Set ProfileImagePath so SHGetKnownFolderPath(FOLDERID_Profile) returns $homeDir
# for testuser (GetHomeDirectory in wolfsshd uses that; otherwise it can fail for new users).
$sid = (New-Object System.Security.Principal.NTAccount("testuser")).Translate([System.Security.Principal.SecurityIdentifier]).Value
$profKey = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\$sid"
if (-not (Test-Path $profKey)) { New-Item -Path $profKey -Force | Out-Null }
Set-ItemProperty -Path $profKey -Name "ProfileImagePath" -Value $homeDir -Force
Write-Host "Set ProfileImagePath for testuser to $homeDir"
- name: Create wolfSSHd config file
working-directory: ${{ github.workspace }}\wolfssh
shell: pwsh
run: |
$configContent = @"
Port ${{env.TEST_PORT}}
PasswordAuthentication yes
PermitRootLogin yes
"@
# For X509 client auth: configure TrustedUserCAKeys and HostCertificate (server verifies client cert against CA)
# Use PEM format as per apps/wolfsshd/test/create_sshd_config.sh
$caCertPathPem = Join-Path "${{ github.workspace }}" "wolfssh\keys\ca-cert-ecc.pem"
$caCertPathFull = (Resolve-Path $caCertPathPem -ErrorAction SilentlyContinue)
if (-not $caCertPathFull) {
Write-Host "ERROR: CA cert not found at: $caCertPathPem"
exit 1
}
Write-Host "Using CA cert (PEM format) for X509 verification: $($caCertPathFull.Path)"
$configContent += @"
TrustedUserCAKeys $($caCertPathFull.Path)
"@
if ("${{ matrix.server_key_source }}" -eq "store") {
# Get server cert subject from environment
$serverSubject = $env:SERVER_CERT_SUBJECT
if ([string]::IsNullOrEmpty($serverSubject)) {
Write-Host "ERROR: SERVER_CERT_SUBJECT not set"
exit 1
}
Write-Host "Using cert store host key with subject: $serverSubject"
# Server cert is in LocalMachine (service runs as LocalSystem)
# Note: When using cert store, the certificate is part of the store entry
# Do NOT specify HostCertificate separately - it would conflict with the store cert
$configContent += @"
HostKeyStore My
HostKeyStoreSubject $serverSubject
HostKeyStoreFlags LOCAL_MACHINE
"@
} else {
# Use PEM format as per apps/wolfsshd/test/create_sshd_config.sh
$keyPath = Join-Path "${{ github.workspace }}" "wolfssh\keys\server-key.pem"
$keyPathFull = (Resolve-Path $keyPath -ErrorAction SilentlyContinue)
if (-not $keyPathFull) {
Write-Host "ERROR: Host key file not found at: $keyPath"
Write-Host "Checking for key files..."
Get-ChildItem -Path "${{ github.workspace }}\wolfssh\keys" -Filter "server-key*" | Select-Object FullName
exit 1
}
Write-Host "Using file-based host key: $($keyPathFull.Path)"
# Add HostCertificate only for file-based keys (not cert store)
$serverCertPathPem = Join-Path "${{ github.workspace }}" "wolfssh\keys\server-cert.pem"
$serverCertPathFull = (Resolve-Path $serverCertPathPem -ErrorAction SilentlyContinue)
if (-not $serverCertPathFull) {
Write-Host "ERROR: server-cert.pem not found at: $serverCertPathPem (required for X509)"
Write-Host "Checking for server cert files..."
Get-ChildItem -Path "${{ github.workspace }}\wolfssh\keys" -Filter "server-cert*" | Select-Object FullName
exit 1
}
Write-Host "Using server certificate: $($serverCertPathFull.Path)"
$configContent += @"
HostKey $($keyPathFull.Path)
HostCertificate $($serverCertPathFull.Path)
"@
}
$configContent | Out-File -FilePath sshd_config_test -Encoding ASCII
Write-Host "=== wolfSSHd Config ==="
Get-Content sshd_config_test
Write-Host "=== End Config ==="
- name: Find wolfSSH executables
working-directory: ${{ github.workspace }}\wolfssh
shell: pwsh
run: |
$searchRoot = "${{ github.workspace }}"
Write-Host "Searching for built executables under: $searchRoot"
# Find wolfsshd.exe
$sshdExe = Get-ChildItem -Path $searchRoot -Recurse -Filter "wolfsshd.exe" -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -like "*Release*" -or $_.FullName -like "*Debug*" } |
Select-Object -First 1
if ($sshdExe) {
Write-Host "Found wolfsshd.exe at: $($sshdExe.FullName)"
Add-Content -Path $env:GITHUB_ENV -Value "SSHD_PATH=$($sshdExe.FullName)"
} else {
Write-Host "ERROR: wolfsshd.exe not found"
Get-ChildItem -Path $searchRoot -Recurse -Filter "*.exe" -ErrorAction SilentlyContinue | Select-Object FullName
exit 1
}
# Find wolfsftp client exe (project name is often wolfsftp-client)
$sftpExe = Get-ChildItem -Path $searchRoot -Recurse -Filter "wolfsftp.exe" -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -like "*Release*" -or $_.FullName -like "*Debug*" } |
Select-Object -First 1
if (-not $sftpExe) {
$sftpExe = Get-ChildItem -Path $searchRoot -Recurse -Filter "wolfsftp-client.exe" -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -like "*Release*" -or $_.FullName -like "*Debug*" } |
Select-Object -First 1
}
if ($sftpExe) {
Write-Host "Found SFTP client exe at: $($sftpExe.FullName)"
Add-Content -Path $env:GITHUB_ENV -Value "SFTP_PATH=$($sftpExe.FullName)"
} else {
Write-Host "ERROR: SFTP client exe not found (wolfsftp.exe or wolfsftp-client.exe)"
Get-ChildItem -Path $searchRoot -Recurse -Filter "*.exe" -ErrorAction SilentlyContinue | Select-Object FullName
exit 1
}
# Find wolfssh.exe (SSH client) (optional)
$sshExe = Get-ChildItem -Path $searchRoot -Recurse -Filter "wolfssh.exe" -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -like "*Release*" -or $_.FullName -like "*Debug*" } |
Select-Object -First 1
if ($sshExe) {
Write-Host "Found wolfssh.exe at: $($sshExe.FullName)"
Add-Content -Path $env:GITHUB_ENV -Value "SSH_PATH=$($sshExe.FullName)"
} else {
Write-Host "WARNING: wolfssh.exe not found (SSH client test will be skipped)"
}
# Find echoserver.exe (used for cert store server test instead of wolfsshd for better debug logs)
$echoserverExe = Get-ChildItem -Path $searchRoot -Recurse -Filter "echoserver.exe" -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -like "*Release*" -or $_.FullName -like "*Debug*" } |
Select-Object -First 1
if ($echoserverExe) {
Write-Host "Found echoserver.exe at: $($echoserverExe.FullName)"
Add-Content -Path $env:GITHUB_ENV -Value "ECHOSERVER_PATH=$($echoserverExe.FullName)"
} else {
Write-Host "WARNING: echoserver.exe not found (cert store server test will need it)"
}
- name: Copy wolfSSL DLL to executable directory (if dynamic build)
working-directory: ${{ github.workspace }}
shell: pwsh
run: |
$sshdPath = (Get-Content env:SSHD_PATH)
if (-not (Test-Path $sshdPath)) {
Write-Host "ERROR: wolfsshd.exe path not found in environment"
exit 1
}
$sshdDir = Split-Path -Parent $sshdPath
Write-Host "wolfsshd.exe directory: $sshdDir"
# If wolfssl.lib is already next to wolfsshd.exe, it's a static build - no DLL needed
$libInSshdDir = Join-Path $sshdDir "wolfssl.lib"
if (Test-Path $libInSshdDir) {
Write-Host "wolfssl.lib present beside wolfsshd.exe - static build; wolfssl.dll not required"
exit 0
}
# Dynamic build: find and copy wolfssl.dll
$wolfsslRoot = "${{ github.workspace }}\wolfssl"
$buildConfig = "${{env.WOLFSSL_BUILD_CONFIGURATION}}"
$buildPlatform = "${{env.BUILD_PLATFORM}}"
$commonPaths = @(
"$wolfsslRoot\IDE\WIN\$buildConfig\$buildPlatform\wolfssl.dll",
"$wolfsslRoot\IDE\WIN\$buildConfig\wolfssl.dll",
"$wolfsslRoot\$buildConfig\$buildPlatform\wolfssl.dll",
"$wolfsslRoot\$buildConfig\wolfssl.dll"
)
$wolfsslDll = $null
foreach ($path in $commonPaths) {
if (Test-Path $path) { $wolfsslDll = Get-Item $path; break }
}
if (-not $wolfsslDll) {
$wolfsslDll = Get-ChildItem -Path $wolfsslRoot -Recurse -Filter "wolfssl.dll" -ErrorAction SilentlyContinue | Select-Object -First 1
}
if ($wolfsslDll) {
$targetDll = Join-Path $sshdDir "wolfssl.dll"
Copy-Item -Path $wolfsslDll.FullName -Destination $targetDll -Force
Write-Host "Copied wolfssl.dll to $targetDll"
} else {
Write-Host "wolfssl.dll not found; if build is static (wolfssl.lib in output), this is OK"
}
- name: Verify host key configuration
working-directory: ${{ github.workspace }}\wolfssh
shell: pwsh
run: |
Write-Host "=== Verifying Host Key Configuration ==="
$configPath = "sshd_config_test"
if (-not (Test-Path $configPath)) {
Write-Host "ERROR: Config file not found: $configPath"
exit 1
}
$configContent = Get-Content $configPath -Raw
Write-Host "Config file content:"
Write-Host $configContent
# Check if host key is configured
$hasHostKey = $false
if ($configContent -match "HostKey\s+") {
Write-Host "Found HostKey directive (file-based)"
$hasHostKey = $true
# Verify the key file exists
if ($configContent -match "HostKey\s+([^\r\n]+)") {
$keyPath = $matches[1].Trim()
Write-Host "Host key path: $keyPath"
if (Test-Path $keyPath) {
Write-Host "Host key file exists: OK"
} else {
Write-Host "ERROR: Host key file not found: $keyPath"
exit 1
}
}
}
if ($configContent -match "HostKeyStore\s+") {
Write-Host "Found HostKeyStore directive (cert store-based)"
$hasHostKey = $true
# Verify cert store subject is set
if ($configContent -match "HostKeyStoreSubject\s+([^\r\n]+)") {
$subject = $matches[1].Trim()
Write-Host "Host key store subject: $subject"
if ([string]::IsNullOrEmpty($subject)) {
Write-Host "ERROR: HostKeyStoreSubject is empty"
exit 1
}
# Verify cert exists in store (check both LocalMachine and CurrentUser based on flags)
$storeFlags = ""
if ($configContent -match "HostKeyStoreFlags\s+([^\r\n]+)") {
$storeFlags = $matches[1].Trim()
}
$storePath = "Cert:\CurrentUser\My"
if ($storeFlags -eq "LOCAL_MACHINE") {
$storePath = "Cert:\LocalMachine\My"
}
Write-Host "Checking cert store: $storePath"
# Use -like with wildcards for substring match (same as CertFindCertificateInStore)
# The subject might be "wolfSSH-Test-Server" but cert has "CN=wolfSSH-Test-Server"
$cert = Get-ChildItem -Path $storePath -ErrorAction SilentlyContinue | Where-Object { $_.Subject -like "*$subject*" } | Select-Object -First 1
if ($cert) {
Write-Host "Certificate found in store: OK (Thumbprint: $($cert.Thumbprint), Subject: $($cert.Subject))"
# Verify cert has private key accessible
try {
$hasPrivateKey = $cert.HasPrivateKey
Write-Host "Cert has private key: $hasPrivateKey"
if (-not $hasPrivateKey) {
Write-Host "WARNING: Certificate does not have a private key accessible"
}
} catch {
Write-Host "WARNING: Could not verify private key access: $_"
}
} else {
Write-Host "ERROR: Certificate not found in store with subject: $subject"
Write-Host "Available certificates in $storePath :"
Get-ChildItem -Path $storePath -ErrorAction SilentlyContinue | Select-Object Subject, Thumbprint | Format-Table
exit 1
}
} else {
Write-Host "ERROR: HostKeyStoreSubject not found in config"
exit 1
}
}
# For X509 (both x509 file and store): TrustedUserCAKeys is required instead of authorized_keys
if ("${{ matrix.client_key_source }}" -eq "x509" -or "${{ matrix.client_key_source }}" -eq "store") {
if ($configContent -match "TrustedUserCAKeys\s+") {
Write-Host "Found TrustedUserCAKeys directive (X509 CA verification, client source: ${{ matrix.client_key_source }})"
if ($configContent -match "TrustedUserCAKeys\s+([^\r\n]+)") {
$caPath = $matches[1].Trim()
Write-Host "CA cert path: $caPath"
if (Test-Path $caPath) {
Write-Host "CA cert file exists: OK"
$caFileInfo = Get-Item $caPath
Write-Host "CA cert file size: $($caFileInfo.Length) bytes"
} else {
Write-Host "ERROR: CA cert file not found: $caPath"
Write-Host "Current directory: $(Get-Location)"
Write-Host "Files in keys directory:"
if (Test-Path "keys") {
Get-ChildItem "keys" -Filter "*ca-cert*" | Format-Table Name, Length
}
exit 1
}
}
} else {
Write-Host "ERROR: TrustedUserCAKeys not found (required for X509, client source: ${{ matrix.client_key_source }})"
exit 1
}
# For X509: AuthorizedKeysFile should NOT be set (server uses CA verification only)
if ($configContent -match "AuthorizedKeysFile\s+") {
Write-Host "WARNING: AuthorizedKeysFile is set for X509 auth - this may prevent CA verification"
Write-Host "Server will only use CA verification if AuthorizedKeysFile is NOT set"
} else {
Write-Host "AuthorizedKeysFile not set: OK (server will use CA verification)"
}
# Verify client cert files exist (for x509 file case)
if ("${{ matrix.client_key_source }}" -eq "x509") {
$userCertPath = $env:CLIENT_CERT_FILE
$userKeyPath = $env:CLIENT_KEY_FILE
if ([string]::IsNullOrEmpty($userCertPath)) {
$userCertPath = "keys\testuser-cert.der"
}
if ([string]::IsNullOrEmpty($userKeyPath)) {
$userKeyPath = "keys\testuser-key.der"
}
# Fallback to fred if testuser certs don't exist
if (-not (Test-Path $userCertPath)) {
Write-Host "WARNING: testuser-cert.der not found, trying fred-cert.der"
$userCertPath = "keys\fred-cert.der"
}
if (-not (Test-Path $userKeyPath)) {
Write-Host "WARNING: testuser-key.der not found, trying fred-key.der"
$userKeyPath = "keys\fred-key.der"
}
Write-Host "Verifying client cert files for X509 authentication..."
if (-not (Test-Path $userCertPath)) {
Write-Host "ERROR: Client cert not found: $userCertPath"
exit 1
}
if (-not (Test-Path $userKeyPath)) {
Write-Host "ERROR: Client key not found: $userKeyPath"
exit 1
}
Write-Host "Client cert files found: OK"
$certInfo = Get-Item $userCertPath
$keyInfo = Get-Item $userKeyPath
Write-Host " $($certInfo.Name): $($certInfo.Length) bytes"
Write-Host " $($keyInfo.Name): $($keyInfo.Length) bytes"
}
}
if (-not $hasHostKey) {
Write-Host "ERROR: No host key configuration found in config file!"
exit 1
}
Write-Host "Host key configuration verified: OK"
- name: Verify dependencies and environment
working-directory: ${{ github.workspace }}\wolfssh
shell: pwsh
run: |
$sshdPath = (Get-Content env:SSHD_PATH)
$sshdDir = Split-Path -Parent $sshdPath
Write-Host "=== Verifying wolfsshd.exe environment ==="
Write-Host "Executable: $sshdPath"
Write-Host "Directory: $sshdDir"
# Check if DLL is present
$dllPath = Join-Path $sshdDir "wolfssl.dll"
if (Test-Path $dllPath) {
Write-Host "✓ wolfssl.dll found"
$dllInfo = Get-Item $dllPath
Write-Host " Size: $($dllInfo.Length) bytes"
Write-Host " Modified: $($dllInfo.LastWriteTime)"
} else {
Write-Host "✗ wolfssl.dll NOT FOUND in $sshdDir"
Write-Host "Files in directory:"
Get-ChildItem -Path $sshdDir | Select-Object Name, Length | Format-Table
}
# Check config file
$configPath = "sshd_config_test"
if (Test-Path $configPath) {
Write-Host "✓ Config file found: $configPath"
} else {
Write-Host "✗ Config file NOT FOUND: $configPath"
}
# Note: Direct execution will fail with "StartServiceCtrlDispatcher failed"
# This is expected - the executable is built as a service and must run via SCM
Write-Host ""
Write-Host "Note: wolfsshd.exe is built as a Windows service."
Write-Host "Direct execution will fail (this is expected)."
Write-Host "It must be started via Service Control Manager (sc.exe)."
- name: Grant service (LocalSystem) access to config, keys, and executable
working-directory: ${{ github.workspace }}\wolfssh
shell: pwsh
run: |
# wolfsshd runs as LocalSystem; it must be able to read config, key files, and run the exe.
# Grant NT AUTHORITY\SYSTEM read+execute on the entire wolfssh tree so the service can:
# - run wolfsshd.exe (and load wolfssl.dll if dynamic)
# - read sshd_config_test and all files under keys/
# /T = apply to existing files and subdirs; (OI)(CI) = inherit to new objects
$wolfsshRoot = (Get-Location).Path
Write-Host "Granting SYSTEM (RX) on entire wolfssh tree: $wolfsshRoot"
icacls $wolfsshRoot /grant "NT AUTHORITY\SYSTEM:(OI)(CI)RX" /T /q
if ($LASTEXITCODE -ne 0) {
Write-Host "WARNING: icacls on wolfssh root failed, trying config and keys only"
$configPathFull = (Resolve-Path "sshd_config_test" -ErrorAction Stop).Path
$keysDir = (Resolve-Path "keys" -ErrorAction Stop).Path
icacls $configPathFull /grant "NT AUTHORITY\SYSTEM:R" /q
icacls $keysDir /grant "NT AUTHORITY\SYSTEM:(OI)(CI)R" /T /q
$sshdPath = $env:SSHD_PATH
if ($sshdPath -and (Test-Path $sshdPath)) {
$sshdDir = (Resolve-Path (Split-Path -Parent $sshdPath)).Path
icacls $sshdDir /grant "NT AUTHORITY\SYSTEM:(OI)(CI)RX" /T /q
}
}
Write-Host "Done."
- name: Test cert store access as LocalSystem
if: matrix.server_key_source == 'store'
working-directory: ${{ github.workspace }}\wolfssh
shell: pwsh
run: |