Skip to content

Commit

Permalink
Bug 1073867, Part 4: Test that DSS end-entity certificates are reject…
Browse files Browse the repository at this point in the history
…ed, r=mmc

--HG--
extra : rebase_source : 7cfdcdf08f2ae8909062b8803de6702ab47ec65a
  • Loading branch information
briansmith committed Dec 26, 2014
1 parent dcacbfd commit aeda384
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/external/nss/nss.def
Original file line number Diff line number Diff line change
Expand Up @@ -680,3 +680,4 @@ VFY_VerifyData
VFY_VerifyDataDirect
VFY_VerifyDataWithAlgorithmID
_SGN_VerifyPKCS1DigestInfo
PK11_PQG_ParamGenV2
85 changes: 85 additions & 0 deletions security/pkix/test/gtest/pkixbuild_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,88 @@ TEST_F(pkixbuild, NoRevocationCheckingForExpiredCert)
CertPolicyId::anyPolicy,
nullptr));
}

class DSSTrustDomain : public TrustDomain
{
public:
virtual Result GetCertTrust(EndEntityOrCA, const CertPolicyId&,
Input, /*out*/ TrustLevel& trustLevel)
{
trustLevel = TrustLevel::TrustAnchor;
return Success;
}

virtual Result FindIssuer(Input, IssuerChecker&, Time)
{
ADD_FAILURE();
return Result::FATAL_ERROR_LIBRARY_FAILURE;
}

virtual Result CheckRevocation(EndEntityOrCA, const CertID&, Time,
/*optional*/ const Input*,
/*optional*/ const Input*)
{
ADD_FAILURE();
return Result::FATAL_ERROR_LIBRARY_FAILURE;
}

virtual Result IsChainValid(const DERArray&, Time)
{
return Success;
}

virtual Result VerifySignedData(const SignedDataWithSignature& signedData,
Input subjectPublicKeyInfo)
{
ADD_FAILURE();
return Result::FATAL_ERROR_LIBRARY_FAILURE;
}

virtual Result DigestBuf(Input, /*out*/uint8_t*, size_t)
{
ADD_FAILURE();
return Result::FATAL_ERROR_LIBRARY_FAILURE;
}

virtual Result CheckPublicKey(Input subjectPublicKeyInfo)
{
return TestCheckPublicKey(subjectPublicKeyInfo);
}
};

class pkixbuild_DSS : public ::testing::Test { };

TEST_F(pkixbuild_DSS, DSSEndEntityKeyNotAccepted)
{
DSSTrustDomain trustDomain;

ByteString serialNumber(CreateEncodedSerialNumber(1));
ASSERT_FALSE(ENCODING_FAILED(serialNumber));

ByteString subjectDER(CNToDERName("DSS"));
ASSERT_FALSE(ENCODING_FAILED(subjectDER));
ScopedTestKeyPair subjectKey(GenerateDSSKeyPair());
ASSERT_TRUE(subjectKey);

ByteString issuerDER(CNToDERName("RSA"));
ASSERT_FALSE(ENCODING_FAILED(issuerDER));
ScopedTestKeyPair issuerKey(CloneReusedKeyPair());
ASSERT_TRUE(issuerKey);

ByteString cert(CreateEncodedCertificate(v3, sha256WithRSAEncryption,
serialNumber, issuerDER,
oneDayBeforeNow, oneDayAfterNow,
subjectDER, *subjectKey, nullptr,
*issuerKey, sha256WithRSAEncryption));
ASSERT_FALSE(ENCODING_FAILED(cert));
Input certDER;
ASSERT_EQ(Success, certDER.Init(cert.data(), cert.length()));

ASSERT_EQ(Result::ERROR_UNSUPPORTED_KEYALG,
BuildCertChain(trustDomain, certDER, Now(),
EndEntityOrCA::MustBeEndEntity,
KeyUsage::noParticularKeyUsageRequired,
KeyPurposeId::id_kp_serverAuth,
CertPolicyId::anyPolicy,
nullptr/*stapledOCSPResponse*/));
}
48 changes: 48 additions & 0 deletions security/pkix/test/lib/pkixtestnss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "cryptohi.h"
#include "keyhi.h"
#include "nss.h"
#include "pk11pqg.h"
#include "pk11pub.h"
#include "pkix/pkixnss.h"
#include "pkixder.h"
Expand Down Expand Up @@ -241,6 +242,53 @@ CloneReusedKeyPair()
return reusedKeyPair->Clone();
}

TestKeyPair*
GenerateDSSKeyPair()
{
InitNSSIfNeeded();

ScopedPtr<PK11SlotInfo, PK11_FreeSlot> slot(PK11_GetInternalSlot());
if (!slot) {
return nullptr;
}

PQGParams* pqgParamsTemp = nullptr;
PQGVerify* pqgVerify = nullptr;
if (PK11_PQG_ParamGenV2(2048u, 256u, 256u / 8u, &pqgParamsTemp, &pqgVerify)
!= SECSuccess) {
return nullptr;
}
PK11_PQG_DestroyVerify(pqgVerify);
ScopedPtr<PQGParams, PK11_PQG_DestroyParams> params(pqgParamsTemp);

SECKEYPublicKey* publicKeyTemp = nullptr;
ScopedSECKEYPrivateKey
privateKey(PK11_GenerateKeyPair(slot.get(), CKM_DSA_KEY_PAIR_GEN,
params.get(), &publicKeyTemp, false, true,
nullptr));
if (!privateKey) {
return nullptr;
}
ScopedSECKEYPublicKey publicKey(publicKeyTemp);

ScopedSECItem spkiDER(SECKEY_EncodeDERSubjectPublicKeyInfo(publicKey.get()));
if (!spkiDER) {
return nullptr;
}

ScopedPtr<CERTSubjectPublicKeyInfo, SECKEY_DestroySubjectPublicKeyInfo>
spki(SECKEY_CreateSubjectPublicKeyInfo(publicKey.get()));
if (!spki) {
return nullptr;
}

SECItem spkDER = spki->subjectPublicKey;
DER_ConvertBitString(&spkDER); // bits to bytes
return CreateTestKeyPair(ByteString(spkiDER->data, spkiDER->len),
ByteString(spkDER.data, spkDER.len),
privateKey.release());
}

ByteString
SHA1(const ByteString& toHash)
{
Expand Down
1 change: 1 addition & 0 deletions security/pkix/test/lib/pkixtestutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ class TestKeyPair

TestKeyPair* CloneReusedKeyPair();
TestKeyPair* GenerateKeyPair();
TestKeyPair* GenerateDSSKeyPair();
inline void DeleteTestKeyPair(TestKeyPair* keyPair) { delete keyPair; }
typedef ScopedPtr<TestKeyPair, DeleteTestKeyPair> ScopedTestKeyPair;

Expand Down

0 comments on commit aeda384

Please sign in to comment.