Exemplo n.º 1
0
        private static void AddToDomainCertificateHash(byte[] domainName, byte[] certificateHash)
        {
            CertificateHashMapEntry certHashMapEntry;

            byte[] certHashMapEntrySerialized = StorageUtil.readFromStorage(domainName);
            if (certHashMapEntrySerialized == null)
            {
                certHashMapEntry = new CertificateHashMapEntry();
                certHashMapEntry.CertificateHashList = new List <CertificateHashEntry>();
            }
            else
            {
                certHashMapEntry = (CertificateHashMapEntry)SerializationUtil.Deserialize(certHashMapEntrySerialized);
            }

            foreach (var certificateHashEntry in certHashMapEntry.CertificateHashList)
            {
                if (ArrayUtil.AreEqual(certificateHashEntry.CertificateHash, certificateHash))
                {
                    return;
                }
            }

            CertificateHashEntry newCertHashEntry = new CertificateHashEntry();

            newCertHashEntry.CertificateHash = certificateHash;
            newCertHashEntry.IsCa            = false;
            certHashMapEntry.CertificateHashList.Add(newCertHashEntry);
            //certHashMapEntry.Count += 1;

            certHashMapEntrySerialized = SerializationUtil.Serialize(certHashMapEntry);

            StorageUtil.saveToStorage(domainName, certHashMapEntrySerialized);
        }
Exemplo n.º 2
0
        private static RSAParameters?ParsePrivateKey(byte[] buffer)
        {
            // Encoded OID sequence for PKCS #1 rsaEncryption szOID_RSA_RSA = "1.2.840.113549.1.1.1"
            byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };

            using (var stream = new MemoryStream(buffer))
                using (var reader = new BinaryReader(stream))
                {
                    var twobytes = reader.ReadUInt16();
                    if (twobytes == 0x8130)
                    {                      //data read as little endian order (actual data order for Sequence is 30 81)
                        reader.ReadByte(); //advance 1 byte
                    }
                    else if (twobytes == 0x8230)
                    {
                        reader.ReadInt16();  //advance 2 bytes
                    }
                    else
                    {
                        return(null);
                    }

                    var onebyte = reader.ReadByte();
                    if (onebyte != 0x02)
                    {
                        return(null);
                    }

                    twobytes = reader.ReadUInt16();
                    if (twobytes != 0x0001)
                    {
                        return(null);
                    }

                    var seq = reader.ReadBytes(15); //read the Sequence OID
                    if (!ArrayUtil.AreEqual(seq, SeqOID))
                    {                               //make sure Sequence for OID is correct
                        return(null);
                    }

                    onebyte = reader.ReadByte();
                    if (onebyte != 0x04)
                    { //expect an Octet string
                        return(null);
                    }

                    onebyte = reader.ReadByte(); //read next byte, or next 2 bytes is 0x81 or 0x82; otherwise bt is the byte count
                    if (onebyte == 0x81)
                    {
                        reader.ReadByte();
                    }
                    else if (onebyte == 0x82)
                    {
                        reader.ReadUInt16();
                    }

                    //at this stage, the remaining sequence should be the RSA private key
                    return(ParseRsaPrivateKey(reader.ReadBytes((int)(buffer.Length - stream.Position))));
                }
        }
Exemplo n.º 3
0
        public override bool Equals(object obj)
        {
            if (typeof(MatchResult) != obj.GetType())
            {
                return(false);
            }
            var other = (MatchResult)obj;
            var equal = Resolution == other.Resolution;

            equal &= WinningPlayerID == other.WinningPlayerID;
            equal &= ArrayUtil.AreEqual(PlayerStats, other.PlayerStats);
            return(equal);
        }
Exemplo n.º 4
0
        public override bool Equals(object obj)
        {
            if (typeof(MatchConfig) != obj.GetType())
            {
                return(false);
            }
            var other = (MatchConfig)obj;
            var equal = StageID == other.StageID;

            equal &= Time == other.Time;
            equal &= ArrayUtil.AreEqual(PlayerConfigs, other.PlayerConfigs);
            return(equal);
        }
        private static bool checkDnsValues(Certificate fakeButValidCertificate, Certificate signerCertificate)
        {
            bool signerCertificateContainFakeButValidCertificateDnsEntry = false;

            foreach (byte[] fakeButValidCertificateDnsName in fakeButValidCertificate.DNsNames)
            {
                foreach (byte[] signerCertificateDNsName in signerCertificate.DNsNames)
                {
                    if (ArrayUtil.AreEqual(signerCertificateDNsName, fakeButValidCertificateDnsName))
                    {
                        signerCertificateContainFakeButValidCertificateDnsEntry = true;
                    }
                }
            }

            return(signerCertificateContainFakeButValidCertificateDnsEntry);
        }
 private static bool SignatureAlgorithmFieldIsSameWithTBSCertificateSignatureAlgorithmField(
     Certificate certificate)
 {
     return(ArrayUtil.AreEqual(certificate.SignatureAlgorithm, certificate.TBSSignatureAlgorithm));
 }
