static public void Clear() { if (session != null) { session.Dispose(); session = null; } if (le != null) { le.Dispose(); le = null; } session = new storageInfo(); le = new LE(); state = formState.NONE; modified = formModState.UNMODIFIED; }
static public bool saveStorage(storageInfo sess) { Ber data = new Ber(3, 0xB183651C18E500, true); data.UNKLength = true; using (data) { data.addChild(new Ber(BerClass.UNIVERSAL, BerTags.UTF8String, false, Encoding.UTF8.GetBytes("shStorage"))); data.addChild(new Ber(BerClass.UNIVERSAL, BerTags.INTEGER, false, new byte[] { 1 })); data.addChild(storageParse.saveVER1(sess)); data.addChild(new Ber(BerClass.PRIVATE, BerTags.OCTET_STRING, false, SHA256.Create().ComputeHash(data.childs.Last().makeDer()))); byte[] raw = data.makeDer(); sess.file.Seek(0, SeekOrigin.Begin); sess.file.Write(raw, 0, raw.Length); sess.file.SetLength(raw.Length); sess.file.Flush(); } return(true); }
static public storageInfo openStorage(Stream file) { Ber data = null; byte[] buf = null; storageInfo sess = new storageInfo(); sess.file = file; try { if (file.Length < 18) { pu(98); } if (file.Length > 99 * (1 << 20)) { pu(99); } try { buf = new byte[file.Length]; file.Read(buf, 0, (int)file.Length); data = new Ber(buf); } catch (Exception ex) { pu(100); } if (data.tClass != 0x3) { pu(97); } if (data.tag != 0xB183651C18E500) { pu(97); } if (data.childs.Count < 3) { pu(97); } if (data.childs[0].tag != (UInt64)BerTags.UTF8String) { pu(97); } if (Encoding.UTF8.GetString(data.childs[0].payload) != "shStorage") { pu(97); } if (data.childs[1].tag != (UInt64)BerTags.INTEGER) { pu(97); } if (data.childs[0].tClass != 0 || data.childs[1].tClass != 0) { pu(97); } if (data.childs[1].payloadLength == 1) { switch (data.childs[1].payload[0]) { case 1: if (data.childs.Count != 4) { pu(96); } if (data.childs[3].container || data.childs[3].tag != (UInt64)BerTags.OCTET_STRING || data.childs[3].tClass != (byte)BerClass.PRIVATE || data.childs[3].payloadLength != 32) { utils.pu(96); } if (!SHA256.Create().ComputeHash(data.childs[2].makeDer()).SequenceEqual(data.childs[3].payload)) { pu(96); } storageParse.parseVER1(sess, data.childs[2]); break; default: pu(100); break; } } else { pu(97); } if (data != null) { data.Dispose(); } return(sess); } catch (Exception ex) { if (data != null) { data.Dispose(); } sess.Dispose(); if (ex.Data.Contains("sh")) { switch ((int)ex.Data["sh"]) { case 96: throw new Exception("Файл поврежден."); case 97: throw new Exception("Файл не является хранилищем"); case 98: throw new Exception("Размер файла слишком мал, чтобы быть файлом программы."); case 99: throw new Exception("Размер файла слишком велик, чтобы быть файлом программы. Попробуте обновить программу и попробовать снова."); case 100: throw new Exception("Файл не поддерживается программой. Попробуте обновить программу и попробовать снова."); default: throw ex; } } else { throw ex; } } }
static public bool saveStorage(Stream file, storageInfo sess) { sess.file = file; return(saveStorage(sess)); }
static public Ber saveVER1(storageInfo sess) { int j = 77; Ber storage = new Ber(BerClass.UNIVERSAL, BerTags.SEQUENCE, true); Ber box = null; string[] ar = null; //save domains if (sess.domains.Count > 0) { box = storage.addChild(new Ber(3, 'D', true)); ar = new String[sess.domains.Keys.Count]; sess.domains.Keys.CopyTo(ar, 0); Array.Sort(ar, StringComparer.InvariantCulture); foreach (string domainName in ar) { Ber temp = box.addChild(new Ber(BerClass.PRIVATE, BerTags.OCTET_STRING, false, Encoding.UTF8.GetBytes(domainName))); for (int i = 0; i < temp.payload.Length; i++, j += 70) { temp.payload[i] ^= (byte)j; } j += 43; temp = box.addChild(new Ber(BerClass.PRIVATE, BerTags.OCTET_STRING, false, Encoding.UTF8.GetBytes(sess.domains[domainName].dns))); for (int i = 0; i < temp.payload.Length; i++, j += 73) { temp.payload[i] ^= (byte)j; } j -= 9; temp = box.addChild(new Ber(BerClass.PRIVATE, BerTags.OCTET_STRING, false, Encoding.UTF8.GetBytes(sess.domains[domainName].subs))); for (int i = 0; i < temp.payload.Length; i++, j += 79) { temp.payload[i] ^= (byte)j; } j = j * 3 - 1; } box = null; } //certs if (sess.certs.Count > 0) { box = storage.addChild(new Ber(3, 'C', true)); ar = new String[sess.certs.Keys.Count]; sess.certs.Keys.CopyTo(ar, 0); Array.Sort(ar, StringComparer.InvariantCulture); foreach (string certName in ar) { Ber temp = box.addChild(new Ber(BerClass.PRIVATE, BerTags.OCTET_STRING, false, Encoding.UTF8.GetBytes(certName))); for (int i = 0; i < temp.payload.Length; i++, j += 149) { temp.payload[i] ^= (byte)j; } j += 35; temp = box.addChild(new Ber(BerClass.PRIVATE, BerTags.OCTET_STRING, false, sess.certs[certName].cert.makeDer())); for (int i = 0; i < temp.payload.Length; i++, j += 137) { temp.payload[i] ^= (byte)j; } j -= 15; } box = null; } //save keys if (sess.keys.Count > 0) { box = storage.addChild(new Ber(3, 'K', true)); ar = new String[sess.keys.Keys.Count]; sess.keys.Keys.CopyTo(ar, 0); Array.Sort(ar, StringComparer.InvariantCulture); foreach (string keyName in ar) { Ber temp = box.addChild(new Ber(BerClass.PRIVATE, BerTags.OCTET_STRING, false, Encoding.UTF8.GetBytes(keyName))); for (int i = 0; i < temp.payload.Length; i++, j += 171) { temp.payload[i] ^= (byte)j; } j += 31; temp = box.addChild(new Ber(BerClass.PRIVATE, BerTags.OCTET_STRING, false, sess.keys[keyName].key.makeDer())); for (int i = 0; i < temp.payload.Length; i++, j += 73) { temp.payload[i] ^= (byte)j; } j -= 18; } box = null; } //save servers if (sess.servers.Count > 0) { box = storage.addChild(new Ber(3, 'S', true)); ar = new String[sess.servers.Keys.Count]; sess.servers.Keys.CopyTo(ar, 0); Array.Sort(ar, StringComparer.InvariantCulture); foreach (string keyName in ar) { Ber temp = box.addChild(new Ber(BerClass.PRIVATE, BerTags.OCTET_STRING, false, Encoding.UTF8.GetBytes(keyName))); for (int i = 0; i < temp.payload.Length; i++, j += 97) { temp.payload[i] ^= (byte)j; } j += 89; temp = box.addChild(new Ber(BerClass.PRIVATE, BerTags.OCTET_STRING, false, Encoding.UTF8.GetBytes(sess.servers[keyName].link))); for (int i = 0; i < temp.payload.Length; i++, j += 29) { temp.payload[i] ^= (byte)j; } j -= 21; temp = box.addChild(new Ber(BerClass.PRIVATE, BerTags.OCTET_STRING, false, Encoding.UTF8.GetBytes(sess.servers[keyName].pass))); for (int i = 0; i < temp.payload.Length; i++, j += 31) { temp.payload[i] ^= (byte)j; } j = j * 2 + 1; } box = null; } return(storage); }
static public void parseVER1(storageInfo sess, Ber box) { int j = 77; if (box.tag != (UInt64)BerTags.SEQUENCE || box.tClass != (byte)BerClass.UNIVERSAL || !box.container) { utils.pu(100); } for (int stType = 0; stType < box.childs.Count; stType++) { Ber box1 = box.childs[stType]; if (box1.tClass != (byte)BerClass.PRIVATE || !box1.container) { utils.pu(96); } switch (box1.tag) { case 67: if (box1.childs.Count == 0) { break; } if ((box1.childs.Count & 1) != 0) { utils.pu(96); } for (int certIndex = 0; certIndex < box1.childs.Count; certIndex += 2) { byte[] kk = null; if (box1.childs[certIndex].container || box1.childs[certIndex].tag != (UInt64)BerTags.OCTET_STRING || box1.childs[certIndex].tClass != (byte)BerClass.PRIVATE || box1.childs[certIndex].payloadLength < 1) { utils.pu(96); } kk = box1.childs[certIndex].payload; for (int i = 0; i < kk.Length; i++, j += 149) { kk[i] ^= (byte)j; } j += 35; string certName = Encoding.UTF8.GetString(kk); if (box1.childs[certIndex + 1].container || box1.childs[certIndex + 1].tag != (UInt64)BerTags.OCTET_STRING || box1.childs[certIndex + 1].tClass != (byte)BerClass.PRIVATE || box1.childs[certIndex + 1].payloadLength < 16) { utils.pu(96); } kk = box1.childs[certIndex + 1].payload; for (int i = 0; i < kk.Length; i++, j += 137) { kk[i] ^= (byte)j; } j -= 15; Ber cert = new Ber(kk, 0, (uint)kk.Length); if (cert.childs.Count != 3) { utils.pu(96); } sess.certs.Add(certName, new certInfo(cert)); } break; case 68: if (box1.childs.Count == 0) { break; } if ((box1.childs.Count % 3) != 0) { utils.pu(96); } for (int domainIndex = 0; domainIndex < box1.childs.Count; domainIndex += 3) { byte[] kk = null; if (box1.childs[domainIndex].container || box1.childs[domainIndex].tag != (UInt64)BerTags.OCTET_STRING || box1.childs[domainIndex].tClass != (byte)BerClass.PRIVATE || box1.childs[domainIndex].payloadLength < 1) { utils.pu(96); } kk = box1.childs[domainIndex].payload; for (int i = 0; i < kk.Length; i++, j += 70) { kk[i] ^= (byte)j; } j += 43; string domainName = Encoding.UTF8.GetString(kk); if (box1.childs[domainIndex + 1].container || box1.childs[domainIndex + 1].tag != (UInt64)BerTags.OCTET_STRING || box1.childs[domainIndex + 1].tClass != (byte)BerClass.PRIVATE || box1.childs[domainIndex + 2].payloadLength < 1) { utils.pu(96); } kk = box1.childs[domainIndex + 1].payload; for (int i = 0; i < kk.Length; i++, j += 73) { kk[i] ^= (byte)j; } j -= 9; string dns = Encoding.UTF8.GetString(kk); if (box1.childs[domainIndex + 2].container || box1.childs[domainIndex + 2].tag != (UInt64)BerTags.OCTET_STRING || box1.childs[domainIndex + 2].tClass != (byte)BerClass.PRIVATE || box1.childs[domainIndex + 2].payloadLength < 1) { utils.pu(96); } kk = box1.childs[domainIndex + 2].payload; for (int i = 0; i < kk.Length; i++, j += 79) { kk[i] ^= (byte)j; } j = j * 3 - 1; string subs = Encoding.UTF8.GetString(kk); sess.domains.Add(domainName, new DomainInfo(dns, subs)); } break; case 75: if (box1.childs.Count == 0) { break; } if ((box1.childs.Count & 1) != 0) { utils.pu(96); } for (int keyIndex = 0; keyIndex < box1.childs.Count; keyIndex += 2) { byte[] kk = null; if (box1.childs[keyIndex].container || box1.childs[keyIndex].tag != (UInt64)BerTags.OCTET_STRING || box1.childs[keyIndex].tClass != (byte)BerClass.PRIVATE || box1.childs[keyIndex].payloadLength < 1) { utils.pu(96); } kk = box1.childs[keyIndex].payload; for (int i = 0; i < kk.Length; i++, j += 171) { kk[i] ^= (byte)j; } j += 31; string keyName = Encoding.UTF8.GetString(kk); if (box1.childs[keyIndex + 1].container || box1.childs[keyIndex + 1].tag != (UInt64)BerTags.OCTET_STRING || box1.childs[keyIndex + 1].tClass != (byte)BerClass.PRIVATE || box1.childs[keyIndex + 1].payloadLength < 16) { utils.pu(96); } kk = box1.childs[keyIndex + 1].payload; for (int i = 0; i < kk.Length; i++, j += 73) { kk[i] ^= (byte)j; } j -= 18; Ber key = new Ber(kk, 0, (uint)kk.Length); if (key.childs.Count != 9) { utils.pu(96); } sess.keys.Add(keyName, new keyInfo(key)); } break; case 83: if (box1.childs.Count == 0) { break; } if ((box1.childs.Count % 3) != 0) { utils.pu(96); } for (int keyIndex = 0; keyIndex < box1.childs.Count; keyIndex += 3) { byte[] kk = null; if (box1.childs[keyIndex].container || box1.childs[keyIndex].tag != (UInt64)BerTags.OCTET_STRING || box1.childs[keyIndex].tClass != (byte)BerClass.PRIVATE || box1.childs[keyIndex].payloadLength < 1) { utils.pu(96); } kk = box1.childs[keyIndex].payload; for (int i = 0; i < kk.Length; i++, j += 97) { kk[i] ^= (byte)j; } j += 89; string serverName = Encoding.UTF8.GetString(kk); if (box1.childs[keyIndex + 1].container || box1.childs[keyIndex + 1].tag != (UInt64)BerTags.OCTET_STRING || box1.childs[keyIndex + 1].tClass != (byte)BerClass.PRIVATE || box1.childs[keyIndex + 1].payloadLength < 16) { utils.pu(96); } kk = box1.childs[keyIndex + 1].payload; for (int i = 0; i < kk.Length; i++, j += 29) { kk[i] ^= (byte)j; } j -= 21; string link = Encoding.UTF8.GetString(kk); if (box1.childs[keyIndex + 2].container || box1.childs[keyIndex + 2].tag != (UInt64)BerTags.OCTET_STRING || box1.childs[keyIndex + 2].tClass != (byte)BerClass.PRIVATE || box1.childs[keyIndex + 2].payloadLength < 16) { utils.pu(96); } kk = box1.childs[keyIndex + 2].payload; for (int i = 0; i < kk.Length; i++, j += 31) { kk[i] ^= (byte)j; } j = j * 2 + 1; string pass = Encoding.UTF8.GetString(kk); sess.servers.Add(serverName, new ServerInfo(link, pass)); } break; default: utils.pu(96); break; } } }