예제 #1
0
        public bool TryGetSecret(KeychainSecretName name, out KeychainSecret secret)
        {
            using (var serviceName = new KeychainMemory(name.Service))
                using (var accountName = new KeychainMemory(name.Account)) {
                    var itemRef = IntPtr.Zero;
                    var result  = SecKeychainFindGenericPassword(
                        IntPtr.Zero,
                        serviceName.Length, serviceName.Buffer,
                        accountName.Length, accountName.Buffer,
                        out var secretValueLength, out var secretValuePtr,
                        ref itemRef);

                    if (result == SecStatus.ItemNotFound)
                    {
                        secret = null;
                        return(false);
                    }

                    if (result != SecStatus.Success)
                    {
                        throw new AppleSecurityException(
                                  nameof(SecKeychainFindGenericPassword),
                                  result);
                    }

                    try {
                        var passwordData = new byte [secretValueLength];
                        Marshal.Copy(secretValuePtr, passwordData, 0, (int)secretValueLength);
                        secret = KeychainSecret.Create(name, passwordData);
                        return(true);
                    } finally {
                        SecKeychainItemFreeContent(IntPtr.Zero, secretValuePtr);
                    }
                }
        }
예제 #2
0
        public void RoundtripString(string key, string value)
        {
            KeychainSecretName name = (serviceName, key);

            keychain.StoreSecret(KeychainSecret.Create(name, value));
            Assert.True(keychain.TryGetSecret(name, out var secret));
            Assert.Equal(value, secret.GetUtf8StringValue());
        }
예제 #3
0
        public void StoreSecret()
        {
            KeychainSecretName name = (serviceName, "dont-update-me");

            keychain.StoreSecret(KeychainSecret.Create(name, "initial value"));
            Assert.Throws <KeychainItemAlreadyExistsException> (() => keychain.StoreSecret(
                                                                    KeychainSecret.Create(name, "new value"),
                                                                    updateExisting: false));
        }
예제 #4
0
        public void RoundtripBytes()
        {
            var random = new Random();
            var value  = new byte [1024 * 1024];

            random.NextBytes(value);
            KeychainSecretName name = (serviceName, "randomblob");

            keychain.StoreSecret(KeychainSecret.Create(name, value));
            Assert.True(keychain.TryGetSecret(name, out var secret));
            Assert.Equal(value, secret.Value);
        }
예제 #5
0
        public bool TryGetSecret(KeychainSecretName name, out KeychainSecret secret)
        {
            var secretPath = GetSecretPath(name);

            if (!File.Exists(secretPath))
            {
                secret = null;
                return(false);
            }

            secret = KeychainSecret.Create(
                name,
                Unprotect(File.ReadAllBytes(secretPath)));

            return(true);
        }
예제 #6
0
    static void Main(string [] args)
    {
        var processPath = Process
                          .GetCurrentProcess()
                          .MainModule
                          .FileName;

        var targetFramework = typeof(Program)
                              .Assembly.GetCustomAttribute <TargetFrameworkAttribute> ()
                              .FrameworkName;

        Console.WriteLine("    This process path: {0}", processPath);
        Console.WriteLine("    Target framework:  {0}", targetFramework);

        var secretName = ("dnc-apple-security-regression", $"secret-{args [0]}");

        Keychain.StoreSecret(
            KeychainSecret.Create(
                secretName,
                "super secret value"));

        try {
            if (Keychain.TryGetSecret(secretName, out var secret))
            {
                Console.WriteLine("      Read secret: {0} = {1}", secretName, secret.GetUtf8StringValue());
            }
            else
            {
                Console.WriteLine("      Secret does not exist: {0}", secretName);
            }
        } catch (Exception e) {
            Console.WriteLine("      Exception reading secret:");
            foreach (var line in e.ToString().Split("\n"))
            {
                Console.WriteLine($"        {line}");
            }
        }
    }