1 /* $NetBSD: test_pkcs12.c,v 1.1.1.1 2011/04/13 18:14:51 elric 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 38 #include <sys/types.h> 39 #include <limits.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 44 #include <pkcs12.h> 45 #include <evp.h> 46 47 struct tests { 48 int id; 49 const char *password; 50 void *salt; 51 size_t saltsize; 52 int iterations; 53 size_t keylen; 54 const EVP_MD * (*md)(void); 55 void *key; 56 }; 57 58 struct tests p12_pbe_tests[] = { 59 { PKCS12_KEY_ID, 60 NULL, 61 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 62 16, 63 100, 64 16, 65 EVP_sha1, 66 "\xd7\x2d\xd4\xcf\x7e\xe1\x89\xc5\xb5\xe5\x31\xa7\x63\x2c\xf0\x4b" 67 }, 68 { PKCS12_KEY_ID, 69 "", 70 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 71 16, 72 100, 73 16, 74 EVP_sha1, 75 "\x00\x54\x91\xaf\xc0\x6a\x76\xc3\xf9\xb6\xf2\x28\x1a\x15\xd9\xfe" 76 }, 77 { PKCS12_KEY_ID, 78 "foobar", 79 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 80 16, 81 100, 82 16, 83 EVP_sha1, 84 "\x79\x95\xbf\x3f\x1c\x6d\xe\xe8\xd3\x71\xc4\x94\xd\xb\x18\xb5" 85 }, 86 { PKCS12_KEY_ID, 87 "foobar", 88 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 89 16, 90 2048, 91 24, 92 EVP_sha1, 93 "\x0b\xb5\xe\xa6\x71\x0d\x0c\xf7\x44\xe\xe1\x9b\xb5\xdf\xf1\xdc\x4f\xb0\xca\xe\xee\x4f\xb9\xfd" 94 }, 95 { PKCS12_IV_ID, 96 "foobar", 97 "\x3c\xdf\x84\x32\x59\xd3\xda\x69", 98 8, 99 2048, 100 8, 101 EVP_sha1, 102 "\xbf\x9a\x12\xb7\x26\x69\xfd\x05" 103 } 104 105 }; 106 107 static int 108 test_pkcs12_pbe(struct tests *t) 109 { 110 void *key; 111 size_t pwlen = 0; 112 113 key = malloc(t->keylen); 114 if (t->password) 115 pwlen = strlen(t->password); 116 117 if (!PKCS12_key_gen(t->password, pwlen, 118 t->salt, t->saltsize, 119 t->id, t->iterations, t->keylen, 120 key, t->md())) 121 { 122 printf("key_gen failed\n"); 123 return 1; 124 } 125 126 if (memcmp(t->key, key, t->keylen) != 0) { 127 printf("incorrect key\n"); 128 free(key); 129 return 1; 130 } 131 free(key); 132 return 0; 133 } 134 135 int 136 main(int argc, char **argv) 137 { 138 int ret = 0; 139 int i; 140 141 for (i = 0; i < sizeof(p12_pbe_tests)/sizeof(p12_pbe_tests[0]); i++) 142 ret += test_pkcs12_pbe(&p12_pbe_tests[i]); 143 144 return ret; 145 } 146