Writes data encoded via the Distinguished Encoding Rules for Abstract Syntax Notation 1 (ASN.1) data.
Exemplo n.º 1
0
        internal static byte[] ToSubjectPublicKeyInfo(this DSAParameters parameters)
        {
            // SubjectPublicKeyInfo::= SEQUENCE  {
            //    algorithm AlgorithmIdentifier,
            //    subjectPublicKey     BIT STRING  }

            // Dss-Parms ::= SEQUENCE {
            //   p INTEGER,
            //   q INTEGER,
            //   g INTEGER
            // }

            return(DerEncoder.ConstructSequence(
                       DerEncoder.ConstructSegmentedSequence(
                           DerEncoder.SegmentedEncodeOid(s_idDsa),
                           DerEncoder.ConstructSegmentedSequence(
                               DerEncoder.SegmentedEncodeUnsignedInteger(parameters.P),
                               DerEncoder.SegmentedEncodeUnsignedInteger(parameters.Q),
                               DerEncoder.SegmentedEncodeUnsignedInteger(parameters.G)
                               )
                           ),
                       DerEncoder.SegmentedEncodeBitString(
                           DerEncoder.SegmentedEncodeUnsignedInteger(parameters.Y))
                       ));
        }
Exemplo n.º 2
0
        internal static byte[] ToPrivateKeyBlob(this ECParameters parameters)
        {
            parameters.Validate();

            if (!parameters.Curve.IsNamed)
            {
                throw new PlatformNotSupportedException(SR.Cryptography_ECC_NamedCurvesOnly);
            }

            byte[] pointBlob = GetPointBlob(ref parameters);

            // ECPrivateKey{CURVES:IOSet} ::= SEQUENCE {
            //   version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
            //   privateKey OCTET STRING,
            //   parameters [0] Parameters{{IOSet}} OPTIONAL,
            //   publicKey  [1] BIT STRING OPTIONAL
            // }
            return(DerEncoder.ConstructSequence(
                       s_encodedVersion1,
                       DerEncoder.SegmentedEncodeOctetString(parameters.D),
                       DerEncoder.ConstructSegmentedContextSpecificValue(
                           0,
                           DerEncoder.SegmentedEncodeOid(parameters.Curve.Oid)),
                       DerEncoder.ConstructSegmentedContextSpecificValue(
                           1,
                           DerEncoder.SegmentedEncodeBitString(pointBlob))));
        }
Exemplo n.º 3
0
        internal static byte[] ToSubjectPublicKeyInfo(this RSAParameters parameters)
        {
            Debug.Assert(parameters.D == null);

            // SubjectPublicKeyInfo::= SEQUENCE  {
            //    algorithm AlgorithmIdentifier,
            //    subjectPublicKey     BIT STRING  }
            return(DerEncoder.ConstructSequence(
                       s_encodedRsaAlgorithmIdentifier,
                       DerEncoder.SegmentedEncodeBitString(
                           parameters.ToPkcs1Blob())));
        }
Exemplo n.º 4
0
        private static byte[] ConvertToOpenSslFormat(byte[] input)
        {
            Debug.Assert(input != null);
            Debug.Assert(input.Length % 2 == 0);
            Debug.Assert(input.Length > 1);

            // Input is (r, s), each of them exactly half of the array.
            // Output is the DER encoded value of CONSTRUCTEDSEQUENCE(INTEGER(r), INTEGER(s)).
            int halfLength = input.Length / 2;

            byte[][] rEncoded = DerEncoder.SegmentedEncodeUnsignedInteger(input, 0, halfLength);
            byte[][] sEncoded = DerEncoder.SegmentedEncodeUnsignedInteger(input, halfLength, halfLength);

            return(DerEncoder.ConstructSequence(rEncoded, sEncoded));
        }
Exemplo n.º 5
0
        internal static byte[] ToSubjectPublicKeyInfo(this ECParameters parameters)
        {
            parameters.Validate();

            if (!parameters.Curve.IsNamed)
            {
                throw new PlatformNotSupportedException(SR.Cryptography_ECC_NamedCurvesOnly);
            }

            byte[] pointBlob = GetPointBlob(ref parameters);

            return(DerEncoder.ConstructSequence(
                       DerEncoder.ConstructSegmentedSequence(
                           s_encodedIdEcPublicKey,
                           DerEncoder.SegmentedEncodeOid(parameters.Curve.Oid)),
                       DerEncoder.SegmentedEncodeBitString(pointBlob)));
        }
Exemplo n.º 6
0
        internal static byte[] ToPkcs1Blob(this RSAParameters parameters)
        {
            if (parameters.Exponent == null || parameters.Modulus == null)
            {
                throw new CryptographicException(SR.Cryptography_InvalidRsaParameters);
            }

            if (parameters.D == null)
            {
                if (parameters.P != null ||
                    parameters.DP != null ||
                    parameters.Q != null ||
                    parameters.DQ != null ||
                    parameters.InverseQ != null)
                {
                    throw new CryptographicException(SR.Cryptography_InvalidRsaParameters);
                }

                return(DerEncoder.ConstructSequence(
                           DerEncoder.SegmentedEncodeUnsignedInteger(parameters.Modulus),
                           DerEncoder.SegmentedEncodeUnsignedInteger(parameters.Exponent)));
            }

            if (parameters.P == null ||
                parameters.DP == null ||
                parameters.Q == null ||
                parameters.DQ == null ||
                parameters.InverseQ == null)
            {
                throw new CryptographicException(SR.Cryptography_InvalidRsaParameters);
            }

            return(DerEncoder.ConstructSequence(
                       DerEncoder.SegmentedEncodeUnsignedInteger(s_versionNumberBytes),
                       DerEncoder.SegmentedEncodeUnsignedInteger(parameters.Modulus),
                       DerEncoder.SegmentedEncodeUnsignedInteger(parameters.Exponent),
                       DerEncoder.SegmentedEncodeUnsignedInteger(parameters.D),
                       DerEncoder.SegmentedEncodeUnsignedInteger(parameters.P),
                       DerEncoder.SegmentedEncodeUnsignedInteger(parameters.Q),
                       DerEncoder.SegmentedEncodeUnsignedInteger(parameters.DP),
                       DerEncoder.SegmentedEncodeUnsignedInteger(parameters.DQ),
                       DerEncoder.SegmentedEncodeUnsignedInteger(parameters.InverseQ)));
        }
Exemplo n.º 7
0
        internal static byte[] ToPrivateKeyBlob(this DSAParameters parameters)
        {
            Debug.Assert(parameters.X != null);

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

            return(DerEncoder.ConstructSequence(
                       DerEncoder.SegmentedEncodeUnsignedInteger(new byte[] { 0 }),
                       DerEncoder.SegmentedEncodeUnsignedInteger(parameters.P),
                       DerEncoder.SegmentedEncodeUnsignedInteger(parameters.Q),
                       DerEncoder.SegmentedEncodeUnsignedInteger(parameters.G),
                       DerEncoder.SegmentedEncodeUnsignedInteger(parameters.Y),
                       DerEncoder.SegmentedEncodeUnsignedInteger(parameters.X)));
        }