~ubuntu-branches/ubuntu/natty/gnome-keyring/natty

« back to all changes in this revision

Viewing changes to pkcs11/gck/tests/unit-test-sexp.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-02-16 19:00:06 UTC
  • mfrom: (1.1.58 upstream)
  • Revision ID: james.westby@ubuntu.com-20100216190006-cqpnic4zxlkmmi0o
Tags: 2.29.90git20100218-0ubuntu1
Updated to a git snapshot version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/* unit-test-sexp.c: Test sexp stuff
 
3
 
 
4
   Copyright (C) 2007 Stefan Walter
 
5
 
 
6
   The Gnome Keyring Library is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU Library General Public License as
 
8
   published by the Free Software Foundation; either version 2 of the
 
9
   License, or (at your option) any later version.
 
10
 
 
11
   The Gnome Keyring Library 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 GNU
 
14
   Library General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU Library General Public
 
17
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
 
18
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
   Boston, MA 02111-1307, USA.
 
20
 
 
21
   Author: Stef Walter <stef@memberwebs.com>
 
22
*/
 
23
 
 
24
#include <stdlib.h>
 
25
#include <stdio.h>
 
26
#include <string.h>
 
27
 
 
28
#include "run-auto-test.h"
 
29
 
 
30
#include "gck/gck-crypto.h"
 
31
#include "gck/gck-sexp.h"
 
32
 
 
33
#include <gcrypt.h>
 
34
 
 
35
#define TEST_RSA \
 
36
"(private-key (rsa " \
 
37
"(n  #00B78758D55EBFFAB61D07D0DC49B5309A6F1DA2AE51C275DFC2370959BB81AC0C39093B1C618E396161A0DECEB8768D0FFB14F197B96C3DA14190EE0F20D51315#)" \
 
38
"(e #010001#)" \
 
39
"(d #108BCAC5FDD35812981E6EC5957D98E2AB76E4064C47B861D27C2CC322C50792313C852B4164A035B42D261F1A09F9FFE8F477F9F78FF2EABBDA6BA875C671D7#)" \
 
40
"(p #00C357F11B19A18C66573D25D1E466D9AB8BCDDCDFE0B2E80BD46712C4BEC18EB7#)" \
 
41
"(q #00F0843B90A60EF7034CA4BE80414ED9497CABCC685143B388013FF989CBB0E093#)" \
 
42
"(u #12F2555F52EB56329A991CF0404B51C68AC921AD370A797860F550415FF987BD#)" \
 
43
"))"
 
44
 
 
45
#define TEST_DSA \
 
46
"(private-key (dsa " \
 
47
"  (p #0090EC0B60735839C754EAF8F64BB03FC35398D69772BFAE540079DEA2D3A61FAFFB27630A038A01A3D0CD62A10745A574A27ECB462F4F0885B79C61BBE954A60A29668AD54BBA5C07A72FD8B1105249670B339DF2C59E64A47064EFCF0B7236C5C72CD55CEB32917430BEC9A003D4E484FBAA84D79571B38D6B5AC95BB73E3F7B#)" \
 
48
"  (q #00FA214A1385C21BFEBAADAB240A2430C607D56271#)" \
 
49
"  (g #2DE05751F5DAEE97F3D43C54595A3E94A080728F0C66C98AEBED5762F6AB155802D8359EAD1DE1EC36A459FBEEEA48E59B9E6A8CB4F5295936B3CC881A5D957C7339175E2CFFE0F30D3711E430DB6648C2EB474AA10A4A3297450531FF2C7C6951220C9D446B6B6B0F00262E1EBEB3CC861476AA518CC555C9ABF9E5F39023FC#)" \
 
