static void Main(string[] args) { otp sotp = new otp(); sotp.time = DateTimeOffset.Now.ToUnixTimeMilliseconds(); sotp.key = "wow awesome"; sotp.distance = 30000; Console.Write(sotp.generate()); }
public bool verifyotp() { if (skipotp) { return(true); } // check if is a fmuv2 and bootloader >= 4 else fail; // 9 = fmuv2 // 5 = px4 1.x if (board_type == 9) // &&up.bl_rev >= 4 { try { // get the device sn byte[] sn = __get_sn(); string line = ""; line = "SN: "; for (int s = 0; s < sn.Length; s += 1) { line += sn[s].ToString("X2"); } print(line); // 20 bytes - sha1 Array.Resize(ref sn, 20); if (ByteArrayCompare(sn, new byte[] { 0x00, 0x23, 0x00, 0x30, 0x35, 0x32, 0x47, 0x18, 0x36, 0x34, 0x30, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })) { print("Libre bootloader"); libre = true; print("Forged Key"); throw new InvalidKeyException("Invalid Board"); } if (ByteArrayCompare(sn, new byte[] { 0x00, 0x38, 0x00, 0x1F, 0x34, 0x32, 0x47, 0x0D, 0x31, 0x32, 0x35, 0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })) { // pixhawk lite // please sign your board via the proper process. // nuttx has an auth command. use it. print("Forged Key"); throw new InvalidKeyException("Invalid Board"); } if (ByteArrayCompare(sn, new byte[] { 0x00, 0x38, 0x00, 0x21, 0x31, 0x34, 0x51, 0x17, 0x33, 0x36, 0x38, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })) { // pixfalcon print("Forged Key"); throw new InvalidKeyException("Invalid Board"); } object obj = new otp(); byte[] test = __read_otp(); ByteArrayToStructure(test, ref obj); otp otp = (otp)obj; print("id: " + otp.id_type.ToString("X")); print("vid: " + otp.vid.ToString("X")); print("pid: " + otp.pid.ToString("X")); if (otp.h1 == 'P' && otp.h2 == 'X' && otp.h3 == '4' && otp.h4 == '\0') { // no vendor checks yet byte[] sig = otp.signature; line = ""; for (int s = 0; s < 512; s += 1) { line += test[s].ToString("X2"); if (s % 16 == 15) { print(line); line = ""; } } /* * byte[] PEMbuffer = Convert.FromBase64String(@""); */ // RSACryptoServiceProvider rsa = DecodeRsaPrivateKey(PEMbuffer); // RSAParameters rsapublic = rsa.ExportParameters(false); foreach (var cert in certs) { byte[] pubpem = Convert.FromBase64String(cert.Value); AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(pubpem); RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)asymmetricKeyParameter; RSAParameters rsaParameters = new RSAParameters(); rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned(); rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned(); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(rsaParameters); bool valid = rsa.VerifyHash(sn, CryptoConfig.MapNameToOID("SHA1"), otp.signature); if (valid) { print("Valid Key"); return(true); } } print("Invalid Key"); throw new InvalidKeyException("Invalid Board"); } else { print("Failed Header Check"); throw new FormatException("Failed Header Check"); } } catch { print("Failed to read Certificate of Authenticity"); throw; } } // not board type 9 return(true); }
public bool verifyotp() { if (skipotp) return true; // check if is a fmuv2 and bootloader >= 4 else fail; // 9 = fmuv2 // 5 = px4 1.x if (board_type == 9) // &&up.bl_rev >= 4 { try { // get the device sn byte[] sn = __get_sn(); string line = ""; line="SN: "; for (int s = 0; s < sn.Length; s += 1) { line += sn[s].ToString("X2"); } print(line); // 20 bytes - sha1 Array.Resize(ref sn, 20); if (ByteArrayCompare(sn, new byte[] { 0x00, 0x23, 0x00, 0x30, 0x35, 0x32, 0x47, 0x18, 0x36, 0x34, 0x30, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 })) { print("Libre bootloader"); libre = true; } object obj = new otp(); byte[] test = __read_otp(); ByteArrayToStructure(test, ref obj); otp otp = (otp)obj; print("id: " + otp.id_type.ToString("X")); print("vid: " + otp.vid.ToString("X")); print("pid: " + otp.pid.ToString("X")); if (otp.h1 == 'P' && otp.h2 == 'X' && otp.h3 == '4' && otp.h4 == '\0') { // no vendor checks yet byte[] sig = otp.signature; line = ""; for (int s = 0; s < 512; s += 1) { line += test[s].ToString("X2"); if (s % 16 == 15) { print(line); line = ""; } } /* byte[] PEMbuffer = Convert.FromBase64String(@""); */ // RSACryptoServiceProvider rsa = DecodeRsaPrivateKey(PEMbuffer); // RSAParameters rsapublic = rsa.ExportParameters(false); foreach (var cert in certs) { byte[] pubpem = Convert.FromBase64String(cert.Value); AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(pubpem); RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)asymmetricKeyParameter; RSAParameters rsaParameters = new RSAParameters(); rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned(); rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned(); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(rsaParameters); bool valid = rsa.VerifyHash(sn, CryptoConfig.MapNameToOID("SHA1"), otp.signature); if (valid) { print("Valid Key"); return true; } } print("Invalid Key"); throw new InvalidKeyException("Invalid Board"); } else { print("Failed Header Check"); throw new FormatException("Failed Header Check"); } } catch { print("Failed to read Certificate of Authenticity"); throw; } } // not board type 9 return true; }
public bool verifyotp() { if (skipotp) { return(true); } // check if is a fmuv2 and bootloader >= 4 else fail; // 9 = fmuv2 // 5 = px4 1.x if (board_type == 9) // &&up.bl_rev >= 4 { try { // get the device sn byte[] sn = __get_sn(); Console.Write("SN: "); for (int s = 0; s < sn.Length; s += 1) { Console.Write(sn[s].ToString("X2")); } Console.WriteLine(); // 20 bytes - sha1 Array.Resize(ref sn, 20); object obj = new otp(); byte[] test = __read_otp(); ByteArrayToStructure(test, ref obj); otp otp = (otp)obj; Console.WriteLine("id: " + otp.id_type.ToString("X")); Console.WriteLine("vid: " + otp.vid.ToString("X")); Console.WriteLine("pid: " + otp.pid.ToString("X")); if (otp.h1 == 'P' && otp.h2 == 'X' && otp.h3 == '4' && otp.h4 == '\0') { // no vendor checks yet byte[] sig = otp.signature; for (int s = 0; s < 512; s += 1) { Console.Write(test[s].ToString("X2")); if (s % 16 == 15) { Console.WriteLine(); } } /* * byte[] PEMbuffer = Convert.FromBase64String(@""); */ // RSACryptoServiceProvider rsa = DecodeRsaPrivateKey(PEMbuffer); // RSAParameters rsapublic = rsa.ExportParameters(false); foreach (var cert in certs) { byte[] pubpem = Convert.FromBase64String(cert.Value); AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(pubpem); RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)asymmetricKeyParameter; RSAParameters rsaParameters = new RSAParameters(); rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned(); rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned(); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(rsaParameters); bool valid = rsa.VerifyHash(sn, CryptoConfig.MapNameToOID("SHA1"), otp.signature); if (valid) { print("Valid Key"); return(true); } } print("Invalid Key"); throw new Exception("Invalid Board"); } else { print("Failed Header Check"); throw new Exception("Failed Header Check"); } } catch { print("Failed to read OTP"); } throw new Exception("Failed to read OTP"); } // not board type 9 return(true); }
public bool verifyotp() { if (skipotp) { return(true); } // check if is a fmuv2 and bootloader >= 4 else fail; // 9 = fmuv2 // 5 = px4 1.x if (board_type == 9) // &&up.bl_rev >= 4 { try { // get the device sn byte[] sn = __get_sn(); Console.Write("SN: "); for (int s = 0; s < sn.Length; s += 1) { Console.Write(sn[s].ToString("X2")); } Console.WriteLine(); // 20 bytes - sha1 Array.Resize(ref sn, 20); object obj = new otp(); byte[] test = __read_otp(); ByteArrayToStructure(test, ref obj); otp otp = (otp)obj; Console.WriteLine("id: " + otp.id_type.ToString("X")); Console.WriteLine("vid: " + otp.vid.ToString("X")); Console.WriteLine("pid: " + otp.pid.ToString("X")); if (otp.h1 == 'P' && otp.h2 == 'X' && otp.h3 == '4' && otp.h4 == '\0') { // no vendor checks yet byte[] sig = otp.signature; for (int s = 0; s < 512; s += 1) { Console.Write(test[s].ToString("X2")); if (s % 16 == 15) { Console.WriteLine(); } } /* * byte[] PEMbuffer = Convert.FromBase64String(@""); */ // RSACryptoServiceProvider rsa = DecodeRsaPrivateKey(PEMbuffer); // RSAParameters rsapublic = rsa.ExportParameters(false); byte[] pubpem = Convert.FromBase64String(@" MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDqi8E6EdZ11iE7nAc95bjdUTwd /gLetSAAx8X9jgjInz5j47DIcDqFVFKEFZWiAc3AxJE/fNrPQey16SfI0FyDAX/U 4jyGIv9w+M1dKgUPI8UdpEMS2w1YnfzW0GO3PX0SBL6pctEIdXr0NGsFFaqU9Yz4 DbgBdR6wBz9qdfRRoQIDAQAB"); AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(pubpem); RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)asymmetricKeyParameter; RSAParameters rsaParameters = new RSAParameters(); rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned(); rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned(); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(rsaParameters); bool valid = rsa.VerifyHash(sn, CryptoConfig.MapNameToOID("SHA1"), otp.signature); if (valid) { print("Valid Key"); return(true); } else { print("Invalid Key"); throw new Exception("Invalid Board"); } } else { print("Failed Header Check"); throw new Exception("Failed Header Check"); } } catch { print("Failed to read OTP"); } throw new Exception("Failed to read OTP"); } // not board type 9 return(true); }