void PEM_StripEncapsulatedBoundary(BufferedTransformation& bt, const SecByteBlock& pre, const SecByteBlock& post) { ByteQueue temp; SecByteBlock::const_iterator it; int n = 1, prePos = -1, postPos = -1; while(bt.AnyRetrievable() && n++) { SecByteBlock line, unused; PEM_ReadLine(bt, line, unused); // The write associated with an empty line must to occur. Otherwise, we loose the CR or LF // in an ecrypted private key between the control fields and the encapsulated text. //if(line.empty()) // continue; it = Search(line, pre); if(it != line.end()) { prePos = n; continue; } it = Search(line, post); if(it != line.end()) { postPos = n; continue; } PEM_WriteLine(temp, line); } if(prePos == -1) { string msg = "PEM_StripEncapsulatedBoundary: '"; msg += string((char*)pre.data(), pre.size()) + "' not found"; throw InvalidDataFormat(msg); } if(postPos == -1) { string msg = "PEM_StripEncapsulatedBoundary: '"; msg += string((char*)post.data(), post.size()) + "' not found"; throw InvalidDataFormat(msg); } if(prePos > postPos) throw InvalidDataFormat("PEM_StripEncapsulatedBoundary: header boundary follows footer boundary"); temp.TransferTo(bt); }
void PutDecodedDatumInto(const TestData &data, const char *name, BufferedTransformation &target) { std::string s1 = GetRequiredDatum(data, name), s2; while (!s1.empty()) { while (s1[0] == ' ') { s1 = s1.substr(1); if (s1.empty()) return; //avoid invalid read if s1 is empty } int repeat = 1; if (s1[0] == 'r') { repeat = atoi(s1.c_str()+1); s1 = s1.substr(s1.find(' ')+1); } s2 = ""; // MSVC 6 doesn't have clear(); if (s1[0] == '\"') { s2 = s1.substr(1, s1.find('\"', 1)-1); s1 = s1.substr(s2.length() + 2); } else if (s1.substr(0, 2) == "0x") { StringSource(s1.substr(2, s1.find(' ')), true, new HexDecoder(new StringSink(s2))); s1 = s1.substr(STDMIN(s1.find(' '), s1.length())); } else { StringSource(s1.substr(0, s1.find(' ')), true, new HexDecoder(new StringSink(s2))); s1 = s1.substr(STDMIN(s1.find(' '), s1.length())); } ByteQueue q; while (repeat--) { q.Put((const byte *)s2.data(), s2.size()); if (q.MaxRetrievable() > 4*1024 || repeat == 0) q.TransferTo(target); } } }