iPXE
pem_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * PEM self-tests
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <string.h>
00036 #include <assert.h>
00037 #include <ipxe/test.h>
00038 #include <ipxe/pem.h>
00039 #include "asn1_test.h"
00040 
00041 /** Define inline expected digest */
00042 #define DIGEST(...) { { __VA_ARGS__ } }
00043 
00044 /** Single RSA private key */
00045 ASN1 ( single, &pem_image_type,
00046        "-----BEGIN RSA PRIVATE KEY-----\n"
00047        "MCwCAQACBQC6loItAgMBAAECBCqhYIkCAwDyVwIDAMUbAgMAr9kCAmr9AgIaWQ==\n"
00048        "-----END RSA PRIVATE KEY-----\n",
00049        DIGEST ( 0xb9, 0x38, 0x83, 0xcd, 0xf4, 0x58, 0xa9, 0xa2, 0x84, 0x11,
00050                 0xfa, 0x0b, 0x6f, 0xdc, 0x3e, 0xa3, 0x7c, 0x90, 0x7c, 0x2d ) );
00051 
00052 /** Three concatenated RSA private keys */
00053 ASN1 ( multiple, &pem_image_type,
00054        "-----BEGIN RSA PRIVATE KEY-----\n"
00055        "MCwCAQACBQDtbjyVAgMBAAECBQCEOtJxAgMA+xsCAwDyDwICLGsCAgqTAgIxVQ==\n"
00056        "-----END RSA PRIVATE KEY-----\n"
00057        "-----BEGIN RSA PRIVATE KEY-----\n"
00058        "MCwCAQACBQC3VlyxAgMBAAECBGakxDUCAwDanwIDANavAgIBWQICTuECAwCmWg==\n"
00059        "-----END RSA PRIVATE KEY-----\n"
00060        "-----BEGIN RSA PRIVATE KEY-----\n"
00061        "MCwCAQACBQC89dS1AgMBAAECBQCxjnLBAgMA3qcCAwDZQwICP3cCAgpRAgI57A==\n"
00062        "-----END RSA PRIVATE KEY-----\n",
00063        DIGEST ( 0x9c, 0xb2, 0xc1, 0xa0, 0x9c, 0xcb, 0x11, 0xbf, 0x80, 0xd0,
00064                 0x8c, 0xe5, 0xda, 0xf2, 0x3b, 0x2c, 0xca, 0x64, 0x25, 0x8a ),
00065        DIGEST ( 0x82, 0x66, 0x24, 0xd9, 0xc3, 0x98, 0x1e, 0x5e, 0x56, 0xed,
00066                 0xd0, 0xd0, 0x2a, 0x5e, 0x9c, 0x3a, 0x58, 0xdf, 0x76, 0x0d ),
00067        DIGEST ( 0x01, 0xd2, 0x8a, 0x74, 0x42, 0x08, 0x0f, 0xb0, 0x03, 0x82,
00068                 0xcd, 0xa3, 0xdc, 0x78, 0xfe, 0xd7, 0xa3, 0x28, 0xfc, 0x29 ) );
00069 
00070 /** Two RSA private keys with various bits of noise added */
00071 ASN1 ( noisy, &pem_image_type,
00072        "Hello world!  This is uninteresting stuff before the actual data.\n"
00073        "-----BEGIN RSA PRIVATE KEY-----\n"
00074        "MCwCAQACBQC3VlyxAgMBAAECBGakxDUCAwDanwIDANavAgIBWQICTuECAwCmWg==\n"
00075        "-----END RSA PRIVATE KEY-----\n"
00076        "Here is some more uninteresting stuff.\n"
00077        "Followed by what is actually another RSA private key, but with "
00078        "extra whitespace added, and the description change to pretend "
00079        "it's a certificate\n"
00080        "-----BEGIN CERTIFICATE-----\n"
00081        "   MCwCAQACBQC6loItAgMBAAECBCqhYIkCAwD\r\n"
00082        "   yVwIDAMUbAgMAr9kCAmr9AgIaWQ==  \r\n"
00083        "-----END CERTIFICATE-----\n"
00084        "and some trailing garbage as well\n"
00085        "and more garbage with no final newline",
00086        DIGEST ( 0x82, 0x66, 0x24, 0xd9, 0xc3, 0x98, 0x1e, 0x5e, 0x56, 0xed,
00087                 0xd0, 0xd0, 0x2a, 0x5e, 0x9c, 0x3a, 0x58, 0xdf, 0x76, 0x0d ),
00088        DIGEST ( 0xb9, 0x38, 0x83, 0xcd, 0xf4, 0x58, 0xa9, 0xa2, 0x84, 0x11,
00089                 0xfa, 0x0b, 0x6f, 0xdc, 0x3e, 0xa3, 0x7c, 0x90, 0x7c, 0x2d ) );
00090 
00091 /**
00092  * Perform PEM self-test
00093  *
00094  */
00095 static void pem_test_exec ( void ) {
00096 
00097         /* Perform tests */
00098         asn1_ok ( &single );
00099         asn1_ok ( &multiple );
00100         asn1_ok ( &noisy );
00101 }
00102 
00103 /** PEM self-test */
00104 struct self_test pem_test __self_test = {
00105         .name = "pem",
00106         .exec = pem_test_exec,
00107 };