You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pem_test.c 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /** @file
  25. *
  26. * PEM self-tests
  27. *
  28. */
  29. /* Forcibly enable assertions */
  30. #undef NDEBUG
  31. #include <string.h>
  32. #include <assert.h>
  33. #include <ipxe/test.h>
  34. #include <ipxe/pem.h>
  35. #include "asn1_test.h"
  36. /** Define inline expected digest */
  37. #define DIGEST(...) { { __VA_ARGS__ } }
  38. /** Single RSA private key */
  39. ASN1 ( single, &pem_image_type,
  40. "-----BEGIN RSA PRIVATE KEY-----\n"
  41. "MCwCAQACBQC6loItAgMBAAECBCqhYIkCAwDyVwIDAMUbAgMAr9kCAmr9AgIaWQ==\n"
  42. "-----END RSA PRIVATE KEY-----\n",
  43. DIGEST ( 0xb9, 0x38, 0x83, 0xcd, 0xf4, 0x58, 0xa9, 0xa2, 0x84, 0x11,
  44. 0xfa, 0x0b, 0x6f, 0xdc, 0x3e, 0xa3, 0x7c, 0x90, 0x7c, 0x2d ) );
  45. /** Three concatenated RSA private keys */
  46. ASN1 ( multiple, &pem_image_type,
  47. "-----BEGIN RSA PRIVATE KEY-----\n"
  48. "MCwCAQACBQDtbjyVAgMBAAECBQCEOtJxAgMA+xsCAwDyDwICLGsCAgqTAgIxVQ==\n"
  49. "-----END RSA PRIVATE KEY-----\n"
  50. "-----BEGIN RSA PRIVATE KEY-----\n"
  51. "MCwCAQACBQC3VlyxAgMBAAECBGakxDUCAwDanwIDANavAgIBWQICTuECAwCmWg==\n"
  52. "-----END RSA PRIVATE KEY-----\n"
  53. "-----BEGIN RSA PRIVATE KEY-----\n"
  54. "MCwCAQACBQC89dS1AgMBAAECBQCxjnLBAgMA3qcCAwDZQwICP3cCAgpRAgI57A==\n"
  55. "-----END RSA PRIVATE KEY-----\n",
  56. DIGEST ( 0x9c, 0xb2, 0xc1, 0xa0, 0x9c, 0xcb, 0x11, 0xbf, 0x80, 0xd0,
  57. 0x8c, 0xe5, 0xda, 0xf2, 0x3b, 0x2c, 0xca, 0x64, 0x25, 0x8a ),
  58. DIGEST ( 0x82, 0x66, 0x24, 0xd9, 0xc3, 0x98, 0x1e, 0x5e, 0x56, 0xed,
  59. 0xd0, 0xd0, 0x2a, 0x5e, 0x9c, 0x3a, 0x58, 0xdf, 0x76, 0x0d ),
  60. DIGEST ( 0x01, 0xd2, 0x8a, 0x74, 0x42, 0x08, 0x0f, 0xb0, 0x03, 0x82,
  61. 0xcd, 0xa3, 0xdc, 0x78, 0xfe, 0xd7, 0xa3, 0x28, 0xfc, 0x29 ) );
  62. /** Two RSA private keys with various bits of noise added */
  63. ASN1 ( noisy, &pem_image_type,
  64. "Hello world! This is uninteresting stuff before the actual data.\n"
  65. "-----BEGIN RSA PRIVATE KEY-----\n"
  66. "MCwCAQACBQC3VlyxAgMBAAECBGakxDUCAwDanwIDANavAgIBWQICTuECAwCmWg==\n"
  67. "-----END RSA PRIVATE KEY-----\n"
  68. "Here is some more uninteresting stuff.\n"
  69. "Followed by what is actually another RSA private key, but with "
  70. "extra whitespace added, and the description change to pretend "
  71. "it's a certificate\n"
  72. "-----BEGIN CERTIFICATE-----\n"
  73. " MCwCAQACBQC6loItAgMBAAECBCqhYIkCAwD\r\n"
  74. " yVwIDAMUbAgMAr9kCAmr9AgIaWQ== \r\n"
  75. "-----END CERTIFICATE-----\n"
  76. "and some trailing garbage as well\n"
  77. "and more garbage with no final newline",
  78. DIGEST ( 0x82, 0x66, 0x24, 0xd9, 0xc3, 0x98, 0x1e, 0x5e, 0x56, 0xed,
  79. 0xd0, 0xd0, 0x2a, 0x5e, 0x9c, 0x3a, 0x58, 0xdf, 0x76, 0x0d ),
  80. DIGEST ( 0xb9, 0x38, 0x83, 0xcd, 0xf4, 0x58, 0xa9, 0xa2, 0x84, 0x11,
  81. 0xfa, 0x0b, 0x6f, 0xdc, 0x3e, 0xa3, 0x7c, 0x90, 0x7c, 0x2d ) );
  82. /**
  83. * Perform PEM self-test
  84. *
  85. */
  86. static void pem_test_exec ( void ) {
  87. /* Perform tests */
  88. asn1_ok ( &single );
  89. asn1_ok ( &multiple );
  90. asn1_ok ( &noisy );
  91. }
  92. /** PEM self-test */
  93. struct self_test pem_test __self_test = {
  94. .name = "pem",
  95. .exec = pem_test_exec,
  96. };