summaryrefslogtreecommitdiff
path: root/lib/gnutls_psk_netconf.c
blob: 6dd0e48d5d87c1a92fb78c0a9e51b177233c80c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2008 Free Software Foundation
 *
 * Author: Simon Josefsson
 *
 * This file is part of GNUTLS.
 *
 * The GNUTLS library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA
 *
 */

/* Functions to support draft-ietf-netconf-tls-01.txt. */

#include <gnutls_int.h>
#include <gnutls_hash_int.h>
#include <gnutls_errors.h>

#ifdef ENABLE_PSK


/**
 * gnutls_psk_netconf_derive_key - derive PSK Netconf key from password
 * @password: zero terminated string containing password.
 * @psk_identity: zero terminated string with PSK identity.
 * @psk_identity_hint: zero terminated string with PSK identity hint.
 * @output_key: output variable, contains newly allocated *data pointer.
 *
 * This function will derive a PSK key from a password, for use with
 * the Netconf protocol.
 *
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
 *
 * Since: 2.4.0
 **/
int
gnutls_psk_netconf_derive_key (const char *password,
			       const char *psk_identity,
			       const char *psk_identity_hint,
			       gnutls_datum_t *output_key)
{
  const char netconf_key_pad[] = "Key Pad for Netconf";
  size_t sha1len = _gnutls_hash_get_algo_len (GNUTLS_DIG_SHA1);
  size_t hintlen = strlen (psk_identity_hint);
  digest_hd_st dig;
  char *inner;
  size_t innerlen;
  int rc;
  /*
   * PSK = SHA-1(SHA-1(psk_identity + "Key Pad for Netconf" + password) +
   *             psk_identity_hint)
   *
   */

  rc = _gnutls_hash_init (&dig, GNUTLS_DIG_SHA1);
  if (rc)
    {
      gnutls_assert ();
      return rc;
    }

  rc = _gnutls_hash (&dig, psk_identity, strlen (psk_identity));
  if (rc)
    {
      gnutls_assert ();
      _gnutls_hash_deinit (&dig, NULL);
      return rc;
    }

  rc = _gnutls_hash (&dig, netconf_key_pad, strlen (netconf_key_pad));
  if (rc)
    {
      gnutls_assert ();
      _gnutls_hash_deinit (&dig, NULL);
      return rc;
    }

  rc = _gnutls_hash (&dig, password, strlen (password));
  if (rc)
    {
      gnutls_assert ();
      _gnutls_hash_deinit (&dig, NULL);
      return rc;
    }

  innerlen = sha1len + hintlen;
  inner = gnutls_malloc (innerlen);
  _gnutls_hash_deinit (&dig, inner);
  if (inner == NULL)
    {
      gnutls_assert ();
      return GNUTLS_E_MEMORY_ERROR;
    }

  memcpy (inner + sha1len, psk_identity_hint, hintlen);

  rc = _gnutls_hash_init (&dig, GNUTLS_DIG_SHA1);
  if (rc)
    {
      gnutls_assert ();
      gnutls_free (inner);
      return rc;
    }

  rc = _gnutls_hash (&dig, inner, innerlen);
  gnutls_free (inner);
  if (rc)
    {
      gnutls_assert ();
      _gnutls_hash_deinit (&dig, NULL);
      return rc;
    }

  output_key->data = gnutls_malloc (sha1len);
  _gnutls_hash_deinit (&dig, output_key->data);
  if (output_key->data == NULL)
    {
      gnutls_assert ();
      return GNUTLS_E_MEMORY_ERROR;
    }
  output_key->size = sha1len;

  return 0;
}

#endif /* ENABLE_PSK */