1 /* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <stdint.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include <utility>
21 #include <vector>
22
23 #include <gtest/gtest.h>
24
25 #include <openssl/bytestring.h>
26 #include <openssl/crypto.h>
27 #include <openssl/dh.h>
28 #include <openssl/digest.h>
29 #include <openssl/err.h>
30 #include <openssl/pkcs8.h>
31 #include <openssl/rsa.h>
32
33 #include "../internal.h"
34 #include "../test/test_util.h"
35
36
37 // kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
38 // should never use this key anywhere but in an example.
39 static const uint8_t kExampleRSAKeyDER[] = {
40 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
41 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
42 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
43 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
44 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
45 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8, 0x50, 0xe4,
46 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b, 0xdc, 0xec,
47 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83, 0x58, 0x76,
48 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48, 0x1a, 0xd8,
49 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a, 0x5c, 0xd7,
50 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2, 0xa7, 0x2c,
51 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01,
52 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a, 0x6d, 0xc7,
53 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5, 0x32, 0x85,
54 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6, 0x5f, 0xee,
55 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8, 0x66, 0x85,
56 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6, 0xa4, 0x0a,
57 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f, 0xc2, 0x15,
58 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c, 0x5b, 0x83,
59 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78, 0x80, 0x1b,
60 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71, 0x99, 0x73,
61 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60, 0x1f, 0x99,
62 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d, 0xb1, 0x02,
63 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3, 0x40, 0x41,
64 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d, 0x3d, 0x59,
65 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18, 0xc6, 0xd9,
66 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d, 0x9f, 0xef,
67 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32, 0x46, 0x87,
68 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc, 0x2c, 0xdf,
69 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63, 0x55, 0xf5,
70 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05, 0xcd, 0xb5,
71 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16, 0xb3, 0x62,
72 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3, 0x9b, 0x64,
73 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85, 0xfa, 0xb8,
74 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97, 0xe8, 0xba,
75 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7, 0xe7, 0xfe,
76 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99, 0x75, 0xe7,
77 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4, 0x9d, 0xfe,
78 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d, 0xf1, 0xdb,
79 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40, 0x5a, 0x34,
80 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26, 0x84, 0x27,
81 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1, 0xe9, 0xc0,
82 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c, 0xb9, 0xba,
83 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30, 0x10, 0x06,
84 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea, 0x52, 0x2c,
85 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b, 0xc4, 0x1e,
86 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e, 0x49, 0xaf,
87 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9, 0xd1, 0x8a,
88 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae, 0x17, 0x17,
89 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d, 0x08, 0xf1,
90 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
91 };
92
93 static const uint8_t kExampleDSAKeyDER[] = {
94 0x30, 0x82, 0x03, 0x56, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
95 0x9e, 0x12, 0xfa, 0xb3, 0xde, 0x12, 0x21, 0x35, 0x01, 0xdd, 0x82, 0xaa,
96 0x10, 0xca, 0x2d, 0x10, 0x1d, 0x2d, 0x4e, 0xbf, 0xef, 0x4d, 0x2a, 0x3f,
97 0x8d, 0xaa, 0x0f, 0xe0, 0xce, 0xda, 0xd8, 0xd6, 0xaf, 0x85, 0x61, 0x6a,
98 0xa2, 0xf3, 0x25, 0x2c, 0x0a, 0x2b, 0x5a, 0x6d, 0xb0, 0x9e, 0x6f, 0x14,
99 0x90, 0x0e, 0x0d, 0xdb, 0x83, 0x11, 0x87, 0x6d, 0xd8, 0xf9, 0x66, 0x95,
100 0x25, 0xf9, 0x9e, 0xd6, 0x59, 0x49, 0xe1, 0x84, 0xd5, 0x06, 0x47, 0x93,
101 0x27, 0x11, 0x69, 0xa2, 0x28, 0x68, 0x0b, 0x95, 0xec, 0x12, 0xf5, 0x9a,
102 0x8e, 0x20, 0xb2, 0x1f, 0x2b, 0x58, 0xeb, 0x2a, 0x20, 0x12, 0xd3, 0x5b,
103 0xde, 0x2e, 0xe3, 0x51, 0x82, 0x2f, 0xe8, 0xf3, 0x2d, 0x0a, 0x33, 0x05,
104 0x65, 0xdc, 0xce, 0x5c, 0x67, 0x2b, 0x72, 0x59, 0xc1, 0x4b, 0x24, 0x33,
105 0xd0, 0xb5, 0xb2, 0xca, 0x2b, 0x2d, 0xb0, 0xab, 0x62, 0x6e, 0x8f, 0x13,
106 0xf4, 0x7f, 0xe0, 0x34, 0x5d, 0x90, 0x4e, 0x72, 0x94, 0xbb, 0x03, 0x8e,
107 0x9c, 0xe2, 0x1a, 0x9e, 0x58, 0x0b, 0x83, 0x35, 0x62, 0x78, 0x70, 0x6c,
108 0xfe, 0x76, 0x84, 0x36, 0xc6, 0x9d, 0xe1, 0x49, 0xcc, 0xff, 0x98, 0xb4,
109 0xaa, 0xb8, 0xcb, 0x4f, 0x63, 0x85, 0xc9, 0xf1, 0x02, 0xce, 0x59, 0x34,
110 0x6e, 0xae, 0xef, 0x27, 0xe0, 0xad, 0x22, 0x2d, 0x53, 0xd6, 0xe8, 0x9c,
111 0xc8, 0xcd, 0xe5, 0x77, 0x6d, 0xd0, 0x00, 0x57, 0xb0, 0x3f, 0x2d, 0x88,
112 0xab, 0x3c, 0xed, 0xba, 0xfd, 0x7b, 0x58, 0x5f, 0x0b, 0x7f, 0x78, 0x35,
113 0xe1, 0x7a, 0x37, 0x28, 0xbb, 0xf2, 0x5e, 0xa6, 0x25, 0x72, 0xf2, 0x45,
114 0xdc, 0x11, 0x1f, 0x3c, 0xe3, 0x9c, 0xb6, 0xff, 0xac, 0xc3, 0x1b, 0x0a,
115 0x27, 0x90, 0xe7, 0xbd, 0xe9, 0x02, 0x24, 0xea, 0x9b, 0x09, 0x31, 0x53,
116 0x62, 0xaf, 0x3d, 0x2b, 0x02, 0x21, 0x00, 0xf3, 0x81, 0xdc, 0xf5, 0x3e,
117 0xbf, 0x72, 0x4f, 0x8b, 0x2e, 0x5c, 0xa8, 0x2c, 0x01, 0x0f, 0xb4, 0xb5,
118 0xed, 0xa9, 0x35, 0x8d, 0x0f, 0xd8, 0x8e, 0xd2, 0x78, 0x58, 0x94, 0x88,
119 0xb5, 0x4f, 0xc3, 0x02, 0x82, 0x01, 0x00, 0x0c, 0x40, 0x2a, 0x72, 0x5d,
120 0xcc, 0x3a, 0x62, 0xe0, 0x2b, 0xf4, 0xcf, 0x43, 0xcd, 0x17, 0xf4, 0xa4,
121 0x93, 0x59, 0x12, 0x20, 0x22, 0x36, 0x69, 0xcf, 0x41, 0x93, 0xed, 0xab,
122 0x42, 0x3a, 0xd0, 0x8d, 0xfb, 0x55, 0x2e, 0x30, 0x8a, 0x6a, 0x57, 0xa5,
123 0xff, 0xbc, 0x7c, 0xd0, 0xfb, 0x20, 0x87, 0xf8, 0x1f, 0x8d, 0xf0, 0xcb,
124 0x08, 0xab, 0x21, 0x33, 0x28, 0x7d, 0x2b, 0x69, 0x68, 0x71, 0x4a, 0x94,
125 0xf6, 0x33, 0xc9, 0x40, 0x84, 0x5a, 0x48, 0xa3, 0xe1, 0x67, 0x08, 0xdd,
126 0xe7, 0x61, 0xcc, 0x6a, 0x8e, 0xab, 0x2d, 0x84, 0xdb, 0x21, 0xb6, 0xea,
127 0x5b, 0x07, 0x68, 0x14, 0x93, 0xcc, 0x9c, 0x31, 0xfb, 0xc3, 0x68, 0xb2,
128 0x43, 0xf6, 0xdd, 0xf8, 0xc9, 0x32, 0xa8, 0xb4, 0x03, 0x8f, 0x44, 0xe7,
129 0xb1, 0x5c, 0xa8, 0x76, 0x34, 0x4a, 0x14, 0x78, 0x59, 0xf2, 0xb4, 0x3b,
130 0x39, 0x45, 0x86, 0x68, 0xad, 0x5e, 0x0a, 0x1a, 0x9a, 0x66, 0x95, 0x46,
131 0xdd, 0x28, 0x12, 0xe3, 0xb3, 0x61, 0x7a, 0x0a, 0xef, 0x99, 0xd5, 0x8e,
132 0x3b, 0xb4, 0xcc, 0x87, 0xfd, 0x94, 0x22, 0x5e, 0x01, 0xd2, 0xdc, 0xc4,
133 0x69, 0xa7, 0x72, 0x68, 0x14, 0x6c, 0x51, 0x91, 0x8f, 0x18, 0xe8, 0xb4,
134 0xd7, 0x0a, 0xa1, 0xf0, 0xc7, 0x62, 0x3b, 0xcc, 0x52, 0xcf, 0x37, 0x31,
135 0xd3, 0x86, 0x41, 0xb2, 0xd2, 0x83, 0x0b, 0x7e, 0xec, 0xb2, 0xf0, 0x95,
136 0x52, 0xff, 0x13, 0x7d, 0x04, 0x6e, 0x49, 0x4e, 0x7f, 0x33, 0xc3, 0x59,
137 0x00, 0x02, 0xb1, 0x6d, 0x1b, 0x97, 0xd9, 0x36, 0xfd, 0xa2, 0x8f, 0x90,
138 0xc3, 0xed, 0x3c, 0xa3, 0x53, 0x38, 0x16, 0x8a, 0xc1, 0x6f, 0x77, 0xc3,
139 0xc5, 0x7a, 0xdc, 0x2e, 0x8f, 0x7c, 0x6c, 0x22, 0x56, 0xe4, 0x1a, 0x5f,
140 0x65, 0x45, 0x05, 0x90, 0xdb, 0xb5, 0xbc, 0xf0, 0x6d, 0x66, 0x61, 0x02,
141 0x82, 0x01, 0x00, 0x31, 0x97, 0x31, 0xa1, 0x4e, 0x38, 0x56, 0x88, 0xdb,
142 0x94, 0x1d, 0xbf, 0x65, 0x5c, 0xda, 0x4b, 0xc2, 0x10, 0xde, 0x74, 0x20,
143 0x03, 0xce, 0x13, 0x60, 0xf2, 0x25, 0x1d, 0x55, 0x7c, 0x5d, 0x94, 0x82,
144 0x54, 0x08, 0x53, 0xdb, 0x85, 0x95, 0xbf, 0xdd, 0x5e, 0x50, 0xd5, 0x96,
145 0xe0, 0x79, 0x51, 0x1b, 0xbf, 0x4d, 0x4e, 0xb9, 0x3a, 0xc5, 0xee, 0xc4,
146 0x5e, 0x98, 0x75, 0x7b, 0xbe, 0xff, 0x30, 0xe6, 0xd0, 0x7b, 0xa6, 0xf1,
147 0xbc, 0x29, 0xea, 0xdf, 0xec, 0xf3, 0x8b, 0xfa, 0x83, 0x11, 0x9f, 0x3f,
148 0xf0, 0x5d, 0x06, 0x51, 0x32, 0xaa, 0x21, 0xfc, 0x26, 0x17, 0xe7, 0x50,
149 0xc2, 0x16, 0xba, 0xfa, 0x54, 0xb7, 0x7e, 0x1d, 0x2c, 0xa6, 0xa3, 0x41,
150 0x66, 0x33, 0x94, 0x83, 0xb9, 0xbf, 0xa0, 0x4f, 0xbd, 0xa6, 0xfd, 0x2c,
151 0x81, 0x58, 0x35, 0x33, 0x39, 0xc0, 0x6d, 0x33, 0x40, 0x56, 0x64, 0x12,
152 0x5a, 0xcd, 0x35, 0x53, 0x21, 0x78, 0x8f, 0x27, 0x24, 0x37, 0x66, 0x8a,
153 0xdf, 0x5e, 0x5f, 0x63, 0xfc, 0x8b, 0x2d, 0xef, 0x57, 0xdb, 0x40, 0x25,
154 0xd5, 0x17, 0x53, 0x0b, 0xe4, 0xa5, 0xae, 0x54, 0xbf, 0x46, 0x4f, 0xa6,
155 0x79, 0xc3, 0x74, 0xfa, 0x1f, 0x85, 0x34, 0x64, 0x6d, 0xc5, 0x03, 0xeb,
156 0x72, 0x98, 0x80, 0x7b, 0xc0, 0x8f, 0x35, 0x11, 0xa7, 0x09, 0xeb, 0x51,
157 0xe0, 0xb0, 0xac, 0x92, 0x14, 0xf2, 0xad, 0x37, 0x95, 0x5a, 0xba, 0x8c,
158 0xc4, 0xdb, 0xed, 0xc4, 0x4e, 0x8b, 0x8f, 0x84, 0x33, 0x64, 0xf8, 0x57,
159 0x12, 0xd7, 0x08, 0x7e, 0x90, 0x66, 0xdf, 0x91, 0x50, 0x23, 0xf2, 0x73,
160 0xc0, 0x6b, 0xb1, 0x15, 0xdd, 0x64, 0xd7, 0xc9, 0x75, 0x17, 0x73, 0x72,
161 0xda, 0x33, 0xc4, 0x6f, 0xa5, 0x47, 0xa1, 0xcc, 0xd1, 0xc6, 0x62, 0xe5,
162 0xca, 0xab, 0x5f, 0x2a, 0x8f, 0x6b, 0xcc, 0x02, 0x21, 0x00, 0xb0, 0xc7,
163 0x68, 0x70, 0x27, 0x43, 0xbc, 0x51, 0x24, 0x29, 0x93, 0xa9, 0x71, 0xa5,
164 0x28, 0x89, 0x79, 0x54, 0x44, 0xf7, 0xc6, 0x45, 0x22, 0x03, 0xd0, 0xce,
165 0x84, 0xfe, 0x61, 0x17, 0xd4, 0x6e,
166 };
167
168 static const uint8_t kMsg[] = {1, 2, 3, 4};
169
170 static const uint8_t kSignature[] = {
171 0xa5, 0xf0, 0x8a, 0x47, 0x5d, 0x3c, 0xb3, 0xcc, 0xa9, 0x79, 0xaf, 0x4d,
172 0x8c, 0xae, 0x4c, 0x14, 0xef, 0xc2, 0x0b, 0x34, 0x36, 0xde, 0xf4, 0x3e,
173 0x3d, 0xbb, 0x4a, 0x60, 0x5c, 0xc8, 0x91, 0x28, 0xda, 0xfb, 0x7e, 0x04,
174 0x96, 0x7e, 0x63, 0x13, 0x90, 0xce, 0xb9, 0xb4, 0x62, 0x7a, 0xfd, 0x09,
175 0x3d, 0xc7, 0x67, 0x78, 0x54, 0x04, 0xeb, 0x52, 0x62, 0x6e, 0x24, 0x67,
176 0xb4, 0x40, 0xfc, 0x57, 0x62, 0xc6, 0xf1, 0x67, 0xc1, 0x97, 0x8f, 0x6a,
177 0xa8, 0xae, 0x44, 0x46, 0x5e, 0xab, 0x67, 0x17, 0x53, 0x19, 0x3a, 0xda,
178 0x5a, 0xc8, 0x16, 0x3e, 0x86, 0xd5, 0xc5, 0x71, 0x2f, 0xfc, 0x23, 0x48,
179 0xd9, 0x0b, 0x13, 0xdd, 0x7b, 0x5a, 0x25, 0x79, 0xef, 0xa5, 0x7b, 0x04,
180 0xed, 0x44, 0xf6, 0x18, 0x55, 0xe4, 0x0a, 0xe9, 0x57, 0x79, 0x5d, 0xd7,
181 0x55, 0xa7, 0xab, 0x45, 0x02, 0x97, 0x60, 0x42,
182 };
183
184 // kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
185 // PrivateKeyInfo.
186 static const uint8_t kExampleRSAKeyPKCS8[] = {
187 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
188 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
189 0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
190 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
191 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
192 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
193 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
194 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
195 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
196 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
197 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
198 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
199 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
200 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
201 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
202 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
203 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
204 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
205 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
206 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
207 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
208 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
209 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
210 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
211 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
212 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
213 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
214 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
215 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
216 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
217 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
218 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
219 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
220 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
221 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
222 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
223 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
224 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
225 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
226 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
227 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
228 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
229 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
230 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
231 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
232 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
233 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
234 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
235 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
236 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
237 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
238 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
239 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
240 };
241
242 // kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
243 // structure.
244 static const uint8_t kExampleECKeyDER[] = {
245 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
246 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
247 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
248 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
249 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
250 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
251 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
252 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
253 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
254 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
255 0xc1,
256 };
257
258 // kExampleECKeyPKCS8 is a sample EC private key encoded as a PKCS#8
259 // PrivateKeyInfo.
260 static const uint8_t kExampleECKeyPKCS8[] = {
261 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
262 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
263 0x03, 0x01, 0x07, 0x04, 0x6d, 0x30, 0x6b, 0x02, 0x01, 0x01, 0x04, 0x20,
264 0x43, 0x09, 0xc0, 0x67, 0x75, 0x21, 0x47, 0x9d, 0xa8, 0xfa, 0x16, 0xdf,
265 0x15, 0x73, 0x61, 0x34, 0x68, 0x6f, 0xe3, 0x8e, 0x47, 0x91, 0x95, 0xab,
266 0x79, 0x4a, 0x72, 0x14, 0xcb, 0xe2, 0x49, 0x4f, 0xa1, 0x44, 0x03, 0x42,
267 0x00, 0x04, 0xde, 0x09, 0x08, 0x07, 0x03, 0x2e, 0x8f, 0x37, 0x9a, 0xd5,
268 0xad, 0xe5, 0xc6, 0x9d, 0xd4, 0x63, 0xc7, 0x4a, 0xe7, 0x20, 0xcb, 0x90,
269 0xa0, 0x1f, 0x18, 0x18, 0x72, 0xb5, 0x21, 0x88, 0x38, 0xc0, 0xdb, 0xba,
270 0xf6, 0x99, 0xd8, 0xa5, 0x3b, 0x83, 0xe9, 0xe3, 0xd5, 0x61, 0x99, 0x73,
271 0x42, 0xc6, 0x6c, 0xe8, 0x0a, 0x95, 0x40, 0x41, 0x3b, 0x0d, 0x10, 0xa7,
272 0x4a, 0x93, 0xdb, 0x5a, 0xe7, 0xec,
273 };
274
275 // kExampleECKeySpecifiedCurvePKCS8 is a sample EC private key encoded as a
276 // PKCS#8 PrivateKeyInfo with P-256's parameters spelled out rather than using
277 // the curve OID.
278 static const uint8_t kExampleECKeySpecifiedCurvePKCS8[] = {
279 0x30, 0x82, 0x01, 0x79, 0x02, 0x01, 0x00, 0x30, 0x82, 0x01, 0x03, 0x06,
280 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, 0x30, 0x81, 0xf7, 0x02,
281 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01,
282 0x01, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
283 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
284 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
285 0x30, 0x5b, 0x04, 0x20, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
286 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
287 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
288 0x04, 0x20, 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 0xb3, 0xeb,
289 0xbd, 0x55, 0x76, 0x98, 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53,
290 0xb0, 0xf6, 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b, 0x03, 0x15,
291 0x00, 0xc4, 0x9d, 0x36, 0x08, 0x86, 0xe7, 0x04, 0x93, 0x6a, 0x66, 0x78,
292 0xe1, 0x13, 0x9d, 0x26, 0xb7, 0x81, 0x9f, 0x7e, 0x90, 0x04, 0x41, 0x04,
293 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6, 0xe5,
294 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0,
295 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3, 0x42, 0xe2,
296 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16,
297 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68,
298 0x37, 0xbf, 0x51, 0xf5, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00,
299 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc,
300 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc,
301 0x63, 0x25, 0x51, 0x02, 0x01, 0x01, 0x04, 0x6d, 0x30, 0x6b, 0x02, 0x01,
302 0x01, 0x04, 0x20, 0x43, 0x09, 0xc0, 0x67, 0x75, 0x21, 0x47, 0x9d, 0xa8,
303 0xfa, 0x16, 0xdf, 0x15, 0x73, 0x61, 0x34, 0x68, 0x6f, 0xe3, 0x8e, 0x47,
304 0x91, 0x95, 0xab, 0x79, 0x4a, 0x72, 0x14, 0xcb, 0xe2, 0x49, 0x4f, 0xa1,
305 0x44, 0x03, 0x42, 0x00, 0x04, 0xde, 0x09, 0x08, 0x07, 0x03, 0x2e, 0x8f,
306 0x37, 0x9a, 0xd5, 0xad, 0xe5, 0xc6, 0x9d, 0xd4, 0x63, 0xc7, 0x4a, 0xe7,
307 0x20, 0xcb, 0x90, 0xa0, 0x1f, 0x18, 0x18, 0x72, 0xb5, 0x21, 0x88, 0x38,
308 0xc0, 0xdb, 0xba, 0xf6, 0x99, 0xd8, 0xa5, 0x3b, 0x83, 0xe9, 0xe3, 0xd5,
309 0x61, 0x99, 0x73, 0x42, 0xc6, 0x6c, 0xe8, 0x0a, 0x95, 0x40, 0x41, 0x3b,
310 0x0d, 0x10, 0xa7, 0x4a, 0x93, 0xdb, 0x5a, 0xe7, 0xec,
311 };
312
313 // kExampleBadECKeyDER is a sample EC private key encoded as an ECPrivateKey
314 // structure. The private key is equal to the order and will fail to import.
315 static const uint8_t kExampleBadECKeyDER[] = {
316 0x30, 0x66, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48,
317 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03,
318 0x01, 0x07, 0x04, 0x4C, 0x30, 0x4A, 0x02, 0x01, 0x01, 0x04, 0x20, 0xFF,
319 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
320 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84, 0xF3,
321 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51, 0xA1, 0x23, 0x03, 0x21, 0x00,
322 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
323 0xFF, 0xFF, 0xFF, 0xFF, 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
324 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51
325 };
326
327 // kExampleBadECKeyDER2 is a sample EC private key encoded as an ECPrivateKey
328 // structure, but with the curve OID swapped out for 1.1.1.1.1.1.1.1.1. It is
329 // then concatenated with an ECPrivateKey wrapped in a PrivateKeyInfo,
330 // optional public key omitted, and with the private key chopped off.
331 static const uint8_t kExampleBadECKeyDER2[] = {
332 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
333 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
334 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
335 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x29, 0x01, 0x01, 0x01, 0x01,
336 0x01, 0x01, 0x01, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
337 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
338 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
339 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
340 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
341 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
342 0xc1, 0x30, 0x41, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
343 0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
344 0x03, 0x01, 0x07, 0x04, 0x27, 0x30, 0x25, 0x02, 0x01, 0x01, 0x04, 0x20,
345 0x07,
346 };
347
348 // kInvalidPrivateKey is an invalid private key. See
349 // https://rt.openssl.org/Ticket/Display.html?id=4131.
350 static const uint8_t kInvalidPrivateKey[] = {
351 0x30, 0x39, 0x02, 0x01, 0x02, 0x30, 0x09, 0x06, 0x01, 0x38, 0x08,
352 0x04, 0x69, 0x30, 0x30, 0x80, 0x30, 0x19, 0x01, 0x02, 0x9f, 0xf8,
353 0x8b, 0x29, 0x80, 0x30, 0xb0, 0x1b, 0x06, 0x09, 0x22, 0xbe, 0x08,
354 0x04, 0xe9, 0x30, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x3a, 0x01, 0x80,
355 0x09, 0x30, 0x80, 0x06, 0x01, 0x02, 0x30, 0x80, 0x30, 0x01, 0x3b,
356 0x02, 0x00, 0x00, 0x04, 0x20, 0x30, 0x82, 0x04, 0xe9, 0x30, 0xc3,
357 0xe8, 0x30, 0x01, 0x05, 0x30, 0x80, 0x30, 0x01, 0x3b, 0x01, 0x04,
358 0x02, 0x02, 0xff, 0x00, 0x30, 0x29, 0x02, 0x11, 0x03, 0x29, 0x29,
359 0x02, 0x00, 0x99, 0x30, 0x80, 0x06, 0x21, 0x02, 0x24, 0x04, 0xe8,
360 0x30, 0x01, 0x01, 0x04, 0x30, 0x80, 0x1b, 0x06, 0x09, 0x2a, 0x86,
361 0x48, 0x30, 0x01, 0xaa, 0x02, 0x86, 0xc0, 0x30, 0xdf, 0xe9, 0x80,
362 };
363
LoadExampleRSAKey()364 static bssl::UniquePtr<EVP_PKEY> LoadExampleRSAKey() {
365 bssl::UniquePtr<RSA> rsa(RSA_private_key_from_bytes(kExampleRSAKeyDER,
366 sizeof(kExampleRSAKeyDER)));
367 if (!rsa) {
368 return nullptr;
369 }
370 bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
371 if (!pkey || !EVP_PKEY_set1_RSA(pkey.get(), rsa.get())) {
372 return nullptr;
373 }
374 return pkey;
375 }
376
TEST(EVPExtraTest,DigestSignInit)377 TEST(EVPExtraTest, DigestSignInit) {
378 bssl::UniquePtr<EVP_PKEY> pkey = LoadExampleRSAKey();
379 ASSERT_TRUE(pkey);
380 bssl::ScopedEVP_MD_CTX md_ctx;
381 ASSERT_TRUE(
382 EVP_DigestSignInit(md_ctx.get(), NULL, EVP_sha256(), NULL, pkey.get()));
383 ASSERT_TRUE(EVP_DigestSignUpdate(md_ctx.get(), kMsg, sizeof(kMsg)));
384
385 // Determine the size of the signature.
386 size_t sig_len = 0;
387 ASSERT_TRUE(EVP_DigestSignFinal(md_ctx.get(), NULL, &sig_len));
388
389 // Sanity check for testing.
390 EXPECT_EQ(static_cast<size_t>(EVP_PKEY_size(pkey.get())), sig_len);
391
392 std::vector<uint8_t> sig;
393 sig.resize(sig_len);
394 ASSERT_TRUE(EVP_DigestSignFinal(md_ctx.get(), sig.data(), &sig_len));
395 sig.resize(sig_len);
396
397 // Ensure that the signature round-trips.
398 md_ctx.Reset();
399 ASSERT_TRUE(
400 EVP_DigestVerifyInit(md_ctx.get(), NULL, EVP_sha256(), NULL, pkey.get()));
401 ASSERT_TRUE(EVP_DigestVerifyUpdate(md_ctx.get(), kMsg, sizeof(kMsg)));
402 ASSERT_TRUE(EVP_DigestVerifyFinal(md_ctx.get(), sig.data(), sig_len));
403 }
404
TEST(EVPExtraTest,DigestVerifyInit)405 TEST(EVPExtraTest, DigestVerifyInit) {
406 bssl::UniquePtr<EVP_PKEY> pkey = LoadExampleRSAKey();
407 bssl::ScopedEVP_MD_CTX md_ctx;
408 ASSERT_TRUE(pkey);
409 ASSERT_TRUE(
410 EVP_DigestVerifyInit(md_ctx.get(), NULL, EVP_sha256(), NULL, pkey.get()));
411 ASSERT_TRUE(EVP_DigestVerifyUpdate(md_ctx.get(), kMsg, sizeof(kMsg)));
412 ASSERT_TRUE(
413 EVP_DigestVerifyFinal(md_ctx.get(), kSignature, sizeof(kSignature)));
414 }
415
TEST(EVPExtraTest,VerifyRecover)416 TEST(EVPExtraTest, VerifyRecover) {
417 bssl::UniquePtr<EVP_PKEY> pkey = LoadExampleRSAKey();
418 ASSERT_TRUE(pkey);
419 bssl::UniquePtr<RSA> rsa(EVP_PKEY_get1_RSA(pkey.get()));
420 ASSERT_TRUE(rsa);
421
422 const uint8_t kDummyHash[32] = {0};
423 uint8_t sig[2048/8];
424 unsigned sig_len = sizeof(sig);
425 ASSERT_TRUE(RSA_sign(NID_sha256, kDummyHash, sizeof(kDummyHash), sig,
426 &sig_len, rsa.get()));
427
428 size_t out_len;
429 bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(pkey.get(), nullptr));
430 ASSERT_TRUE(EVP_PKEY_verify_recover_init(ctx.get()));
431 ASSERT_TRUE(EVP_PKEY_CTX_set_rsa_padding(ctx.get(), RSA_PKCS1_PADDING));
432 ASSERT_TRUE(EVP_PKEY_CTX_set_signature_md(ctx.get(), EVP_sha256()));
433 ASSERT_TRUE(
434 EVP_PKEY_verify_recover(ctx.get(), nullptr, &out_len, sig, sig_len));
435
436 std::vector<uint8_t> recovered;
437 recovered.resize(out_len);
438
439 ASSERT_TRUE(EVP_PKEY_verify_recover(ctx.get(), recovered.data(), &out_len,
440 sig, sig_len));
441 EXPECT_EQ(Bytes(kDummyHash), Bytes(recovered.data(), out_len));
442
443 out_len = recovered.size();
444 ASSERT_TRUE(EVP_PKEY_CTX_set_signature_md(ctx.get(), nullptr));
445 ASSERT_TRUE(EVP_PKEY_verify_recover(ctx.get(), recovered.data(), &out_len,
446 sig, sig_len));
447
448 // The size of a SHA-256 hash plus PKCS#1 v1.5 ASN.1 stuff happens to be 51
449 // bytes.
450 EXPECT_EQ(51u, out_len);
451 }
452
TestValidPrivateKey(const uint8_t * input,size_t input_len,int expected_id)453 static void TestValidPrivateKey(const uint8_t *input, size_t input_len,
454 int expected_id) {
455 const uint8_t *p = input;
456 bssl::UniquePtr<EVP_PKEY> pkey(d2i_AutoPrivateKey(NULL, &p, input_len));
457 ASSERT_TRUE(pkey);
458 EXPECT_EQ(input + input_len, p);
459 EXPECT_EQ(expected_id, EVP_PKEY_id(pkey.get()));
460 }
461
TEST(EVPExtraTest,d2i_AutoPrivateKey)462 TEST(EVPExtraTest, d2i_AutoPrivateKey) {
463 TestValidPrivateKey(kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER),
464 EVP_PKEY_RSA);
465 TestValidPrivateKey(kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8),
466 EVP_PKEY_RSA);
467 TestValidPrivateKey(kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC);
468 TestValidPrivateKey(kExampleECKeyPKCS8, sizeof(kExampleECKeyPKCS8),
469 EVP_PKEY_EC);
470 TestValidPrivateKey(kExampleECKeySpecifiedCurvePKCS8,
471 sizeof(kExampleECKeySpecifiedCurvePKCS8), EVP_PKEY_EC);
472 TestValidPrivateKey(kExampleDSAKeyDER, sizeof(kExampleDSAKeyDER),
473 EVP_PKEY_DSA);
474
475 const uint8_t *p = kInvalidPrivateKey;
476 bssl::UniquePtr<EVP_PKEY> pkey(
477 d2i_AutoPrivateKey(NULL, &p, sizeof(kInvalidPrivateKey)));
478 EXPECT_FALSE(pkey) << "Parsed invalid private key";
479 ERR_clear_error();
480 }
481
ParsePrivateKey(int type,const uint8_t * in,size_t len)482 static bssl::UniquePtr<EVP_PKEY> ParsePrivateKey(int type, const uint8_t *in,
483 size_t len) {
484 const uint8_t *ptr = in;
485 bssl::UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(type, nullptr, &ptr, len));
486 if (!pkey) {
487 return nullptr;
488 }
489
490 EXPECT_EQ(in + len, ptr);
491 return pkey;
492 }
493
PrintToString(const EVP_PKEY * pkey,int indent,int (* print_func)(BIO * out,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx))494 static std::string PrintToString(const EVP_PKEY *pkey, int indent,
495 int (*print_func)(BIO *out,
496 const EVP_PKEY *pkey,
497 int indent,
498 ASN1_PCTX *pctx)) {
499 bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
500 const uint8_t *data;
501 size_t len;
502 if (!bio || !print_func(bio.get(), pkey, indent, nullptr) ||
503 !BIO_mem_contents(bio.get(), &data, &len)) {
504 ADD_FAILURE() << "Error printing.";
505 return "";
506 }
507 return std::string(data, data + len);
508 }
509
TEST(EVPExtraTest,Print)510 TEST(EVPExtraTest, Print) {
511 bssl::UniquePtr<EVP_PKEY> rsa = ParsePrivateKey(
512 EVP_PKEY_RSA, kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER));
513 ASSERT_TRUE(rsa);
514 EXPECT_EQ(PrintToString(rsa.get(), /*indent=*/2, &EVP_PKEY_print_params),
515 " Parameters algorithm unsupported\n");
516 EXPECT_EQ(PrintToString(rsa.get(), /*indent=*/2, &EVP_PKEY_print_public),
517 R"( Public-Key: (1024 bit)
518 Modulus:
519 00:f8:b8:6c:83:b4:bc:d9:a8:57:c0:a5:b4:59:76:
520 8c:54:1d:79:eb:22:52:04:7e:d3:37:eb:41:fd:83:
521 f9:f0:a6:85:15:34:75:71:5a:84:a8:3c:d2:ef:5a:
522 4e:d3:de:97:8a:dd:ff:bb:cf:0a:aa:86:92:be:b8:
523 50:e4:cd:6f:80:33:30:76:13:8f:ca:7b:dc:ec:5a:
524 ca:63:c7:03:25:ef:a8:8a:83:58:76:20:fa:16:77:
525 d7:79:92:63:01:48:1a:d8:7b:67:f1:52:55:49:4e:
526 d6:6e:4a:5c:d7:7a:37:36:0c:de:dd:8f:44:e8:c2:
527 a7:2c:2b:b5:af:64:4b:61:07
528 Exponent: 65537 (0x10001)
529 )");
530 EXPECT_EQ(PrintToString(rsa.get(), /*indent=*/2, &EVP_PKEY_print_private),
531 R"( Private-Key: (1024 bit)
532 modulus:
533 00:f8:b8:6c:83:b4:bc:d9:a8:57:c0:a5:b4:59:76:
534 8c:54:1d:79:eb:22:52:04:7e:d3:37:eb:41:fd:83:
535 f9:f0:a6:85:15:34:75:71:5a:84:a8:3c:d2:ef:5a:
536 4e:d3:de:97:8a:dd:ff:bb:cf:0a:aa:86:92:be:b8:
537 50:e4:cd:6f:80:33:30:76:13:8f:ca:7b:dc:ec:5a:
538 ca:63:c7:03:25:ef:a8:8a:83:58:76:20:fa:16:77:
539 d7:79:92:63:01:48:1a:d8:7b:67:f1:52:55:49:4e:
540 d6:6e:4a:5c:d7:7a:37:36:0c:de:dd:8f:44:e8:c2:
541 a7:2c:2b:b5:af:64:4b:61:07
542 publicExponent: 65537 (0x10001)
543 privateExponent:
544 74:88:64:3f:69:45:3a:6d:c7:7f:b9:a3:c0:6e:ec:
545 dc:d4:5a:b5:32:85:5f:19:d4:f8:d4:3f:3c:fa:c2:
546 f6:5f:ee:e6:ba:87:74:2e:c7:0c:d4:42:b8:66:85:
547 9c:7b:24:61:aa:16:11:f6:b5:b6:a4:0a:c9:55:2e:
548 81:a5:47:61:cb:25:8f:c2:15:7b:0e:7c:36:9f:3a:
549 da:58:86:1c:5b:83:79:e6:2b:cc:e6:fa:2c:61:f2:
550 78:80:1b:e2:f3:9d:39:2b:65:57:91:3d:71:99:73:
551 a5:c2:79:20:8c:07:4f:e5:b4:60:1f:99:a2:b1:4f:
552 0c:ef:bc:59:53:00:7d:b1
553 prime1:
554 00:fc:7e:23:65:70:f8:ce:d3:40:41:80:6a:1d:01:
555 d6:01:ff:b6:1b:3d:3d:59:09:33:79:c0:4f:de:96:
556 27:4b:18:c6:d9:78:f1:f4:35:46:e9:7c:42:7a:5d:
557 9f:ef:54:b8:f7:9f:c4:33:6c:f3:8c:32:46:87:67:
558 30:7b:a7:ac:e3
559 prime2:
560 00:fc:2c:df:0c:0d:88:f5:b1:92:a8:93:47:63:55:
561 f5:ca:58:43:ba:1c:e5:9e:b6:95:05:cd:b5:82:df:
562 eb:04:53:9d:bd:c2:38:16:b3:62:dd:a1:46:db:6d:
563 97:93:9f:8a:c3:9b:64:7e:42:e3:32:57:19:1b:d5:
564 6e:85:fa:b8:8d
565 exponent1:
566 00:bc:3d:de:6d:d6:97:e8:ba:9e:81:37:17:e5:a0:
567 64:c9:00:b7:e7:fe:f4:29:d9:2e:43:6b:19:20:bd:
568 99:75:e7:76:f8:d3:ae:af:7e:b8:eb:81:f4:9d:fe:
569 07:2b:0b:63:0b:5a:55:90:71:7d:f1:db:d9:b1:41:
570 41:68:2f:4e:39
571 exponent2:
572 5a:34:66:d8:f5:e2:7f:18:b5:00:6e:26:84:27:14:
573 93:fb:fc:c6:0f:5e:27:e6:e1:e9:c0:8a:e4:34:da:
574 e9:a2:4b:73:bc:8c:b9:ba:13:6c:7a:2b:51:84:a3:
575 4a:e0:30:10:06:7e:ed:17:5a:14:00:c9:ef:85:ea:
576 52:2c:bc:65
577 coefficient:
578 51:e3:f2:83:19:9b:c4:1e:2f:50:3d:df:5a:a2:18:
579 ca:5f:2e:49:af:6f:cc:fa:65:77:94:b5:a1:0a:a9:
580 d1:8a:39:37:f4:0b:a0:d7:82:27:5e:ae:17:17:a1:
581 1e:54:34:bf:6e:c4:8e:99:5d:08:f1:2d:86:9d:a5:
582 20:1b:e5:df
583 )");
584
585 bssl::UniquePtr<EVP_PKEY> dsa = ParsePrivateKey(
586 EVP_PKEY_DSA, kExampleDSAKeyDER, sizeof(kExampleDSAKeyDER));
587 ASSERT_TRUE(dsa);
588 EXPECT_EQ(PrintToString(dsa.get(), /*indent=*/2, &EVP_PKEY_print_params),
589 R"( DSA-Parameters: (2048 bit)
590 P:
591 00:9e:12:fa:b3:de:12:21:35:01:dd:82:aa:10:ca:
592 2d:10:1d:2d:4e:bf:ef:4d:2a:3f:8d:aa:0f:e0:ce:
593 da:d8:d6:af:85:61:6a:a2:f3:25:2c:0a:2b:5a:6d:
594 b0:9e:6f:14:90:0e:0d:db:83:11:87:6d:d8:f9:66:
595 95:25:f9:9e:d6:59:49:e1:84:d5:06:47:93:27:11:
596 69:a2:28:68:0b:95:ec:12:f5:9a:8e:20:b2:1f:2b:
597 58:eb:2a:20:12:d3:5b:de:2e:e3:51:82:2f:e8:f3:
598 2d:0a:33:05:65:dc:ce:5c:67:2b:72:59:c1:4b:24:
599 33:d0:b5:b2:ca:2b:2d:b0:ab:62:6e:8f:13:f4:7f:
600 e0:34:5d:90:4e:72:94:bb:03:8e:9c:e2:1a:9e:58:
601 0b:83:35:62:78:70:6c:fe:76:84:36:c6:9d:e1:49:
602 cc:ff:98:b4:aa:b8:cb:4f:63:85:c9:f1:02:ce:59:
603 34:6e:ae:ef:27:e0:ad:22:2d:53:d6:e8:9c:c8:cd:
604 e5:77:6d:d0:00:57:b0:3f:2d:88:ab:3c:ed:ba:fd:
605 7b:58:5f:0b:7f:78:35:e1:7a:37:28:bb:f2:5e:a6:
606 25:72:f2:45:dc:11:1f:3c:e3:9c:b6:ff:ac:c3:1b:
607 0a:27:90:e7:bd:e9:02:24:ea:9b:09:31:53:62:af:
608 3d:2b
609 Q:
610 00:f3:81:dc:f5:3e:bf:72:4f:8b:2e:5c:a8:2c:01:
611 0f:b4:b5:ed:a9:35:8d:0f:d8:8e:d2:78:58:94:88:
612 b5:4f:c3
613 G:
614 0c:40:2a:72:5d:cc:3a:62:e0:2b:f4:cf:43:cd:17:
615 f4:a4:93:59:12:20:22:36:69:cf:41:93:ed:ab:42:
616 3a:d0:8d:fb:55:2e:30:8a:6a:57:a5:ff:bc:7c:d0:
617 fb:20:87:f8:1f:8d:f0:cb:08:ab:21:33:28:7d:2b:
618 69:68:71:4a:94:f6:33:c9:40:84:5a:48:a3:e1:67:
619 08:dd:e7:61:cc:6a:8e:ab:2d:84:db:21:b6:ea:5b:
620 07:68:14:93:cc:9c:31:fb:c3:68:b2:43:f6:dd:f8:
621 c9:32:a8:b4:03:8f:44:e7:b1:5c:a8:76:34:4a:14:
622 78:59:f2:b4:3b:39:45:86:68:ad:5e:0a:1a:9a:66:
623 95:46:dd:28:12:e3:b3:61:7a:0a:ef:99:d5:8e:3b:
624 b4:cc:87:fd:94:22:5e:01:d2:dc:c4:69:a7:72:68:
625 14:6c:51:91:8f:18:e8:b4:d7:0a:a1:f0:c7:62:3b:
626 cc:52:cf:37:31:d3:86:41:b2:d2:83:0b:7e:ec:b2:
627 f0:95:52:ff:13:7d:04:6e:49:4e:7f:33:c3:59:00:
628 02:b1:6d:1b:97:d9:36:fd:a2:8f:90:c3:ed:3c:a3:
629 53:38:16:8a:c1:6f:77:c3:c5:7a:dc:2e:8f:7c:6c:
630 22:56:e4:1a:5f:65:45:05:90:db:b5:bc:f0:6d:66:
631 61
632 )");
633 EXPECT_EQ(PrintToString(dsa.get(), /*indent=*/2, &EVP_PKEY_print_public),
634 R"( Public-Key: (2048 bit)
635 pub:
636 31:97:31:a1:4e:38:56:88:db:94:1d:bf:65:5c:da:
637 4b:c2:10:de:74:20:03:ce:13:60:f2:25:1d:55:7c:
638 5d:94:82:54:08:53:db:85:95:bf:dd:5e:50:d5:96:
639 e0:79:51:1b:bf:4d:4e:b9:3a:c5:ee:c4:5e:98:75:
640 7b:be:ff:30:e6:d0:7b:a6:f1:bc:29:ea:df:ec:f3:
641 8b:fa:83:11:9f:3f:f0:5d:06:51:32:aa:21:fc:26:
642 17:e7:50:c2:16:ba:fa:54:b7:7e:1d:2c:a6:a3:41:
643 66:33:94:83:b9:bf:a0:4f:bd:a6:fd:2c:81:58:35:
644 33:39:c0:6d:33:40:56:64:12:5a:cd:35:53:21:78:
645 8f:27:24:37:66:8a:df:5e:5f:63:fc:8b:2d:ef:57:
646 db:40:25:d5:17:53:0b:e4:a5:ae:54:bf:46:4f:a6:
647 79:c3:74:fa:1f:85:34:64:6d:c5:03:eb:72:98:80:
648 7b:c0:8f:35:11:a7:09:eb:51:e0:b0:ac:92:14:f2:
649 ad:37:95:5a:ba:8c:c4:db:ed:c4:4e:8b:8f:84:33:
650 64:f8:57:12:d7:08:7e:90:66:df:91:50:23:f2:73:
651 c0:6b:b1:15:dd:64:d7:c9:75:17:73:72:da:33:c4:
652 6f:a5:47:a1:cc:d1:c6:62:e5:ca:ab:5f:2a:8f:6b:
653 cc
654 P:
655 00:9e:12:fa:b3:de:12:21:35:01:dd:82:aa:10:ca:
656 2d:10:1d:2d:4e:bf:ef:4d:2a:3f:8d:aa:0f:e0:ce:
657 da:d8:d6:af:85:61:6a:a2:f3:25:2c:0a:2b:5a:6d:
658 b0:9e:6f:14:90:0e:0d:db:83:11:87:6d:d8:f9:66:
659 95:25:f9:9e:d6:59:49:e1:84:d5:06:47:93:27:11:
660 69:a2:28:68:0b:95:ec:12:f5:9a:8e:20:b2:1f:2b:
661 58:eb:2a:20:12:d3:5b:de:2e:e3:51:82:2f:e8:f3:
662 2d:0a:33:05:65:dc:ce:5c:67:2b:72:59:c1:4b:24:
663 33:d0:b5:b2:ca:2b:2d:b0:ab:62:6e:8f:13:f4:7f:
664 e0:34:5d:90:4e:72:94:bb:03:8e:9c:e2:1a:9e:58:
665 0b:83:35:62:78:70:6c:fe:76:84:36:c6:9d:e1:49:
666 cc:ff:98:b4:aa:b8:cb:4f:63:85:c9:f1:02:ce:59:
667 34:6e:ae:ef:27:e0:ad:22:2d:53:d6:e8:9c:c8:cd:
668 e5:77:6d:d0:00:57:b0:3f:2d:88:ab:3c:ed:ba:fd:
669 7b:58:5f:0b:7f:78:35:e1:7a:37:28:bb:f2:5e:a6:
670 25:72:f2:45:dc:11:1f:3c:e3:9c:b6:ff:ac:c3:1b:
671 0a:27:90:e7:bd:e9:02:24:ea:9b:09:31:53:62:af:
672 3d:2b
673 Q:
674 00:f3:81:dc:f5:3e:bf:72:4f:8b:2e:5c:a8:2c:01:
675 0f:b4:b5:ed:a9:35:8d:0f:d8:8e:d2:78:58:94:88:
676 b5:4f:c3
677 G:
678 0c:40:2a:72:5d:cc:3a:62:e0:2b:f4:cf:43:cd:17:
679 f4:a4:93:59:12:20:22:36:69:cf:41:93:ed:ab:42:
680 3a:d0:8d:fb:55:2e:30:8a:6a:57:a5:ff:bc:7c:d0:
681 fb:20:87:f8:1f:8d:f0:cb:08:ab:21:33:28:7d:2b:
682 69:68:71:4a:94:f6:33:c9:40:84:5a:48:a3:e1:67:
683 08:dd:e7:61:cc:6a:8e:ab:2d:84:db:21:b6:ea:5b:
684 07:68:14:93:cc:9c:31:fb:c3:68:b2:43:f6:dd:f8:
685 c9:32:a8:b4:03:8f:44:e7:b1:5c:a8:76:34:4a:14:
686 78:59:f2:b4:3b:39:45:86:68:ad:5e:0a:1a:9a:66:
687 95:46:dd:28:12:e3:b3:61:7a:0a:ef:99:d5:8e:3b:
688 b4:cc:87:fd:94:22:5e:01:d2:dc:c4:69:a7:72:68:
689 14:6c:51:91:8f:18:e8:b4:d7:0a:a1:f0:c7:62:3b:
690 cc:52:cf:37:31:d3:86:41:b2:d2:83:0b:7e:ec:b2:
691 f0:95:52:ff:13:7d:04:6e:49:4e:7f:33:c3:59:00:
692 02:b1:6d:1b:97:d9:36:fd:a2:8f:90:c3:ed:3c:a3:
693 53:38:16:8a:c1:6f:77:c3:c5:7a:dc:2e:8f:7c:6c:
694 22:56:e4:1a:5f:65:45:05:90:db:b5:bc:f0:6d:66:
695 61
696 )");
697 EXPECT_EQ(PrintToString(dsa.get(), /*indent=*/2, &EVP_PKEY_print_private),
698 R"( Private-Key: (2048 bit)
699 priv:
700 00:b0:c7:68:70:27:43:bc:51:24:29:93:a9:71:a5:
701 28:89:79:54:44:f7:c6:45:22:03:d0:ce:84:fe:61:
702 17:d4:6e
703 pub:
704 31:97:31:a1:4e:38:56:88:db:94:1d:bf:65:5c:da:
705 4b:c2:10:de:74:20:03:ce:13:60:f2:25:1d:55:7c:
706 5d:94:82:54:08:53:db:85:95:bf:dd:5e:50:d5:96:
707 e0:79:51:1b:bf:4d:4e:b9:3a:c5:ee:c4:5e:98:75:
708 7b:be:ff:30:e6:d0:7b:a6:f1:bc:29:ea:df:ec:f3:
709 8b:fa:83:11:9f:3f:f0:5d:06:51:32:aa:21:fc:26:
710 17:e7:50:c2:16:ba:fa:54:b7:7e:1d:2c:a6:a3:41:
711 66:33:94:83:b9:bf:a0:4f:bd:a6:fd:2c:81:58:35:
712 33:39:c0:6d:33:40:56:64:12:5a:cd:35:53:21:78:
713 8f:27:24:37:66:8a:df:5e:5f:63:fc:8b:2d:ef:57:
714 db:40:25:d5:17:53:0b:e4:a5:ae:54:bf:46:4f:a6:
715 79:c3:74:fa:1f:85:34:64:6d:c5:03:eb:72:98:80:
716 7b:c0:8f:35:11:a7:09:eb:51:e0:b0:ac:92:14:f2:
717 ad:37:95:5a:ba:8c:c4:db:ed:c4:4e:8b:8f:84:33:
718 64:f8:57:12:d7:08:7e:90:66:df:91:50:23:f2:73:
719 c0:6b:b1:15:dd:64:d7:c9:75:17:73:72:da:33:c4:
720 6f:a5:47:a1:cc:d1:c6:62:e5:ca:ab:5f:2a:8f:6b:
721 cc
722 P:
723 00:9e:12:fa:b3:de:12:21:35:01:dd:82:aa:10:ca:
724 2d:10:1d:2d:4e:bf:ef:4d:2a:3f:8d:aa:0f:e0:ce:
725 da:d8:d6:af:85:61:6a:a2:f3:25:2c:0a:2b:5a:6d:
726 b0:9e:6f:14:90:0e:0d:db:83:11:87:6d:d8:f9:66:
727 95:25:f9:9e:d6:59:49:e1:84:d5:06:47:93:27:11:
728 69:a2:28:68:0b:95:ec:12:f5:9a:8e:20:b2:1f:2b:
729 58:eb:2a:20:12:d3:5b:de:2e:e3:51:82:2f:e8:f3:
730 2d:0a:33:05:65:dc:ce:5c:67:2b:72:59:c1:4b:24:
731 33:d0:b5:b2:ca:2b:2d:b0:ab:62:6e:8f:13:f4:7f:
732 e0:34:5d:90:4e:72:94:bb:03:8e:9c:e2:1a:9e:58:
733 0b:83:35:62:78:70:6c:fe:76:84:36:c6:9d:e1:49:
734 cc:ff:98:b4:aa:b8:cb:4f:63:85:c9:f1:02:ce:59:
735 34:6e:ae:ef:27:e0:ad:22:2d:53:d6:e8:9c:c8:cd:
736 e5:77:6d:d0:00:57:b0:3f:2d:88:ab:3c:ed:ba:fd:
737 7b:58:5f:0b:7f:78:35:e1:7a:37:28:bb:f2:5e:a6:
738 25:72:f2:45:dc:11:1f:3c:e3:9c:b6:ff:ac:c3:1b:
739 0a:27:90:e7:bd:e9:02:24:ea:9b:09:31:53:62:af:
740 3d:2b
741 Q:
742 00:f3:81:dc:f5:3e:bf:72:4f:8b:2e:5c:a8:2c:01:
743 0f:b4:b5:ed:a9:35:8d:0f:d8:8e:d2:78:58:94:88:
744 b5:4f:c3
745 G:
746 0c:40:2a:72:5d:cc:3a:62:e0:2b:f4:cf:43:cd:17:
747 f4:a4:93:59:12:20:22:36:69:cf:41:93:ed:ab:42:
748 3a:d0:8d:fb:55:2e:30:8a:6a:57:a5:ff:bc:7c:d0:
749 fb:20:87:f8:1f:8d:f0:cb:08:ab:21:33:28:7d:2b:
750 69:68:71:4a:94:f6:33:c9:40:84:5a:48:a3:e1:67:
751 08:dd:e7:61:cc:6a:8e:ab:2d:84:db:21:b6:ea:5b:
752 07:68:14:93:cc:9c:31:fb:c3:68:b2:43:f6:dd:f8:
753 c9:32:a8:b4:03:8f:44:e7:b1:5c:a8:76:34:4a:14:
754 78:59:f2:b4:3b:39:45:86:68:ad:5e:0a:1a:9a:66:
755 95:46:dd:28:12:e3:b3:61:7a:0a:ef:99:d5:8e:3b:
756 b4:cc:87:fd:94:22:5e:01:d2:dc:c4:69:a7:72:68:
757 14:6c:51:91:8f:18:e8:b4:d7:0a:a1:f0:c7:62:3b:
758 cc:52:cf:37:31:d3:86:41:b2:d2:83:0b:7e:ec:b2:
759 f0:95:52:ff:13:7d:04:6e:49:4e:7f:33:c3:59:00:
760 02:b1:6d:1b:97:d9:36:fd:a2:8f:90:c3:ed:3c:a3:
761 53:38:16:8a:c1:6f:77:c3:c5:7a:dc:2e:8f:7c:6c:
762 22:56:e4:1a:5f:65:45:05:90:db:b5:bc:f0:6d:66:
763 61
764 )");
765
766 bssl::UniquePtr<EVP_PKEY> ec =
767 ParsePrivateKey(EVP_PKEY_EC, kExampleECKeyDER, sizeof(kExampleECKeyDER));
768 ASSERT_TRUE(ec);
769 EXPECT_EQ(PrintToString(ec.get(), /*indent=*/2, &EVP_PKEY_print_params),
770 " ECDSA-Parameters: (P-256)\n");
771 EXPECT_EQ(PrintToString(ec.get(), /*indent=*/2, &EVP_PKEY_print_public),
772 R"( Public-Key: (P-256)
773 pub:
774 04:e6:2b:69:e2:bf:65:9f:97:be:2f:1e:0d:94:8a:
775 4c:d5:97:6b:b7:a9:1e:0d:46:fb:dd:a9:a9:1e:9d:
776 dc:ba:5a:01:e7:d6:97:a8:0a:18:f9:c3:c4:a3:1e:
777 56:e2:7c:83:48:db:16:1a:1c:f5:1d:7e:f1:94:2d:
778 4b:cf:72:22:c1
779 )");
780 EXPECT_EQ(PrintToString(ec.get(), /*indent=*/2, &EVP_PKEY_print_private),
781 R"( Private-Key: (P-256)
782 priv:
783 07:0f:08:72:7a:d4:a0:4a:9c:dd:59:c9:4d:89:68:
784 77:08:b5:6f:c9:5d:30:77:0e:e8:d1:c9:ce:0a:8b:
785 b4:6a
786 pub:
787 04:e6:2b:69:e2:bf:65:9f:97:be:2f:1e:0d:94:8a:
788 4c:d5:97:6b:b7:a9:1e:0d:46:fb:dd:a9:a9:1e:9d:
789 dc:ba:5a:01:e7:d6:97:a8:0a:18:f9:c3:c4:a3:1e:
790 56:e2:7c:83:48:db:16:1a:1c:f5:1d:7e:f1:94:2d:
791 4b:cf:72:22:c1
792 )");
793 }
794
795 // Tests loading a bad key in PKCS8 format.
TEST(EVPExtraTest,BadECKey)796 TEST(EVPExtraTest, BadECKey) {
797 const uint8_t *derp = kExampleBadECKeyDER;
798 bssl::UniquePtr<PKCS8_PRIV_KEY_INFO> p8inf(
799 d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp, sizeof(kExampleBadECKeyDER)));
800 ASSERT_TRUE(p8inf);
801 EXPECT_EQ(kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER), derp);
802
803 bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKCS82PKEY(p8inf.get()));
804 ASSERT_FALSE(pkey) << "Imported invalid EC key";
805 ERR_clear_error();
806 }
807
808 // Tests |EVP_marshal_public_key| on an empty key.
TEST(EVPExtraTest,MarshalEmptyPublicKey)809 TEST(EVPExtraTest, MarshalEmptyPublicKey) {
810 bssl::UniquePtr<EVP_PKEY> empty(EVP_PKEY_new());
811 ASSERT_TRUE(empty);
812
813 bssl::ScopedCBB cbb;
814 EXPECT_FALSE(EVP_marshal_public_key(cbb.get(), empty.get()))
815 << "Marshalled empty public key.";
816 EXPECT_TRUE(ErrorEquals(ERR_peek_last_error(), ERR_LIB_EVP,
817 EVP_R_UNSUPPORTED_ALGORITHM));
818 }
819
TEST(EVPExtraTest,d2i_PrivateKey)820 TEST(EVPExtraTest, d2i_PrivateKey) {
821 EXPECT_TRUE(ParsePrivateKey(EVP_PKEY_RSA, kExampleRSAKeyDER,
822 sizeof(kExampleRSAKeyDER)));
823 EXPECT_TRUE(ParsePrivateKey(EVP_PKEY_DSA, kExampleDSAKeyDER,
824 sizeof(kExampleDSAKeyDER)));
825 EXPECT_TRUE(ParsePrivateKey(EVP_PKEY_RSA, kExampleRSAKeyPKCS8,
826 sizeof(kExampleRSAKeyPKCS8)));
827 EXPECT_TRUE(
828 ParsePrivateKey(EVP_PKEY_EC, kExampleECKeyDER, sizeof(kExampleECKeyDER)));
829
830 EXPECT_FALSE(ParsePrivateKey(EVP_PKEY_EC, kExampleBadECKeyDER,
831 sizeof(kExampleBadECKeyDER)));
832 ERR_clear_error();
833
834 // Copy the input into a |malloc|'d vector to flag memory errors.
835 std::vector<uint8_t> copy(
836 kExampleBadECKeyDER2,
837 kExampleBadECKeyDER2 + sizeof(kExampleBadECKeyDER2));
838 EXPECT_FALSE(ParsePrivateKey(EVP_PKEY_EC, copy.data(), copy.size()));
839 ERR_clear_error();
840
841 // Test that an RSA key may not be imported as an EC key.
842 EXPECT_FALSE(ParsePrivateKey(EVP_PKEY_EC, kExampleRSAKeyPKCS8,
843 sizeof(kExampleRSAKeyPKCS8)));
844 ERR_clear_error();
845 }
846
TEST(EVPExtraTest,Ed25519)847 TEST(EVPExtraTest, Ed25519) {
848 static const uint8_t kPublicKey[32] = {
849 0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b, 0xfe,
850 0xd3, 0xc9, 0x64, 0x07, 0x3a, 0x0e, 0xe1, 0x72, 0xf3, 0xda, 0xa6,
851 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, 0x51, 0x1a,
852 };
853
854 static const uint8_t kPublicKeySPKI[] = {
855 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21,
856 0x00, 0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b,
857 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a, 0x0e, 0xe1, 0x72, 0xf3, 0xda,
858 0xa6, 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, 0x51, 0x1a,
859 };
860
861 static const uint8_t kPrivateKeySeed[32] = {
862 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, 0x4a,
863 0xf4, 0x92, 0xec, 0x2c, 0xc4, 0x44, 0x49, 0xc5, 0x69, 0x7b, 0x32,
864 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, 0x7f, 0x60,
865 };
866
867 static const uint8_t kPrivateKeyPKCS8[] = {
868 0x30, 0x2e, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70,
869 0x04, 0x22, 0x04, 0x20, 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60,
870 0xba, 0x84, 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4, 0x44, 0x49, 0xc5, 0x69,
871 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, 0x7f, 0x60,
872 };
873
874 // Create a public key.
875 bssl::UniquePtr<EVP_PKEY> pubkey(EVP_PKEY_new_raw_public_key(
876 EVP_PKEY_ED25519, nullptr, kPublicKey, sizeof(kPublicKey)));
877 ASSERT_TRUE(pubkey);
878 EXPECT_EQ(EVP_PKEY_ED25519, EVP_PKEY_id(pubkey.get()));
879
880 // The public key must be extractable.
881 uint8_t buf[32];
882 size_t len;
883 ASSERT_TRUE(EVP_PKEY_get_raw_public_key(pubkey.get(), nullptr, &len));
884 EXPECT_EQ(len, 32u);
885 ASSERT_TRUE(EVP_PKEY_get_raw_public_key(pubkey.get(), buf, &len));
886 EXPECT_EQ(Bytes(buf, len), Bytes(kPublicKey));
887 // Passing too large of a buffer is okay. The function will still only read
888 // 32 bytes.
889 len = 64;
890 ASSERT_TRUE(EVP_PKEY_get_raw_public_key(pubkey.get(), buf, &len));
891 EXPECT_EQ(Bytes(buf, len), Bytes(kPublicKey));
892 // Passing too small of a buffer is noticed.
893 len = 31;
894 EXPECT_FALSE(EVP_PKEY_get_raw_public_key(pubkey.get(), buf, &len));
895 EXPECT_TRUE(
896 ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL));
897 ERR_clear_error();
898
899 // There is no private key.
900 EXPECT_FALSE(EVP_PKEY_get_raw_private_key(pubkey.get(), nullptr, &len));
901 EXPECT_TRUE(
902 ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_NOT_A_PRIVATE_KEY));
903 ERR_clear_error();
904
905 // The public key must encode properly.
906 bssl::ScopedCBB cbb;
907 uint8_t *der;
908 size_t der_len;
909 ASSERT_TRUE(CBB_init(cbb.get(), 0));
910 ASSERT_TRUE(EVP_marshal_public_key(cbb.get(), pubkey.get()));
911 ASSERT_TRUE(CBB_finish(cbb.get(), &der, &der_len));
912 bssl::UniquePtr<uint8_t> free_der(der);
913 EXPECT_EQ(Bytes(kPublicKeySPKI), Bytes(der, der_len));
914
915 // The public key must gracefully fail to encode as a private key.
916 ASSERT_TRUE(CBB_init(cbb.get(), 0));
917 EXPECT_FALSE(EVP_marshal_private_key(cbb.get(), pubkey.get()));
918 EXPECT_TRUE(
919 ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_NOT_A_PRIVATE_KEY));
920 ERR_clear_error();
921 cbb.Reset();
922
923 // Create a private key.
924 bssl::UniquePtr<EVP_PKEY> privkey(EVP_PKEY_new_raw_private_key(
925 EVP_PKEY_ED25519, NULL, kPrivateKeySeed, sizeof(kPrivateKeySeed)));
926 ASSERT_TRUE(privkey);
927 EXPECT_EQ(EVP_PKEY_ED25519, EVP_PKEY_id(privkey.get()));
928
929 // The private key must be extractable.
930 ASSERT_TRUE(EVP_PKEY_get_raw_private_key(privkey.get(), nullptr, &len));
931 EXPECT_EQ(len, 32u);
932 ASSERT_TRUE(EVP_PKEY_get_raw_private_key(privkey.get(), buf, &len));
933 EXPECT_EQ(Bytes(buf, len), Bytes(kPrivateKeySeed));
934 // Passing too large of a buffer is okay. The function will still only read
935 // 32 bytes.
936 len = 64;
937 ASSERT_TRUE(EVP_PKEY_get_raw_private_key(privkey.get(), buf, &len));
938 EXPECT_EQ(Bytes(buf, len), Bytes(kPrivateKeySeed));
939 // Passing too small of a buffer is noticed.
940 len = 31;
941 EXPECT_FALSE(EVP_PKEY_get_raw_private_key(privkey.get(), buf, &len));
942 EXPECT_TRUE(
943 ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL));
944 ERR_clear_error();
945 // The public key must be extractable.
946 len = 32;
947 ASSERT_TRUE(EVP_PKEY_get_raw_public_key(privkey.get(), buf, &len));
948 EXPECT_EQ(Bytes(buf, len), Bytes(kPublicKey));
949
950 // The public key must encode from the private key.
951 ASSERT_TRUE(CBB_init(cbb.get(), 0));
952 ASSERT_TRUE(EVP_marshal_public_key(cbb.get(), privkey.get()));
953 ASSERT_TRUE(CBB_finish(cbb.get(), &der, &der_len));
954 free_der.reset(der);
955 EXPECT_EQ(Bytes(kPublicKeySPKI), Bytes(der, der_len));
956
957 // The private key must encode properly.
958 ASSERT_TRUE(CBB_init(cbb.get(), 0));
959 ASSERT_TRUE(EVP_marshal_private_key(cbb.get(), privkey.get()));
960 ASSERT_TRUE(CBB_finish(cbb.get(), &der, &der_len));
961 free_der.reset(der);
962 EXPECT_EQ(Bytes(kPrivateKeyPKCS8), Bytes(der, der_len));
963
964 // Test EVP_PKEY_cmp.
965 EXPECT_EQ(1, EVP_PKEY_cmp(pubkey.get(), privkey.get()));
966
967 static const uint8_t kZeros[32] = {0};
968 bssl::UniquePtr<EVP_PKEY> pubkey2(EVP_PKEY_new_raw_public_key(
969 EVP_PKEY_ED25519, nullptr, kZeros, sizeof(kZeros)));
970 ASSERT_TRUE(pubkey2);
971 EXPECT_EQ(0, EVP_PKEY_cmp(pubkey.get(), pubkey2.get()));
972 EXPECT_EQ(0, EVP_PKEY_cmp(privkey.get(), pubkey2.get()));
973
974 // Ed25519 may not be used streaming.
975 bssl::ScopedEVP_MD_CTX ctx;
976 ASSERT_TRUE(
977 EVP_DigestSignInit(ctx.get(), nullptr, nullptr, nullptr, privkey.get()));
978 EXPECT_FALSE(EVP_DigestSignUpdate(ctx.get(), nullptr, 0));
979 EXPECT_FALSE(EVP_DigestSignFinal(ctx.get(), nullptr, &len));
980 ERR_clear_error();
981
982 ctx.Reset();
983 ASSERT_TRUE(EVP_DigestVerifyInit(ctx.get(), nullptr, nullptr, nullptr,
984 privkey.get()));
985 EXPECT_FALSE(EVP_DigestVerifyUpdate(ctx.get(), nullptr, 0));
986 EXPECT_FALSE(EVP_DigestVerifyFinal(ctx.get(), nullptr, 0));
987 ERR_clear_error();
988
989 // The buffer length to |EVP_DigestSign| is an input/output parameter and
990 // should be checked before signing.
991 ctx.Reset();
992 ASSERT_TRUE(
993 EVP_DigestSignInit(ctx.get(), nullptr, nullptr, nullptr, privkey.get()));
994 len = 31;
995 EXPECT_FALSE(EVP_DigestSign(ctx.get(), buf, &len, nullptr /* msg */, 0));
996 EXPECT_TRUE(
997 ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_BUFFER_TOO_SMALL));
998 ERR_clear_error();
999 }
1000
ExpectECGroupOnly(const EVP_PKEY * pkey,int nid)1001 static void ExpectECGroupOnly(const EVP_PKEY *pkey, int nid) {
1002 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
1003 ASSERT_TRUE(ec);
1004 const EC_GROUP *group = EC_KEY_get0_group(ec);
1005 ASSERT_TRUE(group);
1006 EXPECT_EQ(nid, EC_GROUP_get_curve_name(group));
1007 EXPECT_FALSE(EC_KEY_get0_public_key(ec));
1008 EXPECT_FALSE(EC_KEY_get0_private_key(ec));
1009 }
1010
ExpectECGroupAndKey(const EVP_PKEY * pkey,int nid)1011 static void ExpectECGroupAndKey(const EVP_PKEY *pkey, int nid) {
1012 EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
1013 ASSERT_TRUE(ec);
1014 const EC_GROUP *group = EC_KEY_get0_group(ec);
1015 ASSERT_TRUE(group);
1016 EXPECT_EQ(nid, EC_GROUP_get_curve_name(group));
1017 EXPECT_TRUE(EC_KEY_get0_public_key(ec));
1018 EXPECT_TRUE(EC_KEY_get0_private_key(ec));
1019 }
1020
TEST(EVPExtraTest,ECKeygen)1021 TEST(EVPExtraTest, ECKeygen) {
1022 for (bool copy : {false, true}) {
1023 SCOPED_TRACE(copy);
1024
1025 auto maybe_copy = [&](bssl::UniquePtr<EVP_PKEY_CTX> *ctx) -> bool {
1026 if (copy) {
1027 ctx->reset(EVP_PKEY_CTX_dup(ctx->get()));
1028 }
1029 return *ctx != nullptr;
1030 };
1031
1032 // |EVP_PKEY_paramgen| may be used as an extremely roundabout way to get an
1033 // |EC_GROUP|.
1034 bssl::UniquePtr<EVP_PKEY_CTX> ctx(
1035 EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr));
1036 ASSERT_TRUE(ctx);
1037 ASSERT_TRUE(maybe_copy(&ctx));
1038 ASSERT_TRUE(EVP_PKEY_paramgen_init(ctx.get()));
1039 ASSERT_TRUE(maybe_copy(&ctx));
1040 ASSERT_TRUE(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx.get(),
1041 NID_X9_62_prime256v1));
1042 ASSERT_TRUE(maybe_copy(&ctx));
1043 EVP_PKEY *raw = nullptr;
1044 ASSERT_TRUE(EVP_PKEY_paramgen(ctx.get(), &raw));
1045 bssl::UniquePtr<EVP_PKEY> pkey(raw);
1046 raw = nullptr;
1047 ExpectECGroupOnly(pkey.get(), NID_X9_62_prime256v1);
1048
1049 // That resulting |EVP_PKEY| may be used as a template for key generation.
1050 ctx.reset(EVP_PKEY_CTX_new(pkey.get(), nullptr));
1051 ASSERT_TRUE(ctx);
1052 ASSERT_TRUE(maybe_copy(&ctx));
1053 ASSERT_TRUE(EVP_PKEY_keygen_init(ctx.get()));
1054 ASSERT_TRUE(maybe_copy(&ctx));
1055 raw = nullptr;
1056 ASSERT_TRUE(EVP_PKEY_keygen(ctx.get(), &raw));
1057 pkey.reset(raw);
1058 raw = nullptr;
1059 ExpectECGroupAndKey(pkey.get(), NID_X9_62_prime256v1);
1060
1061 // |EVP_PKEY_paramgen| may also be skipped.
1062 ctx.reset(EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr));
1063 ASSERT_TRUE(ctx);
1064 ASSERT_TRUE(maybe_copy(&ctx));
1065 ASSERT_TRUE(EVP_PKEY_keygen_init(ctx.get()));
1066 ASSERT_TRUE(maybe_copy(&ctx));
1067 ASSERT_TRUE(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx.get(),
1068 NID_X9_62_prime256v1));
1069 ASSERT_TRUE(maybe_copy(&ctx));
1070 raw = nullptr;
1071 ASSERT_TRUE(EVP_PKEY_keygen(ctx.get(), &raw));
1072 pkey.reset(raw);
1073 raw = nullptr;
1074 ExpectECGroupAndKey(pkey.get(), NID_X9_62_prime256v1);
1075 }
1076 }
1077
TEST(EVPExtraTest,DHKeygen)1078 TEST(EVPExtraTest, DHKeygen) {
1079 // Set up some DH params in an |EVP_PKEY|. There is currently no API to do
1080 // this from EVP directly.
1081 bssl::UniquePtr<BIGNUM> p(BN_get_rfc3526_prime_1536(nullptr));
1082 ASSERT_TRUE(p);
1083 bssl::UniquePtr<BIGNUM> g(BN_new());
1084 ASSERT_TRUE(g);
1085 ASSERT_TRUE(BN_set_u64(g.get(), 2));
1086 bssl::UniquePtr<DH> params_dh(DH_new());
1087 ASSERT_TRUE(params_dh);
1088 ASSERT_TRUE(
1089 DH_set0_pqg(params_dh.get(), p.release(), /*q=*/nullptr, g.release()));
1090 bssl::UniquePtr<EVP_PKEY> params(EVP_PKEY_new());
1091 ASSERT_TRUE(params);
1092 ASSERT_TRUE(EVP_PKEY_set1_DH(params.get(), params_dh.get()));
1093
1094 for (bool copy : {false, true}) {
1095 SCOPED_TRACE(copy);
1096
1097 auto maybe_copy = [&](bssl::UniquePtr<EVP_PKEY_CTX> *ctx) -> bool {
1098 if (copy) {
1099 ctx->reset(EVP_PKEY_CTX_dup(ctx->get()));
1100 }
1101 return *ctx != nullptr;
1102 };
1103
1104 // |params| may be used as a template for key generation.
1105 bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(params.get(), nullptr));
1106 ASSERT_TRUE(ctx);
1107 ASSERT_TRUE(maybe_copy(&ctx));
1108 ASSERT_TRUE(EVP_PKEY_keygen_init(ctx.get()));
1109 ASSERT_TRUE(maybe_copy(&ctx));
1110 EVP_PKEY *raw = nullptr;
1111 ASSERT_TRUE(EVP_PKEY_keygen(ctx.get(), &raw));
1112 bssl::UniquePtr<EVP_PKEY> pkey(raw);
1113
1114 EXPECT_EQ(EVP_PKEY_id(pkey.get()), EVP_PKEY_DH);
1115 const DH *dh = EVP_PKEY_get0_DH(pkey.get());
1116 EXPECT_EQ(0, BN_cmp(DH_get0_p(dh), DH_get0_p(params_dh.get())));
1117 EXPECT_EQ(0, BN_cmp(DH_get0_g(dh), DH_get0_g(params_dh.get())));
1118 EXPECT_FALSE(DH_get0_q(dh));
1119 EXPECT_TRUE(DH_get0_pub_key(dh));
1120 EXPECT_TRUE(DH_get0_priv_key(dh));
1121 EXPECT_EQ(1, EVP_PKEY_cmp_parameters(params.get(), pkey.get()));
1122 EXPECT_EQ(0, EVP_PKEY_cmp(params.get(), pkey.get()));
1123
1124 // Generate a second key.
1125 ctx.reset(EVP_PKEY_CTX_new(params.get(), nullptr));
1126 ASSERT_TRUE(ctx);
1127 ASSERT_TRUE(maybe_copy(&ctx));
1128 ASSERT_TRUE(EVP_PKEY_keygen_init(ctx.get()));
1129 ASSERT_TRUE(maybe_copy(&ctx));
1130 raw = nullptr;
1131 ASSERT_TRUE(EVP_PKEY_keygen(ctx.get(), &raw));
1132 bssl::UniquePtr<EVP_PKEY> pkey2(raw);
1133
1134 EXPECT_EQ(1, EVP_PKEY_cmp_parameters(params.get(), pkey2.get()));
1135 EXPECT_EQ(1, EVP_PKEY_cmp_parameters(pkey.get(), pkey2.get()));
1136 EXPECT_EQ(0, EVP_PKEY_cmp(pkey.get(), pkey2.get()));
1137 }
1138 }
1139
1140 // Test that |EVP_PKEY_keygen| works for Ed25519.
TEST(EVPExtraTest,Ed25519Keygen)1141 TEST(EVPExtraTest, Ed25519Keygen) {
1142 bssl::UniquePtr<EVP_PKEY_CTX> pctx(
1143 EVP_PKEY_CTX_new_id(EVP_PKEY_ED25519, nullptr));
1144 ASSERT_TRUE(pctx);
1145 ASSERT_TRUE(EVP_PKEY_keygen_init(pctx.get()));
1146 EVP_PKEY *raw = nullptr;
1147 ASSERT_TRUE(EVP_PKEY_keygen(pctx.get(), &raw));
1148 bssl::UniquePtr<EVP_PKEY> pkey(raw);
1149
1150 // Round-trip a signature to sanity-check the key is good.
1151 bssl::ScopedEVP_MD_CTX ctx;
1152 ASSERT_TRUE(
1153 EVP_DigestSignInit(ctx.get(), nullptr, nullptr, nullptr, pkey.get()));
1154 uint8_t sig[64];
1155 size_t len = sizeof(sig);
1156 ASSERT_TRUE(EVP_DigestSign(ctx.get(), sig, &len,
1157 reinterpret_cast<const uint8_t *>("hello"), 5));
1158
1159 ctx.Reset();
1160 ASSERT_TRUE(
1161 EVP_DigestVerifyInit(ctx.get(), nullptr, nullptr, nullptr, pkey.get()));
1162 ASSERT_TRUE(EVP_DigestVerify(ctx.get(), sig, len,
1163 reinterpret_cast<const uint8_t *>("hello"), 5));
1164 }
1165
1166 // Test that OpenSSL's legacy TLS-specific APIs in EVP work correctly. When we
1167 // target OpenSSL 3.0, these should be renamed to
1168 // |EVP_PKEY_get1_encoded_public_key|.
TEST(EVPExtraTest,TLSEncodedPoint)1169 TEST(EVPExtraTest, TLSEncodedPoint) {
1170 const struct {
1171 int pkey_type;
1172 std::vector<uint8_t> spki;
1173 std::vector<uint8_t> encoded_point;
1174 } kTests[] = {
1175 {EVP_PKEY_EC,
1176 {0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
1177 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
1178 0x42, 0x00, 0x04, 0x2c, 0x15, 0x0f, 0x42, 0x9c, 0xe7, 0x0f, 0x21, 0x6c,
1179 0x25, 0x2c, 0xf5, 0xe0, 0x62, 0xce, 0x1f, 0x63, 0x9c, 0xd5, 0xd1, 0x65,
1180 0xc7, 0xf8, 0x94, 0x24, 0x07, 0x2c, 0x27, 0x19, 0x7d, 0x78, 0xb3, 0x3b,
1181 0x92, 0x0e, 0x95, 0xcd, 0xb6, 0x64, 0xe9, 0x90, 0xdc, 0xf0, 0xcf, 0xea,
1182 0x0d, 0x94, 0xe2, 0xa8, 0xe6, 0xaf, 0x9d, 0x0e, 0x58, 0x05, 0x6e, 0x65,
1183 0x31, 0x04, 0x92, 0x5b, 0x9f, 0xe6, 0xc9},
1184 {0x04, 0x2c, 0x15, 0x0f, 0x42, 0x9c, 0xe7, 0x0f, 0x21, 0x6c, 0x25,
1185 0x2c, 0xf5, 0xe0, 0x62, 0xce, 0x1f, 0x63, 0x9c, 0xd5, 0xd1, 0x65,
1186 0xc7, 0xf8, 0x94, 0x24, 0x07, 0x2c, 0x27, 0x19, 0x7d, 0x78, 0xb3,
1187 0x3b, 0x92, 0x0e, 0x95, 0xcd, 0xb6, 0x64, 0xe9, 0x90, 0xdc, 0xf0,
1188 0xcf, 0xea, 0x0d, 0x94, 0xe2, 0xa8, 0xe6, 0xaf, 0x9d, 0x0e, 0x58,
1189 0x05, 0x6e, 0x65, 0x31, 0x04, 0x92, 0x5b, 0x9f, 0xe6, 0xc9}},
1190 {EVP_PKEY_X25519,
1191 {0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e, 0x03, 0x21,
1192 0x00, 0xe6, 0xdb, 0x68, 0x67, 0x58, 0x30, 0x30, 0xdb, 0x35, 0x94,
1193 0xc1, 0xa4, 0x24, 0xb1, 0x5f, 0x7c, 0x72, 0x66, 0x24, 0xec, 0x26,
1194 0xb3, 0x35, 0x3b, 0x10, 0xa9, 0x03, 0xa6, 0xd0, 0xab, 0x1c, 0x4c},
1195 {0xe6, 0xdb, 0x68, 0x67, 0x58, 0x30, 0x30, 0xdb, 0x35, 0x94, 0xc1,
1196 0xa4, 0x24, 0xb1, 0x5f, 0x7c, 0x72, 0x66, 0x24, 0xec, 0x26, 0xb3,
1197 0x35, 0x3b, 0x10, 0xa9, 0x03, 0xa6, 0xd0, 0xab, 0x1c, 0x4c}}};
1198 for (const auto& test : kTests) {
1199 SCOPED_TRACE(test.pkey_type);
1200 SCOPED_TRACE(Bytes(test.spki));
1201 CBS spki;
1202 CBS_init(&spki, test.spki.data(), test.spki.size());
1203 bssl::UniquePtr<EVP_PKEY> from_spki(EVP_parse_public_key(&spki));
1204 ASSERT_TRUE(from_spki);
1205
1206 uint8_t *data;
1207 size_t len = EVP_PKEY_get1_tls_encodedpoint(from_spki.get(), &data);
1208 ASSERT_GT(len, 0u);
1209 EXPECT_EQ(Bytes(data, len), Bytes(test.encoded_point));
1210 OPENSSL_free(data);
1211
1212 bssl::UniquePtr<EVP_PKEY> from_encoded_point(EVP_PKEY_new());
1213 ASSERT_TRUE(from_encoded_point);
1214 if (test.pkey_type == EVP_PKEY_EC) {
1215 // |EVP_PKEY_EC| should have been |EVP_PKEY_EC_P256|, etc., but instead
1216 // part of the type is buried inside parameters.
1217 ASSERT_TRUE(
1218 EVP_PKEY_copy_parameters(from_encoded_point.get(), from_spki.get()));
1219 } else {
1220 ASSERT_TRUE(EVP_PKEY_set_type(from_encoded_point.get(), test.pkey_type));
1221 }
1222 ASSERT_TRUE(EVP_PKEY_set1_tls_encodedpoint(from_encoded_point.get(),
1223 test.encoded_point.data(),
1224 test.encoded_point.size()));
1225
1226 bssl::ScopedCBB cbb;
1227 ASSERT_TRUE(CBB_init(cbb.get(), test.spki.size()));
1228 ASSERT_TRUE(EVP_marshal_public_key(cbb.get(), from_encoded_point.get()));
1229 EXPECT_EQ(Bytes(CBB_data(cbb.get()), CBB_len(cbb.get())), Bytes(test.spki));
1230 }
1231 }
1232
TEST(EVPExtraTest,Parameters)1233 TEST(EVPExtraTest, Parameters) {
1234 auto new_pkey_with_type = [](int type) -> bssl::UniquePtr<EVP_PKEY> {
1235 bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
1236 if (!pkey || //
1237 !EVP_PKEY_set_type(pkey.get(), type)) {
1238 return nullptr;
1239 }
1240 return pkey;
1241 };
1242
1243 auto new_pkey_with_curve = [](int curve_nid) -> bssl::UniquePtr<EVP_PKEY> {
1244 bssl::UniquePtr<EVP_PKEY_CTX> ctx(
1245 EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr));
1246 EVP_PKEY *pkey = nullptr;
1247 if (!ctx || //
1248 !EVP_PKEY_paramgen_init(ctx.get()) ||
1249 !EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx.get(), curve_nid) ||
1250 !EVP_PKEY_paramgen(ctx.get(), &pkey)) {
1251 return nullptr;
1252 }
1253 return bssl::UniquePtr<EVP_PKEY>(pkey);
1254 };
1255
1256 // RSA keys have no parameters.
1257 bssl::UniquePtr<EVP_PKEY> rsa = new_pkey_with_type(EVP_PKEY_RSA);
1258 ASSERT_TRUE(rsa);
1259 EXPECT_FALSE(EVP_PKEY_missing_parameters(rsa.get()));
1260
1261 // EC keys have parameters.
1262 bssl::UniquePtr<EVP_PKEY> ec_no_params = new_pkey_with_type(EVP_PKEY_EC);
1263 ASSERT_TRUE(ec_no_params);
1264 EXPECT_TRUE(EVP_PKEY_missing_parameters(ec_no_params.get()));
1265
1266 bssl::UniquePtr<EVP_PKEY> p256 = new_pkey_with_curve(NID_X9_62_prime256v1);
1267 ASSERT_TRUE(p256);
1268 EXPECT_FALSE(EVP_PKEY_missing_parameters(p256.get()));
1269
1270 bssl::UniquePtr<EVP_PKEY> p256_2 = new_pkey_with_curve(NID_X9_62_prime256v1);
1271 ASSERT_TRUE(p256_2);
1272 EXPECT_FALSE(EVP_PKEY_missing_parameters(p256_2.get()));
1273
1274 bssl::UniquePtr<EVP_PKEY> p384 = new_pkey_with_curve(NID_secp384r1);
1275 ASSERT_TRUE(p384);
1276 EXPECT_FALSE(EVP_PKEY_missing_parameters(p384.get()));
1277
1278 EXPECT_EQ(1, EVP_PKEY_cmp_parameters(p256.get(), p256_2.get()));
1279 EXPECT_EQ(0, EVP_PKEY_cmp_parameters(p256.get(), p384.get()));
1280
1281 // Copying parameters onto a curve-less EC key works.
1282 ASSERT_TRUE(EVP_PKEY_copy_parameters(ec_no_params.get(), p256.get()));
1283 EXPECT_EQ(1, EVP_PKEY_cmp_parameters(p256.get(), ec_no_params.get()));
1284
1285 // No-op copies silently succeed.
1286 ASSERT_TRUE(EVP_PKEY_copy_parameters(ec_no_params.get(), p256.get()));
1287 EXPECT_EQ(1, EVP_PKEY_cmp_parameters(p256.get(), ec_no_params.get()));
1288
1289 // Copying parameters onto a type-less key works.
1290 bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
1291 ASSERT_TRUE(pkey);
1292 ASSERT_TRUE(EVP_PKEY_copy_parameters(pkey.get(), p256.get()));
1293 EXPECT_EQ(EVP_PKEY_EC, EVP_PKEY_id(pkey.get()));
1294 EXPECT_EQ(1, EVP_PKEY_cmp_parameters(p256.get(), pkey.get()));
1295
1296 // |EVP_PKEY_copy_parameters| cannot change a key's type or curve.
1297 EXPECT_FALSE(EVP_PKEY_copy_parameters(rsa.get(), p256.get()));
1298 EXPECT_EQ(EVP_PKEY_RSA, EVP_PKEY_id(rsa.get()));
1299 EXPECT_FALSE(EVP_PKEY_copy_parameters(rsa.get(), p256.get()));
1300 EXPECT_EQ(EVP_PKEY_RSA, EVP_PKEY_id(rsa.get()));
1301 }
1302
TEST(EVPExtraTest,RawKeyUnsupported)1303 TEST(EVPExtraTest, RawKeyUnsupported) {
1304 static const uint8_t kKey[] = {1, 2, 3, 4};
1305 EXPECT_FALSE(
1306 EVP_PKEY_new_raw_public_key(EVP_PKEY_RSA, nullptr, kKey, sizeof(kKey)));
1307 EXPECT_FALSE(
1308 EVP_PKEY_new_raw_private_key(EVP_PKEY_RSA, nullptr, kKey, sizeof(kKey)));
1309 }
1310