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