2 * Copyright (C) 2005-2014 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file crypt_common.c Crypto functions independent of openssl/libgcrypt. */
15 #include "crypt_backend.h"
17 /** If the key begins with this text, we treat it as an ssh key. */
18 #define KEY_TYPE_TXT "ssh-rsa"
21 * Check if given buffer starts with a ssh rsa key signature.
23 * \param data The buffer.
24 * \param size Number of data bytes.
26 * \return Number of header bytes to be skipped on success, zero if
27 * ssh rsa signature was not found.
29 size_t is_ssh_rsa_key(char *data, size_t size)
33 if (size < strlen(KEY_TYPE_TXT) + 2)
35 cp = memchr(data, ' ', size);
38 if (strncmp(KEY_TYPE_TXT, data, strlen(KEY_TYPE_TXT)))
41 if (cp >= data + size)
49 * This base64/uudecode stuff below is taken from openssh-5.2p1, Copyright (c)
50 * 1996 by Internet Software Consortium. Portions Copyright (c) 1995 by
51 * International Business Machines, Inc.
54 static const char Base64[] =
55 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
56 static const char Pad64 = '=';
59 * base64-decode a buffer.
61 * \param src The buffer to decode.
62 * \param target Result is stored here.
63 * \param targsize Number of bytes of \a target.
65 * Skips all whitespace anywhere. Converts characters, four at a time, starting
66 * at (or after) src from base - 64 numbers into three 8 bit bytes in the
69 * \return The number of data bytes stored at the target, -E_BASE64 on errors.
71 int base64_decode(char const *src, unsigned char *target, size_t targsize)
73 unsigned int tarindex, state;
80 while ((ch = *src++) != '\0') {
81 if (para_isspace(ch)) /* Skip whitespace anywhere. */
87 pos = strchr(Base64, ch);
88 if (pos == NULL) /* A non-base64 character. */
94 if (tarindex >= targsize)
96 target[tarindex] = (pos - Base64) << 2;
102 if (tarindex + 1 >= targsize)
104 target[tarindex] |= (pos - Base64) >> 4;
105 target[tarindex+1] = ((pos - Base64) & 0x0f)
113 if (tarindex + 1 >= targsize)
115 target[tarindex] |= (pos - Base64) >> 2;
116 target[tarindex+1] = ((pos - Base64) & 0x03)
124 if (tarindex >= targsize)
126 target[tarindex] |= (pos - Base64);
135 * We are done decoding Base-64 chars. Let's see if we ended
136 * on a byte boundary, and/or with erroneous trailing characters.
139 if (ch == Pad64) { /* We got a pad char. */
140 ch = *src++; /* Skip it, get next. */
142 case 0: /* Invalid = in first position */
143 case 1: /* Invalid = in second position */
146 case 2: /* Valid, means one byte of info */
147 /* Skip any number of spaces. */
148 for (; ch != '\0'; ch = *src++)
151 /* Make sure there is another trailing = sign. */
154 ch = *src++; /* Skip the = */
155 /* Fall through to "single trailing =" case. */
158 case 3: /* Valid, means two bytes of info */
160 * We know this char is an =. Is there anything but
161 * whitespace after it?
163 for (; ch != '\0'; ch = *src++)
168 * Now make sure for cases 2 and 3 that the "extra"
169 * bits that slopped past the last full byte were
170 * zeros. If we don't check them, they become a
171 * subliminal channel.
173 if (target && target[tarindex] != 0)
178 * We ended by seeing the end of the string. Make sure we
179 * have no partial bytes lying around.
191 * \param src The buffer to decode.
192 * \param target Result buffer.
193 * \param targsize The length of \a target in bytes.
195 * This is just a simple wrapper for base64_decode() which strips whitespace.
197 * \return The return value of the underlying call to base64_decode().
199 int uudecode(const char *src, unsigned char *target, size_t targsize)
204 /* copy the 'readonly' source */
205 encoded = para_strdup(src);
206 /* skip whitespace and data */
207 for (p = encoded; *p == ' ' || *p == '\t'; p++)
209 for (; *p != '\0' && *p != ' ' && *p != '\t'; p++)
211 /* and remove trailing whitespace because base64_decode needs this */
213 len = base64_decode(encoded, target, targsize);
219 * Read a 4-byte number from a buffer in big-endian format.
221 * \param vp The buffer.
223 * The byte-order of the buffer is expected to be big-endian, unlike read_u32()
224 * of portable_io.h which expects little endian.
226 * \return The 32 bit number given by \a vp.
228 uint32_t read_ssh_u32(const void *vp)
230 const unsigned char *p = (const unsigned char *)vp;
233 v = (uint32_t)p[0] << 24;
234 v |= (uint32_t)p[1] << 16;
235 v |= (uint32_t)p[2] << 8;
242 * Sanity checks for the header of an ssh key.
244 * \param blob The buffer.
245 * \param blen The number of bytes of \a blob.
247 * This performs some checks to make sure we really have an ssh key. It also
248 * computes the offset in bytes of the start of the key values (modulus,
251 * \return The number of bytes to skip until the start of the first encoded
252 * number (usually 11).
254 int check_ssh_key_header(const unsigned char *blob, int blen)
256 const unsigned char *p = blob, *end = blob + blen;
260 return -E_SSH_KEY_HEADER;
261 rlen = read_ssh_u32(p);
264 return -E_SSH_KEY_HEADER;
266 return -E_SSH_KEY_HEADER;
267 if (rlen < strlen(KEY_TYPE_TXT))
268 return -E_SSH_KEY_HEADER;
269 PARA_DEBUG_LOG("type: %s, rlen: %d\n", p, rlen);
270 if (strncmp((char *)p, KEY_TYPE_TXT, strlen(KEY_TYPE_TXT)))
271 return -E_SSH_KEY_HEADER;
276 * Check existence and permissions of a key file.
278 * \param file The path of the key file.
279 * \param private_key Whether this is a private key.
281 * This checks whether the file exists. If it is a private key, we additionally
282 * check that the permissions are restrictive enough. It is considered an error
283 * if we own the file and it is readable for others.
287 int check_key_file(const char *file, bool private_key)
291 if (stat(file, &st) != 0)
292 return -ERRNO_TO_PARA_ERROR(errno);
295 if ((st.st_uid == getuid()) && (st.st_mode & 077) != 0)
300 void hash_to_asc(unsigned char *hash, char *asc)
303 const char hexchar[] = "0123456789abcdef";
305 for (i = 0; i < HASH_SIZE; i++) {
306 asc[2 * i] = hexchar[hash[i] >> 4];
307 asc[2 * i + 1] = hexchar[hash[i] & 0xf];
309 asc[2 * HASH_SIZE] = '\0';
312 int hash_compare(unsigned char *h1, unsigned char *h2)
316 for (i = 0; i < HASH_SIZE; i++) {