Exemplo n.º 7
0
        private static RSAParameters?ParseEncryptedPrivateKey(byte[] buffer, string password)
        {
            // Encoded OID sequence for  PKCS #1 rsaEncryption szOID_RSA_RSA = "1.2.840.113549.1.1.1"
            byte[] OIDpkcs5PBES2  = { 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x0D };
            byte[] OIDpkcs5PBKDF2 = { 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x05, 0x0C };
            byte[] OIDdesEDE3CBC  = { 0x06, 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03, 0x07 };

            using (var stream = new MemoryStream(buffer))
                using (var reader = new BinaryReader(stream))
                {
                    var twobytes = reader.ReadUInt16();
                    if (twobytes == 0x8130)
                    {                      //data read as little endian order (actual data order for Sequence is 30 81)
                        reader.ReadByte(); //advance 1 byte
                    }
                    else if (twobytes == 0x8230)
                    {
                        reader.ReadInt16();  //advance 2 bytes
                    }
                    else
                    {
                        return(null);
                    }

                    twobytes = reader.ReadUInt16(); //inner sequence
                    if (twobytes == 0x8130)
                    {
                        reader.ReadByte();
                    }
                    else if (twobytes == 0x8230)
                    {
                        reader.ReadInt16();
                    }

                    var seq = reader.ReadBytes(11); //read the Sequence OID
                    if (!ArrayUtil.AreEqual(seq, OIDpkcs5PBES2))
                    {                               //is it a OIDpkcs5PBES2 ?
                        return(null);
                    }

                    twobytes = reader.ReadUInt16(); //inner sequence for pswd salt
                    if (twobytes == 0x8130)
                    {
                        reader.ReadByte();
                    }
                    else if (twobytes == 0x8230)
                    {
                        reader.ReadInt16();
                    }

                    twobytes = reader.ReadUInt16(); //inner sequence for pswd salt
                    if (twobytes == 0x8130)
                    {
                        reader.ReadByte();
                    }
                    else if (twobytes == 0x8230)
                    {
                        reader.ReadInt16();
                    }

                    seq = reader.ReadBytes(11); //read the Sequence OID
                    if (!ArrayUtil.AreEqual(seq, OIDpkcs5PBKDF2))
                    {                           //is it a OIDpkcs5PBKDF2 ?
                        return(null);
                    }

                    twobytes = reader.ReadUInt16();
                    if (twobytes == 0x8130)
                    {
                        reader.ReadByte();
                    }
                    else if (twobytes == 0x8230)
                    {
                        reader.ReadInt16();
                    }

                    var onebyte = reader.ReadByte();
                    if (onebyte != 0x04)
                    { //expect octet string for salt
                        return(null);
                    }

                    onebyte = reader.ReadByte();
                    var salt = reader.ReadBytes(onebyte);

                    onebyte = reader.ReadByte();
                    if (onebyte != 0x02)
                    { //expect an integer for PBKF2 interation count
                        return(null);
                    }

                    int iterations = 0;
                    onebyte = reader.ReadByte(); //PBKD2 iterations should fit in 2 bytes.
                    if (onebyte == 1)
                    {
                        iterations = reader.ReadByte();
                    }
                    else if (onebyte == 2)
                    {
                        iterations = 256 * reader.ReadByte() + reader.ReadByte();
                    }
                    else
                    {
                        return(null);
                    }

                    twobytes = reader.ReadUInt16();
                    if (twobytes == 0x8130)
                    {
                        reader.ReadByte();
                    }
                    else if (twobytes == 0x8230)
                    {
                        reader.ReadInt16();
                    }

                    seq = reader.ReadBytes(10); //read the Sequence OID
                    if (!ArrayUtil.AreEqual(seq, OIDdesEDE3CBC))
                    {                           //is it a OIDdes-EDE3-CBC ?
                        return(null);
                    }

                    onebyte = reader.ReadByte();
                    if (onebyte != 0x04)
                    { //expect octet string for IV
                        return(null);
                    }

                    onebyte = reader.ReadByte(); // IV byte size should fit in one byte (24 expected for 3DES)
                    var iv = reader.ReadBytes(onebyte);

                    onebyte = reader.ReadByte();
                    if (onebyte != 0x04)
                    { // expect octet string for encrypted PKCS8 data
                        return(null);
                    }

                    int encblobsize = 0;
                    onebyte = reader.ReadByte();
                    if (onebyte == 0x81)
                    {
                        encblobsize = reader.ReadByte(); // data size in next byte
                    }
                    else if (onebyte == 0x82)
                    {
                        encblobsize = 256 * reader.ReadByte() + reader.ReadByte();
                    }
                    else
                    {
                        encblobsize = onebyte;          // we already have the data size
                    }

                    var encryptData = reader.ReadBytes(encblobsize);
                    var decryptData = DecryptPrivateKeyData(encryptData, salt, iv, password, iterations);
                    if (decryptData == null)
                    { // probably a bad pswd entered.
                        return(null);
                    }

                    return(ParsePrivateKey(decryptData));
                }
        }
