Skip to content

Commit 7baad0a

Browse files
authored
make env vars optional for default allow list path (#757)
* make env vars optional for default allow list path * add pkcs11 pester test * use lowercasing within method
1 parent 86bc0d7 commit 7baad0a

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

contrib/win32/win32compat/ssh-agent/agent.c

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -414,26 +414,41 @@ void
414414
agent_initialize_allow_list() {
415415
/*
416416
* allowed paths for PKCS11 libraries,
417-
* initialize to ProgramFiles and ProgramFiles(x86) by default
417+
* attempt to initialize to ProgramFiles and ProgramFiles(x86) by default
418418
* upstream uses /usr/lib/* and /usr/local/lib/*
419419
*/
420-
size_t prog_files_len = 0, prog_files_x86_len = 0;
421-
char* prog_files = NULL, * prog_files_x86 = NULL;
420+
size_t allowed_len = 0, prog_files_len = 0, prog_files_x86_len = 0;
421+
char* allowed_path = NULL, *prog_files = NULL, *prog_files_x86 = NULL;
422422

423423
_dupenv_s(&prog_files, &prog_files_len, "ProgramFiles");
424-
if (!prog_files)
425-
fatal("couldn't find ProgramFiles environment variable");
426-
convertToForwardslash(prog_files);
427-
428424
_dupenv_s(&prog_files_x86, &prog_files_x86_len, "ProgramFiles(x86)");
429-
if (!prog_files_x86)
430-
fatal("couldn't find ProgramFiles environment variable");
431-
convertToForwardslash(prog_files_x86);
432425

433-
size_t allowed_providers_len = 1 + prog_files_len + 4 + prog_files_x86_len + 3;
434-
allowed_providers = xmalloc(allowed_providers_len);
435-
sprintf_s(allowed_providers, allowed_providers_len, "/%s/*,/%s/*", prog_files, prog_files_x86);
426+
if (!prog_files && !prog_files_x86) {
427+
allowed_providers = xstrdup("");
428+
return;
429+
}
430+
431+
if (prog_files && prog_files_x86) {
432+
allowed_len = prog_files_len + 3 + prog_files_x86_len + 1;
433+
allowed_path = xmalloc(allowed_len);
434+
sprintf_s(allowed_path, allowed_len, "%s\\*,%s", prog_files, prog_files_x86);
435+
free(prog_files);
436+
free(prog_files_x86);
437+
}
438+
else if (prog_files) {
439+
allowed_len = prog_files_len;
440+
allowed_path = prog_files;
441+
}
442+
else if (prog_files_x86) {
443+
allowed_len = prog_files_x86_len;
444+
allowed_path = prog_files_x86;
445+
}
446+
447+
allowed_len += 3; /* for additional characters below */
448+
allowed_providers = xmalloc(allowed_len);
449+
sprintf_s(allowed_providers, allowed_len, "%s\\*", allowed_path);
436450

437-
free(prog_files);
438-
free(prog_files_x86);
451+
if (allowed_path) {
452+
free(allowed_path);
453+
}
439454
}

contrib/win32/win32compat/ssh-agent/keyagent-request.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,12 @@ int process_add_smartcard_key(struct sshbuf* request, struct sshbuf* response, s
677677
goto done;
678678
}
679679

680-
if (match_pattern_list(canonical_provider, allowed_providers, 0) != 1) {
680+
to_lower_case(provider);
681+
verbose("provider realpath: \"%.100s\"", provider);
682+
verbose("allowed provider paths: \"%.100s\"", allowed_providers);
683+
if (match_pattern_list(provider, allowed_providers, 1) != 1) {
681684
verbose("refusing PKCS#11 add of \"%.100s\": "
682-
"provider not allowed", canonical_provider);
685+
"provider not allowed", provider);
683686
goto done;
684687
}
685688

regress/pesterTests/KeyUtils.Tests.ps1

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Describe "E2E scenarios for ssh key management" -Tags "CI" {
1717
$null = New-Item $testDir -ItemType directory -Force -ErrorAction SilentlyContinue
1818
}
1919

20+
$pkcs11Pin = "testpin"
2021
$keypassphrase = "testpassword"
2122
$NoLibreSSL = $OpenSSHTestInfo["NoLibreSSL"]
2223
if($NoLibreSSL)
@@ -298,6 +299,33 @@ Describe "E2E scenarios for ssh key management" -Tags "CI" {
298299
$allkeys = @(ssh-add -L)
299300
ValidateRegistryACL -count $allkeys.count
300301
}
302+
303+
It "$tC.$tI - ssh-add - pkcs11 library (if available)" {
304+
$pkcs11Path = "C:\\Program Files\\OpenSC Project\\OpenSC\\pkcs11\\opensc-pkcs11.dll"
305+
if (Test-Path $pkcs11Path) {
306+
#set up SSH_ASKPASS
307+
Add-PasswordSetting -Pass $pkcs11Pin
308+
309+
ssh-add -s "$pkcs11Path"
310+
$LASTEXITCODE | Should Be 0
311+
#remove SSH_ASKPASS
312+
Remove-PasswordSetting
313+
314+
#ensure added keys are listed
315+
$allkeys = ssh-add -L
316+
$allKeys -notmatch "The agent has no identities." | Should Be $True
317+
318+
#delete added keys
319+
iex "cmd /c `"ssh-add -D 2> nul `""
320+
321+
#check keys are deleted
322+
$allkeys = ssh-add -L
323+
$allKeys -match "The agent has no identities." | Should Be $True
324+
}
325+
else {
326+
Write-Host "skipping pkcs11 test because provider not found"
327+
}
328+
}
301329
}
302330

303331
Context "$tC ssh-keygen known_hosts operations" {

0 commit comments

Comments
 (0)