iPXE
pem_test.c
Go to the documentation of this file.
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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26/** @file
27 *
28 * PEM self-tests
29 *
30 */
31
32/* Forcibly enable assertions */
33#undef NDEBUG
34
35#include <string.h>
36#include <assert.h>
37#include <ipxe/test.h>
38#include <ipxe/pem.h>
39#include "asn1_test.h"
40
41/** Define inline expected digest */
42#define DIGEST(...) { { __VA_ARGS__ } }
43
44/** Single RSA private key */
45ASN1 ( single, &pem_image_type,
46 "-----BEGIN RSA PRIVATE KEY-----\n"
47 "MCwCAQACBQC6loItAgMBAAECBCqhYIkCAwDyVwIDAMUbAgMAr9kCAmr9AgIaWQ==\n"
48 "-----END RSA PRIVATE KEY-----\n",
49 DIGEST ( 0xb9, 0x38, 0x83, 0xcd, 0xf4, 0x58, 0xa9, 0xa2, 0x84, 0x11,
50 0xfa, 0x0b, 0x6f, 0xdc, 0x3e, 0xa3, 0x7c, 0x90, 0x7c, 0x2d ) );
51
52/** Three concatenated RSA private keys */
53ASN1 ( multiple, &pem_image_type,
54 "-----BEGIN RSA PRIVATE KEY-----\n"
55 "MCwCAQACBQDtbjyVAgMBAAECBQCEOtJxAgMA+xsCAwDyDwICLGsCAgqTAgIxVQ==\n"
56 "-----END RSA PRIVATE KEY-----\n"
57 "-----BEGIN RSA PRIVATE KEY-----\n"
58 "MCwCAQACBQC3VlyxAgMBAAECBGakxDUCAwDanwIDANavAgIBWQICTuECAwCmWg==\n"
59 "-----END RSA PRIVATE KEY-----\n"
60 "-----BEGIN RSA PRIVATE KEY-----\n"
61 "MCwCAQACBQC89dS1AgMBAAECBQCxjnLBAgMA3qcCAwDZQwICP3cCAgpRAgI57A==\n"
62 "-----END RSA PRIVATE KEY-----\n",
63 DIGEST ( 0x9c, 0xb2, 0xc1, 0xa0, 0x9c, 0xcb, 0x11, 0xbf, 0x80, 0xd0,
64 0x8c, 0xe5, 0xda, 0xf2, 0x3b, 0x2c, 0xca, 0x64, 0x25, 0x8a ),
65 DIGEST ( 0x82, 0x66, 0x24, 0xd9, 0xc3, 0x98, 0x1e, 0x5e, 0x56, 0xed,
66 0xd0, 0xd0, 0x2a, 0x5e, 0x9c, 0x3a, 0x58, 0xdf, 0x76, 0x0d ),
67 DIGEST ( 0x01, 0xd2, 0x8a, 0x74, 0x42, 0x08, 0x0f, 0xb0, 0x03, 0x82,
68 0xcd, 0xa3, 0xdc, 0x78, 0xfe, 0xd7, 0xa3, 0x28, 0xfc, 0x29 ) );
69
70/** Two RSA private keys with various bits of noise added */
71ASN1 ( noisy, &pem_image_type,
72 "Hello world! This is uninteresting stuff before the actual data.\n"
73 "-----BEGIN RSA PRIVATE KEY-----\n"
74 "MCwCAQACBQC3VlyxAgMBAAECBGakxDUCAwDanwIDANavAgIBWQICTuECAwCmWg==\n"
75 "-----END RSA PRIVATE KEY-----\n"
76 "Here is some more uninteresting stuff.\n"
77 "Followed by what is actually another RSA private key, but with "
78 "extra whitespace added, and the description change to pretend "
79 "it's a certificate\n"
80 "-----BEGIN CERTIFICATE-----\n"
81 " MCwCAQACBQC6loItAgMBAAECBCqhYIkCAwD\r\n"
82 " yVwIDAMUbAgMAr9kCAmr9AgIaWQ== \r\n"
83 "-----END CERTIFICATE-----\n"
84 "and some trailing garbage as well\n"
85 "and more garbage with no final newline",
86 DIGEST ( 0x82, 0x66, 0x24, 0xd9, 0xc3, 0x98, 0x1e, 0x5e, 0x56, 0xed,
87 0xd0, 0xd0, 0x2a, 0x5e, 0x9c, 0x3a, 0x58, 0xdf, 0x76, 0x0d ),
88 DIGEST ( 0xb9, 0x38, 0x83, 0xcd, 0xf4, 0x58, 0xa9, 0xa2, 0x84, 0x11,
89 0xfa, 0x0b, 0x6f, 0xdc, 0x3e, 0xa3, 0x7c, 0x90, 0x7c, 0x2d ) );
90
91/**
92 * Perform PEM self-test
93 *
94 */
95static void pem_test_exec ( void ) {
96
97 /* Perform tests */
98 asn1_ok ( &single );
99 asn1_ok ( &multiple );
100 asn1_ok ( &noisy );
101}
102
103/** PEM self-test */
104struct self_test pem_test __self_test = {
105 .name = "pem",
106 .exec = pem_test_exec,
107};
#define asn1_ok(test)
Report ASN.1 test result.
Definition asn1_test.h:72
#define ASN1(_name, _type, _file,...)
Define an ASN.1 test.
Definition asn1_test.h:44
Assertions.
#define DIGEST(...)
Define inline expected digest.
Definition der_test.c:45
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
String functions.
PEM-encoded ASN.1 data.
static void pem_test_exec(void)
Perform PEM self-test.
Definition pem_test.c:95
A self-test set.
Definition test.h:15
Self-test infrastructure.
#define __self_test
Declare a self-test.
Definition test.h:32