Exemplo n.º 8
0
        private static RSAParameters?ParsePublicKey(byte[] buffer)
        {
            // Encoded OID sequence for  PKCS #1 rsaEncryption szOID_RSA_RSA = "1.2.840.113549.1.1.1"
            byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };

            using (var stream = new MemoryStream(buffer))
                using (var reader = new BinaryReader(stream))
                {
                    var twobytes = reader.ReadUInt16();
                    if (twobytes == 0x8130)
                    {                      //data read as little endian order (actual data order for Sequence is 30 81)
                        reader.ReadByte(); //advance 1 byte
                    }
                    else if (twobytes == 0x8230)
                    {
                        reader.ReadInt16();  //advance 2 bytes
                    }
                    else
                    {
                        return(null);
                    }

                    var seq = reader.ReadBytes(15); //read the Sequence OID
                    if (!ArrayUtil.AreEqual(seq, SeqOID))
                    {                               //make sure Sequence for OID is correct
                        return(null);
                    }

                    twobytes = reader.ReadUInt16();
                    if (twobytes == 0x8103)
                    {                      //data read as little endian order (actual data order for Bit String is 03 81)
                        reader.ReadByte(); //advance 1 byte
                    }
                    else if (twobytes == 0x8203)
                    {
                        reader.ReadInt16();  //advance 2 bytes
                    }
                    else
                    {
                        return(null);
                    }

                    var b = reader.ReadByte();
                    if (b != 0x00)
                    { //expect null byte next
                        return(null);
                    }

                    twobytes = reader.ReadUInt16();
                    if (twobytes == 0x8130)
                    {                      //data read as little endian order (actual data order for Sequence is 30 81)
                        reader.ReadByte(); //advance 1 byte
                    }
                    else if (twobytes == 0x8230)
                    {
                        reader.ReadInt16();  //advance 2 bytes
                    }
                    else
                    {
                        return(null);
                    }

                    byte low  = 0x00;
                    byte high = 0x00;
                    twobytes = reader.ReadUInt16();
                    if (twobytes == 0x8102)
                    {                            //data read as little endian order (actual data order for Integer is 02 81)
                        low = reader.ReadByte(); // read next bytes which is bytes in modulus
                    }
                    else if (twobytes == 0x8202)
                    {
                        high = reader.ReadByte(); //advance 2 bytes
                        low  = reader.ReadByte();
                    }
                    else
                    {
                        return(null);
                    }

                    byte[] modint  = { low, high, 0x00, 0x00 }; //reverse byte order since asn.1 key uses big endian order
                    int    modsize = BitConverter.ToInt32(modint, 0);

                    byte first = reader.ReadByte();
                    reader.BaseStream.Seek(-1, SeekOrigin.Current);

                    if (first == 0x00)
                    {                      //if first byte (highest order) of modulus is zero, don't include it
                        reader.ReadByte(); //skip this null byte
                        modsize -= 1;      //reduce modulus buffer size by 1
                    }

                    var modulus = reader.ReadBytes(modsize); //read the modulus bytes

                    if (reader.ReadByte() != 0x02)
                    { //expect an Integer for the exponent data
                        return(null);
                    }

                    int expbytes = reader.ReadByte(); // should only need one byte for actual exponent data (for all useful values)
                    var exponent = reader.ReadBytes(expbytes);

                    var parameters = new RSAParameters {
                        Modulus = modulus, Exponent = exponent
                    };

                    // validate rsa parameters
                    using (RSA rsa = RSA.Create())
                    { rsa.ImportParameters(parameters); }

                    return(parameters);
                }
        }