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.9KB

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