Beispiel #1
0
        public byte[] Export(
            KeyBlobFormat format)
        {
            byte[] blob;
            int    blobSize;

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

                if ((_exportPolicy & KeyExportPolicies.AllowPlaintextExport) == 0)
                {
                    if ((_exportPolicy & KeyExportPolicies.AllowPlaintextArchiving) == 0)
                    {
                        throw Error.InvalidOperation_ExportNotAllowed();
                    }
                    if (_exported)
                    {
                        throw Error.InvalidOperation_AlreadyArchived();
                    }
                }

                _exported = true;

                _algorithm.TryExportKey(_handle, format, Span <byte> .Empty, out blobSize);
                blob = new byte[blobSize];

                if (!_algorithm.TryExportKey(_handle, format, blob, out blobSize))
                {
                    throw Error.Cryptographic_InternalError();
                }

                Debug.Assert(blobSize == blob.Length);
                return(blob);
            }
            else
            {
                if (_publicKey == null)
                {
                    throw Error.Argument_FormatNotSupported(nameof(format), format.ToString());
                }

                _algorithm.TryExportPublicKey(_publicKey.Bytes, format, Span <byte> .Empty, out blobSize);
                blob = new byte[blobSize];

                if (!_algorithm.TryExportPublicKey(_publicKey.Bytes, format, blob, out blobSize))
                {
                    throw Error.Cryptographic_InternalError();
                }

                Debug.Assert(blobSize == blob.Length);
                return(blob);
            }
        }
Beispiel #2
0
        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();
            }
        }