| 1 |
/* GnuTLS --- Guile bindings for GnuTLS. |
| 2 |
Copyright (C) 2007-2025 Free Software Foundation, Inc. |
| 3 |
Copyright (C) 2023 David Thompson <[email protected]> |
| 4 |
|
| 5 |
This file is part of Guile-GnuTLS. |
| 6 |
|
| 7 |
This library is free software; you can redistribute it and/or |
| 8 |
modify it under the terms of the GNU Lesser General Public |
| 9 |
License as published by the Free Software Foundation; either |
| 10 |
version 2.1 of the License, or (at your option) any later version. |
| 11 |
|
| 12 |
This library is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
Lesser General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU Lesser General Public License |
| 18 |
along with this library; if not, see <https://www.gnu.org/licenses/>. */ |
| 19 |
|
| 20 |
/* Written by Ludovic Courtès <[email protected]>. */ |
| 21 |
|
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
# include <config.h> |
| 24 |
# if SIZEOF_TIME_T == 8 |
| 25 |
# define scm_to_time_t scm_to_int64 |
| 26 |
# define scm_from_time_t scm_from_int64 |
| 27 |
# elif SIZEOF_TIME_T == 4 |
| 28 |
# define scm_to_time_t scm_to_int32 |
| 29 |
# define scm_from_time_t scm_from_int32 |
| 30 |
# else |
| 31 |
# error sizeof(time_t) is not 4 or 8. |
| 32 |
# endif |
| 33 |
#endif |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <stdint.h> |
| 37 |
#include <string.h> |
| 38 |
#include <gnutls/gnutls.h> |
| 39 |
#include <gnutls/openpgp.h> |
| 40 |
#include <gnutls/crypto.h> |
| 41 |
#include <gnutls/abstract.h> |
| 42 |
#include <libguile.h> |
| 43 |
|
| 44 |
#include <stdlib.h> |
| 45 |
#include <stddef.h> |
| 46 |
#ifdef HAVE_ALLOCA_H |
| 47 |
# include <alloca.h> |
| 48 |
#endif |
| 49 |
#include <assert.h> |
| 50 |
|
| 51 |
|
| 52 |
/* The gnutls API does not let you find the mac algorithm from a given hmac or |
| 53 |
hash state. This is required to know the size of the output to allocate. So |
| 54 |
we have to remember it as well. */ |
| 55 |
|
| 56 |
struct scm_gnutls_hmac_and_algorithm |
| 57 |
{ |
| 58 |
gnutls_hmac_hd_t handle; |
| 59 |
gnutls_mac_algorithm_t algorithm; |
| 60 |
}; |
| 61 |
|
| 62 |
struct scm_gnutls_hash_and_algorithm |
| 63 |
{ |
| 64 |
gnutls_hash_hd_t handle; |
| 65 |
gnutls_digest_algorithm_t algorithm; |
| 66 |
}; |
| 67 |
|
| 68 |
struct scm_gnutls_aead_cipher_and_algorithm |
| 69 |
{ |
| 70 |
gnutls_aead_cipher_hd_t handle; |
| 71 |
gnutls_cipher_algorithm_t algorithm; |
| 72 |
}; |
| 73 |
|
| 74 |
struct scm_gnutls_cipher_and_algorithm |
| 75 |
{ |
| 76 |
gnutls_cipher_hd_t handle; |
| 77 |
gnutls_cipher_algorithm_t algorithm; |
| 78 |
}; |
| 79 |
|
| 80 |
typedef struct scm_gnutls_hmac_and_algorithm *scm_gnutls_hmac_and_algorithm_t; |
| 81 |
typedef struct scm_gnutls_hash_and_algorithm *scm_gnutls_hash_and_algorithm_t; |
| 82 |
typedef struct scm_gnutls_aead_cipher_and_algorithm |
| 83 |
*scm_gnutls_aead_cipher_and_algorithm_t; |
| 84 |
typedef struct scm_gnutls_cipher_and_algorithm |
| 85 |
*scm_gnutls_cipher_and_algorithm_t; |
| 86 |
|
| 87 |
static void hmac_and_algorithm_deinit (scm_gnutls_hmac_and_algorithm_t |
| 88 |
c_hmac); |
| 89 |
static void hash_and_algorithm_deinit (scm_gnutls_hash_and_algorithm_t |
| 90 |
c_hash); |
| 91 |
static void |
| 92 |
aead_cipher_and_algorithm_deinit (scm_gnutls_aead_cipher_and_algorithm_t |
| 93 |
c_aead); |
| 94 |
|
| 95 |
static void cipher_and_algorithm_deinit (scm_gnutls_cipher_and_algorithm_t |
| 96 |
c_hash); |
| 97 |
|
| 98 |
#include "enums.h" |
| 99 |
#include "smobs.h" |
| 100 |
#include "errors.h" |
| 101 |
#include "utils.h" |
| 102 |
|
| 103 |
|
| 104 |
#ifndef HAVE_SCM_GC_MALLOC_POINTERLESS |
| 105 |
# define scm_gc_malloc_pointerless scm_gc_malloc |
| 106 |
#endif |
| 107 |
|
| 108 |
/* Maximum size allowed for 'alloca'. */ |
| 109 |
#define ALLOCA_MAX_SIZE 1024U |
| 110 |
|
| 111 |
/* Allocate SIZE bytes, either on the C stack or on the GC-managed heap. */ |
| 112 |
#define FAST_ALLOC(size) \ |
| 113 |
(((size) <= ALLOCA_MAX_SIZE) \ |
| 114 |
? alloca (size) \ |
| 115 |
: scm_gc_malloc_pointerless ((size), "gnutls-alloc")) |
| 116 |
|
| 117 |
/* Maximum size, in bytes, of the hash data returned by a digest algorithm. */ |
| 118 |
#define MAX_HASH_SIZE 64 |
| 119 |
|
| 120 |
/* SMOB and enums type definitions. */ |
| 121 |
#include "enum-map.i.c" |
| 122 |
#include "smob-types.i.c" |
| 123 |
|
| 124 |
const char scm_gnutls_array_error_message[] = |
| 125 |
"cannot handle non-contiguous array: ~A"; |
| 126 |
|
| 127 |
/* Data that are attached to `gnutls_session_t' objects. |
| 128 |
|
| 129 |
We need to keep several pieces of information along with each session: |
| 130 |
|
| 131 |
- A boolean indicating whether its underlying transport is a file |
| 132 |
descriptor or Scheme port. This is used to decide whether to leave |
| 133 |
"Guile mode" when invoking `gnutls_record_recv ()'. |
| 134 |
|
| 135 |
- The record port attached to the session (returned by |
| 136 |
`session-record-port'). This is so that several calls to |
| 137 |
`session-record-port' return the same port. |
| 138 |
|
| 139 |
Currently, this information is maintained into a pair. The whole pair is |
| 140 |
marked by the session mark procedure. */ |
| 141 |
|
| 142 |
#define SCM_GNUTLS_MAKE_SESSION_DATA() \ |
| 143 |
scm_cons (SCM_BOOL_F, SCM_BOOL_F) |
| 144 |
#define SCM_GNUTLS_SET_SESSION_DATA(c_session, data) \ |
| 145 |
gnutls_session_set_ptr (c_session, (void *) SCM_UNPACK (data)) |
| 146 |
#define SCM_GNUTLS_SESSION_DATA(c_session) \ |
| 147 |
SCM_PACK ((scm_t_bits) gnutls_session_get_ptr (c_session)) |
| 148 |
|
| 149 |
#define SCM_GNUTLS_SET_SESSION_TRANSPORT_IS_FD(c_session, c_is_fd) \ |
| 150 |
SCM_SETCAR (SCM_GNUTLS_SESSION_DATA (c_session), \ |
| 151 |
scm_from_bool (c_is_fd)) |
| 152 |
#define SCM_GNUTLS_SET_SESSION_RECORD_PORT(c_session, port) \ |
| 153 |
SCM_SETCDR (SCM_GNUTLS_SESSION_DATA (c_session), port) |
| 154 |
|
| 155 |
#define SCM_GNUTLS_SESSION_TRANSPORT_IS_FD(c_session) \ |
| 156 |
scm_to_bool (SCM_CAR (SCM_GNUTLS_SESSION_DATA (c_session))) |
| 157 |
#define SCM_GNUTLS_SESSION_RECORD_PORT(c_session) \ |
| 158 |
SCM_CDR (SCM_GNUTLS_SESSION_DATA (c_session)) |
| 159 |
|
| 160 |
|
| 161 |
/* Weak-key hash table. */ |
| 162 |
static SCM weak_refs; |
| 163 |
|
| 164 |
/* Register a weak reference from @FROM to @TO, such that the lifetime of TO is |
| 165 |
greater than or equal to that of FROM. TO is added to the list of weak |
| 166 |
references of FROM. */ |
| 167 |
static void |
| 168 |
register_weak_reference (SCM from, SCM to) |
| 169 |
{ |
| 170 |
SCM refs = scm_cons (to, scm_hashq_ref (weak_refs, from, SCM_EOL)); |
| 171 |
scm_hashq_set_x (weak_refs, from, refs); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
/* Bindings. */ |
| 178 |
|
| 179 |
/* Mark the data associated with SESSION. */ |
| 180 |
SCM_SMOB_MARK (scm_tc16_gnutls_session, mark_session, session) |
| 181 |
{ |
| 182 |
gnutls_session_t c_session; |
| 183 |
|
| 184 |
c_session = scm_to_gnutls_session (session, 1, "mark_session"); |
| 185 |
|
| 186 |
return (SCM_GNUTLS_SESSION_DATA (c_session)); |
| 187 |
} |
| 188 |
|
| 189 |
SCM_DEFINE (scm_gnutls_version, "gnutls-version", 0, 0, 0, |
| 190 |
(void), |
| 191 |
"Return a string denoting the version number of the underlying " |
| 192 |
"GnuTLS library, e.g., @code{\"1.7.2\"}.") |
| 193 |
#define FUNC_NAME s_scm_gnutls_version |
| 194 |
{ |
| 195 |
return (scm_from_locale_string (gnutls_check_version (NULL))); |
| 196 |
} |
| 197 |
|
| 198 |
#undef FUNC_NAME |
| 199 |
|
| 200 |
SCM_DEFINE (scm_gnutls_make_session, "make-session", 1, 0, 1, |
| 201 |
(SCM end, SCM flags), |
| 202 |
"Return a new session for connection end @var{end}, either " |
| 203 |
"@code{connection-end/server} or @code{connection-end/client}. " |
| 204 |
"The optional @var{flags} arguments are @code{connection-flag} " |
| 205 |
"values such as @code{connection-flag/auto-reauth}.") |
| 206 |
#define FUNC_NAME s_scm_gnutls_make_session |
| 207 |
{ |
| 208 |
int err, i; |
| 209 |
gnutls_session_t c_session; |
| 210 |
gnutls_connection_end_t c_end; |
| 211 |
gnutls_init_flags_t c_flags = 0; |
| 212 |
SCM session_data; |
| 213 |
|
| 214 |
c_end = scm_to_gnutls_connection_end (end, 1, FUNC_NAME); |
| 215 |
|
| 216 |
session_data = SCM_GNUTLS_MAKE_SESSION_DATA (); |
| 217 |
for (i = 2; scm_is_pair (flags); flags = scm_cdr (flags), i++) |
| 218 |
c_flags |= scm_to_gnutls_connection_flag (scm_car (flags), i, FUNC_NAME); |
| 219 |
|
| 220 |
err = gnutls_init (&c_session, c_end | c_flags); |
| 221 |
|
| 222 |
if (EXPECT_FALSE (err)) |
| 223 |
scm_gnutls_error (err, FUNC_NAME); |
| 224 |
|
| 225 |
SCM_GNUTLS_SET_SESSION_DATA (c_session, session_data); |
| 226 |
|
| 227 |
return (scm_from_gnutls_session (c_session)); |
| 228 |
} |
| 229 |
|
| 230 |
#undef FUNC_NAME |
| 231 |
|
| 232 |
SCM_DEFINE (scm_gnutls_bye, "bye", 2, 0, 0, |
| 233 |
(SCM session, SCM how), |
| 234 |
"Close @var{session} according to @var{how}.") |
| 235 |
#define FUNC_NAME s_scm_gnutls_bye |
| 236 |
{ |
| 237 |
int err; |
| 238 |
gnutls_session_t c_session; |
| 239 |
gnutls_close_request_t c_how; |
| 240 |
|
| 241 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 242 |
c_how = scm_to_gnutls_close_request (how, 2, FUNC_NAME); |
| 243 |
|
| 244 |
err = gnutls_bye (c_session, c_how); |
| 245 |
if (EXPECT_FALSE (err)) |
| 246 |
scm_gnutls_error (err, FUNC_NAME); |
| 247 |
|
| 248 |
return SCM_UNSPECIFIED; |
| 249 |
} |
| 250 |
|
| 251 |
#undef FUNC_NAME |
| 252 |
|
| 253 |
SCM_DEFINE (scm_gnutls_handshake, "handshake", 1, 0, 0, |
| 254 |
(SCM session), "Perform a handshake for @var{session}.") |
| 255 |
#define FUNC_NAME s_scm_gnutls_handshake |
| 256 |
{ |
| 257 |
int err; |
| 258 |
gnutls_session_t c_session; |
| 259 |
|
| 260 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 261 |
|
| 262 |
err = gnutls_handshake (c_session); |
| 263 |
if (EXPECT_FALSE (err)) |
| 264 |
scm_gnutls_error (err, FUNC_NAME); |
| 265 |
|
| 266 |
return SCM_UNSPECIFIED; |
| 267 |
} |
| 268 |
|
| 269 |
#undef FUNC_NAME |
| 270 |
|
| 271 |
SCM_DEFINE (scm_gnutls_rehandshake, "rehandshake", 1, 0, 0, |
| 272 |
(SCM session), "Perform a re-handshaking for @var{session}.") |
| 273 |
#define FUNC_NAME s_scm_gnutls_rehandshake |
| 274 |
{ |
| 275 |
int err; |
| 276 |
gnutls_session_t c_session; |
| 277 |
|
| 278 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 279 |
|
| 280 |
err = gnutls_rehandshake (c_session); |
| 281 |
if (EXPECT_FALSE (err)) |
| 282 |
scm_gnutls_error (err, FUNC_NAME); |
| 283 |
|
| 284 |
return SCM_UNSPECIFIED; |
| 285 |
} |
| 286 |
|
| 287 |
#undef FUNC_NAME |
| 288 |
|
| 289 |
SCM_DEFINE (scm_gnutls_reauthenticate, "reauthenticate", 1, 0, 0, |
| 290 |
(SCM session), |
| 291 |
"Perform a re-authentication step for @var{session}.") |
| 292 |
#define FUNC_NAME s_scm_gnutls_reauthenticate |
| 293 |
{ |
| 294 |
int err; |
| 295 |
gnutls_session_t c_session; |
| 296 |
|
| 297 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 298 |
|
| 299 |
/* FIXME: Allow flags as an argument. */ |
| 300 |
err = gnutls_reauth (c_session, 0); |
| 301 |
if (EXPECT_FALSE (err)) |
| 302 |
scm_gnutls_error (err, FUNC_NAME); |
| 303 |
|
| 304 |
return SCM_UNSPECIFIED; |
| 305 |
} |
| 306 |
|
| 307 |
#undef FUNC_NAME |
| 308 |
|
| 309 |
SCM_DEFINE (scm_gnutls_alert_get, "alert-get", 1, 0, 0, |
| 310 |
(SCM session), "Get an aleter from @var{session}.") |
| 311 |
#define FUNC_NAME s_scm_gnutls_alert_get |
| 312 |
{ |
| 313 |
gnutls_session_t c_session; |
| 314 |
gnutls_alert_description_t c_alert; |
| 315 |
|
| 316 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 317 |
|
| 318 |
c_alert = gnutls_alert_get (c_session); |
| 319 |
|
| 320 |
return (scm_from_gnutls_alert_description (c_alert)); |
| 321 |
} |
| 322 |
|
| 323 |
#undef FUNC_NAME |
| 324 |
|
| 325 |
SCM_DEFINE (scm_gnutls_alert_send, "alert-send", 3, 0, 0, |
| 326 |
(SCM session, SCM level, SCM alert), |
| 327 |
"Send @var{alert} via @var{session}.") |
| 328 |
#define FUNC_NAME s_scm_gnutls_alert_send |
| 329 |
{ |
| 330 |
int err; |
| 331 |
gnutls_session_t c_session; |
| 332 |
gnutls_alert_level_t c_level; |
| 333 |
gnutls_alert_description_t c_alert; |
| 334 |
|
| 335 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 336 |
c_level = scm_to_gnutls_alert_level (level, 2, FUNC_NAME); |
| 337 |
c_alert = scm_to_gnutls_alert_description (alert, 3, FUNC_NAME); |
| 338 |
|
| 339 |
err = gnutls_alert_send (c_session, c_level, c_alert); |
| 340 |
if (EXPECT_FALSE (err)) |
| 341 |
scm_gnutls_error (err, FUNC_NAME); |
| 342 |
|
| 343 |
return SCM_UNSPECIFIED; |
| 344 |
} |
| 345 |
|
| 346 |
#undef FUNC_NAME |
| 347 |
|
| 348 |
/* FIXME: Omitting `alert-send-appropriate'. */ |
| 349 |
|
| 350 |
|
| 351 |
/* Session accessors. */ |
| 352 |
|
| 353 |
SCM_DEFINE (scm_gnutls_session_cipher, "session-cipher", 1, 0, 0, |
| 354 |
(SCM session), "Return @var{session}'s cipher.") |
| 355 |
#define FUNC_NAME s_scm_gnutls_session_cipher |
| 356 |
{ |
| 357 |
gnutls_session_t c_session; |
| 358 |
gnutls_cipher_algorithm_t c_cipher; |
| 359 |
|
| 360 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 361 |
|
| 362 |
c_cipher = gnutls_cipher_get (c_session); |
| 363 |
|
| 364 |
return (scm_from_gnutls_cipher (c_cipher)); |
| 365 |
} |
| 366 |
|
| 367 |
#undef FUNC_NAME |
| 368 |
|
| 369 |
SCM_DEFINE (scm_gnutls_session_kx, "session-kx", 1, 0, 0, |
| 370 |
(SCM session), "Return @var{session}'s kx.") |
| 371 |
#define FUNC_NAME s_scm_gnutls_session_kx |
| 372 |
{ |
| 373 |
gnutls_session_t c_session; |
| 374 |
gnutls_kx_algorithm_t c_kx; |
| 375 |
|
| 376 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 377 |
|
| 378 |
c_kx = gnutls_kx_get (c_session); |
| 379 |
|
| 380 |
return (scm_from_gnutls_kx (c_kx)); |
| 381 |
} |
| 382 |
|
| 383 |
#undef FUNC_NAME |
| 384 |
|
| 385 |
SCM_DEFINE (scm_gnutls_session_mac, "session-mac", 1, 0, 0, |
| 386 |
(SCM session), "Return @var{session}'s MAC.") |
| 387 |
#define FUNC_NAME s_scm_gnutls_session_mac |
| 388 |
{ |
| 389 |
gnutls_session_t c_session; |
| 390 |
gnutls_mac_algorithm_t c_mac; |
| 391 |
|
| 392 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 393 |
|
| 394 |
c_mac = gnutls_mac_get (c_session); |
| 395 |
|
| 396 |
return (scm_from_gnutls_mac (c_mac)); |
| 397 |
} |
| 398 |
|
| 399 |
#undef FUNC_NAME |
| 400 |
|
| 401 |
SCM_DEFINE (scm_gnutls_session_compression_method, |
| 402 |
"session-compression-method", 1, 0, 0, |
| 403 |
(SCM session), "Return @var{session}'s compression method.") |
| 404 |
#define FUNC_NAME s_scm_gnutls_session_compression_method |
| 405 |
{ |
| 406 |
gnutls_session_t c_session; |
| 407 |
gnutls_compression_method_t c_comp; |
| 408 |
|
| 409 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 410 |
|
| 411 |
c_comp = gnutls_compression_get (c_session); |
| 412 |
|
| 413 |
return (scm_from_gnutls_compression_method (c_comp)); |
| 414 |
} |
| 415 |
|
| 416 |
#undef FUNC_NAME |
| 417 |
|
| 418 |
SCM_DEFINE (scm_gnutls_session_certificate_type, |
| 419 |
"session-certificate-type", 1, 0, 0, |
| 420 |
(SCM session), "Return @var{session}'s certificate type.") |
| 421 |
#define FUNC_NAME s_scm_gnutls_session_certificate_type |
| 422 |
{ |
| 423 |
gnutls_session_t c_session; |
| 424 |
gnutls_certificate_type_t c_cert; |
| 425 |
|
| 426 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 427 |
|
| 428 |
c_cert = gnutls_certificate_type_get (c_session); |
| 429 |
|
| 430 |
return (scm_from_gnutls_certificate_type (c_cert)); |
| 431 |
} |
| 432 |
|
| 433 |
#undef FUNC_NAME |
| 434 |
|
| 435 |
SCM_DEFINE (scm_gnutls_session_protocol, "session-protocol", 1, 0, 0, |
| 436 |
(SCM session), "Return the protocol used by @var{session}.") |
| 437 |
#define FUNC_NAME s_scm_gnutls_session_protocol |
| 438 |
{ |
| 439 |
gnutls_session_t c_session; |
| 440 |
gnutls_protocol_t c_protocol; |
| 441 |
|
| 442 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 443 |
|
| 444 |
c_protocol = gnutls_protocol_get_version (c_session); |
| 445 |
|
| 446 |
return (scm_from_gnutls_protocol (c_protocol)); |
| 447 |
} |
| 448 |
|
| 449 |
#undef FUNC_NAME |
| 450 |
|
| 451 |
SCM_DEFINE (scm_gnutls_session_authentication_type, |
| 452 |
"session-authentication-type", |
| 453 |
1, 0, 0, |
| 454 |
(SCM session), |
| 455 |
"Return the authentication type (a @code{credential-type} value) " |
| 456 |
"used by @var{session}.") |
| 457 |
#define FUNC_NAME s_scm_gnutls_session_authentication_type |
| 458 |
{ |
| 459 |
gnutls_session_t c_session; |
| 460 |
gnutls_credentials_type_t c_auth; |
| 461 |
|
| 462 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 463 |
|
| 464 |
c_auth = gnutls_auth_get_type (c_session); |
| 465 |
|
| 466 |
return (scm_from_gnutls_credentials (c_auth)); |
| 467 |
} |
| 468 |
|
| 469 |
#undef FUNC_NAME |
| 470 |
|
| 471 |
SCM_DEFINE (scm_gnutls_session_server_authentication_type, |
| 472 |
"session-server-authentication-type", |
| 473 |
1, 0, 0, |
| 474 |
(SCM session), |
| 475 |
"Return the server authentication type (a " |
| 476 |
"@code{credential-type} value) used in @var{session}.") |
| 477 |
#define FUNC_NAME s_scm_gnutls_session_server_authentication_type |
| 478 |
{ |
| 479 |
gnutls_session_t c_session; |
| 480 |
gnutls_credentials_type_t c_auth; |
| 481 |
|
| 482 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 483 |
|
| 484 |
c_auth = gnutls_auth_server_get_type (c_session); |
| 485 |
|
| 486 |
return (scm_from_gnutls_credentials (c_auth)); |
| 487 |
} |
| 488 |
|
| 489 |
#undef FUNC_NAME |
| 490 |
|
| 491 |
SCM_DEFINE (scm_gnutls_session_client_authentication_type, |
| 492 |
"session-client-authentication-type", |
| 493 |
1, 0, 0, |
| 494 |
(SCM session), |
| 495 |
"Return the client authentication type (a " |
| 496 |
"@code{credential-type} value) used in @var{session}.") |
| 497 |
#define FUNC_NAME s_scm_gnutls_session_client_authentication_type |
| 498 |
{ |
| 499 |
gnutls_session_t c_session; |
| 500 |
gnutls_credentials_type_t c_auth; |
| 501 |
|
| 502 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 503 |
|
| 504 |
c_auth = gnutls_auth_client_get_type (c_session); |
| 505 |
|
| 506 |
return (scm_from_gnutls_credentials (c_auth)); |
| 507 |
} |
| 508 |
|
| 509 |
#undef FUNC_NAME |
| 510 |
|
| 511 |
SCM_DEFINE (scm_gnutls_session_peer_certificate_chain, |
| 512 |
"session-peer-certificate-chain", |
| 513 |
1, 0, 0, |
| 514 |
(SCM session), |
| 515 |
"Return the a list of certificates in raw format (u8vectors) " |
| 516 |
"where the first one is the peer's certificate. In the case " |
| 517 |
"of OpenPGP, there is always exactly one certificate. In the " |
| 518 |
"case of X.509, subsequent certificates indicate form a " |
| 519 |
"certificate chain. Return the empty list if no certificate " |
| 520 |
"was sent.") |
| 521 |
#define FUNC_NAME s_scm_gnutls_session_peer_certificate_chain |
| 522 |
{ |
| 523 |
SCM result; |
| 524 |
gnutls_session_t c_session; |
| 525 |
const gnutls_datum_t *c_cert; |
| 526 |
unsigned int c_list_size; |
| 527 |
|
| 528 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 529 |
|
| 530 |
c_cert = gnutls_certificate_get_peers (c_session, &c_list_size); |
| 531 |
|
| 532 |
if (EXPECT_FALSE (c_cert == NULL)) |
| 533 |
result = SCM_EOL; |
| 534 |
else |
| 535 |
{ |
| 536 |
SCM pair; |
| 537 |
unsigned int i; |
| 538 |
|
| 539 |
result = scm_make_list (scm_from_uint (c_list_size), SCM_UNSPECIFIED); |
| 540 |
|
| 541 |
for (i = 0, pair = result; i < c_list_size; i++, pair = SCM_CDR (pair)) |
| 542 |
{ |
| 543 |
unsigned char *c_cert_copy; |
| 544 |
|
| 545 |
c_cert_copy = (unsigned char *) malloc (c_cert[i].size); |
| 546 |
if (EXPECT_FALSE (c_cert_copy == NULL)) |
| 547 |
scm_gnutls_error (GNUTLS_E_MEMORY_ERROR, FUNC_NAME); |
| 548 |
|
| 549 |
memcpy (c_cert_copy, c_cert[i].data, c_cert[i].size); |
| 550 |
|
| 551 |
SCM_SETCAR (pair, scm_take_u8vector (c_cert_copy, c_cert[i].size)); |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
return result; |
| 556 |
} |
| 557 |
|
| 558 |
#undef FUNC_NAME |
| 559 |
|
| 560 |
SCM_DEFINE (scm_gnutls_session_our_certificate_chain, |
| 561 |
"session-our-certificate-chain", |
| 562 |
1, 0, 0, |
| 563 |
(SCM session), |
| 564 |
"Return our certificate chain for @var{session} (as sent to " |
| 565 |
"the peer) in raw format (a u8vector). In the case of OpenPGP " |
| 566 |
"there is exactly one certificate. Return the empty list " |
| 567 |
"if no certificate was used.") |
| 568 |
#define FUNC_NAME s_scm_gnutls_session_our_certificate_chain |
| 569 |
{ |
| 570 |
SCM result; |
| 571 |
gnutls_session_t c_session; |
| 572 |
const gnutls_datum_t *c_cert; |
| 573 |
unsigned char *c_cert_copy; |
| 574 |
|
| 575 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 576 |
|
| 577 |
/* XXX: Currently, the C function actually returns only one certificate. |
| 578 |
Future versions of the API may provide the full certificate chain, as |
| 579 |
for `gnutls_certificate_get_peers ()'. */ |
| 580 |
c_cert = gnutls_certificate_get_ours (c_session); |
| 581 |
|
| 582 |
if (EXPECT_FALSE (c_cert == NULL)) |
| 583 |
result = SCM_EOL; |
| 584 |
else |
| 585 |
{ |
| 586 |
c_cert_copy = (unsigned char *) malloc (c_cert->size); |
| 587 |
if (EXPECT_FALSE (c_cert_copy == NULL)) |
| 588 |
scm_gnutls_error (GNUTLS_E_MEMORY_ERROR, FUNC_NAME); |
| 589 |
|
| 590 |
memcpy (c_cert_copy, c_cert->data, c_cert->size); |
| 591 |
|
| 592 |
result = scm_list_1 (scm_take_u8vector (c_cert_copy, c_cert->size)); |
| 593 |
} |
| 594 |
|
| 595 |
return result; |
| 596 |
} |
| 597 |
|
| 598 |
#undef FUNC_NAME |
| 599 |
|
| 600 |
SCM_DEFINE (scm_gnutls_set_server_session_certificate_request_x, |
| 601 |
"set-server-session-certificate-request!", |
| 602 |
2, 0, 0, |
| 603 |
(SCM session, SCM request), |
| 604 |
"Tell how @var{session}, a server-side session, should deal " |
| 605 |
"with certificate requests. @var{request} should be either " |
| 606 |
"@code{certificate-request/request} or " |
| 607 |
"@code{certificate-request/require}.") |
| 608 |
#define FUNC_NAME s_scm_gnutls_set_server_session_certificate_request_x |
| 609 |
{ |
| 610 |
gnutls_session_t c_session; |
| 611 |
gnutls_certificate_request_t c_request; |
| 612 |
|
| 613 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 614 |
c_request = scm_to_gnutls_certificate_request (request, 2, FUNC_NAME); |
| 615 |
|
| 616 |
gnutls_certificate_server_set_request (c_session, c_request); |
| 617 |
|
| 618 |
return SCM_UNSPECIFIED; |
| 619 |
} |
| 620 |
|
| 621 |
#undef FUNC_NAME |
| 622 |
|
| 623 |
|
| 624 |
/* Choice of a protocol and cipher suite. */ |
| 625 |
|
| 626 |
SCM_DEFINE (scm_gnutls_set_default_priority_x, |
| 627 |
"set-session-default-priority!", 1, 0, 0, |
| 628 |
(SCM session), "Have @var{session} use the default priorities.") |
| 629 |
#define FUNC_NAME s_scm_gnutls_set_default_priority_x |
| 630 |
{ |
| 631 |
gnutls_session_t c_session; |
| 632 |
|
| 633 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 634 |
gnutls_set_default_priority (c_session); |
| 635 |
|
| 636 |
return SCM_UNSPECIFIED; |
| 637 |
} |
| 638 |
|
| 639 |
#undef FUNC_NAME |
| 640 |
|
| 641 |
SCM_DEFINE (scm_gnutls_set_session_priorities_x, |
| 642 |
"set-session-priorities!", 2, 0, 0, |
| 643 |
(SCM session, SCM priorities), |
| 644 |
"Have @var{session} use the given @var{priorities} for " |
| 645 |
"the ciphers, key exchange methods, MACs and compression " |
| 646 |
"methods. @var{priorities} must be a string (@pxref{" |
| 647 |
"Priority Strings,,, gnutls, GnuTLS@comma{} Transport Layer " |
| 648 |
"Security Library for the GNU system}). When @var{priorities} " |
| 649 |
"cannot be parsed, an @code{error/invalid-request} error " |
| 650 |
"is raised, with an extra argument indication the position " |
| 651 |
"of the error.\n") |
| 652 |
#define FUNC_NAME s_scm_gnutls_set_session_priorities_x |
| 653 |
{ |
| 654 |
int err; |
| 655 |
char *c_priorities; |
| 656 |
const char *err_pos; |
| 657 |
gnutls_session_t c_session; |
| 658 |
size_t pos; |
| 659 |
|
| 660 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 661 |
c_priorities = scm_to_locale_string (priorities); /* XXX: to_latin1_string */ |
| 662 |
|
| 663 |
err = gnutls_priority_set_direct (c_session, c_priorities, &err_pos); |
| 664 |
if (err == GNUTLS_E_INVALID_REQUEST) |
| 665 |
pos = err_pos - c_priorities; |
| 666 |
|
| 667 |
free (c_priorities); |
| 668 |
|
| 669 |
switch (err) |
| 670 |
{ |
| 671 |
case GNUTLS_E_SUCCESS: |
| 672 |
break; |
| 673 |
case GNUTLS_E_INVALID_REQUEST: |
| 674 |
{ |
| 675 |
scm_gnutls_error_with_args (err, FUNC_NAME, |
| 676 |
scm_list_1 (scm_from_size_t (pos))); |
| 677 |
break; |
| 678 |
} |
| 679 |
default: |
| 680 |
scm_gnutls_error (err, FUNC_NAME); |
| 681 |
} |
| 682 |
|
| 683 |
return SCM_UNSPECIFIED; |
| 684 |
} |
| 685 |
|
| 686 |
#undef FUNC_NAME |
| 687 |
|
| 688 |
SCM_DEFINE (scm_gnutls_cipher_suite_to_string, "cipher-suite->string", |
| 689 |
3, 0, 0, |
| 690 |
(SCM kx, SCM cipher, SCM mac), |
| 691 |
"Return the name of the given cipher suite.") |
| 692 |
#define FUNC_NAME s_scm_gnutls_cipher_suite_to_string |
| 693 |
{ |
| 694 |
gnutls_kx_algorithm_t c_kx; |
| 695 |
gnutls_cipher_algorithm_t c_cipher; |
| 696 |
gnutls_mac_algorithm_t c_mac; |
| 697 |
const char *c_name; |
| 698 |
|
| 699 |
c_kx = scm_to_gnutls_kx (kx, 1, FUNC_NAME); |
| 700 |
c_cipher = scm_to_gnutls_cipher (cipher, 2, FUNC_NAME); |
| 701 |
c_mac = scm_to_gnutls_mac (mac, 3, FUNC_NAME); |
| 702 |
|
| 703 |
c_name = gnutls_cipher_suite_get_name (c_kx, c_cipher, c_mac); |
| 704 |
|
| 705 |
return (scm_from_locale_string (c_name)); |
| 706 |
} |
| 707 |
|
| 708 |
#undef FUNC_NAME |
| 709 |
|
| 710 |
SCM_DEFINE (scm_gnutls_set_session_credentials_x, "set-session-credentials!", |
| 711 |
2, 0, 0, |
| 712 |
(SCM session, SCM cred), |
| 713 |
"Use @var{cred} as @var{session}'s credentials.") |
| 714 |
#define FUNC_NAME s_scm_gnutls_set_session_credentials_x |
| 715 |
{ |
| 716 |
int err = 0; |
| 717 |
gnutls_session_t c_session; |
| 718 |
|
| 719 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 720 |
|
| 721 |
if (SCM_SMOB_PREDICATE (scm_tc16_gnutls_certificate_credentials, cred)) |
| 722 |
{ |
| 723 |
gnutls_certificate_credentials_t c_cred; |
| 724 |
|
| 725 |
c_cred = scm_to_gnutls_certificate_credentials (cred, 2, FUNC_NAME); |
| 726 |
err = |
| 727 |
gnutls_credentials_set (c_session, GNUTLS_CRD_CERTIFICATE, c_cred); |
| 728 |
} |
| 729 |
else |
| 730 |
if (SCM_SMOB_PREDICATE |
| 731 |
(scm_tc16_gnutls_anonymous_client_credentials, cred)) |
| 732 |
{ |
| 733 |
gnutls_anon_client_credentials_t c_cred; |
| 734 |
|
| 735 |
c_cred = scm_to_gnutls_anonymous_client_credentials (cred, 2, |
| 736 |
FUNC_NAME); |
| 737 |
err = gnutls_credentials_set (c_session, GNUTLS_CRD_ANON, c_cred); |
| 738 |
} |
| 739 |
else if (SCM_SMOB_PREDICATE (scm_tc16_gnutls_anonymous_server_credentials, |
| 740 |
cred)) |
| 741 |
{ |
| 742 |
gnutls_anon_server_credentials_t c_cred; |
| 743 |
|
| 744 |
c_cred = scm_to_gnutls_anonymous_server_credentials (cred, 2, |
| 745 |
FUNC_NAME); |
| 746 |
err = gnutls_credentials_set (c_session, GNUTLS_CRD_ANON, c_cred); |
| 747 |
} |
| 748 |
#ifdef ENABLE_SRP |
| 749 |
else if (SCM_SMOB_PREDICATE (scm_tc16_gnutls_srp_client_credentials, cred)) |
| 750 |
{ |
| 751 |
gnutls_srp_client_credentials_t c_cred; |
| 752 |
|
| 753 |
c_cred = scm_to_gnutls_srp_client_credentials (cred, 2, FUNC_NAME); |
| 754 |
err = gnutls_credentials_set (c_session, GNUTLS_CRD_SRP, c_cred); |
| 755 |
} |
| 756 |
else if (SCM_SMOB_PREDICATE (scm_tc16_gnutls_srp_server_credentials, cred)) |
| 757 |
{ |
| 758 |
gnutls_srp_server_credentials_t c_cred; |
| 759 |
|
| 760 |
c_cred = scm_to_gnutls_srp_server_credentials (cred, 2, FUNC_NAME); |
| 761 |
err = gnutls_credentials_set (c_session, GNUTLS_CRD_SRP, c_cred); |
| 762 |
} |
| 763 |
#endif |
| 764 |
else if (SCM_SMOB_PREDICATE (scm_tc16_gnutls_psk_client_credentials, cred)) |
| 765 |
{ |
| 766 |
gnutls_psk_client_credentials_t c_cred; |
| 767 |
|
| 768 |
c_cred = scm_to_gnutls_psk_client_credentials (cred, 2, FUNC_NAME); |
| 769 |
err = gnutls_credentials_set (c_session, GNUTLS_CRD_PSK, c_cred); |
| 770 |
} |
| 771 |
else if (SCM_SMOB_PREDICATE (scm_tc16_gnutls_psk_server_credentials, cred)) |
| 772 |
{ |
| 773 |
gnutls_psk_server_credentials_t c_cred; |
| 774 |
|
| 775 |
c_cred = scm_to_gnutls_psk_server_credentials (cred, 2, FUNC_NAME); |
| 776 |
err = gnutls_credentials_set (c_session, GNUTLS_CRD_PSK, c_cred); |
| 777 |
} |
| 778 |
else |
| 779 |
scm_wrong_type_arg (FUNC_NAME, 2, cred); |
| 780 |
|
| 781 |
if (EXPECT_FALSE (err)) |
| 782 |
scm_gnutls_error (err, FUNC_NAME); |
| 783 |
else |
| 784 |
register_weak_reference (session, cred); |
| 785 |
|
| 786 |
return SCM_UNSPECIFIED; |
| 787 |
} |
| 788 |
|
| 789 |
#undef FUNC_NAME |
| 790 |
|
| 791 |
SCM_DEFINE (scm_gnutls_set_session_server_name_x, "set-session-server-name!", |
| 792 |
3, 0, 0, |
| 793 |
(SCM session, SCM type, SCM name), |
| 794 |
"For a client, this procedure provides a way to inform " |
| 795 |
"the server that it is known under @var{name}, @i{via} the " |
| 796 |
"@code{SERVER NAME} TLS extension. @var{type} must be " |
| 797 |
"a @code{server-name-type} value, @var{server-name-type/dns} " |
| 798 |
"for DNS names.") |
| 799 |
#define FUNC_NAME s_scm_gnutls_set_session_server_name_x |
| 800 |
{ |
| 801 |
int err; |
| 802 |
gnutls_session_t c_session; |
| 803 |
gnutls_server_name_type_t c_type; |
| 804 |
char *c_name; |
| 805 |
|
| 806 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 807 |
c_type = scm_to_gnutls_server_name_type (type, 2, FUNC_NAME); |
| 808 |
SCM_VALIDATE_STRING (3, name); |
| 809 |
|
| 810 |
c_name = scm_to_locale_string (name); |
| 811 |
|
| 812 |
err = gnutls_server_name_set (c_session, c_type, c_name, strlen (c_name)); |
| 813 |
free (c_name); |
| 814 |
|
| 815 |
if (EXPECT_FALSE (err != GNUTLS_E_SUCCESS)) |
| 816 |
scm_gnutls_error (err, FUNC_NAME); |
| 817 |
|
| 818 |
return SCM_UNSPECIFIED; |
| 819 |
} |
| 820 |
|
| 821 |
#undef FUNC_NAME |
| 822 |
|
| 823 |
|
| 824 |
/* Record layer. */ |
| 825 |
|
| 826 |
SCM_DEFINE (scm_gnutls_record_send, "record-send", 2, 0, 0, |
| 827 |
(SCM session, SCM array), |
| 828 |
"Send the record constituted by @var{array} through " |
| 829 |
"@var{session}.") |
| 830 |
#define FUNC_NAME s_scm_gnutls_record_send |
| 831 |
{ |
| 832 |
SCM result; |
| 833 |
ssize_t c_result; |
| 834 |
gnutls_session_t c_session; |
| 835 |
scm_t_array_handle c_handle; |
| 836 |
const char *c_array; |
| 837 |
size_t c_len; |
| 838 |
|
| 839 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 840 |
SCM_VALIDATE_ARRAY (2, array); |
| 841 |
|
| 842 |
c_array = scm_gnutls_get_array (array, &c_handle, &c_len, FUNC_NAME); |
| 843 |
|
| 844 |
c_result = gnutls_record_send (c_session, c_array, c_len); |
| 845 |
|
| 846 |
scm_gnutls_release_array (&c_handle); |
| 847 |
|
| 848 |
if (EXPECT_TRUE (c_result >= 0)) |
| 849 |
result = scm_from_ssize_t (c_result); |
| 850 |
else |
| 851 |
scm_gnutls_error (c_result, FUNC_NAME); |
| 852 |
|
| 853 |
return (result); |
| 854 |
} |
| 855 |
|
| 856 |
#undef FUNC_NAME |
| 857 |
|
| 858 |
SCM_DEFINE (scm_gnutls_record_receive_x, "record-receive!", 2, 0, 0, |
| 859 |
(SCM session, SCM array), |
| 860 |
"Receive data from @var{session} into @var{array}, a uniform " |
| 861 |
"homogeneous array. Return the number of bytes actually " |
| 862 |
"received.") |
| 863 |
#define FUNC_NAME s_scm_gnutls_record_receive_x |
| 864 |
{ |
| 865 |
SCM result; |
| 866 |
ssize_t c_result; |
| 867 |
gnutls_session_t c_session; |
| 868 |
scm_t_array_handle c_handle; |
| 869 |
char *c_array; |
| 870 |
size_t c_len; |
| 871 |
|
| 872 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 873 |
SCM_VALIDATE_ARRAY (2, array); |
| 874 |
|
| 875 |
c_array = scm_gnutls_get_writable_array (array, &c_handle, &c_len, |
| 876 |
FUNC_NAME); |
| 877 |
|
| 878 |
c_result = gnutls_record_recv (c_session, c_array, c_len); |
| 879 |
|
| 880 |
scm_gnutls_release_array (&c_handle); |
| 881 |
|
| 882 |
if (EXPECT_TRUE (c_result >= 0)) |
| 883 |
result = scm_from_ssize_t (c_result); |
| 884 |
else |
| 885 |
scm_gnutls_error (c_result, FUNC_NAME); |
| 886 |
|
| 887 |
return (result); |
| 888 |
} |
| 889 |
|
| 890 |
#undef FUNC_NAME |
| 891 |
|
| 892 |
SCM_DEFINE (scm_gnutls_record_get_direction, "record-get-direction", 1, 0, 0, |
| 893 |
(SCM session), |
| 894 |
"Determine whether GnuTLS was interrupted when sending or " |
| 895 |
"receiving from @var{session}. This information can be used " |
| 896 |
"when deciding if to wait to be able to read or write from a " |
| 897 |
"socket before retrying. Returns 0 if interrupted when " |
| 898 |
"reading and 1 if interrupted when writing.") |
| 899 |
#define FUNC_NAME s_scm_gnutls_record_get_direction |
| 900 |
{ |
| 901 |
SCM result; |
| 902 |
int c_result; |
| 903 |
gnutls_session_t c_session; |
| 904 |
|
| 905 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 906 |
|
| 907 |
c_result = gnutls_record_get_direction (c_session); |
| 908 |
|
| 909 |
result = scm_from_signed_integer (c_result); |
| 910 |
|
| 911 |
return (result); |
| 912 |
} |
| 913 |
|
| 914 |
#undef FUNC_NAME |
| 915 |
|
| 916 |
/* Whether we're using Guile < 2.2. */ |
| 917 |
#define USING_GUILE_BEFORE_2_2 \ |
| 918 |
(SCM_MAJOR_VERSION < 2 \ |
| 919 |
|| (SCM_MAJOR_VERSION == 2 && SCM_MINOR_VERSION == 0)) |
| 920 |
|
| 921 |
/* The session record port type. Guile 2.1.4 introduced a brand new port API, |
| 922 |
so we have a separate implementation for these newer versions. */ |
| 923 |
#if USING_GUILE_BEFORE_2_2 |
| 924 |
static scm_t_bits session_record_port_type; |
| 925 |
|
| 926 |
/* Hint for the `scm_gc_' functions. */ |
| 927 |
static const char session_record_port_gc_hint[] = |
| 928 |
"gnutls-session-record-port"; |
| 929 |
#else |
| 930 |
static scm_t_port_type *session_record_port_type; |
| 931 |
#endif |
| 932 |
|
| 933 |
/* Return the session associated with PORT. */ |
| 934 |
#define SCM_GNUTLS_SESSION_RECORD_PORT_SESSION(_port) \ |
| 935 |
(SCM_CAR (SCM_PACK (SCM_STREAM (_port)))) |
| 936 |
|
| 937 |
/* Return the 'close' procedure associated with PORT or #f if there is |
| 938 |
none. */ |
| 939 |
#define SCM_GNUTLS_SESSION_RECORD_PORT_CLOSE_PROCEDURE(_port) \ |
| 940 |
(SCM_CDR (SCM_PACK (SCM_STREAM (_port)))) |
| 941 |
|
| 942 |
/* Set PROC as the 'close' procedure of PORT. */ |
| 943 |
#define SCM_GNUTLS_SET_SESSION_RECORD_PORT_CLOSE(_port, _proc) \ |
| 944 |
(SCM_SETCDR (SCM_PACK (SCM_STREAM (_port)), (_proc))) |
| 945 |
|
| 946 |
#if !USING_GUILE_BEFORE_2_2 |
| 947 |
|
| 948 |
/* Return true if PORT is a session record port. */ |
| 949 |
# define SCM_GNUTLS_SESSION_RECORD_PORT_P(_port) \ |
| 950 |
(SCM_PORTP (_port) \ |
| 951 |
&& SCM_PORT_TYPE (_port) == session_record_port_type) |
| 952 |
|
| 953 |
#else /* USING_GUILE_BEFORE_2_2 */ |
| 954 |
|
| 955 |
# define SCM_GNUTLS_SESSION_RECORD_PORT_P(_port) \ |
| 956 |
(SCM_PORTP (_port) \ |
| 957 |
&& SCM_TYP16 (_port) == session_record_port_type) |
| 958 |
|
| 959 |
#endif |
| 960 |
|
| 961 |
/* Raise a wrong-type-arg exception if PORT is not a session record port. */ |
| 962 |
#define SCM_VALIDATE_SESSION_RECORD_PORT(pos, port) \ |
| 963 |
SCM_MAKE_VALIDATE_MSG (pos, port, GNUTLS_SESSION_RECORD_PORT_P, \ |
| 964 |
"session record port") |
| 965 |
|
| 966 |
/* Size of a session port's input buffer. */ |
| 967 |
#define SCM_GNUTLS_SESSION_RECORD_PORT_BUFFER_SIZE 4096 |
| 968 |
|
| 969 |
|
| 970 |
#if USING_GUILE_BEFORE_2_2 |
| 971 |
|
| 972 |
/* Data passed to `do_fill_port ()'. */ |
| 973 |
typedef struct |
| 974 |
{ |
| 975 |
scm_t_port *c_port; |
| 976 |
gnutls_session_t c_session; |
| 977 |
} fill_port_data_t; |
| 978 |
|
| 979 |
/* Actually fill a session record port (see below). */ |
| 980 |
static void * |
| 981 |
do_fill_port (void *data) |
| 982 |
{ |
| 983 |
int chr; |
| 984 |
ssize_t result; |
| 985 |
scm_t_port *c_port; |
| 986 |
const fill_port_data_t *args = (fill_port_data_t *) data; |
| 987 |
|
| 988 |
c_port = args->c_port; |
| 989 |
|
| 990 |
/* We can get GNUTLS_E_AGAIN due to a "short read", which does _not_ |
| 991 |
correspond to an actual EAGAIN from read(2) since the underlying file |
| 992 |
descriptor is blocking. Thus, we can safely loop right away. */ |
| 993 |
do |
| 994 |
result = gnutls_record_recv (args->c_session, |
| 995 |
c_port->read_buf, c_port->read_buf_size); |
| 996 |
while (result == GNUTLS_E_AGAIN || result == GNUTLS_E_INTERRUPTED); |
| 997 |
|
| 998 |
if (EXPECT_TRUE (result > 0)) |
| 999 |
{ |
| 1000 |
c_port->read_pos = c_port->read_buf; |
| 1001 |
c_port->read_end = c_port->read_buf + result; |
| 1002 |
chr = (int) *c_port->read_buf; |
| 1003 |
} |
| 1004 |
else if (result == 0 || result == GNUTLS_E_PREMATURE_TERMINATION) |
| 1005 |
chr = EOF; |
| 1006 |
else |
| 1007 |
scm_gnutls_error (result, "fill_session_record_port_input"); |
| 1008 |
|
| 1009 |
return ((void *) (uintptr_t) chr); |
| 1010 |
} |
| 1011 |
|
| 1012 |
/* Fill in the input buffer of PORT. */ |
| 1013 |
static int |
| 1014 |
fill_session_record_port_input (SCM port) |
| 1015 |
# define FUNC_NAME "fill_session_record_port_input" |
| 1016 |
{ |
| 1017 |
int chr; |
| 1018 |
scm_t_port *c_port = SCM_PTAB_ENTRY (port); |
| 1019 |
|
| 1020 |
if (c_port->read_pos >= c_port->read_end) |
| 1021 |
{ |
| 1022 |
SCM session; |
| 1023 |
fill_port_data_t c_args; |
| 1024 |
gnutls_session_t c_session; |
| 1025 |
|
| 1026 |
session = SCM_GNUTLS_SESSION_RECORD_PORT_SESSION (port); |
| 1027 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 1028 |
|
| 1029 |
c_args.c_session = c_session; |
| 1030 |
c_args.c_port = c_port; |
| 1031 |
|
| 1032 |
if (SCM_GNUTLS_SESSION_TRANSPORT_IS_FD (c_session)) |
| 1033 |
/* SESSION's underlying transport is a raw file descriptor, so we |
| 1034 |
must leave "Guile mode" to allow the GC to run. */ |
| 1035 |
chr = (intptr_t) scm_without_guile (do_fill_port, &c_args); |
| 1036 |
else |
| 1037 |
/* SESSION's underlying transport is a port, so don't leave "Guile |
| 1038 |
mode". */ |
| 1039 |
chr = (intptr_t) do_fill_port (&c_args); |
| 1040 |
} |
| 1041 |
else |
| 1042 |
chr = (int) *c_port->read_pos; |
| 1043 |
|
| 1044 |
return chr; |
| 1045 |
} |
| 1046 |
|
| 1047 |
# undef FUNC_NAME |
| 1048 |
|
| 1049 |
/* Write SIZE octets from DATA to PORT. */ |
| 1050 |
static void |
| 1051 |
write_to_session_record_port (SCM port, const void *data, size_t size) |
| 1052 |
# define FUNC_NAME "write_to_session_record_port" |
| 1053 |
{ |
| 1054 |
SCM session; |
| 1055 |
gnutls_session_t c_session; |
| 1056 |
ssize_t c_result; |
| 1057 |
size_t c_sent = 0; |
| 1058 |
|
| 1059 |
session = SCM_GNUTLS_SESSION_RECORD_PORT_SESSION (port); |
| 1060 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 1061 |
|
| 1062 |
while (c_sent < size) |
| 1063 |
{ |
| 1064 |
c_result = gnutls_record_send (c_session, (char *) data + c_sent, |
| 1065 |
size - c_sent); |
| 1066 |
if (EXPECT_FALSE (c_result < 0)) |
| 1067 |
{ |
| 1068 |
if (c_result != GNUTLS_E_AGAIN && c_result != GNUTLS_E_INTERRUPTED) |
| 1069 |
scm_gnutls_error (c_result, FUNC_NAME); |
| 1070 |
} |
| 1071 |
else |
| 1072 |
c_sent += c_result; |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
# undef FUNC_NAME |
| 1077 |
|
| 1078 |
/* Return a new session port for SESSION. */ |
| 1079 |
static SCM |
| 1080 |
make_session_record_port (SCM session) |
| 1081 |
{ |
| 1082 |
SCM port; |
| 1083 |
scm_t_port *c_port; |
| 1084 |
unsigned char *c_port_buf; |
| 1085 |
const unsigned long mode_bits = SCM_OPN | SCM_RDNG | SCM_WRTNG; |
| 1086 |
|
| 1087 |
c_port_buf = (unsigned char *) |
| 1088 |
scm_gc_malloc_pointerless (SCM_GNUTLS_SESSION_RECORD_PORT_BUFFER_SIZE, |
| 1089 |
session_record_port_gc_hint); |
| 1090 |
|
| 1091 |
/* Create a new port. */ |
| 1092 |
port = scm_new_port_table_entry (session_record_port_type); |
| 1093 |
c_port = SCM_PTAB_ENTRY (port); |
| 1094 |
|
| 1095 |
/* Mark PORT as open, readable and writable (hmm, how elegant...). */ |
| 1096 |
SCM_SET_CELL_TYPE (port, session_record_port_type | mode_bits); |
| 1097 |
|
| 1098 |
/* Associate it with SESSION. */ |
| 1099 |
SCM_SETSTREAM (port, SCM_UNPACK (scm_cons (session, SCM_BOOL_F))); |
| 1100 |
|
| 1101 |
c_port->read_pos = c_port->read_end = c_port->read_buf = c_port_buf; |
| 1102 |
c_port->read_buf_size = SCM_GNUTLS_SESSION_RECORD_PORT_BUFFER_SIZE; |
| 1103 |
|
| 1104 |
c_port->write_buf = c_port->write_pos = &c_port->shortbuf; |
| 1105 |
c_port->write_buf_size = 1; |
| 1106 |
|
| 1107 |
return (port); |
| 1108 |
} |
| 1109 |
|
| 1110 |
#else /* !USING_GUILE_BEFORE_2_2 */ |
| 1111 |
|
| 1112 |
static size_t |
| 1113 |
read_from_session_record_port (SCM port, SCM dst, size_t start, size_t count) |
| 1114 |
# define FUNC_NAME "read_from_session_record_port" |
| 1115 |
{ |
| 1116 |
SCM session; |
| 1117 |
gnutls_session_t c_session; |
| 1118 |
char *read_buf; |
| 1119 |
ssize_t result; |
| 1120 |
|
| 1121 |
session = SCM_GNUTLS_SESSION_RECORD_PORT_SESSION (port); |
| 1122 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 1123 |
|
| 1124 |
read_buf = (char *) SCM_BYTEVECTOR_CONTENTS (dst) + start; |
| 1125 |
|
| 1126 |
/* We can get GNUTLS_E_AGAIN due to a "short read", which does _not_ |
| 1127 |
correspond to an actual EAGAIN from read(2) if the underlying file |
| 1128 |
descriptor is blocking--e.g., from 'get_last_packet', returning |
| 1129 |
GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE. |
| 1130 |
|
| 1131 |
If SESSION is backed by a file descriptor, return -1 to indicate that |
| 1132 |
we'd better poll; otherwise loop, which is good enough if the underlying |
| 1133 |
port is blocking. */ |
| 1134 |
do |
| 1135 |
result = gnutls_record_recv (c_session, read_buf, count); |
| 1136 |
while (result == GNUTLS_E_INTERRUPTED |
| 1137 |
|| (result == GNUTLS_E_AGAIN |
| 1138 |
&& !SCM_GNUTLS_SESSION_TRANSPORT_IS_FD (c_session))); |
| 1139 |
|
| 1140 |
if (result == GNUTLS_E_AGAIN |
| 1141 |
&& SCM_GNUTLS_SESSION_TRANSPORT_IS_FD (c_session)) |
| 1142 |
/* Tell Guile that reading would block. */ |
| 1143 |
return (size_t) -1; |
| 1144 |
|
| 1145 |
if (result == GNUTLS_E_PREMATURE_TERMINATION) |
| 1146 |
/* Treat premature termination as EOF instead of throwing an exception |
| 1147 |
that users of the port may not be prepared to handle. */ |
| 1148 |
result = 0; |
| 1149 |
else if (EXPECT_FALSE (result < 0)) |
| 1150 |
scm_gnutls_error (result, FUNC_NAME); |
| 1151 |
|
| 1152 |
return result; |
| 1153 |
} |
| 1154 |
|
| 1155 |
# undef FUNC_NAME |
| 1156 |
|
| 1157 |
/* Return the file descriptor that backs PORT. This function is called upon a |
| 1158 |
blocking read or write--i.e., 'read_from_session_record_port' or |
| 1159 |
'write_to_session_record_port' returned -1. */ |
| 1160 |
static int |
| 1161 |
session_record_port_fd (SCM port) |
| 1162 |
{ |
| 1163 |
SCM session; |
| 1164 |
gnutls_session_t c_session; |
| 1165 |
|
| 1166 |
session = SCM_GNUTLS_SESSION_RECORD_PORT_SESSION (port); |
| 1167 |
c_session = scm_to_gnutls_session (session, 1, __func__); |
| 1168 |
|
| 1169 |
assert (SCM_GNUTLS_SESSION_TRANSPORT_IS_FD (c_session)); |
| 1170 |
|
| 1171 |
return gnutls_transport_get_int (c_session); |
| 1172 |
} |
| 1173 |
|
| 1174 |
static size_t |
| 1175 |
write_to_session_record_port (SCM port, SCM src, size_t start, size_t count) |
| 1176 |
# define FUNC_NAME "write_to_session_record_port" |
| 1177 |
{ |
| 1178 |
SCM session; |
| 1179 |
gnutls_session_t c_session; |
| 1180 |
char *data; |
| 1181 |
ssize_t result; |
| 1182 |
|
| 1183 |
session = SCM_GNUTLS_SESSION_RECORD_PORT_SESSION (port); |
| 1184 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 1185 |
data = (char *) SCM_BYTEVECTOR_CONTENTS (src) + start; |
| 1186 |
|
| 1187 |
do |
| 1188 |
result = gnutls_record_send (c_session, data, count); |
| 1189 |
while (result == GNUTLS_E_INTERRUPTED |
| 1190 |
|| (result == GNUTLS_E_AGAIN |
| 1191 |
&& !SCM_GNUTLS_SESSION_TRANSPORT_IS_FD (c_session))); |
| 1192 |
|
| 1193 |
if (result == GNUTLS_E_AGAIN |
| 1194 |
&& SCM_GNUTLS_SESSION_TRANSPORT_IS_FD (c_session)) |
| 1195 |
/* Tell Guile that writing would block. */ |
| 1196 |
return (size_t) -1; |
| 1197 |
|
| 1198 |
if (EXPECT_FALSE (result < 0)) |
| 1199 |
scm_gnutls_error (result, FUNC_NAME); |
| 1200 |
|
| 1201 |
return result; |
| 1202 |
} |
| 1203 |
|
| 1204 |
# undef FUNC_NAME |
| 1205 |
|
| 1206 |
/* Return a new session port for SESSION. */ |
| 1207 |
static SCM |
| 1208 |
make_session_record_port (SCM session) |
| 1209 |
{ |
| 1210 |
return scm_c_make_port (session_record_port_type, |
| 1211 |
SCM_OPN | SCM_RDNG | SCM_WRTNG | SCM_BUF0, |
| 1212 |
SCM_UNPACK (scm_cons (session, SCM_BOOL_F))); |
| 1213 |
} |
| 1214 |
|
| 1215 |
#endif /* !USING_GUILE_BEFORE_2_2 */ |
| 1216 |
|
| 1217 |
/* Call PORT's close procedure, if any. */ |
| 1218 |
static |
| 1219 |
#if USING_GUILE_BEFORE_2_2 |
| 1220 |
int |
| 1221 |
#else |
| 1222 |
void |
| 1223 |
#endif |
| 1224 |
close_session_record_port (SCM port) |
| 1225 |
{ |
| 1226 |
SCM session = SCM_GNUTLS_SESSION_RECORD_PORT_SESSION (port); |
| 1227 |
SCM close = SCM_GNUTLS_SESSION_RECORD_PORT_CLOSE_PROCEDURE (port); |
| 1228 |
|
| 1229 |
if (!scm_is_false (close)) |
| 1230 |
scm_call_1 (close, port); |
| 1231 |
|
| 1232 |
/* When called during finalization (as opposed to a 'close-port' call), |
| 1233 |
SESSION might be finalized already. Check whether this is the case. */ |
| 1234 |
if (scm_is_true (scm_gnutls_session_p (session))) |
| 1235 |
{ |
| 1236 |
/* Detach SESSION from PORT. */ |
| 1237 |
gnutls_session_t c_session; |
| 1238 |
c_session = scm_to_gnutls_session (session, 1, __func__); |
| 1239 |
SCM_GNUTLS_SET_SESSION_RECORD_PORT (c_session, SCM_BOOL_F); |
| 1240 |
} |
| 1241 |
|
| 1242 |
#if USING_GUILE_BEFORE_2_2 |
| 1243 |
return 0; |
| 1244 |
#endif |
| 1245 |
} |
| 1246 |
|
| 1247 |
SCM_DEFINE (scm_gnutls_session_record_port, "session-record-port", 1, 1, 0, |
| 1248 |
(SCM session, SCM close), |
| 1249 |
"Return a read-write port that may be used to communicate over " |
| 1250 |
"@var{session}. All invocations of @code{session-port} on a " |
| 1251 |
"given session return the same object (in the sense of " |
| 1252 |
"@code{eq?}).\n\n" |
| 1253 |
"If @var{close} is provided, it must be a one-argument " |
| 1254 |
"procedure, and it will be called when the returned port is " |
| 1255 |
"closed. This is equivalent to setting it by calling " |
| 1256 |
"@code{set-session-record-port-close!}.") |
| 1257 |
#define FUNC_NAME s_scm_gnutls_session_record_port |
| 1258 |
{ |
| 1259 |
SCM port; |
| 1260 |
gnutls_session_t c_session; |
| 1261 |
|
| 1262 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 1263 |
port = SCM_GNUTLS_SESSION_RECORD_PORT (c_session); |
| 1264 |
|
| 1265 |
if (!SCM_PORTP (port)) |
| 1266 |
{ |
| 1267 |
/* Lazily create a new session port. */ |
| 1268 |
port = make_session_record_port (session); |
| 1269 |
SCM_GNUTLS_SET_SESSION_RECORD_PORT (c_session, port); |
| 1270 |
} |
| 1271 |
|
| 1272 |
if (!scm_is_eq (close, SCM_UNDEFINED)) |
| 1273 |
SCM_GNUTLS_SET_SESSION_RECORD_PORT_CLOSE (port, close); |
| 1274 |
|
| 1275 |
return (port); |
| 1276 |
} |
| 1277 |
|
| 1278 |
#undef FUNC_NAME |
| 1279 |
|
| 1280 |
SCM_DEFINE (scm_gnutls_set_session_record_port_close_x, |
| 1281 |
"set-session-record-port-close!", 2, 0, 0, |
| 1282 |
(SCM port, SCM close), |
| 1283 |
"Set @var{close}, a one-argument procedure, as the procedure " |
| 1284 |
"called when @var{port} is closed. @var{close} will be passed " |
| 1285 |
"@var{port}. It may be called when @code{close-port} is " |
| 1286 |
"called on @var{port}, or when @var{port} is garbage-collected. " |
| 1287 |
"It is a useful way to free resources associated with @var{port} " |
| 1288 |
"such as the session's transport file descriptor or port.") |
| 1289 |
#define FUNC_NAME s_scm_gnutls_set_session_record_port_close_x |
| 1290 |
{ |
| 1291 |
SCM_VALIDATE_SESSION_RECORD_PORT (1, port); |
| 1292 |
SCM_VALIDATE_PROC (2, close); |
| 1293 |
|
| 1294 |
SCM_GNUTLS_SET_SESSION_RECORD_PORT_CLOSE (port, close); |
| 1295 |
return SCM_UNSPECIFIED; |
| 1296 |
} |
| 1297 |
|
| 1298 |
#undef FUNC_NAME |
| 1299 |
|
| 1300 |
/* Create the session port type. */ |
| 1301 |
static void |
| 1302 |
scm_init_gnutls_session_record_port_type (void) |
| 1303 |
{ |
| 1304 |
session_record_port_type = scm_make_port_type ("gnutls-session-port", |
| 1305 |
#if USING_GUILE_BEFORE_2_2 |
| 1306 |
fill_session_record_port_input, |
| 1307 |
#else |
| 1308 |
read_from_session_record_port, |
| 1309 |
#endif |
| 1310 |
write_to_session_record_port); |
| 1311 |
|
| 1312 |
scm_set_port_close (session_record_port_type, close_session_record_port); |
| 1313 |
|
| 1314 |
#if !USING_GUILE_BEFORE_2_2 |
| 1315 |
/* Invoke the user-provided 'close' procedure on GC. */ |
| 1316 |
scm_set_port_needs_close_on_gc (session_record_port_type, 1); |
| 1317 |
#endif |
| 1318 |
|
| 1319 |
#if !USING_GUILE_BEFORE_2_2 |
| 1320 |
scm_set_port_read_wait_fd (session_record_port_type, |
| 1321 |
session_record_port_fd); |
| 1322 |
scm_set_port_write_wait_fd (session_record_port_type, |
| 1323 |
session_record_port_fd); |
| 1324 |
#endif |
| 1325 |
} |
| 1326 |
|
| 1327 |
|
| 1328 |
/* Transport. */ |
| 1329 |
|
| 1330 |
SCM_DEFINE (scm_gnutls_set_session_transport_fd_x, |
| 1331 |
"set-session-transport-fd!", 2, 0, 0, (SCM session, SCM fd), |
| 1332 |
"Use file descriptor @var{fd} as the underlying transport for " |
| 1333 |
"@var{session}.") |
| 1334 |
#define FUNC_NAME s_scm_gnutls_set_session_transport_fd_x |
| 1335 |
{ |
| 1336 |
gnutls_session_t c_session; |
| 1337 |
int c_fd; |
| 1338 |
|
| 1339 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 1340 |
c_fd = (int) scm_to_uint (fd); |
| 1341 |
|
| 1342 |
gnutls_transport_set_ptr (c_session, |
| 1343 |
(gnutls_transport_ptr_t) (intptr_t) c_fd); |
| 1344 |
|
| 1345 |
SCM_GNUTLS_SET_SESSION_TRANSPORT_IS_FD (c_session, 1); |
| 1346 |
|
| 1347 |
return SCM_UNSPECIFIED; |
| 1348 |
} |
| 1349 |
|
| 1350 |
#undef FUNC_NAME |
| 1351 |
|
| 1352 |
/* Pull SIZE octets from TRANSPORT (a Scheme port) into DATA. */ |
| 1353 |
static ssize_t |
| 1354 |
pull_from_port (gnutls_transport_ptr_t transport, void *data, size_t size) |
| 1355 |
{ |
| 1356 |
SCM port; |
| 1357 |
ssize_t result; |
| 1358 |
|
| 1359 |
port = SCM_PACK ((scm_t_bits) transport); |
| 1360 |
|
| 1361 |
result = scm_c_read (port, data, size); |
| 1362 |
|
| 1363 |
return ((ssize_t) result); |
| 1364 |
} |
| 1365 |
|
| 1366 |
/* Write SIZE octets from DATA to TRANSPORT (a Scheme port). */ |
| 1367 |
static ssize_t |
| 1368 |
push_to_port (gnutls_transport_ptr_t transport, const void *data, size_t size) |
| 1369 |
{ |
| 1370 |
SCM port; |
| 1371 |
|
| 1372 |
port = SCM_PACK ((scm_t_bits) transport); |
| 1373 |
|
| 1374 |
scm_c_write (port, data, size); |
| 1375 |
|
| 1376 |
/* All we can do is assume that all SIZE octets were written. */ |
| 1377 |
return (size); |
| 1378 |
} |
| 1379 |
|
| 1380 |
SCM_DEFINE (scm_gnutls_set_session_transport_port_x, |
| 1381 |
"set-session-transport-port!", |
| 1382 |
2, 0, 0, |
| 1383 |
(SCM session, SCM port), |
| 1384 |
"Use @var{port} as the input/output port for @var{session}.") |
| 1385 |
#define FUNC_NAME s_scm_gnutls_set_session_transport_port_x |
| 1386 |
{ |
| 1387 |
gnutls_session_t c_session; |
| 1388 |
|
| 1389 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 1390 |
SCM_VALIDATE_PORT (2, port); |
| 1391 |
|
| 1392 |
/* Note: We do not attempt to optimize the case where PORT is a file port |
| 1393 |
(i.e., over a file descriptor), because of port buffering issues. Users |
| 1394 |
are expected to explicitly use `set-session-transport-fd!' and `fileno' |
| 1395 |
when they wish to do it. */ |
| 1396 |
|
| 1397 |
gnutls_transport_set_ptr (c_session, |
| 1398 |
(gnutls_transport_ptr_t) SCM_UNPACK (port)); |
| 1399 |
gnutls_transport_set_push_function (c_session, push_to_port); |
| 1400 |
gnutls_transport_set_pull_function (c_session, pull_from_port); |
| 1401 |
|
| 1402 |
SCM_GNUTLS_SET_SESSION_TRANSPORT_IS_FD (c_session, 0); |
| 1403 |
|
| 1404 |
return SCM_UNSPECIFIED; |
| 1405 |
} |
| 1406 |
|
| 1407 |
#undef FUNC_NAME |
| 1408 |
|
| 1409 |
|
| 1410 |
/* Diffie-Hellman. */ |
| 1411 |
|
| 1412 |
typedef int (*pkcs_export_function_t) (void *, gnutls_x509_crt_fmt_t, |
| 1413 |
unsigned char *, size_t *); |
| 1414 |
|
| 1415 |
/* Hint for the `scm_gc' functions. */ |
| 1416 |
static const char pkcs_export_gc_hint[] = "gnutls-pkcs-export"; |
| 1417 |
|
| 1418 |
|
| 1419 |
/* Export DH/RSA parameters PARAMS through EXPORT, using format FORMAT. |
| 1420 |
Return a `u8vector'. */ |
| 1421 |
static inline SCM |
| 1422 |
pkcs_export_parameters (pkcs_export_function_t export, |
| 1423 |
void *params, gnutls_x509_crt_fmt_t format, |
| 1424 |
const char *func_name) |
| 1425 |
#define FUNC_NAME func_name |
| 1426 |
{ |
| 1427 |
int err; |
| 1428 |
unsigned char *output; |
| 1429 |
size_t output_len, output_total_len = 4096; |
| 1430 |
|
| 1431 |
output = (unsigned char *) scm_gc_malloc (output_total_len, |
| 1432 |
pkcs_export_gc_hint); |
| 1433 |
do |
| 1434 |
{ |
| 1435 |
output_len = output_total_len; |
| 1436 |
err = export (params, format, output, &output_len); |
| 1437 |
|
| 1438 |
if (err == GNUTLS_E_SHORT_MEMORY_BUFFER) |
| 1439 |
{ |
| 1440 |
output = scm_gc_realloc (output, output_total_len, |
| 1441 |
output_total_len * 2, pkcs_export_gc_hint); |
| 1442 |
output_total_len *= 2; |
| 1443 |
} |
| 1444 |
} |
| 1445 |
while (err == GNUTLS_E_SHORT_MEMORY_BUFFER); |
| 1446 |
|
| 1447 |
if (EXPECT_FALSE (err)) |
| 1448 |
{ |
| 1449 |
scm_gc_free (output, output_total_len, pkcs_export_gc_hint); |
| 1450 |
scm_gnutls_error (err, FUNC_NAME); |
| 1451 |
} |
| 1452 |
|
| 1453 |
if (output_len != output_total_len) |
| 1454 |
/* Shrink the output buffer. */ |
| 1455 |
output = scm_gc_realloc (output, output_total_len, |
| 1456 |
output_len, pkcs_export_gc_hint); |
| 1457 |
|
| 1458 |
return (scm_take_u8vector (output, output_len)); |
| 1459 |
} |
| 1460 |
|
| 1461 |
#undef FUNC_NAME |
| 1462 |
|
| 1463 |
|
| 1464 |
SCM_DEFINE (scm_gnutls_make_dh_parameters, "make-dh-parameters", 1, 0, 0, |
| 1465 |
(SCM bits), "Return new Diffie-Hellman parameters.") |
| 1466 |
#define FUNC_NAME s_scm_gnutls_make_dh_parameters |
| 1467 |
{ |
| 1468 |
int err; |
| 1469 |
unsigned c_bits; |
| 1470 |
gnutls_dh_params_t c_dh_params; |
| 1471 |
|
| 1472 |
c_bits = scm_to_uint (bits); |
| 1473 |
|
| 1474 |
err = gnutls_dh_params_init (&c_dh_params); |
| 1475 |
if (EXPECT_FALSE (err)) |
| 1476 |
scm_gnutls_error (err, FUNC_NAME); |
| 1477 |
|
| 1478 |
err = gnutls_dh_params_generate2 (c_dh_params, c_bits); |
| 1479 |
if (EXPECT_FALSE (err)) |
| 1480 |
{ |
| 1481 |
gnutls_dh_params_deinit (c_dh_params); |
| 1482 |
scm_gnutls_error (err, FUNC_NAME); |
| 1483 |
} |
| 1484 |
|
| 1485 |
return (scm_from_gnutls_dh_parameters (c_dh_params)); |
| 1486 |
} |
| 1487 |
|
| 1488 |
#undef FUNC_NAME |
| 1489 |
|
| 1490 |
SCM_DEFINE (scm_gnutls_pkcs3_import_dh_parameters, |
| 1491 |
"pkcs3-import-dh-parameters", |
| 1492 |
2, 0, 0, |
| 1493 |
(SCM array, SCM format), |
| 1494 |
"Import Diffie-Hellman parameters in PKCS3 format (further " |
| 1495 |
"specified by @var{format}, an @code{x509-certificate-format} " |
| 1496 |
"value) from @var{array} (a homogeneous array) and return a " |
| 1497 |
"new @code{dh-params} object.") |
| 1498 |
#define FUNC_NAME s_scm_gnutls_pkcs3_import_dh_parameters |
| 1499 |
{ |
| 1500 |
int err; |
| 1501 |
gnutls_x509_crt_fmt_t c_format; |
| 1502 |
gnutls_dh_params_t c_dh_params; |
| 1503 |
scm_t_array_handle c_handle; |
| 1504 |
const char *c_array; |
| 1505 |
size_t c_len; |
| 1506 |
gnutls_datum_t c_datum; |
| 1507 |
|
| 1508 |
c_format = scm_to_gnutls_x509_certificate_format (format, 2, FUNC_NAME); |
| 1509 |
|
| 1510 |
c_array = scm_gnutls_get_array (array, &c_handle, &c_len, FUNC_NAME); |
| 1511 |
c_datum.data = (unsigned char *) c_array; |
| 1512 |
c_datum.size = c_len; |
| 1513 |
|
| 1514 |
err = gnutls_dh_params_init (&c_dh_params); |
| 1515 |
if (EXPECT_FALSE (err)) |
| 1516 |
{ |
| 1517 |
scm_gnutls_release_array (&c_handle); |
| 1518 |
scm_gnutls_error (err, FUNC_NAME); |
| 1519 |
} |
| 1520 |
|
| 1521 |
err = gnutls_dh_params_import_pkcs3 (c_dh_params, &c_datum, c_format); |
| 1522 |
scm_gnutls_release_array (&c_handle); |
| 1523 |
|
| 1524 |
if (EXPECT_FALSE (err)) |
| 1525 |
{ |
| 1526 |
gnutls_dh_params_deinit (c_dh_params); |
| 1527 |
scm_gnutls_error (err, FUNC_NAME); |
| 1528 |
} |
| 1529 |
|
| 1530 |
return (scm_from_gnutls_dh_parameters (c_dh_params)); |
| 1531 |
} |
| 1532 |
|
| 1533 |
#undef FUNC_NAME |
| 1534 |
|
| 1535 |
SCM_DEFINE (scm_gnutls_pkcs3_export_dh_parameters, |
| 1536 |
"pkcs3-export-dh-parameters", |
| 1537 |
2, 0, 0, |
| 1538 |
(SCM dh_params, SCM format), |
| 1539 |
"Export Diffie-Hellman parameters @var{dh_params} in PKCS3 " |
| 1540 |
"format according for @var{format} (an " |
| 1541 |
"@code{x509-certificate-format} value). Return a " |
| 1542 |
"@code{u8vector} containing the result.") |
| 1543 |
#define FUNC_NAME s_scm_gnutls_pkcs3_export_dh_parameters |
| 1544 |
{ |
| 1545 |
SCM result; |
| 1546 |
gnutls_dh_params_t c_dh_params; |
| 1547 |
gnutls_x509_crt_fmt_t c_format; |
| 1548 |
|
| 1549 |
c_dh_params = scm_to_gnutls_dh_parameters (dh_params, 1, FUNC_NAME); |
| 1550 |
c_format = scm_to_gnutls_x509_certificate_format (format, 2, FUNC_NAME); |
| 1551 |
|
| 1552 |
result = pkcs_export_parameters ((pkcs_export_function_t) |
| 1553 |
gnutls_dh_params_export_pkcs3, |
| 1554 |
(void *) c_dh_params, c_format, FUNC_NAME); |
| 1555 |
|
| 1556 |
return (result); |
| 1557 |
} |
| 1558 |
|
| 1559 |
#undef FUNC_NAME |
| 1560 |
|
| 1561 |
SCM_DEFINE (scm_gnutls_set_session_dh_prime_bits_x, |
| 1562 |
"set-session-dh-prime-bits!", 2, 0, 0, |
| 1563 |
(SCM session, SCM bits), |
| 1564 |
"Use @var{bits} DH prime bits for @var{session}.") |
| 1565 |
#define FUNC_NAME s_scm_gnutls_set_session_dh_prime_bits_x |
| 1566 |
{ |
| 1567 |
unsigned int c_bits; |
| 1568 |
gnutls_session_t c_session; |
| 1569 |
|
| 1570 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 1571 |
c_bits = scm_to_uint (bits); |
| 1572 |
|
| 1573 |
gnutls_dh_set_prime_bits (c_session, c_bits); |
| 1574 |
|
| 1575 |
return SCM_UNSPECIFIED; |
| 1576 |
} |
| 1577 |
|
| 1578 |
#undef FUNC_NAME |
| 1579 |
|
| 1580 |
|
| 1581 |
/* Anonymous credentials. */ |
| 1582 |
|
| 1583 |
SCM_DEFINE (scm_gnutls_make_anon_server_credentials, |
| 1584 |
"make-anonymous-server-credentials", |
| 1585 |
0, 0, 0, (void), "Return anonymous server credentials.") |
| 1586 |
#define FUNC_NAME s_scm_gnutls_make_anon_server_credentials |
| 1587 |
{ |
| 1588 |
int err; |
| 1589 |
gnutls_anon_server_credentials_t c_cred; |
| 1590 |
|
| 1591 |
err = gnutls_anon_allocate_server_credentials (&c_cred); |
| 1592 |
|
| 1593 |
if (EXPECT_FALSE (err)) |
| 1594 |
scm_gnutls_error (err, FUNC_NAME); |
| 1595 |
|
| 1596 |
return (scm_from_gnutls_anonymous_server_credentials (c_cred)); |
| 1597 |
} |
| 1598 |
|
| 1599 |
#undef FUNC_NAME |
| 1600 |
|
| 1601 |
SCM_DEFINE (scm_gnutls_make_anon_client_credentials, |
| 1602 |
"make-anonymous-client-credentials", |
| 1603 |
0, 0, 0, (void), "Return anonymous client credentials.") |
| 1604 |
#define FUNC_NAME s_scm_gnutls_make_anon_client_credentials |
| 1605 |
{ |
| 1606 |
int err; |
| 1607 |
gnutls_anon_client_credentials_t c_cred; |
| 1608 |
|
| 1609 |
err = gnutls_anon_allocate_client_credentials (&c_cred); |
| 1610 |
|
| 1611 |
if (EXPECT_FALSE (err)) |
| 1612 |
scm_gnutls_error (err, FUNC_NAME); |
| 1613 |
|
| 1614 |
return (scm_from_gnutls_anonymous_client_credentials (c_cred)); |
| 1615 |
} |
| 1616 |
|
| 1617 |
#undef FUNC_NAME |
| 1618 |
|
| 1619 |
SCM_DEFINE (scm_gnutls_set_anonymous_server_dh_parameters_x, |
| 1620 |
"set-anonymous-server-dh-parameters!", 2, 0, 0, |
| 1621 |
(SCM cred, SCM dh_params), |
| 1622 |
"Set the Diffie-Hellman parameters of anonymous server " |
| 1623 |
"credentials @var{cred}.") |
| 1624 |
#define FUNC_NAME s_scm_gnutls_set_anonymous_server_dh_parameters_x |
| 1625 |
{ |
| 1626 |
gnutls_dh_params_t c_dh_params; |
| 1627 |
gnutls_anon_server_credentials_t c_cred; |
| 1628 |
|
| 1629 |
c_cred = scm_to_gnutls_anonymous_server_credentials (cred, 1, FUNC_NAME); |
| 1630 |
c_dh_params = scm_to_gnutls_dh_parameters (dh_params, 2, FUNC_NAME); |
| 1631 |
|
| 1632 |
gnutls_anon_set_server_dh_params (c_cred, c_dh_params); |
| 1633 |
register_weak_reference (cred, dh_params); |
| 1634 |
|
| 1635 |
return SCM_UNSPECIFIED; |
| 1636 |
} |
| 1637 |
|
| 1638 |
#undef FUNC_NAME |
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
/* Certificate credentials. */ |
| 1643 |
|
| 1644 |
typedef |
| 1645 |
int (*certificate_set_file_function_t) (gnutls_certificate_credentials_t, |
| 1646 |
const char *, |
| 1647 |
gnutls_x509_crt_fmt_t); |
| 1648 |
|
| 1649 |
typedef |
| 1650 |
int (*certificate_set_data_function_t) (gnutls_certificate_credentials_t, |
| 1651 |
const gnutls_datum_t *, |
| 1652 |
gnutls_x509_crt_fmt_t); |
| 1653 |
|
| 1654 |
/* Helper function to implement the `set-file!' functions. */ |
| 1655 |
static unsigned int |
| 1656 |
set_certificate_file (certificate_set_file_function_t set_file, |
| 1657 |
SCM cred, SCM file, SCM format, const char *func_name) |
| 1658 |
#define FUNC_NAME func_name |
| 1659 |
{ |
| 1660 |
int err; |
| 1661 |
char *c_file; |
| 1662 |
size_t c_file_len; |
| 1663 |
|
| 1664 |
gnutls_certificate_credentials_t c_cred; |
| 1665 |
gnutls_x509_crt_fmt_t c_format; |
| 1666 |
|
| 1667 |
c_cred = scm_to_gnutls_certificate_credentials (cred, 1, FUNC_NAME); |
| 1668 |
SCM_VALIDATE_STRING (2, file); |
| 1669 |
c_format = scm_to_gnutls_x509_certificate_format (format, 3, FUNC_NAME); |
| 1670 |
|
| 1671 |
c_file_len = scm_c_string_length (file); |
| 1672 |
c_file = FAST_ALLOC (c_file_len + 1); |
| 1673 |
|
| 1674 |
(void) scm_to_locale_stringbuf (file, c_file, c_file_len + 1); |
| 1675 |
c_file[c_file_len] = '\0'; |
| 1676 |
|
| 1677 |
err = set_file (c_cred, c_file, c_format); |
| 1678 |
if (EXPECT_FALSE (err < 0)) |
| 1679 |
scm_gnutls_error (err, FUNC_NAME); |
| 1680 |
|
| 1681 |
/* Return the number of certificates processed. */ |
| 1682 |
return ((unsigned int) err); |
| 1683 |
} |
| 1684 |
|
| 1685 |
#undef FUNC_NAME |
| 1686 |
|
| 1687 |
/* Helper function implementing the `set-data!' functions. */ |
| 1688 |
static inline unsigned int |
| 1689 |
set_certificate_data (certificate_set_data_function_t set_data, |
| 1690 |
SCM cred, SCM data, SCM format, const char *func_name) |
| 1691 |
#define FUNC_NAME func_name |
| 1692 |
{ |
| 1693 |
int err; |
| 1694 |
gnutls_certificate_credentials_t c_cred; |
| 1695 |
gnutls_x509_crt_fmt_t c_format; |
| 1696 |
gnutls_datum_t c_datum; |
| 1697 |
scm_t_array_handle c_handle; |
| 1698 |
const char *c_data; |
| 1699 |
size_t c_len; |
| 1700 |
|
| 1701 |
c_cred = scm_to_gnutls_certificate_credentials (cred, 1, FUNC_NAME); |
| 1702 |
SCM_VALIDATE_ARRAY (2, data); |
| 1703 |
c_format = scm_to_gnutls_x509_certificate_format (format, 3, FUNC_NAME); |
| 1704 |
|
| 1705 |
c_data = scm_gnutls_get_array (data, &c_handle, &c_len, FUNC_NAME); |
| 1706 |
c_datum.data = (unsigned char *) c_data; |
| 1707 |
c_datum.size = c_len; |
| 1708 |
|
| 1709 |
err = set_data (c_cred, &c_datum, c_format); |
| 1710 |
scm_gnutls_release_array (&c_handle); |
| 1711 |
|
| 1712 |
if (EXPECT_FALSE (err < 0)) |
| 1713 |
scm_gnutls_error (err, FUNC_NAME); |
| 1714 |
|
| 1715 |
/* Return the number of certificates processed. */ |
| 1716 |
return ((unsigned int) err); |
| 1717 |
} |
| 1718 |
|
| 1719 |
#undef FUNC_NAME |
| 1720 |
|
| 1721 |
|
| 1722 |
SCM_DEFINE (scm_gnutls_make_certificate_credentials, |
| 1723 |
"make-certificate-credentials", |
| 1724 |
0, 0, 0, |
| 1725 |
(void), |
| 1726 |
"Return new certificate credentials (i.e., for use with " |
| 1727 |
"either X.509 or OpenPGP certificates.") |
| 1728 |
#define FUNC_NAME s_scm_gnutls_make_certificate_credentials |
| 1729 |
{ |
| 1730 |
int err; |
| 1731 |
gnutls_certificate_credentials_t c_cred; |
| 1732 |
|
| 1733 |
err = gnutls_certificate_allocate_credentials (&c_cred); |
| 1734 |
if (err) |
| 1735 |
scm_gnutls_error (err, FUNC_NAME); |
| 1736 |
|
| 1737 |
return (scm_from_gnutls_certificate_credentials (c_cred)); |
| 1738 |
} |
| 1739 |
|
| 1740 |
#undef FUNC_NAME |
| 1741 |
|
| 1742 |
SCM_DEFINE (scm_gnutls_set_certificate_credentials_dh_params_x, |
| 1743 |
"set-certificate-credentials-dh-parameters!", |
| 1744 |
2, 0, 0, |
| 1745 |
(SCM cred, SCM dh_params), |
| 1746 |
"Use Diffie-Hellman parameters @var{dh_params} for " |
| 1747 |
"certificate credentials @var{cred}.") |
| 1748 |
#define FUNC_NAME s_scm_gnutls_set_certificate_credentials_dh_params_x |
| 1749 |
{ |
| 1750 |
gnutls_dh_params_t c_dh_params; |
| 1751 |
gnutls_certificate_credentials_t c_cred; |
| 1752 |
|
| 1753 |
c_cred = scm_to_gnutls_certificate_credentials (cred, 1, FUNC_NAME); |
| 1754 |
c_dh_params = scm_to_gnutls_dh_parameters (dh_params, 2, FUNC_NAME); |
| 1755 |
|
| 1756 |
gnutls_certificate_set_dh_params (c_cred, c_dh_params); |
| 1757 |
register_weak_reference (cred, dh_params); |
| 1758 |
|
| 1759 |
return SCM_UNSPECIFIED; |
| 1760 |
} |
| 1761 |
|
| 1762 |
#undef FUNC_NAME |
| 1763 |
|
| 1764 |
SCM_DEFINE (scm_gnutls_set_certificate_credentials_x509_key_files_x, |
| 1765 |
"set-certificate-credentials-x509-key-files!", |
| 1766 |
4, 0, 0, |
| 1767 |
(SCM cred, SCM cert_file, SCM key_file, SCM format), |
| 1768 |
"Use @var{file} as the password file for PSK server " |
| 1769 |
"credentials @var{cred}.") |
| 1770 |
#define FUNC_NAME s_scm_gnutls_set_certificate_credentials_x509_key_files_x |
| 1771 |
{ |
| 1772 |
int err; |
| 1773 |
gnutls_certificate_credentials_t c_cred; |
| 1774 |
gnutls_x509_crt_fmt_t c_format; |
| 1775 |
char *c_cert_file, *c_key_file; |
| 1776 |
size_t c_cert_file_len, c_key_file_len; |
| 1777 |
|
| 1778 |
c_cred = scm_to_gnutls_certificate_credentials (cred, 1, FUNC_NAME); |
| 1779 |
SCM_VALIDATE_STRING (2, cert_file); |
| 1780 |
SCM_VALIDATE_STRING (3, key_file); |
| 1781 |
c_format = scm_to_gnutls_x509_certificate_format (format, 2, FUNC_NAME); |
| 1782 |
|
| 1783 |
c_cert_file_len = scm_c_string_length (cert_file); |
| 1784 |
c_cert_file = FAST_ALLOC (c_cert_file_len + 1); |
| 1785 |
|
| 1786 |
c_key_file_len = scm_c_string_length (key_file); |
| 1787 |
c_key_file = FAST_ALLOC (c_key_file_len + 1); |
| 1788 |
|
| 1789 |
(void) scm_to_locale_stringbuf (cert_file, c_cert_file, |
| 1790 |
c_cert_file_len + 1); |
| 1791 |
c_cert_file[c_cert_file_len] = '\0'; |
| 1792 |
(void) scm_to_locale_stringbuf (key_file, c_key_file, c_key_file_len + 1); |
| 1793 |
c_key_file[c_key_file_len] = '\0'; |
| 1794 |
|
| 1795 |
err = gnutls_certificate_set_x509_key_file (c_cred, c_cert_file, c_key_file, |
| 1796 |
c_format); |
| 1797 |
if (EXPECT_FALSE (err)) |
| 1798 |
scm_gnutls_error (err, FUNC_NAME); |
| 1799 |
|
| 1800 |
return SCM_UNSPECIFIED; |
| 1801 |
} |
| 1802 |
|
| 1803 |
#undef FUNC_NAME |
| 1804 |
|
| 1805 |
SCM_DEFINE (scm_gnutls_set_certificate_credentials_x509_trust_file_x, |
| 1806 |
"set-certificate-credentials-x509-trust-file!", |
| 1807 |
3, 0, 0, |
| 1808 |
(SCM cred, SCM file, SCM format), |
| 1809 |
"Use @var{file} as the X.509 trust file for certificate " |
| 1810 |
"credentials @var{cred}. On success, return the number of " |
| 1811 |
"certificates processed.") |
| 1812 |
#define FUNC_NAME s_scm_gnutls_set_certificate_credentials_x509_trust_file_x |
| 1813 |
{ |
| 1814 |
unsigned int count; |
| 1815 |
|
| 1816 |
count = set_certificate_file (gnutls_certificate_set_x509_trust_file, |
| 1817 |
cred, file, format, FUNC_NAME); |
| 1818 |
|
| 1819 |
return scm_from_uint (count); |
| 1820 |
} |
| 1821 |
|
| 1822 |
#undef FUNC_NAME |
| 1823 |
|
| 1824 |
SCM_DEFINE (scm_gnutls_set_certificate_credentials_x509_crl_file_x, |
| 1825 |
"set-certificate-credentials-x509-crl-file!", |
| 1826 |
3, 0, 0, |
| 1827 |
(SCM cred, SCM file, SCM format), |
| 1828 |
"Use @var{file} as the X.509 CRL (certificate revocation list) " |
| 1829 |
"file for certificate credentials @var{cred}. On success, " |
| 1830 |
"return the number of CRLs processed.") |
| 1831 |
#define FUNC_NAME s_scm_gnutls_set_certificate_credentials_x509_crl_file_x |
| 1832 |
{ |
| 1833 |
unsigned int count; |
| 1834 |
|
| 1835 |
count = set_certificate_file (gnutls_certificate_set_x509_crl_file, |
| 1836 |
cred, file, format, FUNC_NAME); |
| 1837 |
|
| 1838 |
return scm_from_uint (count); |
| 1839 |
} |
| 1840 |
|
| 1841 |
#undef FUNC_NAME |
| 1842 |
|
| 1843 |
SCM_DEFINE (scm_gnutls_set_certificate_credentials_x509_trust_data_x, |
| 1844 |
"set-certificate-credentials-x509-trust-data!", |
| 1845 |
3, 0, 0, |
| 1846 |
(SCM cred, SCM data, SCM format), |
| 1847 |
"Use @var{data} (a uniform array) as the X.509 trust " |
| 1848 |
"database for @var{cred}. On success, return the number " |
| 1849 |
"of certificates processed.") |
| 1850 |
#define FUNC_NAME s_scm_gnutls_set_certificate_credentials_x509_trust_data_x |
| 1851 |
{ |
| 1852 |
unsigned int count; |
| 1853 |
|
| 1854 |
count = set_certificate_data (gnutls_certificate_set_x509_trust_mem, |
| 1855 |
cred, data, format, FUNC_NAME); |
| 1856 |
|
| 1857 |
return scm_from_uint (count); |
| 1858 |
} |
| 1859 |
|
| 1860 |
#undef FUNC_NAME |
| 1861 |
|
| 1862 |
SCM_DEFINE (scm_gnutls_set_certificate_credentials_x509_crl_data_x, |
| 1863 |
"set-certificate-credentials-x509-crl-data!", |
| 1864 |
3, 0, 0, |
| 1865 |
(SCM cred, SCM data, SCM format), |
| 1866 |
"Use @var{data} (a uniform array) as the X.509 CRL " |
| 1867 |
"(certificate revocation list) database for @var{cred}. " |
| 1868 |
"On success, return the number of CRLs processed.") |
| 1869 |
#define FUNC_NAME s_scm_gnutls_set_certificate_credentials_x509_crl_data_x |
| 1870 |
{ |
| 1871 |
unsigned int count; |
| 1872 |
|
| 1873 |
count = set_certificate_data (gnutls_certificate_set_x509_crl_mem, |
| 1874 |
cred, data, format, FUNC_NAME); |
| 1875 |
|
| 1876 |
return scm_from_uint (count); |
| 1877 |
} |
| 1878 |
|
| 1879 |
#undef FUNC_NAME |
| 1880 |
|
| 1881 |
SCM_DEFINE (scm_gnutls_set_certificate_credentials_x509_key_data_x, |
| 1882 |
"set-certificate-credentials-x509-key-data!", |
| 1883 |
4, 0, 0, |
| 1884 |
(SCM cred, SCM cert, SCM key, SCM format), |
| 1885 |
"Use X.509 certificate @var{cert} and private key @var{key}, " |
| 1886 |
"both uniform arrays containing the X.509 certificate and key " |
| 1887 |
"in format @var{format}, for certificate credentials " |
| 1888 |
"@var{cred}.") |
| 1889 |
#define FUNC_NAME s_scm_gnutls_set_certificate_credentials_x509_key_data_x |
| 1890 |
{ |
| 1891 |
int err; |
| 1892 |
gnutls_x509_crt_fmt_t c_format; |
| 1893 |
gnutls_certificate_credentials_t c_cred; |
| 1894 |
gnutls_datum_t c_cert_d, c_key_d; |
| 1895 |
scm_t_array_handle c_cert_handle, c_key_handle; |
| 1896 |
const char *c_cert, *c_key; |
| 1897 |
size_t c_cert_len, c_key_len; |
| 1898 |
|
| 1899 |
c_cred = scm_to_gnutls_certificate_credentials (cred, 1, FUNC_NAME); |
| 1900 |
c_format = scm_to_gnutls_x509_certificate_format (format, 4, FUNC_NAME); |
| 1901 |
SCM_VALIDATE_ARRAY (2, cert); |
| 1902 |
SCM_VALIDATE_ARRAY (3, key); |
| 1903 |
|
| 1904 |
/* FIXME: If the second call fails, an exception is raised and |
| 1905 |
C_CERT_HANDLE is not released. */ |
| 1906 |
c_cert = scm_gnutls_get_array (cert, &c_cert_handle, &c_cert_len, |
| 1907 |
FUNC_NAME); |
| 1908 |
c_key = scm_gnutls_get_array (key, &c_key_handle, &c_key_len, FUNC_NAME); |
| 1909 |
|
| 1910 |
c_cert_d.data = (unsigned char *) c_cert; |
| 1911 |
c_cert_d.size = c_cert_len; |
| 1912 |
c_key_d.data = (unsigned char *) c_key; |
| 1913 |
c_key_d.size = c_key_len; |
| 1914 |
|
| 1915 |
err = gnutls_certificate_set_x509_key_mem (c_cred, &c_cert_d, &c_key_d, |
| 1916 |
c_format); |
| 1917 |
scm_gnutls_release_array (&c_cert_handle); |
| 1918 |
scm_gnutls_release_array (&c_key_handle); |
| 1919 |
|
| 1920 |
if (EXPECT_FALSE (err)) |
| 1921 |
scm_gnutls_error (err, FUNC_NAME); |
| 1922 |
|
| 1923 |
return SCM_UNSPECIFIED; |
| 1924 |
} |
| 1925 |
|
| 1926 |
#undef FUNC_NAME |
| 1927 |
|
| 1928 |
SCM_DEFINE (scm_gnutls_set_certificate_credentials_x509_keys_x, |
| 1929 |
"set-certificate-credentials-x509-keys!", |
| 1930 |
3, 0, 0, |
| 1931 |
(SCM cred, SCM certs, SCM privkey), |
| 1932 |
"Have certificate credentials @var{cred} use the X.509 " |
| 1933 |
"certificates listed in @var{certs} and X.509 private key " |
| 1934 |
"@var{privkey}.") |
| 1935 |
#define FUNC_NAME s_scm_gnutls_set_certificate_credentials_x509_keys_x |
| 1936 |
{ |
| 1937 |
int err; |
| 1938 |
gnutls_x509_crt_t *c_certs; |
| 1939 |
gnutls_x509_privkey_t c_key; |
| 1940 |
gnutls_certificate_credentials_t c_cred; |
| 1941 |
long int c_cert_count, i; |
| 1942 |
|
| 1943 |
c_cred = scm_to_gnutls_certificate_credentials (cred, 1, FUNC_NAME); |
| 1944 |
SCM_VALIDATE_LIST_COPYLEN (2, certs, c_cert_count); |
| 1945 |
c_key = scm_to_gnutls_x509_private_key (privkey, 3, FUNC_NAME); |
| 1946 |
|
| 1947 |
c_certs = FAST_ALLOC (c_cert_count * sizeof (*c_certs)); |
| 1948 |
for (i = 0; scm_is_pair (certs); certs = SCM_CDR (certs), i++) |
| 1949 |
{ |
| 1950 |
c_certs[i] = scm_to_gnutls_x509_certificate (SCM_CAR (certs), |
| 1951 |
2, FUNC_NAME); |
| 1952 |
} |
| 1953 |
|
| 1954 |
err = gnutls_certificate_set_x509_key (c_cred, c_certs, c_cert_count, |
| 1955 |
c_key); |
| 1956 |
if (EXPECT_FALSE (err)) |
| 1957 |
scm_gnutls_error (err, FUNC_NAME); |
| 1958 |
else |
| 1959 |
{ |
| 1960 |
register_weak_reference (cred, privkey); |
| 1961 |
register_weak_reference (cred, scm_list_copy (certs)); |
| 1962 |
} |
| 1963 |
|
| 1964 |
return SCM_UNSPECIFIED; |
| 1965 |
} |
| 1966 |
|
| 1967 |
#undef FUNC_NAME |
| 1968 |
|
| 1969 |
SCM_DEFINE (scm_gnutls_set_certificate_credentials_verify_limits_x, |
| 1970 |
"set-certificate-credentials-verify-limits!", |
| 1971 |
3, 0, 0, |
| 1972 |
(SCM cred, SCM max_bits, SCM max_depth), |
| 1973 |
"Set the verification limits of @code{peer-certificate-status} " |
| 1974 |
"for certificate credentials @var{cred} to @var{max_bits} " |
| 1975 |
"bits for an acceptable certificate and @var{max_depth} " |
| 1976 |
"as the maximum depth of a certificate chain.") |
| 1977 |
#define FUNC_NAME s_scm_gnutls_set_certificate_credentials_verify_limits_x |
| 1978 |
{ |
| 1979 |
gnutls_certificate_credentials_t c_cred; |
| 1980 |
unsigned int c_max_bits, c_max_depth; |
| 1981 |
|
| 1982 |
c_cred = scm_to_gnutls_certificate_credentials (cred, 1, FUNC_NAME); |
| 1983 |
c_max_bits = scm_to_uint (max_bits); |
| 1984 |
c_max_depth = scm_to_uint (max_depth); |
| 1985 |
|
| 1986 |
gnutls_certificate_set_verify_limits (c_cred, c_max_bits, c_max_depth); |
| 1987 |
|
| 1988 |
return SCM_UNSPECIFIED; |
| 1989 |
} |
| 1990 |
|
| 1991 |
#undef FUNC_NAME |
| 1992 |
|
| 1993 |
SCM_DEFINE (scm_gnutls_set_certificate_credentials_verify_flags_x, |
| 1994 |
"set-certificate-credentials-verify-flags!", |
| 1995 |
1, 0, 1, |
| 1996 |
(SCM cred, SCM flags), |
| 1997 |
"Set the certificate verification flags to @var{flags}, a " |
| 1998 |
"series of @code{certificate-verify} values.") |
| 1999 |
#define FUNC_NAME s_scm_gnutls_set_certificate_credentials_verify_flags_x |
| 2000 |
{ |
| 2001 |
unsigned int c_flags, c_pos; |
| 2002 |
gnutls_certificate_credentials_t c_cred; |
| 2003 |
|
| 2004 |
c_cred = scm_to_gnutls_certificate_credentials (cred, 1, FUNC_NAME); |
| 2005 |
|
| 2006 |
for (c_flags = 0, c_pos = 2; |
| 2007 |
!scm_is_null (flags); flags = SCM_CDR (flags), c_pos++) |
| 2008 |
{ |
| 2009 |
c_flags |= (unsigned int) |
| 2010 |
scm_to_gnutls_certificate_verify (SCM_CAR (flags), c_pos, FUNC_NAME); |
| 2011 |
} |
| 2012 |
|
| 2013 |
gnutls_certificate_set_verify_flags (c_cred, c_flags); |
| 2014 |
|
| 2015 |
return SCM_UNSPECIFIED; |
| 2016 |
} |
| 2017 |
|
| 2018 |
#undef FUNC_NAME |
| 2019 |
|
| 2020 |
SCM_DEFINE (scm_gnutls_peer_certificate_status, "peer-certificate-status", |
| 2021 |
1, 0, 0, |
| 2022 |
(SCM session), |
| 2023 |
"Verify the peer certificate for @var{session} and return " |
| 2024 |
"a list of @code{certificate-status} values (such as " |
| 2025 |
"@code{certificate-status/revoked}), or the empty list if " |
| 2026 |
"the certificate is valid.") |
| 2027 |
#define FUNC_NAME s_scm_gnutls_peer_certificate_status |
| 2028 |
{ |
| 2029 |
int err; |
| 2030 |
unsigned int c_status; |
| 2031 |
gnutls_session_t c_session; |
| 2032 |
SCM result = SCM_EOL; |
| 2033 |
|
| 2034 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 2035 |
|
| 2036 |
err = gnutls_certificate_verify_peers2 (c_session, &c_status); |
| 2037 |
if (EXPECT_FALSE (err)) |
| 2038 |
scm_gnutls_error (err, FUNC_NAME); |
| 2039 |
|
| 2040 |
#define MATCH_STATUS(_value) \ |
| 2041 |
if (c_status & (_value)) \ |
| 2042 |
{ \ |
| 2043 |
result = scm_cons (scm_from_gnutls_certificate_status (_value), \ |
| 2044 |
result); \ |
| 2045 |
c_status &= ~(_value); \ |
| 2046 |
} |
| 2047 |
|
| 2048 |
MATCH_STATUS (GNUTLS_CERT_INVALID); |
| 2049 |
MATCH_STATUS (GNUTLS_CERT_REVOKED); |
| 2050 |
MATCH_STATUS (GNUTLS_CERT_SIGNER_NOT_FOUND); |
| 2051 |
MATCH_STATUS (GNUTLS_CERT_SIGNER_NOT_CA); |
| 2052 |
MATCH_STATUS (GNUTLS_CERT_INSECURE_ALGORITHM); |
| 2053 |
MATCH_STATUS (GNUTLS_CERT_NOT_ACTIVATED); |
| 2054 |
MATCH_STATUS (GNUTLS_CERT_EXPIRED); |
| 2055 |
MATCH_STATUS (GNUTLS_CERT_SIGNATURE_FAILURE); |
| 2056 |
MATCH_STATUS (GNUTLS_CERT_REVOCATION_DATA_SUPERSEDED); |
| 2057 |
MATCH_STATUS (GNUTLS_CERT_UNEXPECTED_OWNER); |
| 2058 |
MATCH_STATUS (GNUTLS_CERT_REVOCATION_DATA_ISSUED_IN_FUTURE); |
| 2059 |
MATCH_STATUS (GNUTLS_CERT_SIGNER_CONSTRAINTS_FAILURE); |
| 2060 |
MATCH_STATUS (GNUTLS_CERT_MISMATCH); |
| 2061 |
MATCH_STATUS (GNUTLS_CERT_PURPOSE_MISMATCH); |
| 2062 |
MATCH_STATUS (GNUTLS_CERT_MISSING_OCSP_STATUS); |
| 2063 |
MATCH_STATUS (GNUTLS_CERT_INVALID_OCSP_STATUS); |
| 2064 |
MATCH_STATUS (GNUTLS_CERT_UNKNOWN_CRIT_EXTENSIONS); |
| 2065 |
|
| 2066 |
if (EXPECT_FALSE (c_status != 0)) |
| 2067 |
/* XXX: We failed to interpret one of the status flags. */ |
| 2068 |
scm_gnutls_error (GNUTLS_E_UNIMPLEMENTED_FEATURE, FUNC_NAME); |
| 2069 |
|
| 2070 |
#undef MATCH_STATUS |
| 2071 |
|
| 2072 |
return (result); |
| 2073 |
} |
| 2074 |
|
| 2075 |
#undef FUNC_NAME |
| 2076 |
|
| 2077 |
|
| 2078 |
/* SRP credentials. */ |
| 2079 |
|
| 2080 |
#ifdef ENABLE_SRP |
| 2081 |
SCM_DEFINE (scm_gnutls_make_srp_server_credentials, |
| 2082 |
"make-srp-server-credentials", |
| 2083 |
0, 0, 0, (void), "Return new SRP server credentials.") |
| 2084 |
# define FUNC_NAME s_scm_gnutls_make_srp_server_credentials |
| 2085 |
{ |
| 2086 |
int err; |
| 2087 |
gnutls_srp_server_credentials_t c_cred; |
| 2088 |
|
| 2089 |
err = gnutls_srp_allocate_server_credentials (&c_cred); |
| 2090 |
if (EXPECT_FALSE (err)) |
| 2091 |
scm_gnutls_error (err, FUNC_NAME); |
| 2092 |
|
| 2093 |
return (scm_from_gnutls_srp_server_credentials (c_cred)); |
| 2094 |
} |
| 2095 |
|
| 2096 |
# undef FUNC_NAME |
| 2097 |
|
| 2098 |
SCM_DEFINE (scm_gnutls_set_srp_server_credentials_files_x, |
| 2099 |
"set-srp-server-credentials-files!", |
| 2100 |
3, 0, 0, |
| 2101 |
(SCM cred, SCM password_file, SCM password_conf_file), |
| 2102 |
"Set the credentials files for @var{cred}, an SRP server " |
| 2103 |
"credentials object.") |
| 2104 |
# define FUNC_NAME s_scm_gnutls_set_srp_server_credentials_files_x |
| 2105 |
{ |
| 2106 |
int err; |
| 2107 |
gnutls_srp_server_credentials_t c_cred; |
| 2108 |
char *c_password_file, *c_password_conf_file; |
| 2109 |
size_t c_password_file_len, c_password_conf_file_len; |
| 2110 |
|
| 2111 |
c_cred = scm_to_gnutls_srp_server_credentials (cred, 1, FUNC_NAME); |
| 2112 |
SCM_VALIDATE_STRING (2, password_file); |
| 2113 |
SCM_VALIDATE_STRING (3, password_conf_file); |
| 2114 |
|
| 2115 |
c_password_file_len = scm_c_string_length (password_file); |
| 2116 |
c_password_conf_file_len = scm_c_string_length (password_conf_file); |
| 2117 |
|
| 2118 |
c_password_file = FAST_ALLOC (c_password_file_len + 1); |
| 2119 |
c_password_conf_file = FAST_ALLOC (c_password_conf_file_len + 1); |
| 2120 |
|
| 2121 |
(void) scm_to_locale_stringbuf (password_file, c_password_file, |
| 2122 |
c_password_file_len + 1); |
| 2123 |
c_password_file[c_password_file_len] = '\0'; |
| 2124 |
(void) scm_to_locale_stringbuf (password_conf_file, c_password_conf_file, |
| 2125 |
c_password_conf_file_len + 1); |
| 2126 |
c_password_conf_file[c_password_conf_file_len] = '\0'; |
| 2127 |
|
| 2128 |
err = gnutls_srp_set_server_credentials_file (c_cred, c_password_file, |
| 2129 |
c_password_conf_file); |
| 2130 |
if (EXPECT_FALSE (err)) |
| 2131 |
scm_gnutls_error (err, FUNC_NAME); |
| 2132 |
|
| 2133 |
return SCM_UNSPECIFIED; |
| 2134 |
} |
| 2135 |
|
| 2136 |
# undef FUNC_NAME |
| 2137 |
|
| 2138 |
SCM_DEFINE (scm_gnutls_make_srp_client_credentials, |
| 2139 |
"make-srp-client-credentials", |
| 2140 |
0, 0, 0, (void), "Return new SRP client credentials.") |
| 2141 |
# define FUNC_NAME s_scm_gnutls_make_srp_client_credentials |
| 2142 |
{ |
| 2143 |
int err; |
| 2144 |
gnutls_srp_client_credentials_t c_cred; |
| 2145 |
|
| 2146 |
err = gnutls_srp_allocate_client_credentials (&c_cred); |
| 2147 |
if (EXPECT_FALSE (err)) |
| 2148 |
scm_gnutls_error (err, FUNC_NAME); |
| 2149 |
|
| 2150 |
return (scm_from_gnutls_srp_client_credentials (c_cred)); |
| 2151 |
} |
| 2152 |
|
| 2153 |
# undef FUNC_NAME |
| 2154 |
|
| 2155 |
|
| 2156 |
SCM_DEFINE (scm_gnutls_set_srp_client_credentials_x, |
| 2157 |
"set-srp-client-credentials!", |
| 2158 |
3, 0, 0, |
| 2159 |
(SCM cred, SCM username, SCM password), |
| 2160 |
"Use @var{username} and @var{password} as the credentials " |
| 2161 |
"for @var{cred}, a client-side SRP credentials object.") |
| 2162 |
# define FUNC_NAME s_scm_gnutls_make_srp_client_credentials |
| 2163 |
{ |
| 2164 |
int err; |
| 2165 |
gnutls_srp_client_credentials_t c_cred; |
| 2166 |
char *c_username, *c_password; |
| 2167 |
size_t c_username_len, c_password_len; |
| 2168 |
|
| 2169 |
c_cred = scm_to_gnutls_srp_client_credentials (cred, 1, FUNC_NAME); |
| 2170 |
SCM_VALIDATE_STRING (2, username); |
| 2171 |
SCM_VALIDATE_STRING (3, password); |
| 2172 |
|
| 2173 |
c_username_len = scm_c_string_length (username); |
| 2174 |
c_password_len = scm_c_string_length (password); |
| 2175 |
|
| 2176 |
c_username = FAST_ALLOC (c_username_len + 1); |
| 2177 |
c_password = FAST_ALLOC (c_password_len + 1); |
| 2178 |
|
| 2179 |
(void) scm_to_locale_stringbuf (username, c_username, c_username_len + 1); |
| 2180 |
c_username[c_username_len] = '\0'; |
| 2181 |
(void) scm_to_locale_stringbuf (password, c_password, c_password_len + 1); |
| 2182 |
c_password[c_password_len] = '\0'; |
| 2183 |
|
| 2184 |
err = gnutls_srp_set_client_credentials (c_cred, c_username, c_password); |
| 2185 |
if (EXPECT_FALSE (err)) |
| 2186 |
scm_gnutls_error (err, FUNC_NAME); |
| 2187 |
|
| 2188 |
return SCM_UNSPECIFIED; |
| 2189 |
} |
| 2190 |
|
| 2191 |
# undef FUNC_NAME |
| 2192 |
|
| 2193 |
SCM_DEFINE (scm_gnutls_server_session_srp_username, |
| 2194 |
"server-session-srp-username", |
| 2195 |
1, 0, 0, |
| 2196 |
(SCM session), |
| 2197 |
"Return the SRP username used in @var{session} (a server-side " |
| 2198 |
"session).") |
| 2199 |
# define FUNC_NAME s_scm_gnutls_server_session_srp_username |
| 2200 |
{ |
| 2201 |
SCM result; |
| 2202 |
const char *c_username; |
| 2203 |
gnutls_session_t c_session; |
| 2204 |
|
| 2205 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 2206 |
c_username = gnutls_srp_server_get_username (c_session); |
| 2207 |
|
| 2208 |
if (EXPECT_FALSE (c_username == NULL)) |
| 2209 |
result = SCM_BOOL_F; |
| 2210 |
else |
| 2211 |
result = scm_from_locale_string (c_username); |
| 2212 |
|
| 2213 |
return (result); |
| 2214 |
} |
| 2215 |
|
| 2216 |
# undef FUNC_NAME |
| 2217 |
|
| 2218 |
SCM_DEFINE (scm_gnutls_srp_base64_encode, "srp-base64-encode", |
| 2219 |
1, 0, 0, |
| 2220 |
(SCM str), |
| 2221 |
"Encode @var{str} using SRP's base64 algorithm. Return " |
| 2222 |
"the encoded string.") |
| 2223 |
# define FUNC_NAME s_scm_gnutls_srp_base64_encode |
| 2224 |
{ |
| 2225 |
int err; |
| 2226 |
char *c_str, *c_result; |
| 2227 |
size_t c_str_len, c_result_len, c_result_actual_len; |
| 2228 |
gnutls_datum_t c_str_d; |
| 2229 |
|
| 2230 |
SCM_VALIDATE_STRING (1, str); |
| 2231 |
|
| 2232 |
c_str_len = scm_c_string_length (str); |
| 2233 |
c_str = FAST_ALLOC (c_str_len + 1); |
| 2234 |
(void) scm_to_locale_stringbuf (str, c_str, c_str_len + 1); |
| 2235 |
c_str[c_str_len] = '\0'; |
| 2236 |
|
| 2237 |
/* Typical size ratio is 4/3 so 3/2 is an upper bound. */ |
| 2238 |
c_result_len = (c_str_len * 3) / 2; |
| 2239 |
c_result = (char *) scm_malloc (c_result_len); |
| 2240 |
if (EXPECT_FALSE (c_result == NULL)) |
| 2241 |
scm_gnutls_error (GNUTLS_E_MEMORY_ERROR, FUNC_NAME); |
| 2242 |
|
| 2243 |
c_str_d.data = (unsigned char *) c_str; |
| 2244 |
c_str_d.size = c_str_len; |
| 2245 |
|
| 2246 |
do |
| 2247 |
{ |
| 2248 |
c_result_actual_len = c_result_len; |
| 2249 |
err = gnutls_srp_base64_encode (&c_str_d, c_result, |
| 2250 |
&c_result_actual_len); |
| 2251 |
if (err == GNUTLS_E_SHORT_MEMORY_BUFFER) |
| 2252 |
{ |
| 2253 |
char *c_new_buf; |
| 2254 |
|
| 2255 |
c_new_buf = scm_realloc (c_result, c_result_len * 2); |
| 2256 |
if (EXPECT_FALSE (c_new_buf == NULL)) |
| 2257 |
{ |
| 2258 |
free (c_result); |
| 2259 |
scm_gnutls_error (GNUTLS_E_MEMORY_ERROR, FUNC_NAME); |
| 2260 |
} |
| 2261 |
else |
| 2262 |
c_result = c_new_buf, c_result_len *= 2; |
| 2263 |
} |
| 2264 |
} |
| 2265 |
while (EXPECT_FALSE (err == GNUTLS_E_SHORT_MEMORY_BUFFER)); |
| 2266 |
|
| 2267 |
if (EXPECT_FALSE (err)) |
| 2268 |
scm_gnutls_error (err, FUNC_NAME); |
| 2269 |
|
| 2270 |
if (c_result_actual_len + 1 < c_result_len) |
| 2271 |
/* Shrink the buffer. */ |
| 2272 |
c_result = scm_realloc (c_result, c_result_actual_len + 1); |
| 2273 |
|
| 2274 |
c_result[c_result_actual_len] = '\0'; |
| 2275 |
|
| 2276 |
return (scm_take_locale_string (c_result)); |
| 2277 |
} |
| 2278 |
|
| 2279 |
# undef FUNC_NAME |
| 2280 |
|
| 2281 |
SCM_DEFINE (scm_gnutls_srp_base64_decode, "srp-base64-decode", |
| 2282 |
1, 0, 0, |
| 2283 |
(SCM str), |
| 2284 |
"Decode @var{str}, an SRP-base64 encoded string, and return " |
| 2285 |
"the decoded string.") |
| 2286 |
# define FUNC_NAME s_scm_gnutls_srp_base64_decode |
| 2287 |
{ |
| 2288 |
int err; |
| 2289 |
char *c_str, *c_result; |
| 2290 |
size_t c_str_len, c_result_len, c_result_actual_len; |
| 2291 |
gnutls_datum_t c_str_d; |
| 2292 |
|
| 2293 |
SCM_VALIDATE_STRING (1, str); |
| 2294 |
|
| 2295 |
c_str_len = scm_c_string_length (str); |
| 2296 |
c_str = FAST_ALLOC (c_str_len + 1); |
| 2297 |
(void) scm_to_locale_stringbuf (str, c_str, c_str_len + 1); |
| 2298 |
c_str[c_str_len] = '\0'; |
| 2299 |
|
| 2300 |
/* We assume that the decoded string is smaller than the encoded |
| 2301 |
string. */ |
| 2302 |
c_result_len = c_str_len; |
| 2303 |
c_result = FAST_ALLOC (c_result_len + 1); |
| 2304 |
|
| 2305 |
c_str_d.data = (unsigned char *) c_str; |
| 2306 |
c_str_d.size = c_str_len; |
| 2307 |
|
| 2308 |
c_result_actual_len = c_result_len; |
| 2309 |
err = gnutls_srp_base64_decode (&c_str_d, c_result, &c_result_actual_len); |
| 2310 |
if (EXPECT_FALSE (err)) |
| 2311 |
scm_gnutls_error (err, FUNC_NAME); |
| 2312 |
|
| 2313 |
c_result[c_result_actual_len] = '\0'; |
| 2314 |
|
| 2315 |
return (scm_from_locale_string (c_result)); |
| 2316 |
} |
| 2317 |
|
| 2318 |
# undef FUNC_NAME |
| 2319 |
#endif /* ENABLE_SRP */ |
| 2320 |
|
| 2321 |
|
| 2322 |
/* PSK credentials. */ |
| 2323 |
|
| 2324 |
SCM_DEFINE (scm_gnutls_make_psk_server_credentials, |
| 2325 |
"make-psk-server-credentials", |
| 2326 |
0, 0, 0, (void), "Return new PSK server credentials.") |
| 2327 |
#define FUNC_NAME s_scm_gnutls_make_psk_server_credentials |
| 2328 |
{ |
| 2329 |
int err; |
| 2330 |
gnutls_psk_server_credentials_t c_cred; |
| 2331 |
|
| 2332 |
err = gnutls_psk_allocate_server_credentials (&c_cred); |
| 2333 |
if (EXPECT_FALSE (err)) |
| 2334 |
scm_gnutls_error (err, FUNC_NAME); |
| 2335 |
|
| 2336 |
return (scm_from_gnutls_psk_server_credentials (c_cred)); |
| 2337 |
} |
| 2338 |
|
| 2339 |
#undef FUNC_NAME |
| 2340 |
|
| 2341 |
SCM_DEFINE (scm_gnutls_set_psk_server_credentials_file_x, |
| 2342 |
"set-psk-server-credentials-file!", |
| 2343 |
2, 0, 0, |
| 2344 |
(SCM cred, SCM file), |
| 2345 |
"Use @var{file} as the password file for PSK server " |
| 2346 |
"credentials @var{cred}.") |
| 2347 |
#define FUNC_NAME s_scm_gnutls_set_psk_server_credentials_file_x |
| 2348 |
{ |
| 2349 |
int err; |
| 2350 |
gnutls_psk_server_credentials_t c_cred; |
| 2351 |
char *c_file; |
| 2352 |
size_t c_file_len; |
| 2353 |
|
| 2354 |
c_cred = scm_to_gnutls_psk_server_credentials (cred, 1, FUNC_NAME); |
| 2355 |
SCM_VALIDATE_STRING (2, file); |
| 2356 |
|
| 2357 |
c_file_len = scm_c_string_length (file); |
| 2358 |
c_file = FAST_ALLOC (c_file_len + 1); |
| 2359 |
|
| 2360 |
(void) scm_to_locale_stringbuf (file, c_file, c_file_len + 1); |
| 2361 |
c_file[c_file_len] = '\0'; |
| 2362 |
|
| 2363 |
err = gnutls_psk_set_server_credentials_file (c_cred, c_file); |
| 2364 |
if (EXPECT_FALSE (err)) |
| 2365 |
scm_gnutls_error (err, FUNC_NAME); |
| 2366 |
|
| 2367 |
return SCM_UNSPECIFIED; |
| 2368 |
} |
| 2369 |
|
| 2370 |
#undef FUNC_NAME |
| 2371 |
|
| 2372 |
SCM_DEFINE (scm_gnutls_make_psk_client_credentials, |
| 2373 |
"make-psk-client-credentials", |
| 2374 |
0, 0, 0, (void), "Return a new PSK client credentials object.") |
| 2375 |
#define FUNC_NAME s_scm_gnutls_make_psk_client_credentials |
| 2376 |
{ |
| 2377 |
int err; |
| 2378 |
gnutls_psk_client_credentials_t c_cred; |
| 2379 |
|
| 2380 |
err = gnutls_psk_allocate_client_credentials (&c_cred); |
| 2381 |
if (EXPECT_FALSE (err)) |
| 2382 |
scm_gnutls_error (err, FUNC_NAME); |
| 2383 |
|
| 2384 |
return (scm_from_gnutls_psk_client_credentials (c_cred)); |
| 2385 |
} |
| 2386 |
|
| 2387 |
#undef FUNC_NAME |
| 2388 |
|
| 2389 |
SCM_DEFINE (scm_gnutls_set_psk_client_credentials_x, |
| 2390 |
"set-psk-client-credentials!", |
| 2391 |
4, 0, 0, |
| 2392 |
(SCM cred, SCM username, SCM key, SCM key_format), |
| 2393 |
"Set the client credentials for @var{cred}, a PSK client " |
| 2394 |
"credentials object.") |
| 2395 |
#define FUNC_NAME s_scm_gnutls_set_psk_client_credentials_x |
| 2396 |
{ |
| 2397 |
int err; |
| 2398 |
gnutls_psk_client_credentials_t c_cred; |
| 2399 |
gnutls_psk_key_flags c_key_format; |
| 2400 |
scm_t_array_handle c_handle; |
| 2401 |
const char *c_key; |
| 2402 |
char *c_username; |
| 2403 |
size_t c_username_len, c_key_len; |
| 2404 |
gnutls_datum_t c_datum; |
| 2405 |
|
| 2406 |
c_cred = scm_to_gnutls_psk_client_credentials (cred, 1, FUNC_NAME); |
| 2407 |
SCM_VALIDATE_STRING (2, username); |
| 2408 |
SCM_VALIDATE_ARRAY (3, key); |
| 2409 |
c_key_format = scm_to_gnutls_psk_key_format (key_format, 4, FUNC_NAME); |
| 2410 |
|
| 2411 |
c_username_len = scm_c_string_length (username); |
| 2412 |
c_username = FAST_ALLOC (c_username_len + 1); |
| 2413 |
|
| 2414 |
(void) scm_to_locale_stringbuf (username, c_username, c_username_len + 1); |
| 2415 |
c_username[c_username_len] = '\0'; |
| 2416 |
|
| 2417 |
c_key = scm_gnutls_get_array (key, &c_handle, &c_key_len, FUNC_NAME); |
| 2418 |
c_datum.data = (unsigned char *) c_key; |
| 2419 |
c_datum.size = c_key_len; |
| 2420 |
|
| 2421 |
err = gnutls_psk_set_client_credentials (c_cred, c_username, |
| 2422 |
&c_datum, c_key_format); |
| 2423 |
scm_gnutls_release_array (&c_handle); |
| 2424 |
|
| 2425 |
if (EXPECT_FALSE (err)) |
| 2426 |
scm_gnutls_error (err, FUNC_NAME); |
| 2427 |
|
| 2428 |
return SCM_UNSPECIFIED; |
| 2429 |
} |
| 2430 |
|
| 2431 |
#undef FUNC_NAME |
| 2432 |
|
| 2433 |
SCM_DEFINE (scm_gnutls_server_session_psk_username, |
| 2434 |
"server-session-psk-username", |
| 2435 |
1, 0, 0, |
| 2436 |
(SCM session), |
| 2437 |
"Return the username associated with PSK server session " |
| 2438 |
"@var{session}.") |
| 2439 |
#define FUNC_NAME s_scm_gnutls_server_session_psk_username |
| 2440 |
{ |
| 2441 |
SCM result; |
| 2442 |
const char *c_username; |
| 2443 |
gnutls_session_t c_session; |
| 2444 |
|
| 2445 |
c_session = scm_to_gnutls_session (session, 1, FUNC_NAME); |
| 2446 |
c_username = gnutls_srp_server_get_username (c_session); |
| 2447 |
|
| 2448 |
if (EXPECT_FALSE (c_username == NULL)) |
| 2449 |
result = SCM_BOOL_F; |
| 2450 |
else |
| 2451 |
result = scm_from_locale_string (c_username); |
| 2452 |
|
| 2453 |
return (result); |
| 2454 |
} |
| 2455 |
|
| 2456 |
#undef FUNC_NAME |
| 2457 |
|
| 2458 |
|
| 2459 |
/* X.509 certificates. */ |
| 2460 |
|
| 2461 |
typedef int (*x509_export_function_t) (void *, gnutls_x509_crt_fmt_t, |
| 2462 |
uint8_t *, size_t *); |
| 2463 |
|
| 2464 |
static inline SCM |
| 2465 |
x509_export (x509_export_function_t export, void *cert_or_key, |
| 2466 |
gnutls_x509_crt_fmt_t format, const char *func_name) |
| 2467 |
#define FUNC_NAME func_name |
| 2468 |
{ |
| 2469 |
int err; |
| 2470 |
uint8_t *output; |
| 2471 |
size_t output_len, output_total_len = 4096; |
| 2472 |
|
| 2473 |
output = (uint8_t *) scm_gc_malloc (output_total_len, func_name); |
| 2474 |
do |
| 2475 |
{ |
| 2476 |
output_len = output_total_len; |
| 2477 |
err = export (cert_or_key, format, output, &output_len); |
| 2478 |
|
| 2479 |
if (err == GNUTLS_E_SHORT_MEMORY_BUFFER) |
| 2480 |
{ |
| 2481 |
output = scm_gc_realloc (output, output_total_len, |
| 2482 |
output_total_len * 2, func_name); |
| 2483 |
output_total_len *= 2; |
| 2484 |
} |
| 2485 |
} |
| 2486 |
while (err == GNUTLS_E_SHORT_MEMORY_BUFFER); |
| 2487 |
|
| 2488 |
if (EXPECT_FALSE (err)) |
| 2489 |
{ |
| 2490 |
scm_gc_free (output, output_total_len, func_name); |
| 2491 |
scm_gnutls_error (err, FUNC_NAME); |
| 2492 |
} |
| 2493 |
|
| 2494 |
if (output_len != output_total_len) |
| 2495 |
/* Shrink the output buffer. */ |
| 2496 |
output = scm_gc_realloc (output, output_total_len, output_len, func_name); |
| 2497 |
|
| 2498 |
return (scm_take_u8vector (output, output_len)); |
| 2499 |
} |
| 2500 |
|
| 2501 |
#undef FUNC_NAME |
| 2502 |
|
| 2503 |
SCM_DEFINE (scm_gnutls_make_x509_certificate, "make-x509-certificate", |
| 2504 |
0, 0, 0, (), "Return a new, empty X.509 certificate object.") |
| 2505 |
#define FUNC_NAME s_scm_gnutls_make_x509_certificate |
| 2506 |
{ |
| 2507 |
int err; |
| 2508 |
gnutls_x509_crt_t c_cert; |
| 2509 |
|
| 2510 |
err = gnutls_x509_crt_init (&c_cert); |
| 2511 |
if (EXPECT_FALSE (err)) |
| 2512 |
scm_gnutls_error (err, FUNC_NAME); |
| 2513 |
|
| 2514 |
return (scm_from_gnutls_x509_certificate (c_cert)); |
| 2515 |
} |
| 2516 |
|
| 2517 |
#undef FUNC_NAME |
| 2518 |
|
| 2519 |
SCM_DEFINE (scm_gnutls_import_x509_certificate, "import-x509-certificate", |
| 2520 |
2, 0, 0, |
| 2521 |
(SCM data, SCM format), |
| 2522 |
"Return a new X.509 certificate object resulting from the " |
| 2523 |
"import of @var{data} (a uniform array) according to " |
| 2524 |
"@var{format}.") |
| 2525 |
#define FUNC_NAME s_scm_gnutls_import_x509_certificate |
| 2526 |
{ |
| 2527 |
int err; |
| 2528 |
gnutls_x509_crt_t c_cert; |
| 2529 |
gnutls_x509_crt_fmt_t c_format; |
| 2530 |
gnutls_datum_t c_data_d; |
| 2531 |
scm_t_array_handle c_data_handle; |
| 2532 |
const char *c_data; |
| 2533 |
size_t c_data_len; |
| 2534 |
|
| 2535 |
SCM_VALIDATE_ARRAY (1, data); |
| 2536 |
c_format = scm_to_gnutls_x509_certificate_format (format, 2, FUNC_NAME); |
| 2537 |
|
| 2538 |
c_data = scm_gnutls_get_array (data, &c_data_handle, &c_data_len, |
| 2539 |
FUNC_NAME); |
| 2540 |
c_data_d.data = (unsigned char *) c_data; |
| 2541 |
c_data_d.size = c_data_len; |
| 2542 |
|
| 2543 |
err = gnutls_x509_crt_init (&c_cert); |
| 2544 |
if (EXPECT_FALSE (err)) |
| 2545 |
{ |
| 2546 |
scm_gnutls_release_array (&c_data_handle); |
| 2547 |
scm_gnutls_error (err, FUNC_NAME); |
| 2548 |
} |
| 2549 |
|
| 2550 |
err = gnutls_x509_crt_import (c_cert, &c_data_d, c_format); |
| 2551 |
scm_gnutls_release_array (&c_data_handle); |
| 2552 |
|
| 2553 |
if (EXPECT_FALSE (err)) |
| 2554 |
{ |
| 2555 |
gnutls_x509_crt_deinit (c_cert); |
| 2556 |
scm_gnutls_error (err, FUNC_NAME); |
| 2557 |
} |
| 2558 |
|
| 2559 |
return (scm_from_gnutls_x509_certificate (c_cert)); |
| 2560 |
} |
| 2561 |
|
| 2562 |
#undef FUNC_NAME |
| 2563 |
|
| 2564 |
SCM_DEFINE (scm_gnutls_export_x509_certificate, "export-x509-certificate", |
| 2565 |
2, 0, 0, |
| 2566 |
(SCM cert, SCM format), |
| 2567 |
"Return a bytevector resulting from the export of @var{cert} " |
| 2568 |
"(an X.509 certificate) according to @var{format}.") |
| 2569 |
#define FUNC_NAME s_scm_gnutls_export_x509_certificate |
| 2570 |
{ |
| 2571 |
SCM result; |
| 2572 |
gnutls_x509_crt_t c_cert; |
| 2573 |
gnutls_x509_crt_fmt_t c_format; |
| 2574 |
|
| 2575 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 2576 |
c_format = scm_to_gnutls_x509_certificate_format (format, 2, FUNC_NAME); |
| 2577 |
result = x509_export ((x509_export_function_t) gnutls_x509_crt_export, |
| 2578 |
c_cert, c_format, FUNC_NAME); |
| 2579 |
|
| 2580 |
return result; |
| 2581 |
} |
| 2582 |
|
| 2583 |
#undef FUNC_NAME |
| 2584 |
|
| 2585 |
SCM_DEFINE (scm_gnutls_generate_x509_private_key, "generate-x509-private-key", |
| 2586 |
3, 0, 0, |
| 2587 |
(SCM algorithm, SCM bits, SCM flags), |
| 2588 |
"Return a new X.509 private key object of size @var{bits} " |
| 2589 |
"generated using @var{algorithm}, a pk-algorithm enum value, and" |
| 2590 |
"@var{flags}, a list of privkey enum values.") |
| 2591 |
#define FUNC_NAME s_scm_gnutls_generate_x509_private_key |
| 2592 |
{ |
| 2593 |
int err; |
| 2594 |
gnutls_x509_privkey_t c_key; |
| 2595 |
gnutls_pk_algorithm_t c_algo; |
| 2596 |
unsigned int c_bits, c_flags; |
| 2597 |
|
| 2598 |
c_algo = scm_to_gnutls_pk_algorithm (algorithm, 1, FUNC_NAME); |
| 2599 |
c_bits = scm_to_uint (bits); |
| 2600 |
|
| 2601 |
for (c_flags = 0; !scm_is_null (flags); flags = SCM_CDR (flags)) |
| 2602 |
{ |
| 2603 |
c_flags |= (unsigned int) |
| 2604 |
scm_to_gnutls_privkey (SCM_CAR (flags), 3, FUNC_NAME); |
| 2605 |
} |
| 2606 |
|
| 2607 |
err = gnutls_x509_privkey_init (&c_key); |
| 2608 |
if (EXPECT_FALSE (err)) |
| 2609 |
scm_gnutls_error (err, FUNC_NAME); |
| 2610 |
|
| 2611 |
err = gnutls_x509_privkey_generate (c_key, c_algo, c_bits, c_flags); |
| 2612 |
if (EXPECT_FALSE (err)) |
| 2613 |
scm_gnutls_error (err, FUNC_NAME); |
| 2614 |
|
| 2615 |
return (scm_from_gnutls_x509_private_key (c_key)); |
| 2616 |
} |
| 2617 |
|
| 2618 |
#undef FUNC_NAME |
| 2619 |
|
| 2620 |
SCM_DEFINE (scm_gnutls_import_x509_private_key, "import-x509-private-key", |
| 2621 |
2, 0, 0, |
| 2622 |
(SCM data, SCM format), |
| 2623 |
"Return a new X.509 private key object resulting from the " |
| 2624 |
"import of @var{data} (a uniform array) according to " |
| 2625 |
"@var{format}.") |
| 2626 |
#define FUNC_NAME s_scm_gnutls_import_x509_private_key |
| 2627 |
{ |
| 2628 |
int err; |
| 2629 |
gnutls_x509_privkey_t c_key; |
| 2630 |
gnutls_x509_crt_fmt_t c_format; |
| 2631 |
gnutls_datum_t c_data_d; |
| 2632 |
scm_t_array_handle c_data_handle; |
| 2633 |
const char *c_data; |
| 2634 |
size_t c_data_len; |
| 2635 |
|
| 2636 |
SCM_VALIDATE_ARRAY (1, data); |
| 2637 |
c_format = scm_to_gnutls_x509_certificate_format (format, 2, FUNC_NAME); |
| 2638 |
|
| 2639 |
c_data = scm_gnutls_get_array (data, &c_data_handle, &c_data_len, |
| 2640 |
FUNC_NAME); |
| 2641 |
c_data_d.data = (unsigned char *) c_data; |
| 2642 |
c_data_d.size = c_data_len; |
| 2643 |
|
| 2644 |
err = gnutls_x509_privkey_init (&c_key); |
| 2645 |
if (EXPECT_FALSE (err)) |
| 2646 |
{ |
| 2647 |
scm_gnutls_release_array (&c_data_handle); |
| 2648 |
scm_gnutls_error (err, FUNC_NAME); |
| 2649 |
} |
| 2650 |
|
| 2651 |
err = gnutls_x509_privkey_import (c_key, &c_data_d, c_format); |
| 2652 |
scm_gnutls_release_array (&c_data_handle); |
| 2653 |
|
| 2654 |
if (EXPECT_FALSE (err)) |
| 2655 |
{ |
| 2656 |
gnutls_x509_privkey_deinit (c_key); |
| 2657 |
scm_gnutls_error (err, FUNC_NAME); |
| 2658 |
} |
| 2659 |
|
| 2660 |
return (scm_from_gnutls_x509_private_key (c_key)); |
| 2661 |
} |
| 2662 |
|
| 2663 |
#undef FUNC_NAME |
| 2664 |
|
| 2665 |
SCM_DEFINE (scm_gnutls_export_x509_private_key, "export-x509-private-key", |
| 2666 |
2, 0, 0, |
| 2667 |
(SCM key, SCM format), |
| 2668 |
"Return a bytevector resulting from the export of @var{key} " |
| 2669 |
"(an X.509 private key) according to @var{format}.") |
| 2670 |
#define FUNC_NAME s_scm_gnutls_export_x509_private_key |
| 2671 |
{ |
| 2672 |
SCM result; |
| 2673 |
gnutls_x509_privkey_t c_key; |
| 2674 |
gnutls_x509_crt_fmt_t c_format; |
| 2675 |
|
| 2676 |
c_key = scm_to_gnutls_x509_private_key (key, 1, FUNC_NAME); |
| 2677 |
c_format = scm_to_gnutls_x509_certificate_format (format, 2, FUNC_NAME); |
| 2678 |
result = x509_export ((x509_export_function_t) gnutls_x509_privkey_export, |
| 2679 |
c_key, c_format, FUNC_NAME); |
| 2680 |
|
| 2681 |
return result; |
| 2682 |
} |
| 2683 |
|
| 2684 |
#undef FUNC_NAME |
| 2685 |
|
| 2686 |
SCM_DEFINE (scm_gnutls_pkcs8_import_x509_private_key, |
| 2687 |
"pkcs8-import-x509-private-key", |
| 2688 |
2, 2, 0, |
| 2689 |
(SCM data, SCM format, SCM pass, SCM encrypted), |
| 2690 |
"Return a new X.509 private key object resulting from the " |
| 2691 |
"import of @var{data} (a uniform array) according to " |
| 2692 |
"@var{format}. Optionally, if @var{pass} is not @code{#f}, " |
| 2693 |
"it should be a string denoting a passphrase. " |
| 2694 |
"@var{encrypted} tells whether the private key is encrypted " |
| 2695 |
"(@code{#t} by default).") |
| 2696 |
#define FUNC_NAME s_scm_gnutls_pkcs8_import_x509_private_key |
| 2697 |
{ |
| 2698 |
int err; |
| 2699 |
gnutls_x509_privkey_t c_key; |
| 2700 |
gnutls_x509_crt_fmt_t c_format; |
| 2701 |
unsigned int c_flags; |
| 2702 |
gnutls_datum_t c_data_d; |
| 2703 |
scm_t_array_handle c_data_handle; |
| 2704 |
const char *c_data; |
| 2705 |
char *c_pass; |
| 2706 |
size_t c_data_len, c_pass_len; |
| 2707 |
|
| 2708 |
SCM_VALIDATE_ARRAY (1, data); |
| 2709 |
c_format = scm_to_gnutls_x509_certificate_format (format, 2, FUNC_NAME); |
| 2710 |
if ((pass == SCM_UNDEFINED) || (scm_is_false (pass))) |
| 2711 |
c_pass = NULL; |
| 2712 |
else |
| 2713 |
{ |
| 2714 |
c_pass_len = scm_c_string_length (pass); |
| 2715 |
c_pass = FAST_ALLOC (c_pass_len + 1); |
| 2716 |
(void) scm_to_locale_stringbuf (pass, c_pass, c_pass_len + 1); |
| 2717 |
c_pass[c_pass_len] = '\0'; |
| 2718 |
} |
| 2719 |
|
| 2720 |
if (encrypted == SCM_UNDEFINED) |
| 2721 |
c_flags = 0; |
| 2722 |
else |
| 2723 |
{ |
| 2724 |
SCM_VALIDATE_BOOL (4, encrypted); |
| 2725 |
if (scm_is_true (encrypted)) |
| 2726 |
c_flags = 0; |
| 2727 |
else |
| 2728 |
c_flags = GNUTLS_PKCS8_PLAIN; |
| 2729 |
} |
| 2730 |
|
| 2731 |
c_data = scm_gnutls_get_array (data, &c_data_handle, &c_data_len, |
| 2732 |
FUNC_NAME); |
| 2733 |
c_data_d.data = (unsigned char *) c_data; |
| 2734 |
c_data_d.size = c_data_len; |
| 2735 |
|
| 2736 |
err = gnutls_x509_privkey_init (&c_key); |
| 2737 |
if (EXPECT_FALSE (err)) |
| 2738 |
{ |
| 2739 |
scm_gnutls_release_array (&c_data_handle); |
| 2740 |
scm_gnutls_error (err, FUNC_NAME); |
| 2741 |
} |
| 2742 |
|
| 2743 |
err = gnutls_x509_privkey_import_pkcs8 (c_key, &c_data_d, c_format, c_pass, |
| 2744 |
c_flags); |
| 2745 |
scm_gnutls_release_array (&c_data_handle); |
| 2746 |
|
| 2747 |
if (EXPECT_FALSE (err)) |
| 2748 |
{ |
| 2749 |
gnutls_x509_privkey_deinit (c_key); |
| 2750 |
scm_gnutls_error (err, FUNC_NAME); |
| 2751 |
} |
| 2752 |
|
| 2753 |
return (scm_from_gnutls_x509_private_key (c_key)); |
| 2754 |
} |
| 2755 |
|
| 2756 |
#undef FUNC_NAME |
| 2757 |
|
| 2758 |
/* Provide the body of a `get_dn' function. */ |
| 2759 |
#define X509_CERTIFICATE_DN_FUNCTION_BODY(get_the_dn) \ |
| 2760 |
int err; \ |
| 2761 |
gnutls_x509_crt_t c_cert; \ |
| 2762 |
char *c_dn; \ |
| 2763 |
size_t c_dn_len; \ |
| 2764 |
\ |
| 2765 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); \ |
| 2766 |
\ |
| 2767 |
/* Get the DN size. */ \ |
| 2768 |
(void) get_the_dn (c_cert, NULL, &c_dn_len); \ |
| 2769 |
\ |
| 2770 |
/* Get the DN itself. */ \ |
| 2771 |
c_dn = FAST_ALLOC (c_dn_len); \ |
| 2772 |
err = get_the_dn (c_cert, c_dn, &c_dn_len); \ |
| 2773 |
\ |
| 2774 |
if (EXPECT_FALSE (err)) \ |
| 2775 |
scm_gnutls_error (err, FUNC_NAME); \ |
| 2776 |
\ |
| 2777 |
/* XXX: The returned string is actually ASCII or UTF-8. */ \ |
| 2778 |
return (scm_from_locale_string (c_dn)); |
| 2779 |
|
| 2780 |
SCM_DEFINE (scm_gnutls_x509_certificate_dn, "x509-certificate-dn", |
| 2781 |
1, 0, 0, |
| 2782 |
(SCM cert), |
| 2783 |
"Return the distinguished name (DN) of X.509 certificate " |
| 2784 |
"@var{cert}. The form of the DN is as described in @uref{" |
| 2785 |
"https://tools.ietf.org/html/rfc2253, RFC 2253}.") |
| 2786 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_dn |
| 2787 |
{ |
| 2788 |
X509_CERTIFICATE_DN_FUNCTION_BODY (gnutls_x509_crt_get_dn); |
| 2789 |
} |
| 2790 |
|
| 2791 |
#undef FUNC_NAME |
| 2792 |
|
| 2793 |
SCM_DEFINE (scm_gnutls_x509_certificate_issuer_dn, |
| 2794 |
"x509-certificate-issuer-dn", |
| 2795 |
1, 0, 0, |
| 2796 |
(SCM cert), |
| 2797 |
"Return the distinguished name (DN) of X.509 certificate " |
| 2798 |
"@var{cert}.") |
| 2799 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_issuer_dn |
| 2800 |
{ |
| 2801 |
X509_CERTIFICATE_DN_FUNCTION_BODY (gnutls_x509_crt_get_issuer_dn); |
| 2802 |
} |
| 2803 |
|
| 2804 |
#undef FUNC_NAME |
| 2805 |
|
| 2806 |
#undef X509_CERTIFICATE_DN_FUNCTION_BODY |
| 2807 |
|
| 2808 |
|
| 2809 |
/* Provide the body of a get_dn_by_oid function. */ |
| 2810 |
#define X509_CERTIFICATE_DN_BY_OID_FUNCTION_BODY(get_dn_by_oid) \ |
| 2811 |
int err; \ |
| 2812 |
gnutls_x509_crt_t c_cert; \ |
| 2813 |
const char *c_oid; \ |
| 2814 |
unsigned int c_index; \ |
| 2815 |
unsigned int c_raw_flag; \ |
| 2816 |
char* c_name; \ |
| 2817 |
size_t c_name_actual_len, c_name_len; \ |
| 2818 |
\ |
| 2819 |
SCM result; \ |
| 2820 |
\ |
| 2821 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); \ |
| 2822 |
c_oid = scm_to_gnutls_oid (oid, 2, FUNC_NAME); \ |
| 2823 |
if (SCM_UNBNDP(index)) \ |
| 2824 |
{ \ |
| 2825 |
c_index = 0; \ |
| 2826 |
} \ |
| 2827 |
else \ |
| 2828 |
{ \ |
| 2829 |
c_index = scm_to_uint (index); \ |
| 2830 |
} \ |
| 2831 |
c_raw_flag = 0; \ |
| 2832 |
\ |
| 2833 |
c_name_len = 256; \ |
| 2834 |
c_name = scm_malloc (c_name_len); \ |
| 2835 |
\ |
| 2836 |
do \ |
| 2837 |
{ \ |
| 2838 |
c_name_actual_len = c_name_len; \ |
| 2839 |
err = get_dn_by_oid (c_cert, c_oid, c_index, c_raw_flag, \ |
| 2840 |
c_name, &c_name_actual_len); \ |
| 2841 |
if (err == GNUTLS_E_SHORT_MEMORY_BUFFER) \ |
| 2842 |
{ \ |
| 2843 |
c_name = scm_realloc (c_name, c_name_len * 2); \ |
| 2844 |
c_name_len *= 2; \ |
| 2845 |
} \ |
| 2846 |
} \ |
| 2847 |
while (err == GNUTLS_E_SHORT_MEMORY_BUFFER); \ |
| 2848 |
\ |
| 2849 |
if (EXPECT_FALSE (err)) \ |
| 2850 |
{ \ |
| 2851 |
free (c_name); \ |
| 2852 |
if (err == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) \ |
| 2853 |
result = SCM_BOOL_F; \ |
| 2854 |
else \ |
| 2855 |
scm_gnutls_error (err, FUNC_NAME); \ |
| 2856 |
} \ |
| 2857 |
else \ |
| 2858 |
{ \ |
| 2859 |
if (c_name_actual_len < c_name_len) \ |
| 2860 |
c_name = scm_realloc (c_name, c_name_actual_len); \ |
| 2861 |
\ |
| 2862 |
result = scm_take_locale_stringn (c_name, \ |
| 2863 |
c_name_actual_len); \ |
| 2864 |
} \ |
| 2865 |
\ |
| 2866 |
return result; |
| 2867 |
|
| 2868 |
|
| 2869 |
SCM_DEFINE (scm_gnutls_x509_certificate_dn_by_oid, |
| 2870 |
"x509-certificate-dn-by-oid", |
| 2871 |
2, 1, 0, |
| 2872 |
(SCM cert, SCM oid, SCM index), |
| 2873 |
"Return part of the subject DN specified by @var{oid} from " |
| 2874 |
"@var{cert} at @var{index} or the first if not specified. " |
| 2875 |
"Return @code{#f} if no value is available.") |
| 2876 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_dn_by_oid |
| 2877 |
{ |
| 2878 |
X509_CERTIFICATE_DN_BY_OID_FUNCTION_BODY (gnutls_x509_crt_get_dn_by_oid); |
| 2879 |
} |
| 2880 |
|
| 2881 |
#undef FUNC_NAME |
| 2882 |
|
| 2883 |
SCM_DEFINE (scm_gnutls_x509_certificate_issuer_dn_by_oid, |
| 2884 |
"x509-certificate-issuer-dn-by-oid", |
| 2885 |
2, 1, 0, |
| 2886 |
(SCM cert, SCM oid, SCM index), |
| 2887 |
"Return part of the issuer DN specified by @var{oid} from " |
| 2888 |
"@var{cert} at @var{index} or the first if not specified. " |
| 2889 |
"Return @code{#f} if no value is available at @var{index}.") |
| 2890 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_issuer_dn_by_oid |
| 2891 |
{ |
| 2892 |
X509_CERTIFICATE_DN_BY_OID_FUNCTION_BODY |
| 2893 |
(gnutls_x509_crt_get_issuer_dn_by_oid); |
| 2894 |
} |
| 2895 |
|
| 2896 |
#undef FUNC_NAME |
| 2897 |
|
| 2898 |
#undef X509X509_CERTIFICATE_DN_BY_OID_FUNCTION_BODY |
| 2899 |
|
| 2900 |
|
| 2901 |
/* Provide the body of a `get_dn_oid' function. */ |
| 2902 |
#define X509_CERTIFICATE_DN_OID_FUNCTION_BODY(get_dn_oid) \ |
| 2903 |
int err; \ |
| 2904 |
gnutls_x509_crt_t c_cert; \ |
| 2905 |
unsigned int c_index; \ |
| 2906 |
char *c_oid; \ |
| 2907 |
size_t c_oid_actual_len, c_oid_len; \ |
| 2908 |
SCM result; \ |
| 2909 |
\ |
| 2910 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); \ |
| 2911 |
c_index = scm_to_uint (index); \ |
| 2912 |
\ |
| 2913 |
c_oid_len = 256; \ |
| 2914 |
c_oid = scm_malloc (c_oid_len); \ |
| 2915 |
\ |
| 2916 |
do \ |
| 2917 |
{ \ |
| 2918 |
c_oid_actual_len = c_oid_len; \ |
| 2919 |
err = get_dn_oid (c_cert, c_index, c_oid, &c_oid_actual_len); \ |
| 2920 |
if (err == GNUTLS_E_SHORT_MEMORY_BUFFER) \ |
| 2921 |
{ \ |
| 2922 |
c_oid = scm_realloc (c_oid, c_oid_len * 2); \ |
| 2923 |
c_oid_len *= 2; \ |
| 2924 |
} \ |
| 2925 |
} \ |
| 2926 |
while (err == GNUTLS_E_SHORT_MEMORY_BUFFER); \ |
| 2927 |
\ |
| 2928 |
if (EXPECT_FALSE (err)) \ |
| 2929 |
{ \ |
| 2930 |
free (c_oid); \ |
| 2931 |
\ |
| 2932 |
if (err == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) \ |
| 2933 |
result = SCM_BOOL_F; \ |
| 2934 |
else \ |
| 2935 |
scm_gnutls_error (err, FUNC_NAME); \ |
| 2936 |
} \ |
| 2937 |
else \ |
| 2938 |
{ \ |
| 2939 |
if (c_oid_actual_len < c_oid_len) \ |
| 2940 |
c_oid = scm_realloc (c_oid, c_oid_actual_len); \ |
| 2941 |
\ |
| 2942 |
result = scm_take_locale_stringn (c_oid, \ |
| 2943 |
c_oid_actual_len); \ |
| 2944 |
} \ |
| 2945 |
\ |
| 2946 |
return result; |
| 2947 |
|
| 2948 |
SCM_DEFINE (scm_gnutls_x509_certificate_dn_oid, "x509-certificate-dn-oid", |
| 2949 |
2, 0, 0, |
| 2950 |
(SCM cert, SCM index), |
| 2951 |
"Return OID (a string) at @var{index} from @var{cert}. " |
| 2952 |
"Return @code{#f} if no OID is available at @var{index}.") |
| 2953 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_dn_oid |
| 2954 |
{ |
| 2955 |
X509_CERTIFICATE_DN_OID_FUNCTION_BODY (gnutls_x509_crt_get_dn_oid); |
| 2956 |
} |
| 2957 |
|
| 2958 |
#undef FUNC_NAME |
| 2959 |
|
| 2960 |
SCM_DEFINE (scm_gnutls_x509_certificate_issuer_dn_oid, |
| 2961 |
"x509-certificate-issuer-dn-oid", |
| 2962 |
2, 0, 0, |
| 2963 |
(SCM cert, SCM index), |
| 2964 |
"Return the OID (a string) at @var{index} from @var{cert}'s " |
| 2965 |
"issuer DN. Return @code{#f} if no OID is available at " |
| 2966 |
"@var{index}.") |
| 2967 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_issuer_dn_oid |
| 2968 |
{ |
| 2969 |
X509_CERTIFICATE_DN_OID_FUNCTION_BODY (gnutls_x509_crt_get_issuer_dn_oid); |
| 2970 |
} |
| 2971 |
|
| 2972 |
#undef FUNC_NAME |
| 2973 |
|
| 2974 |
#undef X509_CERTIFICATE_DN_OID_FUNCTION_BODY |
| 2975 |
|
| 2976 |
|
| 2977 |
SCM_DEFINE (scm_gnutls_x509_certificate_matches_hostname_p, |
| 2978 |
"x509-certificate-matches-hostname?", |
| 2979 |
2, 0, 0, |
| 2980 |
(SCM cert, SCM hostname), |
| 2981 |
"Return true if @var{cert} matches @var{hostname}, a string " |
| 2982 |
"denoting a DNS host name. This is the basic implementation " |
| 2983 |
"of @uref{https://tools.ietf.org/html/rfc2818, RFC 2818} (aka. " |
| 2984 |
"HTTPS).") |
| 2985 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_matches_hostname_p |
| 2986 |
{ |
| 2987 |
SCM result; |
| 2988 |
gnutls_x509_crt_t c_cert; |
| 2989 |
char *c_hostname; |
| 2990 |
size_t c_hostname_len; |
| 2991 |
|
| 2992 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 2993 |
SCM_VALIDATE_STRING (2, hostname); |
| 2994 |
|
| 2995 |
c_hostname_len = scm_c_string_length (hostname); |
| 2996 |
c_hostname = FAST_ALLOC (c_hostname_len + 1); |
| 2997 |
|
| 2998 |
(void) scm_to_locale_stringbuf (hostname, c_hostname, c_hostname_len + 1); |
| 2999 |
c_hostname[c_hostname_len] = '\0'; |
| 3000 |
|
| 3001 |
if (gnutls_x509_crt_check_hostname (c_cert, c_hostname)) |
| 3002 |
result = SCM_BOOL_T; |
| 3003 |
else |
| 3004 |
result = SCM_BOOL_F; |
| 3005 |
|
| 3006 |
return result; |
| 3007 |
} |
| 3008 |
|
| 3009 |
#undef FUNC_NAME |
| 3010 |
|
| 3011 |
SCM_DEFINE (scm_gnutls_x509_certificate_signature_algorithm, |
| 3012 |
"x509-certificate-signature-algorithm", |
| 3013 |
1, 0, 0, |
| 3014 |
(SCM cert), |
| 3015 |
"Return the signature algorithm used by @var{cert} (i.e., " |
| 3016 |
"one of the @code{sign-algorithm/} values).") |
| 3017 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_signature_algorithm |
| 3018 |
{ |
| 3019 |
int c_result; |
| 3020 |
gnutls_x509_crt_t c_cert; |
| 3021 |
|
| 3022 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3023 |
|
| 3024 |
c_result = gnutls_x509_crt_get_signature_algorithm (c_cert); |
| 3025 |
if (EXPECT_FALSE (c_result < 0)) |
| 3026 |
scm_gnutls_error (c_result, FUNC_NAME); |
| 3027 |
|
| 3028 |
return (scm_from_gnutls_sign_algorithm (c_result)); |
| 3029 |
} |
| 3030 |
|
| 3031 |
#undef FUNC_NAME |
| 3032 |
|
| 3033 |
SCM_DEFINE (scm_gnutls_x509_certificate_public_key_algorithm, |
| 3034 |
"x509-certificate-public-key-algorithm", |
| 3035 |
1, 0, 0, |
| 3036 |
(SCM cert), |
| 3037 |
"Return two values: the public key algorithm (i.e., " |
| 3038 |
"one of the @code{pk-algorithm/} values) of @var{cert} " |
| 3039 |
"and the number of bits used.") |
| 3040 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_public_key_algorithm |
| 3041 |
{ |
| 3042 |
gnutls_x509_crt_t c_cert; |
| 3043 |
gnutls_pk_algorithm_t c_pk; |
| 3044 |
unsigned int c_bits; |
| 3045 |
|
| 3046 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3047 |
|
| 3048 |
c_pk = gnutls_x509_crt_get_pk_algorithm (c_cert, &c_bits); |
| 3049 |
|
| 3050 |
return (scm_values (scm_list_2 (scm_from_gnutls_pk_algorithm (c_pk), |
| 3051 |
scm_from_uint (c_bits)))); |
| 3052 |
} |
| 3053 |
|
| 3054 |
#undef FUNC_NAME |
| 3055 |
|
| 3056 |
SCM_DEFINE (scm_gnutls_x509_certificate_key_usage, |
| 3057 |
"x509-certificate-key-usage", |
| 3058 |
1, 0, 0, |
| 3059 |
(SCM cert), |
| 3060 |
"Return the key usage of @var{cert} (i.e., a list of " |
| 3061 |
"@code{key-usage/} values), or the empty list if @var{cert} " |
| 3062 |
"does not contain such information.") |
| 3063 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_key_usage |
| 3064 |
{ |
| 3065 |
int err; |
| 3066 |
SCM usage; |
| 3067 |
gnutls_x509_crt_t c_cert; |
| 3068 |
unsigned int c_usage, c_critical; |
| 3069 |
|
| 3070 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3071 |
|
| 3072 |
err = gnutls_x509_crt_get_key_usage (c_cert, &c_usage, &c_critical); |
| 3073 |
if (EXPECT_FALSE (err)) |
| 3074 |
{ |
| 3075 |
if (err == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) |
| 3076 |
usage = SCM_EOL; |
| 3077 |
else |
| 3078 |
scm_gnutls_error (err, FUNC_NAME); |
| 3079 |
} |
| 3080 |
else |
| 3081 |
usage = scm_from_gnutls_key_usage_flags (c_usage); |
| 3082 |
|
| 3083 |
return usage; |
| 3084 |
} |
| 3085 |
|
| 3086 |
#undef FUNC_NAME |
| 3087 |
|
| 3088 |
SCM_DEFINE (scm_gnutls_set_x509_certificate_key_usage, |
| 3089 |
"set-x509-certificate-key-usage!", 2, 0, 0, (SCM cert, SCM flags), |
| 3090 |
"Set the key_usage of @var{cert} to @var{flags}, a list of " |
| 3091 |
"usage flags.") |
| 3092 |
#define FUNC_NAME s_scm_gnutls_set_x509_certificate_key_usage |
| 3093 |
{ |
| 3094 |
int err; |
| 3095 |
gnutls_x509_crt_t c_cert; |
| 3096 |
int c_flags; |
| 3097 |
|
| 3098 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3099 |
|
| 3100 |
for (c_flags = 0; !scm_is_null (flags); flags = SCM_CDR (flags)) |
| 3101 |
{ |
| 3102 |
c_flags |= (unsigned int) |
| 3103 |
scm_to_gnutls_key_usage (SCM_CAR (flags), 3, FUNC_NAME); |
| 3104 |
} |
| 3105 |
|
| 3106 |
err = gnutls_x509_crt_set_key_usage (c_cert, c_flags); |
| 3107 |
if (EXPECT_FALSE (err)) |
| 3108 |
scm_gnutls_error (err, FUNC_NAME); |
| 3109 |
|
| 3110 |
return SCM_UNSPECIFIED; |
| 3111 |
} |
| 3112 |
|
| 3113 |
#undef FUNC_NAME |
| 3114 |
|
| 3115 |
SCM_DEFINE (scm_gnutls_x509_certificate_version, "x509-certificate-version", |
| 3116 |
1, 0, 0, (SCM cert), "Return the version of @var{cert}.") |
| 3117 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_version |
| 3118 |
{ |
| 3119 |
int c_result; |
| 3120 |
gnutls_x509_crt_t c_cert; |
| 3121 |
|
| 3122 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3123 |
|
| 3124 |
c_result = gnutls_x509_crt_get_version (c_cert); |
| 3125 |
if (EXPECT_FALSE (c_result < 0)) |
| 3126 |
scm_gnutls_error (c_result, FUNC_NAME); |
| 3127 |
|
| 3128 |
return (scm_from_int (c_result)); |
| 3129 |
} |
| 3130 |
|
| 3131 |
#undef FUNC_NAME |
| 3132 |
|
| 3133 |
SCM_DEFINE (scm_gnutls_set_x509_certificate_version, |
| 3134 |
"set-x509-certificate-version!", 2, 0, 0, (SCM cert, SCM version), |
| 3135 |
"Set the version of @var{cert} to @var{version}.") |
| 3136 |
#define FUNC_NAME s_scm_gnutls_set_x509_certificate_version |
| 3137 |
{ |
| 3138 |
int err; |
| 3139 |
gnutls_x509_crt_t c_cert; |
| 3140 |
int c_version; |
| 3141 |
|
| 3142 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3143 |
c_version = scm_to_uint (version); |
| 3144 |
|
| 3145 |
err = gnutls_x509_crt_set_version (c_cert, c_version); |
| 3146 |
if (EXPECT_FALSE (err)) |
| 3147 |
scm_gnutls_error (err, FUNC_NAME); |
| 3148 |
|
| 3149 |
return SCM_UNSPECIFIED; |
| 3150 |
} |
| 3151 |
|
| 3152 |
#undef FUNC_NAME |
| 3153 |
|
| 3154 |
SCM_DEFINE (scm_gnutls_x509_certificate_key_id, "x509-certificate-key-id", |
| 3155 |
1, 0, 0, |
| 3156 |
(SCM cert), |
| 3157 |
"Return a statistically unique ID (a u8vector) for @var{cert} " |
| 3158 |
"that depends on its public key parameters. This is normally " |
| 3159 |
"a 20-byte SHA-1 hash.") |
| 3160 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_key_id |
| 3161 |
{ |
| 3162 |
int err; |
| 3163 |
SCM result; |
| 3164 |
scm_t_array_handle c_id_handle; |
| 3165 |
gnutls_x509_crt_t c_cert; |
| 3166 |
uint8_t *c_id; |
| 3167 |
size_t c_id_len = 20; |
| 3168 |
|
| 3169 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3170 |
|
| 3171 |
result = scm_make_u8vector (scm_from_uint (c_id_len), SCM_INUM0); |
| 3172 |
scm_array_get_handle (result, &c_id_handle); |
| 3173 |
c_id = scm_array_handle_u8_writable_elements (&c_id_handle); |
| 3174 |
|
| 3175 |
err = gnutls_x509_crt_get_key_id (c_cert, 0, c_id, &c_id_len); |
| 3176 |
scm_array_handle_release (&c_id_handle); |
| 3177 |
|
| 3178 |
if (EXPECT_FALSE (err)) |
| 3179 |
scm_gnutls_error (err, FUNC_NAME); |
| 3180 |
|
| 3181 |
return result; |
| 3182 |
} |
| 3183 |
|
| 3184 |
#undef FUNC_NAME |
| 3185 |
|
| 3186 |
SCM_DEFINE (scm_gnutls_x509_certificate_authority_key_id, |
| 3187 |
"x509-certificate-authority-key-id", |
| 3188 |
1, 0, 0, |
| 3189 |
(SCM cert), |
| 3190 |
"Return the key ID (a u8vector) of the X.509 certificate " |
| 3191 |
"authority of @var{cert}.") |
| 3192 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_authority_key_id |
| 3193 |
{ |
| 3194 |
int err; |
| 3195 |
SCM result; |
| 3196 |
scm_t_array_handle c_id_handle; |
| 3197 |
gnutls_x509_crt_t c_cert; |
| 3198 |
uint8_t *c_id; |
| 3199 |
size_t c_id_len = 20; |
| 3200 |
|
| 3201 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3202 |
|
| 3203 |
result = scm_make_u8vector (scm_from_uint (c_id_len), SCM_INUM0); |
| 3204 |
scm_array_get_handle (result, &c_id_handle); |
| 3205 |
c_id = scm_array_handle_u8_writable_elements (&c_id_handle); |
| 3206 |
|
| 3207 |
err = gnutls_x509_crt_get_authority_key_id (c_cert, c_id, &c_id_len, NULL); |
| 3208 |
scm_array_handle_release (&c_id_handle); |
| 3209 |
|
| 3210 |
if (EXPECT_FALSE (err)) |
| 3211 |
scm_gnutls_error (err, FUNC_NAME); |
| 3212 |
|
| 3213 |
return result; |
| 3214 |
} |
| 3215 |
|
| 3216 |
#undef FUNC_NAME |
| 3217 |
|
| 3218 |
SCM_DEFINE (scm_gnutls_x509_certificate_subject_key_id, |
| 3219 |
"x509-certificate-subject-key-id", |
| 3220 |
1, 0, 0, |
| 3221 |
(SCM cert), |
| 3222 |
"Return the subject key ID (a u8vector) for @var{cert}.") |
| 3223 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_subject_key_id |
| 3224 |
{ |
| 3225 |
int err; |
| 3226 |
SCM result; |
| 3227 |
scm_t_array_handle c_id_handle; |
| 3228 |
gnutls_x509_crt_t c_cert; |
| 3229 |
uint8_t *c_id; |
| 3230 |
size_t c_id_len = 20; |
| 3231 |
|
| 3232 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3233 |
|
| 3234 |
result = scm_make_u8vector (scm_from_uint (c_id_len), SCM_INUM0); |
| 3235 |
scm_array_get_handle (result, &c_id_handle); |
| 3236 |
c_id = scm_array_handle_u8_writable_elements (&c_id_handle); |
| 3237 |
|
| 3238 |
err = gnutls_x509_crt_get_subject_key_id (c_cert, c_id, &c_id_len, NULL); |
| 3239 |
scm_array_handle_release (&c_id_handle); |
| 3240 |
|
| 3241 |
if (EXPECT_FALSE (err)) |
| 3242 |
scm_gnutls_error (err, FUNC_NAME); |
| 3243 |
|
| 3244 |
return result; |
| 3245 |
} |
| 3246 |
|
| 3247 |
#undef FUNC_NAME |
| 3248 |
|
| 3249 |
SCM_DEFINE (scm_gnutls_set_x509_certificate_subject_key_id, |
| 3250 |
"set-x509-certificate-subject-key-id!", |
| 3251 |
2, 0, 0, |
| 3252 |
(SCM cert, SCM id), |
| 3253 |
"Set the subject key ID for @var{cert} to the bytevector " |
| 3254 |
"@var{id}.") |
| 3255 |
#define FUNC_NAME s_scm_gnutls_set_x509_certificate_subject_key_id |
| 3256 |
{ |
| 3257 |
int err; |
| 3258 |
gnutls_x509_crt_t c_cert; |
| 3259 |
void *c_id; |
| 3260 |
size_t c_id_len; |
| 3261 |
|
| 3262 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3263 |
c_id = SCM_BYTEVECTOR_CONTENTS (id); |
| 3264 |
c_id_len = SCM_BYTEVECTOR_LENGTH (id); |
| 3265 |
|
| 3266 |
err = gnutls_x509_crt_set_subject_key_id (c_cert, c_id, c_id_len); |
| 3267 |
if (EXPECT_FALSE (err)) |
| 3268 |
scm_gnutls_error (err, FUNC_NAME); |
| 3269 |
|
| 3270 |
return SCM_UNSPECIFIED; |
| 3271 |
} |
| 3272 |
|
| 3273 |
#undef FUNC_NAME |
| 3274 |
|
| 3275 |
SCM_DEFINE (scm_gnutls_x509_certificate_subject_alternative_name, |
| 3276 |
"x509-certificate-subject-alternative-name", |
| 3277 |
2, 0, 0, |
| 3278 |
(SCM cert, SCM index), |
| 3279 |
"Return two values: the alternative name type for @var{cert} " |
| 3280 |
"(i.e., one of the @code{x509-subject-alternative-name/} values) " |
| 3281 |
"and the actual subject alternative name (a string) at " |
| 3282 |
"@var{index}. Both values are @code{#f} if no alternative name " |
| 3283 |
"is available at @var{index}.") |
| 3284 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_subject_alternative_name |
| 3285 |
{ |
| 3286 |
int err; |
| 3287 |
SCM result; |
| 3288 |
gnutls_x509_crt_t c_cert; |
| 3289 |
unsigned int c_index; |
| 3290 |
char *c_name; |
| 3291 |
size_t c_name_len = 512, c_name_actual_len; |
| 3292 |
|
| 3293 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3294 |
c_index = scm_to_uint (index); |
| 3295 |
|
| 3296 |
c_name = scm_malloc (c_name_len); |
| 3297 |
do |
| 3298 |
{ |
| 3299 |
c_name_actual_len = c_name_len; |
| 3300 |
err = gnutls_x509_crt_get_subject_alt_name (c_cert, c_index, |
| 3301 |
c_name, &c_name_actual_len, |
| 3302 |
NULL); |
| 3303 |
if (err == GNUTLS_E_SHORT_MEMORY_BUFFER) |
| 3304 |
{ |
| 3305 |
c_name = scm_realloc (c_name, c_name_len * 2); |
| 3306 |
c_name_len *= 2; |
| 3307 |
} |
| 3308 |
} |
| 3309 |
while (err == GNUTLS_E_SHORT_MEMORY_BUFFER); |
| 3310 |
|
| 3311 |
if (EXPECT_FALSE (err < 0)) |
| 3312 |
{ |
| 3313 |
free (c_name); |
| 3314 |
|
| 3315 |
if (err == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) |
| 3316 |
result = scm_values (scm_list_2 (SCM_BOOL_F, SCM_BOOL_F)); |
| 3317 |
else |
| 3318 |
scm_gnutls_error (err, FUNC_NAME); |
| 3319 |
} |
| 3320 |
else |
| 3321 |
{ |
| 3322 |
if (c_name_actual_len < c_name_len) |
| 3323 |
c_name = scm_realloc (c_name, c_name_actual_len); |
| 3324 |
|
| 3325 |
result = |
| 3326 |
scm_values (scm_list_2 |
| 3327 |
(scm_from_gnutls_x509_subject_alternative_name (err), |
| 3328 |
scm_take_locale_string (c_name))); |
| 3329 |
} |
| 3330 |
|
| 3331 |
return result; |
| 3332 |
} |
| 3333 |
|
| 3334 |
#undef FUNC_NAME |
| 3335 |
|
| 3336 |
SCM_DEFINE (scm_gnutls_x509_certificate_fingerprint, |
| 3337 |
"x509-certificate-fingerprint", |
| 3338 |
2, 0, 0, |
| 3339 |
(SCM cert, SCM algo), |
| 3340 |
"Return the fingerprint (a u8vector) of the certificate " |
| 3341 |
"@var{cert}, computed using the digest algorithm @var{algo}.") |
| 3342 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_fingerprint |
| 3343 |
{ |
| 3344 |
int err; |
| 3345 |
SCM result; |
| 3346 |
gnutls_x509_crt_t c_cert; |
| 3347 |
gnutls_digest_algorithm_t c_algo; |
| 3348 |
uint8_t c_fpr[MAX_HASH_SIZE]; |
| 3349 |
size_t c_fpr_len = MAX_HASH_SIZE; |
| 3350 |
scm_t_array_handle c_handle; |
| 3351 |
|
| 3352 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3353 |
c_algo = scm_to_gnutls_digest (algo, 1, FUNC_NAME); |
| 3354 |
|
| 3355 |
err = gnutls_x509_crt_get_fingerprint (c_cert, c_algo, &c_fpr, &c_fpr_len); |
| 3356 |
if (EXPECT_FALSE (err)) |
| 3357 |
scm_gnutls_error (err, FUNC_NAME); |
| 3358 |
|
| 3359 |
result = scm_make_u8vector (scm_from_uint (c_fpr_len), SCM_INUM0); |
| 3360 |
scm_array_get_handle (result, &c_handle); |
| 3361 |
memcpy (scm_array_handle_u8_writable_elements (&c_handle), &c_fpr, |
| 3362 |
c_fpr_len); |
| 3363 |
scm_array_handle_release (&c_handle); |
| 3364 |
|
| 3365 |
return result; |
| 3366 |
} |
| 3367 |
|
| 3368 |
#undef FUNC_NAME |
| 3369 |
|
| 3370 |
SCM_DEFINE (scm_gnutls_sign_x509_certificate, "sign-x509-certificate!", |
| 3371 |
3, 0, 0, |
| 3372 |
(SCM cert, SCM issuer, SCM key), |
| 3373 |
"Sign @var{cert} using @var{cert}, also a certificate, and " |
| 3374 |
"@var{key}, the issuer's private key.") |
| 3375 |
#define FUNC_NAME s_scm_gnutls_sign_x509_certificate |
| 3376 |
{ |
| 3377 |
int err; |
| 3378 |
gnutls_x509_crt_t c_cert, c_issuer; |
| 3379 |
gnutls_x509_privkey_t c_key; |
| 3380 |
|
| 3381 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3382 |
c_issuer = scm_to_gnutls_x509_certificate (issuer, 2, FUNC_NAME); |
| 3383 |
c_key = scm_to_gnutls_x509_private_key (key, 3, FUNC_NAME); |
| 3384 |
|
| 3385 |
err = gnutls_x509_crt_sign (c_cert, c_issuer, c_key); |
| 3386 |
if (EXPECT_FALSE (err)) |
| 3387 |
scm_gnutls_error (err, FUNC_NAME); |
| 3388 |
|
| 3389 |
return SCM_UNSPECIFIED; |
| 3390 |
} |
| 3391 |
|
| 3392 |
#undef FUNC_NAME |
| 3393 |
|
| 3394 |
SCM_DEFINE (scm_gnutls_set_x509_certificate_dn_by_oid, |
| 3395 |
"set-x509-certificate-dn-by-oid!", |
| 3396 |
3, 0, 0, |
| 3397 |
(SCM cert, SCM oid, SCM name), |
| 3398 |
"Set the part of the name of the certificate request " |
| 3399 |
"subject for @var{cert} corresponding to @var{oid} to " |
| 3400 |
"the string @var{name}.") |
| 3401 |
#define FUNC_NAME s_scm_gnutls_set_x509_certificate_dn_by_oid |
| 3402 |
{ |
| 3403 |
int err; |
| 3404 |
gnutls_x509_crt_t c_cert; |
| 3405 |
char *c_oid; |
| 3406 |
char *c_name; |
| 3407 |
size_t c_name_len; |
| 3408 |
|
| 3409 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3410 |
c_oid = scm_to_gnutls_oid (oid, 2, FUNC_NAME); |
| 3411 |
c_name = scm_to_locale_string (name); |
| 3412 |
c_name_len = strlen (c_name); |
| 3413 |
|
| 3414 |
err = gnutls_x509_crt_set_dn_by_oid (c_cert, c_oid, 0, c_name, c_name_len); |
| 3415 |
free (c_name); |
| 3416 |
if (EXPECT_FALSE (err)) |
| 3417 |
scm_gnutls_error (err, FUNC_NAME); |
| 3418 |
|
| 3419 |
return SCM_UNSPECIFIED; |
| 3420 |
} |
| 3421 |
|
| 3422 |
#undef FUNC_NAME |
| 3423 |
|
| 3424 |
SCM_DEFINE (scm_gnutls_set_x509_certificate_key, |
| 3425 |
"set-x509-certificate-key!", |
| 3426 |
2, 0, 0, |
| 3427 |
(SCM cert, SCM key), |
| 3428 |
"Set the public parameters of @var{cert} using the private " |
| 3429 |
"key @var{key}.") |
| 3430 |
#define FUNC_NAME s_scm_gnutls_set_x509_certificate_key |
| 3431 |
{ |
| 3432 |
int err; |
| 3433 |
gnutls_x509_crt_t c_cert; |
| 3434 |
gnutls_x509_privkey_t c_key; |
| 3435 |
|
| 3436 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3437 |
c_key = scm_to_gnutls_x509_private_key (key, 2, FUNC_NAME); |
| 3438 |
|
| 3439 |
err = gnutls_x509_crt_set_key (c_cert, c_key); |
| 3440 |
if (EXPECT_FALSE (err)) |
| 3441 |
scm_gnutls_error (err, FUNC_NAME); |
| 3442 |
|
| 3443 |
return SCM_UNSPECIFIED; |
| 3444 |
} |
| 3445 |
|
| 3446 |
#undef FUNC_NAME |
| 3447 |
|
| 3448 |
SCM_DEFINE (scm_gnutls_x509_certificate_activation_time, |
| 3449 |
"x509-certificate-activation-time", |
| 3450 |
1, 0, 0, (SCM cert), "Return the activation time of @var{cert}.") |
| 3451 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_activation_time |
| 3452 |
{ |
| 3453 |
gnutls_x509_crt_t c_cert; |
| 3454 |
time_t c_time; |
| 3455 |
|
| 3456 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3457 |
c_time = gnutls_x509_crt_get_activation_time (c_cert); |
| 3458 |
|
| 3459 |
return scm_from_time_t (c_time); |
| 3460 |
} |
| 3461 |
|
| 3462 |
#undef FUNC_NAME |
| 3463 |
|
| 3464 |
SCM_DEFINE (scm_gnutls_set_x509_certificate_activation_time, |
| 3465 |
"set-x509-certificate-activation-time!", |
| 3466 |
2, 0, 0, |
| 3467 |
(SCM cert, SCM time), |
| 3468 |
"Set the activation time of @var{cert} to @var{time}.") |
| 3469 |
#define FUNC_NAME s_scm_gnutls_set_x509_certificate_activation_time |
| 3470 |
{ |
| 3471 |
int err; |
| 3472 |
gnutls_x509_crt_t c_cert; |
| 3473 |
time_t c_time; |
| 3474 |
|
| 3475 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3476 |
c_time = scm_to_time_t (time); |
| 3477 |
|
| 3478 |
err = gnutls_x509_crt_set_activation_time (c_cert, c_time); |
| 3479 |
if (EXPECT_FALSE (err)) |
| 3480 |
scm_gnutls_error (err, FUNC_NAME); |
| 3481 |
|
| 3482 |
return SCM_UNSPECIFIED; |
| 3483 |
} |
| 3484 |
|
| 3485 |
#undef FUNC_NAME |
| 3486 |
|
| 3487 |
SCM_DEFINE (scm_gnutls_x509_certificate_expiration_time, |
| 3488 |
"x509-certificate-expiration-time", |
| 3489 |
1, 0, 0, (SCM cert), "Return the expiration time of @var{cert}.") |
| 3490 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_expiration_time |
| 3491 |
{ |
| 3492 |
gnutls_x509_crt_t c_cert; |
| 3493 |
time_t c_time; |
| 3494 |
|
| 3495 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3496 |
c_time = gnutls_x509_crt_get_expiration_time (c_cert); |
| 3497 |
|
| 3498 |
return scm_from_time_t (c_time); |
| 3499 |
} |
| 3500 |
|
| 3501 |
#undef FUNC_NAME |
| 3502 |
|
| 3503 |
SCM_DEFINE (scm_gnutls_set_x509_certificate_expiration_time, |
| 3504 |
"set-x509-certificate-expiration-time!", |
| 3505 |
2, 0, 0, |
| 3506 |
(SCM cert, SCM time), |
| 3507 |
"Set the expiration time of @var{cert} to @var{time}.") |
| 3508 |
#define FUNC_NAME s_scm_gnutls_set_x509_certificate_expiration_time |
| 3509 |
{ |
| 3510 |
int err; |
| 3511 |
gnutls_x509_crt_t c_cert; |
| 3512 |
time_t c_time; |
| 3513 |
|
| 3514 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3515 |
c_time = scm_to_time_t (time); |
| 3516 |
|
| 3517 |
err = gnutls_x509_crt_set_expiration_time (c_cert, c_time); |
| 3518 |
if (EXPECT_FALSE (err)) |
| 3519 |
scm_gnutls_error (err, FUNC_NAME); |
| 3520 |
|
| 3521 |
return SCM_UNSPECIFIED; |
| 3522 |
} |
| 3523 |
|
| 3524 |
#undef FUNC_NAME |
| 3525 |
|
| 3526 |
SCM_DEFINE (scm_gnutls_x509_certificate_ca_status, |
| 3527 |
"x509-certificate-ca-status", |
| 3528 |
1, 0, 0, (SCM cert), "Return the CA status of @var{cert}.") |
| 3529 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_ca_status |
| 3530 |
{ |
| 3531 |
gnutls_x509_crt_t c_cert; |
| 3532 |
int c_status; |
| 3533 |
unsigned int c_critical; // unused currently |
| 3534 |
|
| 3535 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3536 |
c_status = gnutls_x509_crt_get_ca_status (c_cert, &c_critical); |
| 3537 |
|
| 3538 |
if (c_status < 0) |
| 3539 |
{ |
| 3540 |
scm_gnutls_error (c_status, FUNC_NAME); |
| 3541 |
} |
| 3542 |
|
| 3543 |
return scm_from_bool (c_status); |
| 3544 |
} |
| 3545 |
|
| 3546 |
#undef FUNC_NAME |
| 3547 |
|
| 3548 |
SCM_DEFINE (scm_gnutls_set_x509_certificate_ca_status, |
| 3549 |
"set-x509-certificate-ca-status!", |
| 3550 |
2, 0, 0, |
| 3551 |
(SCM cert, SCM status), |
| 3552 |
"Set the CA status flag of @var{cert} to @var{status}, " |
| 3553 |
"either @code{#t} or @code{#f}.") |
| 3554 |
#define FUNC_NAME s_scm_gnutls_set_x509_certificate_ca_status |
| 3555 |
{ |
| 3556 |
int err; |
| 3557 |
gnutls_x509_crt_t c_cert; |
| 3558 |
int c_status; |
| 3559 |
|
| 3560 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3561 |
c_status = scm_to_bool (status); |
| 3562 |
|
| 3563 |
err = gnutls_x509_crt_set_ca_status (c_cert, c_status); |
| 3564 |
if (EXPECT_FALSE (err)) |
| 3565 |
scm_gnutls_error (err, FUNC_NAME); |
| 3566 |
|
| 3567 |
return SCM_UNSPECIFIED; |
| 3568 |
} |
| 3569 |
|
| 3570 |
#undef FUNC_NAME |
| 3571 |
|
| 3572 |
SCM_DEFINE (scm_gnutls_x509_certificate_serial, |
| 3573 |
"x509-certificate-serial", |
| 3574 |
1, 0, 0, (SCM cert), "Return the serial number of @var{cert}.") |
| 3575 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_serial |
| 3576 |
{ |
| 3577 |
int err; |
| 3578 |
SCM result; |
| 3579 |
gnutls_x509_crt_t c_cert; |
| 3580 |
uint8_t c_serial[32]; |
| 3581 |
size_t c_serial_len; |
| 3582 |
|
| 3583 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3584 |
c_serial_len = sizeof (c_serial); |
| 3585 |
|
| 3586 |
err = gnutls_x509_crt_get_serial (c_cert, c_serial, &c_serial_len); |
| 3587 |
if (EXPECT_FALSE (err)) |
| 3588 |
scm_gnutls_error (err, FUNC_NAME); |
| 3589 |
|
| 3590 |
result = scm_c_make_bytevector (c_serial_len); |
| 3591 |
memcpy (SCM_BYTEVECTOR_CONTENTS (result), c_serial, c_serial_len); |
| 3592 |
|
| 3593 |
return result; |
| 3594 |
} |
| 3595 |
|
| 3596 |
#undef FUNC_NAME |
| 3597 |
|
| 3598 |
SCM_DEFINE (scm_gnutls_set_x509_certificate_serial, |
| 3599 |
"set-x509-certificate-serial!", |
| 3600 |
2, 0, 0, |
| 3601 |
(SCM cert, SCM serial), |
| 3602 |
"Set the serial number of @var{cert} to the bytevector @var{serial}.") |
| 3603 |
#define FUNC_NAME s_scm_gnutls_set_x509_certificate_serial |
| 3604 |
{ |
| 3605 |
int err; |
| 3606 |
SCM result; |
| 3607 |
gnutls_x509_crt_t c_cert; |
| 3608 |
uint8_t *c_serial; |
| 3609 |
size_t c_serial_len; |
| 3610 |
|
| 3611 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3612 |
c_serial = SCM_BYTEVECTOR_CONTENTS (serial); |
| 3613 |
c_serial_len = SCM_BYTEVECTOR_LENGTH (serial); |
| 3614 |
|
| 3615 |
err = gnutls_x509_crt_set_serial (c_cert, c_serial, c_serial_len); |
| 3616 |
if (EXPECT_FALSE (err)) |
| 3617 |
scm_gnutls_error (err, FUNC_NAME); |
| 3618 |
|
| 3619 |
return SCM_UNSPECIFIED; |
| 3620 |
} |
| 3621 |
|
| 3622 |
#undef FUNC_NAME |
| 3623 |
|
| 3624 |
SCM_DEFINE (scm_gnutls_x509_certificate_print, "x509-certificate-print", |
| 3625 |
1, 1, 0, (SCM cert, SCM format), |
| 3626 |
"Return the certificate @var{cert} in a human-readable form " |
| 3627 |
"according to @var{format}, a certificate-print-format. " |
| 3628 |
"If unspecified certificate-print-format/full is used.") |
| 3629 |
#define FUNC_NAME s_scm_gnutls_x509_certificate_print |
| 3630 |
{ |
| 3631 |
int err; |
| 3632 |
SCM result; |
| 3633 |
gnutls_x509_crt_t c_cert; |
| 3634 |
gnutls_certificate_print_formats_t c_format; |
| 3635 |
gnutls_datum_t c_output; |
| 3636 |
scm_dynwind_begin (0); |
| 3637 |
c_cert = scm_to_gnutls_x509_certificate (cert, 1, FUNC_NAME); |
| 3638 |
|
| 3639 |
if (format == SCM_UNDEFINED) |
| 3640 |
{ |
| 3641 |
c_format = GNUTLS_CRT_PRINT_FULL; |
| 3642 |
} |
| 3643 |
else |
| 3644 |
{ |
| 3645 |
c_format = |
| 3646 |
scm_to_gnutls_certificate_print_format (format, 2, FUNC_NAME); |
| 3647 |
} |
| 3648 |
|
| 3649 |
err = gnutls_x509_crt_print (c_cert, c_format, &c_output); |
| 3650 |
|
| 3651 |
if (EXPECT_FALSE (err)) |
| 3652 |
{ |
| 3653 |
scm_gnutls_error (err, FUNC_NAME); |
| 3654 |
} |
| 3655 |
else |
| 3656 |
{ |
| 3657 |
scm_dynwind_unwind_handler (gnutls_free, c_output.data, |
| 3658 |
SCM_F_WIND_EXPLICITLY); |
| 3659 |
result = scm_from_utf8_stringn ((char *) c_output.data, c_output.size); |
| 3660 |
} |
| 3661 |
scm_dynwind_end (); |
| 3662 |
return result; |
| 3663 |
} |
| 3664 |
|
| 3665 |
#undef FUNC_NAME |
| 3666 |
|
| 3667 |
|
| 3668 |
|
| 3669 |
/* OpenPGP keys. */ |
| 3670 |
|
| 3671 |
|
| 3672 |
/* Maximum size we support for the name of OpenPGP keys. */ |
| 3673 |
#define GUILE_GNUTLS_MAX_OPENPGP_NAME_LENGTH 2048 |
| 3674 |
|
| 3675 |
SCM_DEFINE (scm_gnutls_import_openpgp_certificate, |
| 3676 |
"%import-openpgp-certificate", 2, 0, 0, (SCM data, SCM format), |
| 3677 |
"Return a new OpenPGP certificate object resulting from the " |
| 3678 |
"import of @var{data} (a uniform array) according to " |
| 3679 |
"@var{format}.") |
| 3680 |
#define FUNC_NAME s_scm_gnutls_import_openpgp_certificate |
| 3681 |
{ |
| 3682 |
int err; |
| 3683 |
gnutls_openpgp_crt_t c_key; |
| 3684 |
gnutls_openpgp_crt_fmt_t c_format; |
| 3685 |
gnutls_datum_t c_data_d; |
| 3686 |
scm_t_array_handle c_data_handle; |
| 3687 |
const char *c_data; |
| 3688 |
size_t c_data_len; |
| 3689 |
|
| 3690 |
SCM_VALIDATE_ARRAY (1, data); |
| 3691 |
c_format = scm_to_gnutls_openpgp_certificate_format (format, 2, FUNC_NAME); |
| 3692 |
|
| 3693 |
c_data = scm_gnutls_get_array (data, &c_data_handle, &c_data_len, |
| 3694 |
FUNC_NAME); |
| 3695 |
c_data_d.data = (unsigned char *) c_data; |
| 3696 |
c_data_d.size = c_data_len; |
| 3697 |
|
| 3698 |
err = gnutls_openpgp_crt_init (&c_key); |
| 3699 |
if (EXPECT_FALSE (err)) |
| 3700 |
{ |
| 3701 |
scm_gnutls_release_array (&c_data_handle); |
| 3702 |
scm_gnutls_error (err, FUNC_NAME); |
| 3703 |
} |
| 3704 |
|
| 3705 |
err = gnutls_openpgp_crt_import (c_key, &c_data_d, c_format); |
| 3706 |
scm_gnutls_release_array (&c_data_handle); |
| 3707 |
|
| 3708 |
if (EXPECT_FALSE (err)) |
| 3709 |
{ |
| 3710 |
gnutls_openpgp_crt_deinit (c_key); |
| 3711 |
scm_gnutls_error (err, FUNC_NAME); |
| 3712 |
} |
| 3713 |
|
| 3714 |
return (scm_from_gnutls_openpgp_certificate (c_key)); |
| 3715 |
} |
| 3716 |
|
| 3717 |
#undef FUNC_NAME |
| 3718 |
|
| 3719 |
SCM_DEFINE (scm_gnutls_import_openpgp_private_key, |
| 3720 |
"%import-openpgp-private-key", 2, 1, 0, (SCM data, SCM format, |
| 3721 |
SCM pass), |
| 3722 |
"Return a new OpenPGP private key object resulting from the " |
| 3723 |
"import of @var{data} (a uniform array) according to " |
| 3724 |
"@var{format}. Optionally, a passphrase may be provided.") |
| 3725 |
#define FUNC_NAME s_scm_gnutls_import_openpgp_private_key |
| 3726 |
{ |
| 3727 |
int err; |
| 3728 |
gnutls_openpgp_privkey_t c_key; |
| 3729 |
gnutls_openpgp_crt_fmt_t c_format; |
| 3730 |
gnutls_datum_t c_data_d; |
| 3731 |
scm_t_array_handle c_data_handle; |
| 3732 |
const char *c_data; |
| 3733 |
char *c_pass; |
| 3734 |
size_t c_data_len, c_pass_len; |
| 3735 |
|
| 3736 |
SCM_VALIDATE_ARRAY (1, data); |
| 3737 |
c_format = scm_to_gnutls_openpgp_certificate_format (format, 2, FUNC_NAME); |
| 3738 |
if ((pass == SCM_UNDEFINED) || (scm_is_false (pass))) |
| 3739 |
c_pass = NULL; |
| 3740 |
else |
| 3741 |
{ |
| 3742 |
c_pass_len = scm_c_string_length (pass); |
| 3743 |
c_pass = FAST_ALLOC (c_pass_len + 1); |
| 3744 |
(void) scm_to_locale_stringbuf (pass, c_pass, c_pass_len + 1); |
| 3745 |
c_pass[c_pass_len] = '\0'; |
| 3746 |
} |
| 3747 |
|
| 3748 |
c_data = scm_gnutls_get_array (data, &c_data_handle, &c_data_len, |
| 3749 |
FUNC_NAME); |
| 3750 |
c_data_d.data = (unsigned char *) c_data; |
| 3751 |
c_data_d.size = c_data_len; |
| 3752 |
|
| 3753 |
err = gnutls_openpgp_privkey_init (&c_key); |
| 3754 |
if (EXPECT_FALSE (err)) |
| 3755 |
{ |
| 3756 |
scm_gnutls_release_array (&c_data_handle); |
| 3757 |
scm_gnutls_error (err, FUNC_NAME); |
| 3758 |
} |
| 3759 |
|
| 3760 |
err = gnutls_openpgp_privkey_import (c_key, &c_data_d, c_format, c_pass, |
| 3761 |
0 /* currently unused */ ); |
| 3762 |
scm_gnutls_release_array (&c_data_handle); |
| 3763 |
|
| 3764 |
if (EXPECT_FALSE (err)) |
| 3765 |
{ |
| 3766 |
gnutls_openpgp_privkey_deinit (c_key); |
| 3767 |
scm_gnutls_error (err, FUNC_NAME); |
| 3768 |
} |
| 3769 |
|
| 3770 |
return (scm_from_gnutls_openpgp_private_key (c_key)); |
| 3771 |
} |
| 3772 |
|
| 3773 |
#undef FUNC_NAME |
| 3774 |
|
| 3775 |
SCM_DEFINE (scm_gnutls_openpgp_certificate_id, "%openpgp-certificate-id", |
| 3776 |
1, 0, 0, |
| 3777 |
(SCM key), |
| 3778 |
"Return the ID (an 8-element u8vector) of certificate " |
| 3779 |
"@var{key}.") |
| 3780 |
#define FUNC_NAME s_scm_gnutls_openpgp_certificate_id |
| 3781 |
{ |
| 3782 |
int err; |
| 3783 |
unsigned char *c_id; |
| 3784 |
gnutls_openpgp_crt_t c_key; |
| 3785 |
|
| 3786 |
c_key = scm_to_gnutls_openpgp_certificate (key, 1, FUNC_NAME); |
| 3787 |
|
| 3788 |
c_id = (unsigned char *) malloc (8); |
| 3789 |
if (c_id == NULL) |
| 3790 |
scm_gnutls_error (GNUTLS_E_MEMORY_ERROR, FUNC_NAME); |
| 3791 |
|
| 3792 |
err = gnutls_openpgp_crt_get_key_id (c_key, c_id); |
| 3793 |
if (EXPECT_FALSE (err)) |
| 3794 |
scm_gnutls_error (err, FUNC_NAME); |
| 3795 |
|
| 3796 |
return (scm_take_u8vector (c_id, 8)); |
| 3797 |
} |
| 3798 |
|
| 3799 |
#undef FUNC_NAME |
| 3800 |
|
| 3801 |
SCM_DEFINE (scm_gnutls_openpgp_certificate_id_x, "%openpgp-certificate-id!", |
| 3802 |
2, 0, 0, |
| 3803 |
(SCM key, SCM id), |
| 3804 |
"Store the ID (an 8 byte sequence) of certificate " |
| 3805 |
"@var{key} in @var{id} (a u8vector).") |
| 3806 |
#define FUNC_NAME s_scm_gnutls_openpgp_certificate_id_x |
| 3807 |
{ |
| 3808 |
int err; |
| 3809 |
char *c_id; |
| 3810 |
scm_t_array_handle c_id_handle; |
| 3811 |
size_t c_id_size; |
| 3812 |
gnutls_openpgp_crt_t c_key; |
| 3813 |
|
| 3814 |
c_key = scm_to_gnutls_openpgp_certificate (key, 1, FUNC_NAME); |
| 3815 |
c_id = scm_gnutls_get_writable_array (id, &c_id_handle, &c_id_size, |
| 3816 |
FUNC_NAME); |
| 3817 |
|
| 3818 |
if (EXPECT_FALSE (c_id_size < 8)) |
| 3819 |
{ |
| 3820 |
scm_gnutls_release_array (&c_id_handle); |
| 3821 |
scm_misc_error (FUNC_NAME, "ID vector too small: ~A", scm_list_1 (id)); |
| 3822 |
} |
| 3823 |
|
| 3824 |
err = gnutls_openpgp_crt_get_key_id (c_key, (unsigned char *) c_id); |
| 3825 |
scm_gnutls_release_array (&c_id_handle); |
| 3826 |
|
| 3827 |
if (EXPECT_FALSE (err)) |
| 3828 |
scm_gnutls_error (err, FUNC_NAME); |
| 3829 |
|
| 3830 |
return SCM_UNSPECIFIED; |
| 3831 |
} |
| 3832 |
|
| 3833 |
#undef FUNC_NAME |
| 3834 |
|
| 3835 |
SCM_DEFINE (scm_gnutls_openpgp_certificate_fingerpint_x, |
| 3836 |
"%openpgp-certificate-fingerprint!", |
| 3837 |
2, 0, 0, |
| 3838 |
(SCM key, SCM fpr), |
| 3839 |
"Store in @var{fpr} (a u8vector) the fingerprint of @var{key}. " |
| 3840 |
"Return the number of bytes stored in @var{fpr}.") |
| 3841 |
#define FUNC_NAME s_scm_gnutls_openpgp_certificate_fingerpint_x |
| 3842 |
{ |
| 3843 |
int err; |
| 3844 |
gnutls_openpgp_crt_t c_key; |
| 3845 |
char *c_fpr; |
| 3846 |
scm_t_array_handle c_fpr_handle; |
| 3847 |
size_t c_fpr_len, c_actual_len = 0; |
| 3848 |
|
| 3849 |
c_key = scm_to_gnutls_openpgp_certificate (key, 1, FUNC_NAME); |
| 3850 |
SCM_VALIDATE_ARRAY (2, fpr); |
| 3851 |
|
| 3852 |
c_fpr = scm_gnutls_get_writable_array (fpr, &c_fpr_handle, &c_fpr_len, |
| 3853 |
FUNC_NAME); |
| 3854 |
|
| 3855 |
err = gnutls_openpgp_crt_get_fingerprint (c_key, c_fpr, &c_actual_len); |
| 3856 |
scm_gnutls_release_array (&c_fpr_handle); |
| 3857 |
|
| 3858 |
if (EXPECT_FALSE (err)) |
| 3859 |
scm_gnutls_error (err, FUNC_NAME); |
| 3860 |
|
| 3861 |
return (scm_from_size_t (c_actual_len)); |
| 3862 |
} |
| 3863 |
|
| 3864 |
#undef FUNC_NAME |
| 3865 |
|
| 3866 |
SCM_DEFINE (scm_gnutls_openpgp_certificate_fingerprint, |
| 3867 |
"%openpgp-certificate-fingerprint", |
| 3868 |
1, 0, 0, |
| 3869 |
(SCM key), |
| 3870 |
"Return a new u8vector denoting the fingerprint of " "@var{key}.") |
| 3871 |
#define FUNC_NAME s_scm_gnutls_openpgp_certificate_fingerprint |
| 3872 |
{ |
| 3873 |
int err; |
| 3874 |
gnutls_openpgp_crt_t c_key; |
| 3875 |
unsigned char *c_fpr; |
| 3876 |
size_t c_fpr_len, c_actual_len; |
| 3877 |
|
| 3878 |
c_key = scm_to_gnutls_openpgp_certificate (key, 1, FUNC_NAME); |
| 3879 |
|
| 3880 |
/* V4 fingerprints are 160-bit SHA-1 hashes (see RFC2440). */ |
| 3881 |
c_fpr_len = 20; |
| 3882 |
c_fpr = (unsigned char *) malloc (c_fpr_len); |
| 3883 |
if (EXPECT_FALSE (c_fpr == NULL)) |
| 3884 |
scm_gnutls_error (GNUTLS_E_MEMORY_ERROR, FUNC_NAME); |
| 3885 |
|
| 3886 |
do |
| 3887 |
{ |
| 3888 |
c_actual_len = 0; |
| 3889 |
err = gnutls_openpgp_crt_get_fingerprint (c_key, c_fpr, &c_actual_len); |
| 3890 |
if (err == GNUTLS_E_SHORT_MEMORY_BUFFER) |
| 3891 |
{ |
| 3892 |
/* Grow C_FPR. */ |
| 3893 |
unsigned char *c_new; |
| 3894 |
|
| 3895 |
c_new = (unsigned char *) realloc (c_fpr, c_fpr_len * 2); |
| 3896 |
if (EXPECT_FALSE (c_new == NULL)) |
| 3897 |
{ |
| 3898 |
free (c_fpr); |
| 3899 |
scm_gnutls_error (GNUTLS_E_MEMORY_ERROR, FUNC_NAME); |
| 3900 |
} |
| 3901 |
else |
| 3902 |
{ |
| 3903 |
c_fpr_len *= 2; |
| 3904 |
c_fpr = c_new; |
| 3905 |
} |
| 3906 |
} |
| 3907 |
} |
| 3908 |
while (err == GNUTLS_E_SHORT_MEMORY_BUFFER); |
| 3909 |
|
| 3910 |
if (EXPECT_FALSE (err)) |
| 3911 |
{ |
| 3912 |
free (c_fpr); |
| 3913 |
scm_gnutls_error (err, FUNC_NAME); |
| 3914 |
} |
| 3915 |
|
| 3916 |
if (c_actual_len < c_fpr_len) |
| 3917 |
/* Shrink C_FPR. */ |
| 3918 |
c_fpr = realloc (c_fpr, c_actual_len); |
| 3919 |
|
| 3920 |
return (scm_take_u8vector (c_fpr, c_actual_len)); |
| 3921 |
} |
| 3922 |
|
| 3923 |
#undef FUNC_NAME |
| 3924 |
|
| 3925 |
SCM_DEFINE (scm_gnutls_openpgp_certificate_name, "%openpgp-certificate-name", |
| 3926 |
2, 0, 0, |
| 3927 |
(SCM key, SCM index), |
| 3928 |
"Return the @var{index}th name of @var{key}.") |
| 3929 |
#define FUNC_NAME s_scm_gnutls_openpgp_certificate_name |
| 3930 |
{ |
| 3931 |
int err; |
| 3932 |
gnutls_openpgp_crt_t c_key; |
| 3933 |
int c_index; |
| 3934 |
char c_name[GUILE_GNUTLS_MAX_OPENPGP_NAME_LENGTH]; |
| 3935 |
size_t c_name_len = sizeof (c_name); |
| 3936 |
|
| 3937 |
c_key = scm_to_gnutls_openpgp_certificate (key, 1, FUNC_NAME); |
| 3938 |
c_index = scm_to_int (index); |
| 3939 |
|
| 3940 |
err = gnutls_openpgp_crt_get_name (c_key, c_index, c_name, &c_name_len); |
| 3941 |
if (EXPECT_FALSE (err)) |
| 3942 |
scm_gnutls_error (err, FUNC_NAME); |
| 3943 |
|
| 3944 |
/* XXX: The name is really UTF-8. */ |
| 3945 |
return (scm_from_locale_string (c_name)); |
| 3946 |
} |
| 3947 |
|
| 3948 |
#undef FUNC_NAME |
| 3949 |
|
| 3950 |
SCM_DEFINE (scm_gnutls_openpgp_certificate_names, |
| 3951 |
"%openpgp-certificate-names", 1, 0, 0, (SCM key), |
| 3952 |
"Return the list of names for @var{key}.") |
| 3953 |
#define FUNC_NAME s_scm_gnutls_openpgp_certificate_names |
| 3954 |
{ |
| 3955 |
int err; |
| 3956 |
SCM result = SCM_EOL; |
| 3957 |
gnutls_openpgp_crt_t c_key; |
| 3958 |
int c_index = 0; |
| 3959 |
char c_name[GUILE_GNUTLS_MAX_OPENPGP_NAME_LENGTH]; |
| 3960 |
size_t c_name_len = sizeof (c_name); |
| 3961 |
|
| 3962 |
c_key = scm_to_gnutls_openpgp_certificate (key, 1, FUNC_NAME); |
| 3963 |
|
| 3964 |
do |
| 3965 |
{ |
| 3966 |
err = gnutls_openpgp_crt_get_name (c_key, c_index, c_name, &c_name_len); |
| 3967 |
if (!err) |
| 3968 |
{ |
| 3969 |
result = scm_cons (scm_from_locale_string (c_name), result); |
| 3970 |
c_index++; |
| 3971 |
} |
| 3972 |
} |
| 3973 |
while (!err); |
| 3974 |
|
| 3975 |
if (EXPECT_FALSE (err != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)) |
| 3976 |
scm_gnutls_error (err, FUNC_NAME); |
| 3977 |
|
| 3978 |
return (scm_reverse_x (result, SCM_EOL)); |
| 3979 |
} |
| 3980 |
|
| 3981 |
#undef FUNC_NAME |
| 3982 |
|
| 3983 |
SCM_DEFINE (scm_gnutls_openpgp_certificate_algorithm, |
| 3984 |
"%openpgp-certificate-algorithm", |
| 3985 |
1, 0, 0, |
| 3986 |
(SCM key), |
| 3987 |
"Return two values: the certificate algorithm used by " |
| 3988 |
"@var{key} and the number of bits used.") |
| 3989 |
#define FUNC_NAME s_scm_gnutls_openpgp_certificate_algorithm |
| 3990 |
{ |
| 3991 |
gnutls_openpgp_crt_t c_key; |
| 3992 |
unsigned int c_bits; |
| 3993 |
gnutls_pk_algorithm_t c_algo; |
| 3994 |
|
| 3995 |
c_key = scm_to_gnutls_openpgp_certificate (key, 1, FUNC_NAME); |
| 3996 |
c_algo = gnutls_openpgp_crt_get_pk_algorithm (c_key, &c_bits); |
| 3997 |
|
| 3998 |
return (scm_values (scm_list_2 (scm_from_gnutls_pk_algorithm (c_algo), |
| 3999 |
scm_from_uint (c_bits)))); |
| 4000 |
} |
| 4001 |
|
| 4002 |
#undef FUNC_NAME |
| 4003 |
|
| 4004 |
SCM_DEFINE (scm_gnutls_openpgp_certificate_version, |
| 4005 |
"%openpgp-certificate-version", |
| 4006 |
1, 0, 0, |
| 4007 |
(SCM key), |
| 4008 |
"Return the version of the OpenPGP message format (RFC2440) " |
| 4009 |
"honored by @var{key}.") |
| 4010 |
#define FUNC_NAME s_scm_gnutls_openpgp_certificate_version |
| 4011 |
{ |
| 4012 |
int c_version; |
| 4013 |
gnutls_openpgp_crt_t c_key; |
| 4014 |
|
| 4015 |
c_key = scm_to_gnutls_openpgp_certificate (key, 1, FUNC_NAME); |
| 4016 |
c_version = gnutls_openpgp_crt_get_version (c_key); |
| 4017 |
|
| 4018 |
return (scm_from_int (c_version)); |
| 4019 |
} |
| 4020 |
|
| 4021 |
#undef FUNC_NAME |
| 4022 |
|
| 4023 |
SCM_DEFINE (scm_gnutls_openpgp_certificate_usage, |
| 4024 |
"%openpgp-certificate-usage", 1, 0, 0, (SCM key), |
| 4025 |
"Return a list of values denoting the key usage of @var{key}.") |
| 4026 |
#define FUNC_NAME s_scm_gnutls_openpgp_certificate_usage |
| 4027 |
{ |
| 4028 |
int err; |
| 4029 |
unsigned int c_usage = 0; |
| 4030 |
gnutls_openpgp_crt_t c_key; |
| 4031 |
|
| 4032 |
c_key = scm_to_gnutls_openpgp_certificate (key, 1, FUNC_NAME); |
| 4033 |
|
| 4034 |
err = gnutls_openpgp_crt_get_key_usage (c_key, &c_usage); |
| 4035 |
if (EXPECT_FALSE (err)) |
| 4036 |
scm_gnutls_error (err, FUNC_NAME); |
| 4037 |
|
| 4038 |
return (scm_from_gnutls_key_usage_flags (c_usage)); |
| 4039 |
} |
| 4040 |
|
| 4041 |
#undef FUNC_NAME |
| 4042 |
|
| 4043 |
|
| 4044 |
|
| 4045 |
/* OpenPGP keyrings. */ |
| 4046 |
|
| 4047 |
SCM_DEFINE (scm_gnutls_import_openpgp_keyring, "import-openpgp-keyring", |
| 4048 |
2, 0, 0, |
| 4049 |
(SCM data, SCM format), |
| 4050 |
"Import @var{data} (a u8vector) according to @var{format} " |
| 4051 |
"and return the imported keyring.") |
| 4052 |
#define FUNC_NAME s_scm_gnutls_import_openpgp_keyring |
| 4053 |
{ |
| 4054 |
int err; |
| 4055 |
gnutls_openpgp_keyring_t c_keyring; |
| 4056 |
gnutls_openpgp_crt_fmt_t c_format; |
| 4057 |
gnutls_datum_t c_data_d; |
| 4058 |
scm_t_array_handle c_data_handle; |
| 4059 |
const char *c_data; |
| 4060 |
size_t c_data_len; |
| 4061 |
|
| 4062 |
SCM_VALIDATE_ARRAY (1, data); |
| 4063 |
c_format = scm_to_gnutls_openpgp_certificate_format (format, 2, FUNC_NAME); |
| 4064 |
|
| 4065 |
c_data = scm_gnutls_get_array (data, &c_data_handle, &c_data_len, |
| 4066 |
FUNC_NAME); |
| 4067 |
|
| 4068 |
c_data_d.data = (unsigned char *) c_data; |
| 4069 |
c_data_d.size = c_data_len; |
| 4070 |
|
| 4071 |
err = gnutls_openpgp_keyring_init (&c_keyring); |
| 4072 |
if (EXPECT_FALSE (err)) |
| 4073 |
{ |
| 4074 |
scm_gnutls_release_array (&c_data_handle); |
| 4075 |
scm_gnutls_error (err, FUNC_NAME); |
| 4076 |
} |
| 4077 |
|
| 4078 |
err = gnutls_openpgp_keyring_import (c_keyring, &c_data_d, c_format); |
| 4079 |
scm_gnutls_release_array (&c_data_handle); |
| 4080 |
|
| 4081 |
if (EXPECT_FALSE (err)) |
| 4082 |
{ |
| 4083 |
gnutls_openpgp_keyring_deinit (c_keyring); |
| 4084 |
scm_gnutls_error (err, FUNC_NAME); |
| 4085 |
} |
| 4086 |
|
| 4087 |
return (scm_from_gnutls_openpgp_keyring (c_keyring)); |
| 4088 |
} |
| 4089 |
|
| 4090 |
#undef FUNC_NAME |
| 4091 |
|
| 4092 |
SCM_DEFINE (scm_gnutls_openpgp_keyring_contains_key_id_p, |
| 4093 |
"%openpgp-keyring-contains-key-id?", |
| 4094 |
2, 0, 0, |
| 4095 |
(SCM keyring, SCM id), |
| 4096 |
"Return @code{#f} if key ID @var{id} is in @var{keyring}, " |
| 4097 |
"@code{#f} otherwise.") |
| 4098 |
#define FUNC_NAME s_scm_gnutls_openpgp_keyring_contains_key_id_p |
| 4099 |
{ |
| 4100 |
int c_result; |
| 4101 |
gnutls_openpgp_keyring_t c_keyring; |
| 4102 |
scm_t_array_handle c_id_handle; |
| 4103 |
const char *c_id; |
| 4104 |
size_t c_id_len; |
| 4105 |
|
| 4106 |
c_keyring = scm_to_gnutls_openpgp_keyring (keyring, 1, FUNC_NAME); |
| 4107 |
SCM_VALIDATE_ARRAY (1, id); |
| 4108 |
|
| 4109 |
c_id = scm_gnutls_get_array (id, &c_id_handle, &c_id_len, FUNC_NAME); |
| 4110 |
if (EXPECT_FALSE (c_id_len != 8)) |
| 4111 |
{ |
| 4112 |
scm_gnutls_release_array (&c_id_handle); |
| 4113 |
scm_wrong_type_arg (FUNC_NAME, 1, id); |
| 4114 |
} |
| 4115 |
|
| 4116 |
c_result = gnutls_openpgp_keyring_check_id (c_keyring, |
| 4117 |
(unsigned char *) c_id, |
| 4118 |
0 /* unused */ ); |
| 4119 |
|
| 4120 |
scm_gnutls_release_array (&c_id_handle); |
| 4121 |
|
| 4122 |
return (scm_from_bool (c_result == 0)); |
| 4123 |
} |
| 4124 |
|
| 4125 |
#undef FUNC_NAME |
| 4126 |
|
| 4127 |
|
| 4128 |
/* OpenPGP certificates. */ |
| 4129 |
|
| 4130 |
SCM_DEFINE (scm_gnutls_set_certificate_credentials_openpgp_keys_x, |
| 4131 |
"%set-certificate-credentials-openpgp-keys!", |
| 4132 |
3, 0, 0, |
| 4133 |
(SCM cred, SCM pub, SCM sec), |
| 4134 |
"Use certificate @var{pub} and secret key @var{sec} in " |
| 4135 |
"certificate credentials @var{cred}.") |
| 4136 |
#define FUNC_NAME s_scm_gnutls_set_certificate_credentials_openpgp_keys_x |
| 4137 |
{ |
| 4138 |
int err; |
| 4139 |
gnutls_certificate_credentials_t c_cred; |
| 4140 |
gnutls_openpgp_crt_t c_pub; |
| 4141 |
gnutls_openpgp_privkey_t c_sec; |
| 4142 |
|
| 4143 |
c_cred = scm_to_gnutls_certificate_credentials (cred, 1, FUNC_NAME); |
| 4144 |
c_pub = scm_to_gnutls_openpgp_certificate (pub, 2, FUNC_NAME); |
| 4145 |
c_sec = scm_to_gnutls_openpgp_private_key (sec, 3, FUNC_NAME); |
| 4146 |
|
| 4147 |
err = gnutls_certificate_set_openpgp_key (c_cred, c_pub, c_sec); |
| 4148 |
if (EXPECT_FALSE (err)) |
| 4149 |
scm_gnutls_error (err, FUNC_NAME); |
| 4150 |
|
| 4151 |
return SCM_UNSPECIFIED; |
| 4152 |
} |
| 4153 |
|
| 4154 |
#undef FUNC_NAME |
| 4155 |
|
| 4156 |
|
| 4157 |
|
| 4158 |
/* Debugging. */ |
| 4159 |
|
| 4160 |
static SCM log_procedure = SCM_BOOL_F; |
| 4161 |
|
| 4162 |
static void |
| 4163 |
scm_gnutls_log (int level, const char *str) |
| 4164 |
{ |
| 4165 |
if (scm_is_true (log_procedure)) |
| 4166 |
(void) scm_call_2 (log_procedure, scm_from_int (level), |
| 4167 |
scm_from_locale_string (str)); |
| 4168 |
} |
| 4169 |
|
| 4170 |
SCM_DEFINE (scm_gnutls_set_log_procedure_x, "set-log-procedure!", |
| 4171 |
1, 0, 0, |
| 4172 |
(SCM proc), |
| 4173 |
"Use @var{proc} (a two-argument procedure) as the global " |
| 4174 |
"GnuTLS log procedure.") |
| 4175 |
#define FUNC_NAME s_scm_gnutls_set_log_procedure_x |
| 4176 |
{ |
| 4177 |
SCM_VALIDATE_PROC (1, proc); |
| 4178 |
|
| 4179 |
if (scm_is_true (log_procedure)) |
| 4180 |
(void) scm_gc_unprotect_object (log_procedure); |
| 4181 |
|
| 4182 |
log_procedure = scm_gc_protect_object (proc); |
| 4183 |
gnutls_global_set_log_function (scm_gnutls_log); |
| 4184 |
|
| 4185 |
return SCM_UNSPECIFIED; |
| 4186 |
} |
| 4187 |
|
| 4188 |
#undef FUNC_NAME |
| 4189 |
|
| 4190 |
SCM_DEFINE (scm_gnutls_set_log_level_x, "set-log-level!", 1, 0, 0, |
| 4191 |
(SCM level), |
| 4192 |
"Enable GnuTLS logging up to @var{level} (an integer).") |
| 4193 |
#define FUNC_NAME s_scm_gnutls_set_log_level_x |
| 4194 |
{ |
| 4195 |
unsigned int c_level; |
| 4196 |
|
| 4197 |
c_level = scm_to_uint (level); |
| 4198 |
gnutls_global_set_log_level (c_level); |
| 4199 |
|
| 4200 |
return SCM_UNSPECIFIED; |
| 4201 |
} |
| 4202 |
|
| 4203 |
#undef FUNC_NAME |
| 4204 |
|
| 4205 |
|
| 4206 |
/* Hmac */ |
| 4207 |
|
| 4208 |
SCM_DEFINE (scm_gnutls_hmac_direct, "hmac-direct", 3, 0, 0, |
| 4209 |
(SCM algorithm, SCM key, SCM text), |
| 4210 |
"Hash @var{text} with @var{algorithm}, and " |
| 4211 |
"the secret @var{key}. It will not work if " |
| 4212 |
"@var{algorithm} requires a nonce, such as " |
| 4213 |
"UMAC or GMAC. Both @var{key} and @var{text} " |
| 4214 |
"must be bytevectors.") |
| 4215 |
#define FUNC_NAME s_scm_gnutls_hmac_direct |
| 4216 |
{ |
| 4217 |
int error; |
| 4218 |
gnutls_mac_algorithm_t c_algorithm = |
| 4219 |
scm_to_gnutls_mac (algorithm, 1, FUNC_NAME); |
| 4220 |
size_t c_key_length = scm_c_bytevector_length (key); |
| 4221 |
const void *c_key = SCM_BYTEVECTOR_CONTENTS (key); |
| 4222 |
size_t c_ptext_length = scm_c_bytevector_length (text); |
| 4223 |
const void *c_ptext = SCM_BYTEVECTOR_CONTENTS (text); |
| 4224 |
unsigned digest_length = gnutls_hmac_get_len (c_algorithm); |
| 4225 |
if (EXPECT_FALSE (digest_length == 0)) |
| 4226 |
{ |
| 4227 |
/* I guess throwing unknown-algorithm is correct, if gnutls can’t find a |
| 4228 |
meaningful value for the algorithm digest size. */ |
| 4229 |
scm_gnutls_error (GNUTLS_E_UNKNOWN_ALGORITHM, FUNC_NAME); |
| 4230 |
} |
| 4231 |
SCM digest = scm_c_make_bytevector (digest_length); |
| 4232 |
void *c_digest = SCM_BYTEVECTOR_CONTENTS (digest); |
| 4233 |
error = |
| 4234 |
gnutls_hmac_fast (c_algorithm, c_key, c_key_length, c_ptext, |
| 4235 |
c_ptext_length, c_digest); |
| 4236 |
if (EXPECT_FALSE (error)) |
| 4237 |
{ |
| 4238 |
scm_gnutls_error (error, FUNC_NAME); |
| 4239 |
} |
| 4240 |
return digest; |
| 4241 |
} |
| 4242 |
|
| 4243 |
#undef FUNC_NAME |
| 4244 |
|
| 4245 |
SCM_DEFINE (scm_gnutls_make_hmac, "make-hmac", 2, 0, 0, |
| 4246 |
(SCM algorithm, SCM key), |
| 4247 |
"Return a new hmac object that can be fed " |
| 4248 |
"further input to hash. Use the given MAC " |
| 4249 |
"or HMAC @var{algorithm}, and use " |
| 4250 |
"@var{key} (a bytevector) as the secret.") |
| 4251 |
#define FUNC_NAME s_scm_gnutls_make_hmac |
| 4252 |
{ |
| 4253 |
int error; |
| 4254 |
scm_gnutls_hmac_and_algorithm_t c_hmac = |
| 4255 |
scm_gc_malloc (sizeof (struct scm_gnutls_hmac_and_algorithm), |
| 4256 |
"hmac-and-algorithm"); |
| 4257 |
gnutls_mac_algorithm_t c_algorithm; |
| 4258 |
size_t c_key_length = scm_c_bytevector_length (key); |
| 4259 |
const void *c_key = SCM_BYTEVECTOR_CONTENTS (key); |
| 4260 |
c_algorithm = scm_to_gnutls_mac (algorithm, 1, FUNC_NAME); |
| 4261 |
c_hmac->algorithm = c_algorithm; |
| 4262 |
error = |
| 4263 |
gnutls_hmac_init (&(c_hmac->handle), c_algorithm, c_key, c_key_length); |
| 4264 |
if (EXPECT_FALSE (error)) |
| 4265 |
{ |
| 4266 |
scm_gnutls_error (error, FUNC_NAME); |
| 4267 |
} |
| 4268 |
return scm_from_gnutls_hmac (c_hmac); |
| 4269 |
} |
| 4270 |
|
| 4271 |
#undef FUNC_NAME |
| 4272 |
|
| 4273 |
static void |
| 4274 |
hmac_and_algorithm_deinit (scm_gnutls_hmac_and_algorithm_t c_hmac) |
| 4275 |
{ |
| 4276 |
gnutls_hmac_deinit (c_hmac->handle, NULL); |
| 4277 |
} |
| 4278 |
|
| 4279 |
SCM_DEFINE (scm_gnutls_hmac_x, "hmac!", 2, 0, 0, |
| 4280 |
(SCM hmac, SCM text), |
| 4281 |
"Hash the @var{text} bytes in the @var{hmac} state.") |
| 4282 |
#define FUNC_NAME s_scm_gnutls_hmac_x |
| 4283 |
{ |
| 4284 |
int error; |
| 4285 |
scm_gnutls_hmac_and_algorithm_t c_hmac = |
| 4286 |
scm_to_gnutls_hmac (hmac, 1, FUNC_NAME); |
| 4287 |
size_t c_ptext_length = scm_c_bytevector_length (text); |
| 4288 |
const void *c_ptext = SCM_BYTEVECTOR_CONTENTS (text); |
| 4289 |
error = gnutls_hmac (c_hmac->handle, c_ptext, c_ptext_length); |
| 4290 |
if (EXPECT_FALSE (error)) |
| 4291 |
{ |
| 4292 |
scm_gnutls_error (error, FUNC_NAME); |
| 4293 |
} |
| 4294 |
return SCM_UNSPECIFIED; |
| 4295 |
} |
| 4296 |
|
| 4297 |
#undef FUNC_NAME |
| 4298 |
|
| 4299 |
#ifdef HAVE_GNUTLS_HMAC_GET_KEY_SIZE |
| 4300 |
SCM_DEFINE (scm_gnutls_hmac_key_size, "hmac-key-size", 1, 0, 0, |
| 4301 |
(SCM algorithm), |
| 4302 |
"Return the default key size for @var{algorithm}, " |
| 4303 |
"or 0 if unavailable. " |
| 4304 |
"This function is only provided under the " |
| 4305 |
"@code{'gnutls-hmac-key-size} Guile feature check " |
| 4306 |
"(@pxref{Living on the cutting edge}).") |
| 4307 |
# define FUNC_NAME s_scm_gnutls_hmac_key_size |
| 4308 |
{ |
| 4309 |
gnutls_mac_algorithm_t c_algorithm = |
| 4310 |
scm_to_gnutls_mac (algorithm, 1, FUNC_NAME); |
| 4311 |
return scm_from_uint (gnutls_hmac_get_key_size (c_algorithm)); |
| 4312 |
} |
| 4313 |
|
| 4314 |
# undef FUNC_NAME |
| 4315 |
#endif /* HAVE_GNUTLS_HMAC_GET_KEY_SIZE */ |
| 4316 |
|
| 4317 |
#ifdef HAVE_GNUTLS_HMAC_COPY |
| 4318 |
SCM_DEFINE (scm_gnutls_hmac_copy, "hmac-copy", 1, 0, 0, |
| 4319 |
(SCM hmac), |
| 4320 |
"Return a copy of the current @var{hmac} state, " |
| 4321 |
"or throw an @code{error/illegal-parameter} " |
| 4322 |
"if this is not supported. ") |
| 4323 |
# define FUNC_NAME s_scm_gnutls_hmac_copy |
| 4324 |
{ |
| 4325 |
scm_gnutls_hmac_and_algorithm_t c_hmac = |
| 4326 |
scm_to_gnutls_hmac (hmac, 1, FUNC_NAME); |
| 4327 |
gnutls_hmac_hd_t c_ret = NULL; |
| 4328 |
c_ret = gnutls_hmac_copy (c_hmac->handle); |
| 4329 |
if (EXPECT_FALSE (c_ret == NULL)) |
| 4330 |
{ |
| 4331 |
scm_gnutls_error (GNUTLS_E_ILLEGAL_PARAMETER, FUNC_NAME); |
| 4332 |
} |
| 4333 |
scm_gnutls_hmac_and_algorithm_t c_combined = |
| 4334 |
scm_gc_malloc (sizeof (struct scm_gnutls_hmac_and_algorithm), |
| 4335 |
"hmac-and-algorithm"); |
| 4336 |
c_combined->handle = c_ret; |
| 4337 |
c_combined->algorithm = c_hmac->algorithm; |
| 4338 |
return scm_from_gnutls_hmac (c_combined); |
| 4339 |
} |
| 4340 |
#endif /* HAVE_GNUTLS_HMAC_COPY */ |
| 4341 |
|
| 4342 |
#undef FUNC_NAME |
| 4343 |
|
| 4344 |
SCM_DEFINE (scm_gnutls_hmac_algorithm, "hmac-algorithm", 1, 0, 0, |
| 4345 |
(SCM hmac), |
| 4346 |
"Return the algorithm that @var{hmac} has been built for.") |
| 4347 |
#define FUNC_NAME s_scm_gnutls_hmac_algorithm |
| 4348 |
{ |
| 4349 |
scm_gnutls_hmac_and_algorithm_t c_hmac = |
| 4350 |
scm_to_gnutls_hmac (hmac, 1, FUNC_NAME); |
| 4351 |
return scm_from_gnutls_mac (c_hmac->algorithm); |
| 4352 |
} |
| 4353 |
|
| 4354 |
#undef FUNC_NAME |
| 4355 |
|
| 4356 |
SCM_DEFINE (scm_gnutls_hmac_length, "hmac-length", 1, 0, 0, |
| 4357 |
(SCM algorithm), |
| 4358 |
"Return the length of the @var{algorithm} " |
| 4359 |
"HMAC output, or 0 if unavailable.") |
| 4360 |
#define FUNC_NAME s_scm_gnutls_hmac_length |
| 4361 |
{ |
| 4362 |
gnutls_mac_algorithm_t c_algorithm = |
| 4363 |
scm_to_gnutls_mac (algorithm, 1, FUNC_NAME); |
| 4364 |
unsigned int c_ret = gnutls_hmac_get_len (c_algorithm); |
| 4365 |
return scm_from_uint (c_ret); |
| 4366 |
} |
| 4367 |
|
| 4368 |
#undef FUNC_NAME |
| 4369 |
|
| 4370 |
SCM_DEFINE (scm_gnutls_hmac_output, "hmac-output", 1, 0, 0, |
| 4371 |
(SCM hmac), "Return the digest of the current @var{hmac} state.") |
| 4372 |
#define FUNC_NAME s_scm_gnutls_hmac_output |
| 4373 |
{ |
| 4374 |
scm_gnutls_hmac_and_algorithm_t c_hmac = |
| 4375 |
scm_to_gnutls_hmac (hmac, 1, FUNC_NAME); |
| 4376 |
unsigned digest_length = gnutls_hmac_get_len (c_hmac->algorithm); |
| 4377 |
if (EXPECT_FALSE (digest_length == 0)) |
| 4378 |
{ |
| 4379 |
/* See hmac-direct. */ |
| 4380 |
scm_gnutls_error (GNUTLS_E_UNKNOWN_ALGORITHM, FUNC_NAME); |
| 4381 |
} |
| 4382 |
SCM digest = scm_c_make_bytevector (digest_length); |
| 4383 |
void *c_digest = SCM_BYTEVECTOR_CONTENTS (digest); |
| 4384 |
gnutls_hmac_output (c_hmac->handle, c_digest); |
| 4385 |
return digest; |
| 4386 |
} |
| 4387 |
|
| 4388 |
#undef FUNC_NAME |
| 4389 |
|
| 4390 |
SCM_DEFINE (scm_gnutls_set_hmac_nonce_x, "set-hmac-nonce!", 2, 0, 0, |
| 4391 |
(SCM hmac, SCM nonce), "Set @var{nonce} in the @var{hmac} state.") |
| 4392 |
#define FUNC_NAME s_scm_gnutls_set_hmac_nonce_x |
| 4393 |
{ |
| 4394 |
scm_gnutls_hmac_and_algorithm_t c_hmac = |
| 4395 |
scm_to_gnutls_hmac (hmac, 1, FUNC_NAME); |
| 4396 |
size_t c_nonce_length = scm_c_bytevector_length (nonce); |
| 4397 |
const void *c_nonce = SCM_BYTEVECTOR_CONTENTS (nonce); |
| 4398 |
gnutls_hmac_set_nonce (c_hmac->handle, c_nonce, c_nonce_length); |
| 4399 |
return SCM_UNSPECIFIED; |
| 4400 |
} |
| 4401 |
|
| 4402 |
#undef FUNC_NAME |
| 4403 |
|
| 4404 |
SCM_DEFINE (scm_gnutls_mac_nonce_size, "mac-nonce-size", 1, 0, 0, |
| 4405 |
(SCM algorithm), |
| 4406 |
"Return the length of the nonce for @var{algorithm}, " |
| 4407 |
"or 0 if unavailable.") |
| 4408 |
#define FUNC_NAME s_scm_gnutls_mac_nonce_size |
| 4409 |
{ |
| 4410 |
gnutls_mac_algorithm_t c_algorithm = |
| 4411 |
scm_to_gnutls_mac (algorithm, 1, FUNC_NAME); |
| 4412 |
size_t ret = gnutls_mac_get_nonce_size (c_algorithm); |
| 4413 |
return scm_from_size_t (ret); |
| 4414 |
} |
| 4415 |
|
| 4416 |
#undef FUNC_NAME |
| 4417 |
|
| 4418 |
|
| 4419 |
/* Hash functions */ |
| 4420 |
|
| 4421 |
SCM_DEFINE (scm_gnutls_hash_direct, "hash-direct", 2, 0, 0, |
| 4422 |
(SCM algorithm, SCM text), |
| 4423 |
"Hash @var{text} according to @var{algorithm}. " |
| 4424 |
"Return the digest as a bytevector.") |
| 4425 |
#define FUNC_NAME s_scm_gnutls_hash_direct |
| 4426 |
{ |
| 4427 |
int error; |
| 4428 |
gnutls_digest_algorithm_t c_algorithm = |
| 4429 |
scm_to_gnutls_digest (algorithm, 1, FUNC_NAME); |
| 4430 |
size_t c_ptext_length = scm_c_bytevector_length (text); |
| 4431 |
const void *c_ptext = SCM_BYTEVECTOR_CONTENTS (text); |
| 4432 |
unsigned digest_length = gnutls_hash_get_len (c_algorithm); |
| 4433 |
if (EXPECT_FALSE (digest_length == 0)) |
| 4434 |
{ |
| 4435 |
/* See hmac-direct */ |
| 4436 |
scm_gnutls_error (GNUTLS_E_UNKNOWN_ALGORITHM, FUNC_NAME); |
| 4437 |
} |
| 4438 |
SCM digest = scm_c_make_bytevector (digest_length); |
| 4439 |
void *c_digest = SCM_BYTEVECTOR_CONTENTS (digest); |
| 4440 |
error = gnutls_hash_fast (c_algorithm, c_ptext, c_ptext_length, c_digest); |
| 4441 |
if (EXPECT_FALSE (error)) |
| 4442 |
{ |
| 4443 |
scm_gnutls_error (error, FUNC_NAME); |
| 4444 |
} |
| 4445 |
return digest; |
| 4446 |
} |
| 4447 |
|
| 4448 |
#undef FUNC_NAME |
| 4449 |
|
| 4450 |
SCM_DEFINE (scm_gnutls_make_hash, "make-hash", 1, 0, 0, |
| 4451 |
(SCM algorithm), |
| 4452 |
"Start a hash operation according to @var{algorithm}.") |
| 4453 |
#define FUNC_NAME s_scm_gnutls_make_hash |
| 4454 |
{ |
| 4455 |
int error; |
| 4456 |
scm_gnutls_hash_and_algorithm_t c_hash = |
| 4457 |
scm_gc_malloc (sizeof (struct scm_gnutls_hash_and_algorithm), |
| 4458 |
"hash-and-algorithm"); |
| 4459 |
gnutls_digest_algorithm_t c_algorithm = |
| 4460 |
scm_to_gnutls_digest (algorithm, 1, FUNC_NAME); |
| 4461 |
c_hash->algorithm = c_algorithm; |
| 4462 |
error = gnutls_hash_init (&(c_hash->handle), c_algorithm); |
| 4463 |
if (EXPECT_FALSE (error)) |
| 4464 |
{ |
| 4465 |
scm_gnutls_error (error, FUNC_NAME); |
| 4466 |
} |
| 4467 |
return scm_from_gnutls_hash (c_hash); |
| 4468 |
} |
| 4469 |
|
| 4470 |
#undef FUNC_NAME |
| 4471 |
|
| 4472 |
static void |
| 4473 |
hash_and_algorithm_deinit (scm_gnutls_hash_and_algorithm_t c_hash) |
| 4474 |
{ |
| 4475 |
gnutls_hash_deinit (c_hash->handle, NULL); |
| 4476 |
} |
| 4477 |
|
| 4478 |
SCM_DEFINE (scm_gnutls_hash_x, "hash!", 2, 0, 0, |
| 4479 |
(SCM hash, SCM text), |
| 4480 |
"Hash the @var{text} bytes in the @var{hash} state.") |
| 4481 |
#define FUNC_NAME s_scm_gnutls_hash_x |
| 4482 |
{ |
| 4483 |
int error; |
| 4484 |
scm_gnutls_hash_and_algorithm_t c_hash = |
| 4485 |
scm_to_gnutls_hash (hash, 1, FUNC_NAME); |
| 4486 |
size_t c_ptext_length = scm_c_bytevector_length (text); |
| 4487 |
const void *c_ptext = SCM_BYTEVECTOR_CONTENTS (text); |
| 4488 |
error = gnutls_hash (c_hash->handle, c_ptext, c_ptext_length); |
| 4489 |
if (EXPECT_FALSE (error)) |
| 4490 |
{ |
| 4491 |
scm_gnutls_error (error, FUNC_NAME); |
| 4492 |
} |
| 4493 |
return SCM_UNSPECIFIED; |
| 4494 |
} |
| 4495 |
|
| 4496 |
#undef FUNC_NAME |
| 4497 |
|
| 4498 |
#ifdef HAVE_GNUTLS_HASH_COPY |
| 4499 |
SCM_DEFINE (scm_gnutls_hash_copy, "hash-copy", 1, 0, 0, |
| 4500 |
(SCM hash), |
| 4501 |
"Return a copy of the current @var{hash} state, " |
| 4502 |
"or throw an @code{error/illegal-parameter} " |
| 4503 |
"if this is not supported. ") |
| 4504 |
# define FUNC_NAME s_scm_gnutls_hash_copy |
| 4505 |
{ |
| 4506 |
scm_gnutls_hash_and_algorithm_t c_hash = |
| 4507 |
scm_to_gnutls_hash (hash, 1, FUNC_NAME); |
| 4508 |
gnutls_hash_hd_t c_ret = NULL; |
| 4509 |
c_ret = gnutls_hash_copy (c_hash->handle); |
| 4510 |
if (EXPECT_FALSE (c_ret == NULL)) |
| 4511 |
{ |
| 4512 |
scm_gnutls_error (GNUTLS_E_ILLEGAL_PARAMETER, FUNC_NAME); |
| 4513 |
} |
| 4514 |
scm_gnutls_hash_and_algorithm_t c_combined = |
| 4515 |
scm_gc_malloc (sizeof (struct scm_gnutls_hash_and_algorithm), |
| 4516 |
"hash-and-algorithm"); |
| 4517 |
c_combined->handle = c_ret; |
| 4518 |
c_combined->algorithm = c_hash->algorithm; |
| 4519 |
return scm_from_gnutls_hash (c_combined); |
| 4520 |
} |
| 4521 |
|
| 4522 |
# undef FUNC_NAME |
| 4523 |
#endif /* HAVE_GNUTLS_HASH_COPY */ |
| 4524 |
|
| 4525 |
SCM_DEFINE (scm_gnutls_hash_algorithm, "hash-algorithm", 1, 0, 0, |
| 4526 |
(SCM hash), |
| 4527 |
"Return the algorithm that @var{hash} has been built for.") |
| 4528 |
#define FUNC_NAME s_scm_gnutls_hash_algorithm |
| 4529 |
{ |
| 4530 |
scm_gnutls_hash_and_algorithm_t c_hash = |
| 4531 |
scm_to_gnutls_hash (hash, 1, FUNC_NAME); |
| 4532 |
return scm_from_gnutls_digest (c_hash->algorithm); |
| 4533 |
} |
| 4534 |
|
| 4535 |
#undef FUNC_NAME |
| 4536 |
|
| 4537 |
SCM_DEFINE (scm_gnutls_hash_length, "hash-length", 1, 0, 0, |
| 4538 |
(SCM algorithm), |
| 4539 |
"Return the length of the @var{algorithm} " |
| 4540 |
"digest output, or 0 if unavailable.") |
| 4541 |
#define FUNC_NAME s_scm_gnutls_hash_length |
| 4542 |
{ |
| 4543 |
gnutls_digest_algorithm_t c_algorithm = |
| 4544 |
scm_to_gnutls_digest (algorithm, 1, FUNC_NAME); |
| 4545 |
unsigned int c_ret = gnutls_hash_get_len (c_algorithm); |
| 4546 |
return scm_from_uint (c_ret); |
| 4547 |
} |
| 4548 |
|
| 4549 |
#undef FUNC_NAME |
| 4550 |
|
| 4551 |
SCM_DEFINE (scm_gnutls_hash_output, "hash-output", 1, 0, 0, |
| 4552 |
(SCM hash), "Return the digest of the current @var{hash} state.") |
| 4553 |
#define FUNC_NAME s_scm_gnutls_hash_output |
| 4554 |
{ |
| 4555 |
scm_gnutls_hash_and_algorithm_t c_hash = |
| 4556 |
scm_to_gnutls_hash (hash, 1, FUNC_NAME); |
| 4557 |
unsigned digest_length = gnutls_hash_get_len (c_hash->algorithm); |
| 4558 |
if (EXPECT_FALSE (digest_length == 0)) |
| 4559 |
{ |
| 4560 |
/* See hash-direct. */ |
| 4561 |
scm_gnutls_error (GNUTLS_E_UNKNOWN_ALGORITHM, FUNC_NAME); |
| 4562 |
} |
| 4563 |
SCM digest = scm_c_make_bytevector (digest_length); |
| 4564 |
void *c_digest = SCM_BYTEVECTOR_CONTENTS (digest); |
| 4565 |
gnutls_hash_output (c_hash->handle, c_digest); |
| 4566 |
return digest; |
| 4567 |
} |
| 4568 |
|
| 4569 |
#undef FUNC_NAME |
| 4570 |
|
| 4571 |
SCM_DEFINE (scm_gnutls_cipher_tag_size, "cipher-tag-size", 1, 0, 0, |
| 4572 |
(SCM algorithm), |
| 4573 |
"Return the default tag size for @var{algorithm}, " |
| 4574 |
"or 0 if this is not an AEAD algorithm.") |
| 4575 |
#define FUNC_NAME s_scm_gnutls_cipher_tag_size |
| 4576 |
{ |
| 4577 |
gnutls_cipher_algorithm_t c_algorithm; |
| 4578 |
c_algorithm = scm_to_gnutls_cipher (algorithm, 1, FUNC_NAME); |
| 4579 |
return scm_from_uint (gnutls_cipher_get_tag_size (c_algorithm)); |
| 4580 |
} |
| 4581 |
|
| 4582 |
#undef FUNC_NAME |
| 4583 |
|
| 4584 |
SCM_DEFINE (scm_gnutls_cipher_key_size, "cipher-key-size", 1, 0, 0, |
| 4585 |
(SCM algorithm), |
| 4586 |
"Return the required key size for @var{algorithm}.") |
| 4587 |
#define FUNC_NAME s_scm_gnutls_cipher_key_size |
| 4588 |
{ |
| 4589 |
gnutls_cipher_algorithm_t c_algorithm; |
| 4590 |
c_algorithm = scm_to_gnutls_cipher (algorithm, 1, FUNC_NAME); |
| 4591 |
return scm_from_uint (gnutls_cipher_get_key_size (c_algorithm)); |
| 4592 |
} |
| 4593 |
|
| 4594 |
#undef FUNC_NAME |
| 4595 |
|
| 4596 |
SCM_DEFINE (scm_gnutls_cipher_block_size, "cipher-block-size", 1, 0, 0, |
| 4597 |
(SCM algorithm), |
| 4598 |
"Return the required block size for @var{algorithm}.") |
| 4599 |
#define FUNC_NAME s_scm_gnutls_cipher_block_size |
| 4600 |
{ |
| 4601 |
gnutls_cipher_algorithm_t c_algorithm; |
| 4602 |
c_algorithm = scm_to_gnutls_cipher (algorithm, 1, FUNC_NAME); |
| 4603 |
return scm_from_uint (gnutls_cipher_get_block_size (c_algorithm)); |
| 4604 |
} |
| 4605 |
|
| 4606 |
#undef FUNC_NAME |
| 4607 |
|
| 4608 |
SCM_DEFINE (scm_gnutls_cipher_iv_size, "cipher-iv-size", 1, 0, 0, |
| 4609 |
(SCM algorithm), |
| 4610 |
"Return the length of the initialization vector for @var{algorithm}.") |
| 4611 |
#define FUNC_NAME s_scm_gnutls_cipher_iv_size |
| 4612 |
{ |
| 4613 |
gnutls_cipher_algorithm_t c_algorithm; |
| 4614 |
c_algorithm = scm_to_gnutls_cipher (algorithm, 1, FUNC_NAME); |
| 4615 |
return scm_from_uint (gnutls_cipher_get_iv_size (c_algorithm)); |
| 4616 |
} |
| 4617 |
|
| 4618 |
#undef FUNC_NAME |
| 4619 |
|
| 4620 |
SCM_DEFINE (scm_gnutls_make_aead_cipher, "make-aead-cipher", 2, 0, 0, |
| 4621 |
(SCM algorithm, SCM key), |
| 4622 |
"Return a new AEAD cipher context, using the AEAD @var{algorithm}, " |
| 4623 |
"and with @var{key} (a bytevector) as the secret.") |
| 4624 |
#define FUNC_NAME s_scm_gnutls_make_aead_cipher |
| 4625 |
{ |
| 4626 |
int error; |
| 4627 |
scm_gnutls_aead_cipher_and_algorithm_t c_aead = |
| 4628 |
scm_gc_malloc (sizeof (struct scm_gnutls_aead_cipher_and_algorithm), |
| 4629 |
"aead-cipher-and-algorithm"); |
| 4630 |
gnutls_cipher_algorithm_t c_algorithm; |
| 4631 |
size_t c_key_length = scm_c_bytevector_length (key); |
| 4632 |
const void *c_key = SCM_BYTEVECTOR_CONTENTS (key); |
| 4633 |
gnutls_datum_t datum_key; |
| 4634 |
datum_key.data = (unsigned char *) c_key; |
| 4635 |
datum_key.size = c_key_length; |
| 4636 |
c_algorithm = scm_to_gnutls_cipher (algorithm, 1, FUNC_NAME); |
| 4637 |
c_aead->algorithm = c_algorithm; |
| 4638 |
error = |
| 4639 |
gnutls_aead_cipher_init (&(c_aead->handle), c_algorithm, &datum_key); |
| 4640 |
if (EXPECT_FALSE (error)) |
| 4641 |
{ |
| 4642 |
scm_gnutls_error (error, FUNC_NAME); |
| 4643 |
} |
| 4644 |
return scm_from_gnutls_aead_cipher (c_aead); |
| 4645 |
} |
| 4646 |
|
| 4647 |
#undef FUNC_NAME |
| 4648 |
|
| 4649 |
static void |
| 4650 |
aead_cipher_and_algorithm_deinit (scm_gnutls_aead_cipher_and_algorithm_t |
| 4651 |
c_aead) |
| 4652 |
{ |
| 4653 |
gnutls_aead_cipher_deinit (c_aead->handle); |
| 4654 |
} |
| 4655 |
|
| 4656 |
SCM_DEFINE (scm_gnutls_aead_cipher_encrypt, "aead-cipher-encrypt", 5, 0, 0, |
| 4657 |
(SCM handle, SCM nonce, SCM auth, SCM tagsize, SCM data), |
| 4658 |
"Encrypt the @var{data}, with additional @var{auth} data. " |
| 4659 |
"Use 0 for @var{tagsize} to use the default tag size " |
| 4660 |
"for the algorithm.") |
| 4661 |
#define FUNC_NAME s_scm_gnutls_aead_cipher_encrypt |
| 4662 |
{ |
| 4663 |
int error; |
| 4664 |
scm_gnutls_aead_cipher_and_algorithm_t c_aead = |
| 4665 |
scm_to_gnutls_aead_cipher (handle, 1, FUNC_NAME); |
| 4666 |
size_t c_nonce_len = scm_c_bytevector_length (nonce); |
| 4667 |
const void *c_nonce = SCM_BYTEVECTOR_CONTENTS (nonce); |
| 4668 |
size_t c_auth_len = scm_c_bytevector_length (auth); |
| 4669 |
const void *c_auth = SCM_BYTEVECTOR_CONTENTS (auth); |
| 4670 |
size_t c_tag_size = scm_to_size_t (tagsize); |
| 4671 |
if (c_tag_size == 0) |
| 4672 |
{ |
| 4673 |
c_tag_size = gnutls_cipher_get_tag_size (c_aead->algorithm); |
| 4674 |
} |
| 4675 |
size_t c_data_len = scm_c_bytevector_length (data); |
| 4676 |
const void *c_data = SCM_BYTEVECTOR_CONTENTS (data); |
| 4677 |
size_t output_size = c_data_len + c_tag_size; |
| 4678 |
size_t used_size = output_size; |
| 4679 |
SCM ret = scm_c_make_bytevector (output_size); |
| 4680 |
error = |
| 4681 |
gnutls_aead_cipher_encrypt (c_aead->handle, c_nonce, c_nonce_len, |
| 4682 |
c_auth, c_auth_len, c_tag_size, c_data, |
| 4683 |
c_data_len, SCM_BYTEVECTOR_CONTENTS (ret), |
| 4684 |
&used_size); |
| 4685 |
if (EXPECT_FALSE (error)) |
| 4686 |
{ |
| 4687 |
scm_gnutls_error (error, FUNC_NAME); |
| 4688 |
} |
| 4689 |
assert (used_size == output_size); |
| 4690 |
return ret; |
| 4691 |
} |
| 4692 |
|
| 4693 |
#undef FUNC_NAME |
| 4694 |
|
| 4695 |
SCM_DEFINE (scm_gnutls_aead_cipher_decrypt, "aead-cipher-decrypt", 5, 0, 0, |
| 4696 |
(SCM handle, SCM nonce, SCM auth, SCM tagsize, SCM data), |
| 4697 |
"Decrypt the @var{data}, checking that the authentication " |
| 4698 |
"data @var{auth} is correct. Pass 0 as @var{tagsize} " |
| 4699 |
"to use the default tag size for the underlying algorithm.") |
| 4700 |
#define FUNC_NAME s_scm_gnutls_aead_cipher_decrypt |
| 4701 |
{ |
| 4702 |
int error; |
| 4703 |
scm_gnutls_aead_cipher_and_algorithm_t c_aead = |
| 4704 |
scm_to_gnutls_aead_cipher (handle, 1, FUNC_NAME); |
| 4705 |
size_t c_nonce_len = scm_c_bytevector_length (nonce); |
| 4706 |
const void *c_nonce = SCM_BYTEVECTOR_CONTENTS (nonce); |
| 4707 |
size_t c_auth_len = scm_c_bytevector_length (auth); |
| 4708 |
const void *c_auth = SCM_BYTEVECTOR_CONTENTS (auth); |
| 4709 |
size_t c_tag_size = scm_to_size_t (tagsize); |
| 4710 |
if (c_tag_size == 0) |
| 4711 |
{ |
| 4712 |
c_tag_size = gnutls_cipher_get_tag_size (c_aead->algorithm); |
| 4713 |
} |
| 4714 |
size_t c_data_len = scm_c_bytevector_length (data); |
| 4715 |
const void *c_data = SCM_BYTEVECTOR_CONTENTS (data); |
| 4716 |
size_t output_size = c_data_len; |
| 4717 |
size_t used_size = output_size; |
| 4718 |
/* The gnutls manual reads: ptext_len: the length of decrypted data |
| 4719 |
(initially must hold the maximum available size). I interpret it as: it |
| 4720 |
must be large enough to fit all the data, including the tag, because |
| 4721 |
that’s how much memory the function needs to decrypt the data. We need to |
| 4722 |
remove the space for the tag though. */ |
| 4723 |
SCM work = scm_c_make_bytevector (output_size); |
| 4724 |
error = |
| 4725 |
gnutls_aead_cipher_decrypt (c_aead->handle, c_nonce, c_nonce_len, |
| 4726 |
c_auth, c_auth_len, c_tag_size, c_data, |
| 4727 |
c_data_len, SCM_BYTEVECTOR_CONTENTS (work), |
| 4728 |
&used_size); |
| 4729 |
if (EXPECT_FALSE (error)) |
| 4730 |
{ |
| 4731 |
scm_gnutls_error (error, FUNC_NAME); |
| 4732 |
} |
| 4733 |
SCM ret = scm_c_make_bytevector (used_size); |
| 4734 |
scm_bytevector_copy_x (work, scm_from_uint (0), ret, scm_from_uint (0), |
| 4735 |
scm_from_size_t (used_size)); |
| 4736 |
return ret; |
| 4737 |
} |
| 4738 |
|
| 4739 |
#undef FUNC_NAME |
| 4740 |
|
| 4741 |
SCM_DEFINE (scm_gnutls_aead_cipher_algorithm, "aead-cipher-algorithm", 1, 0, |
| 4742 |
0, (SCM handle), "Return the underlying AEAD cipher algorithm.") |
| 4743 |
#define FUNC_NAME s_scm_gnutls_aead_cipher_algorithm |
| 4744 |
{ |
| 4745 |
scm_gnutls_aead_cipher_and_algorithm_t c_aead = |
| 4746 |
scm_to_gnutls_aead_cipher (handle, 1, FUNC_NAME); |
| 4747 |
return scm_from_gnutls_cipher (c_aead->algorithm); |
| 4748 |
} |
| 4749 |
|
| 4750 |
#undef FUNC_NAME |
| 4751 |
|
| 4752 |
SCM_DEFINE (scm_gnutls_make_cipher, "make-cipher", 3, 0, 0, |
| 4753 |
(SCM algorithm, SCM key, SCM iv), |
| 4754 |
"Return a new cipher context, using the cipher @var{algorithm}.") |
| 4755 |
#define FUNC_NAME s_scm_gnutls_make_cipher |
| 4756 |
{ |
| 4757 |
int error; |
| 4758 |
scm_gnutls_cipher_and_algorithm_t c_cipher = |
| 4759 |
scm_gc_malloc (sizeof (struct scm_gnutls_cipher_and_algorithm), |
| 4760 |
"cipher-and-algorithm"); |
| 4761 |
gnutls_cipher_algorithm_t c_algorithm; |
| 4762 |
size_t c_key_length = scm_c_bytevector_length (key); |
| 4763 |
const void *c_key = SCM_BYTEVECTOR_CONTENTS (key); |
| 4764 |
gnutls_datum_t datum_key; |
| 4765 |
datum_key.data = (unsigned char *) c_key; |
| 4766 |
datum_key.size = c_key_length; |
| 4767 |
gnutls_datum_t c_iv; |
| 4768 |
c_iv.size = scm_c_bytevector_length (iv); |
| 4769 |
c_iv.data = (unsigned char *) SCM_BYTEVECTOR_CONTENTS (iv); |
| 4770 |
c_algorithm = scm_to_gnutls_cipher (algorithm, 1, FUNC_NAME); |
| 4771 |
c_cipher->algorithm = c_algorithm; |
| 4772 |
error = |
| 4773 |
gnutls_cipher_init (&(c_cipher->handle), c_algorithm, &datum_key, &c_iv); |
| 4774 |
if (EXPECT_FALSE (error)) |
| 4775 |
{ |
| 4776 |
scm_gnutls_error (error, FUNC_NAME); |
| 4777 |
} |
| 4778 |
return scm_from_gnutls_cipher_hd (c_cipher); |
| 4779 |
} |
| 4780 |
|
| 4781 |
#undef FUNC_NAME |
| 4782 |
|
| 4783 |
static void |
| 4784 |
cipher_and_algorithm_deinit (scm_gnutls_cipher_and_algorithm_t c_cipher) |
| 4785 |
{ |
| 4786 |
gnutls_cipher_deinit (c_cipher->handle); |
| 4787 |
} |
| 4788 |
|
| 4789 |
SCM_DEFINE (scm_gnutls_cipher_encrypt, "cipher-encrypt", 2, 0, 0, |
| 4790 |
(SCM handle, SCM data), "Encrypt the @var{data}.") |
| 4791 |
#define FUNC_NAME s_scm_gnutls_cipher_encrypt |
| 4792 |
{ |
| 4793 |
int error; |
| 4794 |
scm_gnutls_cipher_and_algorithm_t c_cipher = |
| 4795 |
scm_to_gnutls_cipher_hd (handle, 1, FUNC_NAME); |
| 4796 |
size_t c_data_len = scm_c_bytevector_length (data); |
| 4797 |
const void *c_data = SCM_BYTEVECTOR_CONTENTS (data); |
| 4798 |
SCM ret = scm_c_make_bytevector (c_data_len); |
| 4799 |
error = |
| 4800 |
gnutls_cipher_encrypt2 (c_cipher->handle, c_data, c_data_len, |
| 4801 |
SCM_BYTEVECTOR_CONTENTS (ret), c_data_len); |
| 4802 |
if (EXPECT_FALSE (error)) |
| 4803 |
{ |
| 4804 |
scm_gnutls_error (error, FUNC_NAME); |
| 4805 |
} |
| 4806 |
return ret; |
| 4807 |
} |
| 4808 |
|
| 4809 |
#undef FUNC_NAME |
| 4810 |
|
| 4811 |
SCM_DEFINE (scm_gnutls_cipher_decrypt, "cipher-decrypt", 2, 0, 0, |
| 4812 |
(SCM handle, SCM data), "Decrypt the @var{data}.") |
| 4813 |
#define FUNC_NAME s_scm_gnutls_cipher_decrypt |
| 4814 |
{ |
| 4815 |
int error; |
| 4816 |
scm_gnutls_cipher_and_algorithm_t c_cipher = |
| 4817 |
scm_to_gnutls_cipher_hd (handle, 1, FUNC_NAME); |
| 4818 |
size_t c_data_len = scm_c_bytevector_length (data); |
| 4819 |
const void *c_data = SCM_BYTEVECTOR_CONTENTS (data); |
| 4820 |
SCM ret = scm_c_make_bytevector (c_data_len); |
| 4821 |
error = |
| 4822 |
gnutls_cipher_decrypt2 (c_cipher->handle, c_data, |
| 4823 |
c_data_len, SCM_BYTEVECTOR_CONTENTS (ret), |
| 4824 |
c_data_len); |
| 4825 |
if (EXPECT_FALSE (error)) |
| 4826 |
{ |
| 4827 |
scm_gnutls_error (error, FUNC_NAME); |
| 4828 |
} |
| 4829 |
return ret; |
| 4830 |
} |
| 4831 |
|
| 4832 |
#undef FUNC_NAME |
| 4833 |
|
| 4834 |
SCM_DEFINE (scm_gnutls_cipher_set_iv, "cipher-set-iv!", 2, 0, 0, |
| 4835 |
(SCM handle, SCM data), "Set the IV @var{data}.") |
| 4836 |
#define FUNC_NAME s_scm_gnutls_cipher_set_iv |
| 4837 |
{ |
| 4838 |
scm_gnutls_cipher_and_algorithm_t c_cipher = |
| 4839 |
scm_to_gnutls_cipher_hd (handle, 1, FUNC_NAME); |
| 4840 |
size_t c_data_len = scm_c_bytevector_length (data); |
| 4841 |
void *c_data = SCM_BYTEVECTOR_CONTENTS (data); |
| 4842 |
gnutls_cipher_set_iv (c_cipher->handle, c_data, c_data_len); |
| 4843 |
return SCM_UNSPECIFIED; |
| 4844 |
} |
| 4845 |
|
| 4846 |
#undef FUNC_NAME |
| 4847 |
|
| 4848 |
SCM_DEFINE (scm_gnutls_cipher_add_auth, "cipher-add-auth!", 2, 0, 0, |
| 4849 |
(SCM handle, SCM data), "Add authentication @var{data}.") |
| 4850 |
#define FUNC_NAME s_scm_gnutls_cipher_add_auth |
| 4851 |
{ |
| 4852 |
int error; |
| 4853 |
scm_gnutls_cipher_and_algorithm_t c_cipher = |
| 4854 |
scm_to_gnutls_cipher_hd (handle, 1, FUNC_NAME); |
| 4855 |
size_t c_data_len = scm_c_bytevector_length (data); |
| 4856 |
const void *c_data = SCM_BYTEVECTOR_CONTENTS (data); |
| 4857 |
error = gnutls_cipher_add_auth (c_cipher->handle, c_data, c_data_len); |
| 4858 |
if (EXPECT_FALSE (error)) |
| 4859 |
{ |
| 4860 |
scm_gnutls_error (error, FUNC_NAME); |
| 4861 |
} |
| 4862 |
return SCM_UNSPECIFIED; |
| 4863 |
} |
| 4864 |
|
| 4865 |
#undef FUNC_NAME |
| 4866 |
|
| 4867 |
SCM_DEFINE (scm_gnutls_cipher_tag, "cipher-tag", 2, 0, 0, |
| 4868 |
(SCM handle, SCM tagsize), "Read a tag.") |
| 4869 |
#define FUNC_NAME s_scm_gnutls_cipher_tag |
| 4870 |
{ |
| 4871 |
int error; |
| 4872 |
scm_gnutls_cipher_and_algorithm_t c_cipher = |
| 4873 |
scm_to_gnutls_cipher_hd (handle, 1, FUNC_NAME); |
| 4874 |
size_t c_tag_size = scm_to_size_t (tagsize); |
| 4875 |
SCM ret = scm_c_make_bytevector (c_tag_size); |
| 4876 |
error = |
| 4877 |
gnutls_cipher_tag (c_cipher->handle, SCM_BYTEVECTOR_CONTENTS (ret), |
| 4878 |
c_tag_size); |
| 4879 |
if (EXPECT_FALSE (error)) |
| 4880 |
{ |
| 4881 |
scm_gnutls_error (error, FUNC_NAME); |
| 4882 |
} |
| 4883 |
return ret; |
| 4884 |
} |
| 4885 |
|
| 4886 |
#undef FUNC_NAME |
| 4887 |
|
| 4888 |
SCM_DEFINE (scm_gnutls_cipher_algorithm, "cipher-algorithm", 1, 0, 0, |
| 4889 |
(SCM handle), "Return the underlying cipher algorithm.") |
| 4890 |
#define FUNC_NAME s_scm_gnutls_cipher_algorithm |
| 4891 |
{ |
| 4892 |
scm_gnutls_cipher_and_algorithm_t c_cipher = |
| 4893 |
scm_to_gnutls_cipher_hd (handle, 1, FUNC_NAME); |
| 4894 |
return scm_from_gnutls_cipher (c_cipher->algorithm); |
| 4895 |
} |
| 4896 |
|
| 4897 |
#undef FUNC_NAME |
| 4898 |
|
| 4899 |
SCM_DEFINE (scm_string_to_pk_algorithm, "string->pk-algorithm", 1, 0, 0, |
| 4900 |
(SCM id), |
| 4901 |
"Return the public key algorithm identified by @var{id}.") |
| 4902 |
#define FUNC_NAME s_scm_string_to_pk_algorithm |
| 4903 |
{ |
| 4904 |
scm_dynwind_begin (0); |
| 4905 |
char *c_id = scm_to_latin1_stringn (id, NULL); |
| 4906 |
scm_dynwind_free (c_id); |
| 4907 |
gnutls_pk_algorithm_t c_ret = gnutls_pk_get_id (c_id); |
| 4908 |
scm_dynwind_end (); |
| 4909 |
scm_remember_upto_here_1 (c_ret); |
| 4910 |
return scm_from_gnutls_pk_algorithm (c_ret); |
| 4911 |
} |
| 4912 |
|
| 4913 |
#undef FUNC_NAME |
| 4914 |
|
| 4915 |
SCM_DEFINE (scm_string_to_sign_algorithm, "string->sign-algorithm", 1, 0, 0, |
| 4916 |
(SCM id), |
| 4917 |
"Return the signature algorithm identified by @var{id}.") |
| 4918 |
#define FUNC_NAME s_scm_string_to_sign_algorithm |
| 4919 |
{ |
| 4920 |
scm_dynwind_begin (0); |
| 4921 |
char *c_id = scm_to_latin1_stringn (id, NULL); |
| 4922 |
scm_dynwind_free (c_id); |
| 4923 |
gnutls_sign_algorithm_t c_ret = gnutls_sign_get_id (c_id); |
| 4924 |
scm_dynwind_end (); |
| 4925 |
scm_remember_upto_here_1 (c_ret); |
| 4926 |
return scm_from_gnutls_sign_algorithm (c_ret); |
| 4927 |
} |
| 4928 |
|
| 4929 |
#undef FUNC_NAME |
| 4930 |
|
| 4931 |
SCM_DEFINE (scm_string_to_ecc_curve, "string->ecc-curve", 1, 0, 0, |
| 4932 |
(SCM id), "Return ECC curve identified by @var{id}.") |
| 4933 |
#define FUNC_NAME s_scm_string_to_ecc_curve |
| 4934 |
{ |
| 4935 |
scm_dynwind_begin (0); |
| 4936 |
char *c_id = scm_to_latin1_stringn (id, NULL); |
| 4937 |
scm_dynwind_free (c_id); |
| 4938 |
gnutls_ecc_curve_t c_ret = gnutls_ecc_curve_get_id (c_id); |
| 4939 |
scm_dynwind_end (); |
| 4940 |
scm_remember_upto_here_1 (c_ret); |
| 4941 |
return scm_from_gnutls_ecc_curve (c_ret); |
| 4942 |
} |
| 4943 |
|
| 4944 |
#undef FUNC_NAME |
| 4945 |
|
| 4946 |
SCM_DEFINE (scm_pk_algorithm_to_oid, "pk-algorithm->oid", 1, 0, 0, |
| 4947 |
(SCM algorithm), "Return the OID associated to @var{algorithm}.") |
| 4948 |
#define FUNC_NAME s_scm_pk_algorithm_to_oid |
| 4949 |
{ |
| 4950 |
gnutls_pk_algorithm_t c_algorithm = |
| 4951 |
scm_to_gnutls_pk_algorithm (algorithm, 1, FUNC_NAME); |
| 4952 |
const char *oid = gnutls_pk_get_oid (c_algorithm); |
| 4953 |
if (EXPECT_FALSE (!oid)) |
| 4954 |
{ |
| 4955 |
return SCM_BOOL_F; |
| 4956 |
} |
| 4957 |
return scm_from_latin1_string (oid); |
| 4958 |
} |
| 4959 |
|
| 4960 |
#undef FUNC_NAME |
| 4961 |
|
| 4962 |
SCM_DEFINE (scm_sign_algorithm_to_oid, "sign-algorithm->oid", 1, 0, 0, |
| 4963 |
(SCM algo), "Return the OID allocated to @var{algo}.") |
| 4964 |
#define FUNC_NAME s_scm_sign_algorithm_to_oid |
| 4965 |
{ |
| 4966 |
gnutls_sign_algorithm_t c_algo = |
| 4967 |
scm_to_gnutls_sign_algorithm (algo, 1, FUNC_NAME); |
| 4968 |
const char *c_oid = gnutls_sign_get_oid (c_algo); |
| 4969 |
if (EXPECT_FALSE (c_oid == NULL)) |
| 4970 |
{ |
| 4971 |
return SCM_BOOL_F; |
| 4972 |
} |
| 4973 |
return scm_from_latin1_string (c_oid); |
| 4974 |
} |
| 4975 |
|
| 4976 |
#undef FUNC_NAME |
| 4977 |
|
| 4978 |
SCM_DEFINE (scm_ecc_curve_to_oid, "ecc-curve->oid", 1, 0, 0, |
| 4979 |
(SCM curve), "Return the OID allocated to @var{curve}.") |
| 4980 |
#define FUNC_NAME s_scm_ecc_curve_to_oid |
| 4981 |
{ |
| 4982 |
gnutls_ecc_curve_t c_curve = scm_to_gnutls_ecc_curve (curve, 1, FUNC_NAME); |
| 4983 |
const char *c_oid = gnutls_ecc_curve_get_oid (c_curve); |
| 4984 |
if (EXPECT_FALSE (c_oid == NULL)) |
| 4985 |
{ |
| 4986 |
return SCM_BOOL_F; |
| 4987 |
} |
| 4988 |
return scm_from_latin1_string (c_oid); |
| 4989 |
} |
| 4990 |
|
| 4991 |
#undef FUNC_NAME |
| 4992 |
|
| 4993 |
SCM_DEFINE (scm_pk_algorithm_list, "pk-algorithm-list", 0, 0, 0, |
| 4994 |
(), "Return the list of public key algorithms. " |
| 4995 |
"@strong{This function is not thread-safe.}") |
| 4996 |
#define FUNC_NAME s_scm_pk_algorithm_list |
| 4997 |
{ |
| 4998 |
const gnutls_pk_algorithm_t *c_ret = gnutls_pk_list (); |
| 4999 |
SCM ret = SCM_EOL; |
| 5000 |
size_t i; |
| 5001 |
for (i = 0; c_ret[i] != 0; i++) |
| 5002 |
; |
| 5003 |
for (; i-- > 0;) |
| 5004 |
{ |
| 5005 |
ret = scm_cons (scm_from_gnutls_pk_algorithm (c_ret[i]), ret); |
| 5006 |
} |
| 5007 |
return ret; |
| 5008 |
} |
| 5009 |
|
| 5010 |
#undef FUNC_NAME |
| 5011 |
|
| 5012 |
SCM_DEFINE (scm_sign_algorithm_list, "sign-algorithm-list", 0, 0, 0, |
| 5013 |
(), "Return the list of public key algorithms. " |
| 5014 |
"@strong{This function is not thread-safe.}") |
| 5015 |
#define FUNC_NAME s_scm_sign_algorithm_list |
| 5016 |
{ |
| 5017 |
const gnutls_sign_algorithm_t *c_ret = gnutls_sign_list (); |
| 5018 |
SCM ret = SCM_EOL; |
| 5019 |
size_t i; |
| 5020 |
for (i = 0; c_ret[i] != 0; i++) |
| 5021 |
; |
| 5022 |
for (; i-- > 0;) |
| 5023 |
{ |
| 5024 |
ret = scm_cons (scm_from_gnutls_sign_algorithm (c_ret[i]), ret); |
| 5025 |
} |
| 5026 |
return ret; |
| 5027 |
} |
| 5028 |
|
| 5029 |
#undef FUNC_NAME |
| 5030 |
|
| 5031 |
SCM_DEFINE (scm_ecc_curve_list, "ecc-curve-list", 0, 0, 0, |
| 5032 |
(), "Return the list of ECC curves. " |
| 5033 |
"@strong{This function is not thread-safe.}") |
| 5034 |
#define FUNC_NAME s_scm_ecc_curve_list |
| 5035 |
{ |
| 5036 |
const gnutls_ecc_curve_t *c_ret = gnutls_ecc_curve_list (); |
| 5037 |
SCM ret = SCM_EOL; |
| 5038 |
size_t i; |
| 5039 |
for (i = 0; c_ret[i] != 0; i++) |
| 5040 |
; |
| 5041 |
for (; i-- > 0;) |
| 5042 |
{ |
| 5043 |
ret = scm_cons (scm_from_gnutls_ecc_curve (c_ret[i]), ret); |
| 5044 |
} |
| 5045 |
return ret; |
| 5046 |
} |
| 5047 |
|
| 5048 |
#undef FUNC_NAME |
| 5049 |
|
| 5050 |
SCM_DEFINE (scm_pk_algorithm_to_sign_algorithm, |
| 5051 |
"pk-algorithm->sign-algorithm", 2, 0, 0, (SCM pk, SCM digest), |
| 5052 |
"Return the signature algorithm compatible with " |
| 5053 |
"the @var{pk} public-key algorithm and " |
| 5054 |
"the @var{digest} algorithm.") |
| 5055 |
#define FUNC_NAME s_scm_pk_algorithm_to_sign_algorithm |
| 5056 |
{ |
| 5057 |
gnutls_pk_algorithm_t c_pk = scm_to_gnutls_pk_algorithm (pk, 1, FUNC_NAME); |
| 5058 |
gnutls_digest_algorithm_t c_digest = |
| 5059 |
scm_to_gnutls_digest (digest, 2, FUNC_NAME); |
| 5060 |
gnutls_sign_algorithm_t c_sign = gnutls_pk_to_sign (c_pk, c_digest); |
| 5061 |
SCM ret = scm_from_gnutls_sign_algorithm (c_sign); |
| 5062 |
return ret; |
| 5063 |
} |
| 5064 |
|
| 5065 |
#undef FUNC_NAME |
| 5066 |
|
| 5067 |
SCM_DEFINE (scm_sign_algorithm_to_digest_algorithm, |
| 5068 |
"sign-algorithm->digest-algorithm", 1, 0, 0, (SCM sign), |
| 5069 |
"Return the digest algorithm used " |
| 5070 |
"for the @var{sign} algorithm.") |
| 5071 |
#define FUNC_NAME s_scm_sign_algorithm_to_digest_algorithm |
| 5072 |
{ |
| 5073 |
gnutls_sign_algorithm_t c_sign = |
| 5074 |
scm_to_gnutls_sign_algorithm (sign, 1, FUNC_NAME); |
| 5075 |
gnutls_digest_algorithm_t c_dig = gnutls_sign_get_hash_algorithm (c_sign); |
| 5076 |
SCM ret = scm_from_gnutls_digest (c_dig); |
| 5077 |
return ret; |
| 5078 |
} |
| 5079 |
|
| 5080 |
#undef FUNC_NAME |
| 5081 |
|
| 5082 |
SCM_DEFINE (scm_oid_to_pk_algorithm, "oid->pk-algorithm", 1, 0, 0, |
| 5083 |
(SCM oid), |
| 5084 |
"Return the public key algorithm identified by @var{oid}.") |
| 5085 |
#define FUNC_NAME s_scm_oid_to_pk_algorithm |
| 5086 |
{ |
| 5087 |
scm_dynwind_begin (0); |
| 5088 |
char *c_oid = scm_to_latin1_stringn (oid, NULL); |
| 5089 |
scm_dynwind_free (c_oid); |
| 5090 |
gnutls_pk_algorithm_t c_ret = gnutls_oid_to_pk (c_oid); |
| 5091 |
scm_dynwind_end (); |
| 5092 |
scm_remember_upto_here_1 (c_ret); |
| 5093 |
return scm_from_gnutls_pk_algorithm (c_ret); |
| 5094 |
} |
| 5095 |
|
| 5096 |
#undef FUNC_NAME |
| 5097 |
|
| 5098 |
SCM_DEFINE (scm_oid_to_sign_algorithm, "oid->sign-algorithm", 1, 0, 0, |
| 5099 |
(SCM oid), "Return the sign algorithm identified by @var{oid}.") |
| 5100 |
#define FUNC_NAME s_scm_oid_to_sign_algorithm |
| 5101 |
{ |
| 5102 |
scm_dynwind_begin (0); |
| 5103 |
char *c_oid = scm_to_latin1_stringn (oid, NULL); |
| 5104 |
scm_dynwind_free (c_oid); |
| 5105 |
gnutls_sign_algorithm_t c_ret = gnutls_oid_to_sign (c_oid); |
| 5106 |
scm_dynwind_end (); |
| 5107 |
scm_remember_upto_here_1 (c_ret); |
| 5108 |
return scm_from_gnutls_sign_algorithm (c_ret); |
| 5109 |
} |
| 5110 |
|
| 5111 |
#undef FUNC_NAME |
| 5112 |
|
| 5113 |
SCM_DEFINE (scm_oid_to_ecc_curve, "oid->ecc-curve", 1, 0, 0, |
| 5114 |
(SCM oid), "Return the ECC curve identified by @var{oid}.") |
| 5115 |
#define FUNC_NAME s_scm_oid_to_ecc_curve |
| 5116 |
{ |
| 5117 |
scm_dynwind_begin (0); |
| 5118 |
char *c_oid = scm_to_latin1_stringn (oid, NULL); |
| 5119 |
scm_dynwind_free (c_oid); |
| 5120 |
gnutls_ecc_curve_t c_ret = gnutls_oid_to_ecc_curve (c_oid); |
| 5121 |
scm_dynwind_end (); |
| 5122 |
scm_remember_upto_here_1 (c_ret); |
| 5123 |
return scm_from_gnutls_ecc_curve (c_ret); |
| 5124 |
} |
| 5125 |
|
| 5126 |
#undef FUNC_NAME |
| 5127 |
|
| 5128 |
SCM_DEFINE (scm_sign_algorithm_to_pk_algorithm, |
| 5129 |
"sign-algorithm->pk-algorithm", 1, 0, 0, (SCM sign), |
| 5130 |
"Return a public key algorithm that can sign " |
| 5131 |
"data with the @var{sign} algorithm.") |
| 5132 |
#define FUNC_NAME s_scm_sign_algorithm_to_pk_algorithm |
| 5133 |
{ |
| 5134 |
gnutls_sign_algorithm_t c_sign = |
| 5135 |
scm_to_gnutls_sign_algorithm (sign, 1, FUNC_NAME); |
| 5136 |
gnutls_pk_algorithm_t c_pk = gnutls_sign_get_pk_algorithm (c_sign); |
| 5137 |
return scm_from_gnutls_pk_algorithm (c_pk); |
| 5138 |
} |
| 5139 |
|
| 5140 |
#undef FUNC_NAME |
| 5141 |
|
| 5142 |
SCM_DEFINE (scm_ecc_curve_to_pk_algorithm, |
| 5143 |
"ecc-curve->pk-algorithm", 1, 0, 0, (SCM curve), |
| 5144 |
"Return the public key algorithm that can be used " |
| 5145 |
"with @var{curve}.") |
| 5146 |
#define FUNC_NAME s_scm_ecc_curve_to_pk_algorithm |
| 5147 |
{ |
| 5148 |
gnutls_ecc_curve_t c_curve = scm_to_gnutls_ecc_curve (curve, 1, FUNC_NAME); |
| 5149 |
gnutls_pk_algorithm_t c_pk = gnutls_ecc_curve_get_pk (c_curve); |
| 5150 |
return scm_from_gnutls_pk_algorithm (c_pk); |
| 5151 |
} |
| 5152 |
|
| 5153 |
#undef FUNC_NAME |
| 5154 |
|
| 5155 |
SCM_DEFINE (scm_sign_algorithm_supports_p, "sign-algorithm-supports?", 2, 0, |
| 5156 |
0, (SCM sign, SCM pk), |
| 5157 |
"Check whether the @var{sign} algorithm can be used " |
| 5158 |
"with the @var{pk} public-key algorithm.") |
| 5159 |
#define FUNC_NAME s_scm_sign_algorithm_supports_p |
| 5160 |
{ |
| 5161 |
gnutls_sign_algorithm_t c_sign = |
| 5162 |
scm_to_gnutls_sign_algorithm (sign, 1, FUNC_NAME); |
| 5163 |
gnutls_pk_algorithm_t c_pk = scm_to_gnutls_pk_algorithm (pk, 2, FUNC_NAME); |
| 5164 |
if (gnutls_sign_supports_pk_algorithm (c_sign, c_pk) != 0) |
| 5165 |
{ |
| 5166 |
return SCM_BOOL_T; |
| 5167 |
} |
| 5168 |
return SCM_BOOL_F; |
| 5169 |
} |
| 5170 |
|
| 5171 |
#undef FUNC_NAME |
| 5172 |
|
| 5173 |
SCM_DEFINE (scm_sign_algorithm_is_secure_p, "sign-algorithm-is-secure?", 2, 0, |
| 5174 |
0, (SCM sign, SCM for_certs), |
| 5175 |
"Check whether the @var{sign} algorithm is considered safe. " |
| 5176 |
"@var{for-certs?} is @code{#t} if the security is for signing " |
| 5177 |
"a certificate, or @code{#f} for other data.") |
| 5178 |
#define FUNC_NAME s_scm_sign_algorithm_is_secure_p |
| 5179 |
{ |
| 5180 |
gnutls_sign_algorithm_t c_sign = |
| 5181 |
scm_to_gnutls_sign_algorithm (sign, 1, FUNC_NAME); |
| 5182 |
unsigned int c_flags = 0; |
| 5183 |
if (scm_is_true (for_certs)) |
| 5184 |
{ |
| 5185 |
c_flags = GNUTLS_SIGN_FLAG_SECURE_FOR_CERTS; |
| 5186 |
} |
| 5187 |
if (gnutls_sign_is_secure2 (c_sign, c_flags) != 0) |
| 5188 |
{ |
| 5189 |
return SCM_BOOL_T; |
| 5190 |
} |
| 5191 |
return SCM_BOOL_F; |
| 5192 |
} |
| 5193 |
|
| 5194 |
#undef FUNC_NAME |
| 5195 |
|
| 5196 |
SCM_DEFINE (scm_ecc_curve_size, "ecc-curve-size", 1, 0, 0, |
| 5197 |
(SCM curve), |
| 5198 |
"Return the size of @var{curve}, in bytes (0 on failure).") |
| 5199 |
#define FUNC_NAME s_scm_ecc_curve_size |
| 5200 |
{ |
| 5201 |
gnutls_ecc_curve_t c_curve = scm_to_gnutls_ecc_curve (curve, 1, FUNC_NAME); |
| 5202 |
return scm_from_int (gnutls_ecc_curve_get_size (c_curve)); |
| 5203 |
} |
| 5204 |
|
| 5205 |
#undef FUNC_NAME |
| 5206 |
|
| 5207 |
SCM_DEFINE (scm_hex_encode, "hex-encode", 1, 0, 0, |
| 5208 |
(SCM data), |
| 5209 |
"Return as an ASCII string the base16 encoding of @var{data}.") |
| 5210 |
#define FUNC_NAME s_scm_hex_encode |
| 5211 |
{ |
| 5212 |
if (scm_is_string (data)) |
| 5213 |
{ |
| 5214 |
data = scm_string_to_utf8 (data); |
| 5215 |
} |
| 5216 |
gnutls_datum_t c_data_d; |
| 5217 |
const char *c_data; |
| 5218 |
size_t c_data_len; |
| 5219 |
scm_t_array_handle c_data_handle; |
| 5220 |
c_data = scm_gnutls_get_array (data, &c_data_handle, &c_data_len, |
| 5221 |
FUNC_NAME); |
| 5222 |
c_data_d.data = (unsigned char *) c_data; |
| 5223 |
c_data_d.size = c_data_len; |
| 5224 |
gnutls_datum_t c_result; |
| 5225 |
SCM result; |
| 5226 |
int err = gnutls_hex_encode2 (&c_data_d, &c_result); |
| 5227 |
scm_gnutls_release_array (&c_data_handle); |
| 5228 |
scm_dynwind_begin (0); |
| 5229 |
scm_dynwind_unwind_handler (gnutls_free, c_result.data, |
| 5230 |
SCM_F_WIND_EXPLICITLY); |
| 5231 |
if (EXPECT_FALSE (err)) |
| 5232 |
{ |
| 5233 |
scm_gnutls_error (err, FUNC_NAME); |
| 5234 |
} |
| 5235 |
else |
| 5236 |
{ |
| 5237 |
result = |
| 5238 |
scm_from_latin1_stringn ((char *) c_result.data, c_result.size); |
| 5239 |
} |
| 5240 |
scm_dynwind_end (); |
| 5241 |
return result; |
| 5242 |
} |
| 5243 |
|
| 5244 |
#undef FUNC_NAME |
| 5245 |
|
| 5246 |
SCM_DEFINE (scm_base64_encode, "base64-encode", 1, 0, 0, |
| 5247 |
(SCM data), |
| 5248 |
"Return as an ASCII string the base64 encoding of @var{data}.") |
| 5249 |
#define FUNC_NAME s_scm_base64_encode |
| 5250 |
{ |
| 5251 |
if (scm_is_string (data)) |
| 5252 |
{ |
| 5253 |
data = scm_string_to_utf8 (data); |
| 5254 |
} |
| 5255 |
gnutls_datum_t c_data_d; |
| 5256 |
const char *c_data; |
| 5257 |
size_t c_data_len; |
| 5258 |
scm_t_array_handle c_data_handle; |
| 5259 |
c_data = scm_gnutls_get_array (data, &c_data_handle, &c_data_len, |
| 5260 |
FUNC_NAME); |
| 5261 |
c_data_d.data = (unsigned char *) c_data; |
| 5262 |
c_data_d.size = c_data_len; |
| 5263 |
gnutls_datum_t c_result; |
| 5264 |
SCM result; |
| 5265 |
int err = gnutls_base64_encode2 (&c_data_d, &c_result); |
| 5266 |
scm_gnutls_release_array (&c_data_handle); |
| 5267 |
scm_dynwind_begin (0); |
| 5268 |
scm_dynwind_unwind_handler (gnutls_free, c_result.data, |
| 5269 |
SCM_F_WIND_EXPLICITLY); |
| 5270 |
if (EXPECT_FALSE (err)) |
| 5271 |
{ |
| 5272 |
scm_gnutls_error (err, FUNC_NAME); |
| 5273 |
} |
| 5274 |
else |
| 5275 |
{ |
| 5276 |
result = |
| 5277 |
scm_from_latin1_stringn ((char *) c_result.data, c_result.size); |
| 5278 |
} |
| 5279 |
scm_dynwind_end (); |
| 5280 |
return result; |
| 5281 |
} |
| 5282 |
|
| 5283 |
#undef FUNC_NAME |
| 5284 |
|
| 5285 |
SCM_DEFINE (scm_base64_decode, "base64-decode", 1, 0, 0, |
| 5286 |
(SCM data), |
| 5287 |
"Try and decode @var{data}, return it as a bytevector.") |
| 5288 |
#define FUNC_NAME s_scm_base64_decode |
| 5289 |
{ |
| 5290 |
if (scm_is_string (data)) |
| 5291 |
{ |
| 5292 |
data = scm_string_to_utf8 (data); |
| 5293 |
} |
| 5294 |
gnutls_datum_t c_data_d; |
| 5295 |
const char *c_data; |
| 5296 |
size_t c_data_len; |
| 5297 |
scm_t_array_handle c_data_handle; |
| 5298 |
c_data = scm_gnutls_get_array (data, &c_data_handle, &c_data_len, |
| 5299 |
FUNC_NAME); |
| 5300 |
c_data_d.data = (unsigned char *) c_data; |
| 5301 |
c_data_d.size = c_data_len; |
| 5302 |
gnutls_datum_t c_result; |
| 5303 |
SCM result; |
| 5304 |
int err = gnutls_base64_decode2 (&c_data_d, &c_result); |
| 5305 |
scm_gnutls_release_array (&c_data_handle); |
| 5306 |
scm_dynwind_begin (0); |
| 5307 |
scm_dynwind_unwind_handler (gnutls_free, c_result.data, |
| 5308 |
SCM_F_WIND_EXPLICITLY); |
| 5309 |
if (EXPECT_FALSE (err)) |
| 5310 |
{ |
| 5311 |
scm_gnutls_error (err, FUNC_NAME); |
| 5312 |
} |
| 5313 |
else |
| 5314 |
{ |
| 5315 |
result = scm_c_make_bytevector (c_result.size); |
| 5316 |
memcpy (SCM_BYTEVECTOR_CONTENTS (result), c_result.data, c_result.size); |
| 5317 |
} |
| 5318 |
scm_dynwind_end (); |
| 5319 |
return result; |
| 5320 |
} |
| 5321 |
|
| 5322 |
#undef FUNC_NAME |
| 5323 |
|
| 5324 |
SCM_DEFINE (scm_hex_decode, "hex-decode", 1, 0, 0, |
| 5325 |
(SCM data), |
| 5326 |
"Try and decode @var{data} from base16, return it as a bytevector.") |
| 5327 |
#define FUNC_NAME s_scm_hex_decode |
| 5328 |
{ |
| 5329 |
if (scm_is_string (data)) |
| 5330 |
{ |
| 5331 |
data = scm_string_to_utf8 (data); |
| 5332 |
} |
| 5333 |
gnutls_datum_t c_data_d; |
| 5334 |
const char *c_data; |
| 5335 |
size_t c_data_len; |
| 5336 |
scm_t_array_handle c_data_handle; |
| 5337 |
c_data = scm_gnutls_get_array (data, &c_data_handle, &c_data_len, |
| 5338 |
FUNC_NAME); |
| 5339 |
c_data_d.data = (unsigned char *) c_data; |
| 5340 |
c_data_d.size = c_data_len; |
| 5341 |
gnutls_datum_t c_result; |
| 5342 |
SCM result; |
| 5343 |
int err = gnutls_hex_decode2 (&c_data_d, &c_result); |
| 5344 |
scm_gnutls_release_array (&c_data_handle); |
| 5345 |
scm_dynwind_begin (0); |
| 5346 |
scm_dynwind_unwind_handler (gnutls_free, c_result.data, |
| 5347 |
SCM_F_WIND_EXPLICITLY); |
| 5348 |
if (EXPECT_FALSE (err)) |
| 5349 |
{ |
| 5350 |
scm_gnutls_error (err, FUNC_NAME); |
| 5351 |
} |
| 5352 |
else |
| 5353 |
{ |
| 5354 |
result = scm_c_make_bytevector (c_result.size); |
| 5355 |
memcpy (SCM_BYTEVECTOR_CONTENTS (result), c_result.data, c_result.size); |
| 5356 |
} |
| 5357 |
scm_dynwind_end (); |
| 5358 |
return result; |
| 5359 |
} |
| 5360 |
|
| 5361 |
#undef FUNC_NAME |
| 5362 |
|
| 5363 |
static inline void |
| 5364 |
do_gnutls_pubkey_deinit (void *key) |
| 5365 |
{ |
| 5366 |
/* The function type is compatible with unwind handler. */ |
| 5367 |
gnutls_pubkey_deinit (key); |
| 5368 |
} |
| 5369 |
|
| 5370 |
static inline void |
| 5371 |
do_gnutls_privkey_deinit (void *key) |
| 5372 |
{ |
| 5373 |
gnutls_privkey_deinit (key); |
| 5374 |
} |
| 5375 |
|
| 5376 |
SCM_DEFINE (scm_import_raw_dsa_public_key, "import-raw-dsa-public-key", 4, 0, |
| 5377 |
0, (SCM p, SCM q, SCM g, SCM y), "Create a new DSA public key.") |
| 5378 |
#define FUNC_NAME s_scm_import_raw_dsa_public_key |
| 5379 |
{ |
| 5380 |
scm_dynwind_begin (0); |
| 5381 |
SCM args[4] = { p, q, g, y }; |
| 5382 |
gnutls_datum_t c_args_d[4]; |
| 5383 |
const char *c_args[4]; |
| 5384 |
size_t c_args_length[4]; |
| 5385 |
scm_t_array_handle c_args_handle[4]; |
| 5386 |
for (size_t i = 0; i < 4; i++) |
| 5387 |
{ |
| 5388 |
c_args[i] = |
| 5389 |
scm_gnutls_get_array (args[i], &(c_args_handle[i]), |
| 5390 |
&(c_args_length[i]), FUNC_NAME); |
| 5391 |
c_args_d[i].data = (unsigned char *) c_args[i]; |
| 5392 |
c_args_d[i].size = c_args_length[i]; |
| 5393 |
scm_dynwind_release_handle (&(c_args_handle[i])); |
| 5394 |
} |
| 5395 |
gnutls_pubkey_t c_key; |
| 5396 |
int error = gnutls_pubkey_init (&c_key); |
| 5397 |
if (EXPECT_FALSE (error)) |
| 5398 |
{ |
| 5399 |
scm_gnutls_error (error, FUNC_NAME); |
| 5400 |
} |
| 5401 |
else |
| 5402 |
{ |
| 5403 |
scm_dynwind_unwind_handler (do_gnutls_pubkey_deinit, c_key, 0); |
| 5404 |
error = |
| 5405 |
gnutls_pubkey_import_dsa_raw (c_key, &(c_args_d[0]), &(c_args_d[1]), |
| 5406 |
&(c_args_d[2]), &(c_args_d[3])); |
| 5407 |
} |
| 5408 |
SCM key = SCM_UNSPECIFIED; |
| 5409 |
if (EXPECT_FALSE (error)) |
| 5410 |
{ |
| 5411 |
scm_gnutls_error (error, FUNC_NAME); |
| 5412 |
} |
| 5413 |
else |
| 5414 |
{ |
| 5415 |
key = scm_from_gnutls_public_key (c_key); |
| 5416 |
} |
| 5417 |
scm_dynwind_end (); |
| 5418 |
return key; |
| 5419 |
} |
| 5420 |
|
| 5421 |
#undef FUNC_NAME |
| 5422 |
|
| 5423 |
SCM_DEFINE (scm_import_raw_ecc_public_key, "import-raw-ecc-public-key", 3, 0, |
| 5424 |
0, (SCM curve, SCM x, SCM y), |
| 5425 |
"Create a new ECC public key. For EdDSA curves, the y parameter " |
| 5426 |
"should be @code{#f}.") |
| 5427 |
#define FUNC_NAME s_scm_import_raw_ecc_public_key |
| 5428 |
{ |
| 5429 |
scm_dynwind_begin (0); |
| 5430 |
gnutls_ecc_curve_t c_curve = scm_to_gnutls_ecc_curve (curve, 1, FUNC_NAME); |
| 5431 |
SCM args[2] = { x, y }; |
| 5432 |
gnutls_datum_t c_args_d[2]; |
| 5433 |
gnutls_datum_t *c_args_ptr[2] = { NULL, NULL }; |
| 5434 |
const char *c_args[2]; |
| 5435 |
size_t c_args_length[2]; |
| 5436 |
scm_t_array_handle c_args_handle[2]; |
| 5437 |
/* If args[i] is #f, then we should pass NULL to |
| 5438 |
gnutls_pubkey_import_ecc_raw for the ith argument. */ |
| 5439 |
for (size_t i = 0; i < 2; i++) |
| 5440 |
{ |
| 5441 |
if (!scm_is_true (args[i])) |
| 5442 |
{ |
| 5443 |
continue; |
| 5444 |
} |
| 5445 |
c_args[i] = |
| 5446 |
scm_gnutls_get_array (args[i], &(c_args_handle[i]), |
| 5447 |
&(c_args_length[i]), FUNC_NAME); |
| 5448 |
c_args_d[i].data = (unsigned char *) c_args[i]; |
| 5449 |
c_args_d[i].size = c_args_length[i]; |
| 5450 |
scm_dynwind_release_handle (&(c_args_handle[i])); |
| 5451 |
c_args_ptr[i] = &c_args_d[i]; |
| 5452 |
} |
| 5453 |
gnutls_pubkey_t c_key; |
| 5454 |
int error = gnutls_pubkey_init (&c_key); |
| 5455 |
if (EXPECT_FALSE (error)) |
| 5456 |
{ |
| 5457 |
scm_gnutls_error (error, FUNC_NAME); |
| 5458 |
} |
| 5459 |
else |
| 5460 |
{ |
| 5461 |
scm_dynwind_unwind_handler (do_gnutls_pubkey_deinit, c_key, 0); |
| 5462 |
error = |
| 5463 |
gnutls_pubkey_import_ecc_raw (c_key, c_curve, c_args_ptr[0], |
| 5464 |
c_args_ptr[1]); |
| 5465 |
} |
| 5466 |
SCM key = SCM_UNSPECIFIED; |
| 5467 |
if (EXPECT_FALSE (error)) |
| 5468 |
{ |
| 5469 |
scm_gnutls_error (error, FUNC_NAME); |
| 5470 |
} |
| 5471 |
else |
| 5472 |
{ |
| 5473 |
key = scm_from_gnutls_public_key (c_key); |
| 5474 |
} |
| 5475 |
scm_dynwind_end (); |
| 5476 |
return key; |
| 5477 |
} |
| 5478 |
|
| 5479 |
#undef FUNC_NAME |
| 5480 |
|
| 5481 |
SCM_DEFINE (scm_import_raw_rsa_public_key, "import-raw-rsa-public-key", 2, 0, |
| 5482 |
0, (SCM m, SCM e), "Create a new RSA public key.") |
| 5483 |
#define FUNC_NAME s_scm_import_raw_rsa_public_key |
| 5484 |
{ |
| 5485 |
scm_dynwind_begin (0); |
| 5486 |
SCM args[2] = { m, e }; |
| 5487 |
gnutls_datum_t c_args_d[2]; |
| 5488 |
const char *c_args[2]; |
| 5489 |
size_t c_args_length[2]; |
| 5490 |
scm_t_array_handle c_args_handle[2]; |
| 5491 |
for (size_t i = 0; i < 2; i++) |
| 5492 |
{ |
| 5493 |
c_args[i] = |
| 5494 |
scm_gnutls_get_array (args[i], &(c_args_handle[i]), |
| 5495 |
&(c_args_length[i]), FUNC_NAME); |
| 5496 |
c_args_d[i].data = (unsigned char *) c_args[i]; |
| 5497 |
c_args_d[i].size = c_args_length[i]; |
| 5498 |
scm_dynwind_release_handle (&(c_args_handle[i])); |
| 5499 |
} |
| 5500 |
gnutls_pubkey_t c_key; |
| 5501 |
int error = gnutls_pubkey_init (&c_key); |
| 5502 |
if (EXPECT_FALSE (error)) |
| 5503 |
{ |
| 5504 |
scm_gnutls_error (error, FUNC_NAME); |
| 5505 |
} |
| 5506 |
else |
| 5507 |
{ |
| 5508 |
scm_dynwind_unwind_handler (do_gnutls_pubkey_deinit, c_key, 0); |
| 5509 |
error = |
| 5510 |
gnutls_pubkey_import_rsa_raw (c_key, &(c_args_d[0]), &(c_args_d[1])); |
| 5511 |
} |
| 5512 |
SCM key = SCM_UNSPECIFIED; |
| 5513 |
if (EXPECT_FALSE (error)) |
| 5514 |
{ |
| 5515 |
scm_gnutls_error (error, FUNC_NAME); |
| 5516 |
} |
| 5517 |
else |
| 5518 |
{ |
| 5519 |
key = scm_from_gnutls_public_key (c_key); |
| 5520 |
} |
| 5521 |
scm_dynwind_end (); |
| 5522 |
return key; |
| 5523 |
} |
| 5524 |
|
| 5525 |
#undef FUNC_NAME |
| 5526 |
|
| 5527 |
SCM_DEFINE (scm_import_raw_dsa_private_key, "import-raw-dsa-private-key", 5, |
| 5528 |
0, 0, (SCM p, SCM q, SCM g, SCM y, SCM x), |
| 5529 |
"Create a new DSA private key. " |
| 5530 |
"Starting at 3.7.0, the @var{y} parameter is optional, " |
| 5531 |
"pass @code{#f} if unknown.") |
| 5532 |
#define FUNC_NAME s_scm_import_raw_dsa_private_key |
| 5533 |
{ |
| 5534 |
scm_dynwind_begin (0); |
| 5535 |
SCM args[5] = { p, q, g, y, x }; |
| 5536 |
gnutls_datum_t c_args_d[5]; |
| 5537 |
gnutls_datum_t *c_args_opt[5]; |
| 5538 |
const char *c_args[5] = { NULL }; |
| 5539 |
size_t c_args_length[5] = { 0 }; |
| 5540 |
scm_t_array_handle c_args_handle[5]; |
| 5541 |
for (size_t i = 0; i < 5; i++) |
| 5542 |
{ |
| 5543 |
if (scm_is_true (args[i])) |
| 5544 |
{ |
| 5545 |
c_args[i] = |
| 5546 |
scm_gnutls_get_array (args[i], &(c_args_handle[i]), |
| 5547 |
&(c_args_length[i]), FUNC_NAME); |
| 5548 |
c_args_d[i].data = (unsigned char *) c_args[i]; |
| 5549 |
c_args_d[i].size = c_args_length[i]; |
| 5550 |
scm_dynwind_release_handle (&(c_args_handle[i])); |
| 5551 |
c_args_opt[i] = &(c_args_d[i]); |
| 5552 |
} |
| 5553 |
else |
| 5554 |
{ |
| 5555 |
c_args_opt[i] = NULL; |
| 5556 |
} |
| 5557 |
} |
| 5558 |
gnutls_privkey_t c_key; |
| 5559 |
int error = 0; |
| 5560 |
size_t offending_arg = 0; |
| 5561 |
for (size_t i = 0; i < 5; i++) |
| 5562 |
{ |
| 5563 |
if (c_args_opt[i] == NULL) |
| 5564 |
{ |
| 5565 |
/* This is only valid for argument 4, if the gnutls version is after |
| 5566 |
3.6.14. */ |
| 5567 |
if (i != 4 || !(gnutls_check_version_numeric (3, 7, 0))) |
| 5568 |
{ |
| 5569 |
error = GNUTLS_E_ILLEGAL_PARAMETER; |
| 5570 |
offending_arg = i; |
| 5571 |
} |
| 5572 |
} |
| 5573 |
} |
| 5574 |
if (EXPECT_FALSE (error)) |
| 5575 |
{ |
| 5576 |
scm_wrong_type_arg (FUNC_NAME, offending_arg, args[offending_arg]); |
| 5577 |
} |
| 5578 |
else |
| 5579 |
{ |
| 5580 |
error = gnutls_privkey_init (&c_key); |
| 5581 |
} |
| 5582 |
if (EXPECT_FALSE (error)) |
| 5583 |
{ |
| 5584 |
scm_gnutls_error (error, FUNC_NAME); |
| 5585 |
} |
| 5586 |
else |
| 5587 |
{ |
| 5588 |
scm_dynwind_unwind_handler (do_gnutls_privkey_deinit, c_key, 0); |
| 5589 |
error = |
| 5590 |
gnutls_privkey_import_dsa_raw (c_key, c_args_opt[0], c_args_opt[1], |
| 5591 |
c_args_opt[2], c_args_opt[3], |
| 5592 |
c_args_opt[4]); |
| 5593 |
} |
| 5594 |
SCM key = SCM_UNSPECIFIED; |
| 5595 |
if (EXPECT_FALSE (error)) |
| 5596 |
{ |
| 5597 |
scm_gnutls_error (error, FUNC_NAME); |
| 5598 |
} |
| 5599 |
else |
| 5600 |
{ |
| 5601 |
key = scm_from_gnutls_private_key (c_key); |
| 5602 |
} |
| 5603 |
scm_dynwind_end (); |
| 5604 |
return key; |
| 5605 |
} |
| 5606 |
|
| 5607 |
#undef FUNC_NAME |
| 5608 |
|
| 5609 |
SCM_DEFINE (scm_import_raw_ecc_private_key, "import-raw-ecc-private-key", 4, |
| 5610 |
0, 0, (SCM curve, SCM x, SCM y, SCM k), |
| 5611 |
"Create a new ECC private key. For EdDSA curves, " |
| 5612 |
"the y parameter should be @code{#f}.") |
| 5613 |
#define FUNC_NAME s_scm_import_raw_ecc_private_key |
| 5614 |
{ |
| 5615 |
gnutls_ecc_curve_t c_curve = scm_to_gnutls_ecc_curve (curve, 1, FUNC_NAME); |
| 5616 |
scm_dynwind_begin (0); |
| 5617 |
SCM args[3] = { x, y, k }; |
| 5618 |
gnutls_datum_t c_args_d[3]; |
| 5619 |
gnutls_datum_t *c_args_ptr[3] = { NULL, NULL, NULL }; |
| 5620 |
const char *c_args[3]; |
| 5621 |
size_t c_args_length[3]; |
| 5622 |
scm_t_array_handle c_args_handle[3]; |
| 5623 |
/* See import-raw-ecc-public-key. The Y parameter can be #f, in which case |
| 5624 |
we must pass NULL. */ |
| 5625 |
for (size_t i = 0; i < 3; i++) |
| 5626 |
{ |
| 5627 |
if (!scm_is_true (args[i])) |
| 5628 |
{ |
| 5629 |
continue; |
| 5630 |
} |
| 5631 |
c_args[i] = |
| 5632 |
scm_gnutls_get_array (args[i], &(c_args_handle[i]), |
| 5633 |
&(c_args_length[i]), FUNC_NAME); |
| 5634 |
c_args_d[i].data = (unsigned char *) c_args[i]; |
| 5635 |
c_args_d[i].size = c_args_length[i]; |
| 5636 |
scm_dynwind_release_handle (&(c_args_handle[i])); |
| 5637 |
c_args_ptr[i] = &c_args_d[i]; |
| 5638 |
} |
| 5639 |
gnutls_privkey_t c_key; |
| 5640 |
int error = gnutls_privkey_init (&c_key); |
| 5641 |
if (EXPECT_FALSE (error)) |
| 5642 |
{ |
| 5643 |
scm_gnutls_error (error, FUNC_NAME); |
| 5644 |
} |
| 5645 |
else |
| 5646 |
{ |
| 5647 |
scm_dynwind_unwind_handler (do_gnutls_privkey_deinit, c_key, 0); |
| 5648 |
error = |
| 5649 |
gnutls_privkey_import_ecc_raw (c_key, c_curve, c_args_ptr[0], |
| 5650 |
c_args_ptr[1], c_args_ptr[2]); |
| 5651 |
} |
| 5652 |
SCM key = SCM_UNSPECIFIED; |
| 5653 |
if (EXPECT_FALSE (error)) |
| 5654 |
{ |
| 5655 |
scm_gnutls_error (error, FUNC_NAME); |
| 5656 |
} |
| 5657 |
else |
| 5658 |
{ |
| 5659 |
key = scm_from_gnutls_private_key (c_key); |
| 5660 |
} |
| 5661 |
scm_dynwind_end (); |
| 5662 |
return key; |
| 5663 |
} |
| 5664 |
|
| 5665 |
#undef FUNC_NAME |
| 5666 |
|
| 5667 |
SCM_DEFINE (scm_import_raw_rsa_private_key, "import-raw-rsa-private-key", 8, |
| 5668 |
0, 0, (SCM m, SCM e, SCM d, SCM p, SCM q, SCM u, SCM e1, SCM e2), |
| 5669 |
"Create a new RSA private key. " |
| 5670 |
"@var{d} starting at 3.7.0, and @var{u}, @var{e1} and @var{e2} " |
| 5671 |
"are optional, pass @code{#f} to not set them.") |
| 5672 |
#define FUNC_NAME s_scm_import_raw_rsa_private_key |
| 5673 |
{ |
| 5674 |
scm_dynwind_begin (0); |
| 5675 |
SCM args[8] = { m, e, d, p, q, u, e1, e2 }; |
| 5676 |
gnutls_datum_t c_args_d[8]; |
| 5677 |
gnutls_datum_t *c_args_opt[8]; |
| 5678 |
const char *c_args[8] = { NULL }; |
| 5679 |
size_t c_args_length[8] = { 0 }; |
| 5680 |
scm_t_array_handle c_args_handle[8]; |
| 5681 |
for (size_t i = 0; i < 8; i++) |
| 5682 |
{ |
| 5683 |
if (scm_is_true (args[i])) |
| 5684 |
{ |
| 5685 |
c_args[i] = |
| 5686 |
scm_gnutls_get_array (args[i], &(c_args_handle[i]), |
| 5687 |
&(c_args_length[i]), FUNC_NAME); |
| 5688 |
c_args_d[i].data = (unsigned char *) c_args[i]; |
| 5689 |
c_args_d[i].size = c_args_length[i]; |
| 5690 |
scm_dynwind_release_handle (&(c_args_handle[i])); |
| 5691 |
c_args_opt[i] = &(c_args_d[i]); |
| 5692 |
} |
| 5693 |
else |
| 5694 |
{ |
| 5695 |
c_args_opt[i] = NULL; |
| 5696 |
} |
| 5697 |
} |
| 5698 |
gnutls_privkey_t c_key; |
| 5699 |
int error = 0; |
| 5700 |
size_t offending_arg = 0; |
| 5701 |
for (size_t i = 0; i < 8; i++) |
| 5702 |
{ |
| 5703 |
if (c_args_opt[i] == NULL) |
| 5704 |
{ |
| 5705 |
/* This is only valid for argument 2, if the gnutls version is after |
| 5706 |
3.6.14, or for arguments 5-7. */ |
| 5707 |
int valid = 0; |
| 5708 |
if (i == 2 && gnutls_check_version_numeric (3, 7, 0)) |
| 5709 |
{ |
| 5710 |
valid = 1; |
| 5711 |
} |
| 5712 |
if (i >= 5) |
| 5713 |
{ |
| 5714 |
valid = 1; |
| 5715 |
} |
| 5716 |
if (!valid) |
| 5717 |
{ |
| 5718 |
error = GNUTLS_E_ILLEGAL_PARAMETER; |
| 5719 |
offending_arg = i; |
| 5720 |
} |
| 5721 |
} |
| 5722 |
} |
| 5723 |
if (EXPECT_FALSE (error)) |
| 5724 |
{ |
| 5725 |
scm_wrong_type_arg (FUNC_NAME, offending_arg, args[offending_arg]); |
| 5726 |
} |
| 5727 |
else |
| 5728 |
{ |
| 5729 |
error = gnutls_privkey_init (&c_key); |
| 5730 |
} |
| 5731 |
if (EXPECT_FALSE (error)) |
| 5732 |
{ |
| 5733 |
scm_gnutls_error (error, FUNC_NAME); |
| 5734 |
} |
| 5735 |
else |
| 5736 |
{ |
| 5737 |
scm_dynwind_unwind_handler (do_gnutls_privkey_deinit, c_key, 0); |
| 5738 |
error = |
| 5739 |
gnutls_privkey_import_rsa_raw (c_key, c_args_opt[0], c_args_opt[1], |
| 5740 |
c_args_opt[2], c_args_opt[3], |
| 5741 |
c_args_opt[4], c_args_opt[5], |
| 5742 |
c_args_opt[6], c_args_opt[7]); |
| 5743 |
} |
| 5744 |
SCM key = SCM_UNSPECIFIED; |
| 5745 |
if (EXPECT_FALSE (error)) |
| 5746 |
{ |
| 5747 |
scm_gnutls_error (error, FUNC_NAME); |
| 5748 |
} |
| 5749 |
else |
| 5750 |
{ |
| 5751 |
key = scm_from_gnutls_private_key (c_key); |
| 5752 |
} |
| 5753 |
scm_dynwind_end (); |
| 5754 |
return key; |
| 5755 |
} |
| 5756 |
|
| 5757 |
#undef FUNC_NAME |
| 5758 |
|
| 5759 |
SCM_DEFINE (scm_public_key_export_raw_dsa, "public-key-export-raw-dsa", 1, 0, |
| 5760 |
0, (SCM key), |
| 5761 |
"Export a DSA public key, and return 4 parameters: " |
| 5762 |
"P, Q, G and Y.") |
| 5763 |
#define FUNC_NAME s_scm_public_key_export_raw_dsa |
| 5764 |
{ |
| 5765 |
gnutls_pubkey_t c_key = scm_to_gnutls_public_key (key, 1, FUNC_NAME); |
| 5766 |
gnutls_datum_t c_parameters[4]; |
| 5767 |
scm_dynwind_begin (0); |
| 5768 |
int error = gnutls_pubkey_export_dsa_raw2 (c_key, &(c_parameters[0]), |
| 5769 |
&(c_parameters[1]), |
| 5770 |
&(c_parameters[2]), |
| 5771 |
&(c_parameters[3]), 0); |
| 5772 |
SCM ret = SCM_UNSPECIFIED; |
| 5773 |
if (EXPECT_FALSE (error)) |
| 5774 |
{ |
| 5775 |
scm_gnutls_error (error, FUNC_NAME); |
| 5776 |
} |
| 5777 |
else |
| 5778 |
{ |
| 5779 |
for (size_t i = 0; i < 4; i++) |
| 5780 |
{ |
| 5781 |
scm_dynwind_unwind_handler (gnutls_free, c_parameters[i].data, |
| 5782 |
SCM_F_WIND_EXPLICITLY); |
| 5783 |
} |
| 5784 |
SCM parameters[4]; |
| 5785 |
for (size_t i = 0; i < 4; i++) |
| 5786 |
{ |
| 5787 |
parameters[i] = scm_c_make_bytevector (c_parameters[i].size); |
| 5788 |
memcpy (SCM_BYTEVECTOR_CONTENTS (parameters[i]), |
| 5789 |
c_parameters[i].data, c_parameters[i].size); |
| 5790 |
} |
| 5791 |
ret = scm_c_values (parameters, 4); |
| 5792 |
} |
| 5793 |
scm_dynwind_end (); |
| 5794 |
return ret; |
| 5795 |
} |
| 5796 |
|
| 5797 |
#undef FUNC_NAME |
| 5798 |
|
| 5799 |
SCM_DEFINE (scm_public_key_export_raw_ecc, "public-key-export-raw-ecc", 1, 0, |
| 5800 |
0, (SCM key), |
| 5801 |
"Export a ECC public key, and return 3 parameters: " |
| 5802 |
"the curve, X and Y. For EdDSA curves, Y is always " "@code{#f}.") |
| 5803 |
#define FUNC_NAME s_scm_public_key_export_raw_ecc |
| 5804 |
{ |
| 5805 |
gnutls_pubkey_t c_key = scm_to_gnutls_public_key (key, 1, FUNC_NAME); |
| 5806 |
gnutls_ecc_curve_t c_curve; |
| 5807 |
gnutls_datum_t c_parameters[2]; |
| 5808 |
scm_dynwind_begin (0); |
| 5809 |
int error = |
| 5810 |
gnutls_pubkey_export_ecc_raw2 (c_key, &c_curve, &(c_parameters[0]), |
| 5811 |
&(c_parameters[1]), 0); |
| 5812 |
SCM ret = SCM_UNSPECIFIED; |
| 5813 |
if (EXPECT_FALSE (error)) |
| 5814 |
{ |
| 5815 |
scm_gnutls_error (error, FUNC_NAME); |
| 5816 |
} |
| 5817 |
else |
| 5818 |
{ |
| 5819 |
for (size_t i = 0; i < 2; i++) |
| 5820 |
{ |
| 5821 |
scm_dynwind_unwind_handler (gnutls_free, c_parameters[i].data, |
| 5822 |
SCM_F_WIND_EXPLICITLY); |
| 5823 |
} |
| 5824 |
SCM parameters[3]; |
| 5825 |
parameters[0] = scm_from_gnutls_ecc_curve (c_curve); |
| 5826 |
for (size_t i = 0; i < 2; i++) |
| 5827 |
{ |
| 5828 |
/* c_parameters[i].data might be NULL: the Y part of an EdDSA public key |
| 5829 |
is always NULL. */ |
| 5830 |
if (c_parameters[i].data != NULL) |
| 5831 |
{ |
| 5832 |
parameters[i + 1] = |
| 5833 |
scm_c_make_bytevector (c_parameters[i].size); |
| 5834 |
memcpy (SCM_BYTEVECTOR_CONTENTS (parameters[i + 1]), |
| 5835 |
c_parameters[i].data, c_parameters[i].size); |
| 5836 |
} |
| 5837 |
else |
| 5838 |
{ |
| 5839 |
parameters[i + 1] = SCM_BOOL_F; |
| 5840 |
} |
| 5841 |
} |
| 5842 |
ret = scm_c_values (parameters, 3); |
| 5843 |
} |
| 5844 |
scm_dynwind_end (); |
| 5845 |
return ret; |
| 5846 |
} |
| 5847 |
|
| 5848 |
#undef FUNC_NAME |
| 5849 |
|
| 5850 |
SCM_DEFINE (scm_public_key_export_raw_rsa, "public-key-export-raw-rsa", 1, 0, |
| 5851 |
0, (SCM key), |
| 5852 |
"Export a RSA public key, and return 2 parameters: " "M and E.") |
| 5853 |
#define FUNC_NAME s_scm_public_key_export_raw_rsa |
| 5854 |
{ |
| 5855 |
gnutls_pubkey_t c_key = scm_to_gnutls_public_key (key, 1, FUNC_NAME); |
| 5856 |
gnutls_datum_t c_parameters[2]; |
| 5857 |
scm_dynwind_begin (0); |
| 5858 |
int error = gnutls_pubkey_export_rsa_raw2 (c_key, &(c_parameters[0]), |
| 5859 |
&(c_parameters[1]), 0); |
| 5860 |
SCM ret = SCM_UNSPECIFIED; |
| 5861 |
if (EXPECT_FALSE (error)) |
| 5862 |
{ |
| 5863 |
scm_gnutls_error (error, FUNC_NAME); |
| 5864 |
} |
| 5865 |
else |
| 5866 |
{ |
| 5867 |
for (size_t i = 0; i < 2; i++) |
| 5868 |
{ |
| 5869 |
scm_dynwind_unwind_handler (gnutls_free, c_parameters[i].data, |
| 5870 |
SCM_F_WIND_EXPLICITLY); |
| 5871 |
} |
| 5872 |
SCM parameters[2]; |
| 5873 |
for (size_t i = 0; i < 2; i++) |
| 5874 |
{ |
| 5875 |
parameters[i] = scm_c_make_bytevector (c_parameters[i].size); |
| 5876 |
memcpy (SCM_BYTEVECTOR_CONTENTS (parameters[i]), |
| 5877 |
c_parameters[i].data, c_parameters[i].size); |
| 5878 |
} |
| 5879 |
ret = scm_c_values (parameters, 2); |
| 5880 |
} |
| 5881 |
scm_dynwind_end (); |
| 5882 |
return ret; |
| 5883 |
} |
| 5884 |
|
| 5885 |
#undef FUNC_NAME |
| 5886 |
|
| 5887 |
SCM_DEFINE (scm_private_key_export_raw_dsa, "private-key-export-raw-dsa", 1, |
| 5888 |
0, 0, (SCM key), |
| 5889 |
"Export a DSA private key, and return 5 parameters: " |
| 5890 |
"P, Q, G, Y and X.") |
| 5891 |
#define FUNC_NAME s_scm_private_key_export_raw_dsa |
| 5892 |
{ |
| 5893 |
gnutls_privkey_t c_key = scm_to_gnutls_private_key (key, 1, FUNC_NAME); |
| 5894 |
gnutls_datum_t c_parameters[5]; |
| 5895 |
scm_dynwind_begin (0); |
| 5896 |
int error = gnutls_privkey_export_dsa_raw2 (c_key, &(c_parameters[0]), |
| 5897 |
&(c_parameters[1]), |
| 5898 |
&(c_parameters[2]), |
| 5899 |
&(c_parameters[3]), |
| 5900 |
&(c_parameters[4]), |
| 5901 |
0); |
| 5902 |
SCM ret = SCM_UNSPECIFIED; |
| 5903 |
if (EXPECT_FALSE (error)) |
| 5904 |
{ |
| 5905 |
scm_gnutls_error (error, FUNC_NAME); |
| 5906 |
} |
| 5907 |
else |
| 5908 |
{ |
| 5909 |
for (size_t i = 0; i < 5; i++) |
| 5910 |
{ |
| 5911 |
scm_dynwind_unwind_handler (gnutls_free, c_parameters[i].data, |
| 5912 |
SCM_F_WIND_EXPLICITLY); |
| 5913 |
} |
| 5914 |
SCM parameters[5]; |
| 5915 |
for (size_t i = 0; i < 5; i++) |
| 5916 |
{ |
| 5917 |
parameters[i] = scm_c_make_bytevector (c_parameters[i].size); |
| 5918 |
memcpy (SCM_BYTEVECTOR_CONTENTS (parameters[i]), |
| 5919 |
c_parameters[i].data, c_parameters[i].size); |
| 5920 |
} |
| 5921 |
ret = scm_c_values (parameters, 5); |
| 5922 |
} |
| 5923 |
scm_dynwind_end (); |
| 5924 |
return ret; |
| 5925 |
} |
| 5926 |
|
| 5927 |
#undef FUNC_NAME |
| 5928 |
|
| 5929 |
SCM_DEFINE (scm_private_key_export_raw_ecc, "private-key-export-raw-ecc", 1, |
| 5930 |
0, 0, (SCM key), |
| 5931 |
"Export a ECC private key, and return 4 parameters: " |
| 5932 |
"the curve, X, Y and K. For EdDSA keys, Y is always @code{#f}.") |
| 5933 |
#define FUNC_NAME s_scm_private_key_export_raw_ecc |
| 5934 |
{ |
| 5935 |
gnutls_privkey_t c_key = scm_to_gnutls_private_key (key, 1, FUNC_NAME); |
| 5936 |
gnutls_ecc_curve_t c_curve; |
| 5937 |
gnutls_datum_t c_parameters[3]; |
| 5938 |
scm_dynwind_begin (0); |
| 5939 |
int error = |
| 5940 |
gnutls_privkey_export_ecc_raw2 (c_key, &c_curve, &(c_parameters[0]), |
| 5941 |
&(c_parameters[1]), &(c_parameters[2]), |
| 5942 |
0); |
| 5943 |
SCM ret = SCM_UNSPECIFIED; |
| 5944 |
if (EXPECT_FALSE (error)) |
| 5945 |
{ |
| 5946 |
scm_gnutls_error (error, FUNC_NAME); |
| 5947 |
} |
| 5948 |
else |
| 5949 |
{ |
| 5950 |
for (size_t i = 0; i < 3; i++) |
| 5951 |
{ |
| 5952 |
scm_dynwind_unwind_handler (gnutls_free, c_parameters[i].data, |
| 5953 |
SCM_F_WIND_EXPLICITLY); |
| 5954 |
} |
| 5955 |
SCM parameters[4]; |
| 5956 |
parameters[0] = scm_from_gnutls_ecc_curve (c_curve); |
| 5957 |
for (size_t i = 0; i < 3; i++) |
| 5958 |
{ |
| 5959 |
/* See public-key-export-raw-ecc: the Y parameter will be NULL for |
| 5960 |
EdDSA keys. */ |
| 5961 |
if (c_parameters[i].data != NULL) |
| 5962 |
{ |
| 5963 |
parameters[i + 1] = |
| 5964 |
scm_c_make_bytevector (c_parameters[i].size); |
| 5965 |
memcpy (SCM_BYTEVECTOR_CONTENTS (parameters[i + 1]), |
| 5966 |
c_parameters[i].data, c_parameters[i].size); |
| 5967 |
} |
| 5968 |
else |
| 5969 |
{ |
| 5970 |
parameters[i + 1] = SCM_BOOL_F; |
| 5971 |
} |
| 5972 |
} |
| 5973 |
ret = scm_c_values (parameters, 4); |
| 5974 |
} |
| 5975 |
scm_dynwind_end (); |
| 5976 |
return ret; |
| 5977 |
} |
| 5978 |
|
| 5979 |
#undef FUNC_NAME |
| 5980 |
|
| 5981 |
SCM_DEFINE (scm_private_key_export_raw_rsa, "private-key-export-raw-rsa", 1, |
| 5982 |
0, 0, (SCM key), |
| 5983 |
"Export a RSA private key, and return 8 parameters: " |
| 5984 |
"M, E, D, P, Q, U, E1, E2.") |
| 5985 |
#define FUNC_NAME s_scm_private_key_export_raw_rsa |
| 5986 |
{ |
| 5987 |
gnutls_privkey_t c_key = scm_to_gnutls_private_key (key, 1, FUNC_NAME); |
| 5988 |
gnutls_datum_t c_parameters[8]; |
| 5989 |
scm_dynwind_begin (0); |
| 5990 |
int error = gnutls_privkey_export_rsa_raw2 (c_key, &(c_parameters[0]), |
| 5991 |
&(c_parameters[1]), |
| 5992 |
&(c_parameters[2]), |
| 5993 |
&(c_parameters[3]), |
| 5994 |
&(c_parameters[4]), |
| 5995 |
&(c_parameters[5]), |
| 5996 |
&(c_parameters[6]), |
| 5997 |
&(c_parameters[7]), 0); |
| 5998 |
SCM ret = SCM_UNSPECIFIED; |
| 5999 |
if (EXPECT_FALSE (error)) |
| 6000 |
{ |
| 6001 |
scm_gnutls_error (error, FUNC_NAME); |
| 6002 |
} |
| 6003 |
else |
| 6004 |
{ |
| 6005 |
for (size_t i = 0; i < 8; i++) |
| 6006 |
{ |
| 6007 |
scm_dynwind_unwind_handler (gnutls_free, c_parameters[i].data, |
| 6008 |
SCM_F_WIND_EXPLICITLY); |
| 6009 |
} |
| 6010 |
SCM parameters[8]; |
| 6011 |
for (size_t i = 0; i < 8; i++) |
| 6012 |
{ |
| 6013 |
parameters[i] = scm_c_make_bytevector (c_parameters[i].size); |
| 6014 |
memcpy (SCM_BYTEVECTOR_CONTENTS (parameters[i]), |
| 6015 |
c_parameters[i].data, c_parameters[i].size); |
| 6016 |
} |
| 6017 |
ret = scm_c_values (parameters, 8); |
| 6018 |
} |
| 6019 |
scm_dynwind_end (); |
| 6020 |
return ret; |
| 6021 |
} |
| 6022 |
|
| 6023 |
#undef FUNC_NAME |
| 6024 |
|
| 6025 |
SCM_DEFINE (scm_private_key_to_public_key, "private-key->public-key", 2, 0, 0, |
| 6026 |
(SCM key, SCM usage), |
| 6027 |
"Return the public part of @var{key}. @var{usage} is a list of " |
| 6028 |
"key usage flags, such as @code{key-usage/digital-signature}.") |
| 6029 |
#define FUNC_NAME s_scm_private_key_to_public_key |
| 6030 |
{ |
| 6031 |
unsigned int c_usage = 0; |
| 6032 |
for (c_usage = 0; !scm_is_null (usage); usage = SCM_CDR (usage)) |
| 6033 |
{ |
| 6034 |
c_usage |= |
| 6035 |
((unsigned int) |
| 6036 |
scm_to_gnutls_key_usage (SCM_CAR (usage), 2, FUNC_NAME)); |
| 6037 |
} |
| 6038 |
gnutls_privkey_t c_key = scm_to_gnutls_private_key (key, 1, FUNC_NAME); |
| 6039 |
gnutls_pubkey_t c_pub; |
| 6040 |
scm_dynwind_begin (0); |
| 6041 |
int error = gnutls_pubkey_init (&c_pub); |
| 6042 |
SCM pub = SCM_UNSPECIFIED; |
| 6043 |
if (EXPECT_FALSE (error)) |
| 6044 |
{ |
| 6045 |
scm_gnutls_error (error, FUNC_NAME); |
| 6046 |
} |
| 6047 |
else |
| 6048 |
{ |
| 6049 |
scm_dynwind_unwind_handler (do_gnutls_pubkey_deinit, c_pub, 0); |
| 6050 |
error = gnutls_pubkey_import_privkey (c_pub, c_key, c_usage, 0); |
| 6051 |
} |
| 6052 |
if (EXPECT_FALSE (error)) |
| 6053 |
{ |
| 6054 |
scm_gnutls_error (error, FUNC_NAME); |
| 6055 |
} |
| 6056 |
else |
| 6057 |
{ |
| 6058 |
pub = scm_from_gnutls_public_key (c_pub); |
| 6059 |
} |
| 6060 |
scm_dynwind_end (); |
| 6061 |
return pub; |
| 6062 |
} |
| 6063 |
|
| 6064 |
#undef FUNC_NAME |
| 6065 |
|
| 6066 |
SCM_DEFINE (scm_public_key_export, "public-key-export", 2, 0, 0, |
| 6067 |
(SCM key, SCM format), "Export a public key to PEM or DER.") |
| 6068 |
#define FUNC_NAME s_scm_public_key_export |
| 6069 |
{ |
| 6070 |
gnutls_pubkey_t c_key = scm_to_gnutls_public_key (key, 1, FUNC_NAME); |
| 6071 |
gnutls_x509_crt_fmt_t c_format = |
| 6072 |
scm_to_gnutls_x509_certificate_format (format, 2, FUNC_NAME); |
| 6073 |
scm_dynwind_begin (0); |
| 6074 |
gnutls_datum_t c_out; |
| 6075 |
int error = gnutls_pubkey_export2 (c_key, c_format, &c_out); |
| 6076 |
scm_dynwind_unwind_handler (gnutls_free, c_out.data, SCM_F_WIND_EXPLICITLY); |
| 6077 |
SCM out = SCM_UNSPECIFIED; |
| 6078 |
if (EXPECT_FALSE (error)) |
| 6079 |
{ |
| 6080 |
scm_gnutls_error (error, FUNC_NAME); |
| 6081 |
} |
| 6082 |
else |
| 6083 |
{ |
| 6084 |
out = scm_c_make_bytevector (c_out.size); |
| 6085 |
memcpy (SCM_BYTEVECTOR_CONTENTS (out), c_out.data, c_out.size); |
| 6086 |
} |
| 6087 |
scm_dynwind_end (); |
| 6088 |
return out; |
| 6089 |
} |
| 6090 |
|
| 6091 |
#undef FUNC_NAME |
| 6092 |
|
| 6093 |
SCM_DEFINE (scm_public_key_pk_algorithm, "public-key-pk-algorithm", 1, 0, 0, |
| 6094 |
(SCM key), |
| 6095 |
"Return the public key algorithm used by @var{key} " |
| 6096 |
"and the number of bits.") |
| 6097 |
#define FUNC_NAME s_scm_public_key_pk_algorithm |
| 6098 |
{ |
| 6099 |
gnutls_pubkey_t c_key = scm_to_gnutls_public_key (key, 1, FUNC_NAME); |
| 6100 |
unsigned int c_bits = 0; |
| 6101 |
int error = gnutls_pubkey_get_pk_algorithm (c_key, &c_bits); |
| 6102 |
if (EXPECT_FALSE (error < 0)) |
| 6103 |
{ |
| 6104 |
scm_gnutls_error (error, FUNC_NAME); |
| 6105 |
} |
| 6106 |
SCM output[2]; |
| 6107 |
output[0] = scm_from_gnutls_pk_algorithm (error); |
| 6108 |
output[1] = scm_from_uint (c_bits); |
| 6109 |
return scm_c_values (output, 2); |
| 6110 |
} |
| 6111 |
|
| 6112 |
#undef FUNC_NAME |
| 6113 |
|
| 6114 |
SCM_DEFINE (scm_private_key_pk_algorithm, "private-key-pk-algorithm", 1, 0, 0, |
| 6115 |
(SCM key), |
| 6116 |
"Return the private key algorithm used by @var{key} " |
| 6117 |
"and the number of bits.") |
| 6118 |
#define FUNC_NAME s_scm_private_key_pk_algorithm |
| 6119 |
{ |
| 6120 |
gnutls_privkey_t c_key = scm_to_gnutls_private_key (key, 1, FUNC_NAME); |
| 6121 |
unsigned int c_bits = 0; |
| 6122 |
int error = gnutls_privkey_get_pk_algorithm (c_key, &c_bits); |
| 6123 |
if (EXPECT_FALSE (error < 0)) |
| 6124 |
{ |
| 6125 |
scm_gnutls_error (error, FUNC_NAME); |
| 6126 |
} |
| 6127 |
SCM output[2]; |
| 6128 |
output[0] = scm_from_gnutls_pk_algorithm (error); |
| 6129 |
output[1] = scm_from_uint (c_bits); |
| 6130 |
return scm_c_values (output, 2); |
| 6131 |
} |
| 6132 |
|
| 6133 |
#undef FUNC_NAME |
| 6134 |
|
| 6135 |
SCM_DEFINE (scm_public_key_preferred_hash_algorithm, |
| 6136 |
"public-key-preferred-hash-algorithm", 1, 0, 0, (SCM key), |
| 6137 |
"Return the preferred hash algorithm for @var{key}, " |
| 6138 |
"and a boolean indicating whether this algorithm is " |
| 6139 |
"mandatory.") |
| 6140 |
#define FUNC_NAME s_scm_public_key_preferred_hash_algorithm |
| 6141 |
{ |
| 6142 |
gnutls_pubkey_t c_key = scm_to_gnutls_public_key (key, 1, FUNC_NAME); |
| 6143 |
gnutls_digest_algorithm_t c_algo; |
| 6144 |
unsigned int c_mandatory = 0; |
| 6145 |
int error = |
| 6146 |
gnutls_pubkey_get_preferred_hash_algorithm (c_key, &c_algo, &c_mandatory); |
| 6147 |
if (EXPECT_FALSE (error)) |
| 6148 |
{ |
| 6149 |
scm_gnutls_error (error, FUNC_NAME); |
| 6150 |
} |
| 6151 |
SCM output[2]; |
| 6152 |
output[0] = scm_from_gnutls_digest (c_algo); |
| 6153 |
output[1] = scm_from_bool (c_mandatory); |
| 6154 |
return scm_c_values (output, 2); |
| 6155 |
} |
| 6156 |
|
| 6157 |
#undef FUNC_NAME |
| 6158 |
|
| 6159 |
SCM_DEFINE (scm_generate_private_key, "generate-private-key", 2, 0, 0, |
| 6160 |
(SCM algo, SCM bits_or_curve), "Return a new private key.") |
| 6161 |
#define FUNC_NAME s_scm_generate_private_key |
| 6162 |
{ |
| 6163 |
gnutls_pk_algorithm_t c_algo = |
| 6164 |
scm_to_gnutls_pk_algorithm (algo, 1, FUNC_NAME); |
| 6165 |
unsigned int c_bits; |
| 6166 |
if (scm_is_integer (bits_or_curve)) |
| 6167 |
{ |
| 6168 |
c_bits = scm_to_uint (bits_or_curve); |
| 6169 |
} |
| 6170 |
else |
| 6171 |
{ |
| 6172 |
c_bits = |
| 6173 |
GNUTLS_CURVE_TO_BITS (scm_to_gnutls_ecc_curve |
| 6174 |
(bits_or_curve, 2, FUNC_NAME)); |
| 6175 |
} |
| 6176 |
gnutls_privkey_t c_key; |
| 6177 |
SCM key = SCM_UNSPECIFIED; |
| 6178 |
scm_dynwind_begin (0); |
| 6179 |
int error = gnutls_privkey_init (&c_key); |
| 6180 |
if (EXPECT_FALSE (error)) |
| 6181 |
{ |
| 6182 |
scm_gnutls_error (error, FUNC_NAME); |
| 6183 |
} |
| 6184 |
else |
| 6185 |
{ |
| 6186 |
scm_dynwind_unwind_handler (do_gnutls_privkey_deinit, c_key, 0); |
| 6187 |
error = gnutls_privkey_generate2 (c_key, c_algo, c_bits, 0, NULL, 0); |
| 6188 |
} |
| 6189 |
if (EXPECT_FALSE (error)) |
| 6190 |
{ |
| 6191 |
scm_gnutls_error (error, FUNC_NAME); |
| 6192 |
} |
| 6193 |
else |
| 6194 |
{ |
| 6195 |
key = scm_from_gnutls_private_key (c_key); |
| 6196 |
} |
| 6197 |
scm_dynwind_end (); |
| 6198 |
return key; |
| 6199 |
} |
| 6200 |
|
| 6201 |
#undef FUNC_NAME |
| 6202 |
|
| 6203 |
SCM_DEFINE (scm_private_key_sign_data, "private-key-sign-data", 4, 0, 0, |
| 6204 |
(SCM key, SCM algo, SCM data, SCM flags), |
| 6205 |
"Sign the @var{data} and return the signature. " |
| 6206 |
"@var{flags} is a list of privkey flags." |
| 6207 |
"Available flags are: @code{privkey/sign-flag-tls1-rsa} " |
| 6208 |
"@code{privkey/sign-flag-rsa-pss} @code{privkey/flag-reproducible}.") |
| 6209 |
#define FUNC_NAME s_scm_private_key_sign_data |
| 6210 |
{ |
| 6211 |
gnutls_privkey_t c_key = scm_to_gnutls_private_key (key, 1, FUNC_NAME); |
| 6212 |
gnutls_sign_algorithm_t c_algo = |
| 6213 |
scm_to_gnutls_sign_algorithm (algo, 2, FUNC_NAME); |
| 6214 |
gnutls_datum_t c_data_d; |
| 6215 |
const char *c_data; |
| 6216 |
size_t c_data_length; |
| 6217 |
scm_t_array_handle c_data_handle; |
| 6218 |
unsigned int c_flags = 0; |
| 6219 |
gnutls_datum_t c_signature; |
| 6220 |
for (c_flags = 0; !scm_is_null (flags); flags = SCM_CDR (flags)) |
| 6221 |
{ |
| 6222 |
c_flags |= |
| 6223 |
((unsigned int) |
| 6224 |
scm_to_gnutls_privkey (SCM_CAR (flags), 4, FUNC_NAME)); |
| 6225 |
} |
| 6226 |
scm_dynwind_begin (0); |
| 6227 |
c_data = |
| 6228 |
scm_gnutls_get_array (data, &c_data_handle, &c_data_length, FUNC_NAME); |
| 6229 |
scm_dynwind_release_handle (&c_data_handle); |
| 6230 |
c_data_d.data = (unsigned char *) c_data; |
| 6231 |
c_data_d.size = c_data_length; |
| 6232 |
int error = gnutls_privkey_sign_data2 (c_key, c_algo, c_flags, &c_data_d, |
| 6233 |
&c_signature); |
| 6234 |
SCM signature = SCM_UNSPECIFIED; |
| 6235 |
if (EXPECT_FALSE (error)) |
| 6236 |
{ |
| 6237 |
scm_gnutls_error (error, FUNC_NAME); |
| 6238 |
} |
| 6239 |
else |
| 6240 |
{ |
| 6241 |
scm_dynwind_unwind_handler (gnutls_free, c_signature.data, |
| 6242 |
SCM_F_WIND_EXPLICITLY); |
| 6243 |
signature = scm_c_make_bytevector (c_signature.size); |
| 6244 |
memcpy (SCM_BYTEVECTOR_CONTENTS (signature), c_signature.data, |
| 6245 |
c_signature.size); |
| 6246 |
} |
| 6247 |
scm_dynwind_end (); |
| 6248 |
return signature; |
| 6249 |
} |
| 6250 |
|
| 6251 |
#undef FUNC_NAME |
| 6252 |
|
| 6253 |
SCM_DEFINE (scm_private_key_sign_hash, "private-key-sign-hash", 4, 0, 0, |
| 6254 |
(SCM key, SCM algo, SCM hash_data, SCM flags), |
| 6255 |
"Sign the @var{hash_data} and return the signature. " |
| 6256 |
"@var{flags} is a list of privkey flags. " |
| 6257 |
"Available flags are: @code{privkey/sign-flag-tls1-rsa} " |
| 6258 |
"@code{privkey/sign-flag-rsa-pss} @code{flag-reproducible}.") |
| 6259 |
#define FUNC_NAME s_scm_private_key_sign_hash |
| 6260 |
{ |
| 6261 |
gnutls_privkey_t c_key = scm_to_gnutls_private_key (key, 1, FUNC_NAME); |
| 6262 |
gnutls_sign_algorithm_t c_algo = |
| 6263 |
scm_to_gnutls_sign_algorithm (algo, 2, FUNC_NAME); |
| 6264 |
gnutls_datum_t c_data_d; |
| 6265 |
const char *c_data; |
| 6266 |
size_t c_data_length; |
| 6267 |
scm_t_array_handle c_data_handle; |
| 6268 |
unsigned int c_flags = 0; |
| 6269 |
gnutls_datum_t c_signature; |
| 6270 |
for (c_flags = 0; !scm_is_null (flags); flags = SCM_CDR (flags)) |
| 6271 |
{ |
| 6272 |
c_flags |= |
| 6273 |
((unsigned int) |
| 6274 |
scm_to_gnutls_privkey (SCM_CAR (flags), 4, FUNC_NAME)); |
| 6275 |
} |
| 6276 |
scm_dynwind_begin (0); |
| 6277 |
c_data = |
| 6278 |
scm_gnutls_get_array (hash_data, &c_data_handle, &c_data_length, |
| 6279 |
FUNC_NAME); |
| 6280 |
scm_dynwind_release_handle (&c_data_handle); |
| 6281 |
c_data_d.data = (unsigned char *) c_data; |
| 6282 |
c_data_d.size = c_data_length; |
| 6283 |
int error = gnutls_privkey_sign_hash2 (c_key, c_algo, c_flags, &c_data_d, |
| 6284 |
&c_signature); |
| 6285 |
SCM signature = SCM_UNSPECIFIED; |
| 6286 |
if (EXPECT_FALSE (error)) |
| 6287 |
{ |
| 6288 |
scm_gnutls_error (error, FUNC_NAME); |
| 6289 |
} |
| 6290 |
else |
| 6291 |
{ |
| 6292 |
scm_dynwind_unwind_handler (gnutls_free, c_signature.data, |
| 6293 |
SCM_F_WIND_EXPLICITLY); |
| 6294 |
signature = scm_c_make_bytevector (c_signature.size); |
| 6295 |
memcpy (SCM_BYTEVECTOR_CONTENTS (signature), c_signature.data, |
| 6296 |
c_signature.size); |
| 6297 |
} |
| 6298 |
scm_dynwind_end (); |
| 6299 |
return signature; |
| 6300 |
} |
| 6301 |
|
| 6302 |
#undef FUNC_NAME |
| 6303 |
|
| 6304 |
SCM_DEFINE (scm_private_key_decrypt_data, "private-key-decrypt-data", 2, 0, 0, |
| 6305 |
(SCM key, SCM data), "Decrypt the @var{data}.") |
| 6306 |
#define FUNC_NAME s_scm_private_key_decrypt_data |
| 6307 |
{ |
| 6308 |
gnutls_privkey_t c_key = scm_to_gnutls_private_key (key, 1, FUNC_NAME); |
| 6309 |
gnutls_datum_t c_data_d; |
| 6310 |
const char *c_data; |
| 6311 |
size_t c_data_length; |
| 6312 |
scm_t_array_handle c_data_handle; |
| 6313 |
scm_dynwind_begin (0); |
| 6314 |
c_data = |
| 6315 |
scm_gnutls_get_array (data, &c_data_handle, &c_data_length, FUNC_NAME); |
| 6316 |
scm_dynwind_release_handle (&c_data_handle); |
| 6317 |
c_data_d.data = (unsigned char *) c_data; |
| 6318 |
c_data_d.size = c_data_length; |
| 6319 |
SCM output = scm_c_make_bytevector (c_data_length); |
| 6320 |
unsigned char *c_output = |
| 6321 |
(unsigned char *) SCM_BYTEVECTOR_CONTENTS (output); |
| 6322 |
int error = gnutls_privkey_decrypt_data2 (c_key, 0, &c_data_d, c_output, |
| 6323 |
c_data_length); |
| 6324 |
if (EXPECT_FALSE (error)) |
| 6325 |
{ |
| 6326 |
scm_gnutls_error (error, FUNC_NAME); |
| 6327 |
} |
| 6328 |
scm_dynwind_end (); |
| 6329 |
return output; |
| 6330 |
} |
| 6331 |
|
| 6332 |
#undef FUNC_NAME |
| 6333 |
|
| 6334 |
SCM_DEFINE (scm_public_key_encrypt_data, "public-key-encrypt-data", 2, 0, 0, |
| 6335 |
(SCM key, SCM data), "Encrypt the @var{data}.") |
| 6336 |
#define FUNC_NAME s_scm_public_key_encrypt_data |
| 6337 |
{ |
| 6338 |
gnutls_pubkey_t c_key = scm_to_gnutls_public_key (key, 1, FUNC_NAME); |
| 6339 |
gnutls_datum_t c_data_d; |
| 6340 |
const char *c_data; |
| 6341 |
size_t c_data_length; |
| 6342 |
scm_t_array_handle c_data_handle; |
| 6343 |
scm_dynwind_begin (0); |
| 6344 |
c_data = |
| 6345 |
scm_gnutls_get_array (data, &c_data_handle, &c_data_length, FUNC_NAME); |
| 6346 |
scm_dynwind_release_handle (&c_data_handle); |
| 6347 |
c_data_d.data = (unsigned char *) c_data; |
| 6348 |
c_data_d.size = c_data_length; |
| 6349 |
gnutls_datum_t c_output; |
| 6350 |
SCM output = SCM_UNSPECIFIED; |
| 6351 |
int error = gnutls_pubkey_encrypt_data (c_key, 0, &c_data_d, &c_output); |
| 6352 |
if (EXPECT_FALSE (error)) |
| 6353 |
{ |
| 6354 |
scm_gnutls_error (error, FUNC_NAME); |
| 6355 |
} |
| 6356 |
else |
| 6357 |
{ |
| 6358 |
scm_dynwind_unwind_handler (gnutls_free, c_output.data, |
| 6359 |
SCM_F_WIND_EXPLICITLY); |
| 6360 |
output = scm_c_make_bytevector (c_output.size); |
| 6361 |
memcpy (SCM_BYTEVECTOR_CONTENTS (output), c_output.data, c_output.size); |
| 6362 |
} |
| 6363 |
scm_dynwind_end (); |
| 6364 |
return output; |
| 6365 |
} |
| 6366 |
|
| 6367 |
#undef FUNC_NAME |
| 6368 |
|
| 6369 |
SCM_DEFINE (scm_public_key_verify_data, "public-key-verify-data", 4, 0, 0, |
| 6370 |
(SCM key, SCM algo, SCM data, SCM signature), |
| 6371 |
"Verify the data @var{signature}.") |
| 6372 |
#define FUNC_NAME s_scm_public_key_verify_data |
| 6373 |
{ |
| 6374 |
gnutls_sign_algorithm_t c_algo = |
| 6375 |
scm_to_gnutls_sign_algorithm (algo, 2, FUNC_NAME); |
| 6376 |
gnutls_pubkey_t c_key = scm_to_gnutls_public_key (key, 1, FUNC_NAME); |
| 6377 |
gnutls_datum_t c_data_d, c_signature_d; |
| 6378 |
const char *c_data, *c_signature; |
| 6379 |
size_t c_data_length, c_signature_length; |
| 6380 |
scm_t_array_handle c_data_handle, c_signature_handle; |
| 6381 |
scm_dynwind_begin (0); |
| 6382 |
c_data = |
| 6383 |
scm_gnutls_get_array (data, &c_data_handle, &c_data_length, FUNC_NAME); |
| 6384 |
scm_dynwind_release_handle (&c_data_handle); |
| 6385 |
c_data_d.data = (unsigned char *) c_data; |
| 6386 |
c_data_d.size = c_data_length; |
| 6387 |
c_signature = |
| 6388 |
scm_gnutls_get_array (signature, &c_signature_handle, &c_signature_length, |
| 6389 |
FUNC_NAME); |
| 6390 |
scm_dynwind_release_handle (&c_signature_handle); |
| 6391 |
c_signature_d.data = (unsigned char *) c_signature; |
| 6392 |
c_signature_d.size = c_signature_length; |
| 6393 |
int error = |
| 6394 |
gnutls_pubkey_verify_data2 (c_key, c_algo, 0, &c_data_d, &c_signature_d); |
| 6395 |
if (EXPECT_FALSE (error)) |
| 6396 |
{ |
| 6397 |
scm_gnutls_error (error, FUNC_NAME); |
| 6398 |
} |
| 6399 |
scm_dynwind_end (); |
| 6400 |
return SCM_UNSPECIFIED; |
| 6401 |
} |
| 6402 |
|
| 6403 |
#undef FUNC_NAME |
| 6404 |
|
| 6405 |
SCM_DEFINE (scm_public_key_verify_hash, "public-key-verify-hash", 4, 0, 0, |
| 6406 |
(SCM key, SCM algo, SCM hash_data, SCM signature), |
| 6407 |
"Verify the hash data @var{signature}.") |
| 6408 |
#define FUNC_NAME s_scm_public_key_verify_hash |
| 6409 |
{ |
| 6410 |
gnutls_sign_algorithm_t c_algo = |
| 6411 |
scm_to_gnutls_sign_algorithm (algo, 2, FUNC_NAME); |
| 6412 |
gnutls_pubkey_t c_key = scm_to_gnutls_public_key (key, 1, FUNC_NAME); |
| 6413 |
gnutls_datum_t c_data_d, c_signature_d; |
| 6414 |
const char *c_data, *c_signature; |
| 6415 |
size_t c_data_length, c_signature_length; |
| 6416 |
scm_t_array_handle c_data_handle, c_signature_handle; |
| 6417 |
scm_dynwind_begin (0); |
| 6418 |
c_data = |
| 6419 |
scm_gnutls_get_array (hash_data, &c_data_handle, &c_data_length, |
| 6420 |
FUNC_NAME); |
| 6421 |
scm_dynwind_release_handle (&c_data_handle); |
| 6422 |
c_data_d.data = (unsigned char *) c_data; |
| 6423 |
c_data_d.size = c_data_length; |
| 6424 |
c_signature = |
| 6425 |
scm_gnutls_get_array (signature, &c_signature_handle, &c_signature_length, |
| 6426 |
FUNC_NAME); |
| 6427 |
scm_dynwind_release_handle (&c_signature_handle); |
| 6428 |
c_signature_d.data = (unsigned char *) c_signature; |
| 6429 |
c_signature_d.size = c_signature_length; |
| 6430 |
int error = |
| 6431 |
gnutls_pubkey_verify_hash2 (c_key, c_algo, 0, &c_data_d, &c_signature_d); |
| 6432 |
if (EXPECT_FALSE (error)) |
| 6433 |
{ |
| 6434 |
scm_gnutls_error (error, FUNC_NAME); |
| 6435 |
} |
| 6436 |
scm_dynwind_end (); |
| 6437 |
return SCM_UNSPECIFIED; |
| 6438 |
} |
| 6439 |
|
| 6440 |
#undef FUNC_NAME |
| 6441 |
|
| 6442 |
SCM_DEFINE (scm_x509_certificate_to_public_key, |
| 6443 |
"x509-certificate->public-key", 1, 0, 0, (SCM crt), |
| 6444 |
"Convert the X509 certificate, @var{crt}, to an abstract public key.") |
| 6445 |
#define FUNC_NAME s_scm_x509_certificate_to_public_key |
| 6446 |
{ |
| 6447 |
gnutls_x509_crt_t c_crt = |
| 6448 |
scm_to_gnutls_x509_certificate (crt, 1, FUNC_NAME); |
| 6449 |
gnutls_pubkey_t c_pub; |
| 6450 |
scm_dynwind_begin (0); |
| 6451 |
int error = gnutls_pubkey_init (&c_pub); |
| 6452 |
SCM pub = SCM_UNSPECIFIED; |
| 6453 |
if (EXPECT_FALSE (error)) |
| 6454 |
{ |
| 6455 |
scm_gnutls_error (error, FUNC_NAME); |
| 6456 |
} |
| 6457 |
else |
| 6458 |
{ |
| 6459 |
scm_dynwind_unwind_handler (do_gnutls_pubkey_deinit, c_pub, 0); |
| 6460 |
error = gnutls_pubkey_import_x509 (c_pub, c_crt, 0); |
| 6461 |
} |
| 6462 |
if (EXPECT_FALSE (error)) |
| 6463 |
{ |
| 6464 |
scm_gnutls_error (error, FUNC_NAME); |
| 6465 |
} |
| 6466 |
else |
| 6467 |
{ |
| 6468 |
pub = scm_from_gnutls_public_key (c_pub); |
| 6469 |
} |
| 6470 |
scm_dynwind_end (); |
| 6471 |
return pub; |
| 6472 |
} |
| 6473 |
|
| 6474 |
#undef FUNC_NAME |
| 6475 |
|
| 6476 |
SCM_DEFINE (scm_x509_private_key_to_private_key, |
| 6477 |
"x509-private-key->private-key", 2, 0, 0, |
| 6478 |
(SCM privkey, SCM flags), |
| 6479 |
"Convert the X509 private key, @var{privkey}, to an abstract private key.") |
| 6480 |
#define FUNC_NAME s_scm_x509_private_key_to_private_key |
| 6481 |
{ |
| 6482 |
unsigned int c_flags = 0; |
| 6483 |
for (c_flags = 0; !scm_is_null (flags); flags = SCM_CDR (flags)) |
| 6484 |
{ |
| 6485 |
c_flags |= |
| 6486 |
((unsigned int) |
| 6487 |
scm_to_gnutls_privkey (SCM_CAR (flags), 4, FUNC_NAME)); |
| 6488 |
} |
| 6489 |
gnutls_x509_privkey_t c_privkey = |
| 6490 |
scm_to_gnutls_x509_private_key (privkey, 1, FUNC_NAME); |
| 6491 |
gnutls_privkey_t c_key; |
| 6492 |
scm_dynwind_begin (0); |
| 6493 |
int error = gnutls_privkey_init (&c_key); |
| 6494 |
SCM key = SCM_UNSPECIFIED; |
| 6495 |
if (EXPECT_FALSE (error)) |
| 6496 |
{ |
| 6497 |
scm_gnutls_error (error, FUNC_NAME); |
| 6498 |
} |
| 6499 |
else |
| 6500 |
{ |
| 6501 |
scm_dynwind_unwind_handler (do_gnutls_privkey_deinit, c_key, 0); |
| 6502 |
error = gnutls_privkey_import_x509 (c_key, c_privkey, c_flags); |
| 6503 |
} |
| 6504 |
if (EXPECT_FALSE (error)) |
| 6505 |
{ |
| 6506 |
scm_gnutls_error (error, FUNC_NAME); |
| 6507 |
} |
| 6508 |
else |
| 6509 |
{ |
| 6510 |
key = scm_from_gnutls_private_key (c_key); |
| 6511 |
} |
| 6512 |
scm_dynwind_end (); |
| 6513 |
return key; |
| 6514 |
} |
| 6515 |
|
| 6516 |
#undef FUNC_NAME |
| 6517 |
|
| 6518 |
SCM_DEFINE (scm_gnutls_random, "gnutls-random", 2, 0, 0, |
| 6519 |
(SCM level, SCM length), |
| 6520 |
"Return a random vector of @var{length} bytes.") |
| 6521 |
#define FUNC_NAME s_scm_gnutls_random |
| 6522 |
{ |
| 6523 |
gnutls_rnd_level_t c_level = |
| 6524 |
scm_to_gnutls_random_level (level, 1, FUNC_NAME); |
| 6525 |
unsigned c_length = scm_to_uint (length); |
| 6526 |
SCM data = scm_c_make_bytevector (c_length); |
| 6527 |
int error = gnutls_rnd (c_level, SCM_BYTEVECTOR_CONTENTS (data), c_length); |
| 6528 |
if (EXPECT_FALSE (error)) |
| 6529 |
{ |
| 6530 |
scm_gnutls_error (error, FUNC_NAME); |
| 6531 |
} |
| 6532 |
return data; |
| 6533 |
} |
| 6534 |
|
| 6535 |
#undef FUNC_NAME |
| 6536 |
|
| 6537 |
|
| 6538 |
|
| 6539 |
|
| 6540 |
/* Initialization. */ |
| 6541 |
|
| 6542 |
void |
| 6543 |
scm_init_gnutls (void) |
| 6544 |
{ |
| 6545 |
#include "core.x" |
| 6546 |
|
| 6547 |
(void) gnutls_global_init (); |
| 6548 |
|
| 6549 |
scm_gnutls_define_enums (); |
| 6550 |
|
| 6551 |
scm_init_gnutls_error (); |
| 6552 |
|
| 6553 |
scm_init_gnutls_session_record_port_type (); |
| 6554 |
|
| 6555 |
weak_refs = scm_make_weak_key_hash_table (scm_from_int (42)); |
| 6556 |
weak_refs = scm_permanent_object (weak_refs); |
| 6557 |
} |