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. */
93 if (tarindex
>= targsize
)
95 target
[tarindex
] = (pos
- Base64
) << 2;
99 if (tarindex
+ 1 >= targsize
)
101 target
[tarindex
] |= (pos
- Base64
) >> 4;
102 target
[tarindex
+ 1] = ((pos
- Base64
) & 0x0f) << 4;
107 if (tarindex
+ 1 >= targsize
)
109 target
[tarindex
] |= (pos
- Base64
) >> 2;
110 target
[tarindex
+ 1] = ((pos
- Base64
) & 0x03) << 6;
115 if (tarindex
>= targsize
)
117 target
[tarindex
] |= pos
- Base64
;
125 * We are done decoding Base-64 chars. Let's see if we ended
126 * on a byte boundary, and/or with erroneous trailing characters.
129 if (ch
== Pad64
) { /* We got a pad char. */
130 ch
= *src
++; /* Skip it, get next. */
132 case 0: /* Invalid = in first position */
133 case 1: /* Invalid = in second position */
136 case 2: /* Valid, means one byte of info */
137 /* Skip any number of spaces. */
138 for (; ch
!= '\0'; ch
= *src
++)
141 /* Make sure there is another trailing = sign. */
144 ch
= *src
++; /* Skip the = */
145 /* Fall through to "single trailing =" case. */
148 case 3: /* Valid, means two bytes of info */
150 * We know this char is an =. Is there anything but
151 * whitespace after it?
153 for (; ch
!= '\0'; ch
= *src
++)
158 * Now make sure for cases 2 and 3 that the "extra"
159 * bits that slopped past the last full byte were
160 * zeros. If we don't check them, they become a
161 * subliminal channel.
163 if (target
[tarindex
] != 0)
168 * We ended by seeing the end of the string. Make sure we
169 * have no partial bytes lying around.
181 * \param src The buffer to decode.
182 * \param target Result buffer.
183 * \param targsize The length of \a target in bytes.
185 * This is just a simple wrapper for base64_decode() which strips whitespace.
187 * \return The return value of the underlying call to base64_decode().
189 int uudecode(const char *src
, unsigned char *target
, size_t targsize
)
194 /* copy the 'readonly' source */
195 encoded
= para_strdup(src
);
196 /* skip whitespace and data */
197 for (p
= encoded
; *p
== ' ' || *p
== '\t'; p
++)
199 for (; *p
!= '\0' && *p
!= ' ' && *p
!= '\t'; p
++)
201 /* and remove trailing whitespace because base64_decode needs this */
203 len
= base64_decode(encoded
, target
, targsize
);
209 * Read a 4-byte number from a buffer in big-endian format.
211 * \param vp The buffer.
213 * The byte-order of the buffer is expected to be big-endian, unlike read_u32()
214 * of portable_io.h which expects little endian.
216 * \return The 32 bit number given by \a vp.
218 uint32_t read_ssh_u32(const void *vp
)
220 const unsigned char *p
= (const unsigned char *)vp
;
223 v
= (uint32_t)p
[0] << 24;
224 v
|= (uint32_t)p
[1] << 16;
225 v
|= (uint32_t)p
[2] << 8;
232 * Sanity checks for the header of an ssh key.
234 * \param blob The buffer.
235 * \param blen The number of bytes of \a blob.
237 * This performs some checks to make sure we really have an ssh key. It also
238 * computes the offset in bytes of the start of the key values (modulus,
241 * \return The number of bytes to skip until the start of the first encoded
242 * number (usually 11).
244 int check_ssh_key_header(const unsigned char *blob
, int blen
)
246 const unsigned char *p
= blob
, *end
= blob
+ blen
;
250 return -E_SSH_KEY_HEADER
;
251 rlen
= read_ssh_u32(p
);
254 return -E_SSH_KEY_HEADER
;
256 return -E_SSH_KEY_HEADER
;
257 if (rlen
< strlen(KEY_TYPE_TXT
))
258 return -E_SSH_KEY_HEADER
;
259 PARA_DEBUG_LOG("type: %s, rlen: %d\n", p
, rlen
);
260 if (strncmp((char *)p
, KEY_TYPE_TXT
, strlen(KEY_TYPE_TXT
)))
261 return -E_SSH_KEY_HEADER
;
266 * Check existence and permissions of a key file.
268 * \param file The path of the key file.
269 * \param private_key Whether this is a private key.
271 * This checks whether the file exists. If it is a private key, we additionally
272 * check that the permissions are restrictive enough. It is considered an error
273 * if we own the file and it is readable for others.
277 int check_key_file(const char *file
, bool private_key
)
281 if (stat(file
, &st
) != 0)
282 return -ERRNO_TO_PARA_ERROR(errno
);
285 if ((st
.st_uid
== getuid()) && (st
.st_mode
& 077) != 0)
290 void hash_to_asc(unsigned char *hash
, char *asc
)
293 const char hexchar
[] = "0123456789abcdef";
295 for (i
= 0; i
< HASH_SIZE
; i
++) {
296 asc
[2 * i
] = hexchar
[hash
[i
] >> 4];
297 asc
[2 * i
+ 1] = hexchar
[hash
[i
] & 0xf];
299 asc
[2 * HASH_SIZE
] = '\0';
302 int hash_compare(unsigned char *h1
, unsigned char *h2
)
306 for (i
= 0; i
< HASH_SIZE
; i
++) {