@@ -13,6 +13,8 @@ namespace Microsoft.AspNetCore.Certificates.Generation;
13
13
14
14
internal sealed class MacOSCertificateManager : CertificateManager
15
15
{
16
+ // User keychain. Guard with quotes when using in command lines since users may have set
17
+ // their user profile (HOME) directory to a non-standard path that includes whitespace.
16
18
private static readonly string MacOSUserKeychain = Environment . GetFolderPath ( Environment . SpecialFolder . UserProfile ) + "/Library/Keychains/login.keychain-db" ;
17
19
18
20
// System keychain. We no longer store certificates or create trust rules in the system
@@ -25,34 +27,34 @@ internal sealed class MacOSCertificateManager : CertificateManager
25
27
26
28
// Verify the certificate {0} for the SSL and X.509 Basic Policy.
27
29
private const string MacOSVerifyCertificateCommandLine = "security" ;
28
- private const string MacOSVerifyCertificateCommandLineArgumentsFormat = $ "verify-cert -c {{0}} -p basic -p ssl";
30
+ private const string MacOSVerifyCertificateCommandLineArgumentsFormat = "verify-cert -c \" {0} \" -p basic -p ssl" ;
29
31
30
32
// Delete a certificate with the specified SHA-256 (or SHA-1) hash {0} from keychain {1}.
31
33
private const string MacOSDeleteCertificateCommandLine = "sudo" ;
32
- private const string MacOSDeleteCertificateCommandLineArgumentsFormat = "security delete-certificate -Z {0} {1}" ;
34
+ private const string MacOSDeleteCertificateCommandLineArgumentsFormat = "security delete-certificate -Z {0} \" {1}\" " ;
33
35
34
36
// Add a certificate to the per-user trust settings in the user keychain. The trust policy
35
37
// for the certificate will be set to be always trusted for SSL and X.509 Basic Policy.
36
38
// Note: This operation will require user authentication.
37
39
private const string MacOSTrustCertificateCommandLine = "security" ;
38
- private static readonly string MacOSTrustCertificateCommandLineArguments = $ "add-trusted-cert -p basic -p ssl -k { MacOSUserKeychain } ";
40
+ private static readonly string MacOSTrustCertificateCommandLineArguments = $ "add-trusted-cert -p basic -p ssl -k \" { MacOSUserKeychain } \" ";
39
41
40
42
// Import a pkcs12 certificate into the user keychain using the unwrapping passphrase {1}, and
41
43
// allow any application to access the imported key without warning.
42
44
private const string MacOSAddCertificateToKeyChainCommandLine = "security" ;
43
- private static readonly string MacOSAddCertificateToKeyChainCommandLineArgumentsFormat = "import {0} -k " + MacOSUserKeychain + " -t cert -f pkcs12 -P {1} -A" ;
45
+ private static readonly string MacOSAddCertificateToKeyChainCommandLineArgumentsFormat = "import \" {0}\" -k \" " + MacOSUserKeychain + " \ " -t cert -f pkcs12 -P {1} -A";
44
46
45
47
// Remove a certificate from the admin trust settings. We no longer add certificates to the
46
48
// admin trust settings, but need this for cleaning up certs generated by pre-.NET 7 versions
47
49
// of this tool that used to create trust settings in the system keychain.
48
50
// Note: This operation will require user authentication.
49
51
private const string MacOSUntrustLegacyCertificateCommandLine = "sudo" ;
50
- private const string MacOSUntrustLegacyCertificateCommandLineArguments = "security remove-trusted-cert -d {0}" ;
52
+ private const string MacOSUntrustLegacyCertificateCommandLineArguments = "security remove-trusted-cert -d \" {0}\" " ;
51
53
52
54
// Find all matching certificates on the keychain {1} that have the name {0} and print
53
55
// print their SHA-256 and SHA-1 hashes.
54
56
private const string MacOSFindCertificateOnKeychainCommandLine = "security" ;
55
- private const string MacOSFindCertificateOnKeychainCommandLineArgumentsFormat = "find-certificate -c {0} -a -Z -p {1}" ;
57
+ private const string MacOSFindCertificateOnKeychainCommandLineArgumentsFormat = "find-certificate -c {0} -a -Z -p \" {1}\" " ;
56
58
57
59
// Format used by the tool when printing SHA-1 hashes.
58
60
private const string MacOSFindCertificateOutputRegex = "SHA-1 hash: ([0-9A-Z]+)" ;
0 commit comments