iPXE
Functions
der.c File Reference

DER-encoded ASN.1 data. More...

#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/asn1.h>
#include <ipxe/der.h>
#include <ipxe/uaccess.h>
#include <ipxe/image.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static int der_asn1 (struct image *image, size_t offset __unused, struct asn1_cursor **cursor)
 Extract ASN.1 object from image.
static int der_probe (struct image *image)
 Probe DER image.
struct image_type der_image_type __image_type (PROBE_NORMAL)
 DER image type.

Detailed Description

DER-encoded ASN.1 data.

Definition in file der.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static int der_asn1 ( struct image image,
size_t offset  __unused,
struct asn1_cursor **  cursor 
) [static]

Extract ASN.1 object from image.

Parameters:
imageDER image
offsetOffset within image
cursorASN.1 cursor to fill in
Return values:
nextOffset to next image, or negative error

The caller is responsible for eventually calling free() on the allocated ASN.1 cursor.

Definition at line 51 of file der.c.

References copy_from_user(), image::data, data, ENOMEM, image::len, and malloc().

                                                    {
        void *data;

        /* Allocate cursor and data buffer */
        *cursor = malloc ( sizeof ( **cursor ) + image->len );
        if ( ! *cursor )
                return -ENOMEM;
        data = ( ( ( void * ) *cursor ) + sizeof ( **cursor ) );

        /* Populate cursor and data buffer */
        (*cursor)->data = data;
        (*cursor)->len = image->len;
        copy_from_user ( data, image->data, 0, image->len );

        return image->len;
}
static int der_probe ( struct image image) [static]

Probe DER image.

Parameters:
imageDER image
Return values:
rcReturn status code

Definition at line 75 of file der.c.

References ASN1_SEQUENCE, asn1_start(), assert, copy_from_user(), asn1_cursor::data, image::data, DBGC, ENOEXEC, asn1_cursor::len, image::len, len, image::name, rc, strerror(), and total.

                                             {
        struct asn1_cursor cursor;
        uint8_t buf[8];
        size_t extra;
        size_t total;
        int len;
        int rc;

        /* Sanity check: no realistic DER image can be smaller than this */
        if ( image->len < sizeof ( buf ) )
                return -ENOEXEC;

        /* Prepare partial cursor */
        cursor.data = buf;
        cursor.len = sizeof ( buf );
        copy_from_user ( buf, image->data, 0, sizeof ( buf ) );
        extra = ( image->len - sizeof ( buf ) );

        /* Get length of ASN.1 sequence */
        len = asn1_start ( &cursor, ASN1_SEQUENCE, extra );
        if ( len < 0 ) {
                rc = len;
                DBGC ( image, "DER %s is not valid ASN.1: %s\n",
                       image->name, strerror ( rc ) );
                return rc;
        }

        /* Add length of tag and length bytes consumed by asn1_start() */
        total = ( len + ( cursor.data - ( ( void * ) buf ) ) );
        assert ( total <= image->len );

        /* Check that image comprises a single well-formed ASN.1 object */
        if ( total != image->len ) {
                DBGC ( image, "DER %s is not single ASN.1\n", image->name );
                return -ENOEXEC;
        }

        return 0;
}
struct image_type der_image_type __image_type ( PROBE_NORMAL  ) [read]

DER image type.