xref: /minix3/crypto/external/bsd/openssl/dist/test/evp_extra_test.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* Copyright (c) 2014, Google Inc.
2*0a6a1f1dSLionel Sambuc  *
3*0a6a1f1dSLionel Sambuc  * Permission to use, copy, modify, and/or distribute this software for any
4*0a6a1f1dSLionel Sambuc  * purpose with or without fee is hereby granted, provided that the above
5*0a6a1f1dSLionel Sambuc  * copyright notice and this permission notice appear in all copies.
6*0a6a1f1dSLionel Sambuc  *
7*0a6a1f1dSLionel Sambuc  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8*0a6a1f1dSLionel Sambuc  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9*0a6a1f1dSLionel Sambuc  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10*0a6a1f1dSLionel Sambuc  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11*0a6a1f1dSLionel Sambuc  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12*0a6a1f1dSLionel Sambuc  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13*0a6a1f1dSLionel Sambuc  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14*0a6a1f1dSLionel Sambuc  */
15*0a6a1f1dSLionel Sambuc /* ====================================================================
16*0a6a1f1dSLionel Sambuc  * Copyright (c) 1998-2015 The OpenSSL Project.  All rights reserved.
17*0a6a1f1dSLionel Sambuc  *
18*0a6a1f1dSLionel Sambuc  * Redistribution and use in source and binary forms, with or without
19*0a6a1f1dSLionel Sambuc  * modification, are permitted provided that the following conditions
20*0a6a1f1dSLionel Sambuc  * are met:
21*0a6a1f1dSLionel Sambuc  *
22*0a6a1f1dSLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
23*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
24*0a6a1f1dSLionel Sambuc  *
25*0a6a1f1dSLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
26*0a6a1f1dSLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
27*0a6a1f1dSLionel Sambuc  *    the documentation and/or other materials provided with the
28*0a6a1f1dSLionel Sambuc  *    distribution.
29*0a6a1f1dSLionel Sambuc  *
30*0a6a1f1dSLionel Sambuc  * 3. All advertising materials mentioning features or use of this
31*0a6a1f1dSLionel Sambuc  *    software must display the following acknowledgment:
32*0a6a1f1dSLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
33*0a6a1f1dSLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
34*0a6a1f1dSLionel Sambuc  *
35*0a6a1f1dSLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
36*0a6a1f1dSLionel Sambuc  *    endorse or promote products derived from this software without
37*0a6a1f1dSLionel Sambuc  *    prior written permission. For written permission, please contact
38*0a6a1f1dSLionel Sambuc  *    openssl-core@openssl.org.
39*0a6a1f1dSLionel Sambuc  *
40*0a6a1f1dSLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
41*0a6a1f1dSLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
42*0a6a1f1dSLionel Sambuc  *    permission of the OpenSSL Project.
43*0a6a1f1dSLionel Sambuc  *
44*0a6a1f1dSLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
45*0a6a1f1dSLionel Sambuc  *    acknowledgment:
46*0a6a1f1dSLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
47*0a6a1f1dSLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
48*0a6a1f1dSLionel Sambuc  *
49*0a6a1f1dSLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
50*0a6a1f1dSLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51*0a6a1f1dSLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
52*0a6a1f1dSLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
53*0a6a1f1dSLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54*0a6a1f1dSLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
55*0a6a1f1dSLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56*0a6a1f1dSLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57*0a6a1f1dSLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
58*0a6a1f1dSLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59*0a6a1f1dSLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
60*0a6a1f1dSLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
61*0a6a1f1dSLionel Sambuc  * ====================================================================
62*0a6a1f1dSLionel Sambuc  *
63*0a6a1f1dSLionel Sambuc  * This product includes cryptographic software written by Eric Young
64*0a6a1f1dSLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
65*0a6a1f1dSLionel Sambuc  * Hudson (tjh@cryptsoft.com).
66*0a6a1f1dSLionel Sambuc  *
67*0a6a1f1dSLionel Sambuc  */
68*0a6a1f1dSLionel Sambuc 
69*0a6a1f1dSLionel Sambuc #include <stdio.h>
70*0a6a1f1dSLionel Sambuc #include <stdlib.h>
71*0a6a1f1dSLionel Sambuc #include <openssl/bio.h>
72*0a6a1f1dSLionel Sambuc #include <openssl/crypto.h>
73*0a6a1f1dSLionel Sambuc #include <openssl/err.h>
74*0a6a1f1dSLionel Sambuc #include <openssl/evp.h>
75*0a6a1f1dSLionel Sambuc #include <openssl/rsa.h>
76*0a6a1f1dSLionel Sambuc #include <openssl/x509.h>
77*0a6a1f1dSLionel Sambuc 
78*0a6a1f1dSLionel Sambuc /*
79*0a6a1f1dSLionel Sambuc  * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
80*0a6a1f1dSLionel Sambuc  * should never use this key anywhere but in an example.
81*0a6a1f1dSLionel Sambuc  */
82*0a6a1f1dSLionel Sambuc static const unsigned char kExampleRSAKeyDER[] = {
83*0a6a1f1dSLionel Sambuc     0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
84*0a6a1f1dSLionel Sambuc     0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
85*0a6a1f1dSLionel Sambuc     0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
86*0a6a1f1dSLionel Sambuc     0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
87*0a6a1f1dSLionel Sambuc     0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
88*0a6a1f1dSLionel Sambuc     0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
89*0a6a1f1dSLionel Sambuc     0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
90*0a6a1f1dSLionel Sambuc     0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,
91*0a6a1f1dSLionel Sambuc     0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8,
92*0a6a1f1dSLionel Sambuc     0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
93*0a6a1f1dSLionel Sambuc     0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c,
94*0a6a1f1dSLionel Sambuc     0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
95*0a6a1f1dSLionel Sambuc     0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7,
96*0a6a1f1dSLionel Sambuc     0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85,
97*0a6a1f1dSLionel Sambuc     0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee,
98*0a6a1f1dSLionel Sambuc     0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85,
99*0a6a1f1dSLionel Sambuc     0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a,
100*0a6a1f1dSLionel Sambuc     0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15,
101*0a6a1f1dSLionel Sambuc     0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83,
102*0a6a1f1dSLionel Sambuc     0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b,
103*0a6a1f1dSLionel Sambuc     0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73,
104*0a6a1f1dSLionel Sambuc     0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99,
105*0a6a1f1dSLionel Sambuc     0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02,
106*0a6a1f1dSLionel Sambuc     0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41,
107*0a6a1f1dSLionel Sambuc     0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59,
108*0a6a1f1dSLionel Sambuc     0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9,
109*0a6a1f1dSLionel Sambuc     0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef,
110*0a6a1f1dSLionel Sambuc     0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87,
111*0a6a1f1dSLionel Sambuc     0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf,
112*0a6a1f1dSLionel Sambuc     0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5,
113*0a6a1f1dSLionel Sambuc     0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5,
114*0a6a1f1dSLionel Sambuc     0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62,
115*0a6a1f1dSLionel Sambuc     0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64,
116*0a6a1f1dSLionel Sambuc     0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8,
117*0a6a1f1dSLionel Sambuc     0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba,
118*0a6a1f1dSLionel Sambuc     0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe,
119*0a6a1f1dSLionel Sambuc     0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7,
120*0a6a1f1dSLionel Sambuc     0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe,
121*0a6a1f1dSLionel Sambuc     0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb,
122*0a6a1f1dSLionel Sambuc     0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
123*0a6a1f1dSLionel Sambuc     0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27,
124*0a6a1f1dSLionel Sambuc     0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
125*0a6a1f1dSLionel Sambuc     0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba,
126*0a6a1f1dSLionel Sambuc     0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06,
127*0a6a1f1dSLionel Sambuc     0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
128*0a6a1f1dSLionel Sambuc     0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e,
129*0a6a1f1dSLionel Sambuc     0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
130*0a6a1f1dSLionel Sambuc     0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a,
131*0a6a1f1dSLionel Sambuc     0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17,
132*0a6a1f1dSLionel Sambuc     0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
133*0a6a1f1dSLionel Sambuc     0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
134*0a6a1f1dSLionel Sambuc };
135*0a6a1f1dSLionel Sambuc 
136*0a6a1f1dSLionel Sambuc static const unsigned char kMsg[] = { 1, 2, 3, 4 };
137*0a6a1f1dSLionel Sambuc 
138*0a6a1f1dSLionel Sambuc static const unsigned char kSignature[] = {
139*0a6a1f1dSLionel Sambuc     0xa5, 0xf0, 0x8a, 0x47, 0x5d, 0x3c, 0xb3, 0xcc, 0xa9, 0x79, 0xaf, 0x4d,
140*0a6a1f1dSLionel Sambuc     0x8c, 0xae, 0x4c, 0x14, 0xef, 0xc2, 0x0b, 0x34, 0x36, 0xde, 0xf4, 0x3e,
141*0a6a1f1dSLionel Sambuc     0x3d, 0xbb, 0x4a, 0x60, 0x5c, 0xc8, 0x91, 0x28, 0xda, 0xfb, 0x7e, 0x04,
142*0a6a1f1dSLionel Sambuc     0x96, 0x7e, 0x63, 0x13, 0x90, 0xce, 0xb9, 0xb4, 0x62, 0x7a, 0xfd, 0x09,
143*0a6a1f1dSLionel Sambuc     0x3d, 0xc7, 0x67, 0x78, 0x54, 0x04, 0xeb, 0x52, 0x62, 0x6e, 0x24, 0x67,
144*0a6a1f1dSLionel Sambuc     0xb4, 0x40, 0xfc, 0x57, 0x62, 0xc6, 0xf1, 0x67, 0xc1, 0x97, 0x8f, 0x6a,
145*0a6a1f1dSLionel Sambuc     0xa8, 0xae, 0x44, 0x46, 0x5e, 0xab, 0x67, 0x17, 0x53, 0x19, 0x3a, 0xda,
146*0a6a1f1dSLionel Sambuc     0x5a, 0xc8, 0x16, 0x3e, 0x86, 0xd5, 0xc5, 0x71, 0x2f, 0xfc, 0x23, 0x48,
147*0a6a1f1dSLionel Sambuc     0xd9, 0x0b, 0x13, 0xdd, 0x7b, 0x5a, 0x25, 0x79, 0xef, 0xa5, 0x7b, 0x04,
148*0a6a1f1dSLionel Sambuc     0xed, 0x44, 0xf6, 0x18, 0x55, 0xe4, 0x0a, 0xe9, 0x57, 0x79, 0x5d, 0xd7,
149*0a6a1f1dSLionel Sambuc     0x55, 0xa7, 0xab, 0x45, 0x02, 0x97, 0x60, 0x42,
150*0a6a1f1dSLionel Sambuc };
151*0a6a1f1dSLionel Sambuc 
152*0a6a1f1dSLionel Sambuc /*
153*0a6a1f1dSLionel Sambuc  * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
154*0a6a1f1dSLionel Sambuc  * PrivateKeyInfo.
155*0a6a1f1dSLionel Sambuc  */
156*0a6a1f1dSLionel Sambuc static const unsigned char kExampleRSAKeyPKCS8[] = {
157*0a6a1f1dSLionel Sambuc     0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
158*0a6a1f1dSLionel Sambuc     0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
159*0a6a1f1dSLionel Sambuc     0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
160*0a6a1f1dSLionel Sambuc     0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
161*0a6a1f1dSLionel Sambuc     0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
162*0a6a1f1dSLionel Sambuc     0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
163*0a6a1f1dSLionel Sambuc     0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
164*0a6a1f1dSLionel Sambuc     0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
165*0a6a1f1dSLionel Sambuc     0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
166*0a6a1f1dSLionel Sambuc     0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
167*0a6a1f1dSLionel Sambuc     0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
168*0a6a1f1dSLionel Sambuc     0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
169*0a6a1f1dSLionel Sambuc     0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
170*0a6a1f1dSLionel Sambuc     0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
171*0a6a1f1dSLionel Sambuc     0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
172*0a6a1f1dSLionel Sambuc     0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
173*0a6a1f1dSLionel Sambuc     0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
174*0a6a1f1dSLionel Sambuc     0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
175*0a6a1f1dSLionel Sambuc     0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
176*0a6a1f1dSLionel Sambuc     0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
177*0a6a1f1dSLionel Sambuc     0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
178*0a6a1f1dSLionel Sambuc     0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
179*0a6a1f1dSLionel Sambuc     0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
180*0a6a1f1dSLionel Sambuc     0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
181*0a6a1f1dSLionel Sambuc     0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
182*0a6a1f1dSLionel Sambuc     0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
183*0a6a1f1dSLionel Sambuc     0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
184*0a6a1f1dSLionel Sambuc     0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
185*0a6a1f1dSLionel Sambuc     0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
186*0a6a1f1dSLionel Sambuc     0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
187*0a6a1f1dSLionel Sambuc     0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
188*0a6a1f1dSLionel Sambuc     0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
189*0a6a1f1dSLionel Sambuc     0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
190*0a6a1f1dSLionel Sambuc     0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
191*0a6a1f1dSLionel Sambuc     0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
192*0a6a1f1dSLionel Sambuc     0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
193*0a6a1f1dSLionel Sambuc     0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
194*0a6a1f1dSLionel Sambuc     0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
195*0a6a1f1dSLionel Sambuc     0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
196*0a6a1f1dSLionel Sambuc     0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
197*0a6a1f1dSLionel Sambuc     0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
198*0a6a1f1dSLionel Sambuc     0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
199*0a6a1f1dSLionel Sambuc     0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
200*0a6a1f1dSLionel Sambuc     0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
201*0a6a1f1dSLionel Sambuc     0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
202*0a6a1f1dSLionel Sambuc     0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
203*0a6a1f1dSLionel Sambuc     0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
204*0a6a1f1dSLionel Sambuc     0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
205*0a6a1f1dSLionel Sambuc     0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
206*0a6a1f1dSLionel Sambuc     0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
207*0a6a1f1dSLionel Sambuc     0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
208*0a6a1f1dSLionel Sambuc     0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
209*0a6a1f1dSLionel Sambuc     0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
210*0a6a1f1dSLionel Sambuc };
211*0a6a1f1dSLionel Sambuc 
212*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_EC
213*0a6a1f1dSLionel Sambuc /*
214*0a6a1f1dSLionel Sambuc  * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
215*0a6a1f1dSLionel Sambuc  * structure.
216*0a6a1f1dSLionel Sambuc  */
217*0a6a1f1dSLionel Sambuc static const unsigned char kExampleECKeyDER[] = {
218*0a6a1f1dSLionel Sambuc     0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
219*0a6a1f1dSLionel Sambuc     0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
220*0a6a1f1dSLionel Sambuc     0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
221*0a6a1f1dSLionel Sambuc     0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
222*0a6a1f1dSLionel Sambuc     0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
223*0a6a1f1dSLionel Sambuc     0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
224*0a6a1f1dSLionel Sambuc     0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
225*0a6a1f1dSLionel Sambuc     0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
226*0a6a1f1dSLionel Sambuc     0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
227*0a6a1f1dSLionel Sambuc     0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
228*0a6a1f1dSLionel Sambuc     0xc1,
229*0a6a1f1dSLionel Sambuc };
230*0a6a1f1dSLionel Sambuc 
231*0a6a1f1dSLionel Sambuc /*
232*0a6a1f1dSLionel Sambuc  * kExampleBadECKeyDER is a sample EC private key encoded as an ECPrivateKey
233*0a6a1f1dSLionel Sambuc  * structure. The private key is equal to the order and will fail to import
234*0a6a1f1dSLionel Sambuc  */
235*0a6a1f1dSLionel Sambuc static const unsigned char kExampleBadECKeyDER[] = {
236*0a6a1f1dSLionel Sambuc     0x30, 0x66, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48,
237*0a6a1f1dSLionel Sambuc     0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03,
238*0a6a1f1dSLionel Sambuc     0x01, 0x07, 0x04, 0x4C, 0x30, 0x4A, 0x02, 0x01, 0x01, 0x04, 0x20, 0xFF,
239*0a6a1f1dSLionel Sambuc     0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
240*0a6a1f1dSLionel Sambuc     0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3,
241*0a6a1f1dSLionel Sambuc     0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51, 0xA1, 0x23, 0x03, 0x21, 0x00,
242*0a6a1f1dSLionel Sambuc     0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
243*0a6a1f1dSLionel Sambuc     0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
244*0a6a1f1dSLionel Sambuc     0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51
245*0a6a1f1dSLionel Sambuc };
246*0a6a1f1dSLionel Sambuc #endif
247*0a6a1f1dSLionel Sambuc 
load_example_rsa_key(void)248*0a6a1f1dSLionel Sambuc static EVP_PKEY *load_example_rsa_key(void)
249*0a6a1f1dSLionel Sambuc {
250*0a6a1f1dSLionel Sambuc     EVP_PKEY *ret = NULL;
251*0a6a1f1dSLionel Sambuc     const unsigned char *derp = kExampleRSAKeyDER;
252*0a6a1f1dSLionel Sambuc     EVP_PKEY *pkey = NULL;
253*0a6a1f1dSLionel Sambuc     RSA *rsa = NULL;
254*0a6a1f1dSLionel Sambuc 
255*0a6a1f1dSLionel Sambuc     if (!d2i_RSAPrivateKey(&rsa, &derp, sizeof(kExampleRSAKeyDER))) {
256*0a6a1f1dSLionel Sambuc         return NULL;
257*0a6a1f1dSLionel Sambuc     }
258*0a6a1f1dSLionel Sambuc 
259*0a6a1f1dSLionel Sambuc     pkey = EVP_PKEY_new();
260*0a6a1f1dSLionel Sambuc     if (pkey == NULL || !EVP_PKEY_set1_RSA(pkey, rsa)) {
261*0a6a1f1dSLionel Sambuc         goto out;
262*0a6a1f1dSLionel Sambuc     }
263*0a6a1f1dSLionel Sambuc 
264*0a6a1f1dSLionel Sambuc     ret = pkey;
265*0a6a1f1dSLionel Sambuc     pkey = NULL;
266*0a6a1f1dSLionel Sambuc 
267*0a6a1f1dSLionel Sambuc  out:
268*0a6a1f1dSLionel Sambuc     if (pkey) {
269*0a6a1f1dSLionel Sambuc         EVP_PKEY_free(pkey);
270*0a6a1f1dSLionel Sambuc     }
271*0a6a1f1dSLionel Sambuc     if (rsa) {
272*0a6a1f1dSLionel Sambuc         RSA_free(rsa);
273*0a6a1f1dSLionel Sambuc     }
274*0a6a1f1dSLionel Sambuc 
275*0a6a1f1dSLionel Sambuc     return ret;
276*0a6a1f1dSLionel Sambuc }
277*0a6a1f1dSLionel Sambuc 
test_EVP_DigestSignInit(void)278*0a6a1f1dSLionel Sambuc static int test_EVP_DigestSignInit(void)
279*0a6a1f1dSLionel Sambuc {
280*0a6a1f1dSLionel Sambuc     int ret = 0;
281*0a6a1f1dSLionel Sambuc     EVP_PKEY *pkey = NULL;
282*0a6a1f1dSLionel Sambuc     unsigned char *sig = NULL;
283*0a6a1f1dSLionel Sambuc     size_t sig_len = 0;
284*0a6a1f1dSLionel Sambuc     EVP_MD_CTX md_ctx, md_ctx_verify;
285*0a6a1f1dSLionel Sambuc 
286*0a6a1f1dSLionel Sambuc     EVP_MD_CTX_init(&md_ctx);
287*0a6a1f1dSLionel Sambuc     EVP_MD_CTX_init(&md_ctx_verify);
288*0a6a1f1dSLionel Sambuc 
289*0a6a1f1dSLionel Sambuc     pkey = load_example_rsa_key();
290*0a6a1f1dSLionel Sambuc     if (pkey == NULL ||
291*0a6a1f1dSLionel Sambuc         !EVP_DigestSignInit(&md_ctx, NULL, EVP_sha256(), NULL, pkey) ||
292*0a6a1f1dSLionel Sambuc         !EVP_DigestSignUpdate(&md_ctx, kMsg, sizeof(kMsg))) {
293*0a6a1f1dSLionel Sambuc         goto out;
294*0a6a1f1dSLionel Sambuc     }
295*0a6a1f1dSLionel Sambuc     /* Determine the size of the signature. */
296*0a6a1f1dSLionel Sambuc     if (!EVP_DigestSignFinal(&md_ctx, NULL, &sig_len)) {
297*0a6a1f1dSLionel Sambuc         goto out;
298*0a6a1f1dSLionel Sambuc     }
299*0a6a1f1dSLionel Sambuc     /* Sanity check for testing. */
300*0a6a1f1dSLionel Sambuc     if (sig_len != (size_t)EVP_PKEY_size(pkey)) {
301*0a6a1f1dSLionel Sambuc         fprintf(stderr, "sig_len mismatch\n");
302*0a6a1f1dSLionel Sambuc         goto out;
303*0a6a1f1dSLionel Sambuc     }
304*0a6a1f1dSLionel Sambuc 
305*0a6a1f1dSLionel Sambuc     sig = OPENSSL_malloc(sig_len);
306*0a6a1f1dSLionel Sambuc     if (sig == NULL || !EVP_DigestSignFinal(&md_ctx, sig, &sig_len)) {
307*0a6a1f1dSLionel Sambuc         goto out;
308*0a6a1f1dSLionel Sambuc     }
309*0a6a1f1dSLionel Sambuc 
310*0a6a1f1dSLionel Sambuc     /* Ensure that the signature round-trips. */
311*0a6a1f1dSLionel Sambuc     if (!EVP_DigestVerifyInit(&md_ctx_verify, NULL, EVP_sha256(), NULL, pkey)
312*0a6a1f1dSLionel Sambuc         || !EVP_DigestVerifyUpdate(&md_ctx_verify, kMsg, sizeof(kMsg))
313*0a6a1f1dSLionel Sambuc         || !EVP_DigestVerifyFinal(&md_ctx_verify, sig, sig_len)) {
314*0a6a1f1dSLionel Sambuc         goto out;
315*0a6a1f1dSLionel Sambuc     }
316*0a6a1f1dSLionel Sambuc 
317*0a6a1f1dSLionel Sambuc     ret = 1;
318*0a6a1f1dSLionel Sambuc 
319*0a6a1f1dSLionel Sambuc  out:
320*0a6a1f1dSLionel Sambuc     if (!ret) {
321*0a6a1f1dSLionel Sambuc         ERR_print_errors_fp(stderr);
322*0a6a1f1dSLionel Sambuc     }
323*0a6a1f1dSLionel Sambuc 
324*0a6a1f1dSLionel Sambuc     EVP_MD_CTX_cleanup(&md_ctx);
325*0a6a1f1dSLionel Sambuc     EVP_MD_CTX_cleanup(&md_ctx_verify);
326*0a6a1f1dSLionel Sambuc     if (pkey) {
327*0a6a1f1dSLionel Sambuc         EVP_PKEY_free(pkey);
328*0a6a1f1dSLionel Sambuc     }
329*0a6a1f1dSLionel Sambuc     if (sig) {
330*0a6a1f1dSLionel Sambuc         OPENSSL_free(sig);
331*0a6a1f1dSLionel Sambuc     }
332*0a6a1f1dSLionel Sambuc 
333*0a6a1f1dSLionel Sambuc     return ret;
334*0a6a1f1dSLionel Sambuc }
335*0a6a1f1dSLionel Sambuc 
test_EVP_DigestVerifyInit(void)336*0a6a1f1dSLionel Sambuc static int test_EVP_DigestVerifyInit(void)
337*0a6a1f1dSLionel Sambuc {
338*0a6a1f1dSLionel Sambuc     int ret = 0;
339*0a6a1f1dSLionel Sambuc     EVP_PKEY *pkey = NULL;
340*0a6a1f1dSLionel Sambuc     EVP_MD_CTX md_ctx;
341*0a6a1f1dSLionel Sambuc 
342*0a6a1f1dSLionel Sambuc     EVP_MD_CTX_init(&md_ctx);
343*0a6a1f1dSLionel Sambuc 
344*0a6a1f1dSLionel Sambuc     pkey = load_example_rsa_key();
345*0a6a1f1dSLionel Sambuc     if (pkey == NULL ||
346*0a6a1f1dSLionel Sambuc         !EVP_DigestVerifyInit(&md_ctx, NULL, EVP_sha256(), NULL, pkey) ||
347*0a6a1f1dSLionel Sambuc         !EVP_DigestVerifyUpdate(&md_ctx, kMsg, sizeof(kMsg)) ||
348*0a6a1f1dSLionel Sambuc         !EVP_DigestVerifyFinal(&md_ctx, (unsigned char *)kSignature, sizeof(kSignature))) {
349*0a6a1f1dSLionel Sambuc         goto out;
350*0a6a1f1dSLionel Sambuc     }
351*0a6a1f1dSLionel Sambuc     ret = 1;
352*0a6a1f1dSLionel Sambuc 
353*0a6a1f1dSLionel Sambuc  out:
354*0a6a1f1dSLionel Sambuc     if (!ret) {
355*0a6a1f1dSLionel Sambuc         ERR_print_errors_fp(stderr);
356*0a6a1f1dSLionel Sambuc     }
357*0a6a1f1dSLionel Sambuc 
358*0a6a1f1dSLionel Sambuc     EVP_MD_CTX_cleanup(&md_ctx);
359*0a6a1f1dSLionel Sambuc     if (pkey) {
360*0a6a1f1dSLionel Sambuc         EVP_PKEY_free(pkey);
361*0a6a1f1dSLionel Sambuc     }
362*0a6a1f1dSLionel Sambuc 
363*0a6a1f1dSLionel Sambuc     return ret;
364*0a6a1f1dSLionel Sambuc }
365*0a6a1f1dSLionel Sambuc 
test_d2i_AutoPrivateKey(const unsigned char * input,size_t input_len,int expected_id)366*0a6a1f1dSLionel Sambuc static int test_d2i_AutoPrivateKey(const unsigned char *input,
367*0a6a1f1dSLionel Sambuc                                    size_t input_len, int expected_id)
368*0a6a1f1dSLionel Sambuc {
369*0a6a1f1dSLionel Sambuc     int ret = 0;
370*0a6a1f1dSLionel Sambuc     const unsigned char *p;
371*0a6a1f1dSLionel Sambuc     EVP_PKEY *pkey = NULL;
372*0a6a1f1dSLionel Sambuc 
373*0a6a1f1dSLionel Sambuc     p = input;
374*0a6a1f1dSLionel Sambuc     pkey = d2i_AutoPrivateKey(NULL, &p, input_len);
375*0a6a1f1dSLionel Sambuc     if (pkey == NULL || p != input + input_len) {
376*0a6a1f1dSLionel Sambuc         fprintf(stderr, "d2i_AutoPrivateKey failed\n");
377*0a6a1f1dSLionel Sambuc         goto done;
378*0a6a1f1dSLionel Sambuc     }
379*0a6a1f1dSLionel Sambuc 
380*0a6a1f1dSLionel Sambuc     if (EVP_PKEY_id(pkey) != expected_id) {
381*0a6a1f1dSLionel Sambuc         fprintf(stderr, "Did not decode expected type\n");
382*0a6a1f1dSLionel Sambuc         goto done;
383*0a6a1f1dSLionel Sambuc     }
384*0a6a1f1dSLionel Sambuc 
385*0a6a1f1dSLionel Sambuc     ret = 1;
386*0a6a1f1dSLionel Sambuc 
387*0a6a1f1dSLionel Sambuc  done:
388*0a6a1f1dSLionel Sambuc     if (!ret) {
389*0a6a1f1dSLionel Sambuc         ERR_print_errors_fp(stderr);
390*0a6a1f1dSLionel Sambuc     }
391*0a6a1f1dSLionel Sambuc 
392*0a6a1f1dSLionel Sambuc     if (pkey != NULL) {
393*0a6a1f1dSLionel Sambuc         EVP_PKEY_free(pkey);
394*0a6a1f1dSLionel Sambuc     }
395*0a6a1f1dSLionel Sambuc     return ret;
396*0a6a1f1dSLionel Sambuc }
397*0a6a1f1dSLionel Sambuc 
398*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_EC
399*0a6a1f1dSLionel Sambuc /* Tests loading a bad key in PKCS8 format */
test_EVP_PKCS82PKEY(void)400*0a6a1f1dSLionel Sambuc static int test_EVP_PKCS82PKEY(void)
401*0a6a1f1dSLionel Sambuc {
402*0a6a1f1dSLionel Sambuc     int ret = 0;
403*0a6a1f1dSLionel Sambuc     const unsigned char *derp = kExampleBadECKeyDER;
404*0a6a1f1dSLionel Sambuc     PKCS8_PRIV_KEY_INFO *p8inf = NULL;
405*0a6a1f1dSLionel Sambuc     EVP_PKEY *pkey = NULL;
406*0a6a1f1dSLionel Sambuc 
407*0a6a1f1dSLionel Sambuc     p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp, sizeof(kExampleBadECKeyDER));
408*0a6a1f1dSLionel Sambuc 
409*0a6a1f1dSLionel Sambuc     if (!p8inf || derp != kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER)) {
410*0a6a1f1dSLionel Sambuc         fprintf(stderr, "Failed to parse key\n");
411*0a6a1f1dSLionel Sambuc         goto done;
412*0a6a1f1dSLionel Sambuc     }
413*0a6a1f1dSLionel Sambuc 
414*0a6a1f1dSLionel Sambuc     pkey = EVP_PKCS82PKEY(p8inf);
415*0a6a1f1dSLionel Sambuc     if (pkey) {
416*0a6a1f1dSLionel Sambuc         fprintf(stderr, "Imported invalid EC key\n");
417*0a6a1f1dSLionel Sambuc         goto done;
418*0a6a1f1dSLionel Sambuc     }
419*0a6a1f1dSLionel Sambuc 
420*0a6a1f1dSLionel Sambuc     ret = 1;
421*0a6a1f1dSLionel Sambuc 
422*0a6a1f1dSLionel Sambuc  done:
423*0a6a1f1dSLionel Sambuc     if (p8inf != NULL) {
424*0a6a1f1dSLionel Sambuc         PKCS8_PRIV_KEY_INFO_free(p8inf);
425*0a6a1f1dSLionel Sambuc     }
426*0a6a1f1dSLionel Sambuc 
427*0a6a1f1dSLionel Sambuc     if (pkey != NULL) {
428*0a6a1f1dSLionel Sambuc         EVP_PKEY_free(pkey);
429*0a6a1f1dSLionel Sambuc     }
430*0a6a1f1dSLionel Sambuc 
431*0a6a1f1dSLionel Sambuc     return ret;
432*0a6a1f1dSLionel Sambuc }
433*0a6a1f1dSLionel Sambuc #endif
434*0a6a1f1dSLionel Sambuc 
main(void)435*0a6a1f1dSLionel Sambuc int main(void)
436*0a6a1f1dSLionel Sambuc {
437*0a6a1f1dSLionel Sambuc     CRYPTO_malloc_debug_init();
438*0a6a1f1dSLionel Sambuc     CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
439*0a6a1f1dSLionel Sambuc     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
440*0a6a1f1dSLionel Sambuc 
441*0a6a1f1dSLionel Sambuc     ERR_load_crypto_strings();
442*0a6a1f1dSLionel Sambuc     /* Load up the software EVP_CIPHER and EVP_MD definitions */
443*0a6a1f1dSLionel Sambuc     OpenSSL_add_all_ciphers();
444*0a6a1f1dSLionel Sambuc     OpenSSL_add_all_digests();
445*0a6a1f1dSLionel Sambuc 
446*0a6a1f1dSLionel Sambuc     if (!test_EVP_DigestSignInit()) {
447*0a6a1f1dSLionel Sambuc         fprintf(stderr, "EVP_DigestSignInit failed\n");
448*0a6a1f1dSLionel Sambuc         return 1;
449*0a6a1f1dSLionel Sambuc     }
450*0a6a1f1dSLionel Sambuc 
451*0a6a1f1dSLionel Sambuc     if (!test_EVP_DigestVerifyInit()) {
452*0a6a1f1dSLionel Sambuc         fprintf(stderr, "EVP_DigestVerifyInit failed\n");
453*0a6a1f1dSLionel Sambuc         return 1;
454*0a6a1f1dSLionel Sambuc     }
455*0a6a1f1dSLionel Sambuc 
456*0a6a1f1dSLionel Sambuc     if (!test_d2i_AutoPrivateKey(kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER),
457*0a6a1f1dSLionel Sambuc                                  EVP_PKEY_RSA)) {
458*0a6a1f1dSLionel Sambuc         fprintf(stderr, "d2i_AutoPrivateKey(kExampleRSAKeyDER) failed\n");
459*0a6a1f1dSLionel Sambuc         return 1;
460*0a6a1f1dSLionel Sambuc     }
461*0a6a1f1dSLionel Sambuc 
462*0a6a1f1dSLionel Sambuc     if (!test_d2i_AutoPrivateKey
463*0a6a1f1dSLionel Sambuc         (kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), EVP_PKEY_RSA)) {
464*0a6a1f1dSLionel Sambuc         fprintf(stderr, "d2i_AutoPrivateKey(kExampleRSAKeyPKCS8) failed\n");
465*0a6a1f1dSLionel Sambuc         return 1;
466*0a6a1f1dSLionel Sambuc     }
467*0a6a1f1dSLionel Sambuc 
468*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_EC
469*0a6a1f1dSLionel Sambuc     if (!test_d2i_AutoPrivateKey(kExampleECKeyDER, sizeof(kExampleECKeyDER),
470*0a6a1f1dSLionel Sambuc                                  EVP_PKEY_EC)) {
471*0a6a1f1dSLionel Sambuc         fprintf(stderr, "d2i_AutoPrivateKey(kExampleECKeyDER) failed\n");
472*0a6a1f1dSLionel Sambuc         return 1;
473*0a6a1f1dSLionel Sambuc     }
474*0a6a1f1dSLionel Sambuc 
475*0a6a1f1dSLionel Sambuc     if (!test_EVP_PKCS82PKEY()) {
476*0a6a1f1dSLionel Sambuc         fprintf(stderr, "test_EVP_PKCS82PKEY failed\n");
477*0a6a1f1dSLionel Sambuc         return 1;
478*0a6a1f1dSLionel Sambuc     }
479*0a6a1f1dSLionel Sambuc #endif
480*0a6a1f1dSLionel Sambuc 
481*0a6a1f1dSLionel Sambuc     EVP_cleanup();
482*0a6a1f1dSLionel Sambuc     CRYPTO_cleanup_all_ex_data();
483*0a6a1f1dSLionel Sambuc     ERR_remove_thread_state(NULL);
484*0a6a1f1dSLionel Sambuc     ERR_free_strings();
485*0a6a1f1dSLionel Sambuc     CRYPTO_mem_leaks_fp(stderr);
486*0a6a1f1dSLionel Sambuc 
487*0a6a1f1dSLionel Sambuc     printf("PASS\n");
488*0a6a1f1dSLionel Sambuc     return 0;
489*0a6a1f1dSLionel Sambuc }
490