50
"  (y #54734451DB79D4EEDF0BBCEBD43BB6CBB7B8584603B957080075DD318EB5B0266D4B20DC5EFF376BDFC4EA2983B1F7F02A39ED4C619ED68712729FFF3B7C696ADD1B6D748F56A4B4BEC5C4385E528423A3B88AE65E6D5500F97839E7A486255982189C3B4FA8D94338C76F0E5CAFC9A30A1ED728BB9F2091D594E3250A09EA00#)" \
 
51
"  (x #00876F84F709D51108DFB0CBFA1F1C569C09C413EC#)))"
 
52
 
 
53
gcry_sexp_t rsakey = NULL;
 
54
gcry_sexp_t dsakey = NULL;
 
55
 
 
56
DEFINE_SETUP(crypto_setup)
 
57
{
 
58
        gcry_error_t gcry;
 
59
 
 
60
        gck_crypto_initialize ();
 
61
 
 
62
        gcry = gcry_sexp_new (&rsakey, TEST_RSA, strlen (TEST_RSA), 1);
 
63
        g_return_if_fail (gcry == 0);
 
64
        gcry = gcry_sexp_new (&dsakey, TEST_DSA, strlen (TEST_DSA), 1);
 
65
        g_return_if_fail (gcry == 0);
 
66
}
 
67
 
 
68
DEFINE_TEARDOWN(crypto_setup)
 
69
{
 
70
        gcry_sexp_release (rsakey);
 
71
        rsakey = NULL;
 
72
        gcry_sexp_release (dsakey);
 
73
        dsakey = NULL;
 
74
}
 
75
 
 
76
DEFINE_TEST(parse_key)
 
77
{
 
78
        gcry_sexp_t sexp = NULL;
 
79
        gcry_mpi_t mpi = NULL;
 
80
        gboolean ret;
 
81
        gboolean is_priv = FALSE;
 
82
        int algorithm = 0;
 
83
 
 
84
        /* Get the private key out */
 
85
        ret = gck_sexp_parse_key (rsakey, &algorithm, &is_priv, &sexp);
 
86
        g_assert (ret);
 
87
        g_assert (algorithm == GCRY_PK_RSA);
 
88
        g_assert (is_priv == TRUE);
 
89
        g_assert (sexp != NULL);
 
90
 
 
91
        ret = gck_sexp_extract_mpi (rsakey, &mpi, "p", NULL);
 
92
        g_assert (ret);
 
93
        g_assert (mpi != NULL);
 
94
}
 
95
 
 
96
DEFINE_TEST(sexp_key_to_public)
 
97
{
 
98
        gcry_sexp_t pubkey = NULL;
 
99
        guchar id1[20], id2[20];
 
100
        gboolean ret;
 
101
        guchar *p;
 
102
 
 
103
        /* RSA */
 
104
        ret = gck_sexp_key_to_public (rsakey, &pubkey);
 
105
        g_assert (ret);
 
106
        g_assert (pubkey != NULL);
 
107
 
 
108
        p = gcry_pk_get_keygrip (rsakey, id1);
 
109
        g_return_if_fail (p == id1);
 
110
        p = gcry_pk_get_keygrip (pubkey, id2);
 
111
        g_return_if_fail (p == id2);
 
112
 
 
113
        g_assert (memcmp (id1, id2, sizeof (id1)) == 0);
 
114
 
 
115
        gcry_sexp_release (pubkey);
 
116
 
 
117
 
 
118
        /* DSA */
 
119
        ret = gck_sexp_key_to_public (dsakey, &pubkey);
 
120
        g_assert (ret);
 
121
        g_assert (pubkey != NULL);
 
122
 
 
123
        p = gcry_pk_get_keygrip (dsakey, id1);
 
124
        g_return_if_fail (p == id1);
 
125
        p = gcry_pk_get_keygrip (pubkey, id2);
 
126
        g_return_if_fail (p == id2);
 
127
 
 
128
        g_assert (memcmp (id1, id2, sizeof (id1)) == 0);
 
129
 
 
130
        gcry_sexp_release (pubkey);
 
131
 
 
132
}