예제 #1
0
파일: Ed25519.cs 프로젝트: cwharris/nsec
        internal override bool TryExportPublicKey(
            ReadOnlySpan <byte> publicKeyBytes,
            KeyBlobFormat format,
            Span <byte> blob,
            out int blobSize)
        {
            switch (format)
            {
            case KeyBlobFormat.RawPublicKey:
                return(s_rawPublicKeyFormatter.TryExport(publicKeyBytes, blob, out blobSize));

            case KeyBlobFormat.NSecPublicKey:
                return(s_nsecPublicKeyFormatter.TryExport(publicKeyBytes, blob, out blobSize));

            case KeyBlobFormat.PkixPublicKey:
                return(s_pkixPublicKeyFormatter.TryExport(publicKeyBytes, blob, out blobSize));

            case KeyBlobFormat.PkixPublicKeyText:
                return(s_pkixPublicKeyFormatter.TryExportText(publicKeyBytes, blob, out blobSize));

            default:
                throw Error.Argument_FormatNotSupported(nameof(format), format.ToString());
            }
        }
예제 #2
0
        internal override bool TryImportKey(
            ReadOnlySpan <byte> blob,
            KeyBlobFormat format,
            out SecureMemoryHandle keyHandle,
            out byte[] publicKeyBytes)
        {
            switch (format)
            {
            case KeyBlobFormat.RawPrivateKey:
                return(s_rawPrivateKeyFormatter.TryImport(blob, out keyHandle, out publicKeyBytes));

            case KeyBlobFormat.NSecPrivateKey:
                return(s_nsecPrivateKeyFormatter.TryImport(blob, out keyHandle, out publicKeyBytes));

            case KeyBlobFormat.PkixPrivateKey:
                return(s_pkixPrivateKeyFormatter.TryImport(blob, out keyHandle, out publicKeyBytes));

            case KeyBlobFormat.PkixPrivateKeyText:
                return(s_pkixPrivateKeyFormatter.TryImportText(blob, out keyHandle, out publicKeyBytes));

            default:
                throw Error.Argument_FormatNotSupported(nameof(format), format.ToString());
            }
        }
예제 #3
0
파일: Ed25519.cs 프로젝트: cwharris/nsec
        internal override bool TryExportKey(
            SecureMemoryHandle keyHandle,
            KeyBlobFormat format,
            Span <byte> blob,
            out int blobSize)
        {
            switch (format)
            {
            case KeyBlobFormat.RawPrivateKey:
                return(s_rawPrivateKeyFormatter.TryExport(keyHandle, blob, out blobSize));

            case KeyBlobFormat.NSecPrivateKey:
                return(s_nsecPrivateKeyFormatter.TryExport(keyHandle, blob, out blobSize));

            case KeyBlobFormat.PkixPrivateKey:
                return(s_pkixPrivateKeyFormatter.TryExport(keyHandle, blob, out blobSize));

            case KeyBlobFormat.PkixPrivateKeyText:
                return(s_pkixPrivateKeyFormatter.TryExportText(keyHandle, blob, out blobSize));

            default:
                throw Error.Argument_FormatNotSupported(nameof(format), format.ToString());
            }
        }
예제 #4
0
        private static void Test(Algorithm a, int seedSize, KeyBlobFormat importFormat, int keySize, int outputSize, KeyBlobFormat format, byte[] blobHeader)
        {
            var b = Utilities.RandomBytes.Slice(0, seedSize);

            using (var k = Key.Import(a, b, importFormat, new KeyCreationParameters {
                ExportPolicy = KeyExportPolicies.AllowPlaintextArchiving
            }))
            {
                var blob = k.Export(format);

                Assert.NotNull(blob);
                Assert.Equal(blobHeader.Length + sizeof(short) + sizeof(short) + keySize, blob.Length);
                Assert.Equal(blobHeader, blob.AsSpan(0, blobHeader.Length).ToArray());
                Assert.Equal(keySize, BitConverter.ToInt16(blob, blobHeader.Length));
                Assert.Equal(outputSize, BitConverter.ToInt16(blob, blobHeader.Length + sizeof(short)));
            }
        }
예제 #5
0
 public static void TryImportEmpty(Algorithm a, KeyBlobFormat format)
 {
     Assert.False(PublicKey.TryImport(a, ReadOnlySpan <byte> .Empty, format, out var pk));
     Assert.Null(pk);
 }
