int pkcs12_test() { Source cert; FileSource("../certs/server-cert.pem", cert); if (cert.size() == 0) { FileSource("../../certs/server-cert.pem", cert); // for testsuite if (cert.size() == 0) { FileSource("../../../certs/server-cert.pem", cert); // Debug dir if (cert.size() == 0) err_sys("where's your certs dir?", -109); } } if (GetCert(cert) != 0) return -110; Source source; FileSource("../certs/server.p12", source); if (source.size() == 0) { FileSource("../../certs/server.p12", source); // for testsuite if (source.size() == 0) { FileSource("../../../certs/server.p12", source); // Debug dir if (source.size() == 0) err_sys("where's your certs dir?", -111); } } if (GetPKCS_Cert("password", source) != 0) return -112; return 0; }
int dsa_test() { Source source; FileSource("../certs/dsa512.der", source); if (source.size() == 0) { FileSource("../../certs/dsa512.der", source); // for testsuite if (source.size() == 0) { FileSource("../../../certs/dsa512.der", source); // win32 Debug dir if (source.size() == 0) err_sys("where's your certs dir?", -89); } } const char msg[] = "this is the message"; byte signature[40]; DSA_PrivateKey priv(source); DSA_Signer signer(priv); SHA sha; byte digest[SHA::DIGEST_SIZE]; sha.Update((byte*)msg, sizeof(msg)); sha.Final(digest); signer.Sign(digest, signature, rng); byte encoded[sizeof(signature) + 6]; byte decoded[40]; word32 encSz = EncodeDSA_Signature(signer.GetR(), signer.GetS(), encoded); DecodeDSA_Signature(decoded, encoded, encSz); DSA_PublicKey pub(priv); DSA_Verifier verifier(pub); if (!verifier.Verify(digest, decoded)) return -90; return 0; }
int dh_test() { Source source; FileSource("../certs/dh1024.dat", source); if (source.size() == 0) { FileSource("../../certs/dh1024.dat", source); // for testsuite if (source.size() == 0) { FileSource("../../../certs/dh1024.dat", source); // win32 Debug dir if (source.size() == 0) err_sys("where's your certs dir?", -79); } } HexDecoder hDec(source); DH dh(source); byte pub[128]; byte priv[128]; byte agree[128]; byte pub2[128]; byte priv2[128]; byte agree2[128]; DH dh2(dh); dh.GenerateKeyPair(rng, priv, pub); dh2.GenerateKeyPair(rng, priv2, pub2); dh.Agree(agree, priv, pub2); dh2.Agree(agree2, priv2, pub); if ( memcmp(agree, agree2, dh.GetByteLength()) ) return -80; return 0; }
int rsa_test() { Source source; FileSource("../certs/client-key.der", source); if (source.size() == 0) { FileSource("../../certs/client-key.der", source); // for testsuite if (source.size() == 0) { FileSource("../../../certs/client-key.der", source); // Debug dir if (source.size() == 0) err_sys("where's your certs dir?", -79); } } RSA_PrivateKey priv(source); RSAES_Encryptor enc(priv); byte message[] = "Everyone gets Friday off."; const word32 len = (word32)strlen((char*)message); byte cipher[64]; enc.Encrypt(message, len, cipher, rng); RSAES_Decryptor dec(priv); byte plain[64]; dec.Decrypt(cipher, sizeof(plain), plain, rng); if (memcmp(plain, message, len)) return -70; dec.SSL_Sign(message, len, cipher, rng); if (!enc.SSL_Verify(message, len, cipher)) return -71; // test decode Source source2; FileSource("../certs/client-cert.der", source2); if (source2.size() == 0) { FileSource("../../certs/client-cert.der", source2); // for testsuite if (source2.size() == 0) { FileSource("../../../certs/client-cert.der", source2); // Debug dir if (source2.size() == 0) err_sys("where's your certs dir?", -79); } } CertDecoder cd(source2, true, 0, false, CertDecoder::CA); if (cd.GetError().What()) err_sys("cert error", -80); Source source3(cd.GetPublicKey().GetKey(), cd.GetPublicKey().size()); RSA_PublicKey pub(source3); return 0; }