david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

[crypto] Expose pem_asn1() for use with non-image data

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown 2017-05-29 17:29:20 +01:00
parent b506528c1e
commit 14e3b4b29a
2 changed files with 81 additions and 42 deletions

View File

@ -27,10 +27,10 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include <ipxe/asn1.h> #include <ipxe/asn1.h>
#include <ipxe/pem.h>
#include <ipxe/base64.h> #include <ipxe/base64.h>
#include <ipxe/uaccess.h> #include <ipxe/uaccess.h>
#include <ipxe/image.h> #include <ipxe/image.h>
#include <ipxe/pem.h>
/** @file /** @file
* *
@ -41,95 +41,98 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/** /**
* Locate next line * Locate next line
* *
* @v image PEM image * @v data PEM data
* @v len Length of PEM data
* @v offset Starting offset * @v offset Starting offset
* @ret next Offset to next line * @ret next Offset to next line
*/ */
static size_t pem_next ( struct image *image, size_t offset ) { static size_t pem_next ( userptr_t data, size_t len, size_t offset ) {
off_t eol; off_t eol;
/* Find and skip next newline character, if any */ /* Find and skip next newline character, if any */
eol = memchr_user ( image->data, offset, '\n', ( image->len - offset )); eol = memchr_user ( data, offset, '\n', ( len - offset ) );
if ( eol < 0 ) if ( eol < 0 )
return image->len; return len;
return ( eol + 1 ); return ( eol + 1 );
} }
/** /**
* Locate boundary marker line * Locate boundary marker line
* *
* @v image PEM image * @v data PEM data
* @v len Length of PEM data
* @v offset Starting offset * @v offset Starting offset
* @v marker Boundary marker * @v marker Boundary marker
* @ret offset Offset to boundary marker line, or negative error * @ret offset Offset to boundary marker line, or negative error
*/ */
static int pem_marker ( struct image *image, size_t offset, static int pem_marker ( userptr_t data, size_t len, size_t offset,
const char *marker ) { const char *marker ) {
char buf[ strlen ( marker ) ]; char buf[ strlen ( marker ) ];
/* Sanity check */ /* Sanity check */
assert ( offset <= image->len ); assert ( offset <= len );
/* Scan for marker at start of line */ /* Scan for marker at start of line */
while ( offset < image->len ) { while ( offset < len ) {
/* Check for marker */ /* Check for marker */
if ( ( image->len - offset ) < sizeof ( buf ) ) if ( ( len - offset ) < sizeof ( buf ) )
break; break;
copy_from_user ( buf, image->data, offset, sizeof ( buf ) ); copy_from_user ( buf, data, offset, sizeof ( buf ) );
if ( memcmp ( buf, marker, sizeof ( buf ) ) == 0 ) if ( memcmp ( buf, marker, sizeof ( buf ) ) == 0 )
return offset; return offset;
/* Move to next line */ /* Move to next line */
offset = pem_next ( image, offset ); offset = pem_next ( data, len, offset );
assert ( offset <= image->len ); assert ( offset <= len );
} }
return -ENOENT; return -ENOENT;
} }
/** /**
* Extract ASN.1 object from image * Extract ASN.1 object from PEM data
* *
* @v image PEM image * @v data PEM data
* @v offset Offset within image * @v len Length of PEM data
* @v offset Offset within data
* @v cursor ASN.1 cursor to fill in * @v cursor ASN.1 cursor to fill in
* @ret next Offset to next image, or negative error * @ret next Offset to next object, or negative error
* *
* The caller is responsible for eventually calling free() on the * The caller is responsible for eventually calling free() on the
* allocated ASN.1 cursor. * allocated ASN.1 cursor.
*/ */
static int pem_asn1 ( struct image *image, size_t offset, int pem_asn1 ( userptr_t data, size_t len, size_t offset,
struct asn1_cursor **cursor ) { struct asn1_cursor **cursor ) {
size_t encoded_len; size_t encoded_len;
size_t decoded_max_len; size_t decoded_max_len;
char *encoded; char *encoded;
void *decoded; void *decoded;
int decoded_len;
int begin; int begin;
int end; int end;
int len;
int rc; int rc;
/* Locate and skip BEGIN marker */ /* Locate and skip BEGIN marker */
begin = pem_marker ( image, offset, PEM_BEGIN ); begin = pem_marker ( data, len, offset, PEM_BEGIN );
if ( begin < 0 ) { if ( begin < 0 ) {
rc = begin; rc = begin;
DBGC ( image, "PEM %s [%#zx,%#zx) missing BEGIN marker: %s\n", DBGC ( data, "PEM [%#zx,%#zx) missing BEGIN marker: %s\n",
image->name, offset, image->len, strerror ( rc ) ); offset, len, strerror ( rc ) );
goto err_begin; goto err_begin;
} }
begin = pem_next ( image, begin ); begin = pem_next ( data, len, begin );
/* Locate and skip END marker */ /* Locate and skip END marker */
end = pem_marker ( image, begin, PEM_END ); end = pem_marker ( data, len, begin, PEM_END );
if ( end < 0 ) { if ( end < 0 ) {
rc = end; rc = end;
DBGC ( image, "PEM %s [%#zx,%#zx) missing END marker: %s\n", DBGC ( data, "PEM [%#zx,%#zx) missing END marker: %s\n",
image->name, offset, image->len, strerror ( rc ) ); offset, len, strerror ( rc ) );
goto err_end; goto err_end;
} }
encoded_len = ( end - begin ); encoded_len = ( end - begin );
end = pem_next ( image, end ); end = pem_next ( data, len, end );
/* Extract Base64-encoded data */ /* Extract Base64-encoded data */
encoded = malloc ( encoded_len + 1 /* NUL */ ); encoded = malloc ( encoded_len + 1 /* NUL */ );
@ -137,7 +140,7 @@ static int pem_asn1 ( struct image *image, size_t offset,
rc = -ENOMEM; rc = -ENOMEM;
goto err_alloc_encoded; goto err_alloc_encoded;
} }
copy_from_user ( encoded, image->data, begin, encoded_len ); copy_from_user ( encoded, data, begin, encoded_len );
encoded[encoded_len] = '\0'; encoded[encoded_len] = '\0';
/* Allocate cursor and data buffer */ /* Allocate cursor and data buffer */
@ -150,15 +153,14 @@ static int pem_asn1 ( struct image *image, size_t offset,
decoded = ( ( ( void * ) *cursor ) + sizeof ( **cursor ) ); decoded = ( ( ( void * ) *cursor ) + sizeof ( **cursor ) );
/* Decode Base64-encoded data */ /* Decode Base64-encoded data */
len = base64_decode ( encoded, decoded, decoded_max_len ); decoded_len = base64_decode ( encoded, decoded, decoded_max_len );
if ( len < 0 ) { if ( decoded_len < 0 ) {
rc = len; rc = decoded_len;
DBGC ( image, "PEM %s could not decode: %s\n", DBGC ( data, "PEM could not decode: %s\n", strerror ( rc ) );
image->name, strerror ( rc ) );
goto err_decode; goto err_decode;
} }
(*cursor)->data = decoded; (*cursor)->data = decoded;
(*cursor)->len = len; (*cursor)->len = decoded_len;
assert ( (*cursor)->len <= decoded_max_len ); assert ( (*cursor)->len <= decoded_max_len );
/* Free Base64-encoded data */ /* Free Base64-encoded data */
@ -166,8 +168,8 @@ static int pem_asn1 ( struct image *image, size_t offset,
/* Update offset and skip any unencapsulated trailer */ /* Update offset and skip any unencapsulated trailer */
offset = end; offset = end;
if ( pem_marker ( image, offset, PEM_BEGIN ) < 0 ) if ( pem_marker ( data, len, offset, PEM_BEGIN ) < 0 )
offset = image->len; offset = len;
return offset; return offset;
@ -188,11 +190,14 @@ static int pem_asn1 ( struct image *image, size_t offset,
* @v image PEM image * @v image PEM image
* @ret rc Return status code * @ret rc Return status code
*/ */
static int pem_probe ( struct image *image ) { static int pem_image_probe ( struct image *image ) {
int offset;
int rc; int rc;
/* Check that image contains a BEGIN marker */ /* Check that image contains a BEGIN marker */
if ( ( rc = pem_marker ( image, 0, PEM_BEGIN ) ) < 0 ) { if ( ( offset = pem_marker ( image->data, image->len, 0,
PEM_BEGIN ) ) < 0 ) {
rc = offset;
DBGC ( image, "PEM %s has no BEGIN marker: %s\n", DBGC ( image, "PEM %s has no BEGIN marker: %s\n",
image->name, strerror ( rc ) ); image->name, strerror ( rc ) );
return rc; return rc;
@ -201,9 +206,37 @@ static int pem_probe ( struct image *image ) {
return 0; return 0;
} }
/**
* Extract ASN.1 object from image
*
* @v image PEM image
* @v offset Offset within image
* @v cursor ASN.1 cursor to fill in
* @ret next Offset to next image, or negative error
*
* The caller is responsible for eventually calling free() on the
* allocated ASN.1 cursor.
*/
static int pem_image_asn1 ( struct image *image, size_t offset,
struct asn1_cursor **cursor ) {
int next;
int rc;
/* Extract ASN.1 object */
if ( ( next = pem_asn1 ( image->data, image->len, offset,
cursor ) ) < 0 ) {
rc = next;
DBGC ( image, "PEM %s could not extract ASN.1: %s\n",
image->name, strerror ( rc ) );
return rc;
}
return next;
}
/** PEM image type */ /** PEM image type */
struct image_type pem_image_type __image_type ( PROBE_NORMAL ) = { struct image_type pem_image_type __image_type ( PROBE_NORMAL ) = {
.name = "PEM", .name = "PEM",
.probe = pem_probe, .probe = pem_image_probe,
.asn1 = pem_asn1, .asn1 = pem_image_asn1,
}; };

View File

@ -3,12 +3,15 @@
/** @file /** @file
* *
* PEM image format * PEM-encoded ASN.1 data
* *
*/ */
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <stdint.h>
#include <ipxe/uaccess.h>
#include <ipxe/asn1.h>
#include <ipxe/image.h> #include <ipxe/image.h>
/** Pre-encapsulation boundary marker */ /** Pre-encapsulation boundary marker */
@ -17,6 +20,9 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/** Post-encapsulation boundary marker */ /** Post-encapsulation boundary marker */
#define PEM_END "-----END" #define PEM_END "-----END"
extern int pem_asn1 ( userptr_t data, size_t len, size_t offset,
struct asn1_cursor **cursor );
extern struct image_type pem_image_type __image_type ( PROBE_NORMAL ); extern struct image_type pem_image_type __image_type ( PROBE_NORMAL );
#endif /* _IPXE_PEM_H */ #endif /* _IPXE_PEM_H */