1 using System; 2 using KeePassRPC.DataExchangeModel; 3 4 namespace KeePassRPC 5 { 6 class KeyChallengeResponse 7 { 8 public string cc; 9 public string cr; 10 public string sc; 11 public string sr; 12 static int ProtocolVersion; 13 private string[] features; 14 KeyChallengeResponse(int protocolVersion, string[] features)15 public KeyChallengeResponse (int protocolVersion, string[] features) 16 { 17 ProtocolVersion = protocolVersion; 18 this.features = features; 19 } 20 KeyChallengeResponse1(string userName, int securityLevel)21 public string KeyChallengeResponse1(string userName, int securityLevel) 22 { 23 BigInteger scTemp = new BigInteger(); 24 scTemp.genRandomBits(256, new Random((int)DateTime.Now.Ticks)); 25 sc = scTemp.ToString().ToLower(); 26 27 KPRPCMessage data2client = new KPRPCMessage(); 28 data2client.protocol = "setup"; 29 data2client.key = new KeyParams(); 30 data2client.key.sc = sc; 31 data2client.key.securityLevel = securityLevel; 32 data2client.version = ProtocolVersion; 33 data2client.features = features; 34 35 string response = Jayrock.Json.Conversion.JsonConvert.ExportToString(data2client); 36 return response; 37 } 38 KeyChallengeResponse2(string cc, string cr, KeyContainerClass kc, int securityLevel, out bool authorised)39 public string KeyChallengeResponse2(string cc, string cr, KeyContainerClass kc, int securityLevel, out bool authorised) 40 { 41 string response = null; 42 this.cc = cc; 43 this.cr = KeePassLib.Utility.MemUtil.ByteArrayToHexString(Utils.Hash("1" + kc.Key + this.sc + this.cc)).ToLower(); 44 if (cr != this.cr) 45 { 46 authorised = false; 47 KPRPCMessage data2client = new KPRPCMessage(); 48 data2client.protocol = "setup"; 49 data2client.version = ProtocolVersion; 50 data2client.error = new Error(ErrorCode.AUTH_FAILED, new string[] { "Keys do not match" }); 51 response = Jayrock.Json.Conversion.JsonConvert.ExportToString(data2client); 52 } 53 else 54 { 55 this.sr = KeePassLib.Utility.MemUtil.ByteArrayToHexString(Utils.Hash("0" + kc.Key + this.sc + this.cc)).ToLower(); 56 authorised = true; 57 58 KPRPCMessage data2client = new KPRPCMessage(); 59 data2client.protocol = "setup"; 60 data2client.key = new KeyParams(); 61 data2client.key.sr = this.sr; 62 data2client.key.securityLevel = securityLevel; 63 data2client.version = ProtocolVersion; 64 response = Jayrock.Json.Conversion.JsonConvert.ExportToString(data2client); 65 } 66 return response; 67 } 68 69 } 70 } 71