Skip to content

Commit

Permalink
Bug 1321998 - don't overrun OIDs in alg1485 part 2, r=ttaubert
Browse files Browse the repository at this point in the history
Differential Revision: https://nss-review.dev.mozaws.net/D366

--HG--
extra : rebase_source : 2fbfed77870a0b6407115818cddbb745c480d5e7
extra : amend_source : cd7f1a3fc85a71c0617df7527f95e9af35c72d88
extra : histedit_source : a70dd8eb71edae4c3afeec06f3c849fbdeb1de41%2C036f00cedc9266d1587e8fdf193ae66127a14aeb
  • Loading branch information
franziskuskiefer committed Jul 11, 2017
1 parent 33e8bf4 commit c99e176
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cmd/pp/pp.c
Expand Up @@ -84,6 +84,8 @@ main(int argc, char **argv)
if (!inFile) {
fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
progName, optstate->value);
PORT_Free(typeTag);
PL_DestroyOptState(optstate);
return -1;
}
break;
Expand All @@ -93,6 +95,8 @@ main(int argc, char **argv)
if (!outFile) {
fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
progName, optstate->value);
PORT_Free(typeTag);
PL_DestroyOptState(optstate);
return -1;
}
break;
Expand Down
20 changes: 20 additions & 0 deletions gtests/certdb_gtest/alg1485_unittest.cc
Expand Up @@ -10,6 +10,7 @@

#include "nss.h"
#include "scoped_ptrs.h"
#include "prprf.h"

namespace nss_test {

Expand Down Expand Up @@ -89,4 +90,23 @@ INSTANTIATE_TEST_CASE_P(ParseAVAStrings, Alg1485ParseTest,
::testing::ValuesIn(kAVATestStrings));
INSTANTIATE_TEST_CASE_P(CompareAVAStrings, Alg1485CompareTest,
::testing::ValuesIn(kAVACompareStrings));

TEST_F(Alg1485Test, ShortOIDTest) {
// This is not a valid OID (too short). CERT_GetOidString should return 0.
unsigned char data[] = {0x05};
const SECItem oid = {siBuffer, data, sizeof(data)};
char* result = CERT_GetOidString(&oid);
EXPECT_EQ(result, nullptr);
}

TEST_F(Alg1485Test, BrokenOIDTest) {
// This is not a valid OID (first bit of last byte is not set).
// CERT_GetOidString should return 0.
unsigned char data[] = {0x81, 0x82, 0x83, 0x84};
const SECItem oid = {siBuffer, data, sizeof(data)};
char* result = CERT_GetOidString(&oid);
EXPECT_EQ(15U, strlen(result));
EXPECT_EQ(0, strncmp("OID.UNSUPPORTED", result, 15));
PR_smprintf_free(result);
}
}
7 changes: 6 additions & 1 deletion lib/certdb/alg1485.c
Expand Up @@ -733,6 +733,10 @@ CERT_GetOidString(const SECItem* oid)
break;
}
}
/* There's no first bit set, so this isn't valid. Bail.*/
if (last == stop) {
goto unsupported;
}
bytesBeforeLast = (unsigned int)(last - first);
if (bytesBeforeLast <= 3U) { /* 0-28 bit number */
PRUint32 n = 0;
Expand All @@ -756,8 +760,9 @@ CERT_GetOidString(const SECItem* oid)
n |= last[0] & 0x7f;
break;
}
if (last[0] & 0x80)
if (last[0] & 0x80) {
goto unsupported;
}

if (!rvString) {
/* This is the first number.. decompose it */
Expand Down

0 comments on commit c99e176

Please sign in to comment.