paraslash Paraslash Audio Streaming
About   News   Download   Documentation   Development

Macros | Functions
crypt_common.c File Reference

Crypto functions independent of openssl/libgcrypt. More...

#include <regex.h>
#include "para.h"
#include "error.h"
#include "string.h"
#include "crypt.h"
#include "crypt_backend.h"
#include "portable_io.h"
#include "fd.h"
#include "base64.h"

Macros

#define KEY_TYPE_TXT   "ssh-rsa"
 If the key begins with this text, we treat it as an ssh key. More...
 
#define PRIVATE_PEM_KEY_HEADER   "-----BEGIN RSA PRIVATE KEY-----"
 Private PEM keys (legacy format) start with this header. More...
 
#define PRIVATE_OPENSSH_KEY_HEADER   "-----BEGIN OPENSSH PRIVATE KEY-----"
 Private OPENSSH keys (RFC4716) start with this header. More...
 
#define PRIVATE_PEM_KEY_FOOTER   "-----END RSA PRIVATE KEY-----"
 Private PEM keys (legacy format) end with this footer. More...
 
#define PRIVATE_OPENSSH_KEY_FOOTER   "-----END OPENSSH PRIVATE KEY-----"
 Private OPENSSH keys (RFC4716) end with this footer. More...
 

Functions

int decode_public_key (const char *filename, unsigned char **blob, size_t *decoded_size)
 Perform sanity checks and base64-decode an ssh-rsa key. More...
 
int check_private_key_file (const char *file)
 Check existence and permissions of a private key file. More...
 
void hash_to_asc (const unsigned char *hash, char *asc)
 Convert a hash value to ascii format. More...
 
int hash_compare (const unsigned char *h1, const unsigned char *h2)
 Compare two hashes. More...
 
void hash2_to_asc (const unsigned char *hash, char *asc)
 Convert a hash2 value to ascii format. More...
 
int hash2_compare (const unsigned char *h1, const unsigned char *h2)
 Compare two version 2 hashes. More...
 
int find_openssh_bignum_offset (const unsigned char *data, int len)
 Check header of an openssh private key and compute bignum offset. More...
 
int decode_private_key (const char *key_file, unsigned char **result, size_t *blob_size)
 Decode an openssh-v1 (aka RFC4716) or PEM (aka ASN.1) private key. More...
 

Detailed Description

Crypto functions independent of openssl/libgcrypt.

Macro Definition Documentation

◆ KEY_TYPE_TXT

#define KEY_TYPE_TXT   "ssh-rsa"

If the key begins with this text, we treat it as an ssh key.

◆ PRIVATE_PEM_KEY_HEADER

#define PRIVATE_PEM_KEY_HEADER   "-----BEGIN RSA PRIVATE KEY-----"

Private PEM keys (legacy format) start with this header.

◆ PRIVATE_OPENSSH_KEY_HEADER

#define PRIVATE_OPENSSH_KEY_HEADER   "-----BEGIN OPENSSH PRIVATE KEY-----"

Private OPENSSH keys (RFC4716) start with this header.

◆ PRIVATE_PEM_KEY_FOOTER

#define PRIVATE_PEM_KEY_FOOTER   "-----END RSA PRIVATE KEY-----"

Private PEM keys (legacy format) end with this footer.

◆ PRIVATE_OPENSSH_KEY_FOOTER

#define PRIVATE_OPENSSH_KEY_FOOTER   "-----END OPENSSH PRIVATE KEY-----"

Private OPENSSH keys (RFC4716) end with this footer.

Function Documentation

◆ decode_public_key()

int decode_public_key ( const char *  filename,
unsigned char **  blob,
size_t *  decoded_size 
)

Perform sanity checks and base64-decode an ssh-rsa key.

Parameters
filenameThe public key file (usually id_rsa.pub).
blobPointer to base64-decoded blob is returned here.
decoded_sizeThe size of the decoded blob.

The memory pointed at by the returned blob pointer has to be freed by the caller.

Returns
On success, the offset in bytes of the start of the key values (modulus, exponent..). This is the number of bytes to skip from the blob until the start of the first encoded number. On failure, a negative error code is returned.
See also
uudecode().

References mmap_full_file().

Referenced by apc_get_pubkey().

◆ check_private_key_file()

int check_private_key_file ( const char *  file)

Check existence and permissions of a private key file.

Parameters
fileThe path of the key file.

This checks whether the file exists and its permissions are restrictive enough. It is considered an error if we own the file and it is readable for others.

Returns
Standard.

References ERRNO_TO_PARA_ERROR.

Referenced by apc_priv_decrypt().

◆ hash_to_asc()

void hash_to_asc ( const unsigned char *  hash,
char *  asc 
)

Convert a hash value to ascii format.

Parameters
hashthe hash value.
ascResult pointer.

asc must point to an area of at least 2 * HASH_SIZE + 1 bytes which will be filled by the function with the ascii representation of the hash value given by hash, and a terminating NULL byte.

References HASH_SIZE.

◆ hash_compare()

int hash_compare ( const unsigned char *  h1,
const unsigned char *  h2 
)

Compare two hashes.

Parameters
h1Pointer to the first hash value.
h2Pointer to the second hash value.
Returns
1, -1, or zero, depending on whether h1 is greater than, less than or equal to h2, respectively.

References HASH_SIZE.

◆ hash2_to_asc()

void hash2_to_asc ( const unsigned char *  hash,
char *  asc 
)

Convert a hash2 value to ascii format.

Parameters
hashthe hash value.
ascResult pointer.

asc must point to an area of at least 2 * HASH2_SIZE + 1 bytes which will be filled by the function with the ascii representation of the hash value given by hash, and a terminating NULL byte.

References HASH2_SIZE.

◆ hash2_compare()

int hash2_compare ( const unsigned char *  h1,
const unsigned char *  h2 
)

Compare two version 2 hashes.

Parameters
h1Pointer to the first hash value.
h2Pointer to the second hash value.
Returns
1, -1, or zero, depending on whether h1 is greater than, less than or equal to h2, respectively.

References HASH2_SIZE.

◆ find_openssh_bignum_offset()

int find_openssh_bignum_offset ( const unsigned char *  data,
int  len 
)

Check header of an openssh private key and compute bignum offset.

Parameters
dataThe base64-decoded key.
lenThe size of the decoded key.

Several assumptions are made about the key. Most notably, we only support single unencrypted keys without comments.

Returns
The offset at which the first bignum of the private key (the public exponent n) starts. Negative error code on failure.

◆ decode_private_key()

int decode_private_key ( const char *  key_file,
unsigned char **  result,
size_t *  blob_size 
)

Decode an openssh-v1 (aka RFC4716) or PEM (aka ASN.1) private key.

Parameters
key_fileThe private key file (usually id_rsa).
resultPointer to base64-decoded blob is returned here.
blob_sizeThe size of the decoded blob.

This only checks header and footer and base64-decodes the part in between. No attempt to read the decoded part is made.

Returns
Negative on errors, PKT_PEM or PKT_OPENSSH on success, indicating the type of key.

References alloc(), base64_decode(), mmap_full_file(), PARA_INFO_LOG, para_isspace, para_munmap(), PARA_WARNING_LOG, PKT_OPENSSH, PKT_PEM, PRIVATE_OPENSSH_KEY_FOOTER, PRIVATE_OPENSSH_KEY_HEADER, PRIVATE_PEM_KEY_FOOTER, and PRIVATE_PEM_KEY_HEADER.