xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/hcrypto/test_pkcs12.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1 /*	$NetBSD: test_pkcs12.c,v 1.2 2017/01/28 21:31:47 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Kungliga Tekniska Högskolan
5  * (Royal Institute of Technology, Stockholm, Sweden).
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the Institute nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <config.h>
37 #include <krb5/roken.h>
38 
39 #include <pkcs12.h>
40 #include <evp.h>
41 
42 struct tests {
43     int id;
44     const char *password;
45     void *salt;
46     size_t saltsize;
47     int iterations;
48     size_t keylen;
49     const EVP_MD * (*md)(void);
50     void *key;
51 };
52 
53 struct tests p12_pbe_tests[] = {
54     { PKCS12_KEY_ID,
55       NULL,
56       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
57       16,
58       100,
59       16,
60       EVP_sha1,
61       "\xd7\x2d\xd4\xcf\x7e\xe1\x89\xc5\xb5\xe5\x31\xa7\x63\x2c\xf0\x4b"
62     },
63     { PKCS12_KEY_ID,
64       "",
65       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
66       16,
67       100,
68       16,
69       EVP_sha1,
70       "\x00\x54\x91\xaf\xc0\x6a\x76\xc3\xf9\xb6\xf2\x28\x1a\x15\xd9\xfe"
71     },
72     { PKCS12_KEY_ID,
73       "foobar",
74       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
75       16,
76       100,
77       16,
78       EVP_sha1,
79       "\x79\x95\xbf\x3f\x1c\x6d\xe\xe8\xd3\x71\xc4\x94\xd\xb\x18\xb5"
80     },
81     { PKCS12_KEY_ID,
82       "foobar",
83       "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
84       16,
85       2048,
86       24,
87       EVP_sha1,
88       "\x0b\xb5\xe\xa6\x71\x0d\x0c\xf7\x44\xe\xe1\x9b\xb5\xdf\xf1\xdc\x4f\xb0\xca\xe\xee\x4f\xb9\xfd"
89     },
90     { PKCS12_IV_ID,
91       "foobar",
92       "\x3c\xdf\x84\x32\x59\xd3\xda\x69",
93       8,
94       2048,
95       8,
96       EVP_sha1,
97       "\xbf\x9a\x12\xb7\x26\x69\xfd\x05"
98     }
99 
100 };
101 
102 static int
test_pkcs12_pbe(struct tests * t)103 test_pkcs12_pbe(struct tests *t)
104 {
105     void *key;
106     size_t pwlen = 0;
107 
108     key = malloc(t->keylen);
109     if (t->password)
110 	pwlen = strlen(t->password);
111 
112     if (!PKCS12_key_gen(t->password, pwlen,
113 			t->salt, t->saltsize,
114 			t->id, t->iterations, t->keylen,
115 			key, t->md()))
116     {
117 	printf("key_gen failed\n");
118 	return 1;
119     }
120 
121     if (memcmp(t->key, key, t->keylen) != 0) {
122 	printf("incorrect key\n");
123 	free(key);
124 	return 1;
125     }
126     free(key);
127     return 0;
128 }
129 
130 int
main(int argc,char ** argv)131 main(int argc, char **argv)
132 {
133     int ret = 0;
134     int i;
135 
136     for (i = 0; i < sizeof(p12_pbe_tests)/sizeof(p12_pbe_tests[0]); i++)
137 	ret += test_pkcs12_pbe(&p12_pbe_tests[i]);
138 
139     return ret;
140 }
141