예제 #6
0
 public static void ImportEmpty(Algorithm a, KeyBlobFormat format)
 {
     Assert.Throws <FormatException>(() => PublicKey.Import(a, ReadOnlySpan <byte> .Empty, format));
 }
예제 #7
0
        private static void Test(Algorithm a, int seedSize, KeyBlobFormat importFormat, int keySize, KeyBlobFormat format, byte[] blobHeader)
        {
            var b = Utilities.RandomBytes.Slice(0, seedSize);

            using (var k = Key.Import(a, b, importFormat, KeyFlags.AllowArchiving))
            {
                var blob = k.Export(format);

                Assert.NotNull(blob);
                Assert.Equal(blobHeader.Length + sizeof(uint) + keySize, blob.Length);
                Assert.Equal(blobHeader, blob.AsSpan().Slice(0, blobHeader.Length).ToArray());
                Assert.Equal(BitConverter.GetBytes(keySize), blob.AsSpan().Slice(blobHeader.Length, sizeof(int)).ToArray());
            }
        }
예제 #8
0
        public static void TryImportSymmetricKeyEmpty(Type algorithmType, KeyBlobFormat format)
        {
            var a = (Algorithm)Activator.CreateInstance(algorithmType);

            Assert.False(Key.TryImport(a, ReadOnlySpan <byte> .Empty, format, KeyExportPolicies.None, out Key k));
        }
예제 #9
0
        public static void ImportSymmetricKeyEmpty(Type algorithmType, KeyBlobFormat format)
        {
            var a = (Algorithm)Activator.CreateInstance(algorithmType);

            Assert.Throws <FormatException>(() => Key.Import(a, ReadOnlySpan <byte> .Empty, format));
        }
예제 #10
0
파일: KeyTests.cs 프로젝트: xxponline/nsec
 public static void TryImportSymmetricKeyEmpty(Algorithm a, KeyBlobFormat format)
 {
     Assert.False(Key.TryImport(a, ReadOnlySpan <byte> .Empty, format, out var k, new KeyCreationParameters {
         ExportPolicy = KeyExportPolicies.None
     }));
 }
예제 #11
0
        public static void TryExportSmaller(Algorithm a, KeyBlobFormat format)
        {
            using var k = new Key(a);

            Assert.False(k.PublicKey.TryExport(format, Span <byte> .Empty, out _));
        }
예제 #12
0
 // Gets the size of a key blob in the specified format.
 internal virtual int GetKeyBlobSize(
     KeyBlobFormat format)
 {
     throw Error.NotSupported_ExportKey();
 }
예제 #13
0
파일: Key.cs 프로젝트: judgie79/nsec
        public byte[] Export(
            KeyBlobFormat format)
        {
            byte[] blob;
            int    blobSize;

            if (format < 0)
            {
                if (_handle.IsClosed)
                {
                    throw Error.ObjectDisposed_Key();
                }

                bool allowExport    = (_flags & KeyFlags.AllowExport) != 0;
                bool allowArchiving = (_flags & KeyFlags.AllowArchiving) != 0;

                if (!allowExport)
                {
                    if (!allowArchiving)
                    {
                        throw Error.InvalidOperation_ExportNotAllowed();
                    }
                    if (_exported)
                    {
                        throw Error.InvalidOperation_AlreadyArchived();
                    }
                }

                _exported = true;

                if (_algorithm.TryExportKey(_handle, format, Span <byte> .Empty, out blobSize))
                {
                    Debug.Assert(blobSize == 0);
                    return(Utilities.Empty <byte>());
                }

                blob = new byte[blobSize];

                if (_algorithm.TryExportKey(_handle, format, blob, out blobSize))
                {
                    Debug.Assert(blobSize == blob.Length);
                    return(blob);
                }

                throw Error.Cryptographic_InternalError();
            }
            else
            {
                if (_publicKey == null)
                {
                    throw Error.Argument_FormatNotSupported(nameof(format), format.ToString());
                }

                if (_algorithm.TryExportPublicKey(_publicKey.Bytes, format, Span <byte> .Empty, out blobSize))
                {
                    Debug.Assert(blobSize == 0);
                    return(Utilities.Empty <byte>());
                }

                blob = new byte[blobSize];

                if (_algorithm.TryExportPublicKey(_publicKey.Bytes, format, blob, out blobSize))
                {
                    Debug.Assert(blobSize == blob.Length);
                    return(blob);
                }

                throw Error.Cryptographic_InternalError();
            }
        }