public static bool TryParse(string cryptoLine, out SDPSecurityDescription securityDescription) { securityDescription = null; if (string.IsNullOrWhiteSpace(cryptoLine)) { return(false); } if (!cryptoLine.StartsWith(CRYPTO_ATTRIBUE_PREFIX)) { throw new FormatException($"cryptoLine '{cryptoLine}' is not recognized as a valid SDP Security Description "); } string sCryptoValue = cryptoLine.Substring(cryptoLine.IndexOf(COLON) + 1); securityDescription = new SDPSecurityDescription(); string[] sCryptoParts = sCryptoValue.Split(securityDescription.WHITE_SPACES, StringSplitOptions.RemoveEmptyEntries); if (sCryptoValue.Length < 2) { throw new FormatException($"cryptoLine '{cryptoLine}' is not recognized as a valid SDP Security Description "); } try { securityDescription.Tag = uint.Parse(sCryptoParts[0]); securityDescription.CryptoSuite = (from e in Enum.GetNames(typeof(CryptoSuites)) where e.CompareTo(sCryptoParts[1]) == 0 select(CryptoSuites) Enum.Parse(typeof(CryptoSuites), e)).FirstOrDefault(); if (securityDescription.CryptoSuite == CryptoSuites.unknown) { //this may not be a reason to return FALSE //there might be a new crypto key used } string[] sKeyParams = sCryptoParts[2].Split(SEMI_COLON); if (sKeyParams.Length < 1) { securityDescription = null; return(false); } foreach (string kp in sKeyParams) { KeyParameter keyParam = KeyParameter.Parse(kp, securityDescription.CryptoSuite); securityDescription.KeyParams.Add(keyParam); } if (sCryptoParts.Length > 3) { securityDescription.SessionParam = SessionParameter.Parse(sCryptoParts[3], securityDescription.CryptoSuite); } return(true); } catch { //catch all errors and throw own FormatException } return(false); }
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 "); }