32 #include <gtest/gtest.h>
53 using std::stringstream;
60 using scc::crypto::BasePtr;
62 struct CertTest :
public testing::Test
74 auto sd = getenv(
"TEST_SRCDIR");
77 cout <<
"TEST_SRCDIR=" << sd << endl;
84 dir << sd <<
"/com_stablecc_scclib/crypto/unittest/openssl/";
93 virtual ~CertTest() {}
97 cout <<
"loading pem from " << reldir+fn << endl;
98 ifstream f(reldir+fn);
99 ASSERT_NO_THROW(doc.
parse(f));
101 void load(
const std::string& fn)
105 void load_ecc(
const std::string& fn)
107 cout <<
"loading ecc pem from " << reldir+fn << endl;
108 ifstream f(reldir+fn);
110 ASSERT_NO_THROW(param.
parse(f));
111 ASSERT_NO_THROW(priv.
parse(f));
114 void compare(
const BasePtr& a,
const BasePtr& b)
116 vector<uint8_t> av, bv;
117 ASSERT_NO_THROW(DerDocument::dump_element(a, av));
118 ASSERT_NO_THROW(DerDocument::dump_element(b, bv));
123 TEST_F(CertTest, rsa_certs)
131 ASSERT_EQ(doc.label(),
"PUBLIC KEY");
136 cout << cert.str() << endl;
143 cout << key.
str() << endl;
146 ASSERT_EQ(doc.label(),
"RSA PUBLIC KEY");
149 RsaPublicKeyCert::parse(doc, key2);
151 cout << key2.
str() << endl;
153 ASSERT_EQ(key, key2);
158 auto d1 = cert.dump();
159 auto d2 = cert2.dump();
163 auto k2 = RsaPublicKeyCert::dump(key);
168 ASSERT_EQ(doc.label(),
"RSA PRIVATE KEY");
171 RsaPrivateKeyCert::parse(doc, priv);
173 cout << priv.
str() << endl;
175 ASSERT_EQ(key, priv.
pub_key());
178 auto p2 = RsaPrivateKeyCert::dump(priv);
182 TEST_F(CertTest, ecc_certs)
193 ASSERT_EQ(doc.label(),
"PUBLIC KEY");
198 cout << cert.str() << endl;
202 load_ecc(
"ecpriv.pem");
203 ASSERT_EQ(param.label(),
"EC PARAMETERS");
204 ASSERT_EQ(priv.label(),
"EC PRIVATE KEY");
205 cout <<
"** params" << endl;
206 cout << param << endl;
207 cout <<
"** priv" << endl;
208 cout << priv << endl;
211 EcParametersCert::parse(param.
root_ptr(), algo);
216 EcPrivateKeyCert::parse(priv.root_ptr(), key, algo, pub);
219 cout <<
"*** private cert" << endl;
220 cout <<
"key: " << key << endl;
221 cout <<
"algo: " << algo << endl;
224 cout <<
"pub: width=( " << x.width() <<
" , " << y.width() <<
" ) val=( " << x <<
" , " << y <<
" )" << endl;
226 ASSERT_TRUE(EccGfp::validate_key_pair(key, pub));
230 ASSERT_EQ(pub, pub2);
234 BasePtr d = EcParametersCert::dump(algo);
237 BasePtr d2 = EcPrivateKeyCert::dump(key, algo, pub);
238 compare(d2, priv.root_ptr());
241 struct CaBundleTest :
public testing::Test
243 std::vector<BasePtr> cacerts;
247 ifstream f(
"/etc/ssl/certs/ca-certificates.crt");
257 catch (std::exception& ex)
259 cout <<
"loaded ca certs bundle, exception: " << ex.what() << endl;
265 virtual ~CaBundleTest() {}
268 TEST_F(CaBundleTest, sanity_test)
274 cout <<
"Loaded ca certs, size=" << cacerts.size() << endl;
277 for (
auto& c : cacerts)
279 cout <<
"*******CA CERT " << ++n << endl;
282 ASSERT_TRUE(c->is_seq());
283 ASSERT_GT(c->contain().size(), 0);
284 ASSERT_TRUE(c->contain()[0]->is_seq());
285 ASSERT_GE(c->contain()[0]->contain().size(), 7);
288 cert.parse(c->contain()[0]->contain()[6]);
289 cout << cert.str() << endl;
291 ASSERT_NE(cert.type(), KeyAlgoType::unknown);
297 ASSERT_GT(key.
width(), 0);
298 cout <<
"rsa key width: " << key.
width() << endl;
304 ASSERT_TRUE(key.valid());
305 cout <<
"ecdsa point is valid" << endl;
X.509 and RSA certificates.
BasePtr root_ptr() const
Return the root pointer.
Elliptic curve cryptography over Galois prime field GF(p) curve.
PEM formatted DER document.
virtual void parse(std::istream &)
Parse document from an input stream.
bool validate(const RsaPublicKey &) const
Validate a public key with the private key.
std::string str(unsigned=8) const
Output with formatted values.
RsaPublicKey pub_key() const
Export the public key.
int width() const
Bit width of the key.
std::string str(unsigned=8) const
Output with formatted values.
KeyAlgoType
Key algorithm type.
@ ec_p521r1
parameter {1, 3, 132, 0, 35}
From: https://tools.ietf.org/html/rfc3279#section-2.3.5.
Private key certificate utility.
Public key information certificate.
RSA private key certificate.
RSA public key certificate.