private static SafeSecKeyRefHandle ImportKey(DSAParameters parameters)
            {
                if (parameters.X != null)
                {
                    // DSAPrivateKey ::= SEQUENCE(
                    //   version INTEGER,
                    //   p INTEGER,
                    //   q INTEGER,
                    //   g INTEGER,
                    //   y INTEGER,
                    //   x INTEGER,
                    // )

                    using (AsnWriter privateKeyWriter = new AsnWriter(AsnEncodingRules.DER))
                    {
                        privateKeyWriter.PushSequence();
                        privateKeyWriter.WriteInteger(0);
                        privateKeyWriter.WriteKeyParameterInteger(parameters.P);
                        privateKeyWriter.WriteKeyParameterInteger(parameters.Q);
                        privateKeyWriter.WriteKeyParameterInteger(parameters.G);
                        privateKeyWriter.WriteKeyParameterInteger(parameters.Y);
                        privateKeyWriter.WriteKeyParameterInteger(parameters.X);
                        privateKeyWriter.PopSequence();
                        return(Interop.AppleCrypto.ImportEphemeralKey(privateKeyWriter.EncodeAsSpan(), true));
                    }
                }
                else
                {
                    using (AsnWriter writer = DSAKeyFormatHelper.WriteSubjectPublicKeyInfo(parameters))
                    {
                        return(Interop.AppleCrypto.ImportEphemeralKey(writer.EncodeAsSpan(), false));
                    }
                }
            }
            private static SafeSecKeyRefHandle ImportKey(DSAParameters parameters)
            {
                AsnWriter keyWriter;
                bool      hasPrivateKey;

                if (parameters.X != null)
                {
                    // DSAPrivateKey ::= SEQUENCE(
                    //   version INTEGER,
                    //   p INTEGER,
                    //   q INTEGER,
                    //   g INTEGER,
                    //   y INTEGER,
                    //   x INTEGER,
                    // )

                    keyWriter = new AsnWriter(AsnEncodingRules.DER);

                    using (keyWriter.PushSequence())
                    {
                        keyWriter.WriteInteger(0);
                        keyWriter.WriteKeyParameterInteger(parameters.P);
                        keyWriter.WriteKeyParameterInteger(parameters.Q);
                        keyWriter.WriteKeyParameterInteger(parameters.G);
                        keyWriter.WriteKeyParameterInteger(parameters.Y);
                        keyWriter.WriteKeyParameterInteger(parameters.X);
                    }

                    hasPrivateKey = true;
                }
                else
                {
                    keyWriter     = DSAKeyFormatHelper.WriteSubjectPublicKeyInfo(parameters);
                    hasPrivateKey = false;
                }

                byte[] rented = CryptoPool.Rent(keyWriter.GetEncodedLength());

                if (!keyWriter.TryEncode(rented, out int written))
                {
                    Debug.Fail("TryEncode failed with a pre-allocated buffer");
                    throw new InvalidOperationException();
                }

                // Explicitly clear the inner buffer
                keyWriter.Reset();

                try
                {
                    return(Interop.AppleCrypto.ImportEphemeralKey(rented.AsSpan(0, written), hasPrivateKey));
                }
                finally
                {
                    CryptoPool.Return(rented, written);
                }
            }
            private static SafeSecKeyRefHandle ImportKey(DSAParameters parameters)
            {
                bool hasPrivateKey = parameters.X != null;

                byte[] blob;

                if (hasPrivateKey)
                {
                    Debug.Assert(parameters.X != null);

                    // DSAPrivateKey ::= SEQUENCE(
                    //   version INTEGER,
                    //   p INTEGER,
                    //   q INTEGER,
                    //   g INTEGER,
                    //   y INTEGER,
                    //   x INTEGER,
                    // )

                    using (AsnWriter privateKeyWriter = new AsnWriter(AsnEncodingRules.DER))
                    {
                        privateKeyWriter.PushSequence();
                        privateKeyWriter.WriteInteger(0);
                        privateKeyWriter.WriteKeyParameterInteger(parameters.P);
                        privateKeyWriter.WriteKeyParameterInteger(parameters.Q);
                        privateKeyWriter.WriteKeyParameterInteger(parameters.G);
                        privateKeyWriter.WriteKeyParameterInteger(parameters.Y);
                        privateKeyWriter.WriteKeyParameterInteger(parameters.X);
                        privateKeyWriter.PopSequence();
                        blob = privateKeyWriter.Encode();
                    }
                }
                else
                {
                    using (AsnWriter writer = DSAKeyFormatHelper.WriteSubjectPublicKeyInfo(parameters))
                    {
                        blob = writer.Encode();
                    }
                }

                return(Interop.AppleCrypto.ImportEphemeralKey(blob, hasPrivateKey));
            }
Beispiel #4
0
        private AsnWriter WriteSubjectPublicKeyInfo()
        {
            DSAParameters dsaParameters = ExportParameters(false);

            return(DSAKeyFormatHelper.WriteSubjectPublicKeyInfo(dsaParameters));
        }