internal static void ReadPkcs1PublicBlob(this DerSequenceReader subjectPublicKey, ref RSAParameters parameters) { parameters.Modulus = KeyBlobHelpers.TrimPaddingByte(subjectPublicKey.ReadIntegerBytes()); parameters.Exponent = KeyBlobHelpers.TrimPaddingByte(subjectPublicKey.ReadIntegerBytes()); if (subjectPublicKey.HasData) { throw new CryptographicException(); } }
internal static void ReadSubjectPublicKeyInfo( this DerSequenceReader algParameters, byte[] publicKeyBlob, ref DSAParameters parameters) { parameters.P = algParameters.ReadIntegerBytes(); parameters.Q = algParameters.ReadIntegerBytes(); parameters.G = algParameters.ReadIntegerBytes(); DerSequenceReader privateKeyReader = DerSequenceReader.CreateForPayload(publicKeyBlob); parameters.Y = privateKeyReader.ReadIntegerBytes(); KeyBlobHelpers.TrimPaddingByte(ref parameters.P); KeyBlobHelpers.TrimPaddingByte(ref parameters.Q); KeyBlobHelpers.PadOrTrim(ref parameters.G, parameters.P.Length); KeyBlobHelpers.PadOrTrim(ref parameters.Y, parameters.P.Length); }
private byte[] ConvertToApiFormat(byte[] input, int inputOffset, int inputCount) { int size = GetSignatureFieldSize(); try { DerSequenceReader reader = new DerSequenceReader(input, inputOffset, inputCount); byte[] rDer = reader.ReadIntegerBytes(); byte[] sDer = reader.ReadIntegerBytes(); byte[] response = new byte[2 * size]; CopySignatureField(rDer, response, 0, size); CopySignatureField(sDer, response, size, size); return(response); } catch (InvalidOperationException e) { throw new CryptographicException(SR.Arg_CryptographyException, e); } }
internal static void ReadPkcs8Blob(this DerSequenceReader reader, ref DSAParameters parameters) { // Since the PKCS#8 blob for DSS/DSA does not include the public key (Y) this // structure is only read after filling the public half. Debug.Assert(parameters.P != null); Debug.Assert(parameters.Q != null); Debug.Assert(parameters.G != null); Debug.Assert(parameters.Y != null); // OneAsymmetricKey ::= SEQUENCE { // version Version, // privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, // privateKey PrivateKey, // attributes [0] Attributes OPTIONAL, // ..., // [[2: publicKey [1] PublicKey OPTIONAL ]], // ... // } // // PrivateKeyInfo ::= OneAsymmetricKey // // PrivateKey ::= OCTET STRING int version = reader.ReadInteger(); // We understand both version 0 and 1 formats, // which are now known as v1 and v2, respectively. if (version > 1) { throw new CryptographicException(); } { // Ensure we're reading DSA, extract the parameters DerSequenceReader algorithm = reader.ReadSequence(); string algorithmOid = algorithm.ReadOidAsString(); if (algorithmOid != s_idDsa.Value) { throw new CryptographicException(); } // The Dss-Params SEQUENCE is present here, but not needed since // we got it from the public key already. } byte[] privateKeyBlob = reader.ReadOctetString(); DerSequenceReader privateKeyReader = DerSequenceReader.CreateForPayload(privateKeyBlob); parameters.X = privateKeyReader.ReadIntegerBytes(); }
private static void ReadPkcs1PrivateBlob(byte[] privateKeyBytes, ref RSAParameters parameters) { // RSAPrivateKey::= SEQUENCE { // version Version, // modulus INTEGER, --n // publicExponent INTEGER, --e // privateExponent INTEGER, --d // prime1 INTEGER, --p // prime2 INTEGER, --q // exponent1 INTEGER, --d mod(p - 1) // exponent2 INTEGER, --d mod(q - 1) // coefficient INTEGER, --(inverse of q) mod p // otherPrimeInfos OtherPrimeInfos OPTIONAL // } DerSequenceReader privateKey = new DerSequenceReader(privateKeyBytes); int version = privateKey.ReadInteger(); if (version != 0) { throw new CryptographicException(); } parameters.Modulus = KeyBlobHelpers.TrimPaddingByte(privateKey.ReadIntegerBytes()); parameters.Exponent = KeyBlobHelpers.TrimPaddingByte(privateKey.ReadIntegerBytes()); int modulusLen = parameters.Modulus.Length; // Add one so that odd byte-length values (RSA 1032) get padded correctly. int halfModulus = (modulusLen + 1) / 2; parameters.D = KeyBlobHelpers.PadOrTrim(privateKey.ReadIntegerBytes(), modulusLen); parameters.P = KeyBlobHelpers.PadOrTrim(privateKey.ReadIntegerBytes(), halfModulus); parameters.Q = KeyBlobHelpers.PadOrTrim(privateKey.ReadIntegerBytes(), halfModulus); parameters.DP = KeyBlobHelpers.PadOrTrim(privateKey.ReadIntegerBytes(), halfModulus); parameters.DQ = KeyBlobHelpers.PadOrTrim(privateKey.ReadIntegerBytes(), halfModulus); parameters.InverseQ = KeyBlobHelpers.PadOrTrim(privateKey.ReadIntegerBytes(), halfModulus); if (privateKey.HasData) { throw new CryptographicException(); } }
private byte[] ConvertToApiFormat(byte[] input, int inputOffset, int inputCount) { int size = GetSignatureFieldSize(); try { DerSequenceReader reader = new DerSequenceReader(input, inputOffset, inputCount); byte[] rDer = reader.ReadIntegerBytes(); byte[] sDer = reader.ReadIntegerBytes(); byte[] response = new byte[2 * size]; CopySignatureField(rDer, response, 0, size); CopySignatureField(sDer, response, size, size); return response; } catch (InvalidOperationException e) { throw new CryptographicException(SR.Arg_CryptographyException, e); } }
/// <summary> /// Convert Der format of (r, s) to Ieee1363 format /// </summary> public static byte[] ConvertDerToIeee1363(byte[] input, int inputOffset, int inputCount, int fieldSizeBits) { int size = BitsToBytes(fieldSizeBits); try { DerSequenceReader reader = new DerSequenceReader(input, inputOffset, inputCount); byte[] rDer = reader.ReadIntegerBytes(); byte[] sDer = reader.ReadIntegerBytes(); byte[] response = new byte[2 * size]; CopySignatureField(rDer, response, 0, size); CopySignatureField(sDer, response, size, size); return response; } catch (InvalidOperationException e) { throw new CryptographicException(SR.Arg_CryptographyException, e); } }