Line data Source code
1 : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : /*
3 : Copyright (C) 2008 Stefan Walter
4 :
5 : The Gnome Keyring Library is free software; you can redistribute it and/or
6 : modify it under the terms of the GNU Library General Public License as
7 : published by the Free Software Foundation; either version 2 of the
8 : License, or (at your option) any later version.
9 :
10 : The Gnome Keyring Library is distributed in the hope that it will be useful,
11 : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : Library General Public License for more details.
14 :
15 : You should have received a copy of the GNU Library General Public
16 : License along with the Gnome Library; see the file COPYING.LIB. If not,
17 : <http://www.gnu.org/licenses/>.
18 :
19 : Author: Stef Walter <stef@memberwebs.com>
20 : */
21 :
22 : #include "config.h"
23 :
24 : #include "egg/egg-secure-memory.h"
25 :
26 : #include "ssh-store/gkm-ssh-openssh.h"
27 :
28 : #include "gkm/gkm-sexp.h"
29 :
30 : #include <glib.h>
31 :
32 : #include <stdlib.h>
33 : #include <stdio.h>
34 : #include <string.h>
35 :
36 68 : EGG_SECURE_DEFINE_GLIB_GLOBALS ();
37 :
38 : static const gchar *PRIVATE_FILES[] = {
39 : SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_encrypted",
40 : SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_plain",
41 : SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_encrypted",
42 : SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_plain",
43 : SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_encrypted",
44 : SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_plain",
45 : SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_384",
46 : SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_521"
47 : };
48 :
49 : static const gchar *PUBLIC_FILES[] = {
50 : SRCDIR "/pkcs11/ssh-store/fixtures/id_rsa_test.pub",
51 : SRCDIR "/pkcs11/ssh-store/fixtures/id_dsa_test.pub",
52 : SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_test.pub",
53 : SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_384.pub",
54 : SRCDIR "/pkcs11/ssh-store/fixtures/id_ecdsa_521.pub"
55 : };
56 :
57 : #define COMMENT "A public key comment"
58 :
59 : static void
60 1 : test_parse_public (void)
61 : {
62 : gcry_sexp_t sexp;
63 : gchar *comment;
64 : gchar *data;
65 : gsize n_data;
66 : int algorithm;
67 : gboolean is_private;
68 : guint i;
69 : GkmDataResult res;
70 :
71 :
72 6 : for (i = 0; i < G_N_ELEMENTS (PUBLIC_FILES); ++i) {
73 :
74 5 : if (!g_file_get_contents (PUBLIC_FILES[i], &data, &n_data, NULL))
75 0 : g_assert_not_reached ();
76 :
77 5 : res = gkm_ssh_openssh_parse_public_key (data, n_data, &sexp, &comment);
78 5 : if (res != GKM_DATA_SUCCESS) {
79 0 : g_warning ("couldn't parse public key: %s", PUBLIC_FILES[i]);
80 0 : g_assert_cmpint (res, ==, GKM_DATA_SUCCESS);
81 : }
82 :
83 5 : if (!gkm_sexp_parse_key (sexp, &algorithm, &is_private, NULL))
84 0 : g_assert_not_reached ();
85 :
86 5 : g_assert_cmpstr (comment, ==, COMMENT);
87 5 : g_assert_cmpint (algorithm, !=, 0);
88 5 : g_assert (!is_private);
89 :
90 5 : g_free (data);
91 5 : g_free (comment);
92 5 : gcry_sexp_release (sexp);
93 : }
94 1 : }
95 :
96 : static void
97 1 : test_parse_private (void)
98 : {
99 : gcry_sexp_t sexp;
100 : gchar *data;
101 : gsize n_data;
102 : int algorithm;
103 : gboolean is_private;
104 : GBytes *bytes;
105 : guint i;
106 : GkmDataResult res;
107 :
108 :
109 9 : for (i = 0; i < G_N_ELEMENTS (PRIVATE_FILES); ++i) {
110 :
111 8 : if (!g_file_get_contents (PRIVATE_FILES[i], &data, &n_data, NULL))
112 0 : g_assert_not_reached ();
113 :
114 8 : bytes = g_bytes_new_take (data, n_data);
115 8 : res = gkm_ssh_openssh_parse_private_key (bytes, "password", 8, &sexp);
116 8 : g_bytes_unref (bytes);
117 :
118 8 : if (res != GKM_DATA_SUCCESS) {
119 0 : g_warning ("couldn't parse private key: %s", PRIVATE_FILES[i]);
120 0 : g_assert_cmpint (res, ==, GKM_DATA_SUCCESS);
121 : }
122 :
123 8 : if (!gkm_sexp_parse_key (sexp, &algorithm, &is_private, NULL))
124 0 : g_assert_not_reached ();
125 :
126 8 : g_assert_cmpint (algorithm, !=, 0);
127 8 : g_assert (is_private);
128 :
129 8 : gcry_sexp_release (sexp);
130 : }
131 1 : }
132 :
133 : int
134 1 : main (int argc, char **argv)
135 : {
136 : #if !GLIB_CHECK_VERSION(2,35,0)
137 : g_type_init ();
138 : #endif
139 1 : g_test_init (&argc, &argv, NULL);
140 :
141 1 : g_test_add_func ("/ssh-store/openssh/parse_private", test_parse_private);
142 1 : g_test_add_func ("/ssh-store/openssh/parse_public", test_parse_public);
143 :
144 1 : return g_test_run ();
145 : }
|