Beispiel #1
0
 public SessionParameter(SrtpSessionParams paramType)
 {
     this.SrtpSessionParam = paramType;
 }
            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
                {
                    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);
                        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 ");
            }