aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/https/x509/pkcs12_encr.c
blob: e534489caba3e34a06727a0aa372d4893e944c1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* minip12.c - A mini pkcs-12 implementation (modified for gnutls)
 *
 * Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
 *
 * 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
 *
 */

#include <gnutls_int.h>

#ifdef ENABLE_PKI

#include <gcrypt.h>
#include <gc.h>
#include <gnutls_errors.h>

/* Returns 0 if the password is ok, or a negative error
 * code instead.
 */
static int
MHD_pkcs12_check_pass (const char *pass, size_t plen)
{
  const char *p = pass;
  unsigned int i;

  for (i = 0; i < plen; i++)
    {
      if (isascii (p[i]))
        continue;
      return GNUTLS_E_INVALID_PASSWORD;
    }

  return 0;
}

/* ID should be:
 * 3 for MAC
 * 2 for IV
 * 1 for encryption key
 */
int
MHD_pkcs12_string_to_key (unsigned int id, const opaque * salt,
                          unsigned int salt_size, unsigned int iter,
                          const char *pw, unsigned int req_keylen,
                          opaque * keybuf)
{
  int rc;
  unsigned int i, j;
  MHD_gc_hash_handle md;
  mpi_t num_b1 = NULL;
  unsigned int pwlen;
  opaque hash[20], buf_b[64], buf_i[128], *p;
  size_t cur_keylen;
  size_t n;

  cur_keylen = 0;

  if (pw == NULL)
    pwlen = 0;
  else
    pwlen = strlen (pw);

  if (pwlen > 63 / 2)
    {
      MHD_gnutls_assert ();
      return GNUTLS_E_INVALID_REQUEST;
    }

  if ((rc = MHD_pkcs12_check_pass (pw, pwlen)) < 0)
    {
      MHD_gnutls_assert ();
      return rc;
    }

  /* Store salt and password in BUF_I */
  p = buf_i;
  for (i = 0; i < 64; i++)
    *p++ = salt[i % salt_size];
  if (pw)
    {
      for (i = j = 0; i < 64; i += 2)
        {
          *p++ = 0;
          *p++ = pw[j];
          if (++j > pwlen)      /* Note, that we include the trailing zero */
            j = 0;
        }
    }
  else
    memset (p, 0, 64);

  for (;;)
    {
      rc = MHD_gc_hash_open (GC_SHA1, 0, &md);
      if (rc)
        {
          MHD_gnutls_assert ();
          return GNUTLS_E_DECRYPTION_FAILED;
        }
      for (i = 0; i < 64; i++)
        {
          unsigned char lid = id & 0xFF;
          MHD_gc_hash_write (md, 1, (const char *) &lid);
        }
      MHD_gc_hash_write (md, pw ? 128 : 64, (const char *) buf_i);
      memcpy (hash, MHD_gc_hash_read (md), 20);
      MHD_gc_hash_close (md);
      for (i = 1; i < iter; i++)
        MHD_gc_hash_buffer (GC_SHA1, hash, 20, (char *) hash);
      for (i = 0; i < 20 && cur_keylen < req_keylen; i++)
        keybuf[cur_keylen++] = hash[i];
      if (cur_keylen == req_keylen)
        {
          gcry_mpi_release (num_b1);
          return 0;             /* ready */
        }

      /* need more bytes. */
      for (i = 0; i < 64; i++)
        buf_b[i] = hash[i % 20];
      n = 64;
      rc = MHD_gtls_mpi_scan (&num_b1, buf_b, &n);
      if (rc < 0)
        {
          MHD_gnutls_assert ();
          return rc;
        }
      gcry_mpi_add_ui (num_b1, num_b1, 1);
      for (i = 0; i < 128; i += 64)
        {
          mpi_t num_ij;

          n = 64;
          rc = MHD_gtls_mpi_scan (&num_ij, buf_i + i, &n);
          if (rc < 0)
            {
              MHD_gnutls_assert ();
              return rc;
            }
          gcry_mpi_add (num_ij, num_ij, num_b1);
          gcry_mpi_clear_highbit (num_ij, 64 * 8);
          n = 64;
          rc = MHD_gtls_mpi_print (buf_i + i, &n, num_ij);
          if (rc < 0)
            {
              MHD_gnutls_assert ();
              return rc;
            }
          gcry_mpi_release (num_ij);
        }
    }
}

#endif /* ENABLE_PKI */