Branch data Line data Source code
1 : : /* priv.c --- Shishi PRIV self tests.
2 : : * Copyright (C) 2002, 2003, 2006, 2007 Simon Josefsson
3 : : *
4 : : * This file is part of Shishi.
5 : : *
6 : : * Shishi is free software; you can redistribute it and/or modify
7 : : * it under the terms of the GNU General Public License as published by
8 : : * the Free Software Foundation; either version 3 of the License, or
9 : : * (at your option) any later version.
10 : : *
11 : : * Shishi is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : * GNU General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU General Public License
17 : : * along with Shishi; if not, see http://www.gnu.org/licenses or write
18 : : * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
19 : : * Floor, Boston, MA 02110-1301, USA
20 : : *
21 : : */
22 : :
23 : : #include "utils.c"
24 : :
25 : : void
26 : 1 : test (Shishi * handle)
27 : : {
28 : : Shishi_priv *priv;
29 : : Shishi_key *key;
30 : : Shishi_asn1 asn1priv;
31 : : Shishi_asn1 asn1encprivpart;
32 : : char *p, *q;
33 : : size_t l, m;
34 : : int32_t t;
35 : : int res;
36 : :
37 [ - + ]: 1 : if (debug)
38 : 0 : shishi_cfg (handle, strdup ("verbose-crypto"));
39 : :
40 : : /* shishi_priv() */
41 : 1 : res = shishi_priv (handle, &priv);
42 [ - + ]: 1 : if (debug)
43 : 0 : printf ("shishi_priv () => `%p'.\n", priv);
44 [ + - ]: 1 : if (res == SHISHI_OK)
45 : 1 : success ("shishi_priv() OK\n");
46 : : else
47 : 0 : fail ("shishi_priv() failed\n");
48 : :
49 : : /* shishi_priv_key */
50 : 1 : key = shishi_priv_key (priv);
51 [ + - ]: 1 : if (key)
52 : 1 : success ("shishi_priv_key() OK\n");
53 : : else
54 : 0 : fail ("shishi_priv_key() failed\n");
55 : :
56 : : /* shishi_priv_priv */
57 : 1 : asn1priv = shishi_priv_priv (priv);
58 [ + - ]: 1 : if (asn1priv)
59 : 1 : success ("shishi_priv_priv() OK\n");
60 : : else
61 : 0 : fail ("shishi_priv_priv() failed\n");
62 : :
63 : : /* shishi_priv_encprivpart */
64 : 1 : asn1encprivpart = shishi_priv_encprivpart (priv);
65 [ + - ]: 1 : if (asn1encprivpart)
66 : 1 : success ("shishi_priv_encprivpart() OK\n");
67 : : else
68 : 0 : fail ("shishi_priv_encprivpart() failed\n");
69 : :
70 : : /* shishi_encprivpart_set_user_data */
71 : 1 : res = shishi_encprivpart_set_user_data (handle, asn1encprivpart, "foo", 3);
72 [ + - ]: 1 : if (res == SHISHI_OK)
73 : 1 : success ("shishi_encprivpart_set_user_data() OK\n");
74 : : else
75 : 0 : fail ("shishi_encprivpart_set_user_data() failed (%d)\n", res);
76 : :
77 : : /* shishi_encprivpart_user_data */
78 : 1 : res = shishi_encprivpart_user_data (handle, asn1encprivpart, &p, &l);
79 [ - + ]: 1 : if (debug)
80 : 0 : escapeprint (p, l);
81 [ + - ][ + - ]: 2 : if (res == SHISHI_OK && l == 3 && memcmp (p, "foo", 3) == 0)
[ + - ]
82 : 1 : success ("shishi_encprivpart_user_data() OK\n");
83 : : else
84 : 0 : fail ("shishi_encprivpart_user_data() failed (%d)\n", res);
85 : 1 : free (p);
86 : :
87 : : /* shishi_priv_set_cksum */
88 : 1 : res = shishi_priv_set_enc_part (handle, asn1priv, 42, "bar", 3);
89 [ + - ]: 1 : if (res == SHISHI_OK)
90 : 1 : success ("shishi_priv_set_enc_part() OK\n");
91 : : else
92 : 0 : fail ("shishi_priv_set_enc_part() failed (%d)\n", res);
93 : :
94 : : /* shishi_priv_enc_part_etype */
95 : 1 : res = shishi_priv_enc_part_etype (handle, asn1priv, &t);
96 [ - + ]: 1 : if (debug)
97 : 0 : printf ("type=%d\n", t);
98 [ + - ][ + - ]: 2 : if (res == SHISHI_OK && t == 42)
99 : 1 : success ("shishi_priv_enc_part_etype() OK\n");
100 : : else
101 : 0 : fail ("shishi_priv_enc_part_etype() failed (%d)\n", res);
102 : :
103 : : /* shishi_priv_process */
104 : 1 : res = shishi_priv_process (priv, key);
105 [ + - ]: 1 : if (res == SHISHI_PRIV_BAD_KEYTYPE) /* t==42 unsupported etype */
106 : 1 : success ("shishi_priv_proces() OK\n");
107 : : else
108 : 0 : fail ("shishi_priv_process() failed (%d)\n", res);
109 : :
110 : : /* shishi_priv_priv_der() */
111 : 1 : res = shishi_priv_priv_der (priv, &p, &l);
112 [ + - ]: 1 : if (res == SHISHI_OK)
113 : 1 : success ("shishi_priv_priv_der() OK\n");
114 : : else
115 : 0 : fail ("shishi_priv_priv_der() failed\n");
116 : :
117 : : /* shishi_priv_to_file() */
118 : 1 : res = shishi_priv_to_file (handle, asn1priv, SHISHI_FILETYPE_TEXT,
119 : : "priv.tmp");
120 [ + - ]: 1 : if (res == SHISHI_OK)
121 : 1 : success ("shishi_priv_to_file() OK\n");
122 : : else
123 : 0 : fail ("shishi_priv_to_file() failed\n");
124 : :
125 : : /* shishi_priv_done() */
126 : 1 : shishi_priv_done (priv);
127 : 1 : success ("shishi_priv_done() OK\n");
128 : :
129 : : /* shishi_authenticator_from_file() */
130 : 1 : asn1priv = NULL;
131 : 1 : res = shishi_priv_from_file (handle, &asn1priv, SHISHI_FILETYPE_TEXT,
132 : : "priv.tmp");
133 [ + - ]: 1 : if (res == SHISHI_OK)
134 : 1 : success ("shishi_priv_from_file() OK\n");
135 : : else
136 : 0 : fail ("shishi_priv_from_file() failed\n");
137 : :
138 [ - + ]: 1 : if (debug)
139 : : {
140 : : /* shishi_priv_print() */
141 : 0 : res = shishi_priv_print (handle, stdout, asn1priv);
142 [ # # ]: 0 : if (res == SHISHI_OK)
143 : 0 : success ("shishi_priv_print() OK\n");
144 : : else
145 : 0 : fail ("shishi_priv_print() failed\n");
146 : : }
147 : :
148 : : /* shishi_asn1_to_der() */
149 : 1 : res = shishi_asn1_to_der (handle, asn1priv, &q, &m);
150 [ + - ]: 1 : if (res == SHISHI_OK)
151 : 1 : success ("shishi_asn1_to_der() OK\n");
152 : : else
153 : 0 : fail ("shishi_asn1_to_der() failed\n");
154 : :
155 : 1 : shishi_asn1_done (handle, asn1priv);
156 : :
157 : : /* Compare DER encodings of authenticators */
158 [ + - + - ]: 2 : if (l > 0 && m > 0 && l == m && memcmp (p, q, l) == 0)
[ + - ][ + - ]
159 : 1 : success ("DER comparison OK\n");
160 : : else
161 : 0 : fail ("DER comparison failed\n");
162 : :
163 : 1 : free (q);
164 : 1 : free (p);
165 : :
166 : : /* unlink() */
167 : 1 : res = unlink ("priv.tmp");
168 [ + - ]: 1 : if (res == 0)
169 : 1 : success ("unlink() OK\n");
170 : : else
171 : 0 : fail ("unlink() failed\n");
172 : 1 : }
|