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.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/pem.h>
  29. #include <ipxe/base64.h>
  30. #include <ipxe/uaccess.h>
  31. #include <ipxe/image.h>
  32. /** @file
  33. *
  34. * PEM-encoded ASN.1 data
  35. *
  36. */
  37. /**
  38. * Locate next line
  39. *
  40. * @v image PEM image
  41. * @v offset Starting offset
  42. * @ret next Offset to next line
  43. */
  44. static size_t pem_next ( struct image *image, size_t offset ) {
  45. off_t eol;
  46. /* Find and skip next newline character, if any */
  47. eol = memchr_user ( image->data, offset, '\n', ( image->len - offset ));
  48. if ( eol < 0 )
  49. return image->len;
  50. return ( eol + 1 );
  51. }
  52. /**
  53. * Locate boundary marker line
  54. *
  55. * @v image PEM image
  56. * @v offset Starting offset
  57. * @v marker Boundary marker
  58. * @ret offset Offset to boundary marker line, or negative error
  59. */
  60. static int pem_marker ( struct image *image, size_t offset,
  61. const char *marker ) {
  62. char buf[ strlen ( marker ) ];
  63. /* Sanity check */
  64. assert ( offset <= image->len );
  65. /* Scan for marker at start of line */
  66. while ( offset < image->len ) {
  67. /* Check for marker */
  68. if ( ( image->len - offset ) < sizeof ( buf ) )
  69. break;
  70. copy_from_user ( buf, image->data, offset, sizeof ( buf ) );
  71. if ( memcmp ( buf, marker, sizeof ( buf ) ) == 0 )
  72. return offset;
  73. /* Move to next line */
  74. offset = pem_next ( image, offset );
  75. assert ( offset <= image->len );
  76. }
  77. return -ENOENT;
  78. }
  79. /**
  80. * Extract ASN.1 object from image
  81. *
  82. * @v image PEM image
  83. * @v offset Offset within image
  84. * @v cursor ASN.1 cursor to fill in
  85. * @ret next Offset to next image, or negative error
  86. *
  87. * The caller is responsible for eventually calling free() on the
  88. * allocated ASN.1 cursor.
  89. */
  90. static int pem_asn1 ( struct image *image, size_t offset,
  91. struct asn1_cursor **cursor ) {
  92. size_t encoded_len;
  93. size_t decoded_max_len;
  94. char *encoded;
  95. void *decoded;
  96. int begin;
  97. int end;
  98. int len;
  99. int rc;
  100. /* Locate and skip BEGIN marker */
  101. begin = pem_marker ( image, offset, PEM_BEGIN );
  102. if ( begin < 0 ) {
  103. rc = begin;
  104. DBGC ( image, "PEM %s [%#zx,%#zx) missing BEGIN marker: %s\n",
  105. image->name, offset, image->len, strerror ( rc ) );
  106. goto err_begin;
  107. }
  108. begin = pem_next ( image, begin );
  109. /* Locate and skip END marker */
  110. end = pem_marker ( image, begin, PEM_END );
  111. if ( end < 0 ) {
  112. rc = end;
  113. DBGC ( image, "PEM %s [%#zx,%#zx) missing END marker: %s\n",
  114. image->name, offset, image->len, strerror ( rc ) );
  115. goto err_end;
  116. }
  117. encoded_len = ( end - begin );
  118. end = pem_next ( image, end );
  119. /* Extract Base64-encoded data */
  120. encoded = malloc ( encoded_len + 1 /* NUL */ );
  121. if ( ! encoded ) {
  122. rc = -ENOMEM;
  123. goto err_alloc_encoded;
  124. }
  125. copy_from_user ( encoded, image->data, begin, encoded_len );
  126. encoded[encoded_len] = '\0';
  127. /* Allocate cursor and data buffer */
  128. decoded_max_len = base64_decoded_max_len ( encoded );
  129. *cursor = malloc ( sizeof ( **cursor ) + decoded_max_len );
  130. if ( ! *cursor ) {
  131. rc = -ENOMEM;
  132. goto err_alloc_decoded;
  133. }
  134. decoded = ( ( ( void * ) *cursor ) + sizeof ( **cursor ) );
  135. /* Decode Base64-encoded data */
  136. len = base64_decode ( encoded, decoded, decoded_max_len );
  137. if ( len < 0 ) {
  138. rc = len;
  139. DBGC ( image, "PEM %s could not decode: %s\n",
  140. image->name, strerror ( rc ) );
  141. goto err_decode;
  142. }
  143. (*cursor)->data = decoded;
  144. (*cursor)->len = len;
  145. assert ( (*cursor)->len <= decoded_max_len );
  146. /* Free Base64-encoded data */
  147. free ( encoded );
  148. /* Update offset and skip any unencapsulated trailer */
  149. offset = end;
  150. if ( pem_marker ( image, offset, PEM_BEGIN ) < 0 )
  151. offset = image->len;
  152. return offset;
  153. err_decode:
  154. free ( decoded );
  155. err_alloc_decoded:
  156. free ( encoded );
  157. err_alloc_encoded:
  158. err_end:
  159. err_begin:
  160. return rc;
  161. }
  162. /**
  163. * Probe PEM image
  164. *
  165. * @v image PEM image
  166. * @ret rc Return status code
  167. */
  168. static int pem_probe ( struct image *image ) {
  169. int rc;
  170. /* Check that image contains a BEGIN marker */
  171. if ( ( rc = pem_marker ( image, 0, PEM_BEGIN ) ) < 0 ) {
  172. DBGC ( image, "PEM %s has no BEGIN marker: %s\n",
  173. image->name, strerror ( rc ) );
  174. return rc;
  175. }
  176. return 0;
  177. }
  178. /** PEM image type */
  179. struct image_type pem_image_type __image_type ( PROBE_NORMAL ) = {
  180. .name = "PEM",
  181. .probe = pem_probe,
  182. .asn1 = pem_asn1,
  183. };