示例#1
0
        public static SDPSecurityDescription CreateNew(uint tag = 1, CryptoSuites cryptoSuite = CryptoSuites.AES_CM_128_HMAC_SHA1_80)
        {
            SDPSecurityDescription secdesc = new SDPSecurityDescription(tag, cryptoSuite);

            secdesc.KeyParams.Add(KeyParameter.CreateNew(cryptoSuite));
            return(secdesc);
        }
示例#2
0
            public static bool TryParse(string keyParamString, out KeyParameter keyParam,
                                        CryptoSuites cryptoSuite = CryptoSuites.AES_CM_128_HMAC_SHA1_80)
            {
                keyParam = null;

                if (!string.IsNullOrWhiteSpace(keyParamString))
                {
                    string p = keyParamString.Trim();
                    try
                    {
                        if (p.StartsWith(KEY_METHOD))
                        {
                            string sKeyMethod = KEY_METHOD;
                            int    poscln     = p.IndexOf(COLON);
                            if (poscln == sKeyMethod.Length)
                            {
                                string sKeyInfo = p.Substring(poscln + 1);
                                if (!sKeyInfo.Contains(";"))
                                {
                                    checkValidKeyInfoCharacters(keyParamString, sKeyInfo);
                                    string sMkiVal, sMkiLen, sLifeTime, sBase64KeySalt;
                                    parseKeyInfo(keyParamString, sKeyInfo, out sMkiVal, out sMkiLen, out sLifeTime,
                                                 out sBase64KeySalt);
                                    if (!string.IsNullOrWhiteSpace(sBase64KeySalt))
                                    {
                                        byte[] bKey, bSalt;
                                        parseKeySaltBase64(cryptoSuite, sBase64KeySalt, out bKey, out bSalt);

                                        keyParam = new KeyParameter(bKey, bSalt);
                                        if (!string.IsNullOrWhiteSpace(sMkiVal) && !string.IsNullOrWhiteSpace(sMkiLen))
                                        {
                                            keyParam.MkiValue  = uint.Parse(sMkiVal);
                                            keyParam.MkiLength = uint.Parse(sMkiLen);
                                        }

                                        if (!string.IsNullOrWhiteSpace(sLifeTime))
                                        {
                                            if (sLifeTime.Contains('^'))
                                            {
                                                keyParam.LifeTimeString = sLifeTime;
                                            }
                                            else
                                            {
                                                keyParam.LifeTime = uint.Parse(sLifeTime);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    catch
                    {
                        //catch all errors and throw own FormatException
                    }
                }

                return(false);
            }
示例#3
0
 public SDPSecurityDescription(uint tag, CryptoSuites cryptoSuite)
 {
     this.Tag         = tag;
     this.CryptoSuite = cryptoSuite;
     this.KeyParams   = new List <KeyParameter>();
 }
示例#4
0
            public static SessionParameter Parse(string sessionParam,
                                                 CryptoSuites cryptoSuite = CryptoSuites.AES_CM_128_HMAC_SHA1_80)
            {
                if (string.IsNullOrWhiteSpace(sessionParam))
                {
                    return(null);
                }

                string p = sessionParam.Trim();

                try
                {
                    SessionParameter.SrtpSessionParams paramType = SrtpSessionParams.unknown;
                    if (p.StartsWith(KDR_PREFIX))
                    {
                        string sKdr = p.Substring(KDR_PREFIX.Length);
                        uint   kdr  = 0;
                        if (uint.TryParse(sKdr, out kdr))
                        {
                            return(new SessionParameter(SrtpSessionParams.kdr)
                            {
                                Kdr = kdr
                            });
                        }
                    }
                    else if (p.StartsWith(WSH_PREFIX))
                    {
                        string sWsh = p.Substring(WSH_PREFIX.Length);
                        uint   wsh  = 0;
                        if (uint.TryParse(sWsh, out wsh))
                        {
                            return(new SessionParameter(SrtpSessionParams.wsh)
                            {
                                Wsh = wsh
                            });
                        }
                    }
                    else if (p.StartsWith(FEC_KEY_PREFIX))
                    {
                        string       sFecKey = p.Substring(FEC_KEY_PREFIX.Length);
                        KeyParameter fecKey  = KeyParameter.Parse(sFecKey, cryptoSuite);
                        return(new SessionParameter(SrtpSessionParams.fec_key)
                        {
                            FecKey = fecKey
                        });
                    }
                    else if (p.StartsWith(FEC_ORDER_PREFIX))
                    {
                        string sFecOrder = p.Substring(FEC_ORDER_PREFIX.Length);
                        SessionParameter.FecTypes fecOrder =
                            (from e in Enum.GetNames(typeof(FecTypes))
                             where e.CompareTo(sFecOrder) == 0
                             select(FecTypes) Enum.Parse(typeof(FecTypes), e)).FirstOrDefault();
                        if (fecOrder == FecTypes.unknown)
                        {
                            throw new FormatException(
                                      $"sessionParam '{sessionParam}' is not recognized as a valid SRTP_SESSION_PARAM ");
                        }

                        return(new SessionParameter(SrtpSessionParams.fec_order)
                        {
                            FecOrder = fecOrder
                        });
                    }
                    else
                    {
                        paramType = (from e in Enum.GetNames(typeof(SrtpSessionParams))
                                     where e.CompareTo(p) == 0
                                     select(SrtpSessionParams) Enum.Parse(typeof(SrtpSessionParams), e)).FirstOrDefault();
                        if (paramType == SrtpSessionParams.unknown)
                        {
                            throw new FormatException(
                                      $"sessionParam '{sessionParam}' is not recognized as a valid SRTP_SESSION_PARAM ");
                        }

                        switch (paramType)
                        {
                        case SrtpSessionParams.UNAUTHENTICATED_SRTP:
                        case SrtpSessionParams.UNENCRYPTED_SRTCP:
                        case SrtpSessionParams.UNENCRYPTED_SRTP:
                            return(new SessionParameter(paramType));
                        }
                    }
                }
                catch
                {
                    //catch all errors and throw own FormatException
                }

                throw new FormatException(
                          $"sessionParam '{sessionParam}' is not recognized as a valid SRTP_SESSION_PARAM ");
            }
示例#5
0
            public static KeyParameter CreateNew(CryptoSuites cryptoSuite, string key = null, string salt = null)
            {
                switch (cryptoSuite)
                {
                case CryptoSuites.AES_CM_128_HMAC_SHA1_32:
                case CryptoSuites.AES_CM_128_HMAC_SHA1_80:
                case CryptoSuites.F8_128_HMAC_SHA1_80:
                    if (string.IsNullOrWhiteSpace(key))
                    {
                        key = Sys.Crypto.GetRandomString(128 / 8);
                    }

                    if (string.IsNullOrWhiteSpace(salt))
                    {
                        salt = Sys.Crypto.GetRandomString(112 / 8);
                    }

                    return(new KeyParameter(key, salt));

                case CryptoSuites.AES_192_CM_HMAC_SHA1_80:
                case CryptoSuites.AES_192_CM_HMAC_SHA1_32:
                case CryptoSuites.AES_CM_192_HMAC_SHA1_80:
                case CryptoSuites.AES_CM_192_HMAC_SHA1_32:
                    if (string.IsNullOrWhiteSpace(key))
                    {
                        key = Sys.Crypto.GetRandomString(192 / 8);
                    }

                    if (string.IsNullOrWhiteSpace(salt))
                    {
                        salt = Sys.Crypto.GetRandomString(112 / 8);
                    }

                    return(new KeyParameter(key, salt));

                case CryptoSuites.AEAD_AES_128_GCM:
                    if (string.IsNullOrWhiteSpace(key))
                    {
                        key = Sys.Crypto.GetRandomString(128 / 8);
                    }

                    if (string.IsNullOrWhiteSpace(salt))
                    {
                        salt = Sys.Crypto.GetRandomString(96 / 8);
                    }

                    return(new KeyParameter(key, salt));

                case CryptoSuites.AEAD_AES_256_GCM:
                    if (string.IsNullOrWhiteSpace(key))
                    {
                        key = Sys.Crypto.GetRandomString(256 / 8);
                    }

                    if (string.IsNullOrWhiteSpace(salt))
                    {
                        salt = Sys.Crypto.GetRandomString(96 / 8);
                    }

                    return(new KeyParameter(key, salt));

                case CryptoSuites.AES_256_CM_HMAC_SHA1_80:
                case CryptoSuites.AES_256_CM_HMAC_SHA1_32:
                case CryptoSuites.AES_CM_256_HMAC_SHA1_80:
                case CryptoSuites.AES_CM_256_HMAC_SHA1_32:
                    if (string.IsNullOrWhiteSpace(key))
                    {
                        key = Sys.Crypto.GetRandomString(256 / 8);
                    }

                    if (string.IsNullOrWhiteSpace(salt))
                    {
                        salt = Sys.Crypto.GetRandomString(112 / 8);
                    }

                    return(new KeyParameter(key, salt));
                }

                return(null);
            }
示例#6
0
            private static void parseKeySaltBase64(CryptoSuites cryptoSuite, string base64KeySalt, out byte[] key,
                                                   out byte[] salt)
            {
                byte[] keysalt = Convert.FromBase64String(base64KeySalt);
                key = null;
                switch (cryptoSuite)
                {
                case CryptoSuites.AES_CM_128_HMAC_SHA1_32:
                case CryptoSuites.AES_CM_128_HMAC_SHA1_80:
                case CryptoSuites.F8_128_HMAC_SHA1_80:
                case CryptoSuites.AEAD_AES_128_GCM:
                    key = new byte[128 / 8];
                    Array.Copy(keysalt, 0, key, 0, 128 / 8);
                    break;

                case CryptoSuites.AES_192_CM_HMAC_SHA1_80:
                case CryptoSuites.AES_192_CM_HMAC_SHA1_32:
                case CryptoSuites.AES_CM_192_HMAC_SHA1_80:
                case CryptoSuites.AES_CM_192_HMAC_SHA1_32:
                    key = new byte[192 / 8];
                    Array.Copy(keysalt, 0, key, 0, 192 / 8);
                    break;

                case CryptoSuites.AEAD_AES_256_GCM:
                case CryptoSuites.AES_256_CM_HMAC_SHA1_80:
                case CryptoSuites.AES_256_CM_HMAC_SHA1_32:
                case CryptoSuites.AES_CM_256_HMAC_SHA1_80:
                case CryptoSuites.AES_CM_256_HMAC_SHA1_32:
                    key = new byte[256 / 8];
                    Array.Copy(keysalt, 0, key, 0, 256 / 8);
                    break;
                }

                salt = null;
                switch (cryptoSuite)
                {
                case CryptoSuites.AES_CM_128_HMAC_SHA1_32:
                case CryptoSuites.AES_CM_128_HMAC_SHA1_80:
                case CryptoSuites.F8_128_HMAC_SHA1_80:
                    salt = new byte[112 / 8];
                    Array.Copy(keysalt, 128 / 8, salt, 0, 112 / 8);
                    break;

                case CryptoSuites.AES_192_CM_HMAC_SHA1_80:
                case CryptoSuites.AES_192_CM_HMAC_SHA1_32:
                case CryptoSuites.AES_CM_192_HMAC_SHA1_80:
                case CryptoSuites.AES_CM_192_HMAC_SHA1_32:
                    salt = new byte[112 / 8];
                    Array.Copy(keysalt, 192 / 8, salt, 0, 112 / 8);
                    break;

                case CryptoSuites.AES_256_CM_HMAC_SHA1_80:
                case CryptoSuites.AES_256_CM_HMAC_SHA1_32:
                case CryptoSuites.AES_CM_256_HMAC_SHA1_80:
                case CryptoSuites.AES_CM_256_HMAC_SHA1_32:
                    salt = new byte[256 / 8];
                    Array.Copy(keysalt, 256 / 8, salt, 0, 112 / 8);
                    break;

                case CryptoSuites.AEAD_AES_256_GCM:
                case CryptoSuites.AEAD_AES_128_GCM:
                    salt = new byte[96 / 8];
                    Array.Copy(keysalt, 128 / 8, salt, 0, 96 / 8);
                    break;
                }
            }