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.

der.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <assert.h>
  27. #include <ipxe/asn1.h>
  28. #include <ipxe/der.h>
  29. #include <ipxe/uaccess.h>
  30. #include <ipxe/image.h>
  31. /** @file
  32. *
  33. * DER-encoded ASN.1 data
  34. *
  35. */
  36. /**
  37. * Extract ASN.1 object from image
  38. *
  39. * @v image DER image
  40. * @v offset Offset within image
  41. * @v cursor ASN.1 cursor to fill in
  42. * @ret next Offset to next image, or negative error
  43. *
  44. * The caller is responsible for eventually calling free() on the
  45. * allocated ASN.1 cursor.
  46. */
  47. static int der_asn1 ( struct image *image, size_t offset __unused,
  48. struct asn1_cursor **cursor ) {
  49. void *data;
  50. /* Allocate cursor and data buffer */
  51. *cursor = malloc ( sizeof ( **cursor ) + image->len );
  52. if ( ! *cursor )
  53. return -ENOMEM;
  54. data = ( ( ( void * ) *cursor ) + sizeof ( **cursor ) );
  55. /* Populate cursor and data buffer */
  56. (*cursor)->data = data;
  57. (*cursor)->len = image->len;
  58. copy_from_user ( data, image->data, 0, image->len );
  59. return image->len;
  60. }
  61. /**
  62. * Probe DER image
  63. *
  64. * @v image DER image
  65. * @ret rc Return status code
  66. */
  67. static int der_probe ( struct image *image ) {
  68. struct asn1_cursor cursor;
  69. uint8_t buf[8];
  70. size_t extra;
  71. size_t total;
  72. int len;
  73. int rc;
  74. /* Sanity check: no realistic DER image can be smaller than this */
  75. if ( image->len < sizeof ( buf ) )
  76. return -ENOEXEC;
  77. /* Prepare partial cursor */
  78. cursor.data = buf;
  79. cursor.len = sizeof ( buf );
  80. copy_from_user ( buf, image->data, 0, sizeof ( buf ) );
  81. extra = ( image->len - sizeof ( buf ) );
  82. /* Get length of ASN.1 sequence */
  83. len = asn1_start ( &cursor, ASN1_SEQUENCE, extra );
  84. if ( len < 0 ) {
  85. rc = len;
  86. DBGC ( image, "DER %s is not valid ASN.1: %s\n",
  87. image->name, strerror ( rc ) );
  88. return rc;
  89. }
  90. /* Add length of tag and length bytes consumed by asn1_start() */
  91. total = ( len + ( cursor.data - ( ( void * ) buf ) ) );
  92. assert ( total <= image->len );
  93. /* Check that image comprises a single well-formed ASN.1 object */
  94. if ( total != image->len ) {
  95. DBGC ( image, "DER %s is not single ASN.1\n", image->name );
  96. return -ENOEXEC;
  97. }
  98. return 0;
  99. }
  100. /** DER image type */
  101. struct image_type der_image_type __image_type ( PROBE_NORMAL ) = {
  102. .name = "DER",
  103. .probe = der_probe,
  104. .asn1 = der_asn1,
  105. };