1 /* Copyright (c) 2015, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include "file_test.h"
16 
17 #include <algorithm>
18 #include <utility>
19 
20 #include <assert.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include <openssl/err.h>
29 
30 #include "../internal.h"
31 
32 
FileTest(std::unique_ptr<FileTest::LineReader> reader,std::function<void (const std::string &)> comment_callback,bool is_kas_test)33 FileTest::FileTest(std::unique_ptr<FileTest::LineReader> reader,
34                    std::function<void(const std::string &)> comment_callback,
35                    bool is_kas_test)
36     : reader_(std::move(reader)),
37       is_kas_test_(is_kas_test),
38       comment_callback_(std::move(comment_callback)) {}
39 
~FileTest()40 FileTest::~FileTest() {}
41 
42 // FindDelimiter returns a pointer to the first '=' or ':' in |str| or nullptr
43 // if there is none.
FindDelimiter(const char * str)44 static const char *FindDelimiter(const char *str) {
45   while (*str) {
46     if (*str == ':' || *str == '=') {
47       return str;
48     }
49     str++;
50   }
51   return nullptr;
52 }
53 
54 // StripSpace returns a string containing up to |len| characters from |str| with
55 // leading and trailing whitespace removed.
StripSpace(const char * str,size_t len)56 static std::string StripSpace(const char *str, size_t len) {
57   // Remove leading space.
58   while (len > 0 && isspace(*str)) {
59     str++;
60     len--;
61   }
62   while (len > 0 && isspace(str[len - 1])) {
63     len--;
64   }
65   return std::string(str, len);
66 }
67 
ParseKeyValue(const char * str,const size_t len)68 static std::pair<std::string, std::string> ParseKeyValue(const char *str, const size_t len) {
69   const char *delimiter = FindDelimiter(str);
70   std::string key, value;
71   if (delimiter == nullptr) {
72     key = StripSpace(str, len);
73   } else {
74     key = StripSpace(str, delimiter - str);
75     value = StripSpace(delimiter + 1, str + len - delimiter - 1);
76   }
77   return {key, value};
78 }
79 
ReadNext()80 FileTest::ReadResult FileTest::ReadNext() {
81   // If the previous test had unused attributes or instructions, it is an error.
82   if (!unused_attributes_.empty()) {
83     for (const std::string &key : unused_attributes_) {
84       PrintLine("Unused attribute: %s", key.c_str());
85     }
86     return kReadError;
87   }
88   if (!unused_instructions_.empty()) {
89     for (const std::string &key : unused_instructions_) {
90       PrintLine("Unused instruction: %s", key.c_str());
91     }
92     return kReadError;
93   }
94 
95   ClearTest();
96 
97   static const size_t kBufLen = 8192 * 4;
98   std::unique_ptr<char[]> buf(new char[kBufLen]);
99 
100   bool in_instruction_block = false;
101   is_at_new_instruction_block_ = false;
102 
103   while (true) {
104     // Read the next line.
105     switch (reader_->ReadLine(buf.get(), kBufLen)) {
106       case kReadError:
107         fprintf(stderr, "Error reading from input at line %u.\n", line_ + 1);
108         return kReadError;
109       case kReadEOF:
110         // EOF is a valid terminator for a test.
111         return start_line_ > 0 ? kReadSuccess : kReadEOF;
112       case kReadSuccess:
113         break;
114     }
115 
116     line_++;
117     size_t len = strlen(buf.get());
118     if (buf[0] == '\n' || buf[0] == '\r' || buf[0] == '\0') {
119       // Empty lines delimit tests.
120       if (start_line_ > 0) {
121         return kReadSuccess;
122       }
123       if (in_instruction_block) {
124         in_instruction_block = false;
125         // Delimit instruction block from test with a blank line.
126         current_test_ += "\r\n";
127       } else if (is_kas_test_) {
128         // KAS tests have random blank lines scattered around.
129         current_test_ += "\r\n";
130       }
131     } else if (buf[0] == '#') {
132       if (is_kas_test_ && seen_non_comment_) {
133         // KAS tests have comments after the initial comment block which need
134         // to be included in the corresponding place in the output.
135         current_test_ += std::string(buf.get());
136       } else if (comment_callback_) {
137         comment_callback_(buf.get());
138       }
139       // Otherwise ignore comments.
140     } else if (strcmp("[B.4.2 Key Pair Generation by Testing Candidates]\r\n",
141                       buf.get()) == 0) {
142       // The above instruction-like line is ignored because the FIPS lab's
143       // request files are hopelessly inconsistent.
144     } else if (buf[0] == '[') {  // Inside an instruction block.
145       is_at_new_instruction_block_ = true;
146       seen_non_comment_ = true;
147       if (start_line_ != 0) {
148         // Instructions should be separate blocks.
149         fprintf(stderr, "Line %u is an instruction in a test case.\n", line_);
150         return kReadError;
151       }
152       if (!in_instruction_block) {
153         ClearInstructions();
154         in_instruction_block = true;
155       }
156 
157       // Parse the line as an instruction ("[key = value]" or "[key]").
158 
159       // KAS tests contain invalid syntax.
160       std::string kv = buf.get();
161       const bool is_broken_kas_instruction =
162           is_kas_test_ &&
163           (kv == "[SHA(s) supported (Used for hashing Z): SHA512 \r\n");
164 
165       if (!is_broken_kas_instruction) {
166         kv = StripSpace(buf.get(), len);
167         if (kv[kv.size() - 1] != ']') {
168           fprintf(stderr, "Line %u, invalid instruction: '%s'\n", line_,
169                   kv.c_str());
170           return kReadError;
171         }
172       } else {
173         // Just remove the newline for the broken instruction.
174         kv = kv.substr(0, kv.size() - 2);
175       }
176 
177       current_test_ += kv + "\r\n";
178       kv = std::string(kv.begin() + 1, kv.end() - 1);
179 
180       for (;;) {
181         size_t idx = kv.find(",");
182         if (idx == std::string::npos) {
183           idx = kv.size();
184         }
185         std::string key, value;
186         std::tie(key, value) = ParseKeyValue(kv.c_str(), idx);
187         instructions_[key] = value;
188         if (idx == kv.size())
189           break;
190         kv = kv.substr(idx + 1);
191       }
192     } else {
193       // Parsing a test case.
194       if (in_instruction_block) {
195         // Some NIST CAVP test files (TDES) have a test case immediately
196         // following an instruction block, without a separate blank line, some
197         // of the time.
198         in_instruction_block = false;
199       }
200 
201       current_test_ += std::string(buf.get(), len);
202       std::string key, value;
203       std::tie(key, value) = ParseKeyValue(buf.get(), len);
204 
205       // Duplicate keys are rewritten to have “/2”, “/3”, … suffixes.
206       std::string mapped_key = key;
207       for (unsigned i = 2; attributes_.count(mapped_key) != 0; i++) {
208         char suffix[32];
209         snprintf(suffix, sizeof(suffix), "/%u", i);
210         suffix[sizeof(suffix)-1] = 0;
211         mapped_key = key + suffix;
212       }
213 
214       unused_attributes_.insert(mapped_key);
215       attributes_[mapped_key] = value;
216       if (start_line_ == 0) {
217         // This is the start of a test.
218         type_ = mapped_key;
219         parameter_ = value;
220         start_line_ = line_;
221         for (const auto &kv : instructions_) {
222           unused_instructions_.insert(kv.first);
223         }
224       }
225     }
226   }
227 }
228 
PrintLine(const char * format,...)229 void FileTest::PrintLine(const char *format, ...) {
230   va_list args;
231   va_start(args, format);
232 
233   fprintf(stderr, "Line %u: ", start_line_);
234   vfprintf(stderr, format, args);
235   fprintf(stderr, "\n");
236 
237   va_end(args);
238 }
239 
GetType()240 const std::string &FileTest::GetType() {
241   OnKeyUsed(type_);
242   return type_;
243 }
244 
GetParameter()245 const std::string &FileTest::GetParameter() {
246   OnKeyUsed(type_);
247   return parameter_;
248 }
249 
HasAttribute(const std::string & key)250 bool FileTest::HasAttribute(const std::string &key) {
251   OnKeyUsed(key);
252   return attributes_.count(key) > 0;
253 }
254 
GetAttribute(std::string * out_value,const std::string & key)255 bool FileTest::GetAttribute(std::string *out_value, const std::string &key) {
256   OnKeyUsed(key);
257   auto iter = attributes_.find(key);
258   if (iter == attributes_.end()) {
259     PrintLine("Missing attribute '%s'.", key.c_str());
260     return false;
261   }
262   *out_value = iter->second;
263   return true;
264 }
265 
GetAttributeOrDie(const std::string & key)266 const std::string &FileTest::GetAttributeOrDie(const std::string &key) {
267   if (!HasAttribute(key)) {
268     abort();
269   }
270   return attributes_[key];
271 }
272 
HasInstruction(const std::string & key)273 bool FileTest::HasInstruction(const std::string &key) {
274   OnInstructionUsed(key);
275   return instructions_.count(key) > 0;
276 }
277 
GetInstruction(std::string * out_value,const std::string & key)278 bool FileTest::GetInstruction(std::string *out_value, const std::string &key) {
279   OnInstructionUsed(key);
280   auto iter = instructions_.find(key);
281   if (iter == instructions_.end()) {
282     PrintLine("Missing instruction '%s'.", key.c_str());
283     return false;
284   }
285   *out_value = iter->second;
286   return true;
287 }
288 
CurrentTestToString() const289 const std::string &FileTest::CurrentTestToString() const {
290   return current_test_;
291 }
292 
FromHexDigit(uint8_t * out,char c)293 static bool FromHexDigit(uint8_t *out, char c) {
294   if ('0' <= c && c <= '9') {
295     *out = c - '0';
296     return true;
297   }
298   if ('a' <= c && c <= 'f') {
299     *out = c - 'a' + 10;
300     return true;
301   }
302   if ('A' <= c && c <= 'F') {
303     *out = c - 'A' + 10;
304     return true;
305   }
306   return false;
307 }
308 
GetBytes(std::vector<uint8_t> * out,const std::string & key)309 bool FileTest::GetBytes(std::vector<uint8_t> *out, const std::string &key) {
310   std::string value;
311   if (!GetAttribute(&value, key)) {
312     return false;
313   }
314 
315   if (value.size() >= 2 && value[0] == '"' && value[value.size() - 1] == '"') {
316     out->assign(value.begin() + 1, value.end() - 1);
317     return true;
318   }
319 
320   if (value.size() % 2 != 0) {
321     PrintLine("Error decoding value: %s", value.c_str());
322     return false;
323   }
324   out->clear();
325   out->reserve(value.size() / 2);
326   for (size_t i = 0; i < value.size(); i += 2) {
327     uint8_t hi, lo;
328     if (!FromHexDigit(&hi, value[i]) || !FromHexDigit(&lo, value[i + 1])) {
329       PrintLine("Error decoding value: %s", value.c_str());
330       return false;
331     }
332     out->push_back((hi << 4) | lo);
333   }
334   return true;
335 }
336 
EncodeHex(const uint8_t * in,size_t in_len)337 static std::string EncodeHex(const uint8_t *in, size_t in_len) {
338   static const char kHexDigits[] = "0123456789abcdef";
339   std::string ret;
340   ret.reserve(in_len * 2);
341   for (size_t i = 0; i < in_len; i++) {
342     ret += kHexDigits[in[i] >> 4];
343     ret += kHexDigits[in[i] & 0xf];
344   }
345   return ret;
346 }
347 
ExpectBytesEqual(const uint8_t * expected,size_t expected_len,const uint8_t * actual,size_t actual_len)348 bool FileTest::ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
349                                 const uint8_t *actual, size_t actual_len) {
350   if (expected_len == actual_len &&
351       OPENSSL_memcmp(expected, actual, expected_len) == 0) {
352     return true;
353   }
354 
355   std::string expected_hex = EncodeHex(expected, expected_len);
356   std::string actual_hex = EncodeHex(actual, actual_len);
357   PrintLine("Expected: %s", expected_hex.c_str());
358   PrintLine("Actual:   %s", actual_hex.c_str());
359   return false;
360 }
361 
ClearTest()362 void FileTest::ClearTest() {
363   start_line_ = 0;
364   type_.clear();
365   parameter_.clear();
366   attributes_.clear();
367   unused_attributes_.clear();
368   current_test_ = "";
369 }
370 
ClearInstructions()371 void FileTest::ClearInstructions() {
372   instructions_.clear();
373   unused_attributes_.clear();
374 }
375 
OnKeyUsed(const std::string & key)376 void FileTest::OnKeyUsed(const std::string &key) {
377   unused_attributes_.erase(key);
378 }
379 
OnInstructionUsed(const std::string & key)380 void FileTest::OnInstructionUsed(const std::string &key) {
381   unused_instructions_.erase(key);
382 }
383 
IsAtNewInstructionBlock() const384 bool FileTest::IsAtNewInstructionBlock() const {
385   return is_at_new_instruction_block_;
386 }
387 
InjectInstruction(const std::string & key,const std::string & value)388 void FileTest::InjectInstruction(const std::string &key,
389                                  const std::string &value) {
390   instructions_[key] = value;
391 }
392 
393 class FileLineReader : public FileTest::LineReader {
394  public:
FileLineReader(const char * path)395   explicit FileLineReader(const char *path) : file_(fopen(path, "r")) {}
~FileLineReader()396   ~FileLineReader() override {
397     if (file_ != nullptr) {
398       fclose(file_);
399     }
400   }
401 
402   // is_open returns true if the file was successfully opened.
is_open() const403   bool is_open() const { return file_ != nullptr; }
404 
ReadLine(char * out,size_t len)405   FileTest::ReadResult ReadLine(char *out, size_t len) override {
406     assert(len > 0);
407     if (file_ == nullptr) {
408       return FileTest::kReadError;
409     }
410 
411     if (fgets(out, len, file_) == nullptr) {
412       return feof(file_) ? FileTest::kReadEOF : FileTest::kReadError;
413     }
414 
415     if (strlen(out) == len - 1 && out[len - 2] != '\n' && !feof(file_)) {
416       fprintf(stderr, "Line too long.\n");
417       return FileTest::kReadError;
418     }
419 
420     return FileTest::kReadSuccess;
421   }
422 
423  private:
424   FILE *file_;
425 
426   FileLineReader(const FileLineReader &) = delete;
427   FileLineReader &operator=(const FileLineReader &) = delete;
428 };
429 
FileTestMain(FileTestFunc run_test,void * arg,const char * path)430 int FileTestMain(FileTestFunc run_test, void *arg, const char *path) {
431   FileTest::Options opts;
432   opts.callback = run_test;
433   opts.arg = arg;
434   opts.path = path;
435 
436   return FileTestMain(opts);
437 }
438 
FileTestMain(const FileTest::Options & opts)439 int FileTestMain(const FileTest::Options &opts) {
440   std::unique_ptr<FileLineReader> reader(
441       new FileLineReader(opts.path));
442   if (!reader->is_open()) {
443     fprintf(stderr, "Could not open file %s: %s.\n", opts.path,
444             strerror(errno));
445     return 1;
446   }
447 
448   FileTest t(std::move(reader), opts.comment_callback, opts.is_kas_test);
449 
450   bool failed = false;
451   while (true) {
452     FileTest::ReadResult ret = t.ReadNext();
453     if (ret == FileTest::kReadError) {
454       return 1;
455     } else if (ret == FileTest::kReadEOF) {
456       break;
457     }
458 
459     bool result = opts.callback(&t, opts.arg);
460     if (t.HasAttribute("Error")) {
461       if (result) {
462         t.PrintLine("Operation unexpectedly succeeded.");
463         failed = true;
464         continue;
465       }
466       uint32_t err = ERR_peek_error();
467       if (ERR_reason_error_string(err) != t.GetAttributeOrDie("Error")) {
468         t.PrintLine("Unexpected error; wanted '%s', got '%s'.",
469                     t.GetAttributeOrDie("Error").c_str(),
470                     ERR_reason_error_string(err));
471         failed = true;
472         ERR_clear_error();
473         continue;
474       }
475       ERR_clear_error();
476     } else if (!result) {
477       // In case the test itself doesn't print output, print something so the
478       // line number is reported.
479       t.PrintLine("Test failed");
480       ERR_print_errors_fp(stderr);
481       failed = true;
482       continue;
483     }
484   }
485 
486   if (!opts.silent && !failed) {
487     printf("PASS\n");
488   }
489 
490   return failed ? 1 : 0;
491 }
492 
SkipCurrent()493 void FileTest::SkipCurrent() {
494   ClearTest();
495 }
496