File: extras.c

package info (click to toggle)
gnutls13 1.4.4-3%2Betch5
  • links: PTS
  • area: main
  • in suites: etch
  • size: 24,052 kB
  • ctags: 12,507
  • sloc: ansic: 94,474; xml: 17,016; sh: 9,652; perl: 628; makefile: 607; sed: 16
file content (228 lines) | stat: -rw-r--r-- 5,456 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Copyright (C) 2003, 2004, 2005 Free Software Foundation
 *
 * Author: Nikos Mavroyanopoulos
 *
 * This file is part of GNUTLS-EXTRA.
 *
 * GNUTLS-EXTRA is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * GNUTLS-EXTRA is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNUTLS-EXTRA; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 */

/* Functions on OpenPGP keyring and trustdb parsing
 */

#include <gnutls_int.h>
#include <gnutls_datum.h>
#include <gnutls_global.h>
#include <gnutls_errors.h>
#include <gnutls_openpgp.h>
#include <gnutls_num.h>
#include <openpgp.h>

/* Keyring stuff.
 */

/**
  * gnutls_openpgp_keyring_init - This function initializes a gnutls_openpgp_keyring_t structure
  * @keyring: The structure to be initialized
  *
  * This function will initialize an OpenPGP keyring structure. 
  *
  * Returns 0 on success.
  *
  **/
int
gnutls_openpgp_keyring_init (gnutls_openpgp_keyring_t * keyring)
{
  *keyring = gnutls_calloc (1, sizeof (gnutls_openpgp_keyring_int));

  if (*keyring)
    {
      return 0;			/* success */
    }
  return GNUTLS_E_MEMORY_ERROR;
}

/**
  * gnutls_openpgp_keyring_deinit - This function deinitializes memory used by a gnutls_openpgp_keyring_t structure
  * @keyring: The structure to be initialized
  *
  * This function will deinitialize a CRL structure. 
  *
  **/
void
gnutls_openpgp_keyring_deinit (gnutls_openpgp_keyring_t keyring)
{
  if (!keyring)
    return;

  if (keyring->hd)
    {
      cdk_free (keyring->hd);
      keyring->hd = NULL;
    }

  gnutls_free (keyring);
}

/**
 * gnutls_openpgp_keyring_check_id - Check if a key id exists in the keyring
 * @ring: holds the keyring to check against
 * @keyid: will hold the keyid to check for.
 * @flags: unused (should be 0)
 *
 * Check if a given key ID exists in the keyring.
 *
 * Returns 0 on success (if keyid exists) and a negative error code
 * on failure.
 */
int
gnutls_openpgp_keyring_check_id (gnutls_openpgp_keyring_t ring,
				 const unsigned char keyid[8],
				 unsigned int flags)
{
  int rc;
  cdk_pkt_pubkey_t sig_pk;
  uint32_t id[2];

  id[0] = _gnutls_read_uint32 (keyid);
  id[1] = _gnutls_read_uint32 (&keyid[4]);

  rc = cdk_keydb_get_pk (ring->hd, id, &sig_pk);
  if (!rc)
    return 0;
  else
    return GNUTLS_E_NO_CERTIFICATE_FOUND;
}

/**
  * gnutls_openpgp_keyring_import - This function will import a RAW or BASE64 encoded key
  * @keyring: The structure to store the parsed key.
  * @data: The RAW or BASE64 encoded keyring.
  * @format: One of gnutls_openpgp_keyring_fmt elements.
  *
  * This function will convert the given RAW or Base64 encoded keyring
  * to the native gnutls_openpgp_keyring_t format. The output will be stored in 'keyring'.
  *
  * Returns 0 on success.
  *
  **/
int
gnutls_openpgp_keyring_import (gnutls_openpgp_keyring_t keyring,
			       const gnutls_datum_t * data,
			       gnutls_openpgp_key_fmt_t format)
{
  int rc;
  keybox_blob *blob = NULL;


  blob = kbx_read_blob (data, 0);
  if (!blob)
    {
      gnutls_assert ();
      return GNUTLS_E_OPENPGP_KEYRING_ERROR;
    }

  keyring->hd = kbx_to_keydb (blob);
  if (!keyring->hd)
    {
      gnutls_assert ();
      rc = GNUTLS_E_OPENPGP_KEYRING_ERROR;
      goto leave;
    }

  rc = 0;

leave:
  kbx_blob_release (blob);
  return rc;
}


/* TrustDB stuff.
 */

/**
  * gnutls_openpgp_trustdb_init - This function initializes a gnutls_openpgp_trustdb_t structure
  * @trustdb: The structure to be initialized
  *
  * This function will initialize an OpenPGP trustdb structure. 
  *
  * Returns 0 on success.
  *
  **/
int
gnutls_openpgp_trustdb_init (gnutls_openpgp_trustdb_t * trustdb)
{
  *trustdb = gnutls_calloc (1, sizeof (gnutls_openpgp_trustdb_int));

  if (*trustdb)
    {
      return 0;			/* success */
    }
  return GNUTLS_E_MEMORY_ERROR;
}

/**
  * gnutls_openpgp_trustdb_deinit - This function deinitializes memory used by a gnutls_openpgp_trustdb_t structure
  * @trustdb: The structure to be initialized
  *
  * This function will deinitialize a CRL structure. 
  *
  **/
void
gnutls_openpgp_trustdb_deinit (gnutls_openpgp_trustdb_t trustdb)
{
  if (!trustdb)
    return;

  if (trustdb->st)
    {
      cdk_stream_close (trustdb->st);
      trustdb->st = NULL;
    }

  gnutls_free (trustdb);
}

/**
  * gnutls_openpgp_trustdb_import_file - This function will import a RAW or BASE64 encoded key
  * @trustdb: The structure to store the parsed key.
  * @file: The file that holds the trustdb.
  *
  * This function will convert the given RAW or Base64 encoded trustdb
  * to the native gnutls_openpgp_trustdb_t format. The output will be stored in 'trustdb'.
  *
  * Returns 0 on success.
  *
  **/
int
gnutls_openpgp_trustdb_import_file (gnutls_openpgp_trustdb_t trustdb,
				    const char *file)
{
  int rc;

  rc = cdk_stream_open (file, &trustdb->st);
  if (rc)
    {
      rc = _gnutls_map_cdk_rc (rc);
      gnutls_assert ();
      return rc;
    }

  return 0;
}