Bug 1061021, Part 3: Stop using PLArenaPool for BitString encoding, r=keeler
authorBrian Smith <brian@briansmith.org>
Sat, 30 Aug 2014 16:15:11 -0700
changeset 14674 edf6255af552dcecc64815decf5489021528e7e8
parent 14673 59ca4088b5aeff2012f825d0d22c603059fb8885
child 14675 5754cdc8fa0a43f1b1db36ffec8bfa8974d948eb
push id3202
push userfranziskuskiefer@gmail.com
push dateMon, 01 Oct 2018 08:30:12 +0000
reviewerskeeler
bugs1061021
Bug 1061021, Part 3: Stop using PLArenaPool for BitString encoding, r=keeler
lib/mozpkix/test/lib/pkixtestutil.cpp
--- a/lib/mozpkix/test/lib/pkixtestutil.cpp
+++ b/lib/mozpkix/test/lib/pkixtestutil.cpp
@@ -282,33 +282,30 @@ static ByteString
 KeyHashHelper(const CERTSubjectPublicKeyInfo* spki)
 {
   // We only need a shallow copy here.
   SECItem spk = spki->subjectPublicKey;
   DER_ConvertBitString(&spk); // bits to bytes
   return HashedOctetString(spk);
 }
 
-static SECItem*
-BitString(PLArenaPool* arena, const SECItem* rawBytes, bool corrupt)
+static ByteString
+BitString(const ByteString& rawBytes, bool corrupt)
 {
+  ByteString prefixed;
   // We have to add a byte at the beginning indicating no unused bits.
   // TODO: add ability to have bit strings of bit length not divisible by 8,
   // resulting in unused bits in the bitstring encoding
-  SECItem* prefixed = SECITEM_AllocItem(arena, nullptr, rawBytes->len + 1);
-  if (!prefixed) {
-    return nullptr;
+  prefixed.push_back(0);
+  prefixed.append(rawBytes);
+  if (corrupt) {
+    assert(prefixed.length() > 8);
+    prefixed[8]++;
   }
-  prefixed->data[0] = 0;
-  memcpy(prefixed->data + 1, rawBytes->data, rawBytes->len);
-  if (corrupt) {
-    assert(prefixed->len > 8);
-    prefixed->data[8]++;
-  }
-  return EncodeNested(arena, der::BIT_STRING, prefixed);
+  return TLV(der::BIT_STRING, prefixed);
 }
 
 static SECItem*
 Boolean(PLArenaPool* arena, bool value)
 {
   assert(arena);
   SECItem* result(SECITEM_AllocItem(arena, nullptr, 3));
   if (!result) {
@@ -515,19 +512,20 @@ SignedData(PLArenaPool* arena, const SEC
   SECItem signature;
   if (SEC_SignData(&signature, tbsData->data, tbsData->len, privKey,
                    signatureAlgorithmOidTag) != SECSuccess)
   {
     return nullptr;
   }
   // TODO: add ability to have signatures of bit length not divisible by 8,
   // resulting in unused bits in the bitstring encoding
-  SECItem* signatureNested = BitString(arena, &signature, corrupt);
+  ByteString signatureNested(BitString(ByteString(signature.data, signature.len),
+                                       corrupt));
   SECITEM_FreeItem(&signature, false);
-  if (!signatureNested) {
+  if (signatureNested == ENCODING_FAILED) {
     return nullptr;
   }
 
   SECItem* certsNested = nullptr;
   if (certs) {
     Output certsOutput;
     while (*certs) {
       certsOutput.Add(*certs);
@@ -550,19 +548,17 @@ SignedData(PLArenaPool* arena, const SEC
     return nullptr;
   }
 
   SECItem sigantureAlgorithmDERItem =
     UnsafeMapInputToSECItem(signatureAlgorithmDER);
   if (output.Add(&sigantureAlgorithmDERItem) != Success) {
     return nullptr;
   }
-  if (output.Add(signatureNested) != Success) {
-    return nullptr;
-  }
+  output.Add(signatureNested);
   if (certsNested) {
     if (output.Add(certsNested) != Success) {
       return nullptr;
     }
   }
   return output.Squash(arena, der::SEQUENCE);
 }