int main(int argc,char **argv) { if (argc < 2) { printHelp(argv[0]); return -1; } if (!strcmp(argv[1],"generate")) { Identity id; id.generate(); std::string idser = id.toString(true); if (argc >= 3) { if (!Utils::writeFile(argv[2],idser)) { std::cerr << "Error writing to " << argv[2] << std::endl; return -1; } else std::cout << argv[2] << " written" << std::endl; } else std::cout << idser; } else if (!strcmp(argv[1],"validate")) { if (argc < 3) { printHelp(argv[0]); return -1; } Identity id = getIdFromArg(argv[2]); if (!id) { std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; return -1; } if (!id.locallyValidate(true)) { std::cerr << argv[2] << " FAILED validation." << std::endl; return -1; } else std::cout << argv[2] << " is a valid identity (full check performed)" << std::endl; } else if (!strcmp(argv[1],"getpublic")) { if (argc < 3) { printHelp(argv[0]); return -1; } Identity id = getIdFromArg(argv[2]); if (!id) { std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; return -1; } std::cout << id.toString(false); } else if (!strcmp(argv[1],"sign")) { if (argc < 4) { printHelp(argv[0]); return -1; } Identity id = getIdFromArg(argv[2]); if (!id) { std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; return -1; } if (!id.hasPrivate()) { std::cerr << argv[2] << " does not contain a private key (must use private to sign)" << std::endl; return -1; } std::string inf; if (!Utils::readFile(argv[3],inf)) { std::cerr << argv[3] << " is not readable" << std::endl; return -1; } C25519::Signature signature = id.sign(inf.data(),inf.length()); std::cout << Utils::hex(signature.data,signature.size()); } else if (!strcmp(argv[1],"verify")) { if (argc < 4) { printHelp(argv[0]); return -1; } Identity id = getIdFromArg(argv[2]); if (!id) { std::cerr << "Identity argument invalid or file unreadable: " << argv[2] << std::endl; return -1; } std::string inf; if (!Utils::readFile(argv[3],inf)) { std::cerr << argv[3] << " is not readable" << std::endl; return -1; } std::string signature(Utils::unhex(argv[4])); if ((signature.length() > ZT_ADDRESS_LENGTH)&&(id.verify(inf.data(),inf.length(),signature.data(),signature.length()))) { std::cout << argv[3] << " signature valid" << std::endl; } else { std::cerr << argv[3] << " signature check FAILED" << std::endl; return -1; } } else { printHelp(argv[0]); return -1; } return 0; }
static int testIdentity() { Identity id; Buffer<512> buf; std::cout << "[identity] Fully validate known-good identity... "; std::cout.flush(); if (!id.fromString("b487ffe552:2:9b121d26968a86eceea96d689dfb364a13f645aea9530c6d0c00c457569751340e8ff9ddf46be38190dcdd6178ff555cc48012a47280fbdece35799d8c445104:902474096fc914f0d6320a9d19b9e52d23bcf652e98b3930432d07a8271be0e19a813d1e77ee24db3454ce0c6c4a35e18a3adc0d06ee3bf086b38bd26ff95b085b4f1fd1d4ce423b15bc362cd5f13079b58252fd38b98b67b45203bb81423780:24f7ce86df8e242e4d7d04b657cf37eddc1aa7b34b6f38821c35fe393a4a381e0eef6e7b8b4ceab35a51e6ab0b6cbeb7c7282bc21c0c60cb6a512e454ecd45c5")) { std::cout << "FAIL (1)" << std::endl; return -1; } if (!id.locallyValidate(true)) { std::cout << "FAIL (2)" << std::endl; return -1; } std::cout << "PASS" << std::endl; std::cout << "[identity] Generate identity... "; std::cout.flush(); uint64_t genstart = Utils::now(); id.generate(); uint64_t genend = Utils::now(); std::cout << "(took " << (genend - genstart) << "ms): " << id.toString(true) << std::endl; std::cout << "[identity] Locally validate identity: "; if (id.locallyValidate(false)) { std::cout << "PASS" << std::endl; } else { std::cout << "FAIL" << std::endl; return -1; } { Identity id2; buf.clear(); id.serialize(buf,true); id2.deserialize(buf); std::cout << "[identity] Serialize and deserialize (w/private): "; if ((id == id2)&&(id2.locallyValidate(false))) { std::cout << "PASS" << std::endl; } else { std::cout << "FAIL" << std::endl; return -1; } } { Identity id2; buf.clear(); id.serialize(buf,false); id2.deserialize(buf); std::cout << "[identity] Serialize and deserialize (no private): "; if ((id == id2)&&(id2.locallyValidate(false))) { std::cout << "PASS" << std::endl; } else { std::cout << "FAIL" << std::endl; return -1; } } { Identity id2; id2.fromString(id.toString(true).c_str()); std::cout << "[identity] Serialize and deserialize (ASCII w/private): "; if ((id == id2)&&(id2.locallyValidate(false))) { std::cout << "PASS" << std::endl; } else { std::cout << "FAIL" << std::endl; return -1; } } { Identity id2; id2.fromString(id.toString(false).c_str()); std::cout << "[identity] Serialize and deserialize (ASCII no private): "; if ((id == id2)&&(id2.locallyValidate(false))) { std::cout << "PASS" << std::endl; } else { std::cout << "FAIL" << std::endl; return -1; } } return 0; }