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 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(); } }
public override DSAParameters ExportParameters(bool includePrivateParameters) { SecKeyPair keys = GetKeys(); if (keys.PublicKey == null || (includePrivateParameters && keys.PrivateKey == null)) { throw new CryptographicException(SR.Cryptography_OpenInvalidHandle); } DSAParameters parameters = new DSAParameters(); DerSequenceReader publicKeyReader = Interop.AppleCrypto.SecKeyExport(keys.PublicKey, exportPrivate: false); publicKeyReader.ReadSubjectPublicKeyInfo(ref parameters); if (includePrivateParameters) { DerSequenceReader privateKeyReader = Interop.AppleCrypto.SecKeyExport(keys.PrivateKey, exportPrivate: true); privateKeyReader.ReadPkcs8Blob(ref parameters); } 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); if (includePrivateParameters) { KeyBlobHelpers.PadOrTrim(ref parameters.X, parameters.Q.Length); } return(parameters); }