Initial import.
This commit is contained in:
commit
3210f38e04
292 changed files with 58526 additions and 0 deletions
87
tools/brssl.c
Normal file
87
tools/brssl.c
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "brssl.h"
|
||||
#include "bearssl.h"
|
||||
|
||||
static void
|
||||
usage(void)
|
||||
{
|
||||
fprintf(stderr, "usage: brssl command [ options ]\n");
|
||||
fprintf(stderr, "available commands:\n");
|
||||
fprintf(stderr, " client run SSL client\n");
|
||||
fprintf(stderr, " server run SSL server\n");
|
||||
fprintf(stderr, " verify verify certificate chain\n");
|
||||
fprintf(stderr, " skey decode private key\n");
|
||||
fprintf(stderr, " ta decode trust anchors\n");
|
||||
fprintf(stderr, " chain make C code for certificate chains\n");
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
char *cmd;
|
||||
|
||||
if (argc < 2) {
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
cmd = argv[1];
|
||||
if (eqstr(cmd, "client")) {
|
||||
if (do_client(argc - 2, argv + 2) < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
} else if (eqstr(cmd, "server")) {
|
||||
if (do_server(argc - 2, argv + 2) < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
} else if (eqstr(cmd, "verify")) {
|
||||
if (do_verify(argc - 2, argv + 2) < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
} else if (eqstr(cmd, "skey")) {
|
||||
if (do_skey(argc - 2, argv + 2) < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
} else if (eqstr(cmd, "ta")) {
|
||||
if (do_ta(argc - 2, argv + 2) < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
} else if (eqstr(cmd, "chain")) {
|
||||
if (do_chain(argc - 2, argv + 2) < 0) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "unknown command: '%s'\n", cmd);
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
465
tools/brssl.h
Normal file
465
tools/brssl.h
Normal file
|
|
@ -0,0 +1,465 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef BRSSL_H__
|
||||
#define BRSSL_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "bearssl.h"
|
||||
|
||||
/*
|
||||
* malloc() wrapper:
|
||||
* -- If len is 0, then NULL is returned.
|
||||
* -- If len is non-zero, and allocation fails, then an error message is
|
||||
* printed and the process exits with an error code.
|
||||
*/
|
||||
void *xmalloc(size_t len);
|
||||
|
||||
/*
|
||||
* free() wrapper, meant to release blocks allocated with xmalloc().
|
||||
*/
|
||||
void xfree(void *buf);
|
||||
|
||||
/*
|
||||
* Duplicate a character string into a newly allocated block.
|
||||
*/
|
||||
char *xstrdup(const void *src);
|
||||
|
||||
/*
|
||||
* Allocate a new block with the provided length, filled with a copy
|
||||
* of exactly that many bytes starting at address 'src'.
|
||||
*/
|
||||
void *xblobdup(const void *src, size_t len);
|
||||
|
||||
/*
|
||||
* Duplicate a public key, into newly allocated blocks. The returned
|
||||
* key must be later on released with xfreepkey().
|
||||
*/
|
||||
br_x509_pkey *xpkeydup(const br_x509_pkey *pk);
|
||||
|
||||
/*
|
||||
* Release a public key that was allocated with xpkeydup(). If pk is NULL,
|
||||
* this function does nothing.
|
||||
*/
|
||||
void xfreepkey(br_x509_pkey *pk);
|
||||
|
||||
/*
|
||||
* Macros for growable arrays.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Make a structure type for a vector of 'type'.
|
||||
*/
|
||||
#define VECTOR(type) struct { \
|
||||
type *buf; \
|
||||
size_t ptr, len; \
|
||||
}
|
||||
|
||||
/*
|
||||
* Constant initialiser for a vector.
|
||||
*/
|
||||
#define VEC_INIT { 0, 0, 0 }
|
||||
|
||||
/*
|
||||
* Clear a vector.
|
||||
*/
|
||||
#define VEC_CLEAR(vec) do { \
|
||||
xfree((vec).buf); \
|
||||
(vec).buf = NULL; \
|
||||
(vec).ptr = 0; \
|
||||
(vec).len = 0; \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* Clear a vector, first calling the provided function on each vector
|
||||
* element.
|
||||
*/
|
||||
#define VEC_CLEAREXT(vec, fun) do { \
|
||||
size_t vec_tmp; \
|
||||
for (vec_tmp = 0; vec_tmp < (vec).ptr; vec_tmp ++) { \
|
||||
(fun)(&(vec).buf[vec_tmp]); \
|
||||
} \
|
||||
VEC_CLEAR(vec); \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* Add a value at the end of a vector.
|
||||
*/
|
||||
#define VEC_ADD(vec, x) do { \
|
||||
(vec).buf = vector_expand((vec).buf, sizeof *((vec).buf), \
|
||||
&(vec).ptr, &(vec).len, 1); \
|
||||
(vec).buf[(vec).ptr ++] = (x); \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* Add several values at the end of a vector.
|
||||
*/
|
||||
#define VEC_ADDMANY(vec, xp, num) do { \
|
||||
size_t vec_num = (num); \
|
||||
(vec).buf = vector_expand((vec).buf, sizeof *((vec).buf), \
|
||||
&(vec).ptr, &(vec).len, vec_num); \
|
||||
memcpy((vec).buf + (vec).ptr, \
|
||||
(xp), vec_num * sizeof *((vec).buf)); \
|
||||
(vec).ptr += vec_num; \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* Access a vector element by index. This is a lvalue, and can be modified.
|
||||
*/
|
||||
#define VEC_ELT(vec, idx) ((vec).buf[idx])
|
||||
|
||||
/*
|
||||
* Get current vector length.
|
||||
*/
|
||||
#define VEC_LEN(vec) ((vec).ptr)
|
||||
|
||||
/*
|
||||
* Copy all vector elements into a newly allocated block.
|
||||
*/
|
||||
#define VEC_TOARRAY(vec) xblobdup((vec).buf, sizeof *((vec).buf) * (vec).ptr)
|
||||
|
||||
/*
|
||||
* Internal function used to handle memory allocations for vectors.
|
||||
*/
|
||||
void *vector_expand(void *buf,
|
||||
size_t esize, size_t *ptr, size_t *len, size_t extra);
|
||||
|
||||
/*
|
||||
* Type for a vector of bytes.
|
||||
*/
|
||||
typedef VECTOR(unsigned char) bvector;
|
||||
|
||||
/*
|
||||
* Compare two strings for equality; returned value is 1 if the strings
|
||||
* are to be considered equal, 0 otherwise. Comparison is case-insensitive
|
||||
* (ASCII letters only) and skips some characters (all whitespace, defined
|
||||
* as ASCII codes 0 to 32 inclusive, and also '-', '_', '.', '/', '+' and
|
||||
* ':').
|
||||
*/
|
||||
int eqstr(const char *s1, const char *s2);
|
||||
|
||||
/*
|
||||
* Structure for a known protocol version.
|
||||
*/
|
||||
typedef struct {
|
||||
const char *name;
|
||||
unsigned version;
|
||||
const char *comment;
|
||||
} protocol_version;
|
||||
|
||||
/*
|
||||
* Known protocol versions. Last element has a NULL name.
|
||||
*/
|
||||
extern const protocol_version protocol_versions[];
|
||||
|
||||
/*
|
||||
* Parse a version name. If the name is not recognized, then an error
|
||||
* message is printed, and 0 is returned.
|
||||
*/
|
||||
unsigned parse_version(const char *name, size_t len);
|
||||
|
||||
/*
|
||||
* Type for a known hash function.
|
||||
*/
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const br_hash_class *hclass;
|
||||
const char *comment;
|
||||
} hash_function;
|
||||
|
||||
/*
|
||||
* Known hash functions. Last element has a NULL name.
|
||||
*/
|
||||
extern const hash_function hash_functions[];
|
||||
|
||||
/*
|
||||
* Parse hash function names. This function expects a comma-separated
|
||||
* list of names, and returns a bit mask corresponding to the matched
|
||||
* names. If one of the name does not match, or the list is empty, then
|
||||
* an error message is printed, and 0 is returned.
|
||||
*/
|
||||
unsigned parse_hash_functions(const char *arg);
|
||||
|
||||
/*
|
||||
* Type for a known cipher suite.
|
||||
*/
|
||||
typedef struct {
|
||||
const char *name;
|
||||
uint16_t suite;
|
||||
unsigned req;
|
||||
const char *comment;
|
||||
} cipher_suite;
|
||||
|
||||
/*
|
||||
* Known cipher suites. Last element has a NULL name.
|
||||
*/
|
||||
extern const cipher_suite cipher_suites[];
|
||||
|
||||
/*
|
||||
* Flags for cipher suite requirements.
|
||||
*/
|
||||
#define REQ_TLS12 0x0001 /* suite needs TLS 1.2 */
|
||||
#define REQ_SHA1 0x0002 /* suite needs SHA-1 */
|
||||
#define REQ_SHA256 0x0004 /* suite needs SHA-256 */
|
||||
#define REQ_SHA384 0x0008 /* suite needs SHA-384 */
|
||||
#define REQ_AESCBC 0x0010 /* suite needs AES/CBC encryption */
|
||||
#define REQ_AESGCM 0x0020 /* suite needs AES/GCM encryption */
|
||||
#define REQ_3DESCBC 0x0040 /* suite needs 3DES/CBC encryption */
|
||||
#define REQ_RSAKEYX 0x0080 /* suite uses RSA key exchange */
|
||||
#define REQ_ECDHE_RSA 0x0100 /* suite uses ECDHE_RSA key exchange */
|
||||
#define REQ_ECDHE_ECDSA 0x0200 /* suite uses ECDHE_ECDSA key exchange */
|
||||
#define REQ_ECDH 0x0400 /* suite uses static ECDH key exchange */
|
||||
|
||||
/*
|
||||
* Parse a list of cipher suite names. The names are comma-separated. If
|
||||
* one of the name is not recognised, or the list is empty, then an
|
||||
* appropriate error message is printed, and NULL is returned.
|
||||
* The returned array is allocated with xmalloc() and must be released
|
||||
* by the caller. That array is terminated with a dummy entry whose 'name'
|
||||
* field is NULL. The number of entries (not counting the dummy entry)
|
||||
* is also written into '*num'.
|
||||
*/
|
||||
cipher_suite *parse_suites(const char *arg, size_t *num);
|
||||
|
||||
/*
|
||||
* Get the name of a cipher suite. Returned value is NULL if the suite is
|
||||
* not recognized.
|
||||
*/
|
||||
const char *get_suite_name(unsigned suite);
|
||||
|
||||
/*
|
||||
* Get the name of a cipher suite. The name is written in the provided
|
||||
* buffer; if the suite is not recognised, then the name is
|
||||
* "unknown (0x****)" where "****" is the hexadecimal value of the suite.
|
||||
* If the name does not fit in the provided buffer, then dst[0] is set
|
||||
* to 0 (unless len is 0, in which case nothing is written), and -1 is
|
||||
* returned. Otherwise, the name is written in dst[] (with a terminating
|
||||
* 0), and this function returns 0.
|
||||
*/
|
||||
int get_suite_name_ext(unsigned suite, char *dst, size_t len);
|
||||
|
||||
/*
|
||||
* Print out all known names (for protocol versions, cipher suites...).
|
||||
*/
|
||||
void list_names(void);
|
||||
|
||||
/*
|
||||
* Get the symbolic name for an elliptic curve (by ID).
|
||||
*/
|
||||
const char *ec_curve_name(int curve);
|
||||
|
||||
/*
|
||||
* Read a file completely. The returned block is allocated with xmalloc()
|
||||
* and must be released by the caller.
|
||||
* If the file cannot be found or read completely, or is empty, then an
|
||||
* appropriate error message is written, and NULL is returned.
|
||||
*/
|
||||
unsigned char *read_file(const char *fname, size_t *len);
|
||||
|
||||
/*
|
||||
* Write a file completely. This returns 0 on success, -1 on error. On
|
||||
* error, an appropriate error message is printed.
|
||||
*/
|
||||
int write_file(const char *fname, const void *data, size_t len);
|
||||
|
||||
/*
|
||||
* This function returns non-zero if the provided buffer "looks like"
|
||||
* a DER-encoded ASN.1 object (criteria: it has the tag for a SEQUENCE
|
||||
* with a definite length that matches the total object length).
|
||||
*/
|
||||
int looks_like_DER(const unsigned char *buf, size_t len);
|
||||
|
||||
/*
|
||||
* Type for a named blob (the 'name' is a normalised PEM header name).
|
||||
*/
|
||||
typedef struct {
|
||||
char *name;
|
||||
unsigned char *data;
|
||||
size_t data_len;
|
||||
} pem_object;
|
||||
|
||||
/*
|
||||
* Release the contents of a named blob (buffer and name).
|
||||
*/
|
||||
void free_pem_object_contents(pem_object *po);
|
||||
|
||||
/*
|
||||
* Decode a buffer as a PEM file, and return all objects. On error, NULL
|
||||
* is returned and an error message is printed. Absence of any object
|
||||
* is an error.
|
||||
*
|
||||
* The returned array is terminated by a dummy object whose 'name' is
|
||||
* NULL. The number of objects (not counting the dummy terminator) is
|
||||
* written in '*num'.
|
||||
*/
|
||||
pem_object *decode_pem(const void *src, size_t len, size_t *num);
|
||||
|
||||
/*
|
||||
* Get the certificate(s) from a file. This accepts both a single
|
||||
* DER-encoded certificate, and a text file that contains
|
||||
* PEM-encoded certificates (and possibly other objects, which are
|
||||
* then ignored).
|
||||
*
|
||||
* On decoding error, or if the file turns out to contain no certificate
|
||||
* at all, then an error message is printed and NULL is returned.
|
||||
*
|
||||
* The returned array, and all referenced buffers, are allocated with
|
||||
* xmalloc() and must be released by the caller. The returned array
|
||||
* ends with a dummy entry whose 'data' field is NULL.
|
||||
* The number of decoded certificates (not counting the dummy entry)
|
||||
* is written into '*num'.
|
||||
*/
|
||||
br_x509_certificate *read_certificates(const char *fname, size_t *num);
|
||||
|
||||
/*
|
||||
* Interpret a certificate as a trust anchor. The trust anchor is
|
||||
* newly allocated with xmalloc() and the caller must release it.
|
||||
* On decoding error, an error message is printed, and this function
|
||||
* returns NULL.
|
||||
*/
|
||||
br_x509_trust_anchor *certificate_to_trust_anchor(br_x509_certificate *xc);
|
||||
|
||||
/*
|
||||
* Type for a vector of trust anchors.
|
||||
*/
|
||||
typedef VECTOR(br_x509_trust_anchor) anchor_list;
|
||||
|
||||
/*
|
||||
* Release contents for a trust anchor (assuming they were dynamically
|
||||
* allocated with xmalloc()). The structure itself is NOT released.
|
||||
*/
|
||||
void free_ta_contents(br_x509_trust_anchor *ta);
|
||||
|
||||
/*
|
||||
* Decode certificates from a file and interpret them as trust anchors.
|
||||
* The trust anchors are added to the provided list. The number of found
|
||||
* anchors is returned; on error, 0 is returned (finding no anchor at
|
||||
* all is considered an error). An appropriate error message is displayed.
|
||||
*/
|
||||
size_t read_trust_anchors(anchor_list *dst, const char *fname);
|
||||
|
||||
/*
|
||||
* Special "no anchor" X.509 validator that wraps around another X.509
|
||||
* validator and turns "not trusted" error codes into success. This is
|
||||
* by definition insecure, but convenient for debug purposes.
|
||||
*/
|
||||
typedef struct {
|
||||
const br_x509_class *vtable;
|
||||
const br_x509_class **inner;
|
||||
} x509_noanchor_context;
|
||||
extern const br_x509_class x509_noanchor_vtable;
|
||||
|
||||
/*
|
||||
* Initialise a "no anchor" X.509 validator.
|
||||
*/
|
||||
void x509_noanchor_init(x509_noanchor_context *xwc,
|
||||
const br_x509_class **inner);
|
||||
|
||||
/*
|
||||
* Aggregate type for a private key.
|
||||
*/
|
||||
typedef struct {
|
||||
int key_type; /* BR_KEYTYPE_RSA or BR_KEYTYPE_EC */
|
||||
union {
|
||||
br_rsa_private_key rsa;
|
||||
br_ec_private_key ec;
|
||||
} key;
|
||||
} private_key;
|
||||
|
||||
/*
|
||||
* Decode a private key from a file. On error, this prints an error
|
||||
* message and returns NULL.
|
||||
*/
|
||||
private_key *read_private_key(const char *fname);
|
||||
|
||||
/*
|
||||
* Free a private key.
|
||||
*/
|
||||
void free_private_key(private_key *sk);
|
||||
|
||||
/*
|
||||
* Find the symbolic name and the description for an error. If 'err' is
|
||||
* recognised then the error symbolic name is returned; if 'comment' is
|
||||
* not NULL then '*comment' is then set to a descriptive human-readable
|
||||
* message. If the error code 'err' is not recognised, then '*comment' is
|
||||
* untouched and this function returns NULL.
|
||||
*/
|
||||
const char *find_error_name(int err, const char **comment);
|
||||
|
||||
/*
|
||||
* Run a SSL engine, with a socket connected to the peer, and using
|
||||
* stdin/stdout to exchange application data.
|
||||
*
|
||||
* Returned value:
|
||||
* 0 SSL connection closed successfully
|
||||
* x > 0 SSL error "x"
|
||||
* -1 early socket close
|
||||
* -2 stdout was closed, or something failed badly
|
||||
*/
|
||||
int run_ssl_engine(br_ssl_engine_context *eng, int fd, unsigned flags);
|
||||
|
||||
#define RUN_ENGINE_VERBOSE 0x0001 /* enable verbose messages */
|
||||
#define RUN_ENGINE_TRACE 0x0002 /* hex dump of records */
|
||||
|
||||
/*
|
||||
* Do the "client" command. Returned value is 0 on success, -1 on failure.
|
||||
* Command-line arguments start _after_ the command name.
|
||||
*/
|
||||
int do_client(int argc, char *argv[]);
|
||||
|
||||
/*
|
||||
* Do the "server" command. Returned value is 0 on success, -1 on failure.
|
||||
* Command-line arguments start _after_ the command name.
|
||||
*/
|
||||
int do_server(int argc, char *argv[]);
|
||||
|
||||
/*
|
||||
* Do the "verify" command. Returned value is 0 on success, -1 on failure.
|
||||
* Command-line arguments start _after_ the command name.
|
||||
*/
|
||||
int do_verify(int argc, char *argv[]);
|
||||
|
||||
/*
|
||||
* Do the "skey" command. Returned value is 0 on success, -1 on failure.
|
||||
* Command-line arguments start _after_ the command name.
|
||||
*/
|
||||
int do_skey(int argc, char *argv[]);
|
||||
|
||||
/*
|
||||
* Do the "ta" command. Returned value is 0 on success, -1 on failure.
|
||||
* Command-line arguments start _after_ the command name.
|
||||
*/
|
||||
int do_ta(int argc, char *argv[]);
|
||||
|
||||
/*
|
||||
* Do the "chain" command. Returned value is 0 on success, -1 on failure.
|
||||
* Command-line arguments start _after_ the command name.
|
||||
*/
|
||||
int do_chain(int argc, char *argv[]);
|
||||
|
||||
#endif
|
||||
218
tools/certs.c
Normal file
218
tools/certs.c
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "brssl.h"
|
||||
|
||||
static void
|
||||
dn_append(void *ctx, const void *buf, size_t len)
|
||||
{
|
||||
VEC_ADDMANY(*(bvector *)ctx, buf, len);
|
||||
}
|
||||
|
||||
static int
|
||||
certificate_to_trust_anchor_inner(br_x509_trust_anchor *ta,
|
||||
br_x509_certificate *xc)
|
||||
{
|
||||
br_x509_decoder_context dc;
|
||||
bvector vdn = VEC_INIT;
|
||||
br_x509_pkey *pk;
|
||||
|
||||
br_x509_decoder_init(&dc, dn_append, &vdn);
|
||||
br_x509_decoder_push(&dc, xc->data, xc->data_len);
|
||||
pk = br_x509_decoder_get_pkey(&dc);
|
||||
if (pk == NULL) {
|
||||
fprintf(stderr, "ERROR: CA decoding failed with error %d\n",
|
||||
br_x509_decoder_last_error(&dc));
|
||||
VEC_CLEAR(vdn);
|
||||
return -1;
|
||||
}
|
||||
ta->dn = VEC_TOARRAY(vdn);
|
||||
ta->dn_len = VEC_LEN(vdn);
|
||||
VEC_CLEAR(vdn);
|
||||
ta->flags = 0;
|
||||
if (br_x509_decoder_isCA(&dc)) {
|
||||
ta->flags |= BR_X509_TA_CA;
|
||||
}
|
||||
switch (pk->key_type) {
|
||||
case BR_KEYTYPE_RSA:
|
||||
ta->pkey.key_type = BR_KEYTYPE_RSA;
|
||||
ta->pkey.key.rsa.n = xblobdup(pk->key.rsa.n, pk->key.rsa.nlen);
|
||||
ta->pkey.key.rsa.nlen = pk->key.rsa.nlen;
|
||||
ta->pkey.key.rsa.e = xblobdup(pk->key.rsa.e, pk->key.rsa.elen);
|
||||
ta->pkey.key.rsa.elen = pk->key.rsa.elen;
|
||||
break;
|
||||
case BR_KEYTYPE_EC:
|
||||
ta->pkey.key_type = BR_KEYTYPE_EC;
|
||||
ta->pkey.key.ec.curve = pk->key.ec.curve;
|
||||
ta->pkey.key.ec.q = xblobdup(pk->key.ec.q, pk->key.ec.qlen);
|
||||
ta->pkey.key.ec.qlen = pk->key.ec.qlen;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "ERROR: unsupported public key type in CA\n");
|
||||
xfree(ta->dn);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
br_x509_trust_anchor *
|
||||
certificate_to_trust_anchor(br_x509_certificate *xc)
|
||||
{
|
||||
br_x509_trust_anchor ta;
|
||||
|
||||
if (certificate_to_trust_anchor_inner(&ta, xc) < 0) {
|
||||
return NULL;
|
||||
} else {
|
||||
return xblobdup(&ta, sizeof ta);
|
||||
}
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
void
|
||||
free_ta_contents(br_x509_trust_anchor *ta)
|
||||
{
|
||||
xfree(ta->dn);
|
||||
switch (ta->pkey.key_type) {
|
||||
case BR_KEYTYPE_RSA:
|
||||
xfree(ta->pkey.key.rsa.n);
|
||||
xfree(ta->pkey.key.rsa.e);
|
||||
break;
|
||||
case BR_KEYTYPE_EC:
|
||||
xfree(ta->pkey.key.ec.q);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
size_t
|
||||
read_trust_anchors(anchor_list *dst, const char *fname)
|
||||
{
|
||||
br_x509_certificate *xcs;
|
||||
anchor_list tas = VEC_INIT;
|
||||
size_t u, num;
|
||||
|
||||
xcs = read_certificates(fname, &num);
|
||||
if (xcs == NULL) {
|
||||
return 0;
|
||||
}
|
||||
for (u = 0; u < num; u ++) {
|
||||
br_x509_trust_anchor ta;
|
||||
|
||||
if (certificate_to_trust_anchor_inner(&ta, &xcs[u]) < 0) {
|
||||
VEC_CLEAREXT(tas, free_ta_contents);
|
||||
return 0;
|
||||
}
|
||||
VEC_ADD(tas, ta);
|
||||
}
|
||||
VEC_ADDMANY(*dst, &VEC_ELT(tas, 0), num);
|
||||
VEC_CLEAR(tas);
|
||||
return num;
|
||||
}
|
||||
|
||||
static void
|
||||
xwc_start_chain(const br_x509_class **ctx,
|
||||
unsigned expected_key_type, const char *server_name)
|
||||
{
|
||||
x509_noanchor_context *xwc;
|
||||
|
||||
xwc = (x509_noanchor_context *)ctx;
|
||||
(*xwc->inner)->start_chain(xwc->inner,
|
||||
expected_key_type, server_name);
|
||||
}
|
||||
|
||||
static void
|
||||
xwc_start_cert(const br_x509_class **ctx, uint32_t length)
|
||||
{
|
||||
x509_noanchor_context *xwc;
|
||||
|
||||
xwc = (x509_noanchor_context *)ctx;
|
||||
(*xwc->inner)->start_cert(xwc->inner, length);
|
||||
}
|
||||
|
||||
static void
|
||||
xwc_append(const br_x509_class **ctx, const unsigned char *buf, size_t len)
|
||||
{
|
||||
x509_noanchor_context *xwc;
|
||||
|
||||
xwc = (x509_noanchor_context *)ctx;
|
||||
(*xwc->inner)->append(xwc->inner, buf, len);
|
||||
}
|
||||
|
||||
static void
|
||||
xwc_end_cert(const br_x509_class **ctx)
|
||||
{
|
||||
x509_noanchor_context *xwc;
|
||||
|
||||
xwc = (x509_noanchor_context *)ctx;
|
||||
(*xwc->inner)->end_cert(xwc->inner);
|
||||
}
|
||||
|
||||
static unsigned
|
||||
xwc_end_chain(const br_x509_class **ctx)
|
||||
{
|
||||
x509_noanchor_context *xwc;
|
||||
unsigned r;
|
||||
|
||||
xwc = (x509_noanchor_context *)ctx;
|
||||
r = (*xwc->inner)->end_chain(xwc->inner);
|
||||
if (r == BR_ERR_X509_NOT_TRUSTED) {
|
||||
r = 0;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static const br_x509_pkey *
|
||||
xwc_get_pkey(const br_x509_class *const *ctx)
|
||||
{
|
||||
x509_noanchor_context *xwc;
|
||||
|
||||
xwc = (x509_noanchor_context *)ctx;
|
||||
return (*xwc->inner)->get_pkey(xwc->inner);
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
const br_x509_class x509_noanchor_vtable = {
|
||||
sizeof(x509_noanchor_context),
|
||||
xwc_start_chain,
|
||||
xwc_start_cert,
|
||||
xwc_append,
|
||||
xwc_end_cert,
|
||||
xwc_end_chain,
|
||||
xwc_get_pkey
|
||||
};
|
||||
|
||||
/* see brssl.h */
|
||||
void
|
||||
x509_noanchor_init(x509_noanchor_context *xwc, const br_x509_class **inner)
|
||||
{
|
||||
xwc->vtable = &x509_noanchor_vtable;
|
||||
xwc->inner = inner;
|
||||
}
|
||||
154
tools/chain.c
Normal file
154
tools/chain.c
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "brssl.h"
|
||||
#include "bearssl.h"
|
||||
|
||||
static void
|
||||
print_blob(const char *name, const unsigned char *buf, size_t len)
|
||||
{
|
||||
size_t u;
|
||||
|
||||
printf("\nstatic const unsigned char %s[] = {", name);
|
||||
for (u = 0; u < len; u ++) {
|
||||
if (u != 0) {
|
||||
printf(",");
|
||||
}
|
||||
if (u % 12 == 0) {
|
||||
printf("\n\t");
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
printf("0x%02X", buf[u]);
|
||||
}
|
||||
printf("\n};\n");
|
||||
}
|
||||
|
||||
static void
|
||||
usage_chain(void)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"usage: brssl chain [ options ] file...\n");
|
||||
fprintf(stderr,
|
||||
"options:\n");
|
||||
fprintf(stderr,
|
||||
" -q suppress verbose messages\n");
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
int
|
||||
do_chain(int argc, char *argv[])
|
||||
{
|
||||
int retcode;
|
||||
int verbose;
|
||||
int i, num_files;
|
||||
long k, ctr;
|
||||
|
||||
retcode = 0;
|
||||
verbose = 1;
|
||||
num_files = 0;
|
||||
for (i = 0; i < argc; i ++) {
|
||||
const char *arg;
|
||||
|
||||
arg = argv[i];
|
||||
if (arg[0] != '-') {
|
||||
num_files ++;
|
||||
continue;
|
||||
}
|
||||
argv[i] = NULL;
|
||||
if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
|
||||
verbose = 1;
|
||||
} else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
|
||||
verbose = 0;
|
||||
} else {
|
||||
fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
|
||||
usage_chain();
|
||||
goto chain_exit_error;
|
||||
}
|
||||
}
|
||||
if (num_files == 0) {
|
||||
fprintf(stderr, "ERROR: no certificate file provided\n");
|
||||
usage_chain();
|
||||
goto chain_exit_error;
|
||||
}
|
||||
|
||||
ctr = 0;
|
||||
for (i = 0; i < argc; i ++) {
|
||||
const char *fname;
|
||||
br_x509_certificate *xcs;
|
||||
size_t u, num;
|
||||
|
||||
fname = argv[i];
|
||||
if (fname == NULL) {
|
||||
continue;
|
||||
}
|
||||
if (verbose) {
|
||||
fprintf(stderr, "Reading file '%s': ", fname);
|
||||
fflush(stderr);
|
||||
}
|
||||
xcs = read_certificates(fname, &num);
|
||||
if (xcs == NULL) {
|
||||
goto chain_exit_error;
|
||||
}
|
||||
if (verbose) {
|
||||
fprintf(stderr, "%lu certificate%s\n",
|
||||
(unsigned long)num, num > 1 ? "s" : "");
|
||||
}
|
||||
for (u = 0; u < num; u ++) {
|
||||
char tmp[50];
|
||||
|
||||
sprintf(tmp, "CERT%ld", ctr ++);
|
||||
print_blob(tmp, xcs[u].data, xcs[u].data_len);
|
||||
xfree(xcs[u].data);
|
||||
}
|
||||
xfree(xcs);
|
||||
}
|
||||
|
||||
printf("\nstatic const br_x509_certificate CHAIN[] = {");
|
||||
for (k = 0; k < ctr; k ++) {
|
||||
if (k != 0) {
|
||||
printf(",");
|
||||
}
|
||||
printf("\n\t{ (unsigned char *)CERT%ld, sizeof CERT%ld }",
|
||||
k, k);
|
||||
}
|
||||
printf("\n};\n");
|
||||
printf("\n#define CHAIN_LEN %ld\n", ctr);
|
||||
|
||||
/*
|
||||
* Release allocated structures.
|
||||
*/
|
||||
chain_exit:
|
||||
return retcode;
|
||||
|
||||
chain_exit_error:
|
||||
retcode = -1;
|
||||
goto chain_exit;
|
||||
}
|
||||
611
tools/client.c
Normal file
611
tools/client.c
Normal file
|
|
@ -0,0 +1,611 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/poll.h>
|
||||
|
||||
#include "brssl.h"
|
||||
#include "bearssl.h"
|
||||
|
||||
static int
|
||||
host_connect(const char *host, const char *port, int verbose)
|
||||
{
|
||||
struct addrinfo hints, *si, *p;
|
||||
int fd;
|
||||
int err;
|
||||
|
||||
memset(&hints, 0, sizeof hints);
|
||||
hints.ai_family = PF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
err = getaddrinfo(host, port, &hints, &si);
|
||||
if (err != 0) {
|
||||
fprintf(stderr, "ERROR: getaddrinfo(): %s\n",
|
||||
gai_strerror(err));
|
||||
return -1;
|
||||
}
|
||||
fd = -1;
|
||||
for (p = si; p != NULL; p = p->ai_next) {
|
||||
struct sockaddr *sa;
|
||||
void *addr;
|
||||
char tmp[INET6_ADDRSTRLEN + 50];
|
||||
|
||||
sa = (struct sockaddr *)p->ai_addr;
|
||||
if (sa->sa_family == AF_INET) {
|
||||
addr = &((struct sockaddr_in *)sa)->sin_addr;
|
||||
} else if (sa->sa_family == AF_INET6) {
|
||||
addr = &((struct sockaddr_in6 *)sa)->sin6_addr;
|
||||
} else {
|
||||
addr = NULL;
|
||||
}
|
||||
if (addr != NULL) {
|
||||
inet_ntop(p->ai_family, addr, tmp, sizeof tmp);
|
||||
} else {
|
||||
sprintf(tmp, "<unknown family: %d>",
|
||||
(int)sa->sa_family);
|
||||
}
|
||||
if (verbose) {
|
||||
fprintf(stderr, "connecting to: %s\n", tmp);
|
||||
}
|
||||
fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
|
||||
if (fd < 0) {
|
||||
if (verbose) {
|
||||
perror("socket()");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (connect(fd, p->ai_addr, p->ai_addrlen) < 0) {
|
||||
if (verbose) {
|
||||
perror("connect()");
|
||||
}
|
||||
close(fd);
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (p == NULL) {
|
||||
freeaddrinfo(si);
|
||||
fprintf(stderr, "ERROR: failed to connect\n");
|
||||
return -1;
|
||||
}
|
||||
freeaddrinfo(si);
|
||||
if (verbose) {
|
||||
fprintf(stderr, "connected.\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* We make the socket non-blocking, since we are going to use
|
||||
* poll() to organise I/O.
|
||||
*/
|
||||
fcntl(fd, F_SETFL, O_NONBLOCK);
|
||||
return fd;
|
||||
}
|
||||
|
||||
static void
|
||||
usage_client(void)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"usage: brssl client server[:port] [ options ]\n");
|
||||
fprintf(stderr,
|
||||
"options:\n");
|
||||
fprintf(stderr,
|
||||
" -q suppress verbose messages\n");
|
||||
fprintf(stderr,
|
||||
" -trace activate extra debug messages (dump of all packets)\n");
|
||||
fprintf(stderr,
|
||||
" -sni name use this specific name for SNI\n");
|
||||
fprintf(stderr,
|
||||
" -nosni do not send any SNI\n");
|
||||
fprintf(stderr,
|
||||
" -mono use monodirectional buffering\n");
|
||||
fprintf(stderr,
|
||||
" -buf length set the I/O buffer length (in bytes)\n");
|
||||
fprintf(stderr,
|
||||
" -CA file add certificates in 'file' to trust anchors\n");
|
||||
fprintf(stderr,
|
||||
" -list list supported names (protocols, algorithms...)\n");
|
||||
fprintf(stderr,
|
||||
" -vmin name set minimum supported version (default: TLS-1.0)\n");
|
||||
fprintf(stderr,
|
||||
" -vmax name set maximum supported version (default: TLS-1.2)\n");
|
||||
fprintf(stderr,
|
||||
" -cs names set list of supported cipher suites (comma-separated)\n");
|
||||
fprintf(stderr,
|
||||
" -hf names add support for some hash functions (comma-separated)\n");
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
int
|
||||
do_client(int argc, char *argv[])
|
||||
{
|
||||
int retcode;
|
||||
int verbose;
|
||||
int trace;
|
||||
int i, bidi;
|
||||
const char *server_name;
|
||||
char *host;
|
||||
char *port;
|
||||
const char *sni;
|
||||
anchor_list anchors = VEC_INIT;
|
||||
unsigned vmin, vmax;
|
||||
cipher_suite *suites;
|
||||
size_t num_suites;
|
||||
uint16_t *suite_ids;
|
||||
unsigned hfuns;
|
||||
size_t u;
|
||||
br_ssl_client_context cc;
|
||||
br_x509_minimal_context xc;
|
||||
x509_noanchor_context xwc;
|
||||
const br_hash_class *dnhash;
|
||||
unsigned char *iobuf;
|
||||
size_t iobuf_len;
|
||||
int fd;
|
||||
|
||||
retcode = 0;
|
||||
verbose = 1;
|
||||
trace = 0;
|
||||
server_name = NULL;
|
||||
host = NULL;
|
||||
port = NULL;
|
||||
sni = NULL;
|
||||
bidi = 1;
|
||||
vmin = 0;
|
||||
vmax = 0;
|
||||
suites = NULL;
|
||||
num_suites = 0;
|
||||
hfuns = 0;
|
||||
suite_ids = NULL;
|
||||
iobuf = NULL;
|
||||
iobuf_len = 0;
|
||||
fd = -1;
|
||||
for (i = 0; i < argc; i ++) {
|
||||
const char *arg;
|
||||
|
||||
arg = argv[i];
|
||||
if (arg[0] != '-') {
|
||||
if (server_name != NULL) {
|
||||
fprintf(stderr,
|
||||
"ERROR: duplicate server name\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
server_name = arg;
|
||||
continue;
|
||||
}
|
||||
if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
|
||||
verbose = 1;
|
||||
} else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
|
||||
verbose = 0;
|
||||
} else if (eqstr(arg, "-trace")) {
|
||||
trace = 1;
|
||||
} else if (eqstr(arg, "-sni")) {
|
||||
if (++ i >= argc) {
|
||||
fprintf(stderr,
|
||||
"ERROR: no argument for '-sni'\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
if (sni != NULL) {
|
||||
fprintf(stderr, "ERROR: duplicate SNI\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
sni = argv[i];
|
||||
} else if (eqstr(arg, "-nosni")) {
|
||||
if (sni != NULL) {
|
||||
fprintf(stderr, "ERROR: duplicate SNI\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
sni = "";
|
||||
} else if (eqstr(arg, "-mono")) {
|
||||
bidi = 0;
|
||||
} else if (eqstr(arg, "-buf")) {
|
||||
if (++ i >= argc) {
|
||||
fprintf(stderr,
|
||||
"ERROR: no argument for '-buf'\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
arg = argv[i];
|
||||
if (iobuf_len != 0) {
|
||||
fprintf(stderr,
|
||||
"ERROR: duplicate I/O buffer length\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
iobuf_len = strtoul(arg, 0, 10);
|
||||
} else if (eqstr(arg, "-CA")) {
|
||||
if (++ i >= argc) {
|
||||
fprintf(stderr,
|
||||
"ERROR: no argument for '-CA'\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
arg = argv[i];
|
||||
if (read_trust_anchors(&anchors, arg) == 0) {
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
} else if (eqstr(arg, "-list")) {
|
||||
list_names();
|
||||
goto client_exit;
|
||||
} else if (eqstr(arg, "-vmin")) {
|
||||
if (++ i >= argc) {
|
||||
fprintf(stderr,
|
||||
"ERROR: no argument for '-vmin'\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
arg = argv[i];
|
||||
if (vmin != 0) {
|
||||
fprintf(stderr,
|
||||
"ERROR: duplicate minimum version\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
vmin = parse_version(arg, strlen(arg));
|
||||
if (vmin == 0) {
|
||||
fprintf(stderr,
|
||||
"ERROR: unrecognised version '%s'\n",
|
||||
arg);
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
} else if (eqstr(arg, "-vmax")) {
|
||||
if (++ i >= argc) {
|
||||
fprintf(stderr,
|
||||
"ERROR: no argument for '-vmax'\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
arg = argv[i];
|
||||
if (vmax != 0) {
|
||||
fprintf(stderr,
|
||||
"ERROR: duplicate maximum version\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
vmax = parse_version(arg, strlen(arg));
|
||||
if (vmax == 0) {
|
||||
fprintf(stderr,
|
||||
"ERROR: unrecognised version '%s'\n",
|
||||
arg);
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
} else if (eqstr(arg, "-cs")) {
|
||||
if (++ i >= argc) {
|
||||
fprintf(stderr,
|
||||
"ERROR: no argument for '-cs'\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
arg = argv[i];
|
||||
if (suites != NULL) {
|
||||
fprintf(stderr, "ERROR: duplicate list"
|
||||
" of cipher suites\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
suites = parse_suites(arg, &num_suites);
|
||||
if (suites == NULL) {
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
} else if (eqstr(arg, "-hf")) {
|
||||
unsigned x;
|
||||
|
||||
if (++ i >= argc) {
|
||||
fprintf(stderr,
|
||||
"ERROR: no argument for '-hf'\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
arg = argv[i];
|
||||
x = parse_hash_functions(arg);
|
||||
if (x == 0) {
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
hfuns |= x;
|
||||
} else {
|
||||
fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
}
|
||||
if (server_name == NULL) {
|
||||
fprintf(stderr, "ERROR: no server name/address provided\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
for (u = strlen(server_name); u > 0; u --) {
|
||||
int c = server_name[u - 1];
|
||||
if (c == ':') {
|
||||
break;
|
||||
}
|
||||
if (c < '0' || c > '9') {
|
||||
u = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (u == 0) {
|
||||
host = xstrdup(server_name);
|
||||
port = "443";
|
||||
} else {
|
||||
port = xstrdup(server_name + u);
|
||||
host = xmalloc(u);
|
||||
memcpy(host, server_name, u - 1);
|
||||
host[u - 1] = 0;
|
||||
}
|
||||
if (sni == NULL) {
|
||||
sni = host;
|
||||
}
|
||||
|
||||
if (vmin == 0) {
|
||||
vmin = BR_TLS10;
|
||||
}
|
||||
if (vmax == 0) {
|
||||
vmax = BR_TLS12;
|
||||
}
|
||||
if (vmax < vmin) {
|
||||
fprintf(stderr, "ERROR: impossible minimum/maximum protocol"
|
||||
" version combination\n");
|
||||
usage_client();
|
||||
goto client_exit_error;
|
||||
}
|
||||
if (suites == NULL) {
|
||||
num_suites = 0;
|
||||
|
||||
for (u = 0; cipher_suites[u].name; u ++) {
|
||||
if ((cipher_suites[u].req & REQ_TLS12) == 0
|
||||
|| vmax >= BR_TLS12)
|
||||
{
|
||||
num_suites ++;
|
||||
}
|
||||
}
|
||||
suites = xmalloc(num_suites * sizeof *suites);
|
||||
num_suites = 0;
|
||||
for (u = 0; cipher_suites[u].name; u ++) {
|
||||
if ((cipher_suites[u].req & REQ_TLS12) == 0
|
||||
|| vmax >= BR_TLS12)
|
||||
{
|
||||
suites[num_suites ++] = cipher_suites[u];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (hfuns == 0) {
|
||||
hfuns = (unsigned)-1;
|
||||
}
|
||||
if (iobuf_len == 0) {
|
||||
if (bidi) {
|
||||
iobuf_len = BR_SSL_BUFSIZE_BIDI;
|
||||
} else {
|
||||
iobuf_len = BR_SSL_BUFSIZE_MONO;
|
||||
}
|
||||
}
|
||||
iobuf = xmalloc(iobuf_len);
|
||||
|
||||
/*
|
||||
* Compute implementation requirements and inject implementations.
|
||||
*/
|
||||
suite_ids = xmalloc(num_suites * sizeof *suite_ids);
|
||||
br_ssl_client_zero(&cc);
|
||||
br_ssl_engine_set_versions(&cc.eng, vmin, vmax);
|
||||
dnhash = NULL;
|
||||
for (u = 0; hash_functions[u].name; u ++) {
|
||||
const br_hash_class *hc;
|
||||
int id;
|
||||
|
||||
hc = hash_functions[u].hclass;
|
||||
id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
|
||||
if ((hfuns & ((unsigned)1 << id)) != 0) {
|
||||
dnhash = hc;
|
||||
}
|
||||
}
|
||||
if (dnhash == NULL) {
|
||||
fprintf(stderr, "ERROR: no supported hash function\n");
|
||||
goto client_exit_error;
|
||||
}
|
||||
br_x509_minimal_init(&xc, dnhash,
|
||||
&VEC_ELT(anchors, 0), VEC_LEN(anchors));
|
||||
if (vmin <= BR_TLS11) {
|
||||
if (!(hfuns & (1 << br_md5_ID))) {
|
||||
fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need MD5\n");
|
||||
goto client_exit_error;
|
||||
}
|
||||
if (!(hfuns & (1 << br_sha1_ID))) {
|
||||
fprintf(stderr, "ERROR: TLS 1.0 and 1.1 need SHA-1\n");
|
||||
goto client_exit_error;
|
||||
}
|
||||
}
|
||||
for (u = 0; u < num_suites; u ++) {
|
||||
unsigned req;
|
||||
|
||||
req = suites[u].req;
|
||||
suite_ids[u] = suites[u].suite;
|
||||
if ((req & REQ_TLS12) != 0 && vmax < BR_TLS12) {
|
||||
fprintf(stderr,
|
||||
"ERROR: cipher suite %s requires TLS 1.2\n",
|
||||
suites[u].name);
|
||||
goto client_exit_error;
|
||||
}
|
||||
if ((req & REQ_SHA1) != 0 && !(hfuns & (1 << br_sha1_ID))) {
|
||||
fprintf(stderr,
|
||||
"ERROR: cipher suite %s requires SHA-1\n",
|
||||
suites[u].name);
|
||||
goto client_exit_error;
|
||||
}
|
||||
if ((req & REQ_SHA256) != 0 && !(hfuns & (1 << br_sha256_ID))) {
|
||||
fprintf(stderr,
|
||||
"ERROR: cipher suite %s requires SHA-256\n",
|
||||
suites[u].name);
|
||||
goto client_exit_error;
|
||||
}
|
||||
if ((req & REQ_SHA384) != 0 && !(hfuns & (1 << br_sha384_ID))) {
|
||||
fprintf(stderr,
|
||||
"ERROR: cipher suite %s requires SHA-384\n",
|
||||
suites[u].name);
|
||||
goto client_exit_error;
|
||||
}
|
||||
/* TODO: algorithm implementation selection */
|
||||
if ((req & REQ_AESCBC) != 0) {
|
||||
br_ssl_engine_set_aes_cbc(&cc.eng,
|
||||
&br_aes_ct_cbcenc_vtable,
|
||||
&br_aes_ct_cbcdec_vtable);
|
||||
br_ssl_engine_set_cbc(&cc.eng,
|
||||
&br_sslrec_in_cbc_vtable,
|
||||
&br_sslrec_out_cbc_vtable);
|
||||
}
|
||||
if ((req & REQ_AESGCM) != 0) {
|
||||
br_ssl_engine_set_aes_ctr(&cc.eng,
|
||||
&br_aes_ct_ctr_vtable);
|
||||
br_ssl_engine_set_ghash(&cc.eng,
|
||||
&br_ghash_ctmul);
|
||||
br_ssl_engine_set_gcm(&cc.eng,
|
||||
&br_sslrec_in_gcm_vtable,
|
||||
&br_sslrec_out_gcm_vtable);
|
||||
}
|
||||
if ((req & REQ_3DESCBC) != 0) {
|
||||
br_ssl_engine_set_des_cbc(&cc.eng,
|
||||
&br_des_ct_cbcenc_vtable,
|
||||
&br_des_ct_cbcdec_vtable);
|
||||
br_ssl_engine_set_cbc(&cc.eng,
|
||||
&br_sslrec_in_cbc_vtable,
|
||||
&br_sslrec_out_cbc_vtable);
|
||||
}
|
||||
if ((req & REQ_RSAKEYX) != 0) {
|
||||
br_ssl_client_set_rsapub(&cc, &br_rsa_i31_public);
|
||||
}
|
||||
if ((req & REQ_ECDHE_RSA) != 0) {
|
||||
br_ssl_engine_set_ec(&cc.eng, &br_ec_prime_i31);
|
||||
br_ssl_client_set_rsavrfy(&cc, &br_rsa_i31_pkcs1_vrfy);
|
||||
}
|
||||
if ((req & REQ_ECDHE_ECDSA) != 0) {
|
||||
br_ssl_engine_set_ec(&cc.eng, &br_ec_prime_i31);
|
||||
br_ssl_client_set_ecdsa(&cc, &br_ecdsa_i31_vrfy_asn1);
|
||||
}
|
||||
if ((req & REQ_ECDH) != 0) {
|
||||
br_ssl_engine_set_ec(&cc.eng, &br_ec_prime_i31);
|
||||
}
|
||||
}
|
||||
br_ssl_engine_set_suites(&cc.eng, suite_ids, num_suites);
|
||||
|
||||
for (u = 0; hash_functions[u].name; u ++) {
|
||||
const br_hash_class *hc;
|
||||
int id;
|
||||
|
||||
hc = hash_functions[u].hclass;
|
||||
id = (hc->desc >> BR_HASHDESC_ID_OFF) & BR_HASHDESC_ID_MASK;
|
||||
if ((hfuns & ((unsigned)1 << id)) != 0) {
|
||||
br_ssl_engine_set_hash(&cc.eng, id, hc);
|
||||
br_x509_minimal_set_hash(&xc, id, hc);
|
||||
}
|
||||
}
|
||||
if (vmin <= BR_TLS11) {
|
||||
br_ssl_engine_set_prf10(&cc.eng, &br_tls10_prf);
|
||||
}
|
||||
if (vmax >= BR_TLS12) {
|
||||
if ((hfuns & ((unsigned)1 << br_sha256_ID)) != 0) {
|
||||
br_ssl_engine_set_prf_sha256(&cc.eng,
|
||||
&br_tls12_sha256_prf);
|
||||
}
|
||||
if ((hfuns & ((unsigned)1 << br_sha384_ID)) != 0) {
|
||||
br_ssl_engine_set_prf_sha384(&cc.eng,
|
||||
&br_tls12_sha384_prf);
|
||||
}
|
||||
}
|
||||
br_x509_minimal_set_rsa(&xc, &br_rsa_i31_pkcs1_vrfy);
|
||||
br_x509_minimal_set_ecdsa(&xc,
|
||||
&br_ec_prime_i31, &br_ecdsa_i31_vrfy_asn1);
|
||||
|
||||
/*
|
||||
* If there is no provided trust anchor, then certificate validation
|
||||
* will always fail. In that situation, we use our custom wrapper
|
||||
* that tolerates unknown anchors.
|
||||
*/
|
||||
if (VEC_LEN(anchors) == 0) {
|
||||
if (verbose) {
|
||||
fprintf(stderr,
|
||||
"WARNING: no configured trust anchor\n");
|
||||
}
|
||||
x509_noanchor_init(&xwc, &xc.vtable);
|
||||
br_ssl_engine_set_x509(&cc.eng, &xwc.vtable);
|
||||
} else {
|
||||
br_ssl_engine_set_x509(&cc.eng, &xc.vtable);
|
||||
}
|
||||
|
||||
br_ssl_engine_set_buffer(&cc.eng, iobuf, iobuf_len, bidi);
|
||||
br_ssl_client_reset(&cc, sni, 0);
|
||||
|
||||
/*
|
||||
* Connect to the peer.
|
||||
*/
|
||||
fd = host_connect(host, port, verbose);
|
||||
if (fd < 0) {
|
||||
goto client_exit_error;
|
||||
}
|
||||
|
||||
/*
|
||||
* Run the engine until completion.
|
||||
*/
|
||||
if (run_ssl_engine(&cc.eng, fd,
|
||||
(verbose ? RUN_ENGINE_VERBOSE : 0)
|
||||
| (trace ? RUN_ENGINE_TRACE : 0)) != 0)
|
||||
{
|
||||
goto client_exit_error;
|
||||
} else {
|
||||
goto client_exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Release allocated structures.
|
||||
*/
|
||||
client_exit:
|
||||
xfree(host);
|
||||
xfree(suites);
|
||||
xfree(suite_ids);
|
||||
VEC_CLEAREXT(anchors, &free_ta_contents);
|
||||
xfree(iobuf);
|
||||
if (fd >= 0) {
|
||||
close(fd);
|
||||
}
|
||||
return retcode;
|
||||
|
||||
client_exit_error:
|
||||
retcode = -1;
|
||||
goto client_exit;
|
||||
}
|
||||
333
tools/errors.c
Normal file
333
tools/errors.c
Normal file
|
|
@ -0,0 +1,333 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "brssl.h"
|
||||
#include "bearssl.h"
|
||||
|
||||
static struct {
|
||||
int err;
|
||||
const char *name;
|
||||
const char *comment;
|
||||
} errors[] = {
|
||||
{
|
||||
BR_ERR_BAD_PARAM,
|
||||
"BR_ERR_BAD_PARAM",
|
||||
"Caller-provided parameter is incorrect."
|
||||
}, {
|
||||
BR_ERR_BAD_STATE,
|
||||
"BR_ERR_BAD_STATE",
|
||||
"Operation requested by the caller cannot be applied with"
|
||||
" the current context state (e.g. reading data while"
|
||||
" outgoing data is waiting to be sent)."
|
||||
}, {
|
||||
BR_ERR_UNSUPPORTED_VERSION,
|
||||
"BR_ERR_UNSUPPORTED_VERSION",
|
||||
"Incoming protocol or record version is unsupported."
|
||||
}, {
|
||||
BR_ERR_BAD_VERSION,
|
||||
"BR_ERR_BAD_VERSION",
|
||||
"Incoming record version does not match the expected version."
|
||||
}, {
|
||||
BR_ERR_BAD_LENGTH,
|
||||
"BR_ERR_BAD_LENGTH",
|
||||
"Incoming record length is invalid."
|
||||
}, {
|
||||
BR_ERR_TOO_LARGE,
|
||||
"BR_ERR_TOO_LARGE",
|
||||
"Incoming record is too large to be processed, or buffer"
|
||||
" is too small for the handshake message to send."
|
||||
}, {
|
||||
BR_ERR_BAD_MAC,
|
||||
"BR_ERR_BAD_MAC",
|
||||
"Decryption found an invalid padding, or the record MAC is"
|
||||
" not correct."
|
||||
}, {
|
||||
BR_ERR_NO_RANDOM,
|
||||
"BR_ERR_NO_RANDOM",
|
||||
"No initial entropy was provided, and none can be obtained"
|
||||
" from the OS."
|
||||
}, {
|
||||
BR_ERR_UNKNOWN_TYPE,
|
||||
"BR_ERR_UNKNOWN_TYPE",
|
||||
"Incoming record type is unknown."
|
||||
}, {
|
||||
BR_ERR_UNEXPECTED,
|
||||
"BR_ERR_UNEXPECTED",
|
||||
"Incoming record or message has wrong type with regards to"
|
||||
" the current engine state."
|
||||
}, {
|
||||
BR_ERR_BAD_CCS,
|
||||
"BR_ERR_BAD_CCS",
|
||||
"ChangeCipherSpec message from the peer has invalid contents."
|
||||
}, {
|
||||
BR_ERR_BAD_ALERT,
|
||||
"BR_ERR_BAD_ALERT",
|
||||
"Alert message from the peer has invalid contents"
|
||||
" (odd length)."
|
||||
}, {
|
||||
BR_ERR_BAD_HANDSHAKE,
|
||||
"BR_ERR_BAD_HANDSHAKE",
|
||||
"Incoming handshake message decoding failed."
|
||||
}, {
|
||||
BR_ERR_OVERSIZED_ID,
|
||||
"BR_ERR_OVERSIZED_ID",
|
||||
"ServerHello contains a session ID which is larger than"
|
||||
" 32 bytes."
|
||||
}, {
|
||||
BR_ERR_BAD_CIPHER_SUITE,
|
||||
"BR_ERR_BAD_CIPHER_SUITE",
|
||||
"Server wants to use a cipher suite that we did not claim"
|
||||
" to support. This is also reported if we tried to advertise"
|
||||
" a cipher suite that we do not support."
|
||||
}, {
|
||||
BR_ERR_BAD_COMPRESSION,
|
||||
"BR_ERR_BAD_COMPRESSION",
|
||||
"Server wants to use a compression that we did not claim"
|
||||
" to support."
|
||||
}, {
|
||||
BR_ERR_BAD_FRAGLEN,
|
||||
"BR_ERR_BAD_FRAGLEN",
|
||||
"Server's max fragment length does not match client's."
|
||||
}, {
|
||||
BR_ERR_BAD_SECRENEG,
|
||||
"BR_ERR_BAD_SECRENEG",
|
||||
"Secure renegotiation failed."
|
||||
}, {
|
||||
BR_ERR_EXTRA_EXTENSION,
|
||||
"BR_ERR_EXTRA_EXTENSION",
|
||||
"Server sent an extension type that we did not announce,"
|
||||
" or used the same extension type several times in a"
|
||||
" single ServerHello."
|
||||
}, {
|
||||
BR_ERR_BAD_SNI,
|
||||
"BR_ERR_BAD_SNI",
|
||||
"Invalid Server Name Indication contents (when used by"
|
||||
" the server, this extension shall be empty)."
|
||||
}, {
|
||||
BR_ERR_BAD_HELLO_DONE,
|
||||
"BR_ERR_BAD_HELLO_DONE",
|
||||
"Invalid ServerHelloDone from the server (length is not 0)."
|
||||
}, {
|
||||
BR_ERR_LIMIT_EXCEEDED,
|
||||
"BR_ERR_LIMIT_EXCEEDED",
|
||||
"Internal limit exceeded (e.g. server's public key is too"
|
||||
" large)."
|
||||
}, {
|
||||
BR_ERR_BAD_FINISHED,
|
||||
"BR_ERR_BAD_FINISHED",
|
||||
"Finished message from peer does not match the expected"
|
||||
" value."
|
||||
}, {
|
||||
BR_ERR_RESUME_MISMATCH,
|
||||
"BR_ERR_RESUME_MISMATCH",
|
||||
"Session resumption attempt with distinct version or cipher"
|
||||
" suite."
|
||||
}, {
|
||||
BR_ERR_INVALID_ALGORITHM,
|
||||
"BR_ERR_INVALID_ALGORITHM",
|
||||
"Unsupported or invalid algorithm (ECDHE curve, signature"
|
||||
" algorithm, hash function)."
|
||||
}, {
|
||||
BR_ERR_BAD_SIGNATURE,
|
||||
"BR_ERR_BAD_SIGNATURE",
|
||||
"Invalid signature on ServerKeyExchange message."
|
||||
}, {
|
||||
BR_ERR_IO,
|
||||
"BR_ERR_IO",
|
||||
"I/O error or premature close on transport stream."
|
||||
}, {
|
||||
BR_ERR_X509_INVALID_VALUE,
|
||||
"BR_ERR_X509_INVALID_VALUE",
|
||||
"Invalid value in an ASN.1 structure."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_TRUNCATED,
|
||||
"BR_ERR_X509_TRUNCATED",
|
||||
"Truncated certificate or other ASN.1 object."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_EMPTY_CHAIN,
|
||||
"BR_ERR_X509_EMPTY_CHAIN",
|
||||
"Empty certificate chain (no certificate at all)."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_INNER_TRUNC,
|
||||
"BR_ERR_X509_INNER_TRUNC",
|
||||
"Decoding error: inner element extends beyond outer element"
|
||||
" size."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_BAD_TAG_CLASS,
|
||||
"BR_ERR_X509_BAD_TAG_CLASS",
|
||||
"Decoding error: unsupported tag class (application or"
|
||||
" private)."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_BAD_TAG_VALUE,
|
||||
"BR_ERR_X509_BAD_TAG_VALUE",
|
||||
"Decoding error: unsupported tag value."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_INDEFINITE_LENGTH,
|
||||
"BR_ERR_X509_INDEFINITE_LENGTH",
|
||||
"Decoding error: indefinite length."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_EXTRA_ELEMENT,
|
||||
"BR_ERR_X509_EXTRA_ELEMENT",
|
||||
"Decoding error: extraneous element."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_UNEXPECTED,
|
||||
"BR_ERR_X509_UNEXPECTED",
|
||||
"Decoding error: unexpected element."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_NOT_CONSTRUCTED,
|
||||
"BR_ERR_X509_NOT_CONSTRUCTED",
|
||||
"Decoding error: expected constructed element, but is"
|
||||
" primitive."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_NOT_PRIMITIVE,
|
||||
"BR_ERR_X509_NOT_PRIMITIVE",
|
||||
"Decoding error: expected primitive element, but is"
|
||||
" constructed."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_PARTIAL_BYTE,
|
||||
"BR_ERR_X509_PARTIAL_BYTE",
|
||||
"Decoding error: BIT STRING length is not multiple of 8."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_BAD_BOOLEAN,
|
||||
"BR_ERR_X509_BAD_BOOLEAN",
|
||||
"Decoding error: BOOLEAN value has invalid length."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_OVERFLOW,
|
||||
"BR_ERR_X509_OVERFLOW",
|
||||
"Decoding error: value is off-limits."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_BAD_DN,
|
||||
"BR_ERR_X509_BAD_DN",
|
||||
"Invalid distinguished name."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_BAD_TIME,
|
||||
"BR_ERR_X509_BAD_TIME",
|
||||
"Invalid date/time representation."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_UNSUPPORTED,
|
||||
"BR_ERR_X509_UNSUPPORTED",
|
||||
"Certificate contains unsupported features that cannot be"
|
||||
" ignored."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_LIMIT_EXCEEDED,
|
||||
"BR_ERR_X509_LIMIT_EXCEEDED",
|
||||
"Key or signature size exceeds internal limits."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_WRONG_KEY_TYPE,
|
||||
"BR_ERR_X509_WRONG_KEY_TYPE",
|
||||
"Key type does not match that which was expected."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_BAD_SIGNATURE,
|
||||
"BR_ERR_X509_BAD_SIGNATURE",
|
||||
"Signature is invalid."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_TIME_UNKNOWN,
|
||||
"BR_ERR_X509_TIME_UNKNOWN",
|
||||
"Validation time is unknown."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_EXPIRED,
|
||||
"BR_ERR_X509_EXPIRED",
|
||||
"Certificate is expired or not yet valid."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_DN_MISMATCH,
|
||||
"BR_ERR_X509_DN_MISMATCH",
|
||||
"Issuer/Subject DN mismatch in the chain."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_BAD_SERVER_NAME,
|
||||
"BR_ERR_X509_BAD_SERVER_NAME",
|
||||
"Expected server name was not found in the chain."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_CRITICAL_EXTENSION,
|
||||
"BR_ERR_X509_CRITICAL_EXTENSION",
|
||||
"Unknown critical extension in certificate."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_NOT_CA,
|
||||
"BR_ERR_X509_NOT_CA",
|
||||
"Not a CA, or path length constraint violation."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_FORBIDDEN_KEY_USAGE,
|
||||
"BR_ERR_X509_FORBIDDEN_KEY_USAGE",
|
||||
"Key Usage extension prohibits intended usage."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_WEAK_PUBLIC_KEY,
|
||||
"BR_ERR_X509_WEAK_PUBLIC_KEY",
|
||||
"Public key found in certificate is too small."
|
||||
},
|
||||
{
|
||||
BR_ERR_X509_NOT_TRUSTED,
|
||||
"BR_ERR_X509_NOT_TRUSTED",
|
||||
"Chain could not be linked to a trust anchor."
|
||||
},
|
||||
{ 0, 0, 0 }
|
||||
};
|
||||
|
||||
/* see brssl.h */
|
||||
const char *
|
||||
find_error_name(int err, const char **comment)
|
||||
{
|
||||
size_t u;
|
||||
|
||||
for (u = 0; errors[u].name; u ++) {
|
||||
if (errors[u].err == err) {
|
||||
if (comment != NULL) {
|
||||
*comment = errors[u].comment;
|
||||
}
|
||||
return errors[u].name;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
298
tools/files.c
Normal file
298
tools/files.c
Normal file
|
|
@ -0,0 +1,298 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "brssl.h"
|
||||
|
||||
/* see brssl.h */
|
||||
unsigned char *
|
||||
read_file(const char *fname, size_t *len)
|
||||
{
|
||||
bvector vbuf = VEC_INIT;
|
||||
FILE *f;
|
||||
|
||||
*len = 0;
|
||||
f = fopen(fname, "rb");
|
||||
if (f == NULL) {
|
||||
fprintf(stderr,
|
||||
"ERROR: could not open file '%s' for reading\n", fname);
|
||||
return NULL;
|
||||
}
|
||||
for (;;) {
|
||||
unsigned char tmp[1024];
|
||||
size_t rlen;
|
||||
|
||||
rlen = fread(tmp, 1, sizeof tmp, f);
|
||||
if (rlen == 0) {
|
||||
unsigned char *buf;
|
||||
|
||||
if (ferror(f)) {
|
||||
fprintf(stderr,
|
||||
"ERROR: read error on file '%s'\n",
|
||||
fname);
|
||||
fclose(f);
|
||||
return NULL;
|
||||
}
|
||||
buf = VEC_TOARRAY(vbuf);
|
||||
*len = VEC_LEN(vbuf);
|
||||
VEC_CLEAR(vbuf);
|
||||
fclose(f);
|
||||
return buf;
|
||||
}
|
||||
VEC_ADDMANY(vbuf, tmp, rlen);
|
||||
}
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
int
|
||||
write_file(const char *fname, const void *data, size_t len)
|
||||
{
|
||||
FILE *f;
|
||||
const unsigned char *buf;
|
||||
|
||||
f = fopen(fname, "wb");
|
||||
if (f == NULL) {
|
||||
fprintf(stderr,
|
||||
"ERROR: could not open file '%s' for reading\n", fname);
|
||||
return -1;
|
||||
}
|
||||
buf = data;
|
||||
while (len > 0) {
|
||||
size_t wlen;
|
||||
|
||||
wlen = fwrite(buf, 1, len, f);
|
||||
if (wlen == 0) {
|
||||
fprintf(stderr,
|
||||
"ERROR: could not write all bytes to '%s'\n",
|
||||
fname);
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
buf += wlen;
|
||||
len -= wlen;
|
||||
}
|
||||
if (ferror(f)) {
|
||||
fprintf(stderr, "ERROR: write error on file '%s'\n", fname);
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
int
|
||||
looks_like_DER(const unsigned char *buf, size_t len)
|
||||
{
|
||||
int fb;
|
||||
size_t dlen;
|
||||
|
||||
if (len < 2) {
|
||||
return 0;
|
||||
}
|
||||
if (*buf ++ != 0x30) {
|
||||
return 0;
|
||||
}
|
||||
fb = *buf ++;
|
||||
len -= 2;
|
||||
if (fb < 0x80) {
|
||||
return (size_t)fb == len;
|
||||
} else if (fb == 0x80) {
|
||||
return 0;
|
||||
} else {
|
||||
fb -= 0x80;
|
||||
if (len < (size_t)fb + 2) {
|
||||
return 0;
|
||||
}
|
||||
len -= (size_t)fb;
|
||||
dlen = 0;
|
||||
while (fb -- > 0) {
|
||||
if (dlen > (len >> 8)) {
|
||||
return 0;
|
||||
}
|
||||
dlen = (dlen << 8) + (size_t)*buf ++;
|
||||
}
|
||||
return dlen == len;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
vblob_append(void *cc, const void *data, size_t len)
|
||||
{
|
||||
bvector *bv;
|
||||
|
||||
bv = cc;
|
||||
VEC_ADDMANY(*bv, data, len);
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
void
|
||||
free_pem_object_contents(pem_object *po)
|
||||
{
|
||||
if (po != NULL) {
|
||||
xfree(po->name);
|
||||
xfree(po->data);
|
||||
}
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
pem_object *
|
||||
decode_pem(const void *src, size_t len, size_t *num)
|
||||
{
|
||||
VECTOR(pem_object) pem_list = VEC_INIT;
|
||||
br_pem_decoder_context pc;
|
||||
pem_object po, *pos;
|
||||
const unsigned char *buf;
|
||||
bvector bv = VEC_INIT;
|
||||
int inobj;
|
||||
|
||||
br_pem_decoder_init(&pc);
|
||||
buf = src;
|
||||
inobj = 0;
|
||||
po.name = NULL;
|
||||
po.data = NULL;
|
||||
po.data_len = 0;
|
||||
while (len > 0) {
|
||||
size_t tlen;
|
||||
|
||||
tlen = br_pem_decoder_push(&pc, buf, len);
|
||||
buf += tlen;
|
||||
len -= tlen;
|
||||
switch (br_pem_decoder_event(&pc)) {
|
||||
|
||||
case BR_PEM_BEGIN_OBJ:
|
||||
po.name = xstrdup(br_pem_decoder_name(&pc));
|
||||
br_pem_decoder_setdest(&pc, vblob_append, &bv);
|
||||
inobj = 1;
|
||||
break;
|
||||
|
||||
case BR_PEM_END_OBJ:
|
||||
if (inobj) {
|
||||
po.data = VEC_TOARRAY(bv);
|
||||
po.data_len = VEC_LEN(bv);
|
||||
VEC_ADD(pem_list, po);
|
||||
VEC_CLEAR(bv);
|
||||
po.name = NULL;
|
||||
po.data = NULL;
|
||||
po.data_len = 0;
|
||||
inobj = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case BR_PEM_ERROR:
|
||||
xfree(po.name);
|
||||
VEC_CLEAR(bv);
|
||||
fprintf(stderr,
|
||||
"ERROR: invalid PEM encoding\n");
|
||||
VEC_CLEAREXT(pem_list, &free_pem_object_contents);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
if (inobj) {
|
||||
fprintf(stderr, "ERROR: unfinished PEM object\n");
|
||||
xfree(po.name);
|
||||
VEC_CLEAR(bv);
|
||||
VEC_CLEAREXT(pem_list, &free_pem_object_contents);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*num = VEC_LEN(pem_list);
|
||||
VEC_ADD(pem_list, po);
|
||||
pos = VEC_TOARRAY(pem_list);
|
||||
VEC_CLEAR(pem_list);
|
||||
return pos;
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
br_x509_certificate *
|
||||
read_certificates(const char *fname, size_t *num)
|
||||
{
|
||||
VECTOR(br_x509_certificate) cert_list = VEC_INIT;
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
pem_object *pos;
|
||||
size_t u, num_pos;
|
||||
br_x509_certificate *xcs;
|
||||
br_x509_certificate dummy;
|
||||
|
||||
*num = 0;
|
||||
|
||||
/*
|
||||
* TODO: reading the whole file is crude; we could parse them
|
||||
* in a streamed fashion. But it does not matter much in practice.
|
||||
*/
|
||||
buf = read_file(fname, &len);
|
||||
if (buf == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check for a DER-encoded certificate.
|
||||
*/
|
||||
if (looks_like_DER(buf, len)) {
|
||||
xcs = xmalloc(2 * sizeof *xcs);
|
||||
xcs[0].data = buf;
|
||||
xcs[0].data_len = len;
|
||||
xcs[1].data = NULL;
|
||||
xcs[1].data_len = 0;
|
||||
*num = 1;
|
||||
return xcs;
|
||||
}
|
||||
|
||||
pos = decode_pem(buf, len, &num_pos);
|
||||
xfree(buf);
|
||||
for (u = 0; u < num_pos; u ++) {
|
||||
if (eqstr(pos[u].name, "CERTIFICATE")
|
||||
|| eqstr(pos[u].name, "X509 CERTIFICATE"))
|
||||
{
|
||||
br_x509_certificate xc;
|
||||
|
||||
xc.data = pos[u].data;
|
||||
xc.data_len = pos[u].data_len;
|
||||
pos[u].data = NULL;
|
||||
VEC_ADD(cert_list, xc);
|
||||
}
|
||||
}
|
||||
for (u = 0; u < num_pos; u ++) {
|
||||
free_pem_object_contents(&pos[u]);
|
||||
}
|
||||
xfree(pos);
|
||||
|
||||
if (VEC_LEN(cert_list) == 0) {
|
||||
fprintf(stderr, "ERROR: no certificate in file '%s'\n", fname);
|
||||
return NULL;
|
||||
}
|
||||
*num = VEC_LEN(cert_list);
|
||||
dummy.data = NULL;
|
||||
dummy.data_len = 0;
|
||||
VEC_ADD(cert_list, dummy);
|
||||
xcs = VEC_TOARRAY(cert_list);
|
||||
VEC_CLEAR(cert_list);
|
||||
return xcs;
|
||||
}
|
||||
170
tools/keys.c
Normal file
170
tools/keys.c
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "brssl.h"
|
||||
#include "bearssl.h"
|
||||
|
||||
static private_key *
|
||||
decode_key(const unsigned char *buf, size_t len)
|
||||
{
|
||||
br_skey_decoder_context dc;
|
||||
int err;
|
||||
private_key *sk;
|
||||
|
||||
br_skey_decoder_init(&dc);
|
||||
br_skey_decoder_push(&dc, buf, len);
|
||||
err = br_skey_decoder_last_error(&dc);
|
||||
if (err != 0) {
|
||||
const char *errname, *errmsg;
|
||||
|
||||
fprintf(stderr, "ERROR (decoding): err=%d\n", err);
|
||||
errname = find_error_name(err, &errmsg);
|
||||
if (errname != NULL) {
|
||||
fprintf(stderr, " %s: %s\n", errname, errmsg);
|
||||
} else {
|
||||
fprintf(stderr, " (unknown)\n");
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
switch (br_skey_decoder_key_type(&dc)) {
|
||||
const br_rsa_private_key *rk;
|
||||
const br_ec_private_key *ek;
|
||||
|
||||
case BR_KEYTYPE_RSA:
|
||||
rk = br_skey_decoder_get_rsa(&dc);
|
||||
sk = xmalloc(sizeof *sk);
|
||||
sk->key_type = BR_KEYTYPE_RSA;
|
||||
sk->key.rsa.n_bitlen = rk->n_bitlen;
|
||||
sk->key.rsa.p = xblobdup(rk->p, rk->plen);
|
||||
sk->key.rsa.plen = rk->plen;
|
||||
sk->key.rsa.q = xblobdup(rk->q, rk->qlen);
|
||||
sk->key.rsa.qlen = rk->qlen;
|
||||
sk->key.rsa.dp = xblobdup(rk->dp, rk->dplen);
|
||||
sk->key.rsa.dplen = rk->dplen;
|
||||
sk->key.rsa.dq = xblobdup(rk->dq, rk->dqlen);
|
||||
sk->key.rsa.dqlen = rk->dqlen;
|
||||
sk->key.rsa.iq = xblobdup(rk->iq, rk->iqlen);
|
||||
sk->key.rsa.iqlen = rk->iqlen;
|
||||
break;
|
||||
|
||||
case BR_KEYTYPE_EC:
|
||||
ek = br_skey_decoder_get_ec(&dc);
|
||||
sk = xmalloc(sizeof *sk);
|
||||
sk->key_type = BR_KEYTYPE_EC;
|
||||
sk->key.ec.curve = ek->curve;
|
||||
sk->key.ec.x = xblobdup(ek->x, ek->xlen);
|
||||
sk->key.ec.xlen = ek->xlen;
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "Unknown key type: %d\n",
|
||||
br_skey_decoder_key_type(&dc));
|
||||
sk = NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
return sk;
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
private_key *
|
||||
read_private_key(const char *fname)
|
||||
{
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
private_key *sk;
|
||||
pem_object *pos;
|
||||
size_t num, u;
|
||||
|
||||
buf = NULL;
|
||||
pos = NULL;
|
||||
sk = NULL;
|
||||
buf = read_file(fname, &len);
|
||||
if (buf == NULL) {
|
||||
goto deckey_exit;
|
||||
}
|
||||
if (looks_like_DER(buf, len)) {
|
||||
sk = decode_key(buf, len);
|
||||
goto deckey_exit;
|
||||
} else {
|
||||
pos = decode_pem(buf, len, &num);
|
||||
if (pos == NULL) {
|
||||
goto deckey_exit;
|
||||
}
|
||||
for (u = 0; pos[u].name; u ++) {
|
||||
const char *name;
|
||||
|
||||
name = pos[u].name;
|
||||
if (eqstr(name, "RSA PRIVATE KEY")
|
||||
|| eqstr(name, "EC PRIVATE KEY")
|
||||
|| eqstr(name, "PRIVATE KEY"))
|
||||
{
|
||||
sk = decode_key(pos[u].data, pos[u].data_len);
|
||||
goto deckey_exit;
|
||||
}
|
||||
}
|
||||
fprintf(stderr, "ERROR: no private key in file '%s'\n", fname);
|
||||
goto deckey_exit;
|
||||
}
|
||||
|
||||
deckey_exit:
|
||||
if (buf != NULL) {
|
||||
xfree(buf);
|
||||
}
|
||||
if (pos != NULL) {
|
||||
for (u = 0; pos[u].name; u ++) {
|
||||
free_pem_object_contents(&pos[u]);
|
||||
}
|
||||
xfree(pos);
|
||||
}
|
||||
return sk;
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
void
|
||||
free_private_key(private_key *sk)
|
||||
{
|
||||
if (sk == NULL) {
|
||||
return;
|
||||
}
|
||||
switch (sk->key_type) {
|
||||
case BR_KEYTYPE_RSA:
|
||||
xfree(sk->key.rsa.p);
|
||||
xfree(sk->key.rsa.q);
|
||||
xfree(sk->key.rsa.dp);
|
||||
xfree(sk->key.rsa.dq);
|
||||
xfree(sk->key.rsa.iq);
|
||||
break;
|
||||
case BR_KEYTYPE_EC:
|
||||
xfree(sk->key.ec.x);
|
||||
break;
|
||||
}
|
||||
xfree(sk);
|
||||
}
|
||||
600
tools/names.c
Normal file
600
tools/names.c
Normal file
|
|
@ -0,0 +1,600 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "brssl.h"
|
||||
#include "bearssl.h"
|
||||
|
||||
/* see brssl.h */
|
||||
const protocol_version protocol_versions[] = {
|
||||
{ "tls10", BR_TLS10, "TLS 1.0" },
|
||||
{ "tls11", BR_TLS11, "TLS 1.1" },
|
||||
{ "tls12", BR_TLS12, "TLS 1.2" },
|
||||
{ NULL, 0, NULL }
|
||||
};
|
||||
|
||||
/* see brssl.h */
|
||||
const hash_function hash_functions[] = {
|
||||
{ "md5", &br_md5_vtable, "MD5" },
|
||||
{ "sha1", &br_sha1_vtable, "SHA-1" },
|
||||
{ "sha224", &br_sha224_vtable, "SHA-224" },
|
||||
{ "sha256", &br_sha256_vtable, "SHA-256" },
|
||||
{ "sha384", &br_sha384_vtable, "SHA-384" },
|
||||
{ "sha512", &br_sha512_vtable, "SHA-512" },
|
||||
{ NULL, 0, NULL }
|
||||
};
|
||||
|
||||
/* see brssl.h */
|
||||
const cipher_suite cipher_suites[] = {
|
||||
{
|
||||
"ECDHE_ECDSA_WITH_AES_128_GCM_SHA256",
|
||||
BR_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
||||
REQ_ECDHE_ECDSA | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
|
||||
"ECDHE with ECDSA, AES-128/GCM encryption (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDHE_RSA_WITH_AES_128_GCM_SHA256",
|
||||
BR_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
||||
REQ_ECDHE_RSA | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
|
||||
"ECDHE with RSA, AES-128/GCM encryption (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
|
||||
BR_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
||||
REQ_ECDHE_ECDSA | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
|
||||
"ECDHE with ECDSA, AES-256/GCM encryption (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDHE_RSA_WITH_AES_256_GCM_SHA384",
|
||||
BR_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
||||
REQ_ECDHE_RSA | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
|
||||
"ECDHE with RSA, AES-256/GCM encryption (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDHE_ECDSA_WITH_AES_128_CBC_SHA256",
|
||||
BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
|
||||
REQ_ECDHE_ECDSA | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
|
||||
"ECDHE with ECDSA, AES-128/CBC + SHA-256 (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDHE_RSA_WITH_AES_128_CBC_SHA256",
|
||||
BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
|
||||
REQ_ECDHE_RSA | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
|
||||
"ECDHE with RSA, AES-128/CBC + SHA-256 (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",
|
||||
BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384,
|
||||
REQ_ECDHE_ECDSA | REQ_AESCBC | REQ_SHA384 | REQ_TLS12,
|
||||
"ECDHE with ECDSA, AES-256/CBC + SHA-384 (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDHE_RSA_WITH_AES_256_CBC_SHA384",
|
||||
BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384,
|
||||
REQ_ECDHE_RSA | REQ_AESCBC | REQ_SHA384 | REQ_TLS12,
|
||||
"ECDHE with RSA, AES-256/CBC + SHA-384 (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDHE_ECDSA_WITH_AES_128_CBC_SHA",
|
||||
BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
|
||||
REQ_ECDHE_ECDSA | REQ_AESCBC | REQ_SHA1,
|
||||
"ECDHE with ECDSA, AES-128/CBC + SHA-1"
|
||||
},
|
||||
{
|
||||
"ECDHE_RSA_WITH_AES_128_CBC_SHA",
|
||||
BR_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
||||
REQ_ECDHE_RSA | REQ_AESCBC | REQ_SHA1,
|
||||
"ECDHE with RSA, AES-128/CBC + SHA-1"
|
||||
},
|
||||
{
|
||||
"ECDHE_ECDSA_WITH_AES_256_CBC_SHA",
|
||||
BR_TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
|
||||
REQ_ECDHE_ECDSA | REQ_AESCBC | REQ_SHA1,
|
||||
"ECDHE with ECDSA, AES-256/CBC + SHA-1"
|
||||
},
|
||||
{
|
||||
"ECDHE_RSA_WITH_AES_256_CBC_SHA",
|
||||
BR_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
||||
REQ_ECDHE_RSA | REQ_AESCBC | REQ_SHA1,
|
||||
"ECDHE with RSA, AES-256/CBC + SHA-1"
|
||||
},
|
||||
{
|
||||
"ECDH_ECDSA_WITH_AES_128_GCM_SHA256",
|
||||
BR_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256,
|
||||
REQ_ECDH | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
|
||||
"ECDH key exchange (EC cert), AES-128/GCM (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDH_RSA_WITH_AES_128_GCM_SHA256",
|
||||
BR_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256,
|
||||
REQ_ECDH | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
|
||||
"ECDH key exchange (RSA cert), AES-128/GCM (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDH_ECDSA_WITH_AES_256_GCM_SHA384",
|
||||
BR_TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384,
|
||||
REQ_ECDH | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
|
||||
"ECDH key exchange (EC cert), AES-256/GCM (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDH_RSA_WITH_AES_256_GCM_SHA384",
|
||||
BR_TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384,
|
||||
REQ_ECDH | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
|
||||
"ECDH key exchange (RSA cert), AES-256/GCM (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDH_ECDSA_WITH_AES_128_CBC_SHA256",
|
||||
BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256,
|
||||
REQ_ECDH | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
|
||||
"ECDH key exchange (EC cert), AES-128/CBC + HMAC/SHA-256 (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDH_RSA_WITH_AES_128_CBC_SHA256",
|
||||
BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256,
|
||||
REQ_ECDH | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
|
||||
"ECDH key exchange (RSA cert), AES-128/CBC + HMAC/SHA-256 (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDH_ECDSA_WITH_AES_256_CBC_SHA384",
|
||||
BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384,
|
||||
REQ_ECDH | REQ_AESCBC | REQ_SHA384 | REQ_TLS12,
|
||||
"ECDH key exchange (EC cert), AES-256/CBC + HMAC/SHA-384 (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDH_RSA_WITH_AES_256_CBC_SHA384",
|
||||
BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384,
|
||||
REQ_ECDH | REQ_AESCBC | REQ_SHA384 | REQ_TLS12,
|
||||
"ECDH key exchange (RSA cert), AES-256/CBC + HMAC/SHA-384 (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"ECDH_ECDSA_WITH_AES_128_CBC_SHA",
|
||||
BR_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA,
|
||||
REQ_ECDH | REQ_AESCBC | REQ_SHA1,
|
||||
"ECDH key exchange (EC cert), AES-128/CBC + HMAC/SHA-1"
|
||||
},
|
||||
{
|
||||
"ECDH_RSA_WITH_AES_128_CBC_SHA",
|
||||
BR_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA,
|
||||
REQ_ECDH | REQ_AESCBC | REQ_SHA1,
|
||||
"ECDH key exchange (RSA cert), AES-128/CBC + HMAC/SHA-1"
|
||||
},
|
||||
{
|
||||
"ECDH_ECDSA_WITH_AES_256_CBC_SHA",
|
||||
BR_TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA,
|
||||
REQ_ECDH | REQ_AESCBC | REQ_SHA1,
|
||||
"ECDH key exchange (EC cert), AES-256/CBC + HMAC/SHA-1"
|
||||
},
|
||||
{
|
||||
"ECDH_RSA_WITH_AES_256_CBC_SHA",
|
||||
BR_TLS_ECDH_RSA_WITH_AES_256_CBC_SHA,
|
||||
REQ_ECDH | REQ_AESCBC | REQ_SHA1,
|
||||
"ECDH key exchange (RSA cert), AES-256/CBC + HMAC/SHA-1"
|
||||
},
|
||||
{
|
||||
"RSA_WITH_AES_128_GCM_SHA256",
|
||||
BR_TLS_RSA_WITH_AES_128_GCM_SHA256,
|
||||
REQ_RSAKEYX | REQ_AESGCM | REQ_SHA256 | REQ_TLS12,
|
||||
"RSA key exchange, AES-128/GCM encryption (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"RSA_WITH_AES_256_GCM_SHA384",
|
||||
BR_TLS_RSA_WITH_AES_256_GCM_SHA384,
|
||||
REQ_RSAKEYX | REQ_AESGCM | REQ_SHA384 | REQ_TLS12,
|
||||
"RSA key exchange, AES-256/GCM encryption (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"RSA_WITH_AES_128_CBC_SHA256",
|
||||
BR_TLS_RSA_WITH_AES_128_CBC_SHA256,
|
||||
REQ_RSAKEYX | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
|
||||
"RSA key exchange, AES-128/CBC + HMAC/SHA-256 (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"RSA_WITH_AES_256_CBC_SHA256",
|
||||
BR_TLS_RSA_WITH_AES_256_CBC_SHA256,
|
||||
REQ_RSAKEYX | REQ_AESCBC | REQ_SHA256 | REQ_TLS12,
|
||||
"RSA key exchange, AES-256/CBC + HMAC/SHA-256 (TLS 1.2+)"
|
||||
},
|
||||
{
|
||||
"RSA_WITH_AES_128_CBC_SHA",
|
||||
BR_TLS_RSA_WITH_AES_128_CBC_SHA,
|
||||
REQ_RSAKEYX | REQ_AESCBC | REQ_SHA1,
|
||||
"RSA key exchange, AES-128/CBC + HMAC/SHA-1"
|
||||
},
|
||||
{
|
||||
"RSA_WITH_AES_256_CBC_SHA",
|
||||
BR_TLS_RSA_WITH_AES_256_CBC_SHA,
|
||||
REQ_RSAKEYX | REQ_AESCBC | REQ_SHA1,
|
||||
"RSA key exchange, AES-256/CBC + HMAC/SHA-1"
|
||||
},
|
||||
{
|
||||
"ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA",
|
||||
BR_TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA,
|
||||
REQ_ECDHE_ECDSA | REQ_3DESCBC | REQ_SHA1,
|
||||
"ECDHE with ECDSA, 3DES/CBC + SHA-1"
|
||||
},
|
||||
{
|
||||
"ECDHE_RSA_WITH_3DES_EDE_CBC_SHA",
|
||||
BR_TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
|
||||
REQ_ECDHE_RSA | REQ_3DESCBC | REQ_SHA1,
|
||||
"ECDHE with RSA, 3DES/CBC + SHA-1"
|
||||
},
|
||||
{
|
||||
"ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA",
|
||||
BR_TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA,
|
||||
REQ_ECDH | REQ_3DESCBC | REQ_SHA1,
|
||||
"ECDH key exchange (EC cert), 3DES/CBC + HMAC/SHA-1"
|
||||
},
|
||||
{
|
||||
"ECDH_RSA_WITH_3DES_EDE_CBC_SHA",
|
||||
BR_TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA,
|
||||
REQ_ECDH | REQ_3DESCBC | REQ_SHA1,
|
||||
"ECDH key exchange (RSA cert), 3DES/CBC + HMAC/SHA-1"
|
||||
},
|
||||
{
|
||||
"RSA_WITH_3DES_EDE_CBC_SHA",
|
||||
BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA,
|
||||
REQ_RSAKEYX | REQ_3DESCBC | REQ_SHA1,
|
||||
"RSA key exchange, 3DES/CBC + HMAC/SHA-1"
|
||||
},
|
||||
{ NULL, 0, 0, NULL }
|
||||
};
|
||||
|
||||
/* see brssl.h */
|
||||
const char *
|
||||
get_suite_name(unsigned suite)
|
||||
{
|
||||
size_t u;
|
||||
|
||||
for (u = 0; cipher_suites[u].name; u ++) {
|
||||
if (cipher_suites[u].suite == suite) {
|
||||
return cipher_suites[u].name;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
int
|
||||
get_suite_name_ext(unsigned suite, char *dst, size_t len)
|
||||
{
|
||||
const char *name;
|
||||
char tmp[30];
|
||||
size_t n;
|
||||
|
||||
name = get_suite_name(suite);
|
||||
if (name == NULL) {
|
||||
sprintf(tmp, "unknown (0x%04X)", suite);
|
||||
name = tmp;
|
||||
}
|
||||
n = 1 + strlen(name);
|
||||
if (n > len) {
|
||||
if (len > 0) {
|
||||
dst[0] = 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
memcpy(dst, name, n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
void
|
||||
list_names(void)
|
||||
{
|
||||
size_t u;
|
||||
|
||||
printf("Protocol versions:\n");
|
||||
for (u = 0; protocol_versions[u].name; u ++) {
|
||||
printf(" %-8s %s\n",
|
||||
protocol_versions[u].name,
|
||||
protocol_versions[u].comment);
|
||||
}
|
||||
printf("Hash functions:\n");
|
||||
for (u = 0; hash_functions[u].name; u ++) {
|
||||
printf(" %-8s %s\n",
|
||||
hash_functions[u].name,
|
||||
hash_functions[u].comment);
|
||||
}
|
||||
printf("Cipher suites:\n");
|
||||
for (u = 0; cipher_suites[u].name; u ++) {
|
||||
printf(" %s\n %s\n",
|
||||
cipher_suites[u].name,
|
||||
cipher_suites[u].comment);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
is_ign(int c)
|
||||
{
|
||||
if (c == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (c <= 32 || c == '-' || c == '_' || c == '.'
|
||||
|| c == '/' || c == '+' || c == ':')
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Get next non-ignored character, normalised:
|
||||
* ASCII letters are converted to lowercase
|
||||
* control characters, space, '-', '_', '.', '/', '+' and ':' are ignored
|
||||
* A terminating zero is returned as 0.
|
||||
*/
|
||||
static int
|
||||
next_char(const char **ps, const char *limit)
|
||||
{
|
||||
for (;;) {
|
||||
int c;
|
||||
|
||||
if (*ps == limit) {
|
||||
return 0;
|
||||
}
|
||||
c = *(*ps) ++;
|
||||
if (c == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (c >= 'A' && c <= 'Z') {
|
||||
c += 'a' - 'A';
|
||||
}
|
||||
if (!is_ign(c)) {
|
||||
return c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Partial string equality comparison, with normalisation.
|
||||
*/
|
||||
static int
|
||||
eqstr_chunk(const char *s1, size_t s1_len, const char *s2, size_t s2_len)
|
||||
{
|
||||
const char *lim1, *lim2;
|
||||
|
||||
lim1 = s1 + s1_len;
|
||||
lim2 = s2 + s2_len;
|
||||
for (;;) {
|
||||
int c1, c2;
|
||||
|
||||
c1 = next_char(&s1, lim1);
|
||||
c2 = next_char(&s2, lim2);
|
||||
if (c1 != c2) {
|
||||
return 0;
|
||||
}
|
||||
if (c1 == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
int
|
||||
eqstr(const char *s1, const char *s2)
|
||||
{
|
||||
return eqstr_chunk(s1, strlen(s1), s2, strlen(s2));
|
||||
}
|
||||
|
||||
/*
|
||||
* Comma-separated list enumeration. This returns a pointer to the first
|
||||
* word in the string, skipping leading ignored characters. '*len' is
|
||||
* set to the word length (not counting trailing ignored characters).
|
||||
* '*str' is updated to point to immediately after the next comma, or to
|
||||
* the terminating zero, whichever comes first.
|
||||
*
|
||||
* Empty words are skipped. If there is no next non-empty word, then this
|
||||
* function returns NULL and sets *len to 0.
|
||||
*/
|
||||
static const char *
|
||||
next_word(const char **str, size_t *len)
|
||||
{
|
||||
int c;
|
||||
const char *begin;
|
||||
size_t u;
|
||||
|
||||
/*
|
||||
* Find next non-ignored character which is not a comma.
|
||||
*/
|
||||
for (;;) {
|
||||
c = **str;
|
||||
if (c == 0) {
|
||||
*len = 0;
|
||||
return NULL;
|
||||
}
|
||||
if (!is_ign(c) && c != ',') {
|
||||
break;
|
||||
}
|
||||
(*str) ++;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find next comma or terminator.
|
||||
*/
|
||||
begin = *str;
|
||||
for (;;) {
|
||||
c = *(*str);
|
||||
if (c == 0 || c == ',') {
|
||||
break;
|
||||
}
|
||||
(*str) ++;
|
||||
}
|
||||
|
||||
/*
|
||||
* Remove trailing ignored characters.
|
||||
*/
|
||||
u = (size_t)(*str - begin);
|
||||
while (u > 0 && is_ign(begin[u - 1])) {
|
||||
u --;
|
||||
}
|
||||
if (c == ',') {
|
||||
(*str) ++;
|
||||
}
|
||||
*len = u;
|
||||
return begin;
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
unsigned
|
||||
parse_version(const char *name, size_t len)
|
||||
{
|
||||
size_t u;
|
||||
|
||||
for (u = 0;; u ++) {
|
||||
const char *ref;
|
||||
|
||||
ref = protocol_versions[u].name;
|
||||
if (ref == NULL) {
|
||||
fprintf(stderr, "ERROR: unrecognised protocol"
|
||||
" version name: '%s'\n", name);
|
||||
return 0;
|
||||
}
|
||||
if (eqstr_chunk(ref, strlen(ref), name, len)) {
|
||||
return protocol_versions[u].version;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
unsigned
|
||||
parse_hash_functions(const char *arg)
|
||||
{
|
||||
unsigned r;
|
||||
|
||||
r = 0;
|
||||
for (;;) {
|
||||
const char *name;
|
||||
size_t len;
|
||||
size_t u;
|
||||
|
||||
name = next_word(&arg, &len);
|
||||
if (name == NULL) {
|
||||
break;
|
||||
}
|
||||
for (u = 0;; u ++) {
|
||||
const char *ref;
|
||||
|
||||
ref = hash_functions[u].name;
|
||||
if (ref == 0) {
|
||||
fprintf(stderr, "ERROR: unrecognised"
|
||||
" hash function name: '");
|
||||
fwrite(name, 1, len, stderr);
|
||||
fprintf(stderr, "'\n");
|
||||
return 0;
|
||||
}
|
||||
if (eqstr_chunk(ref, strlen(ref), name, len)) {
|
||||
int id;
|
||||
|
||||
id = (hash_functions[u].hclass->desc
|
||||
>> BR_HASHDESC_ID_OFF)
|
||||
& BR_HASHDESC_ID_MASK;
|
||||
r |= (unsigned)1 << id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (r == 0) {
|
||||
fprintf(stderr, "ERROR: no hash function name provided\n");
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
cipher_suite *
|
||||
parse_suites(const char *arg, size_t *num)
|
||||
{
|
||||
VECTOR(cipher_suite) suites = VEC_INIT;
|
||||
cipher_suite *r;
|
||||
|
||||
for (;;) {
|
||||
const char *name;
|
||||
size_t u, len;
|
||||
|
||||
name = next_word(&arg, &len);
|
||||
if (name == NULL) {
|
||||
break;
|
||||
}
|
||||
for (u = 0;; u ++) {
|
||||
const char *ref;
|
||||
|
||||
ref = cipher_suites[u].name;
|
||||
if (ref == NULL) {
|
||||
fprintf(stderr, "ERROR: unrecognised"
|
||||
" cipher suite '");
|
||||
fwrite(name, 1, len, stderr);
|
||||
fprintf(stderr, "'\n");
|
||||
return 0;
|
||||
}
|
||||
if (eqstr_chunk(ref, strlen(ref), name, len)) {
|
||||
VEC_ADD(suites, cipher_suites[u]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (VEC_LEN(suites) == 0) {
|
||||
fprintf(stderr, "ERROR: no cipher suite provided\n");
|
||||
}
|
||||
r = VEC_TOARRAY(suites);
|
||||
*num = VEC_LEN(suites);
|
||||
VEC_CLEAR(suites);
|
||||
return r;
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
const char *
|
||||
ec_curve_name(int curve)
|
||||
{
|
||||
switch (curve) {
|
||||
case BR_EC_sect163k1: return "sect163k1";
|
||||
case BR_EC_sect163r1: return "sect163r1";
|
||||
case BR_EC_sect163r2: return "sect163r2";
|
||||
case BR_EC_sect193r1: return "sect193r1";
|
||||
case BR_EC_sect193r2: return "sect193r2";
|
||||
case BR_EC_sect233k1: return "sect233k1";
|
||||
case BR_EC_sect233r1: return "sect233r1";
|
||||
case BR_EC_sect239k1: return "sect239k1";
|
||||
case BR_EC_sect283k1: return "sect283k1";
|
||||
case BR_EC_sect283r1: return "sect283r1";
|
||||
case BR_EC_sect409k1: return "sect409k1";
|
||||
case BR_EC_sect409r1: return "sect409r1";
|
||||
case BR_EC_sect571k1: return "sect571k1";
|
||||
case BR_EC_sect571r1: return "sect571r1";
|
||||
case BR_EC_secp160k1: return "secp160k1";
|
||||
case BR_EC_secp160r1: return "secp160r1";
|
||||
case BR_EC_secp160r2: return "secp160r2";
|
||||
case BR_EC_secp192k1: return "secp192k1";
|
||||
case BR_EC_secp192r1: return "secp192r1";
|
||||
case BR_EC_secp224k1: return "secp224k1";
|
||||
case BR_EC_secp224r1: return "secp224r1";
|
||||
case BR_EC_secp256k1: return "secp256k1";
|
||||
case BR_EC_secp256r1: return "secp256r1";
|
||||
case BR_EC_secp384r1: return "secp384r1";
|
||||
case BR_EC_secp521r1: return "secp521r1";
|
||||
case BR_EC_brainpoolP256r1: return "brainpoolP256r1";
|
||||
case BR_EC_brainpoolP384r1: return "brainpoolP384r1";
|
||||
case BR_EC_brainpoolP512r1: return "brainpoolP512r1";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
1053
tools/server.c
Normal file
1053
tools/server.c
Normal file
File diff suppressed because it is too large
Load diff
298
tools/skey.c
Normal file
298
tools/skey.c
Normal file
|
|
@ -0,0 +1,298 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "brssl.h"
|
||||
#include "bearssl.h"
|
||||
|
||||
static void
|
||||
print_int_text(const char *name, const unsigned char *buf, size_t len)
|
||||
{
|
||||
size_t u;
|
||||
|
||||
printf("%s = ", name);
|
||||
for (u = 0; u < len; u ++) {
|
||||
printf("%02X", buf[u]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void
|
||||
print_int_C(const char *name, const unsigned char *buf, size_t len)
|
||||
{
|
||||
size_t u;
|
||||
|
||||
printf("\nstatic const unsigned char %s[] = {", name);
|
||||
for (u = 0; u < len; u ++) {
|
||||
if (u != 0) {
|
||||
printf(",");
|
||||
}
|
||||
if (u % 12 == 0) {
|
||||
printf("\n\t");
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
printf("0x%02X", buf[u]);
|
||||
}
|
||||
printf("\n};\n");
|
||||
}
|
||||
|
||||
static void
|
||||
print_rsa(const br_rsa_private_key *sk, int print_text, int print_C)
|
||||
{
|
||||
if (print_text) {
|
||||
print_int_text("p ", sk->p, sk->plen);
|
||||
print_int_text("q ", sk->q, sk->qlen);
|
||||
print_int_text("dp", sk->dp, sk->dplen);
|
||||
print_int_text("dq", sk->dq, sk->dqlen);
|
||||
print_int_text("iq", sk->iq, sk->iqlen);
|
||||
}
|
||||
if (print_C) {
|
||||
print_int_C("RSA_P", sk->p, sk->plen);
|
||||
print_int_C("RSA_Q", sk->q, sk->qlen);
|
||||
print_int_C("RSA_DP", sk->dp, sk->dplen);
|
||||
print_int_C("RSA_DQ", sk->dq, sk->dqlen);
|
||||
print_int_C("RSA_IQ", sk->iq, sk->iqlen);
|
||||
printf("\nstatic const br_rsa_private_key RSA = {\n");
|
||||
printf("\t%lu,\n", (unsigned long)sk->n_bitlen);
|
||||
printf("\t(unsigned char *)RSA_P, sizeof RSA_P,\n");
|
||||
printf("\t(unsigned char *)RSA_Q, sizeof RSA_Q,\n");
|
||||
printf("\t(unsigned char *)RSA_DP, sizeof RSA_DP,\n");
|
||||
printf("\t(unsigned char *)RSA_DQ, sizeof RSA_DQ,\n");
|
||||
printf("\t(unsigned char *)RSA_IQ, sizeof RSA_IQ\n");
|
||||
printf("};\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
print_ec(const br_ec_private_key *sk, int print_text, int print_C)
|
||||
{
|
||||
if (print_text) {
|
||||
print_int_text("x", sk->x, sk->xlen);
|
||||
}
|
||||
if (print_C) {
|
||||
print_int_C("EC_X", sk->x, sk->xlen);
|
||||
printf("\nstatic const br_ec_private_key EC = {\n");
|
||||
printf("\t%d,\n", sk->curve);
|
||||
printf("\t(unsigned char *)EC_X, sizeof EC_X\n");
|
||||
printf("};\n");
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
decode_key(const unsigned char *buf, size_t len, int print_text, int print_C)
|
||||
{
|
||||
br_skey_decoder_context dc;
|
||||
int err;
|
||||
|
||||
br_skey_decoder_init(&dc);
|
||||
br_skey_decoder_push(&dc, buf, len);
|
||||
err = br_skey_decoder_last_error(&dc);
|
||||
if (err != 0) {
|
||||
const char *errname, *errmsg;
|
||||
|
||||
fprintf(stderr, "ERROR (decoding): err=%d\n", err);
|
||||
errname = find_error_name(err, &errmsg);
|
||||
if (errname != NULL) {
|
||||
fprintf(stderr, " %s: %s\n", errname, errmsg);
|
||||
} else {
|
||||
fprintf(stderr, " (unknown)\n");
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
switch (br_skey_decoder_key_type(&dc)) {
|
||||
const br_rsa_private_key *rk;
|
||||
const br_ec_private_key *ek;
|
||||
|
||||
case BR_KEYTYPE_RSA:
|
||||
rk = br_skey_decoder_get_rsa(&dc);
|
||||
printf("RSA key (%lu bits)\n", (unsigned long)rk->n_bitlen);
|
||||
print_rsa(rk, print_text, print_C);
|
||||
break;
|
||||
|
||||
case BR_KEYTYPE_EC:
|
||||
ek = br_skey_decoder_get_ec(&dc);
|
||||
printf("EC key (curve = %d: %s)\n",
|
||||
ek->curve, ec_curve_name(ek->curve));
|
||||
print_ec(ek, print_text, print_C);
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "Unknown key type: %d\n",
|
||||
br_skey_decoder_key_type(&dc));
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
usage_skey(void)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"usage: brssl skey [ options ] file...\n");
|
||||
fprintf(stderr,
|
||||
"options:\n");
|
||||
fprintf(stderr,
|
||||
" -q suppress verbose messages\n");
|
||||
fprintf(stderr,
|
||||
" -text print public key details (human-readable)\n");
|
||||
fprintf(stderr,
|
||||
" -C print public key details (C code)\n");
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
int
|
||||
do_skey(int argc, char *argv[])
|
||||
{
|
||||
int retcode;
|
||||
int verbose;
|
||||
int i, num_files;
|
||||
int print_text, print_C;
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
pem_object *pos;
|
||||
|
||||
retcode = 0;
|
||||
verbose = 1;
|
||||
print_text = 0;
|
||||
print_C = 0;
|
||||
num_files = 0;
|
||||
buf = NULL;
|
||||
pos = NULL;
|
||||
for (i = 0; i < argc; i ++) {
|
||||
const char *arg;
|
||||
|
||||
arg = argv[i];
|
||||
if (arg[0] != '-') {
|
||||
num_files ++;
|
||||
continue;
|
||||
}
|
||||
argv[i] = NULL;
|
||||
if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
|
||||
verbose = 1;
|
||||
} else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
|
||||
verbose = 0;
|
||||
} else if (eqstr(arg, "-text")) {
|
||||
print_text = 1;
|
||||
} else if (eqstr(arg, "-C")) {
|
||||
print_C = 1;
|
||||
} else {
|
||||
fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
|
||||
usage_skey();
|
||||
goto skey_exit_error;
|
||||
}
|
||||
}
|
||||
if (num_files == 0) {
|
||||
fprintf(stderr, "ERROR: no private key provided\n");
|
||||
usage_skey();
|
||||
goto skey_exit_error;
|
||||
}
|
||||
|
||||
for (i = 0; i < argc; i ++) {
|
||||
const char *fname;
|
||||
|
||||
fname = argv[i];
|
||||
if (fname == NULL) {
|
||||
continue;
|
||||
}
|
||||
buf = read_file(fname, &len);
|
||||
if (buf == NULL) {
|
||||
goto skey_exit_error;
|
||||
}
|
||||
if (looks_like_DER(buf, len)) {
|
||||
if (verbose) {
|
||||
fprintf(stderr, "File '%s': ASN.1/DER object\n",
|
||||
fname);
|
||||
}
|
||||
if (decode_key(buf, len, print_text, print_C) < 0) {
|
||||
goto skey_exit_error;
|
||||
}
|
||||
} else {
|
||||
size_t u, num;
|
||||
|
||||
if (verbose) {
|
||||
fprintf(stderr, "File '%s': decoding as PEM\n",
|
||||
fname);
|
||||
}
|
||||
pos = decode_pem(buf, len, &num);
|
||||
if (pos == NULL) {
|
||||
goto skey_exit_error;
|
||||
}
|
||||
for (u = 0; pos[u].name; u ++) {
|
||||
const char *name;
|
||||
|
||||
name = pos[u].name;
|
||||
if (eqstr(name, "RSA PRIVATE KEY")
|
||||
|| eqstr(name, "EC PRIVATE KEY")
|
||||
|| eqstr(name, "PRIVATE KEY"))
|
||||
{
|
||||
if (decode_key(pos[u].data,
|
||||
pos[u].data_len,
|
||||
print_text, print_C) < 0)
|
||||
{
|
||||
goto skey_exit_error;
|
||||
}
|
||||
} else {
|
||||
if (verbose) {
|
||||
fprintf(stderr,
|
||||
"(skipping '%s')\n",
|
||||
name);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (u = 0; pos[u].name; u ++) {
|
||||
free_pem_object_contents(&pos[u]);
|
||||
}
|
||||
xfree(pos);
|
||||
pos = NULL;
|
||||
}
|
||||
xfree(buf);
|
||||
buf = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Release allocated structures.
|
||||
*/
|
||||
skey_exit:
|
||||
xfree(buf);
|
||||
if (pos != NULL) {
|
||||
size_t u;
|
||||
|
||||
for (u = 0; pos[u].name; u ++) {
|
||||
free_pem_object_contents(&pos[u]);
|
||||
}
|
||||
xfree(pos);
|
||||
}
|
||||
return retcode;
|
||||
|
||||
skey_exit_error:
|
||||
retcode = -1;
|
||||
goto skey_exit;
|
||||
}
|
||||
372
tools/sslio.c
Normal file
372
tools/sslio.c
Normal file
|
|
@ -0,0 +1,372 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/poll.h>
|
||||
|
||||
#include "brssl.h"
|
||||
#include "bearssl.h"
|
||||
|
||||
static void
|
||||
dump_blob(const char *name, const void *data, size_t len)
|
||||
{
|
||||
const unsigned char *buf;
|
||||
size_t u;
|
||||
|
||||
buf = data;
|
||||
fprintf(stderr, "%s (len = %lu)", name, (unsigned long)len);
|
||||
for (u = 0; u < len; u ++) {
|
||||
if ((u & 15) == 0) {
|
||||
fprintf(stderr, "\n%08lX ", (unsigned long)u);
|
||||
} else if ((u & 7) == 0) {
|
||||
fprintf(stderr, " ");
|
||||
}
|
||||
fprintf(stderr, " %02x", buf[u]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
|
||||
/*
|
||||
* Inspect the provided data in case it is a "command" to trigger a
|
||||
* special behaviour. If the command is recognised, then it is executed
|
||||
* and this function returns 1. Otherwise, this function returns 0.
|
||||
*/
|
||||
static int
|
||||
run_command(br_ssl_engine_context *cc, unsigned char *buf, size_t len)
|
||||
{
|
||||
if (len < 2 || len > 3) {
|
||||
return 0;
|
||||
}
|
||||
if (len == 3 && (buf[1] != '\r' || buf[2] != '\n')) {
|
||||
return 0;
|
||||
}
|
||||
if (len == 2 && buf[1] != '\n') {
|
||||
return 0;
|
||||
}
|
||||
switch (buf[0]) {
|
||||
case 'Q':
|
||||
fprintf(stderr, "closing...\n");
|
||||
br_ssl_engine_close(cc);
|
||||
return 1;
|
||||
case 'R':
|
||||
fprintf(stderr, "renegotiating...\n");
|
||||
br_ssl_engine_renegotiate(cc);
|
||||
return 1;
|
||||
case 'F':
|
||||
/*
|
||||
* Session forget is nominally client-only. But the
|
||||
* session parameters are in the engine structure, which
|
||||
* is the first field of the client context, so the cast
|
||||
* still works properly. On the server, this forgetting
|
||||
* has no effect.
|
||||
*/
|
||||
fprintf(stderr, "forgetting session...\n");
|
||||
br_ssl_client_forget_session((br_ssl_client_context *)cc);
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
int
|
||||
run_ssl_engine(br_ssl_engine_context *cc, int fd, unsigned flags)
|
||||
{
|
||||
int hsdetails;
|
||||
int retcode;
|
||||
int verbose;
|
||||
int trace;
|
||||
|
||||
hsdetails = 0;
|
||||
retcode = 0;
|
||||
verbose = (flags & RUN_ENGINE_VERBOSE) != 0;
|
||||
trace = (flags & RUN_ENGINE_TRACE) != 0;
|
||||
|
||||
/*
|
||||
* Make sure that stdin and stdout are non-blocking.
|
||||
*/
|
||||
fcntl(0, F_SETFL, O_NONBLOCK);
|
||||
fcntl(1, F_SETFL, O_NONBLOCK);
|
||||
|
||||
/*
|
||||
* Perform the loop.
|
||||
*/
|
||||
for (;;) {
|
||||
unsigned st;
|
||||
int sendrec, recvrec, sendapp, recvapp;
|
||||
struct pollfd pfd[3];
|
||||
int n;
|
||||
size_t u, k_fd, k_in, k_out;
|
||||
|
||||
/*
|
||||
* Get current engine state.
|
||||
*/
|
||||
st = br_ssl_engine_current_state(cc);
|
||||
if (st == BR_SSL_CLOSED) {
|
||||
int err;
|
||||
|
||||
err = br_ssl_engine_last_error(cc);
|
||||
if (err == BR_ERR_OK) {
|
||||
if (verbose) {
|
||||
fprintf(stderr,
|
||||
"SSL closed normally\n");
|
||||
}
|
||||
retcode = 0;
|
||||
goto engine_exit;
|
||||
} else {
|
||||
fprintf(stderr, "ERROR: SSL error %d", err);
|
||||
retcode = err;
|
||||
if (err >= BR_ERR_SEND_FATAL_ALERT) {
|
||||
err -= BR_ERR_SEND_FATAL_ALERT;
|
||||
fprintf(stderr,
|
||||
" (sent alert %d)\n", err);
|
||||
} else if (err >= BR_ERR_RECV_FATAL_ALERT) {
|
||||
err -= BR_ERR_RECV_FATAL_ALERT;
|
||||
fprintf(stderr,
|
||||
" (received alert %d)\n", err);
|
||||
} else {
|
||||
const char *ename;
|
||||
|
||||
ename = find_error_name(err, NULL);
|
||||
if (ename == NULL) {
|
||||
ename = "unknown";
|
||||
}
|
||||
fprintf(stderr, " (%s)\n", ename);
|
||||
}
|
||||
goto engine_exit;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Compute descriptors that must be polled, depending
|
||||
* on engine state.
|
||||
*/
|
||||
sendrec = ((st & BR_SSL_SENDREC) != 0);
|
||||
recvrec = ((st & BR_SSL_RECVREC) != 0);
|
||||
sendapp = ((st & BR_SSL_SENDAPP) != 0);
|
||||
recvapp = ((st & BR_SSL_RECVAPP) != 0);
|
||||
if (verbose && sendapp && !hsdetails) {
|
||||
char csn[80];
|
||||
|
||||
fprintf(stderr, "Handshake completed\n");
|
||||
fprintf(stderr, " version: ");
|
||||
switch (cc->session.version) {
|
||||
case BR_SSL30:
|
||||
fprintf(stderr, "SSL 3.0");
|
||||
break;
|
||||
case BR_TLS10:
|
||||
fprintf(stderr, "TLS 1.0");
|
||||
break;
|
||||
case BR_TLS11:
|
||||
fprintf(stderr, "TLS 1.1");
|
||||
break;
|
||||
case BR_TLS12:
|
||||
fprintf(stderr, "TLS 1.2");
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "unknown (0x%04X)",
|
||||
(unsigned)cc->session.version);
|
||||
break;
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
get_suite_name_ext(
|
||||
cc->session.cipher_suite, csn, sizeof csn);
|
||||
fprintf(stderr, " cipher suite: %s\n", csn);
|
||||
fprintf(stderr, " secure renegotiation: %s\n",
|
||||
cc->reneg == 1 ? "no" : "yes");
|
||||
hsdetails = 1;
|
||||
}
|
||||
|
||||
k_fd = 0;
|
||||
k_in = 0;
|
||||
k_out = 0;
|
||||
|
||||
u = 0;
|
||||
if (sendrec || recvrec) {
|
||||
pfd[u].fd = fd;
|
||||
pfd[u].revents = 0;
|
||||
pfd[u].events = 0;
|
||||
if (sendrec) {
|
||||
pfd[u].events |= POLLOUT;
|
||||
}
|
||||
if (recvrec) {
|
||||
pfd[u].events |= POLLIN;
|
||||
}
|
||||
k_fd = u;
|
||||
u ++;
|
||||
}
|
||||
if (sendapp) {
|
||||
pfd[u].fd = 0;
|
||||
pfd[u].revents = 0;
|
||||
pfd[u].events = POLLIN;
|
||||
k_in = u;
|
||||
u ++;
|
||||
}
|
||||
if (recvapp) {
|
||||
pfd[u].fd = 1;
|
||||
pfd[u].revents = 0;
|
||||
pfd[u].events = POLLOUT;
|
||||
k_out = u;
|
||||
u ++;
|
||||
}
|
||||
n = poll(pfd, u, -1);
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
perror("ERROR: poll()");
|
||||
retcode = -2;
|
||||
goto engine_exit;
|
||||
}
|
||||
if (n == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
* We transform closures/errors into read+write accesses
|
||||
* so as to force the read() or write() call that will
|
||||
* detect the situation.
|
||||
*/
|
||||
while (u -- > 0) {
|
||||
if (pfd[u].revents & (POLLERR | POLLHUP)) {
|
||||
pfd[u].revents |= POLLIN | POLLOUT;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* We give preference to outgoing data, on stdout and on
|
||||
* the socket.
|
||||
*/
|
||||
if (recvapp) {
|
||||
if (pfd[k_out].revents & POLLOUT) {
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
ssize_t wlen;
|
||||
|
||||
buf = br_ssl_engine_recvapp_buf(cc, &len);
|
||||
wlen = write(1, buf, len);
|
||||
if (wlen <= 0) {
|
||||
if (verbose) {
|
||||
fprintf(stderr,
|
||||
"stdout closed...\n");
|
||||
}
|
||||
retcode = -2;
|
||||
goto engine_exit;
|
||||
}
|
||||
br_ssl_engine_recvapp_ack(cc, wlen);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (sendrec) {
|
||||
if (pfd[k_fd].revents & POLLOUT) {
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
ssize_t wlen;
|
||||
|
||||
buf = br_ssl_engine_sendrec_buf(cc, &len);
|
||||
wlen = write(fd, buf, len);
|
||||
if (wlen <= 0) {
|
||||
if (verbose) {
|
||||
fprintf(stderr,
|
||||
"socket closed...\n");
|
||||
}
|
||||
retcode = -1;
|
||||
goto engine_exit;
|
||||
}
|
||||
if (trace) {
|
||||
dump_blob("Outgoing bytes", buf, wlen);
|
||||
}
|
||||
br_ssl_engine_sendrec_ack(cc, wlen);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (recvrec) {
|
||||
if (pfd[k_fd].revents & POLLIN) {
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
ssize_t rlen;
|
||||
|
||||
buf = br_ssl_engine_recvrec_buf(cc, &len);
|
||||
rlen = read(fd, buf, len);
|
||||
if (rlen <= 0) {
|
||||
if (verbose) {
|
||||
fprintf(stderr,
|
||||
"socket closed...\n");
|
||||
}
|
||||
retcode = -1;
|
||||
goto engine_exit;
|
||||
}
|
||||
if (trace) {
|
||||
dump_blob("Incoming bytes", buf, rlen);
|
||||
}
|
||||
br_ssl_engine_recvrec_ack(cc, rlen);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (sendapp) {
|
||||
if (pfd[k_in].revents & POLLIN) {
|
||||
unsigned char *buf;
|
||||
size_t len;
|
||||
ssize_t rlen;
|
||||
|
||||
buf = br_ssl_engine_sendapp_buf(cc, &len);
|
||||
rlen = read(0, buf, len);
|
||||
if (rlen <= 0) {
|
||||
if (verbose) {
|
||||
fprintf(stderr,
|
||||
"stdin closed...\n");
|
||||
}
|
||||
br_ssl_engine_close(cc);
|
||||
} else if (!run_command(cc, buf, rlen)) {
|
||||
br_ssl_engine_sendapp_ack(cc, rlen);
|
||||
}
|
||||
br_ssl_engine_flush(cc, 0);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/* We should never reach that point. */
|
||||
fprintf(stderr, "ERROR: poll() misbehaves\n");
|
||||
retcode = -2;
|
||||
goto engine_exit;
|
||||
}
|
||||
|
||||
/*
|
||||
* Release allocated structures.
|
||||
*/
|
||||
engine_exit:
|
||||
return retcode;
|
||||
}
|
||||
253
tools/ta.c
Normal file
253
tools/ta.c
Normal file
|
|
@ -0,0 +1,253 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "brssl.h"
|
||||
#include "bearssl.h"
|
||||
|
||||
static const char *
|
||||
curve_to_sym(int curve)
|
||||
{
|
||||
switch (curve) {
|
||||
case BR_EC_sect163k1: return "BR_EC_sect163k1";
|
||||
case BR_EC_sect163r1: return "BR_EC_sect163r1";
|
||||
case BR_EC_sect163r2: return "BR_EC_sect163r2";
|
||||
case BR_EC_sect193r1: return "BR_EC_sect193r1";
|
||||
case BR_EC_sect193r2: return "BR_EC_sect193r2";
|
||||
case BR_EC_sect233k1: return "BR_EC_sect233k1";
|
||||
case BR_EC_sect233r1: return "BR_EC_sect233r1";
|
||||
case BR_EC_sect239k1: return "BR_EC_sect239k1";
|
||||
case BR_EC_sect283k1: return "BR_EC_sect283k1";
|
||||
case BR_EC_sect283r1: return "BR_EC_sect283r1";
|
||||
case BR_EC_sect409k1: return "BR_EC_sect409k1";
|
||||
case BR_EC_sect409r1: return "BR_EC_sect409r1";
|
||||
case BR_EC_sect571k1: return "BR_EC_sect571k1";
|
||||
case BR_EC_sect571r1: return "BR_EC_sect571r1";
|
||||
case BR_EC_secp160k1: return "BR_EC_secp160k1";
|
||||
case BR_EC_secp160r1: return "BR_EC_secp160r1";
|
||||
case BR_EC_secp160r2: return "BR_EC_secp160r2";
|
||||
case BR_EC_secp192k1: return "BR_EC_secp192k1";
|
||||
case BR_EC_secp192r1: return "BR_EC_secp192r1";
|
||||
case BR_EC_secp224k1: return "BR_EC_secp224k1";
|
||||
case BR_EC_secp224r1: return "BR_EC_secp224r1";
|
||||
case BR_EC_secp256k1: return "BR_EC_secp256k1";
|
||||
case BR_EC_secp256r1: return "BR_EC_secp256r1";
|
||||
case BR_EC_secp384r1: return "BR_EC_secp384r1";
|
||||
case BR_EC_secp521r1: return "BR_EC_secp521r1";
|
||||
case BR_EC_brainpoolP256r1: return "BR_EC_brainpoolP256r1";
|
||||
case BR_EC_brainpoolP384r1: return "BR_EC_brainpoolP384r1";
|
||||
case BR_EC_brainpoolP512r1: return "BR_EC_brainpoolP512r1";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
print_blob(const char *name, const unsigned char *buf, size_t len)
|
||||
{
|
||||
size_t u;
|
||||
|
||||
printf("\nstatic const unsigned char %s[] = {", name);
|
||||
for (u = 0; u < len; u ++) {
|
||||
if (u != 0) {
|
||||
printf(",");
|
||||
}
|
||||
if (u % 12 == 0) {
|
||||
printf("\n\t");
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
printf("0x%02X", buf[u]);
|
||||
}
|
||||
printf("\n};\n");
|
||||
}
|
||||
|
||||
static int
|
||||
print_ta_internals(br_x509_trust_anchor *ta, long ctr)
|
||||
{
|
||||
char tmp[25];
|
||||
|
||||
sprintf(tmp, "TA%ld_DN", ctr);
|
||||
print_blob(tmp, ta->dn, ta->dn_len);
|
||||
switch (ta->pkey.key_type) {
|
||||
case BR_KEYTYPE_RSA:
|
||||
sprintf(tmp, "TA%ld_RSA_N", ctr);
|
||||
print_blob(tmp, ta->pkey.key.rsa.n, ta->pkey.key.rsa.nlen);
|
||||
sprintf(tmp, "TA%ld_RSA_E", ctr);
|
||||
print_blob(tmp, ta->pkey.key.rsa.e, ta->pkey.key.rsa.elen);
|
||||
break;
|
||||
case BR_KEYTYPE_EC:
|
||||
sprintf(tmp, "TA%ld_EC_Q", ctr);
|
||||
print_blob(tmp, ta->pkey.key.ec.q, ta->pkey.key.ec.qlen);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "ERROR: unknown anchor key type '%d'\n",
|
||||
ta->pkey.key_type);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
print_ta(br_x509_trust_anchor *ta, long ctr)
|
||||
{
|
||||
char tmp[25];
|
||||
|
||||
printf("\t{\n");
|
||||
printf("\t\t(unsigned char *)TA%ld_DN, sizeof TA%ld_DN,\n", ctr, ctr);
|
||||
printf("\t\t%s,\n", (ta->flags & BR_X509_TA_CA)
|
||||
? "BR_X509_TA_CA" : "0");
|
||||
printf("\t\t{\n");
|
||||
switch (ta->pkey.key_type) {
|
||||
const char *cname;
|
||||
|
||||
case BR_KEYTYPE_RSA:
|
||||
printf("\t\t\tBR_KEYTYPE_RSA,\n");
|
||||
printf("\t\t\t{ .rsa = {\n");
|
||||
printf("\t\t\t\t(unsigned char *)TA%ld_RSA_N,"
|
||||
" sizeof TA%ld_RSA_N,\n", ctr, ctr);
|
||||
printf("\t\t\t\t(unsigned char *)TA%ld_RSA_E,"
|
||||
" sizeof TA%ld_RSA_E,\n", ctr, ctr);
|
||||
printf("\t\t\t} }\n");
|
||||
break;
|
||||
case BR_KEYTYPE_EC:
|
||||
printf("\t\t\tBR_KEYTYPE_EC,\n");
|
||||
printf("\t\t\t{ .ec = {\n");
|
||||
cname = curve_to_sym(ta->pkey.key.ec.curve);
|
||||
if (cname == NULL) {
|
||||
sprintf(tmp, "%d", ta->pkey.key.ec.curve);
|
||||
cname = tmp;
|
||||
}
|
||||
printf("\t\t\t\t%s,\n", cname);
|
||||
printf("\t\t\t\t(unsigned char *)TA%ld_EC_Q,"
|
||||
" sizeof TA%ld_EC_Q,\n", ctr, ctr);
|
||||
printf("\t\t\t} }\n");
|
||||
}
|
||||
printf("\t\t}\n");
|
||||
printf("\t}");
|
||||
}
|
||||
|
||||
static void
|
||||
usage_ta(void)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"usage: brssl ta [ options ] file...\n");
|
||||
fprintf(stderr,
|
||||
"options:\n");
|
||||
fprintf(stderr,
|
||||
" -q suppress verbose messages\n");
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
int
|
||||
do_ta(int argc, char *argv[])
|
||||
{
|
||||
int retcode;
|
||||
int verbose;
|
||||
int i, num_files;
|
||||
anchor_list tas = VEC_INIT;
|
||||
size_t u, num;
|
||||
|
||||
retcode = 0;
|
||||
verbose = 1;
|
||||
num_files = 0;
|
||||
for (i = 0; i < argc; i ++) {
|
||||
const char *arg;
|
||||
|
||||
arg = argv[i];
|
||||
if (arg[0] != '-') {
|
||||
num_files ++;
|
||||
continue;
|
||||
}
|
||||
argv[i] = NULL;
|
||||
if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
|
||||
verbose = 1;
|
||||
} else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
|
||||
verbose = 0;
|
||||
} else {
|
||||
fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
|
||||
usage_ta();
|
||||
goto ta_exit_error;
|
||||
}
|
||||
}
|
||||
if (num_files == 0) {
|
||||
fprintf(stderr, "ERROR: no certificate file provided\n");
|
||||
usage_ta();
|
||||
goto ta_exit_error;
|
||||
}
|
||||
|
||||
for (i = 0; i < argc; i ++) {
|
||||
const char *fname;
|
||||
size_t len1, len2;
|
||||
|
||||
fname = argv[i];
|
||||
if (fname == NULL) {
|
||||
continue;
|
||||
}
|
||||
if (verbose) {
|
||||
fprintf(stderr, "Reading file '%s': ", fname);
|
||||
fflush(stderr);
|
||||
}
|
||||
len1 = VEC_LEN(tas);
|
||||
if (read_trust_anchors(&tas, fname) == 0) {
|
||||
goto ta_exit_error;
|
||||
}
|
||||
len2 = VEC_LEN(tas) - len1;
|
||||
if (verbose) {
|
||||
fprintf(stderr, "%lu trust anchor%s\n",
|
||||
(unsigned long)len2, len2 > 1 ? "s" : "");
|
||||
}
|
||||
}
|
||||
num = VEC_LEN(tas);
|
||||
for (u = 0; u < num; u ++) {
|
||||
if (print_ta_internals(&VEC_ELT(tas, u), u) < 0) {
|
||||
goto ta_exit_error;
|
||||
}
|
||||
}
|
||||
printf("\nstatic const br_x509_trust_anchor TAs[%ld] = {", (long)num);
|
||||
for (u = 0; u < num; u ++) {
|
||||
if (u != 0) {
|
||||
printf(",");
|
||||
}
|
||||
printf("\n");
|
||||
print_ta(&VEC_ELT(tas, u), u);
|
||||
}
|
||||
printf("\n};\n");
|
||||
printf("\n#define TAs_NUM %ld\n", (long)num);
|
||||
|
||||
/*
|
||||
* Release allocated structures.
|
||||
*/
|
||||
ta_exit:
|
||||
VEC_CLEAREXT(tas, free_ta_contents);
|
||||
return retcode;
|
||||
|
||||
ta_exit_error:
|
||||
retcode = -1;
|
||||
goto ta_exit;
|
||||
}
|
||||
66
tools/vector.c
Normal file
66
tools/vector.c
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "brssl.h"
|
||||
|
||||
/*
|
||||
* Prepare a vector buffer for adding 'extra' elements.
|
||||
* buf current buffer
|
||||
* esize size of a vector element
|
||||
* ptr pointer to the 'ptr' vector field
|
||||
* len pointer to the 'len' vector field
|
||||
* extra number of elements to add
|
||||
*
|
||||
* If the buffer must be enlarged, then this function allocates the new
|
||||
* buffer and releases the old one. The new buffer address is then returned.
|
||||
* If the buffer needs not be enlarged, then the buffer address is returned.
|
||||
*
|
||||
* In case of enlargement, the 'len' field is adjusted accordingly. The
|
||||
* 'ptr' field is not modified.
|
||||
*/
|
||||
void *
|
||||
vector_expand(void *buf,
|
||||
size_t esize, size_t *ptr, size_t *len, size_t extra)
|
||||
{
|
||||
size_t nlen;
|
||||
void *nbuf;
|
||||
|
||||
if (*len - *ptr >= extra) {
|
||||
return buf;
|
||||
}
|
||||
nlen = (*len << 1);
|
||||
if (nlen - *ptr < extra) {
|
||||
nlen = extra + *ptr;
|
||||
if (nlen < 8) {
|
||||
nlen = 8;
|
||||
}
|
||||
}
|
||||
nbuf = xmalloc(nlen * esize);
|
||||
if (buf != NULL) {
|
||||
memcpy(nbuf, buf, *len * esize);
|
||||
xfree(buf);
|
||||
}
|
||||
*len = nlen;
|
||||
return nbuf;
|
||||
}
|
||||
364
tools/verify.c
Normal file
364
tools/verify.c
Normal file
|
|
@ -0,0 +1,364 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "brssl.h"
|
||||
#include "bearssl.h"
|
||||
|
||||
static unsigned
|
||||
rsa_bit_length(const br_rsa_public_key *pk)
|
||||
{
|
||||
size_t u;
|
||||
unsigned x, bl;
|
||||
|
||||
for (u = 0; u < pk->nlen; u ++) {
|
||||
if (pk->n[u] != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (u == pk->nlen) {
|
||||
return 0;
|
||||
}
|
||||
bl = (unsigned)(pk->nlen - u - 1) << 3;
|
||||
x = pk->n[u];
|
||||
while (x != 0) {
|
||||
bl ++;
|
||||
x >>= 1;
|
||||
}
|
||||
return bl;
|
||||
}
|
||||
|
||||
static void
|
||||
print_rsa(const br_rsa_public_key *pk, int print_text, int print_C)
|
||||
{
|
||||
if (print_text) {
|
||||
size_t u;
|
||||
|
||||
printf("n = ");
|
||||
for (u = 0; u < pk->nlen; u ++) {
|
||||
printf("%02X", pk->n[u]);
|
||||
}
|
||||
printf("\n");
|
||||
printf("e = ");
|
||||
for (u = 0; u < pk->elen; u ++) {
|
||||
printf("%02X", pk->e[u]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
if (print_C) {
|
||||
size_t u;
|
||||
|
||||
printf("\nstatic const unsigned char RSA_N[] = {");
|
||||
for (u = 0; u < pk->nlen; u ++) {
|
||||
if (u != 0) {
|
||||
printf(",");
|
||||
}
|
||||
if (u % 12 == 0) {
|
||||
printf("\n\t");
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
printf("0x%02X", pk->n[u]);
|
||||
}
|
||||
printf("\n};\n");
|
||||
printf("\nstatic const unsigned char RSA_E[] = {");
|
||||
for (u = 0; u < pk->elen; u ++) {
|
||||
if (u != 0) {
|
||||
printf(",");
|
||||
}
|
||||
if (u % 12 == 0) {
|
||||
printf("\n\t");
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
printf("0x%02X", pk->e[u]);
|
||||
}
|
||||
printf("\n};\n");
|
||||
printf("\nstatic const br_rsa_public_key RSA = {\n");
|
||||
printf("\t(unsigned char *)RSA_N, sizeof RSA_N,\n");
|
||||
printf("\t(unsigned char *)RSA_E, sizeof RSA_E\n");
|
||||
printf("};\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
print_ec(const br_ec_public_key *pk, int print_text, int print_C)
|
||||
{
|
||||
if (print_text) {
|
||||
size_t u;
|
||||
|
||||
printf("Q = ");
|
||||
for (u = 0; u < pk->qlen; u ++) {
|
||||
printf("%02X", pk->q[u]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
if (print_C) {
|
||||
size_t u;
|
||||
|
||||
printf("\nstatic const unsigned char EC_Q[] = {");
|
||||
for (u = 0; u < pk->qlen; u ++) {
|
||||
if (u != 0) {
|
||||
printf(",");
|
||||
}
|
||||
if (u % 12 == 0) {
|
||||
printf("\n\t");
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
printf("0x%02X", pk->q[u]);
|
||||
}
|
||||
printf("\n};\n");
|
||||
printf("\nstatic const br_ec_public_key EC = {\n");
|
||||
printf("\t%d,\n", pk->curve);
|
||||
printf("\t(unsigned char *)EC_Q, sizeof EC_Q\n");
|
||||
printf("};\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
usage_verify(void)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"usage: brssl verify [ options ] file...\n");
|
||||
fprintf(stderr,
|
||||
"options:\n");
|
||||
fprintf(stderr,
|
||||
" -q suppress verbose messages\n");
|
||||
fprintf(stderr,
|
||||
" -sni name check presence of a specific server name\n");
|
||||
fprintf(stderr,
|
||||
" -CA file add certificates in 'file' to trust anchors\n");
|
||||
fprintf(stderr,
|
||||
" -text print public key details (human-readable)\n");
|
||||
fprintf(stderr,
|
||||
" -C print public key details (C code)\n");
|
||||
}
|
||||
|
||||
typedef VECTOR(br_x509_certificate) cert_list;
|
||||
|
||||
static void
|
||||
free_cert_contents(br_x509_certificate *xc)
|
||||
{
|
||||
xfree(xc->data);
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
int
|
||||
do_verify(int argc, char *argv[])
|
||||
{
|
||||
int retcode;
|
||||
int verbose;
|
||||
int i;
|
||||
const char *sni;
|
||||
anchor_list anchors = VEC_INIT;
|
||||
cert_list chain = VEC_INIT;
|
||||
size_t u;
|
||||
br_x509_minimal_context mc;
|
||||
int err_keyx, err_sign;
|
||||
int print_text, print_C;
|
||||
br_x509_pkey *pk;
|
||||
|
||||
retcode = 0;
|
||||
verbose = 1;
|
||||
sni = NULL;
|
||||
print_text = 0;
|
||||
print_C = 0;
|
||||
pk = NULL;
|
||||
err_keyx = 0;
|
||||
err_sign = 0;
|
||||
for (i = 0; i < argc; i ++) {
|
||||
const char *arg;
|
||||
|
||||
arg = argv[i];
|
||||
if (arg[0] != '-') {
|
||||
br_x509_certificate *xcs;
|
||||
size_t num;
|
||||
|
||||
xcs = read_certificates(arg, &num);
|
||||
if (xcs == NULL) {
|
||||
usage_verify();
|
||||
goto verify_exit_error;
|
||||
}
|
||||
VEC_ADDMANY(chain, xcs, num);
|
||||
xfree(xcs);
|
||||
continue;
|
||||
}
|
||||
if (eqstr(arg, "-v") || eqstr(arg, "-verbose")) {
|
||||
verbose = 1;
|
||||
} else if (eqstr(arg, "-q") || eqstr(arg, "-quiet")) {
|
||||
verbose = 0;
|
||||
} else if (eqstr(arg, "-sni")) {
|
||||
if (++ i >= argc) {
|
||||
fprintf(stderr,
|
||||
"ERROR: no argument for '-sni'\n");
|
||||
usage_verify();
|
||||
goto verify_exit_error;
|
||||
}
|
||||
if (sni != NULL) {
|
||||
fprintf(stderr, "ERROR: duplicate SNI\n");
|
||||
usage_verify();
|
||||
goto verify_exit_error;
|
||||
}
|
||||
sni = argv[i];
|
||||
continue;
|
||||
} else if (eqstr(arg, "-CA")) {
|
||||
if (++ i >= argc) {
|
||||
fprintf(stderr,
|
||||
"ERROR: no argument for '-CA'\n");
|
||||
usage_verify();
|
||||
goto verify_exit_error;
|
||||
}
|
||||
arg = argv[i];
|
||||
if (read_trust_anchors(&anchors, arg) == 0) {
|
||||
usage_verify();
|
||||
goto verify_exit_error;
|
||||
}
|
||||
continue;
|
||||
} else if (eqstr(arg, "-text")) {
|
||||
print_text = 1;
|
||||
} else if (eqstr(arg, "-C")) {
|
||||
print_C = 1;
|
||||
} else {
|
||||
fprintf(stderr, "ERROR: unknown option: '%s'\n", arg);
|
||||
usage_verify();
|
||||
goto verify_exit_error;
|
||||
}
|
||||
}
|
||||
if (VEC_LEN(chain) == 0) {
|
||||
fprintf(stderr, "ERROR: no certificate chain provided\n");
|
||||
usage_verify();
|
||||
goto verify_exit_error;
|
||||
}
|
||||
br_x509_minimal_init(&mc, &br_sha256_vtable,
|
||||
&VEC_ELT(anchors, 0), VEC_LEN(anchors));
|
||||
br_x509_minimal_set_hash(&mc, br_sha1_ID, &br_sha1_vtable);
|
||||
br_x509_minimal_set_hash(&mc, br_sha224_ID, &br_sha224_vtable);
|
||||
br_x509_minimal_set_hash(&mc, br_sha256_ID, &br_sha256_vtable);
|
||||
br_x509_minimal_set_hash(&mc, br_sha384_ID, &br_sha384_vtable);
|
||||
br_x509_minimal_set_hash(&mc, br_sha512_ID, &br_sha512_vtable);
|
||||
br_x509_minimal_set_rsa(&mc, &br_rsa_i31_pkcs1_vrfy);
|
||||
br_x509_minimal_set_ecdsa(&mc,
|
||||
&br_ec_prime_i31, &br_ecdsa_i31_vrfy_asn1);
|
||||
for (i = 0; i < 2; i ++) {
|
||||
const br_x509_pkey *tpk;
|
||||
int err;
|
||||
|
||||
mc.vtable->start_chain(&mc.vtable,
|
||||
i == 0 ? BR_KEYTYPE_KEYX : BR_KEYTYPE_SIGN, sni);
|
||||
for (u = 0; u < VEC_LEN(chain); u ++) {
|
||||
br_x509_certificate *xc;
|
||||
|
||||
xc = &VEC_ELT(chain, u);
|
||||
mc.vtable->start_cert(&mc.vtable, xc->data_len);
|
||||
mc.vtable->append(&mc.vtable, xc->data, xc->data_len);
|
||||
mc.vtable->end_cert(&mc.vtable);
|
||||
}
|
||||
err = mc.vtable->end_chain(&mc.vtable);
|
||||
if (i == 0) {
|
||||
err_keyx = err;
|
||||
} else {
|
||||
err_sign = err;
|
||||
}
|
||||
tpk = mc.vtable->get_pkey(&mc.vtable);
|
||||
if (pk == NULL && tpk != NULL) {
|
||||
pk = xpkeydup(tpk);
|
||||
}
|
||||
}
|
||||
if (err_keyx == 0 || err_sign == 0) {
|
||||
if (verbose) {
|
||||
fprintf(stderr, "Validation success");
|
||||
if (err_keyx == 0 && err_sign == 0) {
|
||||
fprintf(stderr, " (key exchange, sign)\n");
|
||||
} else if (err_keyx == 0) {
|
||||
fprintf(stderr, " (key exchange)\n");
|
||||
} else if (err_sign == 0) {
|
||||
fprintf(stderr, " (signature)\n");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (verbose) {
|
||||
int err;
|
||||
const char *errname, *errmsg;
|
||||
|
||||
/*
|
||||
* If the two error codes differ, we want the one
|
||||
* which is not a "forbidden key usage".
|
||||
*/
|
||||
err = err_keyx;
|
||||
if (err == BR_ERR_X509_FORBIDDEN_KEY_USAGE) {
|
||||
err = err_sign;
|
||||
}
|
||||
fprintf(stderr, "Validation failed, err = %d", err);
|
||||
errname = find_error_name(err, &errmsg);
|
||||
if (errname != NULL) {
|
||||
fprintf(stderr, " (%s): %s\n", errname, errmsg);
|
||||
} else {
|
||||
fprintf(stderr, " (unknown)\n");
|
||||
}
|
||||
}
|
||||
retcode = -1;
|
||||
}
|
||||
if (pk != NULL) {
|
||||
switch (pk->key_type) {
|
||||
case BR_KEYTYPE_RSA:
|
||||
if (verbose) {
|
||||
fprintf(stderr, "Key type: RSA (%u bits)\n",
|
||||
rsa_bit_length(&pk->key.rsa));
|
||||
}
|
||||
print_rsa(&pk->key.rsa, print_text, print_C);
|
||||
break;
|
||||
case BR_KEYTYPE_EC:
|
||||
if (verbose) {
|
||||
fprintf(stderr, "Key type: EC (%s)\n",
|
||||
ec_curve_name(pk->key.ec.curve));
|
||||
}
|
||||
print_ec(&pk->key.ec, print_text, print_C);
|
||||
break;
|
||||
default:
|
||||
if (verbose) {
|
||||
fprintf(stderr, "Unknown key type\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Release allocated structures.
|
||||
*/
|
||||
verify_exit:
|
||||
VEC_CLEAREXT(anchors, &free_ta_contents);
|
||||
VEC_CLEAREXT(chain, &free_cert_contents);
|
||||
xfreepkey(pk);
|
||||
return retcode;
|
||||
|
||||
verify_exit_error:
|
||||
retcode = -1;
|
||||
goto verify_exit;
|
||||
}
|
||||
120
tools/xmem.c
Normal file
120
tools/xmem.c
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
/*
|
||||
* Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "brssl.h"
|
||||
|
||||
/* see brssl.h */
|
||||
void *
|
||||
xmalloc(size_t len)
|
||||
{
|
||||
void *buf;
|
||||
|
||||
if (len == 0) {
|
||||
return NULL;
|
||||
}
|
||||
buf = malloc(len);
|
||||
if (buf == NULL) {
|
||||
fprintf(stderr, "ERROR: could not allocate %lu byte(s)\n",
|
||||
(unsigned long)len);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
void
|
||||
xfree(void *buf)
|
||||
{
|
||||
if (buf != NULL) {
|
||||
free(buf);
|
||||
}
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
void *
|
||||
xblobdup(const void *src, size_t len)
|
||||
{
|
||||
void *buf;
|
||||
|
||||
buf = xmalloc(len);
|
||||
memcpy(buf, src, len);
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
char *
|
||||
xstrdup(const void *src)
|
||||
{
|
||||
return xblobdup(src, strlen(src) + 1);
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
br_x509_pkey *
|
||||
xpkeydup(const br_x509_pkey *pk)
|
||||
{
|
||||
br_x509_pkey *pk2;
|
||||
|
||||
pk2 = xmalloc(sizeof *pk2);
|
||||
pk2->key_type = pk->key_type;
|
||||
switch (pk->key_type) {
|
||||
case BR_KEYTYPE_RSA:
|
||||
pk2->key.rsa.n = xblobdup(pk->key.rsa.n, pk->key.rsa.nlen);
|
||||
pk2->key.rsa.nlen = pk->key.rsa.nlen;
|
||||
pk2->key.rsa.e = xblobdup(pk->key.rsa.e, pk->key.rsa.elen);
|
||||
pk2->key.rsa.elen = pk->key.rsa.elen;
|
||||
break;
|
||||
case BR_KEYTYPE_EC:
|
||||
pk2->key.ec.curve = pk->key.ec.curve;
|
||||
pk2->key.ec.q = xblobdup(pk->key.ec.q, pk->key.ec.qlen);
|
||||
pk2->key.ec.qlen = pk->key.ec.qlen;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Unknown public key type: %u\n",
|
||||
(unsigned)pk->key_type);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return pk2;
|
||||
}
|
||||
|
||||
/* see brssl.h */
|
||||
void
|
||||
xfreepkey(br_x509_pkey *pk)
|
||||
{
|
||||
if (pk != NULL) {
|
||||
switch (pk->key_type) {
|
||||
case BR_KEYTYPE_RSA:
|
||||
xfree(pk->key.rsa.n);
|
||||
xfree(pk->key.rsa.e);
|
||||
break;
|
||||
case BR_KEYTYPE_EC:
|
||||
xfree(pk->key.ec.q);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Unknown public key type: %u\n",
|
||||
(unsigned)pk->key_type);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
xfree(pk);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue