private void Serialise() { if (_text == null && _data != null) { var serialFormat = (SerialFormat)SysProps.GetValue(SysPropName.SAlg, 0); switch (serialFormat) { case SerialFormat.Binary: // try Binary serialiser _text = BinarySerializerHelper.SerializeToString(_data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Binary); break; case SerialFormat.Soap: // try Soap serialiser _text = SoapSerializerHelper.SerializeToString(_data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Soap); break; case SerialFormat.Json: // try Json serialiser _text = JsonSerializerHelper.SerializeToString(_data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Json); break; case SerialFormat.Xml: try { _text = XmlSerializerHelper.SerializeToString(_dataTypeType, _data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Xml); } catch (Exception excp) { throw new ApplicationException( "The XmlSerializer has thrown an exception: '" + excp.GetType().Name + "'. " + "If your intent was to use the BinaryFormatter or SoapFormatter for serialisation, " + "then you should set the SerialFormat property appropriately.", excp); } break; default: // use default xml serialiser try { _text = XmlSerializerHelper.SerializeToString(_dataTypeType, _data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Xml); } catch (Exception excp) { throw new ApplicationException( "The XmlSerializer has thrown an exception: '" + excp.GetType().Name + "'. " + "If your intent was to use the BinaryFormatter or SoapFormatter for serialisation, " + "then you should set the SerialFormat property appropriately.", excp); } break; } } }
private void Decrypt() { if (_zData == null) { // do asymmetric decryption 1st, if required if (_xData == null) { var yAlg = SysProps.GetValue(SysPropName.YAlg, 0); var yrki = SysProps.GetValue <string>(SysPropName.YRKI, null); if ((yAlg > 0) && (yrki != null)) { _xData = _cryptoManager.DecryptWithSecretKey(yrki, YData); } else { _xData = YData; } //_YData = null; } // now do symmetric decryption 2nd, if required var xAlg = SysProps.GetValue(SysPropName.XAlg, 0); var xtki = SysProps.GetValue <string>(SysPropName.XTKI, null); if ((xAlg > 0) && (xtki != null)) { _zData = _cryptoManager.DecryptWithTranspKey(xtki, _xData); } else { _zData = _xData; } _xData = null; } }
private void Deserialise(Type userDataType) { if (_data == null) { // decompress 1st if required Decompress(); // now deserialise Type dataTypeType = userDataType ?? _dataTypeType; if ((dataTypeType == null) && !String.IsNullOrEmpty(DataTypeName)) { dataTypeType = Type.GetType(DataTypeName); } var serialFormat = (SerialFormat)SysProps.GetValue(SysPropName.SAlg, (int)SerialFormat.Undefined); if (String.IsNullOrEmpty(_text)) { // null data object _data = null; } else if (serialFormat == SerialFormat.Binary) { // use Binary deserialiser _data = BinarySerializerHelper.DeserializeFromString(_text); } else if (serialFormat == SerialFormat.Soap) { // use Soap deserialiser _data = SoapSerializerHelper.DeserializeFromString(_text); } else if (serialFormat == SerialFormat.Json) { // use Json deserialiser _data = JsonSerializerHelper.DeserializeFromString(dataTypeType, _text); } else if (dataTypeType != null) { // try default xml serialiser _data = XmlSerializerHelper.DeserializeFromString(dataTypeType, _text); } else { throw new ApplicationException("Cannot deserialise!"); } } }
// mutable props private void Authenticate() { if (_ySignedState == 0) { var yAlg = SysProps.GetValue(SysPropName.YAlg, 0); var yski = SysProps.GetValue <string>(SysPropName.YSKI, null); if ((yAlg > 0) && (yski != null)) { if (_cryptoManager.VerifySignature(yski, YData, YSign)) { _ySignedState = 1; // success } } else { // cannot authenticate _ySignedState = -1; } } }
/// <summary> /// /// </summary> public void Freeze() { if (_frozen) { return; } if (Name == null) { throw new ApplicationException("Item name not set!"); } TimeSpan maxLifetime = DateTimeOffset.MaxValue - DateTimeOffset.Now - TimeSpan.FromDays(1); if (_lifetime > maxLifetime) { _lifetime = maxLifetime; } if (_lifetime < TimeSpan.Zero) { _lifetime = TimeSpan.Zero; } Created = DateTimeOffset.Now; Expires = Created.Add(_lifetime); // serialise the data if required Serialise(); if (DataTypeName == null) { DataTypeName = ""; } if (_text == null) { //_Text = ""; SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Undefined); } SysProps.Set(SysPropName.TLen, _text?.Length ?? 0); // compress the data _zData = CompressionHelper.CompressToBuffer(_text); SysProps.Set(SysPropName.ZAlg, 1); SysProps.Set(SysPropName.ZLen, _zData?.Length ?? 0); // do symmetric encryption 1st, if required var xtki = SysProps.GetValue <String>(SysPropName.XTKI, null); if (xtki != null) { _xData = _cryptoManager.EncryptWithTranspKey(xtki, _zData); SysProps.Set(SysPropName.XAlg, 1); } else { _xData = _zData; } SysProps.Set(SysPropName.XLen, _xData?.Length ?? 0); // do asymmetric encryption 2nd, if required var yrki = SysProps.GetValue <String>(SysPropName.YRKI, null); if (yrki != null) { SysProps.Set(SysPropName.YAlg, 1); YData = _cryptoManager.EncryptWithPublicKey(yrki, _xData); } else { YData = _xData; } YDataHash = CalculateBufferHash(YData); SysProps.Set(SysPropName.YLen, YData?.Length ?? 0); // do public signature 3rd, if required var yski = SysProps.GetValue <String>(SysPropName.YSKI, null); if (yski != null) { SysProps.Set(SysPropName.YAlg, 1); YSign = _cryptoManager.CreateSignature(yski, YData); } // add other publisher properties SysProps.Set(SysPropName.ApplName, _moduleInfo.ApplName); SysProps.Set(SysPropName.ApplFVer, _moduleInfo.ApplFVer); SysProps.Set(SysPropName.ApplPTok, _moduleInfo.ApplPTok); SysProps.Set(SysPropName.CoreFVer, _moduleInfo.CoreFVer); SysProps.Set(SysPropName.CorePTok, _moduleInfo.CorePTok); SysProps.Set(SysPropName.HostName, _moduleInfo.HostName); SysProps.Set(SysPropName.UserName, _moduleInfo.UserName); SysProps.Set(SysPropName.UserWDom, _moduleInfo.UserWDom); SysProps.Set(SysPropName.UserIdentity, _moduleInfo.Name); SysProps.Set(SysPropName.UserFullName, _moduleInfo.UserFullName); SysProps.Set(SysPropName.OrgEnvId, EnvHelper.EnvName(_moduleInfo.ConfigEnv)); SysProps.Set(SysPropName.NodeGuid, _moduleInfo.NodeGuid); // done _frozen = true; }