Files
wolfssl/src/ssl.c

19710 lines
513 KiB
C
Raw Normal View History

2011-02-05 11:14:47 -08:00
/* ssl.c
*
2016-03-17 16:02:13 -06:00
* Copyright (C) 2006-2016 wolfSSL Inc.
2011-02-05 11:14:47 -08:00
*
2016-03-17 16:02:13 -06:00
* This file is part of wolfSSL.
2011-02-05 11:14:47 -08:00
*
2014-12-19 11:27:01 -07:00
* wolfSSL is free software; you can redistribute it and/or modify
2011-02-05 11:14:47 -08:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
2014-12-19 11:27:01 -07:00
* wolfSSL is distributed in the hope that it will be useful,
2011-02-05 11:14:47 -08:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2016-03-17 16:02:13 -06:00
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2011-02-05 11:14:47 -08:00
*/
2016-03-17 16:02:13 -06:00
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
#include <wolfssl/wolfcrypt/settings.h>
2015-09-10 16:24:25 -07:00
#ifndef WOLFCRYPT_ONLY
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
2014-12-19 11:27:01 -07:00
#include <wolfssl/internal.h>
#include <wolfssl/error-ssl.h>
#include <wolfssl/wolfcrypt/coding.h>
2015-09-14 17:13:16 -07:00
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
2015-09-14 17:13:16 -07:00
#include <wolfcrypt/src/misc.c>
#endif
2011-02-05 11:14:47 -08:00
#ifndef WOLFSSL_ALLOW_NO_SUITES
#if defined(NO_DH) && !defined(HAVE_ECC) && !defined(WOLFSSL_STATIC_RSA) \
&& !defined(WOLFSSL_STATIC_DH) && !defined(WOLFSSL_STATIC_PSK)
2015-09-22 15:40:46 -06:00
#error "No cipher suites defined because DH disabled, ECC disabled, and no static suites defined. Please see top of README"
#endif
#endif
#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
defined(WOLFSSL_KEY_GEN)
2014-12-19 11:27:01 -07:00
#include <wolfssl/openssl/evp.h>
2015-07-31 11:13:05 -07:00
/* openssl headers end, wolfssl internal headers next */
#include <wolfssl/wolfcrypt/wc_encrypt.h>
#endif
2011-02-05 11:14:47 -08:00
#ifdef OPENSSL_EXTRA
/* openssl headers begin */
2014-12-19 11:27:01 -07:00
#include <wolfssl/openssl/hmac.h>
#include <wolfssl/openssl/crypto.h>
#include <wolfssl/openssl/des.h>
#include <wolfssl/openssl/bn.h>
#include <wolfssl/openssl/dh.h>
#include <wolfssl/openssl/rsa.h>
#include <wolfssl/openssl/pem.h>
2015-07-01 12:24:11 -07:00
#include <wolfssl/openssl/ec.h>
#include <wolfssl/openssl/ec25519.h>
#include <wolfssl/openssl/ed25519.h>
2015-07-01 12:24:11 -07:00
#include <wolfssl/openssl/ecdsa.h>
#include <wolfssl/openssl/ecdh.h>
2014-12-19 11:27:01 -07:00
/* openssl headers end, wolfssl internal headers next */
#include <wolfssl/wolfcrypt/hmac.h>
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/wolfcrypt/des3.h>
#include <wolfssl/wolfcrypt/md4.h>
#include <wolfssl/wolfcrypt/md5.h>
#include <wolfssl/wolfcrypt/arc4.h>
#include <wolfssl/wolfcrypt/idea.h>
#include <wolfssl/wolfcrypt/curve25519.h>
#include <wolfssl/wolfcrypt/ed25519.h>
#ifdef HAVE_STUNNEL
#include <wolfssl/openssl/ocsp.h>
#endif /* WITH_STUNNEL */
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SHA512
#include <wolfssl/wolfcrypt/sha512.h>
2012-04-26 14:58:29 -07:00
#endif
2011-02-05 11:14:47 -08:00
#endif
#ifdef NO_ASN
#include <wolfssl/wolfcrypt/dh.h>
2011-02-05 11:14:47 -08:00
#endif
#ifndef NO_FILESYSTEM
2014-12-19 11:27:01 -07:00
#if !defined(USE_WINDOWS_API) && !defined(NO_WOLFSSL_DIR) \
2012-08-13 17:10:05 -06:00
&& !defined(EBSNET)
#include <dirent.h>
#include <sys/stat.h>
#endif
2012-08-13 17:10:05 -06:00
#ifdef EBSNET
#include "vfapi.h"
#include "vfile.h"
#endif
#endif /* NO_FILESYSTEM */
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
2011-02-05 11:14:47 -08:00
#ifndef WOLFSSL_HAVE_MIN
#define WOLFSSL_HAVE_MIN
2011-02-05 11:14:47 -08:00
static INLINE word32 min(word32 a, word32 b)
{
return a > b ? b : a;
}
#endif /* WOLFSSSL_HAVE_MIN */
2015-08-14 15:06:12 -07:00
#if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_HAVE_MAX)
#define WOLFSSL_HAVE_MAX
2011-02-05 11:14:47 -08:00
static INLINE word32 max(word32 a, word32 b)
{
return a > b ? a : b;
}
2015-08-14 15:06:12 -07:00
#endif /* WOLFSSL_DTLS && !WOLFSSL_HAVE_MAX */
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
#ifndef WOLFSSL_LEANPSK
char* mystrnstr(const char* s1, const char* s2, unsigned int n)
{
unsigned int s2_len = (unsigned int)XSTRLEN(s2);
if (s2_len == 0)
return (char*)s1;
while (n >= s2_len && s1[0]) {
if (s1[0] == s2[0])
if (XMEMCMP(s1, s2, s2_len) == 0)
return (char*)s1;
s1++;
n--;
}
return NULL;
}
#endif
2016-05-10 13:27:45 -06:00
#ifdef WOLFSSL_SESSION_EXPORT
#ifdef WOLFSSL_DTLS
int wolfSSL_dtls_import(WOLFSSL* ssl, unsigned char* buf, unsigned int sz)
{
WOLFSSL_ENTER("wolfSSL_session_import");
if (ssl == NULL || buf == NULL) {
return BAD_FUNC_ARG;
}
/* sanity checks on buffer and protocol are done in internal function */
return wolfSSL_dtls_import_internal(ssl, buf, sz);
2016-05-10 13:27:45 -06:00
}
/* Sets the function to call for serializing the session. This function is
* called right after the handshake is completed. */
2016-05-10 13:27:45 -06:00
int wolfSSL_CTX_dtls_set_export(WOLFSSL_CTX* ctx, wc_dtls_export func)
{
WOLFSSL_ENTER("wolfSSL_CTX_dtls_set_export");
/* purposefully allow func to be NULL */
if (ctx == NULL) {
return BAD_FUNC_ARG;
}
ctx->dtls_export = func;
return SSL_SUCCESS;
}
/* Sets the function in WOLFSSL struct to call for serializing the session. This
* function is called right after the handshake is completed. */
2016-05-10 13:27:45 -06:00
int wolfSSL_dtls_set_export(WOLFSSL* ssl, wc_dtls_export func)
{
WOLFSSL_ENTER("wolfSSL_dtls_set_export");
/* purposefully allow func to be NULL */
if (ssl == NULL) {
return BAD_FUNC_ARG;
}
ssl->dtls_export = func;
return SSL_SUCCESS;
}
/* This function allows for directly serializing a session rather than using
* callbacks. It has less overhead by removing a temporary buffer and gives
* control over when the session gets serialized. When using callbacks the
* session is always serialized immediatly after the handshake is finished.
*
* buf is the argument to contain the serialized session
* sz is the size of the buffer passed in
* ssl is the WOLFSSL struct to serialize
* returns the size of serialized session on success, 0 on no action, and
* negative value on error */
int wolfSSL_dtls_export(WOLFSSL* ssl, unsigned char* buf, unsigned int* sz)
{
WOLFSSL_ENTER("wolfSSL_dtls_export");
if (ssl == NULL || sz == NULL) {
return BAD_FUNC_ARG;
}
if (buf == NULL) {
*sz = MAX_EXPORT_BUFFER;
return 0;
}
/* if not DTLS do nothing */
if (!ssl->options.dtls) {
WOLFSSL_MSG("Currently only DTLS export is supported");
return 0;
}
/* copy over keys, options, and dtls state struct */
return wolfSSL_dtls_export_internal(ssl, buf, *sz);
}
/* returns 0 on success */
2016-05-10 13:27:45 -06:00
int wolfSSL_send_session(WOLFSSL* ssl)
{
int ret;
byte* buf;
word16 bufSz = MAX_EXPORT_BUFFER;
WOLFSSL_ENTER("wolfSSL_send_session");
if (ssl == NULL) {
return BAD_FUNC_ARG;
}
buf = (byte*)XMALLOC(bufSz, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
if (buf == NULL) {
return MEMORY_E;
}
/* if not DTLS do nothing */
if (!ssl->options.dtls) {
XFREE(buf, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
WOLFSSL_MSG("Currently only DTLS export is supported");
return 0;
2016-05-10 13:27:45 -06:00
}
/* copy over keys, options, and dtls state struct */
ret = wolfSSL_dtls_export_internal(ssl, buf, bufSz);
if (ret < 0) {
2016-05-10 13:27:45 -06:00
XFREE(buf, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
2016-05-10 13:27:45 -06:00
}
/* if no error ret has size of buffer */
ret = ssl->dtls_export(ssl, buf, ret, NULL);
if (ret != SSL_SUCCESS) {
2016-05-10 13:27:45 -06:00
XFREE(buf, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
2016-05-10 13:27:45 -06:00
}
XFREE(buf, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
return 0;
2016-05-10 13:27:45 -06:00
}
#endif /* WOLFSSL_DTLS */
#endif /* WOLFSSL_SESSION_EXPORT */
2011-02-05 11:14:47 -08:00
2012-12-06 09:26:59 -08:00
/* prevent multiple mutex initializations */
static volatile int initRefCount = 0;
2014-12-19 11:27:01 -07:00
static wolfSSL_Mutex count_mutex; /* init ref count mutex */
2012-12-06 09:26:59 -08:00
/* Create a new WOLFSSL_CTX struct and return the pointer to created struct.
WOLFSSL_METHOD pointer passed in is given to ctx to manage.
This function frees the passed in WOLFSSL_METHOD struct on failure and on
success is freed when ctx is freed.
*/
WOLFSSL_CTX* wolfSSL_CTX_new_ex(WOLFSSL_METHOD* method, void* heap)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_CTX* ctx = NULL;
WOLFSSL_ENTER("WOLFSSL_CTX_new_ex");
if (initRefCount == 0) {
/* user no longer forced to call Init themselves */
int ret = wolfSSL_Init();
if (ret != SSL_SUCCESS) {
WOLFSSL_MSG("wolfSSL_Init failed");
WOLFSSL_LEAVE("WOLFSSL_CTX_new", 0);
if (method != NULL) {
XFREE(method, heap, DYNAMIC_TYPE_METHOD);
}
return NULL;
}
}
2012-12-06 09:26:59 -08:00
if (method == NULL)
return ctx;
ctx = (WOLFSSL_CTX*) XMALLOC(sizeof(WOLFSSL_CTX), heap, DYNAMIC_TYPE_CTX);
if (ctx) {
if (InitSSL_Ctx(ctx, method, heap) < 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Init CTX failed");
wolfSSL_CTX_free(ctx);
ctx = NULL;
}
}
else {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Alloc CTX failed, method freed");
XFREE(method, heap, DYNAMIC_TYPE_METHOD);
}
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("WOLFSSL_CTX_new", 0);
2011-02-05 11:14:47 -08:00
return ctx;
}
WOLFSSL_CTX* wolfSSL_CTX_new(WOLFSSL_METHOD* method)
{
#ifdef WOLFSSL_HEAP_TEST
/* if testing the heap hint then set top level CTX to have test value */
return wolfSSL_CTX_new_ex(method, (void*)WOLFSSL_HEAP_TEST);
#else
return wolfSSL_CTX_new_ex(method, NULL);
#endif
}
2014-12-19 11:27:01 -07:00
void wolfSSL_CTX_free(WOLFSSL_CTX* ctx)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_CTX_free");
2011-02-05 11:14:47 -08:00
if (ctx)
FreeSSL_Ctx(ctx);
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("SSL_CTX_free", 0);
2011-02-05 11:14:47 -08:00
}
#ifdef SINGLE_THREADED
/* no locking in single threaded mode, allow a CTX level rng to be shared with
* WOLFSSL objects, SSL_SUCCESS on ok */
int wolfSSL_CTX_new_rng(WOLFSSL_CTX* ctx)
{
WC_RNG* rng;
int ret;
if (ctx == NULL) {
return BAD_FUNC_ARG;
}
rng = XMALLOC(sizeof(WC_RNG), ctx->heap, DYNAMIC_TYPE_RNG);
if (rng == NULL) {
return MEMORY_E;
}
#ifndef HAVE_FIPS
ret = wc_InitRng_ex(rng, ctx->heap);
#else
ret = wc_InitRng(rng);
#endif
if (ret != 0) {
XFREE(rng, ctx->heap, DYNAMIC_TYPE_RNG);
return ret;
}
ctx->rng = rng;
return SSL_SUCCESS;
}
#endif
2014-12-19 11:27:01 -07:00
WOLFSSL* wolfSSL_new(WOLFSSL_CTX* ctx)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL* ssl = NULL;
int ret = 0;
2013-05-17 09:49:46 -07:00
(void)ret;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_new");
if (ctx == NULL)
return ssl;
ssl = (WOLFSSL*) XMALLOC(sizeof(WOLFSSL), ctx->heap, DYNAMIC_TYPE_SSL);
2011-02-05 11:14:47 -08:00
if (ssl)
if ( (ret = InitSSL(ssl, ctx)) < 0) {
FreeSSL(ssl, ctx->heap);
2011-02-05 11:14:47 -08:00
ssl = 0;
}
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("SSL_new", ret);
2011-02-05 11:14:47 -08:00
return ssl;
}
2014-12-19 11:27:01 -07:00
void wolfSSL_free(WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_free");
2011-02-05 11:14:47 -08:00
if (ssl)
FreeSSL(ssl, ssl->ctx->heap);
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("SSL_free", 0);
2011-02-05 11:14:47 -08:00
}
2014-07-17 15:00:40 -06:00
#ifdef HAVE_POLY1305
/* set if to use old poly 1 for yes 0 to use new poly */
2014-12-19 11:27:01 -07:00
int wolfSSL_use_old_poly(WOLFSSL* ssl, int value)
2014-07-17 15:00:40 -06:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_use_old_poly");
WOLFSSL_MSG("Warning SSL connection auto detects old/new and this function"
"is depriciated");
ssl->options.oldPoly = (word16)value;
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("SSL_use_old_poly", 0);
2014-07-17 15:00:40 -06:00
return 0;
}
#endif
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
int wolfSSL_set_fd(WOLFSSL* ssl, int fd)
2011-02-05 11:14:47 -08:00
{
int ret;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_set_fd");
ret = wolfSSL_set_read_fd(ssl, fd);
if (ret == SSL_SUCCESS) {
ret = wolfSSL_set_write_fd(ssl, fd);
}
return ret;
}
int wolfSSL_set_read_fd(WOLFSSL* ssl, int fd)
{
WOLFSSL_ENTER("SSL_set_read_fd");
if (ssl == NULL) {
return BAD_FUNC_ARG;
}
2011-02-05 11:14:47 -08:00
ssl->rfd = fd; /* not used directly to allow IO callbacks */
ssl->IOCB_ReadCtx = &ssl->rfd;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_DTLS
if (ssl->options.dtls) {
ssl->IOCB_ReadCtx = &ssl->buffers.dtlsCtx;
ssl->buffers.dtlsCtx.rfd = fd;
}
#endif
WOLFSSL_LEAVE("SSL_set_read_fd", SSL_SUCCESS);
return SSL_SUCCESS;
}
int wolfSSL_set_write_fd(WOLFSSL* ssl, int fd)
{
WOLFSSL_ENTER("SSL_set_write_fd");
if (ssl == NULL) {
return BAD_FUNC_ARG;
}
ssl->wfd = fd; /* not used directly to allow IO callbacks */
ssl->IOCB_WriteCtx = &ssl->wfd;
#ifdef WOLFSSL_DTLS
if (ssl->options.dtls) {
ssl->IOCB_WriteCtx = &ssl->buffers.dtlsCtx;
ssl->buffers.dtlsCtx.wfd = fd;
}
#endif
WOLFSSL_LEAVE("SSL_set_write_fd", SSL_SUCCESS);
2011-02-05 11:14:47 -08:00
return SSL_SUCCESS;
}
2014-08-15 10:56:38 -07:00
2015-02-26 10:40:32 -07:00
/**
* Get the name of cipher at priority level passed in.
2015-02-26 10:40:32 -07:00
*/
char* wolfSSL_get_cipher_list(int priority)
{
const char* const* ciphers = GetCipherNames();
if (priority >= GetCipherNamesSize() || priority < 0) {
return 0;
}
return (char*)ciphers[priority];
}
2014-12-19 11:27:01 -07:00
int wolfSSL_get_ciphers(char* buf, int len)
{
const char* const* ciphers = GetCipherNames();
2014-08-15 10:56:38 -07:00
int totalInc = 0;
int step = 0;
char delim = ':';
int size = GetCipherNamesSize();
int i;
if (buf == NULL || len <= 0)
return BAD_FUNC_ARG;
/* Add each member to the buffer delimited by a : */
2014-08-15 10:56:38 -07:00
for (i = 0; i < size; i++) {
step = (int)(XSTRLEN(ciphers[i]) + 1); /* delimiter */
totalInc += step;
/* Check to make sure buf is large enough and will not overflow */
2014-08-15 10:56:38 -07:00
if (totalInc < len) {
XSTRNCPY(buf, ciphers[i], XSTRLEN(ciphers[i]));
buf += XSTRLEN(ciphers[i]);
if (i < size - 1)
*buf++ = delim;
else
*buf++ = '\0';
}
else
return BUFFER_E;
}
return SSL_SUCCESS;
}
2014-08-15 10:56:38 -07:00
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
int wolfSSL_get_fd(const WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_get_fd");
WOLFSSL_LEAVE("SSL_get_fd", ssl->rfd);
2011-02-05 11:14:47 -08:00
return ssl->rfd;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_get_using_nonblock(WOLFSSL* ssl)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_get_using_nonblock");
WOLFSSL_LEAVE("wolfSSL_get_using_nonblock", ssl->options.usingNonblock);
return ssl->options.usingNonblock;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_dtls(WOLFSSL* ssl)
{
return ssl->options.dtls;
}
2014-12-19 11:27:01 -07:00
#ifndef WOLFSSL_LEANPSK
void wolfSSL_set_using_nonblock(WOLFSSL* ssl, int nonblock)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_set_using_nonblock");
ssl->options.usingNonblock = (nonblock != 0);
}
2014-12-19 11:27:01 -07:00
int wolfSSL_dtls_set_peer(WOLFSSL* ssl, void* peer, unsigned int peerSz)
{
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_DTLS
void* sa = (void*)XMALLOC(peerSz, ssl->heap, DYNAMIC_TYPE_SOCKADDR);
if (sa != NULL) {
if (ssl->buffers.dtlsCtx.peer.sa != NULL)
XFREE(ssl->buffers.dtlsCtx.peer.sa,ssl->heap,DYNAMIC_TYPE_SOCKADDR);
XMEMCPY(sa, peer, peerSz);
ssl->buffers.dtlsCtx.peer.sa = sa;
ssl->buffers.dtlsCtx.peer.sz = peerSz;
return SSL_SUCCESS;
}
return SSL_FAILURE;
#else
(void)ssl;
(void)peer;
(void)peerSz;
return SSL_NOT_IMPLEMENTED;
#endif
}
2014-12-19 11:27:01 -07:00
int wolfSSL_dtls_get_peer(WOLFSSL* ssl, void* peer, unsigned int* peerSz)
{
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_DTLS
if (ssl == NULL) {
return SSL_FAILURE;
}
if (peer != NULL && peerSz != NULL
&& *peerSz >= ssl->buffers.dtlsCtx.peer.sz
&& ssl->buffers.dtlsCtx.peer.sa != NULL) {
*peerSz = ssl->buffers.dtlsCtx.peer.sz;
XMEMCPY(peer, ssl->buffers.dtlsCtx.peer.sa, *peerSz);
return SSL_SUCCESS;
}
return SSL_FAILURE;
#else
(void)ssl;
(void)peer;
(void)peerSz;
return SSL_NOT_IMPLEMENTED;
#endif
}
2016-08-03 08:12:55 -07:00
#if defined(WOLFSSL_SCTP) && defined(WOLFSSL_DTLS)
2016-08-03 08:12:55 -07:00
2016-08-22 08:49:10 -07:00
int wolfSSL_CTX_dtls_set_sctp(WOLFSSL_CTX* ctx)
{
WOLFSSL_ENTER("wolfSSL_CTX_dtls_set_sctp()");
if (ctx == NULL)
return BAD_FUNC_ARG;
2016-08-03 08:12:55 -07:00
2016-08-22 08:49:10 -07:00
ctx->dtlsSctp = 1;
2016-08-03 08:12:55 -07:00
return SSL_SUCCESS;
}
2016-08-22 08:49:10 -07:00
int wolfSSL_dtls_set_sctp(WOLFSSL* ssl)
2016-08-03 08:12:55 -07:00
{
2016-08-22 08:49:10 -07:00
WOLFSSL_ENTER("wolfSSL_dtls_set_sctp()");
2016-08-03 08:12:55 -07:00
if (ssl == NULL)
2016-08-22 08:49:10 -07:00
return BAD_FUNC_ARG;
2016-08-03 08:12:55 -07:00
2016-08-22 08:49:10 -07:00
ssl->options.dtlsSctp = 1;
2016-08-03 08:12:55 -07:00
return SSL_SUCCESS;
}
int wolfSSL_CTX_dtls_set_mtu(WOLFSSL_CTX* ctx, word16 newMtu)
2016-08-03 08:12:55 -07:00
{
if (ctx == NULL || newMtu > MAX_RECORD_SIZE)
2016-08-22 08:49:10 -07:00
return BAD_FUNC_ARG;
ctx->dtlsMtuSz = newMtu;
2016-08-03 08:12:55 -07:00
return SSL_SUCCESS;
}
int wolfSSL_dtls_set_mtu(WOLFSSL* ssl, word16 newMtu)
2016-08-03 08:12:55 -07:00
{
if (ssl == NULL)
2016-08-22 08:49:10 -07:00
return BAD_FUNC_ARG;
if (newMtu > MAX_RECORD_SIZE) {
ssl->error = BAD_FUNC_ARG;
2016-08-03 08:12:55 -07:00
return SSL_FAILURE;
2016-08-22 08:49:10 -07:00
}
2016-08-03 08:12:55 -07:00
ssl->dtlsMtuSz = newMtu;
2016-08-03 08:12:55 -07:00
return SSL_SUCCESS;
}
#endif /* WOLFSSL_DTLS && WOLFSSL_SCTP */
2014-12-19 11:27:01 -07:00
#endif /* WOLFSSL_LEANPSK */
2015-09-22 15:40:46 -06:00
/* return underlying connect or accept, SSL_SUCCESS on ok */
2014-12-19 11:27:01 -07:00
int wolfSSL_negotiate(WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
int err = SSL_FATAL_ERROR;
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_negotiate");
#ifndef NO_WOLFSSL_SERVER
if (ssl->options.side == WOLFSSL_SERVER_END)
err = wolfSSL_accept(ssl);
2011-02-05 11:14:47 -08:00
#endif
2014-12-19 11:27:01 -07:00
#ifndef NO_WOLFSSL_CLIENT
if (ssl->options.side == WOLFSSL_CLIENT_END)
err = wolfSSL_connect(ssl);
2011-02-05 11:14:47 -08:00
#endif
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("wolfSSL_negotiate", err);
2011-03-31 16:45:05 -07:00
return err;
2011-02-05 11:14:47 -08:00
}
2016-07-19 11:29:22 -07:00
WC_RNG* wolfSSL_GetRNG(WOLFSSL* ssl)
{
if (ssl) {
return ssl->rng;
}
return NULL;
}
2014-12-19 11:27:01 -07:00
#ifndef WOLFSSL_LEANPSK
2012-09-05 08:26:08 -07:00
/* object size based on build */
2014-12-19 11:27:01 -07:00
int wolfSSL_GetObjectSize(void)
2012-09-05 08:26:08 -07:00
{
2012-09-05 09:02:51 -07:00
#ifdef SHOW_SIZES
printf("sizeof suites = %lu\n", sizeof(Suites));
printf("sizeof ciphers(2) = %lu\n", sizeof(Ciphers));
#ifndef NO_RC4
printf(" sizeof arc4 = %lu\n", sizeof(Arc4));
#endif
printf(" sizeof aes = %lu\n", sizeof(Aes));
#ifndef NO_DES3
printf(" sizeof des3 = %lu\n", sizeof(Des3));
#endif
#ifndef NO_RABBIT
printf(" sizeof rabbit = %lu\n", sizeof(Rabbit));
2014-07-01 14:16:44 -06:00
#endif
#ifdef HAVE_CHACHA
printf(" sizeof chacha = %lu\n", sizeof(ChaCha));
#endif
printf("sizeof cipher specs = %lu\n", sizeof(CipherSpecs));
printf("sizeof keys = %lu\n", sizeof(Keys));
printf("sizeof Hashes(2) = %lu\n", sizeof(Hashes));
#ifndef NO_MD5
printf(" sizeof MD5 = %lu\n", sizeof(Md5));
#endif
#ifndef NO_SHA
printf(" sizeof SHA = %lu\n", sizeof(Sha));
#endif
#ifdef WOLFSSL_SHA224
printf(" sizeof SHA224 = %lu\n", sizeof(Sha224));
#endif
#ifndef NO_SHA256
printf(" sizeof SHA256 = %lu\n", sizeof(Sha256));
#endif
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SHA384
printf(" sizeof SHA384 = %lu\n", sizeof(Sha384));
#endif
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SHA384
printf(" sizeof SHA512 = %lu\n", sizeof(Sha512));
#endif
printf("sizeof Buffers = %lu\n", sizeof(Buffers));
printf("sizeof Options = %lu\n", sizeof(Options));
printf("sizeof Arrays = %lu\n", sizeof(Arrays));
#ifndef NO_RSA
printf("sizeof RsaKey = %lu\n", sizeof(RsaKey));
#endif
#ifdef HAVE_ECC
printf("sizeof ecc_key = %lu\n", sizeof(ecc_key));
#endif
2014-12-19 11:27:01 -07:00
printf("sizeof WOLFSSL_CIPHER = %lu\n", sizeof(WOLFSSL_CIPHER));
printf("sizeof WOLFSSL_SESSION = %lu\n", sizeof(WOLFSSL_SESSION));
printf("sizeof WOLFSSL = %lu\n", sizeof(WOLFSSL));
printf("sizeof WOLFSSL_CTX = %lu\n", sizeof(WOLFSSL_CTX));
2012-09-05 09:02:51 -07:00
#endif
2014-12-19 11:27:01 -07:00
return sizeof(WOLFSSL);
2012-09-05 08:26:08 -07:00
}
#endif
#ifdef WOLFSSL_STATIC_MEMORY
int wolfSSL_CTX_load_static_memory(WOLFSSL_CTX** ctx, wolfSSL_method_func method,
unsigned char* buf, unsigned int sz,
int flag, int max)
{
WOLFSSL_HEAP* heap;
WOLFSSL_HEAP_HINT* hint;
word32 idx = 0;
if (ctx == NULL || buf == NULL) {
return BAD_FUNC_ARG;
}
if (*ctx == NULL && method == NULL) {
return BAD_FUNC_ARG;
}
if (*ctx == NULL || (*ctx)->heap == NULL) {
2016-07-19 15:57:09 -06:00
if (sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT) > sz - idx) {
return BUFFER_E; /* not enough memory for structures */
}
heap = (WOLFSSL_HEAP*)buf;
idx += sizeof(WOLFSSL_HEAP);
if (wolfSSL_init_memory_heap(heap) != 0) {
return SSL_FAILURE;
}
hint = (WOLFSSL_HEAP_HINT*)(buf + idx);
idx += sizeof(WOLFSSL_HEAP_HINT);
XMEMSET(hint, 0, sizeof(WOLFSSL_HEAP_HINT));
hint->memory = heap;
if (*ctx && (*ctx)->heap == NULL) {
(*ctx)->heap = (void*)hint;
}
}
else {
#ifdef WOLFSSL_HEAP_TEST
/* do not load in memory if test has been set */
if ((*ctx)->heap == (void*)WOLFSSL_HEAP_TEST) {
return SSL_SUCCESS;
}
#endif
hint = (WOLFSSL_HEAP_HINT*)((*ctx)->heap);
heap = hint->memory;
}
if (wolfSSL_load_static_memory(buf + idx, sz - idx, flag, heap) != 1) {
WOLFSSL_MSG("Error partitioning memory");
return SSL_FAILURE;
}
/* create ctx if needed */
if (*ctx == NULL) {
2016-06-09 23:41:51 -06:00
*ctx = wolfSSL_CTX_new_ex(method(hint), hint);
if (*ctx == NULL) {
WOLFSSL_MSG("Error creating ctx");
return SSL_FAILURE;
}
}
/* determine what max applies too */
if (flag & WOLFMEM_IO_POOL || flag & WOLFMEM_IO_POOL_FIXED) {
heap->maxIO = max;
}
else { /* general memory used in handshakes */
heap->maxHa = max;
}
heap->flag |= flag;
(void)max;
(void)method;
return SSL_SUCCESS;
}
int wolfSSL_is_static_memory(WOLFSSL* ssl, WOLFSSL_MEM_CONN_STATS* mem_stats)
{
if (ssl == NULL) {
return BAD_FUNC_ARG;
}
WOLFSSL_ENTER("wolfSSL_is_static_memory");
/* fill out statistics if wanted and WOLFMEM_TRACK_STATS flag */
if (mem_stats != NULL && ssl->heap != NULL) {
WOLFSSL_HEAP_HINT* hint = ((WOLFSSL_HEAP_HINT*)(ssl->heap));
WOLFSSL_HEAP* heap = hint->memory;
if (heap->flag & WOLFMEM_TRACK_STATS && hint->stats != NULL) {
XMEMCPY(mem_stats, hint->stats, sizeof(WOLFSSL_MEM_CONN_STATS));
}
}
return (ssl->heap) ? 1 : 0;
}
int wolfSSL_CTX_is_static_memory(WOLFSSL_CTX* ctx, WOLFSSL_MEM_STATS* mem_stats)
{
if (ctx == NULL) {
return BAD_FUNC_ARG;
}
WOLFSSL_ENTER("wolfSSL_CTX_is_static_memory");
/* fill out statistics if wanted */
if (mem_stats != NULL && ctx->heap != NULL) {
WOLFSSL_HEAP* heap = ((WOLFSSL_HEAP_HINT*)(ctx->heap))->memory;
if (wolfSSL_GetMemStats(heap, mem_stats) != 1) {
return MEMORY_E;
}
}
return (ctx->heap) ? 1 : 0;
}
#endif /* WOLFSSL_STATIC_MEMORY */
2012-09-05 08:26:08 -07:00
2016-06-08 09:32:34 -07:00
2016-06-09 14:51:07 -07:00
/* return max record layer size plaintext input size */
int wolfSSL_GetMaxOutputSize(WOLFSSL* ssl)
2016-06-08 09:32:34 -07:00
{
int maxSize = OUTPUT_RECORD_SIZE;
2016-06-09 14:51:07 -07:00
WOLFSSL_ENTER("wolfSSL_GetMaxOutputSize");
if (ssl == NULL)
2016-06-08 09:32:34 -07:00
return BAD_FUNC_ARG;
2016-06-09 14:51:07 -07:00
if (ssl->options.handShakeState != HANDSHAKE_DONE) {
WOLFSSL_MSG("Handshake not complete yet");
2016-06-08 09:32:34 -07:00
return BAD_FUNC_ARG;
2016-06-09 14:51:07 -07:00
}
2016-06-08 09:32:34 -07:00
#ifdef HAVE_MAX_FRAGMENT
maxSize = min(maxSize, ssl->max_fragment);
#endif
#ifdef WOLFSSL_DTLS
if (ssl->options.dtls) {
maxSize = min(maxSize, MAX_UDP_SIZE);
}
#endif
2016-06-09 14:51:07 -07:00
return maxSize;
}
/* return record layer size of plaintext input size */
int wolfSSL_GetOutputSize(WOLFSSL* ssl, int inSz)
{
int maxSize;
WOLFSSL_ENTER("wolfSSL_GetOutputSize");
if (inSz < 0)
2016-06-08 09:32:34 -07:00
return BAD_FUNC_ARG;
2016-06-09 14:51:07 -07:00
maxSize = wolfSSL_GetMaxOutputSize(ssl);
if (maxSize < 0)
return maxSize; /* error */
if (inSz > maxSize)
return INPUT_SIZE_E;
2016-06-08 09:32:34 -07:00
return BuildMessage(ssl, NULL, 0, NULL, inSz, application_data, 0, 1);
}
2012-09-05 08:26:08 -07:00
#ifdef HAVE_ECC
int wolfSSL_CTX_SetMinEccKey_Sz(WOLFSSL_CTX* ctx, short keySz)
{
if (ctx == NULL || keySz < 0 || keySz % 8 != 0) {
WOLFSSL_MSG("Key size must be divisable by 8 or ctx was null");
return BAD_FUNC_ARG;
}
ctx->minEccKeySz = keySz / 8;
ctx->cm->minEccKeySz = keySz / 8;
return SSL_SUCCESS;
}
int wolfSSL_SetMinEccKey_Sz(WOLFSSL* ssl, short keySz)
{
if (ssl == NULL || keySz < 0 || keySz % 8 != 0) {
WOLFSSL_MSG("Key size must be divisable by 8 or ssl was null");
return BAD_FUNC_ARG;
}
ssl->options.minEccKeySz = keySz / 8;
return SSL_SUCCESS;
}
#endif /* !NO_RSA */
2014-07-10 11:18:49 -06:00
#ifndef NO_RSA
int wolfSSL_CTX_SetMinRsaKey_Sz(WOLFSSL_CTX* ctx, short keySz)
{
if (ctx == NULL || keySz < 0 || keySz % 8 != 0) {
WOLFSSL_MSG("Key size must be divisable by 8 or ctx was null");
return BAD_FUNC_ARG;
}
ctx->minRsaKeySz = keySz / 8;
ctx->cm->minRsaKeySz = keySz / 8;
return SSL_SUCCESS;
}
int wolfSSL_SetMinRsaKey_Sz(WOLFSSL* ssl, short keySz)
{
if (ssl == NULL || keySz < 0 || keySz % 8 != 0) {
WOLFSSL_MSG("Key size must be divisable by 8 or ssl was null");
return BAD_FUNC_ARG;
}
ssl->options.minRsaKeySz = keySz / 8;
return SSL_SUCCESS;
}
#endif /* !NO_RSA */
2014-07-10 11:18:49 -06:00
#ifndef NO_DH
/* server Diffie-Hellman parameters, SSL_SUCCESS on ok */
2014-12-19 11:27:01 -07:00
int wolfSSL_SetTmpDH(WOLFSSL* ssl, const unsigned char* p, int pSz,
2011-11-01 14:55:28 -07:00
const unsigned char* g, int gSz)
2011-04-20 14:40:07 -07:00
{
2015-12-21 16:11:02 -07:00
word16 havePSK = 0;
word16 haveRSA = 1;
2011-04-20 14:40:07 -07:00
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_SetTmpDH");
if (ssl == NULL || p == NULL || g == NULL) return BAD_FUNC_ARG;
2011-04-20 14:40:07 -07:00
if (pSz < ssl->options.minDhKeySz)
return DH_KEY_SIZE_E;
2014-12-19 11:27:01 -07:00
if (ssl->options.side != WOLFSSL_SERVER_END)
2011-04-20 14:40:07 -07:00
return SIDE_ERROR;
if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH)
XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap, DYNAMIC_TYPE_DH);
if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH)
XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap, DYNAMIC_TYPE_DH);
2011-04-20 14:40:07 -07:00
ssl->buffers.weOwnDH = 1; /* SSL owns now */
ssl->buffers.serverDH_P.buffer = (byte*)XMALLOC(pSz, ssl->heap,
2011-04-20 14:40:07 -07:00
DYNAMIC_TYPE_DH);
if (ssl->buffers.serverDH_P.buffer == NULL)
return MEMORY_E;
ssl->buffers.serverDH_G.buffer = (byte*)XMALLOC(gSz, ssl->heap,
2011-04-20 14:40:07 -07:00
DYNAMIC_TYPE_DH);
if (ssl->buffers.serverDH_G.buffer == NULL) {
XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap, DYNAMIC_TYPE_DH);
2011-04-20 14:40:07 -07:00
return MEMORY_E;
}
ssl->buffers.serverDH_P.length = pSz;
ssl->buffers.serverDH_G.length = gSz;
XMEMCPY(ssl->buffers.serverDH_P.buffer, p, pSz);
XMEMCPY(ssl->buffers.serverDH_G.buffer, g, gSz);
ssl->options.haveDH = 1;
#ifndef NO_PSK
havePSK = ssl->options.havePSK;
#endif
2012-10-29 15:39:42 -07:00
#ifdef NO_RSA
haveRSA = 0;
#endif
InitSuites(ssl->suites, ssl->version, haveRSA, havePSK, ssl->options.haveDH,
ssl->options.haveNTRU, ssl->options.haveECDSAsig,
2016-02-10 11:02:09 -07:00
ssl->options.haveECC, ssl->options.haveStaticECC,
ssl->options.side);
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("wolfSSL_SetTmpDH", 0);
return SSL_SUCCESS;
2011-04-20 14:40:07 -07:00
}
/* server ctx Diffie-Hellman parameters, SSL_SUCCESS on ok */
int wolfSSL_CTX_SetTmpDH(WOLFSSL_CTX* ctx, const unsigned char* p, int pSz,
const unsigned char* g, int gSz)
{
WOLFSSL_ENTER("wolfSSL_CTX_SetTmpDH");
if (ctx == NULL || p == NULL || g == NULL) return BAD_FUNC_ARG;
if (pSz < ctx->minDhKeySz)
return DH_KEY_SIZE_E;
XFREE(ctx->serverDH_P.buffer, ctx->heap, DYNAMIC_TYPE_DH);
XFREE(ctx->serverDH_G.buffer, ctx->heap, DYNAMIC_TYPE_DH);
ctx->serverDH_P.buffer = (byte*)XMALLOC(pSz, ctx->heap, DYNAMIC_TYPE_DH);
if (ctx->serverDH_P.buffer == NULL)
return MEMORY_E;
ctx->serverDH_G.buffer = (byte*)XMALLOC(gSz, ctx->heap, DYNAMIC_TYPE_DH);
if (ctx->serverDH_G.buffer == NULL) {
XFREE(ctx->serverDH_P.buffer, ctx->heap, DYNAMIC_TYPE_DH);
return MEMORY_E;
}
ctx->serverDH_P.length = pSz;
ctx->serverDH_G.length = gSz;
XMEMCPY(ctx->serverDH_P.buffer, p, pSz);
XMEMCPY(ctx->serverDH_G.buffer, g, gSz);
ctx->haveDH = 1;
WOLFSSL_LEAVE("wolfSSL_CTX_SetTmpDH", 0);
return SSL_SUCCESS;
}
int wolfSSL_CTX_SetMinDhKey_Sz(WOLFSSL_CTX* ctx, word16 keySz)
{
if (ctx == NULL || keySz > 16000 || keySz % 8 != 0)
return BAD_FUNC_ARG;
ctx->minDhKeySz = keySz / 8;
return SSL_SUCCESS;
}
int wolfSSL_SetMinDhKey_Sz(WOLFSSL* ssl, word16 keySz)
{
if (ssl == NULL || keySz > 16000 || keySz % 8 != 0)
return BAD_FUNC_ARG;
ssl->options.minDhKeySz = keySz / 8;
return SSL_SUCCESS;
}
int wolfSSL_GetDhKey_Sz(WOLFSSL* ssl)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
return (ssl->options.dhKeySz * 8);
}
2014-07-10 11:18:49 -06:00
#endif /* !NO_DH */
2011-04-20 14:40:07 -07:00
2014-12-19 11:27:01 -07:00
int wolfSSL_write(WOLFSSL* ssl, const void* data, int sz)
2011-02-05 11:14:47 -08:00
{
int ret;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_write()");
2011-02-05 11:14:47 -08:00
if (ssl == NULL || data == NULL || sz < 0)
return BAD_FUNC_ARG;
#ifdef HAVE_ERRNO_H
2011-02-05 11:14:47 -08:00
errno = 0;
#endif
2011-05-25 10:25:05 -07:00
ret = SendData(ssl, data, sz);
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("SSL_write()", ret);
2011-02-05 11:14:47 -08:00
if (ret < 0)
return SSL_FATAL_ERROR;
else
return ret;
}
2014-12-19 11:27:01 -07:00
static int wolfSSL_read_internal(WOLFSSL* ssl, void* data, int sz, int peek)
2011-02-05 11:14:47 -08:00
{
int ret;
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_read_internal()");
2011-02-05 11:14:47 -08:00
if (ssl == NULL || data == NULL || sz < 0)
return BAD_FUNC_ARG;
#ifdef HAVE_ERRNO_H
2011-02-05 11:14:47 -08:00
errno = 0;
#endif
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_DTLS
if (ssl->options.dtls) {
ssl->dtls_expected_rx = max(sz + 100, MAX_MTU);
#ifdef WOLFSSL_SCTP
if (ssl->options.dtlsSctp)
ssl->dtls_expected_rx = max(ssl->dtls_expected_rx, ssl->dtlsMtuSz);
#endif
}
#endif
2011-02-05 11:14:47 -08:00
sz = min(sz, OUTPUT_RECORD_SIZE);
#ifdef HAVE_MAX_FRAGMENT
sz = min(sz, ssl->max_fragment);
#endif
ret = ReceiveData(ssl, (byte*)data, sz, peek);
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("wolfSSL_read_internal()", ret);
2011-02-05 11:14:47 -08:00
if (ret < 0)
return SSL_FATAL_ERROR;
else
return ret;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_peek(WOLFSSL* ssl, void* data, int sz)
2012-11-16 12:16:00 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_peek()");
2012-11-16 12:16:00 -08:00
2014-12-19 11:27:01 -07:00
return wolfSSL_read_internal(ssl, data, sz, TRUE);
2012-11-16 12:16:00 -08:00
}
2014-12-19 11:27:01 -07:00
int wolfSSL_read(WOLFSSL* ssl, void* data, int sz)
2012-11-16 12:16:00 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_read()");
2012-11-16 12:16:00 -08:00
2014-12-19 11:27:01 -07:00
return wolfSSL_read_internal(ssl, data, sz, FALSE);
2012-11-16 12:16:00 -08:00
}
#ifdef WOLFSSL_ASYNC_CRYPT
/* let's use async hardware, SSL_SUCCESS on ok */
int wolfSSL_UseAsync(WOLFSSL* ssl, int devId)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
ssl->devId = devId;
return SSL_SUCCESS;
}
/* let's use async hardware, SSL_SUCCESS on ok */
int wolfSSL_CTX_UseAsync(WOLFSSL_CTX* ctx, int devId)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
ctx->devId = devId;
return SSL_SUCCESS;
}
#endif /* WOLFSSL_ASYNC_CRYPT */
2013-05-21 14:37:50 -07:00
#ifdef HAVE_SNI
2014-12-19 11:27:01 -07:00
int wolfSSL_UseSNI(WOLFSSL* ssl, byte type, const void* data, word16 size)
2013-05-21 14:37:50 -07:00
{
if (ssl == NULL)
return BAD_FUNC_ARG;
2013-05-21 14:37:50 -07:00
return TLSX_UseSNI(&ssl->extensions, type, data, size, ssl->heap);
2013-05-21 14:37:50 -07:00
}
2015-11-02 15:51:01 -03:00
int wolfSSL_CTX_UseSNI(WOLFSSL_CTX* ctx, byte type, const void* data,
word16 size)
2013-05-21 14:37:50 -07:00
{
if (ctx == NULL)
return BAD_FUNC_ARG;
return TLSX_UseSNI(&ctx->extensions, type, data, size, ctx->heap);
2013-05-21 14:37:50 -07:00
}
2014-12-19 11:27:01 -07:00
#ifndef NO_WOLFSSL_SERVER
2014-12-19 11:27:01 -07:00
void wolfSSL_SNI_SetOptions(WOLFSSL* ssl, byte type, byte options)
{
if (ssl && ssl->extensions)
TLSX_SNI_SetOptions(ssl->extensions, type, options);
}
2013-05-30 15:40:10 -03:00
2015-11-02 15:51:01 -03:00
2014-12-19 11:27:01 -07:00
void wolfSSL_CTX_SNI_SetOptions(WOLFSSL_CTX* ctx, byte type, byte options)
{
if (ctx && ctx->extensions)
TLSX_SNI_SetOptions(ctx->extensions, type, options);
}
2015-11-02 15:51:01 -03:00
2014-12-19 11:27:01 -07:00
byte wolfSSL_SNI_Status(WOLFSSL* ssl, byte type)
{
return TLSX_SNI_Status(ssl ? ssl->extensions : NULL, type);
}
2015-11-02 15:51:01 -03:00
2014-12-19 11:27:01 -07:00
word16 wolfSSL_SNI_GetRequest(WOLFSSL* ssl, byte type, void** data)
{
if (data)
*data = NULL;
if (ssl && ssl->extensions)
return TLSX_SNI_GetRequest(ssl->extensions, type, data);
return 0;
}
2015-11-02 15:51:01 -03:00
int wolfSSL_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz,
byte type, byte* sni, word32* inOutSz)
{
if (clientHello && helloSz > 0 && sni && inOutSz && *inOutSz > 0)
return TLSX_SNI_GetFromBuffer(clientHello, helloSz, type, sni, inOutSz);
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
#endif /* NO_WOLFSSL_SERVER */
2013-05-21 14:37:50 -07:00
#endif /* HAVE_SNI */
2013-06-19 16:38:57 -03:00
#ifdef HAVE_MAX_FRAGMENT
2014-12-19 11:27:01 -07:00
#ifndef NO_WOLFSSL_CLIENT
2015-11-02 15:51:01 -03:00
2014-12-19 11:27:01 -07:00
int wolfSSL_UseMaxFragment(WOLFSSL* ssl, byte mfl)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
return TLSX_UseMaxFragment(&ssl->extensions, mfl, ssl->heap);
}
2015-11-02 15:51:01 -03:00
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_UseMaxFragment(WOLFSSL_CTX* ctx, byte mfl)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
return TLSX_UseMaxFragment(&ctx->extensions, mfl, ctx->heap);
}
2015-11-02 15:51:01 -03:00
2014-12-19 11:27:01 -07:00
#endif /* NO_WOLFSSL_CLIENT */
#endif /* HAVE_MAX_FRAGMENT */
#ifdef HAVE_TRUNCATED_HMAC
2014-12-19 11:27:01 -07:00
#ifndef NO_WOLFSSL_CLIENT
2015-11-02 15:51:01 -03:00
2014-12-19 11:27:01 -07:00
int wolfSSL_UseTruncatedHMAC(WOLFSSL* ssl)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
return TLSX_UseTruncatedHMAC(&ssl->extensions, ssl->heap);
}
2015-11-02 15:51:01 -03:00
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_UseTruncatedHMAC(WOLFSSL_CTX* ctx)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
return TLSX_UseTruncatedHMAC(&ctx->extensions, ctx->heap);
}
2015-11-02 15:51:01 -03:00
2014-12-19 11:27:01 -07:00
#endif /* NO_WOLFSSL_CLIENT */
#endif /* HAVE_TRUNCATED_HMAC */
2015-11-02 15:51:01 -03:00
#ifdef HAVE_CERTIFICATE_STATUS_REQUEST
2015-12-28 19:38:04 -03:00
int wolfSSL_UseOCSPStapling(WOLFSSL* ssl, byte status_type, byte options)
2015-11-02 15:51:01 -03:00
{
if (ssl == NULL || ssl->options.side != WOLFSSL_CLIENT_END)
return BAD_FUNC_ARG;
return TLSX_UseCertificateStatusRequest(&ssl->extensions, status_type,
options, ssl->heap);
2015-11-02 15:51:01 -03:00
}
2015-12-28 19:38:04 -03:00
int wolfSSL_CTX_UseOCSPStapling(WOLFSSL_CTX* ctx, byte status_type,
2015-11-02 15:51:01 -03:00
byte options)
{
if (ctx == NULL || ctx->method->side != WOLFSSL_CLIENT_END)
return BAD_FUNC_ARG;
return TLSX_UseCertificateStatusRequest(&ctx->extensions, status_type,
options, ctx->heap);
2015-11-02 15:51:01 -03:00
}
#endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
2015-12-28 19:38:04 -03:00
#ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
int wolfSSL_UseOCSPStaplingV2(WOLFSSL* ssl, byte status_type, byte options)
{
if (ssl == NULL || ssl->options.side != WOLFSSL_CLIENT_END)
return BAD_FUNC_ARG;
return TLSX_UseCertificateStatusRequestV2(&ssl->extensions, status_type,
options, ssl->heap);
2015-12-28 19:38:04 -03:00
}
int wolfSSL_CTX_UseOCSPStaplingV2(WOLFSSL_CTX* ctx,
byte status_type, byte options)
{
if (ctx == NULL || ctx->method->side != WOLFSSL_CLIENT_END)
return BAD_FUNC_ARG;
return TLSX_UseCertificateStatusRequestV2(&ctx->extensions, status_type,
options, ctx->heap);
2015-12-28 19:38:04 -03:00
}
#endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
/* Elliptic Curves */
#ifdef HAVE_SUPPORTED_CURVES
2014-12-19 11:27:01 -07:00
#ifndef NO_WOLFSSL_CLIENT
2014-12-19 11:27:01 -07:00
int wolfSSL_UseSupportedCurve(WOLFSSL* ssl, word16 name)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
switch (name) {
case WOLFSSL_ECC_SECP160K1:
2014-12-19 11:27:01 -07:00
case WOLFSSL_ECC_SECP160R1:
case WOLFSSL_ECC_SECP160R2:
case WOLFSSL_ECC_SECP192K1:
2014-12-19 11:27:01 -07:00
case WOLFSSL_ECC_SECP192R1:
case WOLFSSL_ECC_SECP224K1:
2014-12-19 11:27:01 -07:00
case WOLFSSL_ECC_SECP224R1:
case WOLFSSL_ECC_SECP256K1:
2014-12-19 11:27:01 -07:00
case WOLFSSL_ECC_SECP256R1:
case WOLFSSL_ECC_SECP384R1:
case WOLFSSL_ECC_SECP521R1:
case WOLFSSL_ECC_BRAINPOOLP256R1:
case WOLFSSL_ECC_BRAINPOOLP384R1:
case WOLFSSL_ECC_BRAINPOOLP512R1:
break;
default:
return BAD_FUNC_ARG;
}
return TLSX_UseSupportedCurve(&ssl->extensions, name, ssl->heap);
}
2015-11-02 15:51:01 -03:00
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_UseSupportedCurve(WOLFSSL_CTX* ctx, word16 name)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
switch (name) {
case WOLFSSL_ECC_SECP160K1:
2014-12-19 11:27:01 -07:00
case WOLFSSL_ECC_SECP160R1:
case WOLFSSL_ECC_SECP160R2:
case WOLFSSL_ECC_SECP192K1:
2014-12-19 11:27:01 -07:00
case WOLFSSL_ECC_SECP192R1:
case WOLFSSL_ECC_SECP224K1:
2014-12-19 11:27:01 -07:00
case WOLFSSL_ECC_SECP224R1:
case WOLFSSL_ECC_SECP256K1:
2014-12-19 11:27:01 -07:00
case WOLFSSL_ECC_SECP256R1:
case WOLFSSL_ECC_SECP384R1:
case WOLFSSL_ECC_SECP521R1:
case WOLFSSL_ECC_BRAINPOOLP256R1:
case WOLFSSL_ECC_BRAINPOOLP384R1:
case WOLFSSL_ECC_BRAINPOOLP512R1:
break;
default:
return BAD_FUNC_ARG;
}
return TLSX_UseSupportedCurve(&ctx->extensions, name, ctx->heap);
}
2014-12-19 11:27:01 -07:00
#endif /* NO_WOLFSSL_CLIENT */
#endif /* HAVE_SUPPORTED_CURVES */
2015-07-07 09:55:58 -06:00
/* QSH quantum safe handshake */
#ifdef HAVE_QSH
/* returns 1 if QSH has been used 0 otherwise */
int wolfSSL_isQSH(WOLFSSL* ssl)
{
/* if no ssl struct than QSH was not used */
if (ssl == NULL)
return 0;
return ssl->isQSH;
}
int wolfSSL_UseSupportedQSH(WOLFSSL* ssl, word16 name)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
switch (name) {
#ifdef HAVE_NTRU
case WOLFSSL_NTRU_EESS439:
case WOLFSSL_NTRU_EESS593:
case WOLFSSL_NTRU_EESS743:
break;
#endif
default:
return BAD_FUNC_ARG;
}
ssl->user_set_QSHSchemes = 1;
return TLSX_UseQSHScheme(&ssl->extensions, name, NULL, 0, ssl->heap);
2015-07-07 09:55:58 -06:00
}
#ifndef NO_WOLFSSL_CLIENT
/* user control over sending client public key in hello
when flag = 1 will send keys if flag is 0 or function is not called
then will not send keys in the hello extension
return 0 on success
*/
int wolfSSL_UseClientQSHKeys(WOLFSSL* ssl, unsigned char flag)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
ssl->sendQSHKeys = flag;
return 0;
}
#endif /* NO_WOLFSSL_CLIENT */
#endif /* HAVE_QSH */
/* Application-Layer Protocol Negotiation */
#ifdef HAVE_ALPN
int wolfSSL_UseALPN(WOLFSSL* ssl, char *protocol_name_list,
word32 protocol_name_listSz, byte options)
{
char *list, *ptr, *token[10];
word16 len;
int idx = 0;
int ret = SSL_FAILURE;
WOLFSSL_ENTER("wolfSSL_UseALPN");
if (ssl == NULL || protocol_name_list == NULL)
return BAD_FUNC_ARG;
if (protocol_name_listSz > (WOLFSSL_MAX_ALPN_NUMBER *
WOLFSSL_MAX_ALPN_PROTO_NAME_LEN +
WOLFSSL_MAX_ALPN_NUMBER)) {
WOLFSSL_MSG("Invalid arguments, protocol name list too long");
return BAD_FUNC_ARG;
}
if (!(options & WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) &&
!(options & WOLFSSL_ALPN_FAILED_ON_MISMATCH)) {
WOLFSSL_MSG("Invalid arguments, options not supported");
return BAD_FUNC_ARG;
}
list = (char *)XMALLOC(protocol_name_listSz+1, ssl->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (list == NULL) {
WOLFSSL_MSG("Memory failure");
return MEMORY_ERROR;
}
XMEMSET(list, 0, protocol_name_listSz+1);
XSTRNCPY(list, protocol_name_list, protocol_name_listSz);
/* read all protocol name from the list */
token[idx] = XSTRTOK(list, ",", &ptr);
while (token[idx] != NULL)
token[++idx] = XSTRTOK(NULL, ",", &ptr);
/* add protocol name list in the TLS extension in reverse order */
while ((idx--) > 0) {
len = (word16)XSTRLEN(token[idx]);
ret = TLSX_UseALPN(&ssl->extensions, token[idx], len, options,
ssl->heap);
if (ret != SSL_SUCCESS) {
WOLFSSL_MSG("TLSX_UseALPN failure");
break;
}
}
XFREE(list, ssl->heap, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
int wolfSSL_ALPN_GetProtocol(WOLFSSL* ssl, char **protocol_name, word16 *size)
{
return TLSX_ALPN_GetRequest(ssl ? ssl->extensions : NULL,
(void **)protocol_name, size);
}
int wolfSSL_ALPN_GetPeerProtocol(WOLFSSL* ssl, char **list, word16 *listSz)
{
if (list == NULL || listSz == NULL)
return BAD_FUNC_ARG;
if (ssl->alpn_client_list == NULL)
return BUFFER_ERROR;
*listSz = (word16)XSTRLEN(ssl->alpn_client_list);
if (*listSz == 0)
return BUFFER_ERROR;
*list = (char *)XMALLOC((*listSz)+1, ssl->heap, DYNAMIC_TYPE_TLSX);
if (*list == NULL)
return MEMORY_ERROR;
XSTRNCPY(*list, ssl->alpn_client_list, (*listSz)+1);
(*list)[*listSz] = 0;
return SSL_SUCCESS;
}
/* used to free memory allocated by wolfSSL_ALPN_GetPeerProtocol */
int wolfSSL_ALPN_FreePeerProtocol(WOLFSSL* ssl, char **list)
{
if (ssl == NULL) {
return BAD_FUNC_ARG;
}
XFREE(*list, ssl->heap, DYNAMIC_TYPE_TLSX);
*list = NULL;
return SSL_SUCCESS;
}
#endif /* HAVE_ALPN */
/* Secure Renegotiation */
#ifdef HAVE_SECURE_RENEGOTIATION
/* user is forcing ability to use secure renegotiation, we discourage it */
2014-12-19 11:27:01 -07:00
int wolfSSL_UseSecureRenegotiation(WOLFSSL* ssl)
{
int ret = BAD_FUNC_ARG;
if (ssl)
ret = TLSX_UseSecureRenegotiation(&ssl->extensions, ssl->heap);
if (ret == SSL_SUCCESS) {
2015-11-02 15:51:01 -03:00
TLSX* extension = TLSX_Find(ssl->extensions, TLSX_RENEGOTIATION_INFO);
2015-07-01 12:24:11 -07:00
if (extension)
ssl->secure_renegotiation = (SecureRenegotiation*)extension->data;
}
return ret;
}
/* do a secure renegotiation handshake, user forced, we discourage */
2014-12-19 11:27:01 -07:00
int wolfSSL_Rehandshake(WOLFSSL* ssl)
{
int ret;
if (ssl == NULL)
return BAD_FUNC_ARG;
if (ssl->secure_renegotiation == NULL) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Secure Renegotiation not forced on by user");
return SECURE_RENEGOTIATION_E;
}
if (ssl->secure_renegotiation->enabled == 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Secure Renegotiation not enabled at extension level");
return SECURE_RENEGOTIATION_E;
}
if (ssl->options.handShakeState != HANDSHAKE_DONE) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Can't renegotiate until previous handshake complete");
return SECURE_RENEGOTIATION_E;
}
#ifndef NO_FORCE_SCR_SAME_SUITE
/* force same suite */
if (ssl->suites) {
ssl->suites->suiteSz = SUITE_LEN;
ssl->suites->suites[0] = ssl->options.cipherSuite0;
ssl->suites->suites[1] = ssl->options.cipherSuite;
}
#endif
/* reset handshake states */
ssl->options.serverState = NULL_STATE;
ssl->options.clientState = NULL_STATE;
ssl->options.connectState = CONNECT_BEGIN;
ssl->options.acceptState = ACCEPT_BEGIN;
ssl->options.handShakeState = NULL_STATE;
ssl->options.processReply = 0; /* TODO, move states in internal.h */
XMEMSET(&ssl->msgsReceived, 0, sizeof(ssl->msgsReceived));
ssl->secure_renegotiation->cache_status = SCR_CACHE_NEEDED;
#ifndef NO_OLD_TLS
#ifndef NO_MD5
wc_InitMd5(&ssl->hsHashes->hashMd5);
#endif
#ifndef NO_SHA
ret = wc_InitSha(&ssl->hsHashes->hashSha);
if (ret !=0)
return ret;
#endif
#endif /* NO_OLD_TLS */
#ifndef NO_SHA256
ret = wc_InitSha256(&ssl->hsHashes->hashSha256);
if (ret !=0)
return ret;
#endif
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SHA384
ret = wc_InitSha384(&ssl->hsHashes->hashSha384);
if (ret !=0)
return ret;
#endif
#ifdef WOLFSSL_SHA512
ret = wc_InitSha512(&ssl->hsHashes->hashSha512);
if (ret !=0)
return ret;
#endif
2014-12-19 11:27:01 -07:00
ret = wolfSSL_negotiate(ssl);
return ret;
}
#endif /* HAVE_SECURE_RENEGOTIATION */
/* Session Ticket */
#if !defined(NO_WOLFSSL_SERVER) && defined(HAVE_SESSION_TICKET)
/* SSL_SUCCESS on ok */
int wolfSSL_CTX_set_TicketEncCb(WOLFSSL_CTX* ctx, SessionTicketEncCb cb)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
ctx->ticketEncCb = cb;
return SSL_SUCCESS;
}
/* set hint interval, SSL_SUCCESS on ok */
int wolfSSL_CTX_set_TicketHint(WOLFSSL_CTX* ctx, int hint)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
ctx->ticketHint = hint;
return SSL_SUCCESS;
}
/* set user context, SSL_SUCCESS on ok */
int wolfSSL_CTX_set_TicketEncCtx(WOLFSSL_CTX* ctx, void* userCtx)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
ctx->ticketEncCtx = userCtx;
return SSL_SUCCESS;
}
#endif /* !defined(NO_WOLFSSL_CLIENT) && defined(HAVE_SESSION_TICKET) */
/* Session Ticket */
2014-12-19 11:27:01 -07:00
#if !defined(NO_WOLFSSL_CLIENT) && defined(HAVE_SESSION_TICKET)
int wolfSSL_UseSessionTicket(WOLFSSL* ssl)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
return TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_UseSessionTicket(WOLFSSL_CTX* ctx)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
return TLSX_UseSessionTicket(&ctx->extensions, NULL, ctx->heap);
}
WOLFSSL_API int wolfSSL_get_SessionTicket(WOLFSSL* ssl,
byte* buf, word32* bufSz)
{
if (ssl == NULL || buf == NULL || bufSz == NULL || *bufSz == 0)
return BAD_FUNC_ARG;
if (ssl->session.ticketLen <= *bufSz) {
XMEMCPY(buf, ssl->session.ticket, ssl->session.ticketLen);
*bufSz = ssl->session.ticketLen;
}
else
*bufSz = 0;
return SSL_SUCCESS;
}
2014-12-19 11:27:01 -07:00
WOLFSSL_API int wolfSSL_set_SessionTicket(WOLFSSL* ssl, byte* buf, word32 bufSz)
{
if (ssl == NULL || (buf == NULL && bufSz > 0))
return BAD_FUNC_ARG;
if (bufSz > 0) {
/* Ticket will fit into static ticket */
if(bufSz <= SESSION_TICKET_LEN) {
if (ssl->session.isDynamic) {
XFREE(ssl->session.ticket, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
ssl->session.isDynamic = 0;
ssl->session.ticket = ssl->session.staticTicket;
}
} else { /* Ticket requires dynamic ticket storage */
if (ssl->session.ticketLen < bufSz) { /* is dyn buffer big enough */
if(ssl->session.isDynamic)
XFREE(ssl->session.ticket, ssl->heap,
DYNAMIC_TYPE_SESSION_TICK);
ssl->session.ticket = XMALLOC(bufSz, ssl->heap,
DYNAMIC_TYPE_SESSION_TICK);
if(!ssl->session.ticket) {
ssl->session.ticket = ssl->session.staticTicket;
ssl->session.isDynamic = 0;
return MEMORY_ERROR;
}
ssl->session.isDynamic = 1;
}
}
XMEMCPY(ssl->session.ticket, buf, bufSz);
}
2015-05-15 16:10:38 -07:00
ssl->session.ticketLen = (word16)bufSz;
return SSL_SUCCESS;
}
2014-12-19 11:27:01 -07:00
WOLFSSL_API int wolfSSL_set_SessionTicket_cb(WOLFSSL* ssl,
CallbackSessionTicket cb, void* ctx)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
ssl->session_ticket_cb = cb;
ssl->session_ticket_ctx = ctx;
return SSL_SUCCESS;
}
#endif
2016-09-01 15:12:54 -06:00
#ifdef HAVE_EXTENDED_MASTER
#ifndef NO_WOLFSSL_CLIENT
int wolfSSL_CTX_DisableExtendedMasterSecret(WOLFSSL_CTX* ctx)
2016-09-01 15:12:54 -06:00
{
if (ctx == NULL)
2016-09-01 15:12:54 -06:00
return BAD_FUNC_ARG;
ctx->haveEMS = 0;
return SSL_SUCCESS;
2016-09-01 15:12:54 -06:00
}
int wolfSSL_DisableExtendedMasterSecret(WOLFSSL* ssl)
2016-09-01 15:12:54 -06:00
{
if (ssl == NULL)
2016-09-01 15:12:54 -06:00
return BAD_FUNC_ARG;
ssl->options.haveEMS = 0;
return SSL_SUCCESS;
2016-09-01 15:12:54 -06:00
}
#endif
#endif
2016-09-01 15:12:54 -06:00
2014-12-19 11:27:01 -07:00
#ifndef WOLFSSL_LEANPSK
2014-12-19 11:27:01 -07:00
int wolfSSL_send(WOLFSSL* ssl, const void* data, int sz, int flags)
{
int ret;
int oldFlags;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_send()");
if (ssl == NULL || data == NULL || sz < 0)
return BAD_FUNC_ARG;
oldFlags = ssl->wflags;
ssl->wflags = flags;
2014-12-19 11:27:01 -07:00
ret = wolfSSL_write(ssl, data, sz);
ssl->wflags = oldFlags;
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("wolfSSL_send()", ret);
return ret;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_recv(WOLFSSL* ssl, void* data, int sz, int flags)
{
int ret;
int oldFlags;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_recv()");
if (ssl == NULL || data == NULL || sz < 0)
return BAD_FUNC_ARG;
oldFlags = ssl->rflags;
ssl->rflags = flags;
2014-12-19 11:27:01 -07:00
ret = wolfSSL_read(ssl, data, sz);
ssl->rflags = oldFlags;
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("wolfSSL_recv()", ret);
return ret;
}
#endif
/* SSL_SUCCESS on ok */
2014-12-19 11:27:01 -07:00
int wolfSSL_shutdown(WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
2015-02-16 14:23:33 -08:00
int ret = SSL_FATAL_ERROR;
2015-02-11 09:37:54 -07:00
byte tmp;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_shutdown()");
2011-02-05 11:14:47 -08:00
2013-02-14 14:09:41 -08:00
if (ssl == NULL)
return SSL_FATAL_ERROR;
2013-02-14 14:09:41 -08:00
2011-02-05 11:14:47 -08:00
if (ssl->options.quietShutdown) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("quiet shutdown, no close notify sent");
return SSL_SUCCESS;
2011-02-05 11:14:47 -08:00
}
/* try to send close notify, not an error if can't */
if (!ssl->options.isClosed && !ssl->options.connReset &&
!ssl->options.sentNotify) {
ssl->error = SendAlert(ssl, alert_warning, close_notify);
if (ssl->error < 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
ssl->options.sentNotify = 1; /* don't send close_notify twice */
2015-02-11 09:37:54 -07:00
if (ssl->options.closeNotify)
2015-02-16 14:23:33 -08:00
ret = SSL_SUCCESS;
2015-02-11 09:37:54 -07:00
else
2015-02-16 14:23:33 -08:00
ret = SSL_SHUTDOWN_NOT_DONE;
WOLFSSL_LEAVE("SSL_shutdown()", ret);
return ret;
2015-01-30 08:41:34 -07:00
}
/* call wolfSSL_shutdown again for bidirectional shutdown */
2015-01-30 08:41:34 -07:00
if (ssl->options.sentNotify && !ssl->options.closeNotify) {
2015-02-16 14:23:33 -08:00
ret = wolfSSL_read(ssl, &tmp, 0);
if (ret < 0) {
2015-01-30 08:41:34 -07:00
WOLFSSL_ERROR(ssl->error);
2015-02-16 14:23:33 -08:00
ret = SSL_FATAL_ERROR;
} else if (ssl->options.closeNotify) {
ssl->error = SSL_ERROR_SYSCALL; /* simulate OpenSSL behavior */
ret = SSL_SUCCESS;
2015-01-30 08:41:34 -07:00
}
2011-02-05 11:14:47 -08:00
}
2015-02-16 14:23:33 -08:00
WOLFSSL_LEAVE("SSL_shutdown()", ret);
return ret;
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
int wolfSSL_get_error(WOLFSSL* ssl, int ret)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_get_error");
if (ret > 0)
return SSL_ERROR_NONE;
if (ssl == NULL)
return BAD_FUNC_ARG;
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("SSL_get_error", ssl->error);
/* make sure converted types are handled in SetErrorString() too */
2011-02-05 11:14:47 -08:00
if (ssl->error == WANT_READ)
return SSL_ERROR_WANT_READ; /* convert to OpenSSL type */
else if (ssl->error == WANT_WRITE)
return SSL_ERROR_WANT_WRITE; /* convert to OpenSSL type */
else if (ssl->error == ZERO_RETURN)
2011-02-05 11:14:47 -08:00
return SSL_ERROR_ZERO_RETURN; /* convert to OpenSSL type */
return ssl->error;
}
/* retrive alert history, SSL_SUCCESS on ok */
2014-12-19 11:27:01 -07:00
int wolfSSL_get_alert_history(WOLFSSL* ssl, WOLFSSL_ALERT_HISTORY *h)
{
if (ssl && h) {
*h = ssl->alert_history;
}
return SSL_SUCCESS;
}
2011-02-05 11:14:47 -08:00
/* return TRUE if current error is want read */
2014-12-19 11:27:01 -07:00
int wolfSSL_want_read(WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_want_read");
2011-02-05 11:14:47 -08:00
if (ssl->error == WANT_READ)
return 1;
return 0;
}
/* return TRUE if current error is want write */
2014-12-19 11:27:01 -07:00
int wolfSSL_want_write(WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_want_write");
2011-02-05 11:14:47 -08:00
if (ssl->error == WANT_WRITE)
return 1;
return 0;
}
2014-12-19 11:27:01 -07:00
char* wolfSSL_ERR_error_string(unsigned long errNumber, char* data)
2011-02-05 11:14:47 -08:00
{
2011-04-27 17:31:08 -07:00
static const char* msg = "Please supply a buffer for error string";
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("ERR_error_string");
2011-05-25 10:25:05 -07:00
if (data) {
SetErrorString((int)errNumber, data);
2011-05-25 10:25:05 -07:00
return data;
2011-02-05 11:14:47 -08:00
}
2011-04-27 17:31:08 -07:00
return (char*)msg;
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
void wolfSSL_ERR_error_string_n(unsigned long e, char* buf, unsigned long len)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_ERR_error_string_n");
if (len >= WOLFSSL_MAX_ERROR_SZ)
wolfSSL_ERR_error_string(e, buf);
else {
2014-12-19 11:27:01 -07:00
char tmp[WOLFSSL_MAX_ERROR_SZ];
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Error buffer too short, truncating");
if (len) {
2014-12-19 11:27:01 -07:00
wolfSSL_ERR_error_string(e, tmp);
XMEMCPY(buf, tmp, len-1);
buf[len-1] = '\0';
}
}
2011-02-05 11:14:47 -08:00
}
2012-09-14 21:19:06 -07:00
/* don't free temporary arrays at end of handshake */
2014-12-19 11:27:01 -07:00
void wolfSSL_KeepArrays(WOLFSSL* ssl)
2012-09-14 21:19:06 -07:00
{
if (ssl)
ssl->options.saveArrays = 1;
}
/* user doesn't need temporary arrays anymore, Free */
2014-12-19 11:27:01 -07:00
void wolfSSL_FreeArrays(WOLFSSL* ssl)
2012-09-14 21:19:06 -07:00
{
if (ssl && ssl->options.handShakeState == HANDSHAKE_DONE) {
ssl->options.saveArrays = 0;
FreeArrays(ssl, 1);
}
}
2014-12-19 11:27:01 -07:00
const byte* wolfSSL_GetMacSecret(WOLFSSL* ssl, int verify)
2013-08-09 17:27:15 -07:00
{
2013-08-29 08:25:14 -07:00
if (ssl == NULL)
return NULL;
2014-12-19 11:27:01 -07:00
if ( (ssl->options.side == WOLFSSL_CLIENT_END && !verify) ||
(ssl->options.side == WOLFSSL_SERVER_END && verify) )
2013-08-09 17:27:15 -07:00
return ssl->keys.client_write_MAC_secret;
else
return ssl->keys.server_write_MAC_secret;
}
#ifdef ATOMIC_USER
2014-12-19 11:27:01 -07:00
void wolfSSL_CTX_SetMacEncryptCb(WOLFSSL_CTX* ctx, CallbackMacEncrypt cb)
2013-08-09 17:27:15 -07:00
{
if (ctx)
ctx->MacEncryptCb = cb;
}
2014-12-19 11:27:01 -07:00
void wolfSSL_SetMacEncryptCtx(WOLFSSL* ssl, void *ctx)
2013-08-09 17:27:15 -07:00
{
if (ssl)
ssl->MacEncryptCtx = ctx;
}
2014-12-19 11:27:01 -07:00
void* wolfSSL_GetMacEncryptCtx(WOLFSSL* ssl)
2013-08-09 17:27:15 -07:00
{
if (ssl)
return ssl->MacEncryptCtx;
return NULL;
}
2014-12-19 11:27:01 -07:00
void wolfSSL_CTX_SetDecryptVerifyCb(WOLFSSL_CTX* ctx, CallbackDecryptVerify cb)
{
if (ctx)
ctx->DecryptVerifyCb = cb;
}
2014-12-19 11:27:01 -07:00
void wolfSSL_SetDecryptVerifyCtx(WOLFSSL* ssl, void *ctx)
{
if (ssl)
ssl->DecryptVerifyCtx = ctx;
}
2014-12-19 11:27:01 -07:00
void* wolfSSL_GetDecryptVerifyCtx(WOLFSSL* ssl)
{
if (ssl)
return ssl->DecryptVerifyCtx;
return NULL;
}
2014-12-19 11:27:01 -07:00
const byte* wolfSSL_GetClientWriteKey(WOLFSSL* ssl)
2013-08-09 17:27:15 -07:00
{
if (ssl)
return ssl->keys.client_write_key;
return NULL;
}
2014-12-19 11:27:01 -07:00
const byte* wolfSSL_GetClientWriteIV(WOLFSSL* ssl)
2013-08-09 17:27:15 -07:00
{
if (ssl)
return ssl->keys.client_write_IV;
return NULL;
}
2014-12-19 11:27:01 -07:00
const byte* wolfSSL_GetServerWriteKey(WOLFSSL* ssl)
2013-08-09 17:27:15 -07:00
{
if (ssl)
return ssl->keys.server_write_key;
return NULL;
}
2014-12-19 11:27:01 -07:00
const byte* wolfSSL_GetServerWriteIV(WOLFSSL* ssl)
2013-08-09 17:27:15 -07:00
{
if (ssl)
return ssl->keys.server_write_IV;
return NULL;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_GetKeySize(WOLFSSL* ssl)
2013-08-09 17:27:15 -07:00
{
if (ssl)
return ssl->specs.key_size;
2013-08-09 17:27:15 -07:00
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_GetIVSize(WOLFSSL* ssl)
{
if (ssl)
return ssl->specs.iv_size;
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_GetBulkCipher(WOLFSSL* ssl)
2013-08-09 17:27:15 -07:00
{
if (ssl)
return ssl->specs.bulk_cipher_algorithm;
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_GetCipherType(WOLFSSL* ssl)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
if (ssl->specs.cipher_type == block)
2014-12-19 11:27:01 -07:00
return WOLFSSL_BLOCK_TYPE;
if (ssl->specs.cipher_type == stream)
2014-12-19 11:27:01 -07:00
return WOLFSSL_STREAM_TYPE;
if (ssl->specs.cipher_type == aead)
2014-12-19 11:27:01 -07:00
return WOLFSSL_AEAD_TYPE;
return -1;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_GetCipherBlockSize(WOLFSSL* ssl)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
return ssl->specs.block_size;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_GetAeadMacSize(WOLFSSL* ssl)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
return ssl->specs.aead_mac_size;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_IsTLSv1_1(WOLFSSL* ssl)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
if (ssl->options.tls1_1)
return 1;
return 0;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_GetSide(WOLFSSL* ssl)
2013-08-09 17:27:15 -07:00
{
if (ssl)
return ssl->options.side;
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_GetHmacSize(WOLFSSL* ssl)
2013-08-09 17:27:15 -07:00
{
/* AEAD ciphers don't have HMAC keys */
2013-08-09 17:27:15 -07:00
if (ssl)
return (ssl->specs.cipher_type != aead) ? ssl->specs.hash_size : 0;
2013-08-09 17:27:15 -07:00
return BAD_FUNC_ARG;
}
#endif /* ATOMIC_USER */
#ifndef NO_CERTS
int AllocDer(DerBuffer** pDer, word32 length, int type, void* heap)
{
int ret = BAD_FUNC_ARG;
if (pDer) {
int dynType = 0;
DerBuffer* der;
/* Determine dynamic type */
switch (type) {
case CA_TYPE: dynType = DYNAMIC_TYPE_CA; break;
case CERT_TYPE: dynType = DYNAMIC_TYPE_CERT; break;
case CRL_TYPE: dynType = DYNAMIC_TYPE_CRL; break;
case DSA_TYPE: dynType = DYNAMIC_TYPE_DSA; break;
case ECC_TYPE: dynType = DYNAMIC_TYPE_ECC; break;
case RSA_TYPE: dynType = DYNAMIC_TYPE_RSA; break;
default: dynType = DYNAMIC_TYPE_KEY; break;
}
/* Setup new buffer */
*pDer = (DerBuffer*)XMALLOC(sizeof(DerBuffer) + length, heap, dynType);
if (*pDer == NULL) {
return MEMORY_ERROR;
}
der = *pDer;
der->type = type;
der->dynType = dynType; /* Cache this for FreeDer */
der->heap = heap;
der->buffer = (byte*)der + sizeof(DerBuffer);
der->length = length;
ret = 0; /* Success */
}
return ret;
}
void FreeDer(DerBuffer** pDer)
{
if (pDer && *pDer)
{
DerBuffer* der = (DerBuffer*)*pDer;
/* ForceZero private keys */
if (der->type == PRIVATEKEY_TYPE) {
ForceZero(der->buffer, der->length);
}
der->buffer = NULL;
der->length = 0;
XFREE(der, der->heap, der->dynType);
*pDer = NULL;
}
}
WOLFSSL_CERT_MANAGER* wolfSSL_CertManagerNew_ex(void* heap)
2012-05-03 18:07:31 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_CERT_MANAGER* cm = NULL;
2012-05-03 18:07:31 -07:00
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertManagerNew");
2012-05-03 18:07:31 -07:00
cm = (WOLFSSL_CERT_MANAGER*) XMALLOC(sizeof(WOLFSSL_CERT_MANAGER), heap,
DYNAMIC_TYPE_CERT_MANAGER);
2012-05-03 18:07:31 -07:00
if (cm) {
2014-12-19 11:27:01 -07:00
XMEMSET(cm, 0, sizeof(WOLFSSL_CERT_MANAGER));
2012-05-03 18:07:31 -07:00
if (wc_InitMutex(&cm->caLock) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad mutex init");
wolfSSL_CertManagerFree(cm);
2012-05-03 18:07:31 -07:00
return NULL;
}
2016-02-24 15:51:29 -07:00
#ifdef WOLFSSL_TRUST_PEER_CERT
if (wc_InitMutex(&cm->tpLock) != 0) {
2016-02-24 15:51:29 -07:00
WOLFSSL_MSG("Bad mutex init");
wolfSSL_CertManagerFree(cm);
return NULL;
}
#endif
/* set default minimum key size allowed */
#ifndef NO_RSA
cm->minRsaKeySz = MIN_RSAKEY_SZ;
#endif
#ifdef HAVE_ECC
cm->minEccKeySz = MIN_ECCKEY_SZ;
#endif
cm->heap = heap;
2012-05-03 18:07:31 -07:00
}
return cm;
}
WOLFSSL_CERT_MANAGER* wolfSSL_CertManagerNew(void)
{
return wolfSSL_CertManagerNew_ex(NULL);
}
2014-12-19 11:27:01 -07:00
void wolfSSL_CertManagerFree(WOLFSSL_CERT_MANAGER* cm)
2012-05-03 18:07:31 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertManagerFree");
2012-05-03 18:07:31 -07:00
if (cm) {
2012-05-16 17:04:56 -07:00
#ifdef HAVE_CRL
if (cm->crl)
2012-11-27 11:36:36 -08:00
FreeCRL(cm->crl, 1);
2012-05-16 17:04:56 -07:00
#endif
#ifdef HAVE_OCSP
if (cm->ocsp)
FreeOCSP(cm->ocsp, 1);
2015-12-28 19:38:04 -03:00
#if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
|| defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
if (cm->ocsp_stapling)
FreeOCSP(cm->ocsp_stapling, 1);
#endif
#endif
FreeSignerTable(cm->caTable, CA_TABLE_SIZE, cm->heap);
wc_FreeMutex(&cm->caLock);
2016-02-24 15:51:29 -07:00
#ifdef WOLFSSL_TRUST_PEER_CERT
FreeTrustedPeerTable(cm->tpTable, TP_TABLE_SIZE, cm->heap);
wc_FreeMutex(&cm->tpLock);
2016-02-24 15:51:29 -07:00
#endif
XFREE(cm, cm->heap, DYNAMIC_TYPE_CERT_MANAGER);
2012-05-03 18:07:31 -07:00
}
}
/* Unload the CA signer list */
2014-12-19 11:27:01 -07:00
int wolfSSL_CertManagerUnloadCAs(WOLFSSL_CERT_MANAGER* cm)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertManagerUnloadCAs");
if (cm == NULL)
return BAD_FUNC_ARG;
if (wc_LockMutex(&cm->caLock) != 0)
return BAD_MUTEX_E;
FreeSignerTable(cm->caTable, CA_TABLE_SIZE, NULL);
wc_UnLockMutex(&cm->caLock);
return SSL_SUCCESS;
}
2016-02-24 15:51:29 -07:00
#ifdef WOLFSSL_TRUST_PEER_CERT
int wolfSSL_CertManagerUnload_trust_peers(WOLFSSL_CERT_MANAGER* cm)
{
WOLFSSL_ENTER("wolfSSL_CertManagerUnload_trust_peers");
if (cm == NULL)
return BAD_FUNC_ARG;
if (wc_LockMutex(&cm->tpLock) != 0)
2016-02-24 15:51:29 -07:00
return BAD_MUTEX_E;
FreeTrustedPeerTable(cm->tpTable, TP_TABLE_SIZE, NULL);
wc_UnLockMutex(&cm->tpLock);
2016-02-24 15:51:29 -07:00
return SSL_SUCCESS;
}
#endif /* WOLFSSL_TRUST_PEER_CERT */
/* Return bytes written to buff or < 0 for error */
2014-12-19 11:27:01 -07:00
int wolfSSL_CertPemToDer(const unsigned char* pem, int pemSz,
unsigned char* buff, int buffSz, int type)
{
int eccKey = 0;
int ret;
DerBuffer* der = NULL;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
EncryptedInfo* info = NULL;
#else
EncryptedInfo info[1];
#endif
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertPemToDer");
if (pem == NULL || buff == NULL || buffSz <= 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad pem der args");
return BAD_FUNC_ARG;
}
if (type != CERT_TYPE && type != CA_TYPE && type != CERTREQ_TYPE) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad cert type");
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (info == NULL)
return MEMORY_E;
#endif
info->set = 0;
info->ctx = NULL;
info->consumed = 0;
ret = PemToDer(pem, pemSz, type, &der, NULL, info, &eccKey);
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
if (ret < 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad Pem To Der");
}
else {
if (der->length <= (word32)buffSz) {
XMEMCPY(buff, der->buffer, der->length);
ret = der->length;
}
else {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad der length");
ret = BAD_FUNC_ARG;
}
}
FreeDer(&der);
return ret;
}
#endif /* NO_CERTS */
#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
#ifndef NO_AES
2016-06-27 10:53:34 -07:00
static const char *EVP_AES_128_CBC = "AES-128-CBC";
static const char *EVP_AES_192_CBC = "AES-192-CBC";
static const char *EVP_AES_256_CBC = "AES-256-CBC";
#if defined(OPENSSL_EXTRA)
static const char *EVP_AES_128_CTR = "AES-128-CTR";
static const char *EVP_AES_192_CTR = "AES-192-CTR";
static const char *EVP_AES_256_CTR = "AES-256-CTR";
2016-10-13 22:10:50 +09:00
static const char *EVP_AES_128_ECB = "AES-128-ECB";
static const char *EVP_AES_192_ECB = "AES-192-ECB";
static const char *EVP_AES_256_ECB = "AES-256-ECB";
2016-06-27 10:53:34 -07:00
#endif
static const int EVP_AES_SIZE = 11;
#endif
2016-06-27 10:53:34 -07:00
#ifndef NO_DES3
static const char *EVP_DES_CBC = "DES-CBC";
2016-10-13 22:10:50 +09:00
static const char *EVP_DES_ECB = "DES-ECB";
2016-06-27 10:53:34 -07:00
static const int EVP_DES_SIZE = 7;
static const char *EVP_DES_EDE3_CBC = "DES-EDE3-CBC";
2016-10-13 22:10:50 +09:00
static const char *EVP_DES_EDE3_ECB = "DES-EDE3-ECB";
2016-06-27 10:53:34 -07:00
static const int EVP_DES_EDE3_SIZE = 12;
#endif
#ifdef HAVE_IDEA
static const char *EVP_IDEA_CBC = "IDEA-CBC";
static const int EVP_IDEA_SIZE = 8;
#endif
/* our KeyPemToDer password callback, password in userData */
static INLINE int OurPasswordCb(char* passwd, int sz, int rw, void* userdata)
{
(void)rw;
if (userdata == NULL)
return 0;
XSTRNCPY(passwd, (char*)userdata, sz);
2013-12-23 12:24:03 -08:00
return min((word32)sz, (word32)XSTRLEN((char*)userdata));
}
#endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
#ifndef NO_CERTS
2013-11-14 15:00:22 -08:00
/* Return bytes written to buff or < 0 for error */
int wolfSSL_KeyPemToDer(const unsigned char* pem, int pemSz,
unsigned char* buff, int buffSz, const char* pass)
2013-11-14 15:00:22 -08:00
{
int eccKey = 0;
int ret;
DerBuffer* der = NULL;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
EncryptedInfo* info = NULL;
#else
EncryptedInfo info[1];
#endif
2013-11-14 15:00:22 -08:00
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_KeyPemToDer");
2013-11-14 15:00:22 -08:00
if (pem == NULL || buff == NULL || buffSz <= 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad pem der args");
2013-11-14 15:00:22 -08:00
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (info == NULL)
return MEMORY_E;
#endif
info->set = 0;
info->ctx = NULL;
info->consumed = 0;
2013-11-14 15:00:22 -08:00
#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
2013-12-23 12:15:55 -08:00
if (pass) {
2014-12-19 11:27:01 -07:00
info->ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
if (info->ctx == NULL) {
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2014-09-08 21:34:07 -03:00
#endif
2013-12-23 12:15:55 -08:00
return MEMORY_E;
}
2014-12-19 11:27:01 -07:00
wolfSSL_CTX_set_default_passwd_cb(info->ctx, OurPasswordCb);
wolfSSL_CTX_set_default_passwd_cb_userdata(info->ctx, (void*)pass);
2013-12-23 12:15:55 -08:00
}
2015-07-01 12:24:11 -07:00
#else
(void)pass;
#endif
ret = PemToDer(pem, pemSz, PRIVATEKEY_TYPE, &der, NULL, info, &eccKey);
if (info->ctx)
2014-12-19 11:27:01 -07:00
wolfSSL_CTX_free(info->ctx);
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2013-11-14 15:00:22 -08:00
if (ret < 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad Pem To Der");
2013-11-14 15:00:22 -08:00
}
else {
if (der->length <= (word32)buffSz) {
XMEMCPY(buff, der->buffer, der->length);
ret = der->length;
2013-11-14 15:00:22 -08:00
}
else {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad der length");
2013-11-14 15:00:22 -08:00
ret = BAD_FUNC_ARG;
}
}
FreeDer(&der);
2013-11-14 15:00:22 -08:00
return ret;
}
#endif /* !NO_CERTS */
2012-05-03 18:07:31 -07:00
#if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
void wolfSSL_ERR_print_errors_fp(FILE* fp, int err)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
char data[WOLFSSL_MAX_ERROR_SZ + 1];
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_ERR_print_errors_fp");
2011-05-25 10:25:05 -07:00
SetErrorString(err, data);
fprintf(fp, "%s", data);
2011-02-05 11:14:47 -08:00
}
#endif
2014-12-19 11:27:01 -07:00
int wolfSSL_pending(WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_pending");
2011-02-05 11:14:47 -08:00
return ssl->buffers.clearOutputBuffer.length;
}
2014-12-19 11:27:01 -07:00
#ifndef WOLFSSL_LEANPSK
2015-09-22 15:40:46 -06:00
/* turn on handshake group messages for context */
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_set_group_messages(WOLFSSL_CTX* ctx)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
ctx->groupMessages = 1;
return SSL_SUCCESS;
}
#endif
2014-12-19 11:27:01 -07:00
#ifndef NO_WOLFSSL_CLIENT
/* connect enough to get peer cert chain */
2014-12-19 11:27:01 -07:00
int wolfSSL_connect_cert(WOLFSSL* ssl)
{
int ret;
if (ssl == NULL)
return SSL_FAILURE;
ssl->options.certOnly = 1;
2014-12-19 11:27:01 -07:00
ret = wolfSSL_connect(ssl);
ssl->options.certOnly = 0;
return ret;
}
#endif
2014-12-19 11:27:01 -07:00
#ifndef WOLFSSL_LEANPSK
2015-09-22 15:40:46 -06:00
/* turn on handshake group messages for ssl object */
2014-12-19 11:27:01 -07:00
int wolfSSL_set_group_messages(WOLFSSL* ssl)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
ssl->options.groupMessages = 1;
return SSL_SUCCESS;
}
2015-09-22 15:40:46 -06:00
/* make minVersion the internal equivalent SSL version */
2015-03-13 12:20:39 -07:00
static int SetMinVersionHelper(byte* minVersion, int version)
{
#ifdef NO_TLS
(void)minVersion;
#endif
switch (version) {
2015-08-12 16:39:13 -07:00
#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
2014-12-19 11:27:01 -07:00
case WOLFSSL_SSLV3:
2015-03-13 12:20:39 -07:00
*minVersion = SSLv3_MINOR;
break;
#endif
#ifndef NO_TLS
#ifndef NO_OLD_TLS
2014-12-19 11:27:01 -07:00
case WOLFSSL_TLSV1:
2015-03-13 12:20:39 -07:00
*minVersion = TLSv1_MINOR;
break;
2014-12-19 11:27:01 -07:00
case WOLFSSL_TLSV1_1:
2015-03-13 12:20:39 -07:00
*minVersion = TLSv1_1_MINOR;
break;
#endif
2014-12-19 11:27:01 -07:00
case WOLFSSL_TLSV1_2:
2015-03-13 12:20:39 -07:00
*minVersion = TLSv1_2_MINOR;
break;
#endif
default:
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad function argument");
return BAD_FUNC_ARG;
}
return SSL_SUCCESS;
}
2015-03-13 12:20:39 -07:00
/* Set minimum downgrade version allowed, SSL_SUCCESS on ok */
int wolfSSL_CTX_SetMinVersion(WOLFSSL_CTX* ctx, int version)
{
WOLFSSL_ENTER("wolfSSL_CTX_SetMinVersion");
if (ctx == NULL) {
WOLFSSL_MSG("Bad function argument");
return BAD_FUNC_ARG;
}
return SetMinVersionHelper(&ctx->minDowngrade, version);
}
/* Set minimum downgrade version allowed, SSL_SUCCESS on ok */
int wolfSSL_SetMinVersion(WOLFSSL* ssl, int version)
{
WOLFSSL_ENTER("wolfSSL_SetMinVersion");
if (ssl == NULL) {
WOLFSSL_MSG("Bad function argument");
return BAD_FUNC_ARG;
}
return SetMinVersionHelper(&ssl->options.minDowngrade, version);
}
2014-12-19 11:27:01 -07:00
int wolfSSL_SetVersion(WOLFSSL* ssl, int version)
{
2015-12-21 16:11:02 -07:00
word16 haveRSA = 1;
word16 havePSK = 0;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_SetVersion");
if (ssl == NULL) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad function argument");
return BAD_FUNC_ARG;
}
switch (version) {
2015-08-12 16:39:13 -07:00
#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
2014-12-19 11:27:01 -07:00
case WOLFSSL_SSLV3:
ssl->version = MakeSSLv3();
break;
#endif
2012-08-10 12:40:22 -07:00
#ifndef NO_TLS
#ifndef NO_OLD_TLS
2014-12-19 11:27:01 -07:00
case WOLFSSL_TLSV1:
ssl->version = MakeTLSv1();
break;
2014-12-19 11:27:01 -07:00
case WOLFSSL_TLSV1_1:
ssl->version = MakeTLSv1_1();
break;
#endif
2014-12-19 11:27:01 -07:00
case WOLFSSL_TLSV1_2:
ssl->version = MakeTLSv1_2();
break;
2012-08-10 12:40:22 -07:00
#endif
default:
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad function argument");
return BAD_FUNC_ARG;
}
2012-10-29 15:39:42 -07:00
#ifdef NO_RSA
haveRSA = 0;
#endif
#ifndef NO_PSK
havePSK = ssl->options.havePSK;
#endif
2012-10-29 15:39:42 -07:00
InitSuites(ssl->suites, ssl->version, haveRSA, havePSK, ssl->options.haveDH,
ssl->options.haveNTRU, ssl->options.haveECDSAsig,
2016-02-10 11:02:09 -07:00
ssl->options.haveECC, ssl->options.haveStaticECC,
ssl->options.side);
return SSL_SUCCESS;
}
#endif /* !leanpsk */
#if !defined(NO_CERTS) || !defined(NO_SESSION_CACHE)
/* Make a work from the front of random hash */
static INLINE word32 MakeWordFromHash(const byte* hashID)
{
return (hashID[0] << 24) | (hashID[1] << 16) | (hashID[2] << 8) |
hashID[3];
}
#endif /* !NO_CERTS || !NO_SESSION_CACHE */
#ifndef NO_CERTS
/* hash is the SHA digest of name, just use first 32 bits as hash */
static INLINE word32 HashSigner(const byte* hash)
{
return MakeWordFromHash(hash) % CA_TABLE_SIZE;
}
/* does CA already exist on signer list */
2014-12-19 11:27:01 -07:00
int AlreadySigner(WOLFSSL_CERT_MANAGER* cm, byte* hash)
{
Signer* signers;
int ret = 0;
word32 row = HashSigner(hash);
if (wc_LockMutex(&cm->caLock) != 0)
return ret;
signers = cm->caTable[row];
while (signers) {
byte* subjectHash;
#ifndef NO_SKID
subjectHash = signers->subjectKeyIdHash;
#else
subjectHash = signers->subjectNameHash;
#endif
if (XMEMCMP(hash, subjectHash, SIGNER_DIGEST_SIZE) == 0) {
ret = 1;
break;
}
signers = signers->next;
}
wc_UnLockMutex(&cm->caLock);
return ret;
}
2016-02-24 15:51:29 -07:00
#ifdef WOLFSSL_TRUST_PEER_CERT
/* hash is the SHA digest of name, just use first 32 bits as hash */
static INLINE word32 TrustedPeerHashSigner(const byte* hash)
{
return MakeWordFromHash(hash) % TP_TABLE_SIZE;
}
2016-02-24 15:51:29 -07:00
/* does trusted peer already exist on signer list */
int AlreadyTrustedPeer(WOLFSSL_CERT_MANAGER* cm, byte* hash)
{
TrustedPeerCert* tp;
int ret = 0;
word32 row = TrustedPeerHashSigner(hash);
2016-02-24 15:51:29 -07:00
if (wc_LockMutex(&cm->tpLock) != 0)
2016-02-24 15:51:29 -07:00
return ret;
tp = cm->tpTable[row];
while (tp) {
byte* subjectHash;
#ifndef NO_SKID
subjectHash = tp->subjectKeyIdHash;
#else
subjectHash = tp->subjectNameHash;
#endif
if (XMEMCMP(hash, subjectHash, SIGNER_DIGEST_SIZE) == 0) {
ret = 1;
break;
}
tp = tp->next;
}
wc_UnLockMutex(&cm->tpLock);
2016-02-24 15:51:29 -07:00
return ret;
}
/* return Trusted Peer if found, otherwise NULL
type is what to match on
*/
TrustedPeerCert* GetTrustedPeer(void* vp, byte* hash, int type)
2016-02-24 15:51:29 -07:00
{
WOLFSSL_CERT_MANAGER* cm = (WOLFSSL_CERT_MANAGER*)vp;
TrustedPeerCert* ret = NULL;
TrustedPeerCert* tp = NULL;
word32 row;
if (cm == NULL || hash == NULL)
return NULL;
row = TrustedPeerHashSigner(hash);
2016-02-24 15:51:29 -07:00
if (wc_LockMutex(&cm->tpLock) != 0)
2016-02-24 15:51:29 -07:00
return ret;
tp = cm->tpTable[row];
while (tp) {
byte* subjectHash;
switch (type) {
#ifndef NO_SKID
case WC_MATCH_SKID:
subjectHash = tp->subjectKeyIdHash;
break;
#endif
case WC_MATCH_NAME:
subjectHash = tp->subjectNameHash;
break;
default:
WOLFSSL_MSG("Unknown search type");
wc_UnLockMutex(&cm->tpLock);
return NULL;
}
2016-02-24 15:51:29 -07:00
if (XMEMCMP(hash, subjectHash, SIGNER_DIGEST_SIZE) == 0) {
ret = tp;
break;
}
tp = tp->next;
}
wc_UnLockMutex(&cm->tpLock);
2016-02-24 15:51:29 -07:00
return ret;
}
int MatchTrustedPeer(TrustedPeerCert* tp, DecodedCert* cert)
{
if (tp == NULL || cert == NULL)
return BAD_FUNC_ARG;
/* subject key id or subject hash has been compared when searching
tpTable for the cert from function GetTrustedPeer */
/* compare signatures */
if (tp->sigLen == cert->sigLength) {
if (XMEMCMP(tp->sig, cert->signature, cert->sigLength)) {
return SSL_FAILURE;
}
}
else {
return SSL_FAILURE;
}
return SSL_SUCCESS;
}
#endif /* WOLFSSL_TRUST_PEER_CERT */
/* return CA if found, otherwise NULL */
2012-05-03 18:07:31 -07:00
Signer* GetCA(void* vp, byte* hash)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_CERT_MANAGER* cm = (WOLFSSL_CERT_MANAGER*)vp;
2012-05-03 18:07:31 -07:00
Signer* ret = NULL;
Signer* signers;
word32 row = HashSigner(hash);
2012-05-03 18:07:31 -07:00
if (cm == NULL)
return NULL;
if (wc_LockMutex(&cm->caLock) != 0)
return ret;
signers = cm->caTable[row];
while (signers) {
byte* subjectHash;
#ifndef NO_SKID
subjectHash = signers->subjectKeyIdHash;
#else
subjectHash = signers->subjectNameHash;
#endif
if (XMEMCMP(hash, subjectHash, SIGNER_DIGEST_SIZE) == 0) {
ret = signers;
break;
}
signers = signers->next;
}
wc_UnLockMutex(&cm->caLock);
return ret;
}
#ifndef NO_SKID
/* return CA if found, otherwise NULL. Walk through hash table. */
Signer* GetCAByName(void* vp, byte* hash)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_CERT_MANAGER* cm = (WOLFSSL_CERT_MANAGER*)vp;
Signer* ret = NULL;
Signer* signers;
word32 row;
if (cm == NULL)
return NULL;
if (wc_LockMutex(&cm->caLock) != 0)
return ret;
for (row = 0; row < CA_TABLE_SIZE && ret == NULL; row++) {
signers = cm->caTable[row];
while (signers && ret == NULL) {
if (XMEMCMP(hash, signers->subjectNameHash,
SIGNER_DIGEST_SIZE) == 0) {
ret = signers;
}
signers = signers->next;
}
}
wc_UnLockMutex(&cm->caLock);
return ret;
}
#endif
2016-02-24 15:51:29 -07:00
#ifdef WOLFSSL_TRUST_PEER_CERT
/* add a trusted peer cert to linked list */
int AddTrustedPeer(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int verify)
2016-02-24 15:51:29 -07:00
{
int ret, row;
TrustedPeerCert* peerCert;
DecodedCert* cert = NULL;
DerBuffer* der = *pDer;
2016-02-24 15:51:29 -07:00
byte* subjectHash = NULL;
WOLFSSL_MSG("Adding a Trusted Peer Cert");
cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), cm->heap,
2016-02-24 15:51:29 -07:00
DYNAMIC_TYPE_TMP_BUFFER);
if (cert == NULL)
return MEMORY_E;
InitDecodedCert(cert, der->buffer, der->length, cm->heap);
if ((ret = ParseCert(cert, TRUSTED_PEER_TYPE, verify, cm)) != 0) {
XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
WOLFSSL_MSG(" Parsed new trusted peer cert");
peerCert = (TrustedPeerCert*)XMALLOC(sizeof(TrustedPeerCert), cm->heap,
2016-02-24 15:51:29 -07:00
DYNAMIC_TYPE_CERT);
if (peerCert == NULL) {
FreeDecodedCert(cert);
XFREE(cert, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
2016-02-24 15:51:29 -07:00
return MEMORY_E;
}
XMEMSET(peerCert, 0, sizeof(TrustedPeerCert));
#ifndef NO_SKID
if (cert->extAuthKeyIdSet) {
subjectHash = cert->extSubjKeyId;
}
else {
subjectHash = cert->subjectHash;
}
2016-02-24 15:51:29 -07:00
#else
subjectHash = cert->subjectHash;
#endif
#ifndef IGNORE_NAME_CONSTRAINTS
if (peerCert->permittedNames)
FreeNameSubtrees(peerCert->permittedNames, cm->heap);
if (peerCert->excludedNames)
FreeNameSubtrees(peerCert->excludedNames, cm->heap);
#endif
if (AlreadyTrustedPeer(cm, subjectHash)) {
WOLFSSL_MSG(" Already have this CA, not adding again");
(void)ret;
}
else {
/* add trusted peer signature */
peerCert->sigLen = cert->sigLength;
peerCert->sig = XMALLOC(cert->sigLength, cm->heap,
DYNAMIC_TYPE_SIGNATURE);
if (peerCert->sig == NULL) {
FreeDecodedCert(cert);
XFREE(cert, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
2016-02-24 15:51:29 -07:00
FreeTrustedPeer(peerCert, cm->heap);
return MEMORY_E;
}
XMEMCPY(peerCert->sig, cert->signature, cert->sigLength);
/* add trusted peer name */
peerCert->nameLen = cert->subjectCNLen;
peerCert->name = cert->subjectCN;
#ifndef IGNORE_NAME_CONSTRAINTS
peerCert->permittedNames = cert->permittedNames;
peerCert->excludedNames = cert->excludedNames;
#endif
/* add SKID when available and hash of name */
#ifndef NO_SKID
XMEMCPY(peerCert->subjectKeyIdHash, cert->extSubjKeyId,
SIGNER_DIGEST_SIZE);
#endif
XMEMCPY(peerCert->subjectNameHash, cert->subjectHash,
SIGNER_DIGEST_SIZE);
peerCert->next = NULL; /* If Key Usage not set, all uses valid. */
cert->subjectCN = 0;
#ifndef IGNORE_NAME_CONSTRAINTS
cert->permittedNames = NULL;
cert->excludedNames = NULL;
#endif
#ifndef NO_SKID
if (cert->extAuthKeyIdSet) {
row = TrustedPeerHashSigner(peerCert->subjectKeyIdHash);
}
else {
row = TrustedPeerHashSigner(peerCert->subjectNameHash);
}
2016-02-24 15:51:29 -07:00
#else
row = TrustedPeerHashSigner(peerCert->subjectNameHash);
2016-02-24 15:51:29 -07:00
#endif
if (wc_LockMutex(&cm->tpLock) == 0) {
2016-02-24 15:51:29 -07:00
peerCert->next = cm->tpTable[row];
cm->tpTable[row] = peerCert; /* takes ownership */
wc_UnLockMutex(&cm->tpLock);
2016-02-24 15:51:29 -07:00
}
else {
WOLFSSL_MSG(" Trusted Peer Cert Mutex Lock failed");
FreeDecodedCert(cert);
XFREE(cert, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
2016-02-24 15:51:29 -07:00
FreeTrustedPeer(peerCert, cm->heap);
return BAD_MUTEX_E;
}
}
WOLFSSL_MSG(" Freeing parsed trusted peer cert");
FreeDecodedCert(cert);
XFREE(cert, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
2016-02-24 15:51:29 -07:00
WOLFSSL_MSG(" Freeing der trusted peer cert");
FreeDer(&der);
2016-02-24 15:51:29 -07:00
WOLFSSL_MSG(" OK Freeing der trusted peer cert");
WOLFSSL_LEAVE("AddTrustedPeer", ret);
return SSL_SUCCESS;
}
#endif /* WOLFSSL_TRUST_PEER_CERT */
2011-10-31 17:33:40 -07:00
/* owns der, internal now uses too */
2012-01-26 12:43:48 -08:00
/* type flag ids from user or from chain received during verify
don't allow chain ones to be added w/o isCA extension */
int AddCA(WOLFSSL_CERT_MANAGER* cm, DerBuffer** pDer, int type, int verify)
2011-02-05 11:14:47 -08:00
{
int ret;
2011-02-05 11:14:47 -08:00
Signer* signer = 0;
word32 row;
byte* subjectHash;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
DecodedCert* cert = NULL;
#else
DecodedCert cert[1];
#endif
DerBuffer* der = *pDer;
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Adding a CA");
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (cert == NULL)
return MEMORY_E;
#endif
InitDecodedCert(cert, der->buffer, der->length, cm->heap);
ret = ParseCert(cert, CA_TYPE, verify, cm);
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG(" Parsed new CA");
2011-02-05 11:14:47 -08:00
#ifndef NO_SKID
subjectHash = cert->extSubjKeyId;
#else
subjectHash = cert->subjectHash;
#endif
/* check CA key size */
if (verify) {
switch (cert->keyOID) {
#ifndef NO_RSA
case RSAk:
if (cm->minRsaKeySz < 0 ||
cert->pubKeySize < (word16)cm->minRsaKeySz) {
ret = RSA_KEY_SIZE_E;
WOLFSSL_MSG(" CA RSA key size error");
}
break;
#endif /* !NO_RSA */
#ifdef HAVE_ECC
case ECDSAk:
if (cm->minEccKeySz < 0 ||
cert->pubKeySize < (word16)cm->minEccKeySz) {
ret = ECC_KEY_SIZE_E;
WOLFSSL_MSG(" CA ECC key size error");
}
break;
#endif /* HAVE_ECC */
default:
WOLFSSL_MSG(" No key size check done on CA");
break; /* no size check if key type is not in switch */
}
}
2014-12-19 11:27:01 -07:00
if (ret == 0 && cert->isCA == 0 && type != WOLFSSL_USER_CA) {
WOLFSSL_MSG(" Can't add as CA if not actually one");
ret = NOT_CA_ERROR;
}
#ifndef ALLOW_INVALID_CERTSIGN
2014-12-19 11:27:01 -07:00
else if (ret == 0 && cert->isCA == 1 && type != WOLFSSL_USER_CA &&
(cert->extKeyUsage & KEYUSE_KEY_CERT_SIGN) == 0) {
/* Intermediate CA certs are required to have the keyCertSign
* extension set. User loaded root certs are not. */
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG(" Doesn't have key usage certificate signing");
ret = NOT_CA_ERROR;
}
#endif
else if (ret == 0 && AlreadySigner(cm, subjectHash)) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG(" Already have this CA, not adding again");
2011-10-31 17:33:40 -07:00
(void)ret;
}
2011-10-31 17:33:40 -07:00
else if (ret == 0) {
2011-02-05 11:14:47 -08:00
/* take over signer parts */
2012-05-03 18:07:31 -07:00
signer = MakeSigner(cm->heap);
2011-02-05 11:14:47 -08:00
if (!signer)
ret = MEMORY_ERROR;
else {
signer->keyOID = cert->keyOID;
signer->publicKey = cert->publicKey;
signer->pubKeySize = cert->pubKeySize;
signer->nameLen = cert->subjectCNLen;
signer->name = cert->subjectCN;
signer->pathLength = cert->pathLength;
signer->pathLengthSet = cert->pathLengthSet;
#ifndef IGNORE_NAME_CONSTRAINTS
signer->permittedNames = cert->permittedNames;
signer->excludedNames = cert->excludedNames;
#endif
#ifndef NO_SKID
XMEMCPY(signer->subjectKeyIdHash, cert->extSubjKeyId,
SIGNER_DIGEST_SIZE);
#endif
XMEMCPY(signer->subjectNameHash, cert->subjectHash,
SIGNER_DIGEST_SIZE);
signer->keyUsage = cert->extKeyUsageSet ? cert->extKeyUsage
: 0xFFFF;
signer->next = NULL; /* If Key Usage not set, all uses valid. */
cert->publicKey = 0; /* in case lock fails don't free here. */
cert->subjectCN = 0;
#ifndef IGNORE_NAME_CONSTRAINTS
cert->permittedNames = NULL;
cert->excludedNames = NULL;
#endif
2011-02-05 11:14:47 -08:00
#ifndef NO_SKID
row = HashSigner(signer->subjectKeyIdHash);
#else
row = HashSigner(signer->subjectNameHash);
#endif
if (wc_LockMutex(&cm->caLock) == 0) {
signer->next = cm->caTable[row];
cm->caTable[row] = signer; /* takes ownership */
wc_UnLockMutex(&cm->caLock);
2012-05-03 18:07:31 -07:00
if (cm->caCacheCallback)
cm->caCacheCallback(der->buffer, (int)der->length, type);
}
2011-10-31 17:33:40 -07:00
else {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG(" CA Mutex Lock failed");
ret = BAD_MUTEX_E;
FreeSigner(signer, cm->heap);
2011-10-31 17:33:40 -07:00
}
2011-02-05 11:14:47 -08:00
}
}
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG(" Freeing Parsed CA");
FreeDecodedCert(cert);
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG(" Freeing der CA");
FreeDer(pDer);
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG(" OK Freeing der CA");
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("AddCA", ret);
return ret == 0 ? SSL_SUCCESS : ret;
2011-02-05 11:14:47 -08:00
}
#endif /* !NO_CERTS */
2011-02-05 11:14:47 -08:00
#ifndef NO_SESSION_CACHE
/* basic config gives a cache with 33 sessions, adequate for clients and
embedded servers
MEDIUM_SESSION_CACHE allows 1055 sessions, adequate for servers that
aren't under heavy load, basically allows 200 new sessions per minute
BIG_SESSION_CACHE yields 20,027 sessions
2011-02-05 11:14:47 -08:00
HUGE_SESSION_CACHE yields 65,791 sessions, for servers under heavy load,
allows over 13,000 new sessions per minute or over 200 new sessions per
second
SMALL_SESSION_CACHE only stores 6 sessions, good for embedded clients
or systems where the default of nearly 3kB is too much RAM, this define
uses less than 500 bytes RAM
default SESSION_CACHE stores 33 sessions (no XXX_SESSION_CACHE defined)
2011-02-05 11:14:47 -08:00
*/
#ifdef HUGE_SESSION_CACHE
#define SESSIONS_PER_ROW 11
#define SESSION_ROWS 5981
#elif defined(BIG_SESSION_CACHE)
#define SESSIONS_PER_ROW 7
#define SESSION_ROWS 2861
#elif defined(MEDIUM_SESSION_CACHE)
2011-02-05 11:14:47 -08:00
#define SESSIONS_PER_ROW 5
#define SESSION_ROWS 211
#elif defined(SMALL_SESSION_CACHE)
#define SESSIONS_PER_ROW 2
#define SESSION_ROWS 3
2011-02-05 11:14:47 -08:00
#else
#define SESSIONS_PER_ROW 3
#define SESSION_ROWS 11
#endif
typedef struct SessionRow {
int nextIdx; /* where to place next one */
int totalCount; /* sessions ever on this row */
2014-12-19 11:27:01 -07:00
WOLFSSL_SESSION Sessions[SESSIONS_PER_ROW];
2011-02-05 11:14:47 -08:00
} SessionRow;
static SessionRow SessionCache[SESSION_ROWS];
#if defined(WOLFSSL_SESSION_STATS) && defined(WOLFSSL_PEAK_SESSIONS)
static word32 PeakSessions;
#endif
2014-12-19 11:27:01 -07:00
static wolfSSL_Mutex session_mutex; /* SessionCache mutex */
2011-02-05 11:14:47 -08:00
#ifndef NO_CLIENT_CACHE
typedef struct ClientSession {
word16 serverRow; /* SessionCache Row id */
word16 serverIdx; /* SessionCache Idx (column) */
} ClientSession;
typedef struct ClientRow {
int nextIdx; /* where to place next one */
int totalCount; /* sessions ever on this row */
ClientSession Clients[SESSIONS_PER_ROW];
} ClientRow;
static ClientRow ClientCache[SESSION_ROWS]; /* Client Cache */
/* uses session mutex */
#endif /* NO_CLIENT_CACHE */
2011-02-05 11:14:47 -08:00
#endif /* NO_SESSION_CACHE */
2014-12-19 11:27:01 -07:00
int wolfSSL_Init(void)
2012-12-06 09:26:59 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_Init");
2012-12-06 09:26:59 -08:00
if (initRefCount == 0) {
/* Initialize crypto for use with TLS connection */
if (wolfCrypt_Init() != 0) {
WOLFSSL_MSG("Bad wolfCrypt Init");
return WC_INIT_E;
}
2012-12-06 09:26:59 -08:00
#ifndef NO_SESSION_CACHE
if (wc_InitMutex(&session_mutex) != 0) {
WOLFSSL_MSG("Bad Init Mutex session");
return BAD_MUTEX_E;
}
2012-12-06 09:26:59 -08:00
#endif
if (wc_InitMutex(&count_mutex) != 0) {
WOLFSSL_MSG("Bad Init Mutex count");
return BAD_MUTEX_E;
2013-02-20 15:26:22 -08:00
}
}
2015-10-23 11:55:17 -06:00
if (wc_LockMutex(&count_mutex) != 0) {
WOLFSSL_MSG("Bad Lock Mutex count");
return BAD_MUTEX_E;
2012-12-06 09:26:59 -08:00
}
initRefCount++;
wc_UnLockMutex(&count_mutex);
return SSL_SUCCESS;
2012-12-06 09:26:59 -08:00
}
#if (defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)) && !defined(NO_CERTS)
2015-07-01 12:24:11 -07:00
/* SSL_SUCCESS if ok, <= 0 else */
static int wolfssl_decrypt_buffer_key(DerBuffer* der, byte* password,
int passwordSz, EncryptedInfo* info)
2015-07-01 12:24:11 -07:00
{
int ret = SSL_BAD_FILE;
2015-07-01 12:24:11 -07:00
#ifdef WOLFSSL_SMALL_STACK
byte* key = NULL;
2015-07-01 12:24:11 -07:00
#else
byte key[AES_256_KEY_SIZE];
2015-07-01 12:24:11 -07:00
#endif
(void)passwordSz;
(void)key;
WOLFSSL_ENTER("wolfssl_decrypt_buffer_key");
if (der == NULL || password == NULL || info == NULL) {
WOLFSSL_MSG("bad arguments");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
/* use file's salt for key derivation, hex decode first */
if (Base16_Decode(info->iv, info->ivSz, info->iv, &info->ivSz) != 0) {
WOLFSSL_MSG("base16 decode failed");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
#ifndef NO_MD5
#ifdef WOLFSSL_SMALL_STACK
key = (byte*)XMALLOC(AES_256_KEY_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (key == NULL) {
WOLFSSL_MSG("memory failure");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
#endif /* WOLFSSL_SMALL_STACK */
2016-06-27 10:53:34 -07:00
if ((ret = wolfSSL_EVP_BytesToKey(info->name, "MD5", info->iv,
password, passwordSz, 1, key, NULL)) <= 0) {
WOLFSSL_MSG("bytes to key failure");
2015-07-07 09:23:02 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2015-07-07 09:23:02 -07:00
#endif
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
#endif /* NO_MD5 */
#ifndef NO_DES3
if (XSTRNCMP(info->name, EVP_DES_CBC, EVP_DES_SIZE) == 0)
ret = wc_Des_CbcDecryptWithKey(der->buffer, der->buffer, der->length,
key, info->iv);
else if (XSTRNCMP(info->name, EVP_DES_EDE3_CBC, EVP_DES_EDE3_SIZE) == 0)
ret = wc_Des3_CbcDecryptWithKey(der->buffer, der->buffer, der->length,
key, info->iv);
2015-07-01 12:24:11 -07:00
#endif /* NO_DES3 */
#if !defined(NO_AES) && defined(HAVE_AES_CBC)
if (XSTRNCMP(info->name, EVP_AES_128_CBC, EVP_AES_SIZE) == 0)
ret = wc_AesCbcDecryptWithKey(der->buffer, der->buffer, der->length,
key, AES_128_KEY_SIZE, info->iv);
else if (XSTRNCMP(info->name, EVP_AES_192_CBC, EVP_AES_SIZE) == 0)
ret = wc_AesCbcDecryptWithKey(der->buffer, der->buffer, der->length,
key, AES_192_KEY_SIZE, info->iv);
else if (XSTRNCMP(info->name, EVP_AES_256_CBC, EVP_AES_SIZE) == 0)
ret = wc_AesCbcDecryptWithKey(der->buffer, der->buffer, der->length,
key, AES_256_KEY_SIZE, info->iv);
#endif /* !NO_AES && HAVE_AES_CBC */
2015-07-01 12:24:11 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2015-07-01 12:24:11 -07:00
#endif
if (ret == MP_OKAY)
return SSL_SUCCESS;
else if (ret == SSL_BAD_FILE)
return SSL_BAD_FILE;
return SSL_FATAL_ERROR;
2015-07-01 12:24:11 -07:00
}
#endif /* defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) */
#if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA)
static int wolfssl_encrypt_buffer_key(byte* der, word32 derSz, byte* password,
int passwordSz, EncryptedInfo* info)
{
int ret = SSL_BAD_FILE;
#ifdef WOLFSSL_SMALL_STACK
byte* key = NULL;
#else
byte key[AES_256_KEY_SIZE];
#endif
(void)derSz;
(void)passwordSz;
(void)key;
WOLFSSL_ENTER("wolfssl_encrypt_buffer_key");
if (der == NULL || password == NULL || info == NULL || info->ivSz == 0) {
WOLFSSL_MSG("bad arguments");
return SSL_FATAL_ERROR;
}
#ifndef NO_MD5
#ifdef WOLFSSL_SMALL_STACK
key = (byte*)XMALLOC(AES_256_KEY_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (key == NULL) {
WOLFSSL_MSG("memory failure");
return SSL_FATAL_ERROR;
}
#endif /* WOLFSSL_SMALL_STACK */
2016-06-27 10:53:34 -07:00
if ((ret = wolfSSL_EVP_BytesToKey(info->name, "MD5", info->iv,
password, passwordSz, 1, key, NULL)) <= 0) {
WOLFSSL_MSG("bytes to key failure");
#ifdef WOLFSSL_SMALL_STACK
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return SSL_FATAL_ERROR;
}
#endif /* NO_MD5 */
if (ret > 0) {
ret = SSL_BAD_FILE; /* Reset error return */
#ifndef NO_DES3
if (XSTRNCMP(info->name, EVP_DES_CBC, EVP_DES_SIZE) == 0)
ret = wc_Des_CbcEncryptWithKey(der, der, derSz, key, info->iv);
else if (XSTRNCMP(info->name, EVP_DES_EDE3_CBC, EVP_DES_EDE3_SIZE) == 0)
ret = wc_Des3_CbcEncryptWithKey(der, der, derSz, key, info->iv);
#endif /* NO_DES3 */
#ifndef NO_AES
if (XSTRNCMP(info->name, EVP_AES_128_CBC, EVP_AES_SIZE) == 0)
ret = wc_AesCbcEncryptWithKey(der, der, derSz,
key, AES_128_KEY_SIZE, info->iv);
else if (XSTRNCMP(info->name, EVP_AES_192_CBC, EVP_AES_SIZE) == 0)
ret = wc_AesCbcEncryptWithKey(der, der, derSz,
key, AES_192_KEY_SIZE, info->iv);
else if (XSTRNCMP(info->name, EVP_AES_256_CBC, EVP_AES_SIZE) == 0)
ret = wc_AesCbcEncryptWithKey(der, der, derSz,
key, AES_256_KEY_SIZE, info->iv);
#endif /* NO_AES */
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2015-11-02 15:51:01 -03:00
if (ret == MP_OKAY)
return SSL_SUCCESS;
else if (ret == SSL_BAD_FILE)
return SSL_BAD_FILE;
return SSL_FATAL_ERROR;
}
#endif /* defined(WOLFSSL_KEY_GEN) */
2011-02-05 11:14:47 -08:00
#ifndef NO_CERTS
2014-09-08 21:34:07 -03:00
/* Remove PEM header/footer, convert to ASN1, store any encrypted data
info->consumed tracks of PEM bytes consumed in case multiple parts */
int PemToDer(const unsigned char* buff, long longSz, int type,
DerBuffer** pDer, void* heap, EncryptedInfo* info, int* eccKey)
2014-09-08 21:34:07 -03:00
{
const char* header = NULL;
const char* footer = NULL;
char* headerEnd;
char* footerEnd;
char* consumedEnd;
char* bufferEnd = (char*)(buff + longSz);
long neededSz;
int ret = 0;
int sz = (int)longSz;
int encrypted_key = 0;
DerBuffer* der;
WOLFSSL_ENTER("PemToDer");
switch (type) {
case CA_TYPE: /* same as below */
2016-02-24 15:51:29 -07:00
case TRUSTED_PEER_TYPE:
case CERT_TYPE: header=BEGIN_CERT; footer=END_CERT; break;
case CRL_TYPE: header=BEGIN_X509_CRL; footer=END_X509_CRL; break;
case DH_PARAM_TYPE: header=BEGIN_DH_PARAM; footer=END_DH_PARAM; break;
case CERTREQ_TYPE: header=BEGIN_CERT_REQ; footer=END_CERT_REQ; break;
case DSA_TYPE: header=BEGIN_DSA_PRIV; footer=END_DSA_PRIV; break;
case ECC_TYPE: header=BEGIN_EC_PRIV; footer=END_EC_PRIV; break;
case RSA_TYPE: header=BEGIN_RSA_PRIV; footer=END_RSA_PRIV; break;
case PUBLICKEY_TYPE: header=BEGIN_PUB_KEY; footer=END_PUB_KEY; break;
default: header=BEGIN_RSA_PRIV; footer=END_RSA_PRIV; break;
}
2015-07-01 12:24:11 -07:00
2014-09-08 21:34:07 -03:00
/* find header */
for (;;) {
headerEnd = XSTRNSTR((char*)buff, header, sz);
2015-07-01 12:24:11 -07:00
if (headerEnd || type != PRIVATEKEY_TYPE) {
break;
} else if (header == BEGIN_RSA_PRIV) {
header = BEGIN_PRIV_KEY; footer = END_PRIV_KEY;
} else if (header == BEGIN_PRIV_KEY) {
header = BEGIN_ENC_PRIV_KEY; footer = END_ENC_PRIV_KEY;
} else if (header == BEGIN_ENC_PRIV_KEY) {
header = BEGIN_EC_PRIV; footer = END_EC_PRIV;
} else if (header == BEGIN_EC_PRIV) {
header = BEGIN_DSA_PRIV; footer = END_DSA_PRIV;
} else
break;
}
2014-09-08 21:34:07 -03:00
if (!headerEnd) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Couldn't find PEM header");
2014-09-08 21:34:07 -03:00
return SSL_NO_PEM_HEADER;
}
2014-09-08 21:34:07 -03:00
headerEnd += XSTRLEN(header);
2011-02-05 11:14:47 -08:00
if ((headerEnd + 1) >= bufferEnd)
return SSL_BAD_FILE;
2014-09-08 21:34:07 -03:00
/* eat end of line */
if (headerEnd[0] == '\n')
headerEnd++;
else if (headerEnd[1] == '\n')
headerEnd += 2;
else {
if (info)
info->consumed = (long)(headerEnd+2 - (char*)buff);
2014-09-08 21:34:07 -03:00
return SSL_BAD_FILE;
}
2011-02-05 11:14:47 -08:00
if (type == PRIVATEKEY_TYPE) {
if (eccKey)
2015-07-01 12:24:11 -07:00
*eccKey = header == BEGIN_EC_PRIV;
}
2014-09-08 21:34:07 -03:00
#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
{
/* remove encrypted header if there */
char encHeader[] = "Proc-Type";
char* line = XSTRNSTR(headerEnd, encHeader, PEM_LINE_LEN);
if (line) {
char* newline;
char* finish;
char* start = XSTRNSTR(line, "DES", PEM_LINE_LEN);
if (!start)
start = XSTRNSTR(line, "AES", PEM_LINE_LEN);
if (!start) return SSL_BAD_FILE;
if (!info) return SSL_BAD_FILE;
finish = XSTRNSTR(start, ",", PEM_LINE_LEN);
if (start && finish && (start < finish)) {
newline = XSTRNSTR(finish, "\r", PEM_LINE_LEN);
if (XMEMCPY(info->name, start, finish - start) == NULL)
return SSL_FATAL_ERROR;
info->name[finish - start] = 0;
if (XMEMCPY(info->iv, finish + 1, sizeof(info->iv)) == NULL)
return SSL_FATAL_ERROR;
if (!newline) newline = XSTRNSTR(finish, "\n", PEM_LINE_LEN);
if (newline && (newline > finish)) {
info->ivSz = (word32)(newline - (finish + 1));
info->set = 1;
}
else
return SSL_BAD_FILE;
}
else
return SSL_BAD_FILE;
/* eat blank line */
while (*newline == '\r' || *newline == '\n')
newline++;
headerEnd = newline;
2015-07-01 12:24:11 -07:00
encrypted_key = 1;
}
}
#endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
2011-02-05 11:14:47 -08:00
2014-09-08 21:34:07 -03:00
/* find footer */
footerEnd = XSTRNSTR((char*)buff, footer, sz);
if (!footerEnd) {
if (info)
info->consumed = longSz; /* No more certs if no footer */
return SSL_BAD_FILE;
}
2011-02-05 11:14:47 -08:00
2014-09-08 21:34:07 -03:00
consumedEnd = footerEnd + XSTRLEN(footer);
2011-04-09 13:08:56 -07:00
2014-09-08 21:34:07 -03:00
if (consumedEnd < bufferEnd) { /* handle no end of line on last line */
/* eat end of line */
if (consumedEnd[0] == '\n')
consumedEnd++;
else if ((consumedEnd + 1 < bufferEnd) && consumedEnd[1] == '\n')
2014-09-08 21:34:07 -03:00
consumedEnd += 2;
else {
if (info)
info->consumed = (long)(consumedEnd+2 - (char*)buff);
2014-09-08 21:34:07 -03:00
return SSL_BAD_FILE;
}
2014-09-08 21:34:07 -03:00
}
2011-04-09 13:08:56 -07:00
2014-09-08 21:34:07 -03:00
if (info)
info->consumed = (long)(consumedEnd - (char*)buff);
2011-04-09 13:08:56 -07:00
2014-09-08 21:34:07 -03:00
/* set up der buffer */
neededSz = (long)(footerEnd - headerEnd);
if (neededSz > sz || neededSz <= 0)
return SSL_BAD_FILE;
ret = AllocDer(pDer, (word32)neededSz, type, heap);
if (ret < 0) {
return ret;
}
der = *pDer;
2011-02-05 11:14:47 -08:00
if (Base64_Decode((byte*)headerEnd, (word32)neededSz,
der->buffer, &der->length) < 0)
2014-09-08 21:34:07 -03:00
return SSL_BAD_FILE;
2011-02-05 11:14:47 -08:00
2015-07-01 12:24:11 -07:00
if (header == BEGIN_PRIV_KEY && !encrypted_key) {
/* pkcs8 key, convert and adjust length */
if ((ret = ToTraditional(der->buffer, der->length)) < 0)
2014-09-08 21:34:07 -03:00
return ret;
der->length = ret;
return 0;
2014-09-08 21:34:07 -03:00
}
2011-02-05 11:14:47 -08:00
#if (defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)) && !defined(NO_PWDBASED)
2015-07-01 12:24:11 -07:00
if (encrypted_key || header == BEGIN_ENC_PRIV_KEY) {
int passwordSz;
#ifdef WOLFSSL_SMALL_STACK
char* password = NULL;
#else
char password[80];
#endif
2014-09-08 21:34:07 -03:00
if (!info || !info->ctx || !info->ctx->passwd_cb)
return SSL_BAD_FILE; /* no callback error */
#ifdef WOLFSSL_SMALL_STACK
password = (char*)XMALLOC(80, heap, DYNAMIC_TYPE_TMP_BUFFER);
if (password == NULL)
return MEMORY_E;
#endif
passwordSz = info->ctx->passwd_cb(password, sizeof(password), 0,
info->ctx->userdata);
2014-09-08 21:34:07 -03:00
/* convert and adjust length */
if (header == BEGIN_ENC_PRIV_KEY) {
ret = ToTraditionalEnc(der->buffer, der->length,
password, passwordSz);
#ifdef WOLFSSL_SMALL_STACK
XFREE(password, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
if (ret < 0) {
return ret;
}
der->length = ret;
}
/* decrypt the key */
else {
ret = wolfssl_decrypt_buffer_key(der, (byte*)password,
passwordSz, info);
2015-07-01 12:24:11 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(password, heap, DYNAMIC_TYPE_TMP_BUFFER);
2015-07-01 12:24:11 -07:00
#endif
if (ret != SSL_SUCCESS) {
return ret;
}
}
}
2015-07-01 12:24:11 -07:00
#endif /* OPENSSL_EXTRA || HAVE_WEBSERVER || NO_PWDBASED */
2011-02-05 11:14:47 -08:00
2014-09-08 21:34:07 -03:00
return 0;
}
2011-02-05 11:14:47 -08:00
/* process user cert chain to pass during the handshake */
static int ProcessUserChain(WOLFSSL_CTX* ctx, const unsigned char* buff,
long sz, int format, int type, WOLFSSL* ssl,
long* used, EncryptedInfo* info)
{
int ret = 0;
void* heap = ctx ? ctx->heap : ((ssl) ? ssl->heap : NULL);
/* we may have a user cert chain, try to consume */
if (type == CERT_TYPE && info->consumed < sz) {
#ifdef WOLFSSL_SMALL_STACK
byte staticBuffer[1]; /* force heap usage */
#else
byte staticBuffer[FILE_BUFFER_SIZE]; /* tmp chain buffer */
#endif
byte* chainBuffer = staticBuffer;
int dynamicBuffer = 0;
word32 bufferSz = FILE_BUFFER_SIZE;
long consumed = info->consumed;
word32 idx = 0;
int gotOne = 0;
if ( (sz - consumed) > (int)bufferSz) {
WOLFSSL_MSG("Growing Tmp Chain Buffer");
bufferSz = (word32)(sz - consumed);
/* will shrink to actual size */
chainBuffer = (byte*)XMALLOC(bufferSz, heap, DYNAMIC_TYPE_FILE);
if (chainBuffer == NULL) {
return MEMORY_E;
}
dynamicBuffer = 1;
}
WOLFSSL_MSG("Processing Cert Chain");
while (consumed < sz) {
int eccKey = 0;
DerBuffer* part = NULL;
word32 remain = (word32)(sz - consumed);
info->consumed = 0;
if (format == SSL_FILETYPE_PEM) {
ret = PemToDer(buff + consumed, remain, type, &part,
heap, info, &eccKey);
}
else {
int length = remain;
if (format == SSL_FILETYPE_ASN1) {
/* get length of der (read sequence) */
word32 inOutIdx = 0;
if (GetSequence(buff + consumed, &inOutIdx, &length, remain) < 0) {
ret = SSL_NO_PEM_HEADER;
}
length += inOutIdx; /* include leading squence */
}
info->consumed = length;
if (ret == 0) {
ret = AllocDer(&part, length, type, heap);
if (ret == 0) {
XMEMCPY(part->buffer, buff + consumed, length);
}
}
}
if (ret == 0) {
gotOne = 1;
if ((idx + part->length) > bufferSz) {
WOLFSSL_MSG(" Cert Chain bigger than buffer");
ret = BUFFER_E;
}
else {
c32to24(part->length, &chainBuffer[idx]);
idx += CERT_HEADER_SZ;
XMEMCPY(&chainBuffer[idx], part->buffer, part->length);
idx += part->length;
consumed += info->consumed;
if (used)
*used += info->consumed;
}
}
FreeDer(&part);
if (ret == SSL_NO_PEM_HEADER && gotOne) {
WOLFSSL_MSG("We got one good cert, so stuff at end ok");
break;
}
if (ret < 0) {
WOLFSSL_MSG(" Error in Cert in Chain");
if (dynamicBuffer)
XFREE(chainBuffer, heap, DYNAMIC_TYPE_FILE);
return ret;
}
WOLFSSL_MSG(" Consumed another Cert in Chain");
}
WOLFSSL_MSG("Finished Processing Cert Chain");
/* only retain actual size used */
ret = 0;
if (idx > 0) {
if (ssl) {
if (ssl->buffers.weOwnCertChain) {
FreeDer(&ssl->buffers.certChain);
}
ret = AllocDer(&ssl->buffers.certChain, idx, type, heap);
if (ret == 0) {
XMEMCPY(ssl->buffers.certChain->buffer, chainBuffer, idx);
ssl->buffers.weOwnCertChain = 1;
}
} else if (ctx) {
FreeDer(&ctx->certChain);
ret = AllocDer(&ctx->certChain, idx, type, heap);
if (ret == 0) {
XMEMCPY(ctx->certChain->buffer, chainBuffer, idx);
}
}
}
if (dynamicBuffer)
XFREE(chainBuffer, heap, DYNAMIC_TYPE_FILE);
}
return ret;
}
/* process the buffer buff, length sz, into ctx of format and type
2014-09-08 21:34:07 -03:00
used tracks bytes consumed, userChain specifies a user cert chain
to pass during the handshake */
int ProcessBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff,
2014-12-19 11:27:01 -07:00
long sz, int format, int type, WOLFSSL* ssl,
2014-09-08 21:34:07 -03:00
long* used, int userChain)
{
DerBuffer* der = NULL; /* holds DER or RAW (for NTRU) */
int ret = 0;
2014-09-08 21:34:07 -03:00
int eccKey = 0;
int rsaKey = 0;
void* heap = ctx ? ctx->heap : ((ssl) ? ssl->heap : NULL);
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
EncryptedInfo* info = NULL;
#else
2014-09-08 21:34:07 -03:00
EncryptedInfo info[1];
#endif
2011-02-05 11:14:47 -08:00
2014-09-08 21:34:07 -03:00
(void)rsaKey;
2014-09-08 21:34:07 -03:00
if (used)
*used = sz; /* used bytes default to sz, PEM chain may shorten*/
/* check args */
2014-09-08 21:34:07 -03:00
if (format != SSL_FILETYPE_ASN1 && format != SSL_FILETYPE_PEM
&& format != SSL_FILETYPE_RAW)
return SSL_BAD_FILETYPE;
2011-02-05 11:14:47 -08:00
2014-09-08 21:34:07 -03:00
if (ctx == NULL && ssl == NULL)
return BAD_FUNC_ARG;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), heap,
DYNAMIC_TYPE_TMP_BUFFER);
2014-09-08 21:34:07 -03:00
if (info == NULL)
return MEMORY_E;
#endif
2014-09-08 21:34:07 -03:00
info->set = 0;
info->ctx = ctx;
info->consumed = 0;
2011-02-05 11:14:47 -08:00
2014-09-08 21:34:07 -03:00
if (format == SSL_FILETYPE_PEM) {
ret = PemToDer(buff, sz, type, &der, heap, info, &eccKey);
}
else { /* ASN1 (DER) or RAW (NTRU) */
int length = (int)sz;
if (format == SSL_FILETYPE_ASN1) {
/* get length of der (read sequence) */
word32 inOutIdx = 0;
if (GetSequence(buff, &inOutIdx, &length, (word32)sz) < 0) {
ret = ASN_PARSE_E;
2011-04-09 13:08:56 -07:00
}
length += inOutIdx; /* include leading squence */
}
info->consumed = length;
if (ret == 0) {
ret = AllocDer(&der, (word32)length, type, heap);
if (ret == 0) {
XMEMCPY(der->buffer, buff, length);
}
}
}
if (used) {
*used = info->consumed;
}
2014-09-08 21:34:07 -03:00
/* process user chain */
if (ret >= 0) {
if (userChain) {
ret = ProcessUserChain(ctx, buff, sz, format, type, ssl, used, info);
2011-02-05 11:14:47 -08:00
}
2014-09-08 21:34:07 -03:00
}
/* check for error */
if (ret < 0) {
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
FreeDer(&der);
return ret;
2014-09-08 21:34:07 -03:00
}
2011-02-05 11:14:47 -08:00
#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
/* for SSL_FILETYPE_PEM, PemToDer manage the decryption if required */
2015-07-01 12:24:11 -07:00
if (info->set && (format != SSL_FILETYPE_PEM)) {
2014-09-08 21:34:07 -03:00
/* decrypt */
int passwordSz;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
char* password = NULL;
#else
char password[80];
#endif
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
password = (char*)XMALLOC(80, heap, DYNAMIC_TYPE_TMP_BUFFER);
2015-07-01 12:24:11 -07:00
if (password == NULL)
ret = MEMORY_E;
else
#endif
2014-09-08 21:34:07 -03:00
if (!ctx || !ctx->passwd_cb) {
ret = NO_PASSWORD;
2014-09-08 21:34:07 -03:00
}
else {
passwordSz = ctx->passwd_cb(password, sizeof(password),
0, ctx->userdata);
2011-02-05 11:14:47 -08:00
/* decrypt the key */
ret = wolfssl_decrypt_buffer_key(der, (byte*)password,
passwordSz, info);
2011-02-05 11:14:47 -08:00
}
2014-09-08 21:34:07 -03:00
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(password, heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
if (ret != SSL_SUCCESS) {
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, heap, DYNAMIC_TYPE_TMP_BUFFER);
2014-09-08 21:34:07 -03:00
#endif
FreeDer(&der);
2014-09-08 21:34:07 -03:00
return ret;
}
}
#endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
2014-09-08 21:34:07 -03:00
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2011-02-05 11:14:47 -08:00
/* Handle DER owner */
2014-09-08 21:34:07 -03:00
if (type == CA_TYPE) {
if (ctx == NULL) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Need context for CA load");
FreeDer(&der);
2014-09-08 21:34:07 -03:00
return BAD_FUNC_ARG;
2011-02-05 11:14:47 -08:00
}
/* verify CA unless user set to no verify */
return AddCA(ctx->cm, &der, WOLFSSL_USER_CA, !ctx->verifyNone);
2014-09-08 21:34:07 -03:00
}
2016-02-24 15:51:29 -07:00
#ifdef WOLFSSL_TRUST_PEER_CERT
else if (type == TRUSTED_PEER_TYPE) {
if (ctx == NULL) {
WOLFSSL_MSG("Need context for trusted peer cert load");
FreeDer(&der);
2016-02-24 15:51:29 -07:00
return BAD_FUNC_ARG;
}
/* add trusted peer cert */
return AddTrustedPeer(ctx->cm, &der, !ctx->verifyNone);
2014-09-08 21:34:07 -03:00
}
2016-02-24 15:51:29 -07:00
#endif /* WOLFSSL_TRUST_PEER_CERT */
2014-09-08 21:34:07 -03:00
else if (type == CERT_TYPE) {
if (ssl) {
/* Make sure previous is free'd */
if (ssl->buffers.weOwnCert) {
FreeDer(&ssl->buffers.certificate);
#ifdef KEEP_OUR_CERT
FreeX509(ssl->ourCert);
if (ssl->ourCert) {
XFREE(ssl->ourCert, ssl->heap, DYNAMIC_TYPE_X509);
ssl->ourCert = NULL;
}
#endif
}
ssl->buffers.certificate = der;
#ifdef KEEP_OUR_CERT
ssl->keepCert = 1; /* hold cert for ssl lifetime */
#endif
2014-09-08 21:34:07 -03:00
ssl->buffers.weOwnCert = 1;
2011-02-05 11:14:47 -08:00
}
2014-09-08 21:34:07 -03:00
else if (ctx) {
FreeDer(&ctx->certificate); /* Make sure previous is free'd */
#ifdef KEEP_OUR_CERT
FreeX509(ctx->ourCert);
if (ctx->ourCert) {
XFREE(ctx->ourCert, ctx->heap, DYNAMIC_TYPE_X509);
ctx->ourCert = NULL;
}
#endif
ctx->certificate = der;
2011-02-05 11:14:47 -08:00
}
2014-09-08 21:34:07 -03:00
}
else if (type == PRIVATEKEY_TYPE) {
if (ssl) {
/* Make sure previous is free'd */
if (ssl->buffers.weOwnKey) {
FreeDer(&ssl->buffers.key);
}
ssl->buffers.key = der;
2014-09-08 21:34:07 -03:00
ssl->buffers.weOwnKey = 1;
}
else if (ctx) {
FreeDer(&ctx->privateKey);
ctx->privateKey = der;
2014-09-08 21:34:07 -03:00
}
}
else {
FreeDer(&der);
2014-09-08 21:34:07 -03:00
return SSL_BAD_CERTTYPE;
}
2011-02-05 11:14:47 -08:00
2014-09-08 21:34:07 -03:00
if (type == PRIVATEKEY_TYPE && format != SSL_FILETYPE_RAW) {
#ifndef NO_RSA
if (!eccKey) {
/* make sure RSA key can be used */
word32 idx = 0;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
RsaKey* key = NULL;
#else
RsaKey key[1];
#endif
2014-09-08 21:34:07 -03:00
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
key = (RsaKey*)XMALLOC(sizeof(RsaKey), heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (key == NULL)
return MEMORY_E;
#endif
ret = wc_InitRsaKey(key, 0);
if (ret == 0) {
if (wc_RsaPrivateKeyDecode(der->buffer, &idx, key, der->length)
!= 0) {
#ifdef HAVE_ECC
/* could have DER ECC (or pkcs8 ecc), no easy way to tell */
eccKey = 1; /* so try it out */
#endif
if (!eccKey)
ret = SSL_BAD_FILE;
} else {
/* check that the size of the RSA key is enough */
int RsaSz = wc_RsaEncryptSize((RsaKey*)key);
if (ssl) {
if (RsaSz < ssl->options.minRsaKeySz) {
ret = RSA_KEY_SIZE_E;
WOLFSSL_MSG("Private Key size too small");
}
}
else if(ctx) {
if (RsaSz < ctx->minRsaKeySz) {
ret = RSA_KEY_SIZE_E;
WOLFSSL_MSG("Private Key size too small");
}
}
rsaKey = 1;
(void)rsaKey; /* for no ecc builds */
2011-02-05 11:14:47 -08:00
}
}
wc_FreeRsaKey(key);
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(key, heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
if (ret != 0)
return ret;
2011-02-05 11:14:47 -08:00
}
2014-09-08 21:34:07 -03:00
#endif
#ifdef HAVE_ECC
if (!rsaKey) {
/* make sure ECC key can be used */
word32 idx = 0;
ecc_key key;
2012-05-03 08:18:59 -07:00
2014-12-31 10:01:32 -07:00
wc_ecc_init(&key);
if (wc_EccPrivateKeyDecode(der->buffer, &idx, &key,
der->length) != 0) {
2014-12-31 10:01:32 -07:00
wc_ecc_free(&key);
return SSL_BAD_FILE;
}
/* check for minimum ECC key size and then free */
if (ssl) {
if (wc_ecc_size(&key) < ssl->options.minEccKeySz) {
wc_ecc_free(&key);
WOLFSSL_MSG("ECC private key too small");
return ECC_KEY_SIZE_E;
}
}
else if (ctx) {
if (wc_ecc_size(&key) < ctx->minEccKeySz) {
wc_ecc_free(&key);
WOLFSSL_MSG("ECC private key too small");
return ECC_KEY_SIZE_E;
}
}
2014-12-31 10:01:32 -07:00
wc_ecc_free(&key);
2014-09-08 21:34:07 -03:00
eccKey = 1;
2014-01-14 14:20:34 -03:00
if (ctx)
2014-09-08 21:34:07 -03:00
ctx->haveStaticECC = 1;
2014-01-14 14:20:34 -03:00
if (ssl)
2014-09-08 21:34:07 -03:00
ssl->options.haveStaticECC = 1;
}
#endif /* HAVE_ECC */
}
else if (type == CERT_TYPE) {
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
DecodedCert* cert = NULL;
#else
DecodedCert cert[1];
#endif
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (cert == NULL)
return MEMORY_E;
#endif
2014-01-14 14:20:34 -03:00
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Checking cert signature type");
InitDecodedCert(cert, der->buffer, der->length, heap);
2014-09-08 21:34:07 -03:00
if (DecodeToKey(cert, 0) < 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Decode to key failed");
#ifdef WOLFSSL_SMALL_STACK
XFREE(cert, heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2014-09-08 21:34:07 -03:00
return SSL_BAD_FILE;
}
switch (cert->signatureOID) {
2014-09-08 21:34:07 -03:00
case CTC_SHAwECDSA:
case CTC_SHA256wECDSA:
case CTC_SHA384wECDSA:
case CTC_SHA512wECDSA:
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("ECDSA cert signature");
2014-09-08 21:34:07 -03:00
if (ctx)
ctx->haveECDSAsig = 1;
if (ssl)
ssl->options.haveECDSAsig = 1;
break;
default:
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Not ECDSA cert signature");
2014-09-08 21:34:07 -03:00
break;
2012-05-03 08:18:59 -07:00
}
2011-02-05 11:14:47 -08:00
2014-09-08 21:34:07 -03:00
#ifdef HAVE_ECC
2016-02-10 11:02:09 -07:00
if (ctx) {
ctx->pkCurveOID = cert->pkCurveOID;
2016-02-10 11:02:09 -07:00
#ifndef WC_STRICT_SIG
if (cert->keyOID == ECDSAk) {
ctx->haveECC = 1;
}
#else
ctx->haveECC = ctx->haveECDSAsig;
#endif
}
if (ssl) {
ssl->pkCurveOID = cert->pkCurveOID;
2016-02-10 11:02:09 -07:00
#ifndef WC_STRICT_SIG
if (cert->keyOID == ECDSAk) {
ssl->options.haveECC = 1;
}
#else
ssl->options.haveECC = ssl->options.haveECDSAsig;
#endif
}
2014-09-08 21:34:07 -03:00
#endif
2011-02-05 11:14:47 -08:00
/* check key size of cert unless specified not to */
switch (cert->keyOID) {
#ifndef NO_RSA
case RSAk:
if (ssl && !ssl->options.verifyNone) {
if (ssl->options.minRsaKeySz < 0 ||
cert->pubKeySize < (word16)ssl->options.minRsaKeySz) {
ret = RSA_KEY_SIZE_E;
WOLFSSL_MSG("Certificate RSA key size too small");
}
}
else if (ctx && !ctx->verifyNone) {
if (ctx->minRsaKeySz < 0 ||
cert->pubKeySize < (word16)ctx->minRsaKeySz) {
ret = RSA_KEY_SIZE_E;
WOLFSSL_MSG("Certificate RSA key size too small");
}
}
break;
#endif /* !NO_RSA */
#ifdef HAVE_ECC
case ECDSAk:
if (ssl && !ssl->options.verifyNone) {
if (ssl->options.minEccKeySz < 0 ||
cert->pubKeySize < (word16)ssl->options.minEccKeySz) {
ret = ECC_KEY_SIZE_E;
WOLFSSL_MSG("Certificate ECC key size error");
}
}
else if (ctx && !ctx->verifyNone) {
if (ctx->minEccKeySz < 0 ||
cert->pubKeySize < (word16)ctx->minEccKeySz) {
ret = ECC_KEY_SIZE_E;
WOLFSSL_MSG("Certificate ECC key size error");
}
}
break;
#endif /* HAVE_ECC */
default:
WOLFSSL_MSG("No key size check done on certificate");
break; /* do no check if not a case for the key */
}
FreeDecodedCert(cert);
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(cert, heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
if (ret != 0) {
return ret;
}
2014-09-08 21:34:07 -03:00
}
2011-02-05 11:14:47 -08:00
2014-09-08 21:34:07 -03:00
return SSL_SUCCESS;
}
2012-05-07 16:35:23 -07:00
2011-05-09 13:42:48 -07:00
/* CA PEM file for verification, may have multiple/chain certs to process */
2014-12-19 11:27:01 -07:00
static int ProcessChainBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff,
long sz, int format, int type, WOLFSSL* ssl)
{
2012-09-17 17:25:38 -07:00
long used = 0;
int ret = 0;
int gotOne = 0;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Processing CA PEM file");
while (used < sz) {
long consumed = 0;
ret = ProcessBuffer(ctx, buff + used, sz - used, format, type, ssl,
2011-05-09 13:42:48 -07:00
&consumed, 0);
2012-09-17 17:25:38 -07:00
if (ret < 0)
{
if(consumed > 0) { /* Made progress in file */
WOLFSSL_ERROR(ret);
WOLFSSL_MSG("CA Parse failed, with progress in file.");
WOLFSSL_MSG("Search for other certs in file");
} else {
WOLFSSL_MSG("CA Parse failed, no progress in file.");
WOLFSSL_MSG("Do not continue search for other certs in file");
break;
}
} else {
WOLFSSL_MSG(" Processed a CA");
gotOne = 1;
}
used += consumed;
}
2012-09-17 17:25:38 -07:00
if(gotOne)
{
WOLFSSL_MSG("Processed at least one valid CA. Other stuff OK");
return SSL_SUCCESS;
}
return ret;
}
static INLINE WOLFSSL_METHOD* cm_pick_method(void)
{
#ifndef NO_WOLFSSL_CLIENT
2015-08-12 16:39:13 -07:00
#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
return wolfSSLv3_client_method();
2015-08-12 16:39:13 -07:00
#else
return wolfTLSv1_2_client_method();
#endif
#elif !defined(NO_WOLFSSL_SERVER)
2015-08-12 16:39:13 -07:00
#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
return wolfSSLv3_server_method();
2015-08-12 16:39:13 -07:00
#else
return wolfTLSv1_2_server_method();
#endif
#else
return NULL;
#endif
}
/* like load verify locations, 1 for success, < 0 for error */
int wolfSSL_CertManagerLoadCABuffer(WOLFSSL_CERT_MANAGER* cm,
const unsigned char* in, long sz, int format)
{
int ret = SSL_FATAL_ERROR;
WOLFSSL_CTX* tmp;
WOLFSSL_ENTER("wolfSSL_CertManagerLoadCABuffer");
if (cm == NULL) {
WOLFSSL_MSG("No CertManager error");
return ret;
}
tmp = wolfSSL_CTX_new(cm_pick_method());
if (tmp == NULL) {
WOLFSSL_MSG("CTX new failed");
return ret;
}
/* for tmp use */
wolfSSL_CertManagerFree(tmp->cm);
tmp->cm = cm;
ret = wolfSSL_CTX_load_verify_buffer(tmp, in, sz, format);
/* don't loose our good one */
tmp->cm = NULL;
wolfSSL_CTX_free(tmp);
return ret;
}
2015-08-31 14:23:50 -07:00
#ifdef HAVE_CRL
int wolfSSL_CertManagerLoadCRLBuffer(WOLFSSL_CERT_MANAGER* cm,
const unsigned char* buff, long sz, int type)
{
WOLFSSL_ENTER("wolfSSL_CertManagerLoadCRLBuffer");
if (cm == NULL)
return BAD_FUNC_ARG;
if (cm->crl == NULL) {
if (wolfSSL_CertManagerEnableCRL(cm, 0) != SSL_SUCCESS) {
WOLFSSL_MSG("Enable CRL failed");
return SSL_FATAL_ERROR;
}
}
return BufferLoadCRL(cm->crl, buff, sz, type);
}
int wolfSSL_CTX_LoadCRLBuffer(WOLFSSL_CTX* ctx, const unsigned char* buff,
long sz, int type)
{
WOLFSSL_ENTER("wolfSSL_CTX_LoadCRLBuffer");
if (ctx == NULL)
return BAD_FUNC_ARG;
return wolfSSL_CertManagerLoadCRLBuffer(ctx->cm, buff, sz, type);
}
int wolfSSL_LoadCRLBuffer(WOLFSSL* ssl, const unsigned char* buff,
long sz, int type)
{
WOLFSSL_ENTER("wolfSSL_LoadCRLBuffer");
if (ssl == NULL || ssl->ctx == NULL)
return BAD_FUNC_ARG;
return wolfSSL_CertManagerLoadCRLBuffer(ssl->ctx->cm, buff, sz, type);
}
2015-08-31 14:23:50 -07:00
#endif /* HAVE_CRL */
2015-08-31 14:57:23 -07:00
/* turn on CRL if off and compiled in, set options */
int wolfSSL_CertManagerEnableCRL(WOLFSSL_CERT_MANAGER* cm, int options)
{
int ret = SSL_SUCCESS;
(void)options;
WOLFSSL_ENTER("wolfSSL_CertManagerEnableCRL");
if (cm == NULL)
return BAD_FUNC_ARG;
#ifdef HAVE_CRL
if (cm->crl == NULL) {
cm->crl = (WOLFSSL_CRL*)XMALLOC(sizeof(WOLFSSL_CRL), cm->heap,
DYNAMIC_TYPE_CRL);
if (cm->crl == NULL)
return MEMORY_E;
if (InitCRL(cm->crl, cm) != 0) {
WOLFSSL_MSG("Init CRL failed");
FreeCRL(cm->crl, 1);
cm->crl = NULL;
return SSL_FAILURE;
}
}
cm->crlEnabled = 1;
if (options & WOLFSSL_CRL_CHECKALL)
cm->crlCheckAll = 1;
#else
ret = NOT_COMPILED_IN;
#endif
return ret;
}
int wolfSSL_CertManagerDisableCRL(WOLFSSL_CERT_MANAGER* cm)
{
WOLFSSL_ENTER("wolfSSL_CertManagerDisableCRL");
if (cm == NULL)
return BAD_FUNC_ARG;
cm->crlEnabled = 0;
return SSL_SUCCESS;
}
/* Verify the certificate, SSL_SUCCESS for ok, < 0 for error */
2014-12-19 11:27:01 -07:00
int wolfSSL_CertManagerVerifyBuffer(WOLFSSL_CERT_MANAGER* cm, const byte* buff,
long sz, int format)
{
int ret = 0;
DerBuffer* der = NULL;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
DecodedCert* cert = NULL;
#else
DecodedCert cert[1];
#endif
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertManagerVerifyBuffer");
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), cm->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (cert == NULL)
return MEMORY_E;
#endif
if (format == SSL_FILETYPE_PEM) {
int eccKey = 0; /* not used */
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
EncryptedInfo* info = NULL;
2014-09-08 21:34:07 -03:00
#else
EncryptedInfo info[1];
2014-09-08 21:34:07 -03:00
#endif
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), cm->heap,
DYNAMIC_TYPE_TMP_BUFFER);
if (info == NULL) {
XFREE(cert, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
return MEMORY_E;
}
2014-09-08 21:34:07 -03:00
#endif
info->set = 0;
info->ctx = NULL;
info->consumed = 0;
ret = PemToDer(buff, sz, CERT_TYPE, &der, cm->heap, info, &eccKey);
if (ret != 0) {
FreeDer(&der);
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return ret;
}
InitDecodedCert(cert, der->buffer, der->length, cm->heap);
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
2014-09-08 21:34:07 -03:00
#endif
}
else
InitDecodedCert(cert, (byte*)buff, (word32)sz, cm->heap);
if (ret == 0)
ret = ParseCertRelative(cert, CERT_TYPE, 1, cm);
#ifdef HAVE_CRL
if (ret == 0 && cm->crlEnabled)
ret = CheckCertCRL(cm->crl, cert);
#endif
FreeDecodedCert(cert);
FreeDer(&der);
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(cert, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return ret == 0 ? SSL_SUCCESS : ret;
}
/* turn on OCSP if off and compiled in, set options */
2014-12-19 11:27:01 -07:00
int wolfSSL_CertManagerEnableOCSP(WOLFSSL_CERT_MANAGER* cm, int options)
{
int ret = SSL_SUCCESS;
(void)options;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertManagerEnableOCSP");
if (cm == NULL)
return BAD_FUNC_ARG;
#ifdef HAVE_OCSP
if (cm->ocsp == NULL) {
2014-12-19 11:27:01 -07:00
cm->ocsp = (WOLFSSL_OCSP*)XMALLOC(sizeof(WOLFSSL_OCSP), cm->heap,
DYNAMIC_TYPE_OCSP);
if (cm->ocsp == NULL)
return MEMORY_E;
if (InitOCSP(cm->ocsp, cm) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Init OCSP failed");
FreeOCSP(cm->ocsp, 1);
cm->ocsp = NULL;
return SSL_FAILURE;
}
}
cm->ocspEnabled = 1;
2014-12-19 11:27:01 -07:00
if (options & WOLFSSL_OCSP_URL_OVERRIDE)
cm->ocspUseOverrideURL = 1;
2014-12-19 11:27:01 -07:00
if (options & WOLFSSL_OCSP_NO_NONCE)
cm->ocspSendNonce = 0;
else
cm->ocspSendNonce = 1;
if (options & WOLFSSL_OCSP_CHECKALL)
cm->ocspCheckAll = 1;
2014-12-19 11:27:01 -07:00
#ifndef WOLFSSL_USER_IO
cm->ocspIOCb = EmbedOcspLookup;
cm->ocspRespFreeCb = EmbedOcspRespFree;
cm->ocspIOCtx = cm->heap;
2014-12-19 11:27:01 -07:00
#endif /* WOLFSSL_USER_IO */
#else
ret = NOT_COMPILED_IN;
#endif
return ret;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CertManagerDisableOCSP(WOLFSSL_CERT_MANAGER* cm)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertManagerDisableOCSP");
if (cm == NULL)
return BAD_FUNC_ARG;
cm->ocspEnabled = 0;
return SSL_SUCCESS;
}
2015-12-28 19:38:04 -03:00
/* turn on OCSP Stapling if off and compiled in, set options */
int wolfSSL_CertManagerEnableOCSPStapling(WOLFSSL_CERT_MANAGER* cm)
{
int ret = SSL_SUCCESS;
WOLFSSL_ENTER("wolfSSL_CertManagerEnableOCSPStapling");
if (cm == NULL)
return BAD_FUNC_ARG;
#if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
|| defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
if (cm->ocsp_stapling == NULL) {
cm->ocsp_stapling = (WOLFSSL_OCSP*)XMALLOC(sizeof(WOLFSSL_OCSP),
cm->heap, DYNAMIC_TYPE_OCSP);
if (cm->ocsp_stapling == NULL)
return MEMORY_E;
if (InitOCSP(cm->ocsp_stapling, cm) != 0) {
WOLFSSL_MSG("Init OCSP failed");
FreeOCSP(cm->ocsp_stapling, 1);
cm->ocsp_stapling = NULL;
return SSL_FAILURE;
}
}
cm->ocspStaplingEnabled = 1;
#ifndef WOLFSSL_USER_IO
cm->ocspIOCb = EmbedOcspLookup;
cm->ocspRespFreeCb = EmbedOcspRespFree;
cm->ocspIOCtx = cm->heap;
2015-12-28 19:38:04 -03:00
#endif /* WOLFSSL_USER_IO */
#else
ret = NOT_COMPILED_IN;
#endif
return ret;
}
#ifdef HAVE_OCSP
/* check CRL if enabled, SSL_SUCCESS */
2014-12-19 11:27:01 -07:00
int wolfSSL_CertManagerCheckOCSP(WOLFSSL_CERT_MANAGER* cm, byte* der, int sz)
{
int ret;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
DecodedCert* cert = NULL;
#else
DecodedCert cert[1];
#endif
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertManagerCheckOCSP");
if (cm == NULL)
return BAD_FUNC_ARG;
if (cm->ocspEnabled == 0)
return SSL_SUCCESS;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (cert == NULL)
return MEMORY_E;
#endif
InitDecodedCert(cert, der, sz, NULL);
if ((ret = ParseCertRelative(cert, CERT_TYPE, VERIFY_OCSP, cm)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("ParseCert failed");
}
2015-12-28 19:38:04 -03:00
else if ((ret = CheckCertOCSP(cm->ocsp, cert, NULL)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("CheckCertOCSP failed");
}
FreeDecodedCert(cert);
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return ret == 0 ? SSL_SUCCESS : ret;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CertManagerSetOCSPOverrideURL(WOLFSSL_CERT_MANAGER* cm,
const char* url)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertManagerSetOCSPOverrideURL");
if (cm == NULL)
return BAD_FUNC_ARG;
2015-11-19 10:20:28 -08:00
XFREE(cm->ocspOverrideURL, cm->heap, DYNAMIC_TYPE_URL);
if (url != NULL) {
int urlSz = (int)XSTRLEN(url) + 1;
2015-11-19 10:20:28 -08:00
cm->ocspOverrideURL = (char*)XMALLOC(urlSz, cm->heap, DYNAMIC_TYPE_URL);
if (cm->ocspOverrideURL != NULL) {
XMEMCPY(cm->ocspOverrideURL, url, urlSz);
}
else
return MEMORY_E;
}
else
cm->ocspOverrideURL = NULL;
return SSL_SUCCESS;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CertManagerSetOCSP_Cb(WOLFSSL_CERT_MANAGER* cm,
CbOCSPIO ioCb, CbOCSPRespFree respFreeCb, void* ioCbCtx)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertManagerSetOCSP_Cb");
if (cm == NULL)
return BAD_FUNC_ARG;
cm->ocspIOCb = ioCb;
cm->ocspRespFreeCb = respFreeCb;
cm->ocspIOCtx = ioCbCtx;
return SSL_SUCCESS;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_EnableOCSP(WOLFSSL* ssl, int options)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_EnableOCSP");
if (ssl)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerEnableOCSP(ssl->ctx->cm, options);
else
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_DisableOCSP(WOLFSSL* ssl)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_DisableOCSP");
if (ssl)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerDisableOCSP(ssl->ctx->cm);
else
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_SetOCSP_OverrideURL(WOLFSSL* ssl, const char* url)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_SetOCSP_OverrideURL");
if (ssl)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerSetOCSPOverrideURL(ssl->ctx->cm, url);
else
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_SetOCSP_Cb(WOLFSSL* ssl,
CbOCSPIO ioCb, CbOCSPRespFree respFreeCb, void* ioCbCtx)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_SetOCSP_Cb");
if (ssl)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerSetOCSP_Cb(ssl->ctx->cm,
ioCb, respFreeCb, ioCbCtx);
else
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_EnableOCSP(WOLFSSL_CTX* ctx, int options)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_EnableOCSP");
if (ctx)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerEnableOCSP(ctx->cm, options);
else
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_DisableOCSP(WOLFSSL_CTX* ctx)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_DisableOCSP");
if (ctx)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerDisableOCSP(ctx->cm);
else
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_SetOCSP_OverrideURL(WOLFSSL_CTX* ctx, const char* url)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_SetOCSP_OverrideURL");
if (ctx)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerSetOCSPOverrideURL(ctx->cm, url);
else
return BAD_FUNC_ARG;
}
int wolfSSL_CTX_SetOCSP_Cb(WOLFSSL_CTX* ctx, CbOCSPIO ioCb,
CbOCSPRespFree respFreeCb, void* ioCbCtx)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_SetOCSP_Cb");
if (ctx)
return wolfSSL_CertManagerSetOCSP_Cb(ctx->cm, ioCb,
respFreeCb, ioCbCtx);
else
return BAD_FUNC_ARG;
}
2015-12-28 19:38:04 -03:00
#if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
|| defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
int wolfSSL_CTX_EnableOCSPStapling(WOLFSSL_CTX* ctx)
{
WOLFSSL_ENTER("wolfSSL_CTX_EnableOCSPStapling");
if (ctx)
return wolfSSL_CertManagerEnableOCSPStapling(ctx->cm);
else
return BAD_FUNC_ARG;
}
#endif
#endif /* HAVE_OCSP */
#ifndef NO_FILESYSTEM
2011-05-09 13:42:48 -07:00
/* process a file with name fname into ctx of format and type
userChain specifies a user certificate chain to pass during handshake */
2014-12-19 11:27:01 -07:00
int ProcessFile(WOLFSSL_CTX* ctx, const char* fname, int format, int type,
WOLFSSL* ssl, int userChain, WOLFSSL_CRL* crl)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
byte staticBuffer[1]; /* force heap usage */
#else
2011-02-05 11:14:47 -08:00
byte staticBuffer[FILE_BUFFER_SIZE];
#endif
2011-05-25 10:25:05 -07:00
byte* myBuffer = staticBuffer;
2011-02-05 11:14:47 -08:00
int dynamic = 0;
int ret;
long sz = 0;
XFILE file;
void* heapHint = ctx ? ctx->heap : ((ssl) ? ssl->heap : NULL);
2011-02-05 11:14:47 -08:00
2012-05-17 17:44:54 -07:00
(void)crl;
2012-12-04 21:28:18 -08:00
(void)heapHint;
2012-05-17 17:44:54 -07:00
2012-12-04 21:28:18 -08:00
if (fname == NULL) return SSL_BAD_FILE;
file = XFOPEN(fname, "rb");
if (file == XBADFILE) return SSL_BAD_FILE;
2011-02-05 11:14:47 -08:00
XFSEEK(file, 0, XSEEK_END);
sz = XFTELL(file);
XREWIND(file);
if (sz > (long)sizeof(staticBuffer)) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Getting dynamic buffer");
2012-12-04 21:28:18 -08:00
myBuffer = (byte*)XMALLOC(sz, heapHint, DYNAMIC_TYPE_FILE);
2011-05-25 10:25:05 -07:00
if (myBuffer == NULL) {
2011-02-05 11:14:47 -08:00
XFCLOSE(file);
return SSL_BAD_FILE;
}
dynamic = 1;
}
2013-02-14 14:09:41 -08:00
else if (sz < 0) {
XFCLOSE(file);
return SSL_BAD_FILE;
}
2011-02-05 11:14:47 -08:00
if ( (ret = (int)XFREAD(myBuffer, sz, 1, file)) < 0)
2011-02-05 11:14:47 -08:00
ret = SSL_BAD_FILE;
else {
2016-02-24 15:51:29 -07:00
if ((type == CA_TYPE || type == TRUSTED_PEER_TYPE)
&& format == SSL_FILETYPE_PEM)
2011-05-25 10:25:05 -07:00
ret = ProcessChainBuffer(ctx, myBuffer, sz, format, type, ssl);
2012-05-16 17:35:51 -07:00
#ifdef HAVE_CRL
2012-05-16 17:04:56 -07:00
else if (type == CRL_TYPE)
ret = BufferLoadCRL(crl, myBuffer, sz, format);
2012-05-16 17:35:51 -07:00
#endif
else
2011-05-25 10:25:05 -07:00
ret = ProcessBuffer(ctx, myBuffer, sz, format, type, ssl, NULL,
2011-05-09 13:42:48 -07:00
userChain);
}
2011-02-05 11:14:47 -08:00
XFCLOSE(file);
2014-09-08 21:34:07 -03:00
if (dynamic)
XFREE(myBuffer, heapHint, DYNAMIC_TYPE_FILE);
2011-02-05 11:14:47 -08:00
return ret;
}
/* loads file then loads each file in path, no c_rehash */
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_load_verify_locations(WOLFSSL_CTX* ctx, const char* file,
2011-09-23 11:20:13 -07:00
const char* path)
2011-02-05 11:14:47 -08:00
{
int ret = SSL_SUCCESS;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_load_verify_locations");
2011-04-27 17:31:08 -07:00
(void)path;
if (ctx == NULL || (file == NULL && path == NULL) )
2011-05-19 15:49:03 -07:00
return SSL_FAILURE;
if (file)
2012-05-16 17:04:56 -07:00
ret = ProcessFile(ctx, file, SSL_FILETYPE_PEM, CA_TYPE, NULL, 0, NULL);
if (ret == SSL_SUCCESS && path) {
/* try to load each regular file in path */
#ifdef USE_WINDOWS_API
WIN32_FIND_DATAA FindFileData;
HANDLE hFind;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
2014-09-15 20:32:00 -03:00
char* name = NULL;
#else
char name[MAX_FILENAME_SZ];
2014-09-15 20:32:00 -03:00
#endif
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
2014-09-15 20:32:00 -03:00
name = (char*)XMALLOC(MAX_FILENAME_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (name == NULL)
return MEMORY_E;
#endif
XMEMSET(name, 0, MAX_FILENAME_SZ);
XSTRNCPY(name, path, MAX_FILENAME_SZ - 4);
XSTRNCAT(name, "\\*", 3);
hFind = FindFirstFileA(name, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("FindFirstFile for path verify locations failed");
#ifdef WOLFSSL_SMALL_STACK
2014-09-15 20:32:00 -03:00
XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return BAD_PATH_ERROR;
}
do {
if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
XSTRNCPY(name, path, MAX_FILENAME_SZ/2 - 3);
XSTRNCAT(name, "\\", 2);
XSTRNCAT(name, FindFileData.cFileName, MAX_FILENAME_SZ/2);
ret = ProcessFile(ctx, name, SSL_FILETYPE_PEM, CA_TYPE,
NULL, 0, NULL);
}
} while (ret == SSL_SUCCESS && FindNextFileA(hFind, &FindFileData));
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
2014-09-15 20:32:00 -03:00
XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
FindClose(hFind);
2014-12-19 11:27:01 -07:00
#elif !defined(NO_WOLFSSL_DIR)
struct dirent* entry;
DIR* dir = opendir(path);
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
2014-09-15 20:32:00 -03:00
char* name = NULL;
#else
char name[MAX_FILENAME_SZ];
#endif
if (dir == NULL) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("opendir path verify locations failed");
return BAD_PATH_ERROR;
}
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
2014-09-15 20:32:00 -03:00
name = (char*)XMALLOC(MAX_FILENAME_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2015-07-21 11:10:28 -07:00
if (name == NULL) {
closedir(dir);
2014-09-15 20:32:00 -03:00
return MEMORY_E;
2015-07-21 11:10:28 -07:00
}
2014-09-15 20:32:00 -03:00
#endif
while ( ret == SSL_SUCCESS && (entry = readdir(dir)) != NULL) {
struct stat s;
XMEMSET(name, 0, MAX_FILENAME_SZ);
XSTRNCPY(name, path, MAX_FILENAME_SZ/2 - 2);
XSTRNCAT(name, "/", 1);
XSTRNCAT(name, entry->d_name, MAX_FILENAME_SZ/2);
if (stat(name, &s) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("stat on name failed");
ret = BAD_PATH_ERROR;
} else if (s.st_mode & S_IFREG)
ret = ProcessFile(ctx, name, SSL_FILETYPE_PEM, CA_TYPE,
NULL, 0, NULL);
}
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
2014-09-15 20:32:00 -03:00
XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
closedir(dir);
#endif
}
2012-05-29 12:04:48 -07:00
return ret;
}
2016-02-24 15:51:29 -07:00
#ifdef WOLFSSL_TRUST_PEER_CERT
/* Used to specify a peer cert to match when connecting
ctx : the ctx structure to load in peer cert
file: the string name of cert file
type: type of format such as PEM/DER
*/
int wolfSSL_CTX_trust_peer_cert(WOLFSSL_CTX* ctx, const char* file, int type)
{
WOLFSSL_ENTER("wolfSSL_CTX_trust_peer_cert");
if (ctx == NULL || file == NULL) {
return SSL_FAILURE;
}
return ProcessFile(ctx, file, type, TRUSTED_PEER_TYPE, NULL, 0, NULL);
2012-05-29 12:04:48 -07:00
}
2016-02-24 15:51:29 -07:00
#endif /* WOLFSSL_TRUST_PEER_CERT */
2012-05-29 12:04:48 -07:00
/* Verify the certificate, SSL_SUCCESS for ok, < 0 for error */
2014-12-19 11:27:01 -07:00
int wolfSSL_CertManagerVerify(WOLFSSL_CERT_MANAGER* cm, const char* fname,
2012-05-03 18:07:31 -07:00
int format)
{
2012-05-29 12:04:48 -07:00
int ret = SSL_FATAL_ERROR;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
byte staticBuffer[1]; /* force heap usage */
#else
2012-05-03 18:07:31 -07:00
byte staticBuffer[FILE_BUFFER_SIZE];
#endif
2012-05-03 18:07:31 -07:00
byte* myBuffer = staticBuffer;
int dynamic = 0;
long sz = 0;
XFILE file = XFOPEN(fname, "rb");
2012-05-03 18:07:31 -07:00
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertManagerVerify");
2012-05-29 12:04:48 -07:00
if (file == XBADFILE) return SSL_BAD_FILE;
2012-05-03 18:07:31 -07:00
XFSEEK(file, 0, XSEEK_END);
sz = XFTELL(file);
XREWIND(file);
2014-12-19 11:27:01 -07:00
if (sz > MAX_WOLFSSL_FILE_SIZE || sz < 0) {
WOLFSSL_MSG("CertManagerVerify file bad size");
2013-02-14 16:00:45 -08:00
XFCLOSE(file);
return SSL_BAD_FILE;
}
2012-05-03 18:07:31 -07:00
if (sz > (long)sizeof(staticBuffer)) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Getting dynamic buffer");
2012-05-03 18:07:31 -07:00
myBuffer = (byte*) XMALLOC(sz, cm->heap, DYNAMIC_TYPE_FILE);
if (myBuffer == NULL) {
XFCLOSE(file);
return SSL_BAD_FILE;
}
dynamic = 1;
}
if ( (ret = (int)XFREAD(myBuffer, sz, 1, file)) < 0)
2012-05-03 18:07:31 -07:00
ret = SSL_BAD_FILE;
else
2014-12-19 11:27:01 -07:00
ret = wolfSSL_CertManagerVerifyBuffer(cm, myBuffer, sz, format);
2012-05-03 18:07:31 -07:00
XFCLOSE(file);
2014-09-08 21:34:07 -03:00
if (dynamic)
XFREE(myBuffer, cm->heap, DYNAMIC_TYPE_FILE);
2012-05-03 18:07:31 -07:00
return ret;
}
/* like load verify locations, 1 for success, < 0 for error */
2014-12-19 11:27:01 -07:00
int wolfSSL_CertManagerLoadCA(WOLFSSL_CERT_MANAGER* cm, const char* file,
2012-05-03 18:07:31 -07:00
const char* path)
{
int ret = SSL_FATAL_ERROR;
2014-12-19 11:27:01 -07:00
WOLFSSL_CTX* tmp;
2012-05-03 18:07:31 -07:00
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertManagerLoadCA");
2012-05-03 18:07:31 -07:00
if (cm == NULL) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("No CertManager error");
2012-05-03 18:07:31 -07:00
return ret;
}
2014-12-19 11:27:01 -07:00
tmp = wolfSSL_CTX_new(cm_pick_method());
2012-05-03 18:07:31 -07:00
if (tmp == NULL) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("CTX new failed");
2012-05-03 18:07:31 -07:00
return ret;
}
/* for tmp use */
2014-12-19 11:27:01 -07:00
wolfSSL_CertManagerFree(tmp->cm);
2012-05-03 18:07:31 -07:00
tmp->cm = cm;
2014-12-19 11:27:01 -07:00
ret = wolfSSL_CTX_load_verify_locations(tmp, file, path);
2012-05-03 18:07:31 -07:00
/* don't loose our good one */
tmp->cm = NULL;
2014-12-19 11:27:01 -07:00
wolfSSL_CTX_free(tmp);
2012-05-03 18:07:31 -07:00
return ret;
}
2012-05-16 17:04:56 -07:00
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_check_private_key(WOLFSSL_CTX* ctx)
2012-07-17 15:55:20 -07:00
{
/* TODO: check private against public for RSA match */
(void)ctx;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_CTX_check_private_key");
2012-07-17 15:55:20 -07:00
return SSL_SUCCESS;
}
2012-05-16 17:04:56 -07:00
#ifdef HAVE_CRL
2012-05-18 11:03:44 -07:00
/* check CRL if enabled, SSL_SUCCESS */
2014-12-19 11:27:01 -07:00
int wolfSSL_CertManagerCheckCRL(WOLFSSL_CERT_MANAGER* cm, byte* der, int sz)
2012-05-18 11:03:44 -07:00
{
int ret = 0;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
DecodedCert* cert = NULL;
#else
DecodedCert cert[1];
#endif
2012-05-18 11:03:44 -07:00
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertManagerCheckCRL");
2012-05-18 11:03:44 -07:00
if (cm == NULL)
return BAD_FUNC_ARG;
if (cm->crlEnabled == 0)
return SSL_SUCCESS;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (cert == NULL)
return MEMORY_E;
#endif
InitDecodedCert(cert, der, sz, NULL);
2012-05-18 11:03:44 -07:00
if ((ret = ParseCertRelative(cert, CERT_TYPE, VERIFY_CRL, cm)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("ParseCert failed");
}
else if ((ret = CheckCertCRL(cm->crl, cert)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("CheckCertCRL failed");
}
2012-05-18 11:03:44 -07:00
FreeDecodedCert(cert);
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return ret == 0 ? SSL_SUCCESS : ret;
2012-05-18 11:03:44 -07:00
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CertManagerSetCRL_Cb(WOLFSSL_CERT_MANAGER* cm, CbMissingCRL cb)
2012-05-16 17:04:56 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertManagerSetCRL_Cb");
2012-05-16 17:04:56 -07:00
if (cm == NULL)
return BAD_FUNC_ARG;
cm->cbMissingCRL = cb;
return SSL_SUCCESS;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CertManagerLoadCRL(WOLFSSL_CERT_MANAGER* cm, const char* path,
int type, int monitor)
2012-05-16 17:04:56 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CertManagerLoadCRL");
2012-05-16 17:04:56 -07:00
if (cm == NULL)
return BAD_FUNC_ARG;
if (cm->crl == NULL) {
2014-12-19 11:27:01 -07:00
if (wolfSSL_CertManagerEnableCRL(cm, 0) != SSL_SUCCESS) {
WOLFSSL_MSG("Enable CRL failed");
return SSL_FATAL_ERROR;
2012-05-16 17:04:56 -07:00
}
}
return LoadCRL(cm->crl, path, type, monitor);
2012-05-16 17:04:56 -07:00
}
2014-12-19 11:27:01 -07:00
int wolfSSL_EnableCRL(WOLFSSL* ssl, int options)
2012-05-16 17:04:56 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_EnableCRL");
2012-05-16 17:04:56 -07:00
if (ssl)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerEnableCRL(ssl->ctx->cm, options);
2012-05-16 17:04:56 -07:00
else
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_DisableCRL(WOLFSSL* ssl)
2012-05-16 17:04:56 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_DisableCRL");
2012-05-16 17:04:56 -07:00
if (ssl)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerDisableCRL(ssl->ctx->cm);
2012-05-16 17:04:56 -07:00
else
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_LoadCRL(WOLFSSL* ssl, const char* path, int type, int monitor)
2012-05-16 17:04:56 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_LoadCRL");
2012-05-16 17:04:56 -07:00
if (ssl)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerLoadCRL(ssl->ctx->cm, path, type, monitor);
2012-05-16 17:04:56 -07:00
else
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_SetCRL_Cb(WOLFSSL* ssl, CbMissingCRL cb)
2012-05-16 17:35:51 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_SetCRL_Cb");
2012-05-16 17:35:51 -07:00
if (ssl)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerSetCRL_Cb(ssl->ctx->cm, cb);
2012-05-16 17:35:51 -07:00
else
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_EnableCRL(WOLFSSL_CTX* ctx, int options)
2012-05-16 17:04:56 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_EnableCRL");
2012-05-16 17:04:56 -07:00
if (ctx)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerEnableCRL(ctx->cm, options);
2012-05-16 17:04:56 -07:00
else
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_DisableCRL(WOLFSSL_CTX* ctx)
2012-05-16 17:04:56 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_DisableCRL");
2012-05-16 17:04:56 -07:00
if (ctx)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerDisableCRL(ctx->cm);
2012-05-16 17:04:56 -07:00
else
return BAD_FUNC_ARG;
}
int wolfSSL_CTX_LoadCRL(WOLFSSL_CTX* ctx, const char* path,
int type, int monitor)
2012-05-16 17:04:56 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_LoadCRL");
2012-05-16 17:04:56 -07:00
if (ctx)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerLoadCRL(ctx->cm, path, type, monitor);
2012-05-16 17:04:56 -07:00
else
return BAD_FUNC_ARG;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_SetCRL_Cb(WOLFSSL_CTX* ctx, CbMissingCRL cb)
2012-05-16 17:35:51 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_SetCRL_Cb");
2012-05-16 17:35:51 -07:00
if (ctx)
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerSetCRL_Cb(ctx->cm, cb);
2012-05-16 17:35:51 -07:00
else
return BAD_FUNC_ARG;
}
2012-05-16 17:04:56 -07:00
#endif /* HAVE_CRL */
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_DER_LOAD
2011-02-05 11:14:47 -08:00
/* Add format parameter to allow DER load of CA files */
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_der_load_verify_locations(WOLFSSL_CTX* ctx, const char* file,
int format)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_der_load_verify_locations");
if (ctx == NULL || file == NULL)
return SSL_FAILURE;
2012-05-16 17:04:56 -07:00
if (ProcessFile(ctx, file, format, CA_TYPE, NULL, 0, NULL) == SSL_SUCCESS)
2011-02-05 11:14:47 -08:00
return SSL_SUCCESS;
return SSL_FAILURE;
}
2014-12-19 11:27:01 -07:00
#endif /* WOLFSSL_DER_LOAD */
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_CERT_GEN
2011-02-05 11:14:47 -08:00
/* load pem cert from file into der buffer, return der size or error */
2014-12-19 11:27:01 -07:00
int wolfSSL_PemCertToDer(const char* fileName, unsigned char* derBuf, int derSz)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
EncryptedInfo* info = NULL;
byte staticBuffer[1]; /* force XMALLOC */
#else
EncryptedInfo info[1];
2011-02-05 11:14:47 -08:00
byte staticBuffer[FILE_BUFFER_SIZE];
#endif
2011-02-05 11:14:47 -08:00
byte* fileBuf = staticBuffer;
int dynamic = 0;
int ret = 0;
int ecc = 0;
long sz = 0;
XFILE file = XFOPEN(fileName, "rb");
DerBuffer* converted = NULL;
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_PemCertToDer");
2011-02-05 11:14:47 -08:00
if (file == XBADFILE) {
ret = SSL_BAD_FILE;
}
else {
XFSEEK(file, 0, XSEEK_END);
sz = XFTELL(file);
XREWIND(file);
2011-02-05 11:14:47 -08:00
if (sz < 0) {
ret = SSL_BAD_FILE;
}
else if (sz > (long)sizeof(staticBuffer)) {
#ifdef WOLFSSL_STATIC_MEMORY
WOLFSSL_MSG("File was larger then static buffer");
return MEMORY_E;
#endif
fileBuf = (byte*)XMALLOC(sz, 0, DYNAMIC_TYPE_FILE);
if (fileBuf == NULL)
ret = MEMORY_E;
else
dynamic = 1;
2011-02-05 11:14:47 -08:00
}
if (ret == 0) {
if ( (ret = (int)XFREAD(fileBuf, sz, 1, file)) < 0) {
ret = SSL_BAD_FILE;
}
else {
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (info == NULL)
ret = MEMORY_E;
else
#endif
{
ret = PemToDer(fileBuf, sz, CA_TYPE, &converted,
0, info, &ecc);
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
}
}
if (ret == 0) {
if (converted->length < (word32)derSz) {
XMEMCPY(derBuf, converted->buffer, converted->length);
ret = converted->length;
}
else
ret = BUFFER_E;
}
2011-02-05 11:14:47 -08:00
FreeDer(&converted);
2011-02-05 11:14:47 -08:00
}
XFCLOSE(file);
if (dynamic)
XFREE(fileBuf, 0, DYNAMIC_TYPE_FILE);
}
2011-02-05 11:14:47 -08:00
return ret;
}
2014-12-19 11:27:01 -07:00
#endif /* WOLFSSL_CERT_GEN */
2011-02-05 11:14:47 -08:00
#ifdef WOLFSSL_CERT_EXT
#ifndef NO_FILESYSTEM
/* load pem public key from file into der buffer, return der size or error */
int wolfSSL_PemPubKeyToDer(const char* fileName,
unsigned char* derBuf, int derSz)
{
#ifdef WOLFSSL_SMALL_STACK
byte staticBuffer[1]; /* force XMALLOC */
#else
byte staticBuffer[FILE_BUFFER_SIZE];
#endif
byte* fileBuf = staticBuffer;
int dynamic = 0;
int ret = 0;
long sz = 0;
XFILE file = XFOPEN(fileName, "rb");
DerBuffer* converted = NULL;
WOLFSSL_ENTER("wolfSSL_PemPubKeyToDer");
if (file == XBADFILE) {
ret = SSL_BAD_FILE;
}
else {
XFSEEK(file, 0, XSEEK_END);
sz = XFTELL(file);
XREWIND(file);
if (sz < 0) {
ret = SSL_BAD_FILE;
}
else if (sz > (long)sizeof(staticBuffer)) {
#ifdef WOLFSSL_STATIC_MEMORY
WOLFSSL_MSG("File was larger then static buffer");
return MEMORY_E;
#endif
fileBuf = (byte*)XMALLOC(sz, 0, DYNAMIC_TYPE_FILE);
if (fileBuf == NULL)
ret = MEMORY_E;
else
dynamic = 1;
}
if (ret == 0) {
if ( (ret = (int)XFREAD(fileBuf, sz, 1, file)) < 0)
ret = SSL_BAD_FILE;
else
ret = PemToDer(fileBuf, sz, PUBLICKEY_TYPE, &converted,
0, NULL, NULL);
if (ret == 0) {
if (converted->length < (word32)derSz) {
XMEMCPY(derBuf, converted->buffer, converted->length);
ret = converted->length;
}
else
ret = BUFFER_E;
}
FreeDer(&converted);
}
XFCLOSE(file);
if (dynamic)
XFREE(fileBuf, 0, DYNAMIC_TYPE_FILE);
}
return ret;
}
#endif /* NO_FILESYSTEM */
/* Return bytes written to buff or < 0 for error */
int wolfSSL_PubKeyPemToDer(const unsigned char* pem, int pemSz,
unsigned char* buff, int buffSz)
{
int ret;
DerBuffer* der = NULL;
WOLFSSL_ENTER("wolfSSL_PubKeyPemToDer");
if (pem == NULL || buff == NULL || buffSz <= 0) {
WOLFSSL_MSG("Bad pem der args");
return BAD_FUNC_ARG;
}
ret = PemToDer(pem, pemSz, PUBLICKEY_TYPE, &der, NULL, NULL, NULL);
if (ret < 0) {
WOLFSSL_MSG("Bad Pem To Der");
}
else {
if (der->length <= (word32)buffSz) {
XMEMCPY(buff, der->buffer, der->length);
ret = der->length;
}
else {
WOLFSSL_MSG("Bad der length");
ret = BAD_FUNC_ARG;
}
}
FreeDer(&der);
return ret;
}
#endif /* WOLFSSL_CERT_EXT */
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_use_certificate_file(WOLFSSL_CTX* ctx, const char* file,
int format)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_file");
2012-05-16 17:04:56 -07:00
if (ProcessFile(ctx, file, format, CERT_TYPE, NULL, 0, NULL) == SSL_SUCCESS)
2011-02-05 11:14:47 -08:00
return SSL_SUCCESS;
return SSL_FAILURE;
}
int wolfSSL_CTX_use_PrivateKey_file(WOLFSSL_CTX* ctx, const char* file,
int format)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_file");
2012-05-16 17:04:56 -07:00
if (ProcessFile(ctx, file, format, PRIVATEKEY_TYPE, NULL, 0, NULL)
== SSL_SUCCESS)
2011-02-05 11:14:47 -08:00
return SSL_SUCCESS;
return SSL_FAILURE;
}
2015-02-26 10:40:32 -07:00
/* get cert chaining depth using ssl struct */
long wolfSSL_get_verify_depth(WOLFSSL* ssl)
{
if(ssl == NULL) {
return BAD_FUNC_ARG;
}
return MAX_CHAIN_DEPTH;
}
/* get cert chaining depth using ctx struct */
long wolfSSL_CTX_get_verify_depth(WOLFSSL_CTX* ctx)
{
if(ctx == NULL) {
return BAD_FUNC_ARG;
}
return MAX_CHAIN_DEPTH;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_use_certificate_chain_file(WOLFSSL_CTX* ctx, const char* file)
2011-02-05 11:14:47 -08:00
{
/* process up to MAX_CHAIN_DEPTH plus subject cert */
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_chain_file");
2012-05-16 17:04:56 -07:00
if (ProcessFile(ctx, file, SSL_FILETYPE_PEM,CERT_TYPE,NULL,1, NULL)
== SSL_SUCCESS)
2011-02-05 11:14:47 -08:00
return SSL_SUCCESS;
return SSL_FAILURE;
}
2014-07-10 11:18:49 -06:00
#ifndef NO_DH
2011-10-31 17:33:40 -07:00
/* server Diffie-Hellman parameters */
2014-12-19 11:27:01 -07:00
static int wolfSSL_SetTmpDH_file_wrapper(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
const char* fname, int format)
2011-10-31 17:33:40 -07:00
{
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
byte staticBuffer[1]; /* force heap usage */
#else
2011-10-31 17:33:40 -07:00
byte staticBuffer[FILE_BUFFER_SIZE];
#endif
2011-10-31 17:33:40 -07:00
byte* myBuffer = staticBuffer;
int dynamic = 0;
int ret;
long sz = 0;
XFILE file;
2011-10-31 17:33:40 -07:00
if (ctx == NULL || fname == NULL)
return BAD_FUNC_ARG;
file = XFOPEN(fname, "rb");
if (file == XBADFILE) return SSL_BAD_FILE;
2011-10-31 17:33:40 -07:00
XFSEEK(file, 0, XSEEK_END);
sz = XFTELL(file);
XREWIND(file);
if (sz > (long)sizeof(staticBuffer)) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Getting dynamic buffer");
2011-10-31 17:33:40 -07:00
myBuffer = (byte*) XMALLOC(sz, ctx->heap, DYNAMIC_TYPE_FILE);
if (myBuffer == NULL) {
XFCLOSE(file);
return SSL_BAD_FILE;
}
dynamic = 1;
}
2013-02-14 14:09:41 -08:00
else if (sz < 0) {
XFCLOSE(file);
return SSL_BAD_FILE;
}
2011-10-31 17:33:40 -07:00
if ( (ret = (int)XFREAD(myBuffer, sz, 1, file)) < 0)
2011-10-31 17:33:40 -07:00
ret = SSL_BAD_FILE;
else {
if (ssl)
2014-12-19 11:27:01 -07:00
ret = wolfSSL_SetTmpDH_buffer(ssl, myBuffer, sz, format);
else
2014-12-19 11:27:01 -07:00
ret = wolfSSL_CTX_SetTmpDH_buffer(ctx, myBuffer, sz, format);
}
2011-10-31 17:33:40 -07:00
XFCLOSE(file);
2014-09-08 21:34:07 -03:00
if (dynamic)
XFREE(myBuffer, ctx->heap, DYNAMIC_TYPE_FILE);
2011-10-31 17:33:40 -07:00
return ret;
}
2011-10-31 17:33:40 -07:00
/* server Diffie-Hellman parameters */
2014-12-19 11:27:01 -07:00
int wolfSSL_SetTmpDH_file(WOLFSSL* ssl, const char* fname, int format)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
2014-12-19 11:27:01 -07:00
return wolfSSL_SetTmpDH_file_wrapper(ssl->ctx, ssl, fname, format);
2011-10-31 17:33:40 -07:00
}
/* server Diffie-Hellman parameters */
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_SetTmpDH_file(WOLFSSL_CTX* ctx, const char* fname, int format)
{
2014-12-19 11:27:01 -07:00
return wolfSSL_SetTmpDH_file_wrapper(ctx, NULL, fname, format);
}
2014-07-10 11:18:49 -06:00
#endif /* NO_DH */
#ifdef OPENSSL_EXTRA
/* put SSL type in extra for now, not very common */
2014-12-19 11:27:01 -07:00
int wolfSSL_use_certificate_file(WOLFSSL* ssl, const char* file, int format)
2014-07-10 11:18:49 -06:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_use_certificate_file");
if (ProcessFile(ssl->ctx, file, format, CERT_TYPE,
ssl, 0, NULL) == SSL_SUCCESS)
2014-07-10 11:18:49 -06:00
return SSL_SUCCESS;
return SSL_FAILURE;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_use_PrivateKey_file(WOLFSSL* ssl, const char* file, int format)
2014-07-10 11:18:49 -06:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_use_PrivateKey_file");
if (ProcessFile(ssl->ctx, file, format, PRIVATEKEY_TYPE,
ssl, 0, NULL) == SSL_SUCCESS)
2014-07-10 11:18:49 -06:00
return SSL_SUCCESS;
return SSL_FAILURE;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_use_certificate_chain_file(WOLFSSL* ssl, const char* file)
2014-07-10 11:18:49 -06:00
{
/* process up to MAX_CHAIN_DEPTH plus subject cert */
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_use_certificate_chain_file");
if (ProcessFile(ssl->ctx, file, SSL_FILETYPE_PEM, CERT_TYPE,
ssl, 1, NULL) == SSL_SUCCESS)
2014-07-10 11:18:49 -06:00
return SSL_SUCCESS;
return SSL_FAILURE;
}
#if !defined(NO_WOLFSSL_CLIENT)
/* Return the amount of random bytes copied over or error case.
* ssl : ssl struct after handshake
* out : buffer to hold random bytes
* outSz : either 0 (return max buffer sz) or size of out buffer
*
* NOTE: wolfSSL_KeepArrays(ssl) must be called to retain handshake information.
*/
int wolfSSL_get_client_random(WOLFSSL* ssl, unsigned char* out, int outSz)
{
int size;
/* return max size of buffer */
if (outSz == 0) {
return RAN_LEN;
}
if (ssl == NULL || out == NULL || outSz < 0) {
return BAD_FUNC_ARG;
}
if (ssl->options.saveArrays == 0 || ssl->arrays == NULL) {
WOLFSSL_MSG("Arrays struct not saved after handshake");
return BAD_FUNC_ARG;
}
if (outSz > RAN_LEN) {
size = RAN_LEN;
}
else {
size = outSz;
}
XMEMCPY(out, ssl->arrays->clientRandom, size);
return size;
}
#endif /* !defined(NO_WOLFSSL_CLIENT) */
2014-07-10 11:18:49 -06:00
#ifdef HAVE_ECC
/* Set Temp CTX EC-DHE size in octets, should be 20 - 66 for 160 - 521 bit */
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_SetTmpEC_DHE_Sz(WOLFSSL_CTX* ctx, word16 sz)
2014-07-10 11:18:49 -06:00
{
if (ctx == NULL || sz < ECC_MINSIZE || sz > ECC_MAXSIZE)
return BAD_FUNC_ARG;
ctx->eccTempKeySz = sz;
return SSL_SUCCESS;
}
/* Set Temp SSL EC-DHE size in octets, should be 20 - 66 for 160 - 521 bit */
2014-12-19 11:27:01 -07:00
int wolfSSL_SetTmpEC_DHE_Sz(WOLFSSL* ssl, word16 sz)
2014-07-10 11:18:49 -06:00
{
if (ssl == NULL || sz < ECC_MINSIZE || sz > ECC_MAXSIZE)
return BAD_FUNC_ARG;
ssl->eccTempKeySz = sz;
return SSL_SUCCESS;
}
#endif /* HAVE_ECC */
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_use_RSAPrivateKey_file(WOLFSSL_CTX* ctx,const char* file,
int format)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_CTX_use_RSAPrivateKey_file");
2014-12-19 11:27:01 -07:00
return wolfSSL_CTX_use_PrivateKey_file(ctx, file, format);
}
2014-12-19 11:27:01 -07:00
int wolfSSL_use_RSAPrivateKey_file(WOLFSSL* ssl, const char* file, int format)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_use_RSAPrivateKey_file");
2014-12-19 11:27:01 -07:00
return wolfSSL_use_PrivateKey_file(ssl, file, format);
}
2011-10-31 17:33:40 -07:00
#endif /* OPENSSL_EXTRA */
2011-02-05 11:14:47 -08:00
#ifdef HAVE_NTRU
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_use_NTRUPrivateKey_file(WOLFSSL_CTX* ctx, const char* file)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_use_NTRUPrivateKey_file");
if (ctx == NULL)
return SSL_FAILURE;
2012-05-16 17:04:56 -07:00
if (ProcessFile(ctx, file, SSL_FILETYPE_RAW, PRIVATEKEY_TYPE, NULL, 0, NULL)
2011-02-05 11:14:47 -08:00
== SSL_SUCCESS) {
ctx->haveNTRU = 1;
return SSL_SUCCESS;
}
return SSL_FAILURE;
}
#endif /* HAVE_NTRU */
#endif /* NO_FILESYSTEM */
2014-12-19 11:27:01 -07:00
void wolfSSL_CTX_set_verify(WOLFSSL_CTX* ctx, int mode, VerifyCallback vc)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_set_verify");
2011-02-05 11:14:47 -08:00
if (mode & SSL_VERIFY_PEER) {
ctx->verifyPeer = 1;
ctx->verifyNone = 0; /* in case previously set */
2011-02-05 11:14:47 -08:00
}
if (mode == SSL_VERIFY_NONE) {
ctx->verifyNone = 1;
ctx->verifyPeer = 0; /* in case previously set */
}
if (mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
ctx->failNoCert = 1;
if (mode & SSL_VERIFY_FAIL_EXCEPT_PSK) {
ctx->failNoCert = 0; /* fail on all is set to fail on PSK */
ctx->failNoCertxPSK = 1;
}
2011-02-05 11:14:47 -08:00
ctx->verifyCallback = vc;
}
2014-12-19 11:27:01 -07:00
void wolfSSL_set_verify(WOLFSSL* ssl, int mode, VerifyCallback vc)
2011-10-28 18:43:07 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_set_verify");
2011-10-28 18:43:07 -07:00
if (mode & SSL_VERIFY_PEER) {
ssl->options.verifyPeer = 1;
ssl->options.verifyNone = 0; /* in case previously set */
2011-10-28 18:43:07 -07:00
}
if (mode == SSL_VERIFY_NONE) {
ssl->options.verifyNone = 1;
ssl->options.verifyPeer = 0; /* in case previously set */
}
if (mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
ssl->options.failNoCert = 1;
if (mode & SSL_VERIFY_FAIL_EXCEPT_PSK) {
ssl->options.failNoCert = 0; /* fail on all is set to fail on PSK */
ssl->options.failNoCertxPSK = 1;
}
2011-10-28 18:43:07 -07:00
ssl->verifyCallback = vc;
}
/* store user ctx for verify callback */
2014-12-19 11:27:01 -07:00
void wolfSSL_SetCertCbCtx(WOLFSSL* ssl, void* ctx)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_SetCertCbCtx");
if (ssl)
ssl->verifyCbCtx = ctx;
}
2012-01-26 12:43:48 -08:00
/* store context CA Cache addition callback */
2014-12-19 11:27:01 -07:00
void wolfSSL_CTX_SetCACb(WOLFSSL_CTX* ctx, CallbackCACache cb)
2012-01-26 12:43:48 -08:00
{
2012-05-03 18:07:31 -07:00
if (ctx && ctx->cm)
ctx->cm->caCacheCallback = cb;
2012-01-26 12:43:48 -08:00
}
2013-05-02 11:34:26 -07:00
#if defined(PERSIST_CERT_CACHE)
#if !defined(NO_FILESYSTEM)
/* Persist cert cache to file */
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_save_cert_cache(WOLFSSL_CTX* ctx, const char* fname)
2013-05-02 11:34:26 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_save_cert_cache");
2013-05-02 11:34:26 -07:00
if (ctx == NULL || fname == NULL)
return BAD_FUNC_ARG;
return CM_SaveCertCache(ctx->cm, fname);
}
/* Persist cert cache from file */
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_restore_cert_cache(WOLFSSL_CTX* ctx, const char* fname)
2013-05-02 11:34:26 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_restore_cert_cache");
2013-05-02 11:34:26 -07:00
if (ctx == NULL || fname == NULL)
return BAD_FUNC_ARG;
return CM_RestoreCertCache(ctx->cm, fname);
}
#endif /* NO_FILESYSTEM */
/* Persist cert cache to memory */
int wolfSSL_CTX_memsave_cert_cache(WOLFSSL_CTX* ctx, void* mem,
int sz, int* used)
2013-05-02 11:34:26 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_memsave_cert_cache");
2013-05-02 11:34:26 -07:00
if (ctx == NULL || mem == NULL || used == NULL || sz <= 0)
return BAD_FUNC_ARG;
return CM_MemSaveCertCache(ctx->cm, mem, sz, used);
}
2013-05-10 17:31:50 -06:00
/* Restore cert cache from memory */
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_memrestore_cert_cache(WOLFSSL_CTX* ctx, const void* mem, int sz)
2013-05-02 11:34:26 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_memrestore_cert_cache");
2013-05-02 11:34:26 -07:00
if (ctx == NULL || mem == NULL || sz <= 0)
return BAD_FUNC_ARG;
return CM_MemRestoreCertCache(ctx->cm, mem, sz);
}
/* get how big the the cert cache save buffer needs to be */
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_get_cert_cache_memsize(WOLFSSL_CTX* ctx)
2013-05-02 11:34:26 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_get_cert_cache_memsize");
2013-05-02 11:34:26 -07:00
if (ctx == NULL)
return BAD_FUNC_ARG;
return CM_GetCertCacheMemSize(ctx->cm);
}
#endif /* PERSISTE_CERT_CACHE */
#endif /* !NO_CERTS */
2012-01-26 12:43:48 -08:00
2011-02-05 11:14:47 -08:00
#ifndef NO_SESSION_CACHE
2014-12-19 11:27:01 -07:00
WOLFSSL_SESSION* wolfSSL_get_session(WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_get_session");
if (ssl)
return GetSession(ssl, 0, 0);
return NULL;
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
int wolfSSL_set_session(WOLFSSL* ssl, WOLFSSL_SESSION* session)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_set_session");
2011-02-05 11:14:47 -08:00
if (session)
return SetSession(ssl, session);
return SSL_FAILURE;
}
#ifndef NO_CLIENT_CACHE
/* Associate client session with serverID, find existing or store for saving
if newSession flag on, don't reuse existing session
SSL_SUCCESS on ok */
2014-12-19 11:27:01 -07:00
int wolfSSL_SetServerID(WOLFSSL* ssl, const byte* id, int len, int newSession)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_SESSION* session = NULL;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_SetServerID");
if (ssl == NULL || id == NULL || len <= 0)
return BAD_FUNC_ARG;
if (newSession == 0) {
session = GetSessionClient(ssl, id, len);
if (session) {
if (SetSession(ssl, session) != SSL_SUCCESS) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("SetSession failed");
session = NULL;
}
}
}
if (session == NULL) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Valid ServerID not cached already");
2013-06-17 12:26:21 -07:00
ssl->session.idLen = (word16)min(SERVER_ID_LEN, (word32)len);
XMEMCPY(ssl->session.serverID, id, ssl->session.idLen);
}
return SSL_SUCCESS;
}
2013-04-29 14:52:28 -07:00
#endif /* NO_CLIENT_CACHE */
#if defined(PERSIST_SESSION_CACHE)
2013-04-24 11:10:23 -07:00
/* for persistence, if changes to layout need to increment and modify
2014-03-02 10:59:03 -08:00
save_session_cache() and restore_session_cache and memory versions too */
2014-12-19 11:27:01 -07:00
#define WOLFSSL_CACHE_VERSION 2
2014-03-02 10:59:03 -08:00
2013-04-24 11:10:23 -07:00
/* Session Cache Header information */
typedef struct {
int version; /* cache layout version id */
2013-04-24 11:10:23 -07:00
int rows; /* session rows */
int columns; /* session columns */
2014-12-19 11:27:01 -07:00
int sessionSz; /* sizeof WOLFSSL_SESSION */
2013-04-24 11:10:23 -07:00
} cache_header_t;
/* current persistence layout is:
1) cache_header_t
2) SessionCache
3) ClientCache
2014-12-19 11:27:01 -07:00
update WOLFSSL_CACHE_VERSION if change layout for the following
PERSISTENT_SESSION_CACHE functions
*/
/* get how big the the session cache save buffer needs to be */
2014-12-19 11:27:01 -07:00
int wolfSSL_get_session_cache_memsize(void)
{
int sz = (int)(sizeof(SessionCache) + sizeof(cache_header_t));
#ifndef NO_CLIENT_CACHE
sz += (int)(sizeof(ClientCache));
#endif
return sz;
}
/* Persist session cache to memory */
2014-12-19 11:27:01 -07:00
int wolfSSL_memsave_session_cache(void* mem, int sz)
{
int i;
cache_header_t cache_header;
SessionRow* row = (SessionRow*)((byte*)mem + sizeof(cache_header));
#ifndef NO_CLIENT_CACHE
ClientRow* clRow;
#endif
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_memsave_session_cache");
2014-12-19 11:27:01 -07:00
if (sz < wolfSSL_get_session_cache_memsize()) {
WOLFSSL_MSG("Memory buffer too small");
return BUFFER_E;
}
2014-12-19 11:27:01 -07:00
cache_header.version = WOLFSSL_CACHE_VERSION;
cache_header.rows = SESSION_ROWS;
cache_header.columns = SESSIONS_PER_ROW;
2014-12-19 11:27:01 -07:00
cache_header.sessionSz = (int)sizeof(WOLFSSL_SESSION);
XMEMCPY(mem, &cache_header, sizeof(cache_header));
if (wc_LockMutex(&session_mutex) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Session cache mutex lock failed");
return BAD_MUTEX_E;
}
for (i = 0; i < cache_header.rows; ++i)
XMEMCPY(row++, SessionCache + i, sizeof(SessionRow));
#ifndef NO_CLIENT_CACHE
clRow = (ClientRow*)row;
for (i = 0; i < cache_header.rows; ++i)
XMEMCPY(clRow++, ClientCache + i, sizeof(ClientRow));
#endif
wc_UnLockMutex(&session_mutex);
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("wolfSSL_memsave_session_cache", SSL_SUCCESS);
return SSL_SUCCESS;
}
/* Restore the persistent session cache from memory */
2014-12-19 11:27:01 -07:00
int wolfSSL_memrestore_session_cache(const void* mem, int sz)
{
int i;
cache_header_t cache_header;
SessionRow* row = (SessionRow*)((byte*)mem + sizeof(cache_header));
#ifndef NO_CLIENT_CACHE
ClientRow* clRow;
#endif
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_memrestore_session_cache");
2014-12-19 11:27:01 -07:00
if (sz < wolfSSL_get_session_cache_memsize()) {
WOLFSSL_MSG("Memory buffer too small");
return BUFFER_E;
}
XMEMCPY(&cache_header, mem, sizeof(cache_header));
2014-12-19 11:27:01 -07:00
if (cache_header.version != WOLFSSL_CACHE_VERSION ||
cache_header.rows != SESSION_ROWS ||
cache_header.columns != SESSIONS_PER_ROW ||
2014-12-19 11:27:01 -07:00
cache_header.sessionSz != (int)sizeof(WOLFSSL_SESSION)) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Session cache header match failed");
return CACHE_MATCH_ERROR;
}
if (wc_LockMutex(&session_mutex) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Session cache mutex lock failed");
return BAD_MUTEX_E;
}
for (i = 0; i < cache_header.rows; ++i)
XMEMCPY(SessionCache + i, row++, sizeof(SessionRow));
#ifndef NO_CLIENT_CACHE
clRow = (ClientRow*)row;
for (i = 0; i < cache_header.rows; ++i)
XMEMCPY(ClientCache + i, clRow++, sizeof(ClientRow));
#endif
wc_UnLockMutex(&session_mutex);
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("wolfSSL_memrestore_session_cache", SSL_SUCCESS);
return SSL_SUCCESS;
}
#if !defined(NO_FILESYSTEM)
2013-04-24 11:10:23 -07:00
/* Persist session cache to file */
2013-05-10 17:31:50 -06:00
/* doesn't use memsave because of additional memory use */
2014-12-19 11:27:01 -07:00
int wolfSSL_save_session_cache(const char *fname)
2013-04-24 11:10:23 -07:00
{
XFILE file;
2013-04-24 11:10:23 -07:00
int ret;
int rc = SSL_SUCCESS;
int i;
cache_header_t cache_header;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_save_session_cache");
2013-04-24 11:10:23 -07:00
file = XFOPEN(fname, "w+b");
2013-04-24 11:10:23 -07:00
if (file == XBADFILE) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Couldn't open session cache save file");
2013-04-24 11:10:23 -07:00
return SSL_BAD_FILE;
}
2014-12-19 11:27:01 -07:00
cache_header.version = WOLFSSL_CACHE_VERSION;
2013-04-24 11:10:23 -07:00
cache_header.rows = SESSION_ROWS;
cache_header.columns = SESSIONS_PER_ROW;
2014-12-19 11:27:01 -07:00
cache_header.sessionSz = (int)sizeof(WOLFSSL_SESSION);
2013-04-24 11:10:23 -07:00
/* cache header */
2013-04-24 11:10:23 -07:00
ret = (int)XFWRITE(&cache_header, sizeof cache_header, 1, file);
if (ret != 1) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Session cache header file write failed");
2013-04-24 11:10:23 -07:00
XFCLOSE(file);
return FWRITE_ERROR;
}
if (wc_LockMutex(&session_mutex) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Session cache mutex lock failed");
2013-04-24 11:10:23 -07:00
XFCLOSE(file);
return BAD_MUTEX_E;
2013-04-24 11:10:23 -07:00
}
/* session cache */
2013-04-24 11:10:23 -07:00
for (i = 0; i < cache_header.rows; ++i) {
ret = (int)XFWRITE(SessionCache + i, sizeof(SessionRow), 1, file);
if (ret != 1) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Session cache member file write failed");
2013-04-24 11:10:23 -07:00
rc = FWRITE_ERROR;
break;
}
}
#ifndef NO_CLIENT_CACHE
/* client cache */
for (i = 0; i < cache_header.rows; ++i) {
ret = (int)XFWRITE(ClientCache + i, sizeof(ClientRow), 1, file);
if (ret != 1) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Client cache member file write failed");
rc = FWRITE_ERROR;
break;
}
}
#endif /* NO_CLIENT_CACHE */
wc_UnLockMutex(&session_mutex);
2013-04-24 11:10:23 -07:00
XFCLOSE(file);
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("wolfSSL_save_session_cache", rc);
2013-04-24 11:10:23 -07:00
return rc;
}
/* Restore the persistent session cache from file */
2013-05-10 17:31:50 -06:00
/* doesn't use memstore because of additional memory use */
2014-12-19 11:27:01 -07:00
int wolfSSL_restore_session_cache(const char *fname)
2013-04-24 11:10:23 -07:00
{
XFILE file;
int rc = SSL_SUCCESS;
int ret;
2013-04-24 11:10:23 -07:00
int i;
cache_header_t cache_header;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_restore_session_cache");
2013-04-24 11:10:23 -07:00
file = XFOPEN(fname, "rb");
2013-04-24 11:10:23 -07:00
if (file == XBADFILE) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Couldn't open session cache save file");
2013-04-24 11:10:23 -07:00
return SSL_BAD_FILE;
}
/* cache header */
2013-04-24 11:10:23 -07:00
ret = (int)XFREAD(&cache_header, sizeof cache_header, 1, file);
if (ret != 1) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Session cache header file read failed");
XFCLOSE(file);
return FREAD_ERROR;
}
2014-12-19 11:27:01 -07:00
if (cache_header.version != WOLFSSL_CACHE_VERSION ||
cache_header.rows != SESSION_ROWS ||
cache_header.columns != SESSIONS_PER_ROW ||
2014-12-19 11:27:01 -07:00
cache_header.sessionSz != (int)sizeof(WOLFSSL_SESSION)) {
2013-04-24 11:10:23 -07:00
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Session cache header match failed");
2013-04-24 11:10:23 -07:00
XFCLOSE(file);
return CACHE_MATCH_ERROR;
2013-04-24 11:10:23 -07:00
}
if (wc_LockMutex(&session_mutex) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Session cache mutex lock failed");
2013-04-24 11:10:23 -07:00
XFCLOSE(file);
return BAD_MUTEX_E;
2013-04-24 11:10:23 -07:00
}
/* session cache */
2013-04-24 11:10:23 -07:00
for (i = 0; i < cache_header.rows; ++i) {
ret = (int)XFREAD(SessionCache + i, sizeof(SessionRow), 1, file);
if (ret != 1) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Session cache member file read failed");
2013-04-24 11:20:54 -07:00
XMEMSET(SessionCache, 0, sizeof SessionCache);
2013-04-24 11:10:23 -07:00
rc = FREAD_ERROR;
break;
}
}
#ifndef NO_CLIENT_CACHE
/* client cache */
for (i = 0; i < cache_header.rows; ++i) {
ret = (int)XFREAD(ClientCache + i, sizeof(ClientRow), 1, file);
if (ret != 1) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Client cache member file read failed");
XMEMSET(ClientCache, 0, sizeof ClientCache);
rc = FREAD_ERROR;
break;
}
}
#endif /* NO_CLIENT_CACHE */
wc_UnLockMutex(&session_mutex);
2013-04-24 11:10:23 -07:00
XFCLOSE(file);
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("wolfSSL_restore_session_cache", rc);
2013-04-24 11:10:23 -07:00
return rc;
}
#endif /* !NO_FILESYSTEM */
#endif /* PERSIST_SESSION_CACHE */
2011-02-05 11:14:47 -08:00
#endif /* NO_SESSION_CACHE */
2014-12-19 11:27:01 -07:00
void wolfSSL_load_error_strings(void) /* compatibility only */
2011-02-05 11:14:47 -08:00
{}
2014-12-19 11:27:01 -07:00
int wolfSSL_library_init(void)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_library_init");
if (wolfSSL_Init() == SSL_SUCCESS)
2011-02-05 11:14:47 -08:00
return SSL_SUCCESS;
else
return SSL_FATAL_ERROR;
2011-02-05 11:14:47 -08:00
}
#ifdef HAVE_SECRET_CALLBACK
2014-12-19 11:27:01 -07:00
int wolfSSL_set_session_secret_cb(WOLFSSL* ssl, SessionSecretCb cb, void* ctx)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_set_session_secret_cb");
if (ssl == NULL)
return SSL_FATAL_ERROR;
ssl->sessionSecretCb = cb;
ssl->sessionSecretCtx = ctx;
/* If using a pre-set key, assume session resumption. */
ssl->session.sessionIDSz = 0;
ssl->options.resuming = 1;
return SSL_SUCCESS;
}
#endif
2011-02-05 11:14:47 -08:00
#ifndef NO_SESSION_CACHE
/* on by default if built in but allow user to turn off */
2014-12-19 11:27:01 -07:00
long wolfSSL_CTX_set_session_cache_mode(WOLFSSL_CTX* ctx, long mode)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_CTX_set_session_cache_mode");
2011-02-05 11:14:47 -08:00
if (mode == SSL_SESS_CACHE_OFF)
ctx->sessionCacheOff = 1;
if (mode == SSL_SESS_CACHE_NO_AUTO_CLEAR)
ctx->sessionCacheFlushOff = 1;
return SSL_SUCCESS;
}
#endif /* NO_SESSION_CACHE */
2013-05-02 11:34:26 -07:00
#if !defined(NO_CERTS)
#if defined(PERSIST_CERT_CACHE)
2014-12-19 11:27:01 -07:00
#define WOLFSSL_CACHE_CERT_VERSION 1
2013-05-02 11:34:26 -07:00
typedef struct {
int version; /* cache cert layout version id */
int rows; /* hash table rows, CA_TABLE_SIZE */
int columns[CA_TABLE_SIZE]; /* columns per row on list */
int signerSz; /* sizeof Signer object */
} CertCacheHeader;
/* current cert persistence layout is:
2013-05-02 11:34:26 -07:00
1) CertCacheHeader
2) caTable
2014-12-19 11:27:01 -07:00
update WOLFSSL_CERT_CACHE_VERSION if change layout for the following
2013-05-02 11:34:26 -07:00
PERSIST_CERT_CACHE functions
*/
/* Return memory needed to persist this signer, have lock */
static INLINE int GetSignerMemory(Signer* signer)
{
int sz = sizeof(signer->pubKeySize) + sizeof(signer->keyOID)
+ sizeof(signer->nameLen) + sizeof(signer->subjectNameHash);
#if !defined(NO_SKID)
sz += (int)sizeof(signer->subjectKeyIdHash);
2013-05-02 11:34:26 -07:00
#endif
/* add dynamic bytes needed */
sz += signer->pubKeySize;
sz += signer->nameLen;
return sz;
}
/* Return memory needed to persist this row, have lock */
static INLINE int GetCertCacheRowMemory(Signer* row)
{
int sz = 0;
while (row) {
sz += GetSignerMemory(row);
row = row->next;
}
return sz;
}
/* get the size of persist cert cache, have lock */
2014-12-19 11:27:01 -07:00
static INLINE int GetCertCacheMemSize(WOLFSSL_CERT_MANAGER* cm)
2013-05-02 11:34:26 -07:00
{
int sz;
int i;
sz = sizeof(CertCacheHeader);
for (i = 0; i < CA_TABLE_SIZE; i++)
sz += GetCertCacheRowMemory(cm->caTable[i]);
return sz;
}
2013-05-10 17:31:50 -06:00
/* Store cert cache header columns with number of items per list, have lock */
2014-12-19 11:27:01 -07:00
static INLINE void SetCertHeaderColumns(WOLFSSL_CERT_MANAGER* cm, int* columns)
2013-05-02 11:34:26 -07:00
{
int i;
Signer* row;
for (i = 0; i < CA_TABLE_SIZE; i++) {
int count = 0;
row = cm->caTable[i];
while (row) {
++count;
row = row->next;
}
columns[i] = count;
}
}
2013-05-10 17:31:50 -06:00
/* Restore whole cert row from memory, have lock, return bytes consumed,
< 0 on error, have lock */
2014-12-19 11:27:01 -07:00
static INLINE int RestoreCertRow(WOLFSSL_CERT_MANAGER* cm, byte* current,
2013-05-02 11:34:26 -07:00
int row, int listSz, const byte* end)
{
int idx = 0;
if (listSz < 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Row header corrupted, negative value");
2013-05-02 11:34:26 -07:00
return PARSE_ERROR;
}
while (listSz) {
Signer* signer;
byte* start = current + idx; /* for end checks on this signer */
int minSz = sizeof(signer->pubKeySize) + sizeof(signer->keyOID) +
sizeof(signer->nameLen) + sizeof(signer->subjectNameHash);
#ifndef NO_SKID
minSz += (int)sizeof(signer->subjectKeyIdHash);
2013-05-02 11:34:26 -07:00
#endif
if (start + minSz > end) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Would overread restore buffer");
2013-05-02 11:34:26 -07:00
return BUFFER_E;
}
signer = MakeSigner(cm->heap);
if (signer == NULL)
return MEMORY_E;
/* pubKeySize */
XMEMCPY(&signer->pubKeySize, current + idx, sizeof(signer->pubKeySize));
idx += (int)sizeof(signer->pubKeySize);
2013-05-02 11:34:26 -07:00
/* keyOID */
XMEMCPY(&signer->keyOID, current + idx, sizeof(signer->keyOID));
idx += (int)sizeof(signer->keyOID);
2013-05-02 11:34:26 -07:00
/* pulicKey */
if (start + minSz + signer->pubKeySize > end) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Would overread restore buffer");
2013-05-02 11:34:26 -07:00
FreeSigner(signer, cm->heap);
return BUFFER_E;
}
signer->publicKey = (byte*)XMALLOC(signer->pubKeySize, cm->heap,
DYNAMIC_TYPE_KEY);
if (signer->publicKey == NULL) {
FreeSigner(signer, cm->heap);
return MEMORY_E;
}
XMEMCPY(signer->publicKey, current + idx, signer->pubKeySize);
idx += signer->pubKeySize;
/* nameLen */
XMEMCPY(&signer->nameLen, current + idx, sizeof(signer->nameLen));
idx += (int)sizeof(signer->nameLen);
2013-05-02 11:34:26 -07:00
/* name */
if (start + minSz + signer->pubKeySize + signer->nameLen > end) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Would overread restore buffer");
2013-05-02 11:34:26 -07:00
FreeSigner(signer, cm->heap);
return BUFFER_E;
}
signer->name = (char*)XMALLOC(signer->nameLen, cm->heap,
DYNAMIC_TYPE_SUBJECT_CN);
if (signer->name == NULL) {
FreeSigner(signer, cm->heap);
return MEMORY_E;
}
XMEMCPY(signer->name, current + idx, signer->nameLen);
idx += signer->nameLen;
/* subjectNameHash */
XMEMCPY(signer->subjectNameHash, current + idx, SIGNER_DIGEST_SIZE);
idx += SIGNER_DIGEST_SIZE;
#ifndef NO_SKID
/* subjectKeyIdHash */
XMEMCPY(signer->subjectKeyIdHash, current + idx,SIGNER_DIGEST_SIZE);
idx += SIGNER_DIGEST_SIZE;
#endif
signer->next = cm->caTable[row];
cm->caTable[row] = signer;
--listSz;
}
return idx;
}
2013-05-10 17:31:50 -06:00
/* Store whole cert row into memory, have lock, return bytes added */
2014-12-19 11:27:01 -07:00
static INLINE int StoreCertRow(WOLFSSL_CERT_MANAGER* cm, byte* current, int row)
2013-05-02 11:34:26 -07:00
{
int added = 0;
Signer* list = cm->caTable[row];
while (list) {
XMEMCPY(current + added, &list->pubKeySize, sizeof(list->pubKeySize));
added += (int)sizeof(list->pubKeySize);
2013-05-02 11:34:26 -07:00
XMEMCPY(current + added, &list->keyOID, sizeof(list->keyOID));
added += (int)sizeof(list->keyOID);
2013-05-02 11:34:26 -07:00
XMEMCPY(current + added, list->publicKey, list->pubKeySize);
added += list->pubKeySize;
XMEMCPY(current + added, &list->nameLen, sizeof(list->nameLen));
added += (int)sizeof(list->nameLen);
2013-05-02 11:34:26 -07:00
XMEMCPY(current + added, list->name, list->nameLen);
added += list->nameLen;
XMEMCPY(current + added, list->subjectNameHash, SIGNER_DIGEST_SIZE);
added += SIGNER_DIGEST_SIZE;
#ifndef NO_SKID
XMEMCPY(current + added, list->subjectKeyIdHash,SIGNER_DIGEST_SIZE);
added += SIGNER_DIGEST_SIZE;
#endif
list = list->next;
}
return added;
}
/* Persist cert cache to memory, have lock */
static INLINE int DoMemSaveCertCache(WOLFSSL_CERT_MANAGER* cm,
void* mem, int sz)
2013-05-02 11:34:26 -07:00
{
int realSz;
int ret = SSL_SUCCESS;
int i;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("DoMemSaveCertCache");
2013-05-02 11:34:26 -07:00
realSz = GetCertCacheMemSize(cm);
if (realSz > sz) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Mem output buffer too small");
2013-05-02 11:34:26 -07:00
ret = BUFFER_E;
}
else {
byte* current;
CertCacheHeader hdr;
2014-12-19 11:27:01 -07:00
hdr.version = WOLFSSL_CACHE_CERT_VERSION;
2013-05-02 11:34:26 -07:00
hdr.rows = CA_TABLE_SIZE;
SetCertHeaderColumns(cm, hdr.columns);
hdr.signerSz = (int)sizeof(Signer);
XMEMCPY(mem, &hdr, sizeof(CertCacheHeader));
current = (byte*)mem + sizeof(CertCacheHeader);
for (i = 0; i < CA_TABLE_SIZE; ++i)
current += StoreCertRow(cm, current, i);
}
return ret;
}
#if !defined(NO_FILESYSTEM)
/* Persist cert cache to file */
2014-12-19 11:27:01 -07:00
int CM_SaveCertCache(WOLFSSL_CERT_MANAGER* cm, const char* fname)
2013-05-02 11:34:26 -07:00
{
XFILE file;
int rc = SSL_SUCCESS;
int memSz;
byte* mem;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("CM_SaveCertCache");
2013-05-02 11:34:26 -07:00
file = XFOPEN(fname, "w+b");
if (file == XBADFILE) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Couldn't open cert cache save file");
2013-05-02 11:34:26 -07:00
return SSL_BAD_FILE;
}
if (wc_LockMutex(&cm->caLock) != 0) {
WOLFSSL_MSG("wc_LockMutex on caLock failed");
2013-05-02 11:34:26 -07:00
XFCLOSE(file);
return BAD_MUTEX_E;
2013-05-02 11:34:26 -07:00
}
memSz = GetCertCacheMemSize(cm);
mem = (byte*)XMALLOC(memSz, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
2013-05-02 11:34:26 -07:00
if (mem == NULL) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Alloc for tmp buffer failed");
2013-05-02 11:34:26 -07:00
rc = MEMORY_E;
} else {
rc = DoMemSaveCertCache(cm, mem, memSz);
if (rc == SSL_SUCCESS) {
int ret = (int)XFWRITE(mem, memSz, 1, file);
if (ret != 1) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Cert cache file write failed");
2013-05-02 11:34:26 -07:00
rc = FWRITE_ERROR;
}
}
XFREE(mem, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
}
wc_UnLockMutex(&cm->caLock);
2013-05-02 11:34:26 -07:00
XFCLOSE(file);
return rc;
}
2013-05-10 17:31:50 -06:00
/* Restore cert cache from file */
2014-12-19 11:27:01 -07:00
int CM_RestoreCertCache(WOLFSSL_CERT_MANAGER* cm, const char* fname)
2013-05-02 11:34:26 -07:00
{
XFILE file;
int rc = SSL_SUCCESS;
int ret;
int memSz;
byte* mem;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("CM_RestoreCertCache");
2013-05-02 11:34:26 -07:00
file = XFOPEN(fname, "rb");
if (file == XBADFILE) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Couldn't open cert cache save file");
2013-05-02 11:34:26 -07:00
return SSL_BAD_FILE;
}
XFSEEK(file, 0, XSEEK_END);
memSz = (int)XFTELL(file);
XREWIND(file);
if (memSz <= 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad file size");
2013-05-02 11:34:26 -07:00
XFCLOSE(file);
return SSL_BAD_FILE;
}
mem = (byte*)XMALLOC(memSz, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
if (mem == NULL) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Alloc for tmp buffer failed");
2013-05-02 11:34:26 -07:00
XFCLOSE(file);
return MEMORY_E;
}
ret = (int)XFREAD(mem, memSz, 1, file);
if (ret != 1) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Cert file read error");
2013-05-02 11:34:26 -07:00
rc = FREAD_ERROR;
} else {
rc = CM_MemRestoreCertCache(cm, mem, memSz);
if (rc != SSL_SUCCESS) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Mem restore cert cache failed");
2013-05-02 11:34:26 -07:00
}
}
XFREE(mem, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
XFCLOSE(file);
return rc;
}
#endif /* NO_FILESYSTEM */
/* Persist cert cache to memory */
2014-12-19 11:27:01 -07:00
int CM_MemSaveCertCache(WOLFSSL_CERT_MANAGER* cm, void* mem, int sz, int* used)
2013-05-02 11:34:26 -07:00
{
int ret = SSL_SUCCESS;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("CM_MemSaveCertCache");
2013-05-02 11:34:26 -07:00
if (wc_LockMutex(&cm->caLock) != 0) {
WOLFSSL_MSG("wc_LockMutex on caLock failed");
return BAD_MUTEX_E;
2013-05-02 11:34:26 -07:00
}
2013-05-02 12:23:49 -07:00
ret = DoMemSaveCertCache(cm, mem, sz);
if (ret == SSL_SUCCESS)
*used = GetCertCacheMemSize(cm);
2013-05-02 11:34:26 -07:00
wc_UnLockMutex(&cm->caLock);
2013-05-02 11:34:26 -07:00
return ret;
}
2013-05-10 17:31:50 -06:00
/* Restore cert cache from memory */
2014-12-19 11:27:01 -07:00
int CM_MemRestoreCertCache(WOLFSSL_CERT_MANAGER* cm, const void* mem, int sz)
2013-05-02 11:34:26 -07:00
{
int ret = SSL_SUCCESS;
int i;
CertCacheHeader* hdr = (CertCacheHeader*)mem;
byte* current = (byte*)mem + sizeof(CertCacheHeader);
byte* end = (byte*)mem + sz; /* don't go over */
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("CM_MemRestoreCertCache");
2013-05-02 11:34:26 -07:00
if (current > end) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Cert Cache Memory buffer too small");
2013-05-02 11:34:26 -07:00
return BUFFER_E;
}
2014-12-19 11:27:01 -07:00
if (hdr->version != WOLFSSL_CACHE_CERT_VERSION ||
2013-05-02 11:34:26 -07:00
hdr->rows != CA_TABLE_SIZE ||
hdr->signerSz != (int)sizeof(Signer)) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Cert Cache Memory header mismatch");
2013-05-02 11:34:26 -07:00
return CACHE_MATCH_ERROR;
}
if (wc_LockMutex(&cm->caLock) != 0) {
WOLFSSL_MSG("wc_LockMutex on caLock failed");
return BAD_MUTEX_E;
2013-05-02 11:34:26 -07:00
}
FreeSignerTable(cm->caTable, CA_TABLE_SIZE, cm->heap);
for (i = 0; i < CA_TABLE_SIZE; ++i) {
int added = RestoreCertRow(cm, current, i, hdr->columns[i], end);
if (added < 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("RestoreCertRow error");
2013-05-02 11:34:26 -07:00
ret = added;
break;
}
current += added;
}
wc_UnLockMutex(&cm->caLock);
2013-05-02 11:34:26 -07:00
return ret;
}
/* get how big the the cert cache save buffer needs to be */
2014-12-19 11:27:01 -07:00
int CM_GetCertCacheMemSize(WOLFSSL_CERT_MANAGER* cm)
2013-05-02 11:34:26 -07:00
{
int sz;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("CM_GetCertCacheMemSize");
2013-05-02 11:34:26 -07:00
if (wc_LockMutex(&cm->caLock) != 0) {
WOLFSSL_MSG("wc_LockMutex on caLock failed");
return BAD_MUTEX_E;
2013-05-02 11:34:26 -07:00
}
sz = GetCertCacheMemSize(cm);
wc_UnLockMutex(&cm->caLock);
2013-05-02 11:34:26 -07:00
return sz;
}
#endif /* PERSIST_CERT_CACHE */
#endif /* NO_CERTS */
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_set_cipher_list(WOLFSSL_CTX* ctx, const char* list)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_set_cipher_list");
/* alloc/init on demand only */
if (ctx->suites == NULL) {
ctx->suites = (Suites*)XMALLOC(sizeof(Suites), ctx->heap,
DYNAMIC_TYPE_SUITES);
if (ctx->suites == NULL) {
WOLFSSL_MSG("Memory alloc for Suites failed");
return SSL_FAILURE;
}
XMEMSET(ctx->suites, 0, sizeof(Suites));
}
return (SetCipherList(ctx->suites, list)) ? SSL_SUCCESS : SSL_FAILURE;
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
int wolfSSL_set_cipher_list(WOLFSSL* ssl, const char* list)
2011-10-28 18:43:07 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_set_cipher_list");
return (SetCipherList(ssl->suites, list)) ? SSL_SUCCESS : SSL_FAILURE;
2011-10-28 18:43:07 -07:00
}
2014-12-19 11:27:01 -07:00
#ifndef WOLFSSL_LEANPSK
#ifdef WOLFSSL_DTLS
2014-12-19 11:27:01 -07:00
int wolfSSL_dtls_get_current_timeout(WOLFSSL* ssl)
{
(void)ssl;
return ssl->dtls_timeout;
}
/* user may need to alter init dtls recv timeout, SSL_SUCCESS on ok */
2014-12-19 11:27:01 -07:00
int wolfSSL_dtls_set_timeout_init(WOLFSSL* ssl, int timeout)
{
if (ssl == NULL || timeout < 0)
return BAD_FUNC_ARG;
if (timeout > ssl->dtls_timeout_max) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Can't set dtls timeout init greater than dtls timeout max");
return BAD_FUNC_ARG;
}
ssl->dtls_timeout_init = timeout;
ssl->dtls_timeout = timeout;
return SSL_SUCCESS;
}
/* user may need to alter max dtls recv timeout, SSL_SUCCESS on ok */
2014-12-19 11:27:01 -07:00
int wolfSSL_dtls_set_timeout_max(WOLFSSL* ssl, int timeout)
{
if (ssl == NULL || timeout < 0)
return BAD_FUNC_ARG;
if (timeout < ssl->dtls_timeout_init) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Can't set dtls timeout max less than dtls timeout init");
return BAD_FUNC_ARG;
}
ssl->dtls_timeout_max = timeout;
return SSL_SUCCESS;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_dtls_got_timeout(WOLFSSL* ssl)
{
2012-09-14 19:30:50 -07:00
int result = SSL_SUCCESS;
2016-06-27 10:53:34 -07:00
if (!ssl->options.handShakeDone &&
(DtlsMsgPoolTimeout(ssl) < 0 || DtlsMsgPoolSend(ssl, 0) < 0)) {
2016-06-27 10:53:34 -07:00
2012-09-14 09:35:34 -07:00
result = SSL_FATAL_ERROR;
}
return result;
}
#endif /* DTLS */
#endif /* LEANPSK */
#if defined(WOLFSSL_DTLS) && !defined(NO_WOLFSSL_SERVER)
/* Not an SSL function, return 0 for success, error code otherwise */
/* Prereq: ssl's RNG needs to be initialized. */
int wolfSSL_DTLS_SetCookieSecret(WOLFSSL* ssl,
const byte* secret, word32 secretSz)
{
WOLFSSL_ENTER("wolfSSL_DTLS_SetCookieSecret");
if (ssl == NULL) {
WOLFSSL_MSG("need a SSL object");
return BAD_FUNC_ARG;
}
if (secret != NULL && secretSz == 0) {
WOLFSSL_MSG("can't have a new secret without a size");
return BAD_FUNC_ARG;
}
/* If secretSz is 0, use the default size. */
if (secretSz == 0)
secretSz = COOKIE_SECRET_SZ;
if (secretSz != ssl->buffers.dtlsCookieSecret.length) {
byte* newSecret;
if (ssl->buffers.dtlsCookieSecret.buffer != NULL) {
2015-09-14 17:13:16 -07:00
ForceZero(ssl->buffers.dtlsCookieSecret.buffer,
ssl->buffers.dtlsCookieSecret.length);
XFREE(ssl->buffers.dtlsCookieSecret.buffer,
ssl->heap, DYNAMIC_TYPE_NONE);
}
2015-09-14 17:13:16 -07:00
newSecret = (byte*)XMALLOC(secretSz, ssl->heap,DYNAMIC_TYPE_COOKIE_PWD);
if (newSecret == NULL) {
ssl->buffers.dtlsCookieSecret.buffer = NULL;
ssl->buffers.dtlsCookieSecret.length = 0;
WOLFSSL_MSG("couldn't allocate new cookie secret");
return MEMORY_ERROR;
}
ssl->buffers.dtlsCookieSecret.buffer = newSecret;
ssl->buffers.dtlsCookieSecret.length = secretSz;
}
/* If the supplied secret is NULL, randomly generate a new secret. */
if (secret == NULL)
wc_RNG_GenerateBlock(ssl->rng,
ssl->buffers.dtlsCookieSecret.buffer, secretSz);
else
XMEMCPY(ssl->buffers.dtlsCookieSecret.buffer, secret, secretSz);
WOLFSSL_LEAVE("wolfSSL_DTLS_SetCookieSecret", 0);
return 0;
}
#endif /* WOLFSSL_DTLS && !NO_WOLFSSL_SERVER */
2011-02-05 11:14:47 -08:00
/* client only parts */
2014-12-19 11:27:01 -07:00
#ifndef NO_WOLFSSL_CLIENT
2011-02-05 11:14:47 -08:00
2015-08-12 16:39:13 -07:00
#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
2014-12-19 11:27:01 -07:00
WOLFSSL_METHOD* wolfSSLv3_client_method(void)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSLv3_client_method");
return wolfSSLv3_client_method_ex(NULL);
2011-02-05 11:14:47 -08:00
}
#endif
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_DTLS
#ifndef NO_OLD_TLS
2014-12-19 11:27:01 -07:00
WOLFSSL_METHOD* wolfDTLSv1_client_method(void)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("DTLSv1_client_method");
return wolfDTLSv1_client_method_ex(NULL);
2011-02-05 11:14:47 -08:00
}
#endif /* NO_OLD_TLS */
2013-03-06 23:02:33 -08:00
2014-12-19 11:27:01 -07:00
WOLFSSL_METHOD* wolfDTLSv1_2_client_method(void)
2013-03-06 23:02:33 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("DTLSv1_2_client_method");
return wolfDTLSv1_2_client_method_ex(NULL);
2013-03-06 23:02:33 -08:00
}
2011-02-05 11:14:47 -08:00
#endif
#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
WOLFSSL_METHOD* wolfSSLv3_client_method_ex(void* heap)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_METHOD* method =
(WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
heap, DYNAMIC_TYPE_METHOD);
WOLFSSL_ENTER("SSLv3_client_method_ex");
2011-02-05 11:14:47 -08:00
if (method)
InitSSL_Method(method, MakeSSLv3());
return method;
}
#endif
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_DTLS
#ifndef NO_OLD_TLS
WOLFSSL_METHOD* wolfDTLSv1_client_method_ex(void* heap)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_METHOD* method =
(WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
heap, DYNAMIC_TYPE_METHOD);
WOLFSSL_ENTER("DTLSv1_client_method_ex");
2011-02-05 11:14:47 -08:00
if (method)
InitSSL_Method(method, MakeDTLSv1());
return method;
}
#endif /* NO_OLD_TLS */
2013-03-06 23:02:33 -08:00
WOLFSSL_METHOD* wolfDTLSv1_2_client_method_ex(void* heap)
2013-03-06 23:02:33 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_METHOD* method =
(WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
heap, DYNAMIC_TYPE_METHOD);
WOLFSSL_ENTER("DTLSv1_2_client_method_ex");
2013-03-06 23:02:33 -08:00
if (method)
InitSSL_Method(method, MakeDTLSv1_2());
return method;
}
2011-02-05 11:14:47 -08:00
#endif
/* If SCTP is not enabled returns the state of the dtls option.
* If SCTP is enabled returns dtls && !sctp. */
static INLINE int IsDtlsNotSctpMode(WOLFSSL* ssl)
{
int result = ssl->options.dtls;
if (result) {
#ifdef WOLFSSL_SCTP
result = !ssl->options.dtlsSctp;
#endif
}
return result;
}
2011-02-05 11:14:47 -08:00
/* please see note at top of README if you get an error from connect */
2014-12-19 11:27:01 -07:00
int wolfSSL_connect(WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
int neededState;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_connect()");
2011-02-05 11:14:47 -08:00
#ifdef HAVE_ERRNO_H
2011-02-05 11:14:47 -08:00
errno = 0;
#endif
2014-12-19 11:27:01 -07:00
if (ssl->options.side != WOLFSSL_CLIENT_END) {
WOLFSSL_ERROR(ssl->error = SIDE_ERROR);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_DTLS
if (ssl->version.major == DTLS_MAJOR) {
2011-02-05 11:14:47 -08:00
ssl->options.dtls = 1;
ssl->options.tls = 1;
ssl->options.tls1_1 = 1;
}
#endif
if (ssl->buffers.outputBuffer.length > 0) {
if ( (ssl->error = SendBuffered(ssl)) == 0) {
/* fragOffset is non-zero when sending fragments. On the last
* fragment, fragOffset is zero again, and the state can be
* advanced. */
if (ssl->fragOffset == 0) {
ssl->options.connectState++;
WOLFSSL_MSG("connect state: "
"Advanced from last buffered fragment send");
}
else {
WOLFSSL_MSG("connect state: "
"Not advanced, more fragments to send");
}
2011-02-05 11:14:47 -08:00
}
else {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
}
switch (ssl->options.connectState) {
case CONNECT_BEGIN :
/* always send client hello first */
if ( (ssl->error = SendClientHello(ssl)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
ssl->options.connectState = CLIENT_HELLO_SENT;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("connect state: CLIENT_HELLO_SENT");
2011-02-05 11:14:47 -08:00
case CLIENT_HELLO_SENT :
neededState = ssl->options.resuming ? SERVER_FINISHED_COMPLETE :
SERVER_HELLODONE_COMPLETE;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_DTLS
/* In DTLS, when resuming, we can go straight to FINISHED,
* or do a cookie exchange and then skip to FINISHED, assume
* we need the cookie exchange first. */
if (IsDtlsNotSctpMode(ssl))
2011-02-05 11:14:47 -08:00
neededState = SERVER_HELLOVERIFYREQUEST_COMPLETE;
#endif
/* get response */
while (ssl->options.serverState < neededState) {
if ( (ssl->error = ProcessReply(ssl)) < 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
/* if resumption failed, reset needed state */
else if (neededState == SERVER_FINISHED_COMPLETE)
if (!ssl->options.resuming) {
if (!IsDtlsNotSctpMode(ssl))
2011-02-05 11:14:47 -08:00
neededState = SERVER_HELLODONE_COMPLETE;
else
neededState = SERVER_HELLOVERIFYREQUEST_COMPLETE;
}
}
ssl->options.connectState = HELLO_AGAIN;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("connect state: HELLO_AGAIN");
2011-02-05 11:14:47 -08:00
case HELLO_AGAIN :
2011-08-17 12:33:36 -07:00
if (ssl->options.certOnly)
return SSL_SUCCESS;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_DTLS
if (IsDtlsNotSctpMode(ssl)) {
2011-02-05 11:14:47 -08:00
/* re-init hashes, exclude first hello and verify request */
#ifndef NO_OLD_TLS
wc_InitMd5(&ssl->hsHashes->hashMd5);
if ( (ssl->error = wc_InitSha(&ssl->hsHashes->hashSha))
!= 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2014-03-25 16:01:17 -07:00
return SSL_FATAL_ERROR;
}
#endif
2013-03-06 23:02:33 -08:00
if (IsAtLeastTLSv1_2(ssl)) {
#ifndef NO_SHA256
if ( (ssl->error = wc_InitSha256(
&ssl->hsHashes->hashSha256)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2014-03-25 17:11:15 -07:00
return SSL_FATAL_ERROR;
}
2013-03-06 23:02:33 -08:00
#endif
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SHA384
if ( (ssl->error = wc_InitSha384(
&ssl->hsHashes->hashSha384)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2014-03-27 14:37:37 -07:00
return SSL_FATAL_ERROR;
}
2013-03-06 23:02:33 -08:00
#endif
#ifdef WOLFSSL_SHA512
if ( (ssl->error = wc_InitSha512(
&ssl->hsHashes->hashSha512)) != 0) {
WOLFSSL_ERROR(ssl->error);
return SSL_FATAL_ERROR;
}
#endif
2013-03-06 23:02:33 -08:00
}
2011-02-05 11:14:47 -08:00
if ( (ssl->error = SendClientHello(ssl)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
}
#endif
ssl->options.connectState = HELLO_AGAIN_REPLY;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("connect state: HELLO_AGAIN_REPLY");
2011-02-05 11:14:47 -08:00
case HELLO_AGAIN_REPLY :
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_DTLS
if (IsDtlsNotSctpMode(ssl)) {
2011-02-05 11:14:47 -08:00
neededState = ssl->options.resuming ?
SERVER_FINISHED_COMPLETE : SERVER_HELLODONE_COMPLETE;
2011-02-05 11:14:47 -08:00
/* get response */
while (ssl->options.serverState < neededState) {
if ( (ssl->error = ProcessReply(ssl)) < 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
/* if resumption failed, reset needed state */
else if (neededState == SERVER_FINISHED_COMPLETE)
if (!ssl->options.resuming)
neededState = SERVER_HELLODONE_COMPLETE;
}
}
#endif
ssl->options.connectState = FIRST_REPLY_DONE;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("connect state: FIRST_REPLY_DONE");
2011-02-05 11:14:47 -08:00
case FIRST_REPLY_DONE :
#ifndef NO_CERTS
if (ssl->options.sendVerify) {
if ( (ssl->error = SendCertificate(ssl)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
return SSL_FATAL_ERROR;
}
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("sent: certificate");
}
#endif
2011-02-05 11:14:47 -08:00
ssl->options.connectState = FIRST_REPLY_FIRST;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("connect state: FIRST_REPLY_FIRST");
2011-02-05 11:14:47 -08:00
case FIRST_REPLY_FIRST :
if (!ssl->options.resuming) {
2011-02-05 11:14:47 -08:00
if ( (ssl->error = SendClientKeyExchange(ssl)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("sent: client key exchange");
}
2011-02-05 11:14:47 -08:00
ssl->options.connectState = FIRST_REPLY_SECOND;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("connect state: FIRST_REPLY_SECOND");
2011-02-05 11:14:47 -08:00
case FIRST_REPLY_SECOND :
#ifndef NO_CERTS
if (ssl->options.sendVerify) {
2012-10-29 15:39:42 -07:00
if ( (ssl->error = SendCertificateVerify(ssl)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2012-10-29 15:39:42 -07:00
return SSL_FATAL_ERROR;
}
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("sent: certificate verify");
2012-10-29 15:39:42 -07:00
}
#endif
2011-02-05 11:14:47 -08:00
ssl->options.connectState = FIRST_REPLY_THIRD;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("connect state: FIRST_REPLY_THIRD");
2011-02-05 11:14:47 -08:00
case FIRST_REPLY_THIRD :
if ( (ssl->error = SendChangeCipher(ssl)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("sent: change cipher spec");
2011-02-05 11:14:47 -08:00
ssl->options.connectState = FIRST_REPLY_FOURTH;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("connect state: FIRST_REPLY_FOURTH");
2011-02-05 11:14:47 -08:00
case FIRST_REPLY_FOURTH :
if ( (ssl->error = SendFinished(ssl)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("sent: finished");
2011-02-05 11:14:47 -08:00
ssl->options.connectState = FINISHED_DONE;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("connect state: FINISHED_DONE");
2011-02-05 11:14:47 -08:00
case FINISHED_DONE :
/* get response */
while (ssl->options.serverState < SERVER_FINISHED_COMPLETE)
if ( (ssl->error = ProcessReply(ssl)) < 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
2011-02-05 11:14:47 -08:00
ssl->options.connectState = SECOND_REPLY_DONE;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("connect state: SECOND_REPLY_DONE");
2011-02-05 11:14:47 -08:00
case SECOND_REPLY_DONE:
#ifndef NO_HANDSHAKE_DONE_CB
if (ssl->hsDoneCb) {
int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx);
if (cbret < 0) {
ssl->error = cbret;
WOLFSSL_MSG("HandShake Done Cb don't continue error");
return SSL_FATAL_ERROR;
}
}
#endif /* NO_HANDSHAKE_DONE_CB */
if (!ssl->options.dtls) {
FreeHandshakeResources(ssl);
}
#ifdef WOLFSSL_DTLS
else {
ssl->options.dtlsHsRetain = 1;
}
#endif /* WOLFSSL_DTLS */
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("SSL_connect()", SSL_SUCCESS);
2011-02-05 11:14:47 -08:00
return SSL_SUCCESS;
default:
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Unknown connect state ERROR");
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR; /* unknown connect state */
}
}
2014-12-19 11:27:01 -07:00
#endif /* NO_WOLFSSL_CLIENT */
2011-02-05 11:14:47 -08:00
/* server only parts */
2014-12-19 11:27:01 -07:00
#ifndef NO_WOLFSSL_SERVER
2011-02-05 11:14:47 -08:00
2015-08-12 16:39:13 -07:00
#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
2014-12-19 11:27:01 -07:00
WOLFSSL_METHOD* wolfSSLv3_server_method(void)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSLv3_server_method");
return wolfSSLv3_server_method_ex(NULL);
2011-02-05 11:14:47 -08:00
}
#endif
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_DTLS
#ifndef NO_OLD_TLS
2014-12-19 11:27:01 -07:00
WOLFSSL_METHOD* wolfDTLSv1_server_method(void)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("DTLSv1_server_method");
return wolfDTLSv1_server_method_ex(NULL);
2011-02-05 11:14:47 -08:00
}
#endif /* NO_OLD_TLS */
2013-03-06 23:02:33 -08:00
2014-12-19 11:27:01 -07:00
WOLFSSL_METHOD* wolfDTLSv1_2_server_method(void)
2013-03-06 23:02:33 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("DTLSv1_2_server_method");
return wolfDTLSv1_2_server_method_ex(NULL);
2013-03-06 23:02:33 -08:00
}
2011-02-05 11:14:47 -08:00
#endif
#if defined(WOLFSSL_ALLOW_SSLV3) && !defined(NO_OLD_TLS)
WOLFSSL_METHOD* wolfSSLv3_server_method_ex(void* heap)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_METHOD* method =
(WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
heap, DYNAMIC_TYPE_METHOD);
WOLFSSL_ENTER("SSLv3_server_method_ex");
2011-02-05 11:14:47 -08:00
if (method) {
InitSSL_Method(method, MakeSSLv3());
2014-12-19 11:27:01 -07:00
method->side = WOLFSSL_SERVER_END;
2011-02-05 11:14:47 -08:00
}
return method;
}
#endif
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_DTLS
#ifndef NO_OLD_TLS
WOLFSSL_METHOD* wolfDTLSv1_server_method_ex(void* heap)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_METHOD* method =
(WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
heap, DYNAMIC_TYPE_METHOD);
WOLFSSL_ENTER("DTLSv1_server_method_ex");
2011-02-05 11:14:47 -08:00
if (method) {
InitSSL_Method(method, MakeDTLSv1());
2014-12-19 11:27:01 -07:00
method->side = WOLFSSL_SERVER_END;
2011-02-05 11:14:47 -08:00
}
return method;
}
#endif /* NO_OLD_TLS */
2013-03-06 23:02:33 -08:00
WOLFSSL_METHOD* wolfDTLSv1_2_server_method_ex(void* heap)
2013-03-06 23:02:33 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_METHOD* method =
(WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
heap, DYNAMIC_TYPE_METHOD);
WOLFSSL_ENTER("DTLSv1_2_server_method_ex");
2013-03-06 23:02:33 -08:00
if (method) {
InitSSL_Method(method, MakeDTLSv1_2());
2014-12-19 11:27:01 -07:00
method->side = WOLFSSL_SERVER_END;
2013-03-06 23:02:33 -08:00
}
return method;
}
2011-02-05 11:14:47 -08:00
#endif
2014-12-19 11:27:01 -07:00
int wolfSSL_accept(WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
2015-12-21 16:11:02 -07:00
word16 havePSK = 0;
word16 haveAnon = 0;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_accept()");
2011-02-05 11:14:47 -08:00
#ifdef HAVE_ERRNO_H
2011-02-05 11:14:47 -08:00
errno = 0;
#endif
2011-04-04 17:42:50 -07:00
#ifndef NO_PSK
havePSK = ssl->options.havePSK;
2011-04-04 17:42:50 -07:00
#endif
(void)havePSK;
2011-04-04 17:42:50 -07:00
#ifdef HAVE_ANON
haveAnon = ssl->options.haveAnon;
#endif
(void)haveAnon;
2014-12-19 11:27:01 -07:00
if (ssl->options.side != WOLFSSL_SERVER_END) {
WOLFSSL_ERROR(ssl->error = SIDE_ERROR);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
#ifndef NO_CERTS
/* in case used set_accept_state after init */
if (!havePSK && !haveAnon &&
(!ssl->buffers.certificate ||
!ssl->buffers.certificate->buffer ||
!ssl->buffers.key ||
!ssl->buffers.key->buffer)) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("accept error: don't have server cert and key");
ssl->error = NO_PRIVATE_KEY;
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
return SSL_FATAL_ERROR;
}
#endif
2011-04-04 17:42:50 -07:00
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_DTLS
if (ssl->version.major == DTLS_MAJOR) {
2011-02-05 11:14:47 -08:00
ssl->options.dtls = 1;
ssl->options.tls = 1;
ssl->options.tls1_1 = 1;
}
#endif
if (ssl->buffers.outputBuffer.length > 0) {
if ( (ssl->error = SendBuffered(ssl)) == 0) {
/* fragOffset is non-zero when sending fragments. On the last
* fragment, fragOffset is zero again, and the state can be
* advanced. */
if (ssl->fragOffset == 0) {
ssl->options.acceptState++;
WOLFSSL_MSG("accept state: "
"Advanced from last buffered fragment send");
}
else {
WOLFSSL_MSG("accept state: "
"Not advanced, more fragments to send");
}
2011-02-05 11:14:47 -08:00
}
else {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
}
switch (ssl->options.acceptState) {
2011-02-05 11:14:47 -08:00
case ACCEPT_BEGIN :
/* get response */
while (ssl->options.clientState < CLIENT_HELLO_COMPLETE)
if ( (ssl->error = ProcessReply(ssl)) < 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
ssl->options.acceptState = ACCEPT_CLIENT_HELLO_DONE;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("accept state ACCEPT_CLIENT_HELLO_DONE");
2011-02-05 11:14:47 -08:00
case ACCEPT_CLIENT_HELLO_DONE :
ssl->options.acceptState = ACCEPT_FIRST_REPLY_DONE;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE");
2011-02-05 11:14:47 -08:00
case ACCEPT_FIRST_REPLY_DONE :
if ( (ssl->error = SendServerHello(ssl)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
ssl->options.acceptState = SERVER_HELLO_SENT;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("accept state SERVER_HELLO_SENT");
2011-02-05 11:14:47 -08:00
case SERVER_HELLO_SENT :
#ifndef NO_CERTS
if (!ssl->options.resuming)
if ( (ssl->error = SendCertificate(ssl)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
return SSL_FATAL_ERROR;
}
#endif
2011-02-05 11:14:47 -08:00
ssl->options.acceptState = CERT_SENT;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("accept state CERT_SENT");
2011-02-05 11:14:47 -08:00
case CERT_SENT :
#ifndef NO_CERTS
2015-12-28 19:38:04 -03:00
if (!ssl->options.resuming)
if ( (ssl->error = SendCertificateStatus(ssl)) != 0) {
WOLFSSL_ERROR(ssl->error);
return SSL_FATAL_ERROR;
}
#endif
2015-12-28 19:38:04 -03:00
ssl->options.acceptState = CERT_STATUS_SENT;
WOLFSSL_MSG("accept state CERT_STATUS_SENT");
case CERT_STATUS_SENT :
if (!ssl->options.resuming)
2011-02-05 11:14:47 -08:00
if ( (ssl->error = SendServerKeyExchange(ssl)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
ssl->options.acceptState = KEY_EXCHANGE_SENT;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("accept state KEY_EXCHANGE_SENT");
2011-02-05 11:14:47 -08:00
case KEY_EXCHANGE_SENT :
#ifndef NO_CERTS
if (!ssl->options.resuming)
if (ssl->options.verifyPeer)
if ( (ssl->error = SendCertificateRequest(ssl)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
return SSL_FATAL_ERROR;
}
#endif
2011-02-05 11:14:47 -08:00
ssl->options.acceptState = CERT_REQ_SENT;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("accept state CERT_REQ_SENT");
2011-02-05 11:14:47 -08:00
case CERT_REQ_SENT :
if (!ssl->options.resuming)
2011-02-05 11:14:47 -08:00
if ( (ssl->error = SendServerHelloDone(ssl)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
ssl->options.acceptState = SERVER_HELLO_DONE;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("accept state SERVER_HELLO_DONE");
2011-02-05 11:14:47 -08:00
case SERVER_HELLO_DONE :
if (!ssl->options.resuming) {
while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE)
if ( (ssl->error = ProcessReply(ssl)) < 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
}
ssl->options.acceptState = ACCEPT_SECOND_REPLY_DONE;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("accept state ACCEPT_SECOND_REPLY_DONE");
case ACCEPT_SECOND_REPLY_DONE :
#ifdef HAVE_SESSION_TICKET
if (ssl->options.createTicket) {
if ( (ssl->error = SendTicket(ssl)) != 0) {
WOLFSSL_ERROR(ssl->error);
return SSL_FATAL_ERROR;
}
}
#endif /* HAVE_SESSION_TICKET */
ssl->options.acceptState = TICKET_SENT;
WOLFSSL_MSG("accept state TICKET_SENT");
case TICKET_SENT:
2011-02-05 11:14:47 -08:00
if ( (ssl->error = SendChangeCipher(ssl)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
ssl->options.acceptState = CHANGE_CIPHER_SENT;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("accept state CHANGE_CIPHER_SENT");
2011-02-05 11:14:47 -08:00
case CHANGE_CIPHER_SENT :
2011-02-05 11:14:47 -08:00
if ( (ssl->error = SendFinished(ssl)) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
ssl->options.acceptState = ACCEPT_FINISHED_DONE;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("accept state ACCEPT_FINISHED_DONE");
2011-02-05 11:14:47 -08:00
case ACCEPT_FINISHED_DONE :
if (ssl->options.resuming)
while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE)
if ( (ssl->error = ProcessReply(ssl)) < 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_ERROR(ssl->error);
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
ssl->options.acceptState = ACCEPT_THIRD_REPLY_DONE;
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("accept state ACCEPT_THIRD_REPLY_DONE");
2011-02-05 11:14:47 -08:00
case ACCEPT_THIRD_REPLY_DONE :
#ifndef NO_HANDSHAKE_DONE_CB
if (ssl->hsDoneCb) {
int cbret = ssl->hsDoneCb(ssl, ssl->hsDoneCtx);
if (cbret < 0) {
ssl->error = cbret;
WOLFSSL_MSG("HandShake Done Cb don't continue error");
return SSL_FATAL_ERROR;
}
}
#endif /* NO_HANDSHAKE_DONE_CB */
if (!ssl->options.dtls) {
FreeHandshakeResources(ssl);
}
#ifdef WOLFSSL_DTLS
else {
ssl->options.dtlsHsRetain = 1;
}
#endif /* WOLFSSL_DTLS */
2016-05-10 13:27:45 -06:00
#ifdef WOLFSSL_SESSION_EXPORT
if (ssl->dtls_export) {
if ((ssl->error = wolfSSL_send_session(ssl)) != 0) {
WOLFSSL_MSG("Export DTLS session error");
WOLFSSL_ERROR(ssl->error);
return SSL_FATAL_ERROR;
}
}
2016-05-10 13:27:45 -06:00
#endif
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("SSL_accept()", SSL_SUCCESS);
2011-02-05 11:14:47 -08:00
return SSL_SUCCESS;
default :
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Unknown accept state ERROR");
2011-02-05 11:14:47 -08:00
return SSL_FATAL_ERROR;
}
}
2014-12-19 11:27:01 -07:00
#endif /* NO_WOLFSSL_SERVER */
2011-02-05 11:14:47 -08:00
#ifndef NO_HANDSHAKE_DONE_CB
int wolfSSL_SetHsDoneCb(WOLFSSL* ssl, HandShakeDoneCb cb, void* user_ctx)
{
WOLFSSL_ENTER("wolfSSL_SetHsDoneCb");
if (ssl == NULL)
return BAD_FUNC_ARG;
ssl->hsDoneCb = cb;
ssl->hsDoneCtx = user_ctx;
return SSL_SUCCESS;
}
#endif /* NO_HANDSHAKE_DONE_CB */
2014-12-19 11:27:01 -07:00
int wolfSSL_Cleanup(void)
2011-02-05 11:14:47 -08:00
{
int ret = SSL_SUCCESS;
2012-04-04 16:19:42 -07:00
int release = 0;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_Cleanup");
2012-04-04 16:19:42 -07:00
if (initRefCount == 0)
return ret; /* possibly no init yet, but not failure either way */
if (wc_LockMutex(&count_mutex) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad Lock Mutex count");
return BAD_MUTEX_E;
2013-02-20 15:26:22 -08:00
}
2012-04-04 16:19:42 -07:00
release = initRefCount-- == 1;
if (initRefCount < 0)
initRefCount = 0;
wc_UnLockMutex(&count_mutex);
2012-04-04 16:19:42 -07:00
if (!release)
return ret;
2011-02-05 11:14:47 -08:00
#ifndef NO_SESSION_CACHE
if (wc_FreeMutex(&session_mutex) != 0)
ret = BAD_MUTEX_E;
2011-02-05 11:14:47 -08:00
#endif
if (wc_FreeMutex(&count_mutex) != 0)
ret = BAD_MUTEX_E;
2013-09-06 17:08:57 -07:00
#if defined(HAVE_ECC) && defined(FP_ECC)
2015-01-06 13:42:02 -07:00
wc_ecc_fp_free();
2013-09-06 17:08:57 -07:00
#endif
return ret;
2011-02-05 11:14:47 -08:00
}
#ifndef NO_SESSION_CACHE
/* some session IDs aren't random after all, let's make them random */
2014-03-25 16:01:17 -07:00
static INLINE word32 HashSession(const byte* sessionID, word32 len, int* error)
{
byte digest[MAX_DIGEST_SIZE];
#ifndef NO_MD5
2015-03-27 14:28:05 -07:00
*error = wc_Md5Hash(sessionID, len, digest);
#elif !defined(NO_SHA)
2015-03-27 14:28:05 -07:00
*error = wc_ShaHash(sessionID, len, digest);
#elif !defined(NO_SHA256)
2015-03-27 14:28:05 -07:00
*error = wc_Sha256Hash(sessionID, len, digest);
#else
#error "We need a digest to hash the session IDs"
#endif
return *error == 0 ? MakeWordFromHash(digest) : 0; /* 0 on failure */
}
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
void wolfSSL_flush_sessions(WOLFSSL_CTX* ctx, long tm)
2011-02-05 11:14:47 -08:00
{
/* static table now, no flushing needed */
2011-04-27 17:31:08 -07:00
(void)ctx;
(void)tm;
2011-02-05 11:14:47 -08:00
}
/* set ssl session timeout in seconds */
2014-12-19 11:27:01 -07:00
int wolfSSL_set_timeout(WOLFSSL* ssl, unsigned int to)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
ssl->timeout = to;
return SSL_SUCCESS;
}
/* set ctx session timeout in seconds */
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_set_timeout(WOLFSSL_CTX* ctx, unsigned int to)
{
if (ctx == NULL)
return BAD_FUNC_ARG;
ctx->timeout = to;
return SSL_SUCCESS;
}
#ifndef NO_CLIENT_CACHE
/* Get Session from Client cache based on id/len, return NULL on failure */
2014-12-19 11:27:01 -07:00
WOLFSSL_SESSION* GetSessionClient(WOLFSSL* ssl, const byte* id, int len)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_SESSION* ret = NULL;
word32 row;
int idx;
int count;
2014-03-25 16:01:17 -07:00
int error = 0;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("GetSessionClient");
2014-12-19 11:27:01 -07:00
if (ssl->options.side == WOLFSSL_SERVER_END)
return NULL;
len = min(SERVER_ID_LEN, (word32)len);
2014-03-25 16:01:17 -07:00
row = HashSession(id, len, &error) % SESSION_ROWS;
if (error != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Hash session failed");
2014-03-25 16:01:17 -07:00
return NULL;
}
if (wc_LockMutex(&session_mutex) != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Lock session mutex failed");
return NULL;
}
/* start from most recently used */
count = min((word32)ClientCache[row].totalCount, SESSIONS_PER_ROW);
idx = ClientCache[row].nextIdx - 1;
if (idx < 0)
idx = SESSIONS_PER_ROW - 1; /* if back to front, the previous was end */
for (; count > 0; --count, idx = idx ? idx - 1 : SESSIONS_PER_ROW - 1) {
2014-12-19 11:27:01 -07:00
WOLFSSL_SESSION* current;
ClientSession clSess;
if (idx >= SESSIONS_PER_ROW || idx < 0) { /* sanity check */
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad idx");
break;
}
clSess = ClientCache[row].Clients[idx];
current = &SessionCache[clSess.serverRow].Sessions[clSess.serverIdx];
if (XMEMCMP(current->serverID, id, len) == 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Found a serverid match for client");
if (LowResTimer() < (current->bornOn + current->timeout)) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Session valid");
ret = current;
break;
} else {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Session timed out"); /* could have more for id */
}
} else {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("ServerID not a match from client table");
}
}
wc_UnLockMutex(&session_mutex);
return ret;
}
#endif /* NO_CLIENT_CACHE */
WOLFSSL_SESSION* GetSession(WOLFSSL* ssl, byte* masterSecret,
byte restoreSessionCerts)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_SESSION* ret = 0;
2012-09-14 21:19:06 -07:00
const byte* id = NULL;
2011-02-05 11:14:47 -08:00
word32 row;
int idx;
int count;
2014-03-25 16:01:17 -07:00
int error = 0;
(void) restoreSessionCerts;
2011-02-05 11:14:47 -08:00
if (ssl->options.sessionCacheOff)
2012-05-17 10:55:42 -07:00
return NULL;
if (ssl->options.haveSessionId == 0)
return NULL;
2011-02-05 11:14:47 -08:00
#ifdef HAVE_SESSION_TICKET
if (ssl->options.side == WOLFSSL_SERVER_END && ssl->options.useTicket == 1)
return NULL;
#endif
2012-09-14 21:19:06 -07:00
if (ssl->arrays)
id = ssl->arrays->sessionID;
else
id = ssl->session.sessionID;
2014-03-25 16:01:17 -07:00
row = HashSession(id, ID_LEN, &error) % SESSION_ROWS;
if (error != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Hash session failed");
2014-03-25 16:01:17 -07:00
return NULL;
}
2011-02-05 11:14:47 -08:00
if (wc_LockMutex(&session_mutex) != 0)
2011-02-05 11:14:47 -08:00
return 0;
/* start from most recently used */
count = min((word32)SessionCache[row].totalCount, SESSIONS_PER_ROW);
idx = SessionCache[row].nextIdx - 1;
if (idx < 0)
idx = SESSIONS_PER_ROW - 1; /* if back to front, the previous was end */
2011-02-05 11:14:47 -08:00
for (; count > 0; --count, idx = idx ? idx - 1 : SESSIONS_PER_ROW - 1) {
2014-12-19 11:27:01 -07:00
WOLFSSL_SESSION* current;
if (idx >= SESSIONS_PER_ROW || idx < 0) { /* sanity check */
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad idx");
break;
}
2011-02-05 11:14:47 -08:00
current = &SessionCache[row].Sessions[idx];
if (XMEMCMP(current->sessionID, id, ID_LEN) == 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Found a session match");
2011-02-05 11:14:47 -08:00
if (LowResTimer() < (current->bornOn + current->timeout)) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Session valid");
2011-02-05 11:14:47 -08:00
ret = current;
if (masterSecret)
XMEMCPY(masterSecret, current->masterSecret, SECRET_LEN);
#ifdef SESSION_CERTS
/* If set, we should copy the session certs into the ssl object
* from the session we are returning so we can resume */
if (restoreSessionCerts) {
ssl->session.chain = ret->chain;
ssl->session.version = ret->version;
ssl->session.cipherSuite0 = ret->cipherSuite0;
ssl->session.cipherSuite = ret->cipherSuite;
}
#endif /* SESSION_CERTS */
} else {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Session timed out");
2011-02-05 11:14:47 -08:00
}
break; /* no more sessionIDs whether valid or not that match */
} else {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("SessionID not a match at this idx");
}
2011-02-05 11:14:47 -08:00
}
wc_UnLockMutex(&session_mutex);
2011-02-05 11:14:47 -08:00
return ret;
}
static int GetDeepCopySession(WOLFSSL* ssl, WOLFSSL_SESSION* copyFrom)
{
WOLFSSL_SESSION* copyInto = &ssl->session;
void* tmpBuff = NULL;
int ticketLen = 0;
2016-04-25 10:32:41 -06:00
int doDynamicCopy = 0;
int ret = SSL_SUCCESS;
(void)ticketLen;
(void)doDynamicCopy;
(void)tmpBuff;
if (!ssl || !copyFrom)
return BAD_FUNC_ARG;
#ifdef HAVE_SESSION_TICKET
/* Free old dynamic ticket if we had one to avoid leak */
if (copyInto->isDynamic) {
2016-05-25 09:47:54 -07:00
XFREE(copyInto->ticket, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
copyInto->ticket = copyInto->staticTicket;
copyInto->isDynamic = 0;
}
#endif
if (wc_LockMutex(&session_mutex) != 0)
return BAD_MUTEX_E;
#ifdef HAVE_SESSION_TICKET
/* Size of ticket to alloc if needed; Use later for alloc outside lock */
2016-04-25 10:32:41 -06:00
doDynamicCopy = copyFrom->isDynamic;
ticketLen = copyFrom->ticketLen;
#endif
*copyInto = *copyFrom;
/* Default ticket to non dynamic. This will avoid crash if we fail below */
#ifdef HAVE_SESSION_TICKET
copyInto->ticket = copyInto->staticTicket;
copyInto->isDynamic = 0;
#endif
if (wc_UnLockMutex(&session_mutex) != 0) {
return BAD_MUTEX_E;
}
#ifdef HAVE_SESSION_TICKET
/* If doing dynamic copy, need to alloc outside lock, then inside a lock
* confirm the size still matches and memcpy */
if (doDynamicCopy) {
tmpBuff = XMALLOC(ticketLen, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
if (!tmpBuff)
return MEMORY_ERROR;
if (wc_LockMutex(&session_mutex) != 0) {
2016-05-25 09:47:54 -07:00
XFREE(tmpBuff, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
return BAD_MUTEX_E;
}
if (ticketLen != copyFrom->ticketLen) {
/* Another thread modified the ssl-> session ticket during alloc.
* Treat as error, since ticket different than when copy requested */
ret = VAR_STATE_CHANGE_E;
}
if (ret == SSL_SUCCESS) {
copyInto->ticket = tmpBuff;
copyInto->isDynamic = 1;
XMEMCPY(copyInto->ticket, copyFrom->ticket, ticketLen);
}
} else {
/* Need to ensure ticket pointer gets updated to own buffer
* and is not pointing to buff of session copied from */
copyInto->ticket = copyInto->staticTicket;
}
if (wc_UnLockMutex(&session_mutex) != 0) {
if (ret == SSL_SUCCESS)
ret = BAD_MUTEX_E;
}
if (ret != SSL_SUCCESS) {
/* cleanup */
if (tmpBuff)
2016-05-25 09:47:54 -07:00
XFREE(tmpBuff, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
copyInto->ticket = copyInto->staticTicket;
copyInto->isDynamic = 0;
}
#endif /* HAVE_SESSION_TICKET */
return ret;
}
2014-12-19 11:27:01 -07:00
int SetSession(WOLFSSL* ssl, WOLFSSL_SESSION* session)
2011-02-05 11:14:47 -08:00
{
if (ssl->options.sessionCacheOff)
return SSL_FAILURE;
if (LowResTimer() < (session->bornOn + session->timeout)) {
int ret = GetDeepCopySession(ssl, session);
if (ret == SSL_SUCCESS) {
ssl->options.resuming = 1;
2011-02-05 11:14:47 -08:00
#ifdef SESSION_CERTS
ssl->version = session->version;
ssl->options.cipherSuite0 = session->cipherSuite0;
ssl->options.cipherSuite = session->cipherSuite;
2011-02-05 11:14:47 -08:00
#endif
}
2011-02-05 11:14:47 -08:00
return ret;
2011-02-05 11:14:47 -08:00
}
return SSL_FAILURE; /* session timed out */
}
#ifdef WOLFSSL_SESSION_STATS
static int get_locked_session_stats(word32* active, word32* total,
word32* peak);
#endif
2014-12-19 11:27:01 -07:00
int AddSession(WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
word32 row, idx;
2014-03-25 16:01:17 -07:00
int error = 0;
#ifdef HAVE_SESSION_TICKET
byte* tmpBuff = NULL;
int ticLen = 0;
#endif
2011-02-05 11:14:47 -08:00
if (ssl->options.sessionCacheOff)
return 0;
2012-05-17 10:55:42 -07:00
if (ssl->options.haveSessionId == 0)
return 0;
#ifdef HAVE_SESSION_TICKET
if (ssl->options.side == WOLFSSL_SERVER_END && ssl->options.useTicket == 1)
return 0;
#endif
2014-03-25 16:01:17 -07:00
row = HashSession(ssl->arrays->sessionID, ID_LEN, &error) % SESSION_ROWS;
if (error != 0) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Hash session failed");
2014-03-25 16:01:17 -07:00
return error;
}
2011-02-05 11:14:47 -08:00
#ifdef HAVE_SESSION_TICKET
ticLen = ssl->session.ticketLen;
/* Alloc Memory here so if Malloc fails can exit outside of lock */
if(ticLen > SESSION_TICKET_LEN) {
tmpBuff = XMALLOC(ticLen, ssl->heap,
DYNAMIC_TYPE_SESSION_TICK);
if(!tmpBuff)
return MEMORY_E;
}
#endif
if (wc_LockMutex(&session_mutex) != 0) {
#ifdef HAVE_SESSION_TICKET
XFREE(tmpBuff, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
#endif
return BAD_MUTEX_E;
}
2011-02-05 11:14:47 -08:00
idx = SessionCache[row].nextIdx++;
#ifdef SESSION_INDEX
ssl->sessionIndex = (row << SESSIDX_ROW_SHIFT) | idx;
#endif
2011-02-05 11:14:47 -08:00
XMEMCPY(SessionCache[row].Sessions[idx].masterSecret,
2012-09-14 21:19:06 -07:00
ssl->arrays->masterSecret, SECRET_LEN);
2016-09-01 15:12:54 -06:00
SessionCache[row].Sessions[idx].haveEMS = ssl->options.haveEMS;
2012-09-14 21:19:06 -07:00
XMEMCPY(SessionCache[row].Sessions[idx].sessionID, ssl->arrays->sessionID,
2011-02-05 11:14:47 -08:00
ID_LEN);
SessionCache[row].Sessions[idx].sessionIDSz = ssl->arrays->sessionIDSz;
2011-02-05 11:14:47 -08:00
SessionCache[row].Sessions[idx].timeout = ssl->timeout;
2011-02-05 11:14:47 -08:00
SessionCache[row].Sessions[idx].bornOn = LowResTimer();
#ifdef HAVE_SESSION_TICKET
/* Check if another thread modified ticket since alloc */
if (ticLen != ssl->session.ticketLen) {
error = VAR_STATE_CHANGE_E;
}
if (error == 0) {
/* Cleanup cache row's old Dynamic buff if exists */
if(SessionCache[row].Sessions[idx].isDynamic) {
XFREE(SessionCache[row].Sessions[idx].ticket,
2016-05-25 09:47:54 -07:00
ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
SessionCache[row].Sessions[idx].ticket = NULL;
}
/* If too large to store in static buffer, use dyn buffer */
if (ticLen > SESSION_TICKET_LEN) {
SessionCache[row].Sessions[idx].ticket = tmpBuff;
SessionCache[row].Sessions[idx].isDynamic = 1;
} else {
SessionCache[row].Sessions[idx].ticket =
SessionCache[row].Sessions[idx].staticTicket;
SessionCache[row].Sessions[idx].isDynamic = 0;
}
}
if (error == 0) {
SessionCache[row].Sessions[idx].ticketLen = ticLen;
XMEMCPY(SessionCache[row].Sessions[idx].ticket,
ssl->session.ticket, ticLen);
} else { /* cleanup, reset state */
SessionCache[row].Sessions[idx].ticket =
SessionCache[row].Sessions[idx].staticTicket;
SessionCache[row].Sessions[idx].isDynamic = 0;
SessionCache[row].Sessions[idx].ticketLen = 0;
if (tmpBuff) {
XFREE(tmpBuff, ssl->heap, DYNAMIC_TYPE_SESSION_TICK);
tmpBuff = NULL;
}
}
#endif
2011-02-05 11:14:47 -08:00
#ifdef SESSION_CERTS
if (error == 0) {
SessionCache[row].Sessions[idx].chain.count = ssl->session.chain.count;
XMEMCPY(SessionCache[row].Sessions[idx].chain.certs,
ssl->session.chain.certs, sizeof(x509_buffer) * MAX_CHAIN_DEPTH);
2011-02-05 11:14:47 -08:00
SessionCache[row].Sessions[idx].version = ssl->version;
SessionCache[row].Sessions[idx].cipherSuite0 = ssl->options.cipherSuite0;
SessionCache[row].Sessions[idx].cipherSuite = ssl->options.cipherSuite;
}
2013-04-29 14:52:28 -07:00
#endif /* SESSION_CERTS */
if (error == 0) {
SessionCache[row].totalCount++;
if (SessionCache[row].nextIdx == SESSIONS_PER_ROW)
SessionCache[row].nextIdx = 0;
}
#ifndef NO_CLIENT_CACHE
if (error == 0) {
if (ssl->options.side == WOLFSSL_CLIENT_END && ssl->session.idLen) {
word32 clientRow, clientIdx;
WOLFSSL_MSG("Adding client cache entry");
SessionCache[row].Sessions[idx].idLen = ssl->session.idLen;
XMEMCPY(SessionCache[row].Sessions[idx].serverID,
ssl->session.serverID, ssl->session.idLen);
clientRow = HashSession(ssl->session.serverID, ssl->session.idLen,
&error) % SESSION_ROWS;
if (error != 0) {
WOLFSSL_MSG("Hash session failed");
} else {
clientIdx = ClientCache[clientRow].nextIdx++;
ClientCache[clientRow].Clients[clientIdx].serverRow =
(word16)row;
ClientCache[clientRow].Clients[clientIdx].serverIdx =
(word16)idx;
ClientCache[clientRow].totalCount++;
if (ClientCache[clientRow].nextIdx == SESSIONS_PER_ROW)
ClientCache[clientRow].nextIdx = 0;
}
}
else
SessionCache[row].Sessions[idx].idLen = 0;
}
2013-04-29 14:52:28 -07:00
#endif /* NO_CLIENT_CACHE */
#if defined(WOLFSSL_SESSION_STATS) && defined(WOLFSSL_PEAK_SESSIONS)
if (error == 0) {
word32 active = 0;
error = get_locked_session_stats(&active, NULL, NULL);
if (error == SSL_SUCCESS) {
error = 0; /* back to this function ok */
if (active > PeakSessions)
PeakSessions = active;
}
}
#endif /* defined(WOLFSSL_SESSION_STATS) && defined(WOLFSSL_PEAK_SESSIONS) */
if (wc_UnLockMutex(&session_mutex) != 0)
return BAD_MUTEX_E;
2011-02-05 11:14:47 -08:00
return error;
2011-02-05 11:14:47 -08:00
}
#ifdef SESSION_INDEX
2014-12-19 11:27:01 -07:00
int wolfSSL_GetSessionIndex(WOLFSSL* ssl)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_GetSessionIndex");
WOLFSSL_LEAVE("wolfSSL_GetSessionIndex", ssl->sessionIndex);
return ssl->sessionIndex;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_GetSessionAtIndex(int idx, WOLFSSL_SESSION* session)
{
int row, col, result = SSL_FAILURE;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_GetSessionAtIndex");
row = idx >> SESSIDX_ROW_SHIFT;
col = idx & SESSIDX_IDX_MASK;
if (wc_LockMutex(&session_mutex) != 0) {
return BAD_MUTEX_E;
}
if (row < SESSION_ROWS &&
col < (int)min(SessionCache[row].totalCount, SESSIONS_PER_ROW)) {
XMEMCPY(session,
2014-12-19 11:27:01 -07:00
&SessionCache[row].Sessions[col], sizeof(WOLFSSL_SESSION));
result = SSL_SUCCESS;
}
if (wc_UnLockMutex(&session_mutex) != 0)
result = BAD_MUTEX_E;
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("wolfSSL_GetSessionAtIndex", result);
return result;
}
#endif /* SESSION_INDEX */
#if defined(SESSION_INDEX) && defined(SESSION_CERTS)
2014-12-19 11:27:01 -07:00
WOLFSSL_X509_CHAIN* wolfSSL_SESSION_get_peer_chain(WOLFSSL_SESSION* session)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_X509_CHAIN* chain = NULL;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_SESSION_get_peer_chain");
if (session)
chain = &session->chain;
2014-12-19 11:27:01 -07:00
WOLFSSL_LEAVE("wolfSSL_SESSION_get_peer_chain", chain ? 1 : 0);
return chain;
}
#endif /* SESSION_INDEX && SESSION_CERTS */
#ifdef WOLFSSL_SESSION_STATS
/* requires session_mutex lock held, SSL_SUCCESS on ok */
static int get_locked_session_stats(word32* active, word32* total, word32* peak)
{
int result = SSL_SUCCESS;
int i;
int count;
int idx;
word32 now = 0;
word32 seen = 0;
word32 ticks = LowResTimer();
(void)peak;
WOLFSSL_ENTER("get_locked_session_stats");
for (i = 0; i < SESSION_ROWS; i++) {
seen += SessionCache[i].totalCount;
if (active == NULL)
continue; /* no need to calculate what we can't set */
count = min((word32)SessionCache[i].totalCount, SESSIONS_PER_ROW);
idx = SessionCache[i].nextIdx - 1;
if (idx < 0)
idx = SESSIONS_PER_ROW - 1; /* if back to front previous was end */
for (; count > 0; --count, idx = idx ? idx - 1 : SESSIONS_PER_ROW - 1) {
if (idx >= SESSIONS_PER_ROW || idx < 0) { /* sanity check */
WOLFSSL_MSG("Bad idx");
break;
}
/* if not expried then good */
if (ticks < (SessionCache[i].Sessions[idx].bornOn +
SessionCache[i].Sessions[idx].timeout) ) {
now++;
}
}
}
if (active)
*active = now;
if (total)
*total = seen;
#ifdef WOLFSSL_PEAK_SESSIONS
if (peak)
*peak = PeakSessions;
#endif
WOLFSSL_LEAVE("get_locked_session_stats", result);
return result;
}
/* return SSL_SUCCESS on ok */
int wolfSSL_get_session_stats(word32* active, word32* total, word32* peak,
word32* maxSessions)
{
int result = SSL_SUCCESS;
WOLFSSL_ENTER("wolfSSL_get_session_stats");
if (maxSessions) {
*maxSessions = SESSIONS_PER_ROW * SESSION_ROWS;
if (active == NULL && total == NULL && peak == NULL)
return result; /* we're done */
}
/* user must provide at least one query value */
if (active == NULL && total == NULL && peak == NULL)
return BAD_FUNC_ARG;
if (wc_LockMutex(&session_mutex) != 0) {
return BAD_MUTEX_E;
}
result = get_locked_session_stats(active, total, peak);
if (wc_UnLockMutex(&session_mutex) != 0)
result = BAD_MUTEX_E;
WOLFSSL_LEAVE("wolfSSL_get_session_stats", result);
return result;
}
#endif /* WOLFSSL_SESSION_STATS */
#ifdef PRINT_SESSION_STATS
2011-02-05 11:14:47 -08:00
/* SSL_SUCCESS on ok */
int wolfSSL_PrintSessionStats(void)
2011-02-05 11:14:47 -08:00
{
word32 totalSessionsSeen = 0;
word32 totalSessionsNow = 0;
word32 peak = 0;
word32 maxSessions = 0;
2011-02-05 11:14:47 -08:00
int i;
int ret;
2011-02-05 11:14:47 -08:00
double E; /* expected freq */
double chiSquare = 0;
ret = wolfSSL_get_session_stats(&totalSessionsNow, &totalSessionsSeen,
&peak, &maxSessions);
if (ret != SSL_SUCCESS)
return ret;
2011-02-05 11:14:47 -08:00
printf("Total Sessions Seen = %d\n", totalSessionsSeen);
printf("Total Sessions Now = %d\n", totalSessionsNow);
#ifdef WOLFSSL_PEAK_SESSIONS
printf("Peak Sessions = %d\n", peak);
#endif
printf("Max Sessions = %d\n", maxSessions);
2011-02-05 11:14:47 -08:00
E = (double)totalSessionsSeen / SESSION_ROWS;
for (i = 0; i < SESSION_ROWS; i++) {
double diff = SessionCache[i].totalCount - E;
diff *= diff; /* square */
2011-02-05 11:14:47 -08:00
diff /= E; /* normalize */
chiSquare += diff;
}
printf(" chi-square = %5.1f, d.f. = %d\n", chiSquare,
SESSION_ROWS - 1);
#if (SESSION_ROWS == 11)
2011-02-05 11:14:47 -08:00
printf(" .05 p value = 18.3, chi-square should be less\n");
#elif (SESSION_ROWS == 211)
2011-02-05 11:14:47 -08:00
printf(".05 p value = 244.8, chi-square should be less\n");
#elif (SESSION_ROWS == 5981)
2011-02-05 11:14:47 -08:00
printf(".05 p value = 6161.0, chi-square should be less\n");
#elif (SESSION_ROWS == 3)
printf(".05 p value = 6.0, chi-square should be less\n");
#elif (SESSION_ROWS == 2861)
printf(".05 p value = 2985.5, chi-square should be less\n");
#endif
2011-02-05 11:14:47 -08:00
printf("\n");
return ret;
2011-02-05 11:14:47 -08:00
}
#endif /* SESSION_STATS */
2011-08-11 13:23:49 -07:00
#else /* NO_SESSION_CACHE */
/* No session cache version */
WOLFSSL_SESSION* GetSession(WOLFSSL* ssl, byte* masterSecret,
byte restoreSessionCerts)
2011-08-11 13:23:49 -07:00
{
2012-11-27 15:39:40 -08:00
(void)ssl;
(void)masterSecret;
(void)restoreSessionCerts;
2012-11-27 15:39:40 -08:00
return NULL;
2011-08-11 13:23:49 -07:00
}
2011-02-05 11:14:47 -08:00
#endif /* NO_SESSION_CACHE */
/* call before SSL_connect, if verifying will add name check to
date check and signature check */
2014-12-19 11:27:01 -07:00
int wolfSSL_check_domain_name(WOLFSSL* ssl, const char* dn)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_check_domain_name");
2011-02-05 11:14:47 -08:00
if (ssl->buffers.domainName.buffer)
XFREE(ssl->buffers.domainName.buffer, ssl->heap, DYNAMIC_TYPE_DOMAIN);
ssl->buffers.domainName.length = (word32)XSTRLEN(dn) + 1;
ssl->buffers.domainName.buffer = (byte*) XMALLOC(
ssl->buffers.domainName.length, ssl->heap, DYNAMIC_TYPE_DOMAIN);
if (ssl->buffers.domainName.buffer) {
XSTRNCPY((char*)ssl->buffers.domainName.buffer, dn,
ssl->buffers.domainName.length);
return SSL_SUCCESS;
}
else {
ssl->error = MEMORY_ERROR;
return SSL_FAILURE;
}
}
2014-12-19 11:27:01 -07:00
/* turn on wolfSSL zlib compression
returns SSL_SUCCESS for success, else error (not built in)
2011-02-05 11:14:47 -08:00
*/
2014-12-19 11:27:01 -07:00
int wolfSSL_set_compression(WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_set_compression");
2011-04-27 17:31:08 -07:00
(void)ssl;
2011-02-05 11:14:47 -08:00
#ifdef HAVE_LIBZ
ssl->options.usingCompression = 1;
return SSL_SUCCESS;
2011-02-05 11:14:47 -08:00
#else
return NOT_COMPILED_IN;
2011-02-05 11:14:47 -08:00
#endif
}
#ifndef USE_WINDOWS_API
2011-02-05 11:14:47 -08:00
#ifndef NO_WRITEV
/* simulate writev semantics, doesn't actually do block at a time though
because of SSL_write behavior and because front adds may be small */
2014-12-19 11:27:01 -07:00
int wolfSSL_writev(WOLFSSL* ssl, const struct iovec* iov, int iovcnt)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
byte staticBuffer[1]; /* force heap usage */
2014-09-08 21:34:07 -03:00
#else
byte staticBuffer[FILE_BUFFER_SIZE];
2014-09-08 21:34:07 -03:00
#endif
byte* myBuffer = staticBuffer;
int dynamic = 0;
2013-01-29 16:22:49 -08:00
int sending = 0;
2011-02-05 11:14:47 -08:00
int idx = 0;
int i;
int ret;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_writev");
2011-04-25 09:24:21 -07:00
2011-02-05 11:14:47 -08:00
for (i = 0; i < iovcnt; i++)
2013-01-29 16:22:49 -08:00
sending += (int)iov[i].iov_len;
2011-02-05 11:14:47 -08:00
if (sending > (int)sizeof(staticBuffer)) {
2014-09-08 21:34:07 -03:00
myBuffer = (byte*)XMALLOC(sending, ssl->heap,
DYNAMIC_TYPE_WRITEV);
if (!myBuffer)
2011-02-05 11:14:47 -08:00
return MEMORY_ERROR;
dynamic = 1;
2011-02-05 11:14:47 -08:00
}
for (i = 0; i < iovcnt; i++) {
2011-05-25 10:25:05 -07:00
XMEMCPY(&myBuffer[idx], iov[i].iov_base, iov[i].iov_len);
idx += (int)iov[i].iov_len;
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
ret = wolfSSL_write(ssl, myBuffer, sending);
2011-02-05 11:14:47 -08:00
if (dynamic)
XFREE(myBuffer, ssl->heap, DYNAMIC_TYPE_WRITEV);
2011-02-05 11:14:47 -08:00
return ret;
}
#endif
#endif
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_CALLBACKS
2011-02-05 11:14:47 -08:00
typedef struct itimerval Itimerval;
/* don't keep calling simple functions while setting up timer and signals
2011-02-05 11:14:47 -08:00
if no inlining these are the next best */
#define AddTimes(a, b, c) \
do { \
c.tv_sec = a.tv_sec + b.tv_sec; \
c.tv_usec = a.tv_usec + b.tv_usec; \
if (c.tv_usec >= 1000000) { \
2011-02-05 11:14:47 -08:00
c.tv_sec++; \
c.tv_usec -= 1000000; \
} \
} while (0)
#define SubtractTimes(a, b, c) \
do { \
c.tv_sec = a.tv_sec - b.tv_sec; \
c.tv_usec = a.tv_usec - b.tv_usec; \
if (c.tv_usec < 0) { \
2011-02-05 11:14:47 -08:00
c.tv_sec--; \
c.tv_usec += 1000000; \
} \
} while (0)
#define CmpTimes(a, b, cmp) \
((a.tv_sec == b.tv_sec) ? \
(a.tv_usec cmp b.tv_usec) : \
(a.tv_sec cmp b.tv_sec)) \
/* do nothing handler */
static void myHandler(int signo)
{
(void)signo;
2011-02-05 11:14:47 -08:00
return;
}
2014-12-19 11:27:01 -07:00
static int wolfSSL_ex_wrapper(WOLFSSL* ssl, HandShakeCallBack hsCb,
2011-02-05 11:14:47 -08:00
TimeoutCallBack toCb, Timeval timeout)
{
int ret = SSL_FATAL_ERROR;
2011-02-05 11:14:47 -08:00
int oldTimerOn = 0; /* was timer already on */
Timeval startTime;
Timeval endTime;
Timeval totalTime;
Itimerval myTimeout;
Itimerval oldTimeout; /* if old timer adjust from total time to reset */
struct sigaction act, oact;
2011-02-05 11:14:47 -08:00
#define ERR_OUT(x) { ssl->hsInfoOn = 0; ssl->toInfoOn = 0; return x; }
if (hsCb) {
ssl->hsInfoOn = 1;
InitHandShakeInfo(&ssl->handShakeInfo, ssl);
2011-02-05 11:14:47 -08:00
}
if (toCb) {
ssl->toInfoOn = 1;
InitTimeoutInfo(&ssl->timeoutInfo);
2011-02-05 11:14:47 -08:00
if (gettimeofday(&startTime, 0) < 0)
ERR_OUT(GETTIME_ERROR);
/* use setitimer to simulate getitimer, init 0 myTimeout */
myTimeout.it_interval.tv_sec = 0;
myTimeout.it_interval.tv_usec = 0;
myTimeout.it_value.tv_sec = 0;
myTimeout.it_value.tv_usec = 0;
if (setitimer(ITIMER_REAL, &myTimeout, &oldTimeout) < 0)
ERR_OUT(SETITIMER_ERROR);
if (oldTimeout.it_value.tv_sec || oldTimeout.it_value.tv_usec) {
oldTimerOn = 1;
2011-02-05 11:14:47 -08:00
/* is old timer going to expire before ours */
if (CmpTimes(oldTimeout.it_value, timeout, <)) {
2011-02-05 11:14:47 -08:00
timeout.tv_sec = oldTimeout.it_value.tv_sec;
timeout.tv_usec = oldTimeout.it_value.tv_usec;
}
2011-02-05 11:14:47 -08:00
}
myTimeout.it_value.tv_sec = timeout.tv_sec;
myTimeout.it_value.tv_usec = timeout.tv_usec;
2011-02-05 11:14:47 -08:00
/* set up signal handler, don't restart socket send/recv */
act.sa_handler = myHandler;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
#ifdef SA_INTERRUPT
act.sa_flags |= SA_INTERRUPT;
#endif
if (sigaction(SIGALRM, &act, &oact) < 0)
ERR_OUT(SIGACT_ERROR);
if (setitimer(ITIMER_REAL, &myTimeout, 0) < 0)
ERR_OUT(SETITIMER_ERROR);
}
/* do main work */
2014-12-19 11:27:01 -07:00
#ifndef NO_WOLFSSL_CLIENT
if (ssl->options.side == WOLFSSL_CLIENT_END)
ret = wolfSSL_connect(ssl);
2011-02-05 11:14:47 -08:00
#endif
2014-12-19 11:27:01 -07:00
#ifndef NO_WOLFSSL_SERVER
if (ssl->options.side == WOLFSSL_SERVER_END)
ret = wolfSSL_accept(ssl);
2011-02-05 11:14:47 -08:00
#endif
/* do callbacks */
2011-02-05 11:14:47 -08:00
if (toCb) {
if (oldTimerOn) {
gettimeofday(&endTime, 0);
SubtractTimes(endTime, startTime, totalTime);
/* adjust old timer for elapsed time */
if (CmpTimes(totalTime, oldTimeout.it_value, <))
SubtractTimes(oldTimeout.it_value, totalTime,
oldTimeout.it_value);
else {
/* reset value to interval, may be off */
oldTimeout.it_value.tv_sec = oldTimeout.it_interval.tv_sec;
oldTimeout.it_value.tv_usec =oldTimeout.it_interval.tv_usec;
}
/* keep iter the same whether there or not */
}
/* restore old handler */
if (sigaction(SIGALRM, &oact, 0) < 0)
ret = SIGACT_ERROR; /* more pressing error, stomp */
else
/* use old settings which may turn off (expired or not there) */
if (setitimer(ITIMER_REAL, &oldTimeout, 0) < 0)
ret = SETITIMER_ERROR;
2011-02-05 11:14:47 -08:00
/* if we had a timeout call callback */
if (ssl->timeoutInfo.timeoutName[0]) {
ssl->timeoutInfo.timeoutValue.tv_sec = timeout.tv_sec;
ssl->timeoutInfo.timeoutValue.tv_usec = timeout.tv_usec;
(toCb)(&ssl->timeoutInfo);
}
/* clean up */
FreeTimeoutInfo(&ssl->timeoutInfo, ssl->heap);
ssl->toInfoOn = 0;
}
if (hsCb) {
FinishHandShakeInfo(&ssl->handShakeInfo);
2011-02-05 11:14:47 -08:00
(hsCb)(&ssl->handShakeInfo);
ssl->hsInfoOn = 0;
}
return ret;
}
2014-12-19 11:27:01 -07:00
#ifndef NO_WOLFSSL_CLIENT
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
int wolfSSL_connect_ex(WOLFSSL* ssl, HandShakeCallBack hsCb,
2011-02-05 11:14:47 -08:00
TimeoutCallBack toCb, Timeval timeout)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_connect_ex");
return wolfSSL_ex_wrapper(ssl, hsCb, toCb, timeout);
2011-02-05 11:14:47 -08:00
}
#endif
2014-12-19 11:27:01 -07:00
#ifndef NO_WOLFSSL_SERVER
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
int wolfSSL_accept_ex(WOLFSSL* ssl, HandShakeCallBack hsCb,
2011-02-05 11:14:47 -08:00
TimeoutCallBack toCb,Timeval timeout)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_accept_ex");
return wolfSSL_ex_wrapper(ssl, hsCb, toCb, timeout);
2011-02-05 11:14:47 -08:00
}
#endif
2014-12-19 11:27:01 -07:00
#endif /* WOLFSSL_CALLBACKS */
2011-02-05 11:14:47 -08:00
#ifndef NO_PSK
2014-12-19 11:27:01 -07:00
void wolfSSL_CTX_set_psk_client_callback(WOLFSSL_CTX* ctx,
wc_psk_client_callback cb)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_CTX_set_psk_client_callback");
2011-02-05 11:14:47 -08:00
ctx->havePSK = 1;
ctx->client_psk_cb = cb;
}
void wolfSSL_set_psk_client_callback(WOLFSSL* ssl,wc_psk_client_callback cb)
2011-02-05 11:14:47 -08:00
{
2012-10-29 15:39:42 -07:00
byte haveRSA = 1;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_set_psk_client_callback");
2011-02-05 11:14:47 -08:00
ssl->options.havePSK = 1;
ssl->options.client_psk_cb = cb;
2012-10-29 15:39:42 -07:00
#ifdef NO_RSA
haveRSA = 0;
#endif
InitSuites(ssl->suites, ssl->version, haveRSA, TRUE,
ssl->options.haveDH, ssl->options.haveNTRU,
2016-02-10 11:02:09 -07:00
ssl->options.haveECDSAsig, ssl->options.haveECC,
ssl->options.haveStaticECC, ssl->options.side);
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
void wolfSSL_CTX_set_psk_server_callback(WOLFSSL_CTX* ctx,
wc_psk_server_callback cb)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_CTX_set_psk_server_callback");
2011-02-05 11:14:47 -08:00
ctx->havePSK = 1;
ctx->server_psk_cb = cb;
}
void wolfSSL_set_psk_server_callback(WOLFSSL* ssl,wc_psk_server_callback cb)
2011-02-05 11:14:47 -08:00
{
2012-10-29 15:39:42 -07:00
byte haveRSA = 1;
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_set_psk_server_callback");
2011-02-05 11:14:47 -08:00
ssl->options.havePSK = 1;
ssl->options.server_psk_cb = cb;
2012-10-29 15:39:42 -07:00
#ifdef NO_RSA
haveRSA = 0;
#endif
InitSuites(ssl->suites, ssl->version, haveRSA, TRUE,
ssl->options.haveDH, ssl->options.haveNTRU,
2016-02-10 11:02:09 -07:00
ssl->options.haveECDSAsig, ssl->options.haveECC,
ssl->options.haveStaticECC, ssl->options.side);
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
const char* wolfSSL_get_psk_identity_hint(const WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_get_psk_identity_hint");
2012-09-14 21:19:06 -07:00
if (ssl == NULL || ssl->arrays == NULL)
return NULL;
return ssl->arrays->server_hint;
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
const char* wolfSSL_get_psk_identity(const WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_get_psk_identity");
2012-09-14 21:19:06 -07:00
if (ssl == NULL || ssl->arrays == NULL)
return NULL;
return ssl->arrays->client_identity;
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_use_psk_identity_hint(WOLFSSL_CTX* ctx, const char* hint)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_CTX_use_psk_identity_hint");
2011-02-05 11:14:47 -08:00
if (hint == 0)
ctx->server_hint[0] = 0;
else {
2011-02-05 11:14:47 -08:00
XSTRNCPY(ctx->server_hint, hint, MAX_PSK_ID_LEN);
ctx->server_hint[MAX_PSK_ID_LEN - 1] = '\0';
}
2011-02-05 11:14:47 -08:00
return SSL_SUCCESS;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_use_psk_identity_hint(WOLFSSL* ssl, const char* hint)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_use_psk_identity_hint");
2012-09-14 21:19:06 -07:00
if (ssl == NULL || ssl->arrays == NULL)
return SSL_FAILURE;
2011-02-05 11:14:47 -08:00
if (hint == 0)
2012-09-14 21:19:06 -07:00
ssl->arrays->server_hint[0] = 0;
else {
2012-09-14 21:19:06 -07:00
XSTRNCPY(ssl->arrays->server_hint, hint, MAX_PSK_ID_LEN);
ssl->arrays->server_hint[MAX_PSK_ID_LEN - 1] = '\0';
}
2011-02-05 11:14:47 -08:00
return SSL_SUCCESS;
}
#endif /* NO_PSK */
#ifdef HAVE_ANON
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_allow_anon_cipher(WOLFSSL_CTX* ctx)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_allow_anon_cipher");
if (ctx == NULL)
return SSL_FAILURE;
ctx->haveAnon = 1;
return SSL_SUCCESS;
}
#endif /* HAVE_ANON */
#ifndef NO_CERTS
2011-03-31 16:45:05 -07:00
/* used to be defined on NO_FILESYSTEM only, but are generally useful */
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
/* wolfSSL extension allows DER files to be loaded from buffers as well */
int wolfSSL_CTX_load_verify_buffer(WOLFSSL_CTX* ctx,
const unsigned char* in,
long sz, int format)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_load_verify_buffer");
if (format == SSL_FILETYPE_PEM)
2011-05-25 10:25:05 -07:00
return ProcessChainBuffer(ctx, in, sz, format, CA_TYPE, NULL);
else
2011-05-25 10:25:05 -07:00
return ProcessBuffer(ctx, in, sz, format, CA_TYPE, NULL,NULL,0);
2011-02-05 11:14:47 -08:00
}
2016-02-24 15:51:29 -07:00
#ifdef WOLFSSL_TRUST_PEER_CERT
int wolfSSL_CTX_trust_peer_buffer(WOLFSSL_CTX* ctx,
const unsigned char* in,
long sz, int format)
{
WOLFSSL_ENTER("wolfSSL_CTX_trust_peer_buffer");
/* sanity check on arguments */
if (sz < 0 || in == NULL || ctx == NULL) {
return BAD_FUNC_ARG;
}
2016-02-24 15:51:29 -07:00
if (format == SSL_FILETYPE_PEM)
return ProcessChainBuffer(ctx, in, sz, format,
TRUSTED_PEER_TYPE, NULL);
else
return ProcessBuffer(ctx, in, sz, format, TRUSTED_PEER_TYPE,
NULL,NULL,0);
}
#endif /* WOLFSSL_TRUST_PEER_CERT */
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_use_certificate_buffer(WOLFSSL_CTX* ctx,
2011-05-25 10:25:05 -07:00
const unsigned char* in, long sz, int format)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_buffer");
2011-05-25 10:25:05 -07:00
return ProcessBuffer(ctx, in, sz, format, CERT_TYPE, NULL, NULL, 0);
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_use_PrivateKey_buffer(WOLFSSL_CTX* ctx,
2011-05-25 10:25:05 -07:00
const unsigned char* in, long sz, int format)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey_buffer");
2011-05-25 10:25:05 -07:00
return ProcessBuffer(ctx, in, sz, format, PRIVATEKEY_TYPE, NULL,NULL,0);
2011-02-05 11:14:47 -08:00
}
int wolfSSL_CTX_use_certificate_chain_buffer_format(WOLFSSL_CTX* ctx,
const unsigned char* in, long sz, int format)
{
WOLFSSL_ENTER("wolfSSL_CTX_use_certificate_chain_buffer_format");
return ProcessBuffer(ctx, in, sz, format, CERT_TYPE, NULL, NULL, 1);
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_use_certificate_chain_buffer(WOLFSSL_CTX* ctx,
2011-05-25 10:25:05 -07:00
const unsigned char* in, long sz)
2011-02-05 11:14:47 -08:00
{
return wolfSSL_CTX_use_certificate_chain_buffer_format(ctx, in, sz,
SSL_FILETYPE_PEM);
2011-04-04 17:42:50 -07:00
}
#ifndef NO_DH
/* server wrapper for ctx or ssl Diffie-Hellman parameters */
static int wolfSSL_SetTmpDH_buffer_wrapper(WOLFSSL_CTX* ctx, WOLFSSL* ssl,
const unsigned char* buf,
long sz, int format)
{
DerBuffer* der = NULL;
int ret = 0;
word32 pSz = MAX_DH_SIZE;
word32 gSz = MAX_DH_SIZE;
#ifdef WOLFSSL_SMALL_STACK
byte* p = NULL;
byte* g = NULL;
#else
byte p[MAX_DH_SIZE];
byte g[MAX_DH_SIZE];
#endif
if (ctx == NULL || buf == NULL)
return BAD_FUNC_ARG;
ret = AllocDer(&der, 0, DH_PARAM_TYPE, ctx->heap);
if (ret != 0) {
return ret;
}
der->buffer = (byte*)buf;
der->length = (word32)sz;
#ifdef WOLFSSL_SMALL_STACK
p = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
g = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (p == NULL || g == NULL) {
XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(g, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return MEMORY_E;
}
#endif
if (format != SSL_FILETYPE_ASN1 && format != SSL_FILETYPE_PEM)
ret = SSL_BAD_FILETYPE;
else {
if (format == SSL_FILETYPE_PEM) {
FreeDer(&der);
ret = PemToDer(buf, sz, DH_PARAM_TYPE, &der, ctx->heap,
NULL, NULL);
}
if (ret == 0) {
if (wc_DhParamsLoad(der->buffer, der->length, p, &pSz, g, &gSz) < 0)
ret = SSL_BAD_FILETYPE;
else if (ssl)
ret = wolfSSL_SetTmpDH(ssl, p, pSz, g, gSz);
else
ret = wolfSSL_CTX_SetTmpDH(ctx, p, pSz, g, gSz);
}
}
FreeDer(&der);
#ifdef WOLFSSL_SMALL_STACK
XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(g, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return ret;
}
/* server Diffie-Hellman parameters, SSL_SUCCESS on ok */
int wolfSSL_SetTmpDH_buffer(WOLFSSL* ssl, const unsigned char* buf, long sz,
int format)
{
if (ssl == NULL)
return BAD_FUNC_ARG;
return wolfSSL_SetTmpDH_buffer_wrapper(ssl->ctx, ssl, buf, sz, format);
}
/* server ctx Diffie-Hellman parameters, SSL_SUCCESS on ok */
int wolfSSL_CTX_SetTmpDH_buffer(WOLFSSL_CTX* ctx, const unsigned char* buf,
long sz, int format)
{
return wolfSSL_SetTmpDH_buffer_wrapper(ctx, NULL, buf, sz, format);
}
#endif /* NO_DH */
2014-12-19 11:27:01 -07:00
int wolfSSL_use_certificate_buffer(WOLFSSL* ssl,
2011-05-25 10:25:05 -07:00
const unsigned char* in, long sz, int format)
2011-04-04 17:42:50 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_use_certificate_buffer");
2011-05-25 10:25:05 -07:00
return ProcessBuffer(ssl->ctx, in, sz, format,CERT_TYPE,ssl,NULL,0);
2011-04-04 17:42:50 -07:00
}
2014-12-19 11:27:01 -07:00
int wolfSSL_use_PrivateKey_buffer(WOLFSSL* ssl,
2011-05-25 10:25:05 -07:00
const unsigned char* in, long sz, int format)
2011-04-04 17:42:50 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_use_PrivateKey_buffer");
return ProcessBuffer(ssl->ctx, in, sz, format, PRIVATEKEY_TYPE,
2011-05-09 13:42:48 -07:00
ssl, NULL, 0);
2011-04-04 17:42:50 -07:00
}
int wolfSSL_use_certificate_chain_buffer_format(WOLFSSL* ssl,
const unsigned char* in, long sz, int format)
{
WOLFSSL_ENTER("wolfSSL_use_certificate_chain_buffer_format");
return ProcessBuffer(ssl->ctx, in, sz, format, CERT_TYPE,
ssl, NULL, 1);
}
2011-04-04 17:42:50 -07:00
2014-12-19 11:27:01 -07:00
int wolfSSL_use_certificate_chain_buffer(WOLFSSL* ssl,
2011-05-25 10:25:05 -07:00
const unsigned char* in, long sz)
2011-04-04 17:42:50 -07:00
{
return wolfSSL_use_certificate_chain_buffer_format(ssl, in, sz,
SSL_FILETYPE_PEM);
2011-02-05 11:14:47 -08:00
}
/* unload any certs or keys that SSL owns, leave CTX as is
SSL_SUCCESS on ok */
2014-12-19 11:27:01 -07:00
int wolfSSL_UnloadCertsKeys(WOLFSSL* ssl)
{
if (ssl == NULL) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Null function arg");
return BAD_FUNC_ARG;
}
if (ssl->buffers.weOwnCert && !ssl->keepCert) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Unloading cert");
FreeDer(&ssl->buffers.certificate);
#ifdef KEEP_OUR_CERT
FreeX509(ssl->ourCert);
if (ssl->ourCert) {
XFREE(ssl->ourCert, ssl->heap, DYNAMIC_TYPE_X509);
ssl->ourCert = NULL;
}
#endif
ssl->buffers.weOwnCert = 0;
}
2014-07-10 11:18:49 -06:00
if (ssl->buffers.weOwnCertChain) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Unloading cert chain");
FreeDer(&ssl->buffers.certChain);
2014-07-10 11:18:49 -06:00
ssl->buffers.weOwnCertChain = 0;
}
if (ssl->buffers.weOwnKey) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Unloading key");
FreeDer(&ssl->buffers.key);
ssl->buffers.weOwnKey = 0;
}
return SSL_SUCCESS;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_UnloadCAs(WOLFSSL_CTX* ctx)
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_UnloadCAs");
if (ctx == NULL)
return BAD_FUNC_ARG;
2014-12-19 11:27:01 -07:00
return wolfSSL_CertManagerUnloadCAs(ctx->cm);
}
2016-02-24 15:51:29 -07:00
#ifdef WOLFSSL_TRUST_PEER_CERT
int wolfSSL_CTX_Unload_trust_peers(WOLFSSL_CTX* ctx)
{
WOLFSSL_ENTER("wolfSSL_CTX_Unload_trust_peers");
if (ctx == NULL)
return BAD_FUNC_ARG;
return wolfSSL_CertManagerUnload_trust_peers(ctx->cm);
}
#endif /* WOLFSSL_TRUST_PEER_CERT */
2011-03-31 16:45:05 -07:00
/* old NO_FILESYSTEM end */
#endif /* !NO_CERTS */
2011-02-05 11:14:47 -08:00
#if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
2014-12-19 11:27:01 -07:00
int wolfSSL_add_all_algorithms(void)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_add_all_algorithms");
if (wolfSSL_Init() == SSL_SUCCESS)
return SSL_SUCCESS;
else
return SSL_FATAL_ERROR;
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
long wolfSSL_CTX_sess_set_cache_size(WOLFSSL_CTX* ctx, long sz)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
/* cache size fixed at compile time in wolfSSL */
(void)ctx;
(void)sz;
2011-02-05 11:14:47 -08:00
return 0;
}
2014-12-19 11:27:01 -07:00
void wolfSSL_CTX_set_quiet_shutdown(WOLFSSL_CTX* ctx, int mode)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_set_quiet_shutdown");
2011-02-05 11:14:47 -08:00
if (mode)
ctx->quietShutdown = 1;
}
2014-12-19 11:27:01 -07:00
void wolfSSL_set_quiet_shutdown(WOLFSSL* ssl, int mode)
2011-10-28 18:43:07 -07:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_set_quiet_shutdown");
2011-10-28 18:43:07 -07:00
if (mode)
ssl->options.quietShutdown = 1;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_set_bio(WOLFSSL* ssl, WOLFSSL_BIO* rd, WOLFSSL_BIO* wr)
{
WOLFSSL_ENTER("SSL_set_bio");
wolfSSL_set_rfd(ssl, rd->fd);
wolfSSL_set_wfd(ssl, wr->fd);
ssl->biord = rd;
ssl->biowr = wr;
}
2014-12-19 11:27:01 -07:00
void wolfSSL_CTX_set_client_CA_list(WOLFSSL_CTX* ctx,
STACK_OF(WOLFSSL_X509_NAME)* names)
2011-02-05 11:14:47 -08:00
{
(void)ctx;
(void)names;
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
STACK_OF(WOLFSSL_X509_NAME)* wolfSSL_load_client_CA_file(const char* fname)
2011-02-05 11:14:47 -08:00
{
(void)fname;
2011-02-05 11:14:47 -08:00
return 0;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_CTX_set_default_verify_paths(WOLFSSL_CTX* ctx)
2011-02-05 11:14:47 -08:00
{
/* TODO:, not needed in goahead */
(void)ctx;
2011-02-05 11:14:47 -08:00
return SSL_NOT_IMPLEMENTED;
}
2011-10-28 18:43:07 -07:00
/* keyblock size in bytes or -1 */
2014-12-19 11:27:01 -07:00
int wolfSSL_get_keyblock_size(WOLFSSL* ssl)
2011-10-28 18:43:07 -07:00
{
if (ssl == NULL)
return SSL_FATAL_ERROR;
2011-10-28 18:43:07 -07:00
return 2 * (ssl->specs.key_size + ssl->specs.iv_size +
ssl->specs.hash_size);
}
/* store keys returns SSL_SUCCESS or -1 on error */
2014-12-19 11:27:01 -07:00
int wolfSSL_get_keys(WOLFSSL* ssl, unsigned char** ms, unsigned int* msLen,
2011-10-28 18:43:07 -07:00
unsigned char** sr, unsigned int* srLen,
unsigned char** cr, unsigned int* crLen)
{
2012-09-14 21:19:06 -07:00
if (ssl == NULL || ssl->arrays == NULL)
return SSL_FATAL_ERROR;
2011-10-28 18:43:07 -07:00
2012-09-14 21:19:06 -07:00
*ms = ssl->arrays->masterSecret;
*sr = ssl->arrays->serverRandom;
*cr = ssl->arrays->clientRandom;
2011-10-28 18:43:07 -07:00
*msLen = SECRET_LEN;
*srLen = RAN_LEN;
*crLen = RAN_LEN;
return SSL_SUCCESS;
2011-10-28 18:43:07 -07:00
}
2014-12-19 11:27:01 -07:00
void wolfSSL_set_accept_state(WOLFSSL* ssl)
2011-02-05 11:14:47 -08:00
{
2015-12-21 16:11:02 -07:00
word16 haveRSA = 1;
word16 havePSK = 0;
2011-02-05 11:14:47 -08:00
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_set_accept_state");
ssl->options.side = WOLFSSL_SERVER_END;
2011-02-05 11:14:47 -08:00
/* reset suites in case user switched */
2012-10-29 15:39:42 -07:00
#ifdef NO_RSA
haveRSA = 0;
#endif
#ifndef NO_PSK
havePSK = ssl->options.havePSK;
#endif
InitSuites(ssl->suites, ssl->version, haveRSA, havePSK,
ssl->options.haveDH, ssl->options.haveNTRU,
2016-02-10 11:02:09 -07:00
ssl->options.haveECDSAsig, ssl->options.haveECC,
ssl->options.haveStaticECC, ssl->options.side);
2011-02-05 11:14:47 -08:00
}
#endif
2011-02-05 11:14:47 -08:00
2011-10-28 18:43:07 -07:00
/* return true if connection established */
2014-12-19 11:27:01 -07:00
int wolfSSL_is_init_finished(WOLFSSL* ssl)
2011-10-28 18:43:07 -07:00
{
if (ssl == NULL)
return 0;
if (ssl->options.handShakeState == HANDSHAKE_DONE)
return 1;
return 0;
}
#if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
2014-12-19 11:27:01 -07:00
void wolfSSL_CTX_set_tmp_rsa_callback(WOLFSSL_CTX* ctx,
WOLFSSL_RSA*(*f)(WOLFSSL*, int, int))
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
/* wolfSSL verifies all these internally */
(void)ctx;
(void)f;
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
void wolfSSL_set_shutdown(WOLFSSL* ssl, int opt)
2011-02-05 11:14:47 -08:00
{
2015-07-16 16:46:37 -07:00
WOLFSSL_ENTER("wolfSSL_set_shutdown");
if(ssl==NULL) {
WOLFSSL_MSG("Shutdown not set. ssl is null");
return;
}
ssl->options.sentNotify = (opt&SSL_SENT_SHUTDOWN) > 0;
ssl->options.closeNotify = (opt&SSL_RECEIVED_SHUTDOWN) > 0;
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
long wolfSSL_CTX_set_options(WOLFSSL_CTX* ctx, long opt)
2011-02-05 11:14:47 -08:00
{
/* goahead calls with 0, do nothing */
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_CTX_set_options");
(void)ctx;
2011-02-05 11:14:47 -08:00
return opt;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_set_rfd(WOLFSSL* ssl, int rfd)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_set_rfd");
2011-02-05 11:14:47 -08:00
ssl->rfd = rfd; /* not used directly to allow IO callbacks */
ssl->IOCB_ReadCtx = &ssl->rfd;
return SSL_SUCCESS;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_set_wfd(WOLFSSL* ssl, int wfd)
2011-02-05 11:14:47 -08:00
{
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("SSL_set_wfd");
2011-02-05 11:14:47 -08:00
ssl->wfd = wfd; /* not used directly to allow IO callbacks */
ssl->IOCB_WriteCtx = &ssl->wfd;
return SSL_SUCCESS;
}
2014-12-19 11:27:01 -07:00
WOLFSSL_RSA* wolfSSL_RSA_generate_key(int len, unsigned long bits,
void(*f)(int, int, void*), void* data)
2011-02-05 11:14:47 -08:00
{
/* no tmp key needed, actual generation not supported */
2014-12-19 11:27:01 -07:00
WOLFSSL_ENTER("RSA_generate_key");
(void)len;
(void)bits;
(void)f;
(void)data;
2012-05-01 16:12:12 -07:00
return NULL;
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
WOLFSSL_X509* wolfSSL_X509_STORE_CTX_get_current_cert(
WOLFSSL_X509_STORE_CTX* ctx)
2011-02-05 11:14:47 -08:00
{
WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_current_cert");
if(ctx)
return ctx->current_cert;
2015-07-14 14:56:26 -06:00
return NULL;
2011-02-05 11:14:47 -08:00
}
2014-12-19 11:27:01 -07:00
int wolfSSL_X509_STORE_CTX_get_error(WOLFSSL_X509_STORE_CTX* ctx)
2011-02-05 11:14:47 -08:00
{
WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_error");
if (ctx != NULL)
return ctx->error;
2011-02-05 11:14:47 -08:00
return 0;
}
2014-12-19 11:27:01 -07:00
int wolfSSL_X509_STORE_CTX_get_error_depth(WOLFSSL_X509_STORE_CTX* ctx)
2011-02-05 11:14:47 -08:00
{
WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_error_depth");
if(ctx)
return ctx->error_depth;
return SSL_FATAL_ERROR;
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_buffer(void)
{
static WOLFSSL_BIO_METHOD meth;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("BIO_f_buffer");
meth.type = BIO_BUFFER;
2016-06-27 10:53:34 -07:00
return &meth;
}
2016-06-27 10:53:34 -07:00
long wolfSSL_BIO_set_write_buffer_size(WOLFSSL_BIO* bio, long size)
{
2016-06-27 10:53:34 -07:00
/* wolfSSL has internal buffer, compatibility only */
WOLFSSL_ENTER("BIO_set_write_buffer_size");
(void)bio;
return size;
}
2016-06-27 10:53:34 -07:00
WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_ssl(void)
{
2016-06-27 10:53:34 -07:00
static WOLFSSL_BIO_METHOD meth;
WOLFSSL_ENTER("BIO_f_ssl");
meth.type = BIO_SSL;
return &meth;
}
2016-06-27 10:53:34 -07:00
WOLFSSL_BIO* wolfSSL_BIO_new_socket(int sfd, int closeF)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_BIO* bio = (WOLFSSL_BIO*) XMALLOC(sizeof(WOLFSSL_BIO), 0,
DYNAMIC_TYPE_OPENSSL);
WOLFSSL_ENTER("BIO_new_socket");
if (bio) {
bio->type = BIO_SOCKET;
bio->close = (byte)closeF;
bio->eof = 0;
bio->ssl = 0;
bio->fd = sfd;
bio->prev = 0;
bio->next = 0;
bio->mem = NULL;
bio->memLen = 0;
}
return bio;
}
2016-06-27 10:53:34 -07:00
int wolfSSL_BIO_eof(WOLFSSL_BIO* b)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("BIO_eof");
if (b->eof)
return 1;
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
long wolfSSL_BIO_set_ssl(WOLFSSL_BIO* b, WOLFSSL* ssl, int closeF)
2011-02-05 11:14:47 -08:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("BIO_set_ssl");
b->ssl = ssl;
b->close = (byte)closeF;
/* add to ssl for bio free if SSL_free called before/instead of free_all? */
return 0;
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
WOLFSSL_BIO* wolfSSL_BIO_new(WOLFSSL_BIO_METHOD* method)
2011-02-05 11:14:47 -08:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_BIO* bio = (WOLFSSL_BIO*) XMALLOC(sizeof(WOLFSSL_BIO), 0,
DYNAMIC_TYPE_OPENSSL);
WOLFSSL_ENTER("BIO_new");
if (bio) {
bio->type = method->type;
bio->close = 0;
bio->eof = 0;
bio->ssl = NULL;
bio->mem = NULL;
bio->memLen = 0;
bio->fd = 0;
bio->prev = NULL;
bio->next = NULL;
}
return bio;
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
int wolfSSL_BIO_get_mem_data(WOLFSSL_BIO* bio, const byte** p)
{
if (bio == NULL || p == NULL)
return SSL_FATAL_ERROR;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
*p = bio->mem;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return bio->memLen;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_BIO* wolfSSL_BIO_new_mem_buf(void* buf, int len)
{
WOLFSSL_BIO* bio = NULL;
if (buf == NULL)
return bio;
bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
if (bio == NULL)
return bio;
bio->memLen = len;
bio->mem = (byte*)XMALLOC(len, 0, DYNAMIC_TYPE_OPENSSL);
if (bio->mem == NULL) {
XFREE(bio, 0, DYNAMIC_TYPE_OPENSSL);
return NULL;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
XMEMCPY(bio->mem, buf, len);
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return bio;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#ifdef USE_WINDOWS_API
#define CloseSocket(s) closesocket(s)
#elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
#define CloseSocket(s) closesocket(s)
extern int closesocket(int) ;
#else
#define CloseSocket(s) close(s)
#endif
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_BIO_free(WOLFSSL_BIO* bio)
{
/* unchain?, doesn't matter in goahead since from free all */
WOLFSSL_ENTER("BIO_free");
if (bio) {
if (bio->close) {
if (bio->ssl)
wolfSSL_free(bio->ssl);
if (bio->fd)
CloseSocket(bio->fd);
}
2016-06-27 10:53:34 -07:00
if (bio->mem)
XFREE(bio->mem, 0, DYNAMIC_TYPE_OPENSSL);
XFREE(bio, 0, DYNAMIC_TYPE_OPENSSL);
}
2016-06-27 10:53:34 -07:00
return 0;
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
int wolfSSL_BIO_free_all(WOLFSSL_BIO* bio)
2011-02-05 11:14:47 -08:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("BIO_free_all");
while (bio) {
WOLFSSL_BIO* next = bio->next;
wolfSSL_BIO_free(bio);
bio = next;
}
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_BIO_read(WOLFSSL_BIO* bio, void* buf, int len)
{
int ret;
WOLFSSL* ssl = 0;
WOLFSSL_BIO* front = bio;
WOLFSSL_ENTER("BIO_read");
/* already got eof, again is error */
if (front->eof)
return SSL_FATAL_ERROR;
2011-03-30 12:36:23 -07:00
2016-06-27 10:53:34 -07:00
while(bio && ((ssl = bio->ssl) == 0) )
bio = bio->next;
2011-03-30 12:36:23 -07:00
2016-06-27 10:53:34 -07:00
if (ssl == 0) return BAD_FUNC_ARG;
2011-03-30 12:36:23 -07:00
2016-06-27 10:53:34 -07:00
ret = wolfSSL_read(ssl, buf, len);
if (ret == 0)
front->eof = 1;
else if (ret < 0) {
int err = wolfSSL_get_error(ssl, 0);
if ( !(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) )
front->eof = 1;
}
return ret;
}
2011-03-30 12:36:23 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len)
{
int ret;
WOLFSSL* ssl = 0;
WOLFSSL_BIO* front = bio;
2011-03-30 12:36:23 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("BIO_write");
/* already got eof, again is error */
if (front->eof)
return SSL_FATAL_ERROR;
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
while(bio && ((ssl = bio->ssl) == 0) )
bio = bio->next;
2016-06-27 10:53:34 -07:00
if (ssl == 0) return BAD_FUNC_ARG;
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
ret = wolfSSL_write(ssl, data, len);
if (ret == 0)
front->eof = 1;
else if (ret < 0) {
int err = wolfSSL_get_error(ssl, 0);
if ( !(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) )
front->eof = 1;
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
return ret;
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
WOLFSSL_BIO* wolfSSL_BIO_push(WOLFSSL_BIO* top, WOLFSSL_BIO* append)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("BIO_push");
top->next = append;
append->prev = top;
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
return top;
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_BIO_flush(WOLFSSL_BIO* bio)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
/* for wolfSSL no flushing needed */
WOLFSSL_ENTER("BIO_flush");
(void)bio;
return 1;
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
#endif /* OPENSSL_EXTRA || GOAHEAD_WS */
#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
void wolfSSL_CTX_set_default_passwd_cb_userdata(WOLFSSL_CTX* ctx,
void* userdata)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SSL_CTX_set_default_passwd_cb_userdata");
ctx->userdata = userdata;
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_CTX_set_default_passwd_cb(WOLFSSL_CTX* ctx, pem_password_cb cb)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SSL_CTX_set_default_passwd_cb");
if (ctx != NULL) {
ctx->passwd_cb = cb;
}
2016-06-27 10:53:34 -07:00
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_num_locks(void)
{
return 0;
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_set_locking_callback(void (*f)(int, int, const char*, int))
{
(void)f;
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_set_id_callback(unsigned long (*f)(void))
{
(void)f;
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
unsigned long wolfSSL_ERR_get_error(void)
{
/* TODO: */
return 0;
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_MD5
int wolfSSL_EVP_BytesToKey(const WOLFSSL_EVP_CIPHER* type,
const WOLFSSL_EVP_MD* md, const byte* salt,
const byte* data, int sz, int count, byte* key, byte* iv)
{
2016-06-27 10:53:34 -07:00
int keyLen = 0;
int ivLen = 0;
int j;
int keyLeft;
int ivLeft;
int keyOutput = 0;
byte digest[MD5_DIGEST_SIZE];
#ifdef WOLFSSL_SMALL_STACK
Md5* md5 = NULL;
#else
Md5 md5[1];
#endif
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
md5 = (Md5*)XMALLOC(sizeof(Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (md5 == NULL)
return 0;
#endif
(void)type;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EVP_BytesToKey");
wc_InitMd5(md5);
2016-06-27 10:53:34 -07:00
/* only support MD5 for now */
if (XSTRNCMP(md, "MD5", 3) != 0) return 0;
/* only support CBC DES and AES for now */
#ifndef NO_DES3
if (XSTRNCMP(type, EVP_DES_CBC, EVP_DES_SIZE) == 0) {
keyLen = DES_KEY_SIZE;
ivLen = DES_IV_SIZE;
}
else if (XSTRNCMP(type, EVP_DES_EDE3_CBC, EVP_DES_EDE3_SIZE) == 0) {
keyLen = DES3_KEY_SIZE;
ivLen = DES_IV_SIZE;
}
else
#endif /* NO_DES3 */
#ifndef NO_AES
2016-06-27 10:53:34 -07:00
if (XSTRNCMP(type, EVP_AES_128_CBC, EVP_AES_SIZE) == 0) {
keyLen = AES_128_KEY_SIZE;
ivLen = AES_IV_SIZE;
}
else if (XSTRNCMP(type, EVP_AES_192_CBC, EVP_AES_SIZE) == 0) {
keyLen = AES_192_KEY_SIZE;
ivLen = AES_IV_SIZE;
}
else if (XSTRNCMP(type, EVP_AES_256_CBC, EVP_AES_SIZE) == 0) {
keyLen = AES_256_KEY_SIZE;
ivLen = AES_IV_SIZE;
}
else
#endif /* NO_AES */
{
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return 0;
}
2016-06-27 10:53:34 -07:00
keyLeft = keyLen;
ivLeft = ivLen;
while (keyOutput < (keyLen + ivLen)) {
int digestLeft = MD5_DIGEST_SIZE;
/* D_(i - 1) */
if (keyOutput) /* first time D_0 is empty */
wc_Md5Update(md5, digest, MD5_DIGEST_SIZE);
/* data */
wc_Md5Update(md5, data, sz);
/* salt */
if (salt)
wc_Md5Update(md5, salt, EVP_SALT_SIZE);
wc_Md5Final(md5, digest);
/* count */
for (j = 1; j < count; j++) {
wc_Md5Update(md5, digest, MD5_DIGEST_SIZE);
wc_Md5Final(md5, digest);
}
2016-06-27 10:53:34 -07:00
if (keyLeft) {
int store = min(keyLeft, MD5_DIGEST_SIZE);
XMEMCPY(&key[keyLen - keyLeft], digest, store);
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
keyOutput += store;
keyLeft -= store;
digestLeft -= store;
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
if (ivLeft && digestLeft) {
int store = min(ivLeft, digestLeft);
if (iv != NULL)
XMEMCPY(&iv[ivLen - ivLeft],
&digest[MD5_DIGEST_SIZE - digestLeft], store);
keyOutput += store;
ivLeft -= store;
}
}
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
return keyOutput == (keyLen + ivLen) ? keyOutput : 0;
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
#endif /* NO_MD5 */
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
#endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#ifdef OPENSSL_EXTRA
2016-06-27 10:53:34 -07:00
unsigned long wolfSSLeay(void)
2011-02-05 11:14:47 -08:00
{
2016-06-27 10:53:34 -07:00
return SSLEAY_VERSION_NUMBER;
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
const char* wolfSSLeay_version(int type)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
static const char* version = "SSLeay wolfSSL compatibility";
(void)type;
return version;
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
#ifndef NO_MD5
void wolfSSL_MD5_Init(WOLFSSL_MD5_CTX* md5)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
typedef char md5_test[sizeof(MD5_CTX) >= sizeof(Md5) ? 1 : -1];
(void)sizeof(md5_test);
WOLFSSL_ENTER("MD5_Init");
wc_InitMd5((Md5*)md5);
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_MD5_Update(WOLFSSL_MD5_CTX* md5, const void* input,
unsigned long sz)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_MD5_Update");
wc_Md5Update((Md5*)md5, (const byte*)input, (word32)sz);
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_MD5_Final(byte* input, WOLFSSL_MD5_CTX* md5)
2011-02-05 11:14:47 -08:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("MD5_Final");
wc_Md5Final((Md5*)md5, input);
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
#endif /* NO_MD5 */
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#ifndef NO_SHA
void wolfSSL_SHA_Init(WOLFSSL_SHA_CTX* sha)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
typedef char sha_test[sizeof(SHA_CTX) >= sizeof(Sha) ? 1 : -1];
(void)sizeof(sha_test);
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SHA_Init");
wc_InitSha((Sha*)sha); /* OpenSSL compat, no ret */
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_SHA_Update(WOLFSSL_SHA_CTX* sha, const void* input,
unsigned long sz)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SHA_Update");
wc_ShaUpdate((Sha*)sha, (const byte*)input, (word32)sz);
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_SHA_Final(byte* input, WOLFSSL_SHA_CTX* sha)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SHA_Final");
wc_ShaFinal((Sha*)sha, input);
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_SHA1_Init(WOLFSSL_SHA_CTX* sha)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SHA1_Init");
SHA_Init(sha);
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_SHA1_Update(WOLFSSL_SHA_CTX* sha, const void* input,
unsigned long sz)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SHA1_Update");
SHA_Update(sha, input, sz);
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_SHA1_Final(byte* input, WOLFSSL_SHA_CTX* sha)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SHA1_Final");
SHA_Final(input, sha);
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
#endif /* NO_SHA */
2012-04-26 14:58:29 -07:00
#ifdef WOLFSSL_SHA224
void wolfSSL_SHA224_Init(WOLFSSL_SHA224_CTX* sha)
{
typedef char sha_test[sizeof(SHA224_CTX) >= sizeof(Sha224) ? 1 : -1];
(void)sizeof(sha_test);
WOLFSSL_ENTER("SHA224_Init");
wc_InitSha224((Sha224*)sha); /* OpenSSL compat, no error */
}
void wolfSSL_SHA224_Update(WOLFSSL_SHA224_CTX* sha, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("SHA224_Update");
wc_Sha224Update((Sha224*)sha, (const byte*)input, (word32)sz);
/* OpenSSL compat, no error */
}
void wolfSSL_SHA224_Final(byte* input, WOLFSSL_SHA224_CTX* sha)
{
WOLFSSL_ENTER("SHA224_Final");
wc_Sha224Final((Sha224*)sha, input);
/* OpenSSL compat, no error */
}
#endif /* WOLFSSL_SHA224 */
2016-06-27 10:53:34 -07:00
void wolfSSL_SHA256_Init(WOLFSSL_SHA256_CTX* sha256)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
typedef char sha_test[sizeof(SHA256_CTX) >= sizeof(Sha256) ? 1 : -1];
(void)sizeof(sha_test);
WOLFSSL_ENTER("SHA256_Init");
wc_InitSha256((Sha256*)sha256); /* OpenSSL compat, no error */
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_SHA256_Update(WOLFSSL_SHA256_CTX* sha, const void* input,
unsigned long sz)
{
WOLFSSL_ENTER("SHA256_Update");
wc_Sha256Update((Sha256*)sha, (const byte*)input, (word32)sz);
/* OpenSSL compat, no error */
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_SHA256_Final(byte* input, WOLFSSL_SHA256_CTX* sha)
2012-05-01 16:12:12 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SHA256_Final");
wc_Sha256Final((Sha256*)sha, input);
/* OpenSSL compat, no error */
2012-05-01 16:12:12 -07:00
}
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SHA384
2016-06-27 10:53:34 -07:00
void wolfSSL_SHA384_Init(WOLFSSL_SHA384_CTX* sha)
{
typedef char sha_test[sizeof(SHA384_CTX) >= sizeof(Sha384) ? 1 : -1];
(void)sizeof(sha_test);
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SHA384_Init");
wc_InitSha384((Sha384*)sha); /* OpenSSL compat, no error */
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_SHA384_Update(WOLFSSL_SHA384_CTX* sha, const void* input,
unsigned long sz)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SHA384_Update");
wc_Sha384Update((Sha384*)sha, (const byte*)input, (word32)sz);
/* OpenSSL compat, no error */
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_SHA384_Final(byte* input, WOLFSSL_SHA384_CTX* sha)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SHA384_Final");
wc_Sha384Final((Sha384*)sha, input);
/* OpenSSL compat, no error */
}
2016-06-27 10:53:34 -07:00
#endif /* WOLFSSL_SHA384 */
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SHA512
2016-06-27 10:53:34 -07:00
void wolfSSL_SHA512_Init(WOLFSSL_SHA512_CTX* sha)
{
typedef char sha_test[sizeof(SHA512_CTX) >= sizeof(Sha512) ? 1 : -1];
(void)sizeof(sha_test);
WOLFSSL_ENTER("SHA512_Init");
wc_InitSha512((Sha512*)sha); /* OpenSSL compat, no error */
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
void wolfSSL_SHA512_Update(WOLFSSL_SHA512_CTX* sha, const void* input,
unsigned long sz)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SHA512_Update");
wc_Sha512Update((Sha512*)sha, (const byte*)input, (word32)sz);
/* OpenSSL compat, no error */
}
2014-03-19 13:56:11 -07:00
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_SHA512_Final(byte* input, WOLFSSL_SHA512_CTX* sha)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SHA512_Final");
wc_Sha512Final((Sha512*)sha, input);
/* OpenSSL compat, no error */
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#endif /* WOLFSSL_SHA512 */
2015-03-20 11:25:22 -06:00
2016-06-27 10:53:34 -07:00
#ifndef NO_MD5
const WOLFSSL_EVP_MD* wolfSSL_EVP_md5(void)
{
2016-06-27 10:53:34 -07:00
static const char* type = "MD5";
WOLFSSL_ENTER("EVP_md5");
return type;
}
2016-06-27 10:53:34 -07:00
#endif /* NO_MD5 */
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_SHA
const WOLFSSL_EVP_MD* wolfSSL_EVP_sha1(void)
{
static const char* type = "SHA";
WOLFSSL_ENTER("EVP_sha1");
return type;
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
#endif /* NO_SHA */
2012-04-26 14:58:29 -07:00
#ifdef WOLFSSL_SHA224
const WOLFSSL_EVP_MD* wolfSSL_EVP_sha224(void)
{
static const char* type = "SHA224";
WOLFSSL_ENTER("EVP_sha224");
return type;
}
#endif /* WOLFSSL_SHA224 */
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
const WOLFSSL_EVP_MD* wolfSSL_EVP_sha256(void)
{
2016-06-27 10:53:34 -07:00
static const char* type = "SHA256";
WOLFSSL_ENTER("EVP_sha256");
return type;
}
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SHA384
2016-06-27 10:53:34 -07:00
const WOLFSSL_EVP_MD* wolfSSL_EVP_sha384(void)
{
static const char* type = "SHA384";
WOLFSSL_ENTER("EVP_sha384");
return type;
}
2016-06-27 10:53:34 -07:00
#endif /* WOLFSSL_SHA384 */
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SHA512
2016-06-27 10:53:34 -07:00
const WOLFSSL_EVP_MD* wolfSSL_EVP_sha512(void)
{
static const char* type = "SHA512";
WOLFSSL_ENTER("EVP_sha512");
return type;
}
2016-06-27 10:53:34 -07:00
#endif /* WOLFSSL_SHA512 */
2016-06-27 10:53:34 -07:00
void wolfSSL_EVP_MD_CTX_init(WOLFSSL_EVP_MD_CTX* ctx)
{
WOLFSSL_ENTER("EVP_CIPHER_MD_CTX_init");
(void)ctx;
/* do nothing */
}
#ifndef NO_AES
2016-06-27 10:53:34 -07:00
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_cbc(void)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EVP_aes_128_cbc");
return EVP_AES_128_CBC;
}
2016-06-27 10:53:34 -07:00
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_cbc(void)
{
WOLFSSL_ENTER("wolfSSL_EVP_aes_192_cbc");
return EVP_AES_192_CBC;
}
2016-06-27 10:53:34 -07:00
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_cbc(void)
{
WOLFSSL_ENTER("wolfSSL_EVP_aes_256_cbc");
return EVP_AES_256_CBC;
}
2016-06-27 10:53:34 -07:00
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_ctr(void)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EVP_aes_128_ctr");
return EVP_AES_128_CTR;
}
2016-06-27 10:53:34 -07:00
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_ctr(void)
{
WOLFSSL_ENTER("wolfSSL_EVP_aes_192_ctr");
return EVP_AES_192_CTR;
}
2016-06-27 10:53:34 -07:00
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_ctr(void)
{
WOLFSSL_ENTER("wolfSSL_EVP_aes_256_ctr");
return EVP_AES_256_CTR;
}
2016-10-13 22:10:50 +09:00
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_128_ecb(void)
{
WOLFSSL_ENTER("wolfSSL_EVP_aes_128_ecb");
return EVP_AES_128_ECB;
}
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_192_ecb(void)
{
WOLFSSL_ENTER("wolfSSL_EVP_aes_192_ecb");
return EVP_AES_192_ECB;
}
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_aes_256_ecb(void)
{
WOLFSSL_ENTER("wolfSSL_EVP_aes_256_ecb");
return EVP_AES_256_ECB;
}
#endif /* NO_AES */
2016-06-27 10:53:34 -07:00
#ifndef NO_DES3
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_cbc(void)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EVP_des_cbc");
return EVP_DES_CBC;
}
2016-10-13 22:10:50 +09:00
#ifdef WOLFSSL_DES_ECB
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_ecb(void)
{
WOLFSSL_ENTER("wolfSSL_EVP_des_ecb");
return EVP_DES_ECB;
}
#endif
2016-06-27 10:53:34 -07:00
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_ede3_cbc(void)
{
WOLFSSL_ENTER("wolfSSL_EVP_des_ede3_cbc");
return EVP_DES_EDE3_CBC;
}
2016-10-13 22:10:50 +09:00
#ifdef WOLFSSL_DES_ECB
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_des_ede3_ecb(void)
{
WOLFSSL_ENTER("wolfSSL_EVP_des_ede3_ecb");
return EVP_DES_EDE3_ECB;
}
#endif
2016-06-27 10:53:34 -07:00
#endif /* NO_DES3 */
2016-06-27 10:53:34 -07:00
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_rc4(void)
{
static const char* type = "ARC4";
WOLFSSL_ENTER("wolfSSL_EVP_rc4");
return type;
}
2016-06-27 10:53:34 -07:00
#ifdef HAVE_IDEA
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_idea_cbc(void)
{
WOLFSSL_ENTER("wolfSSL_EVP_idea_cbc");
return EVP_IDEA_CBC;
}
#endif
const WOLFSSL_EVP_CIPHER* wolfSSL_EVP_enc_null(void)
{
static const char* type = "NULL";
WOLFSSL_ENTER("wolfSSL_EVP_enc_null");
return type;
}
2016-06-27 10:53:34 -07:00
int wolfSSL_EVP_MD_CTX_cleanup(WOLFSSL_EVP_MD_CTX* ctx)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("EVP_MD_CTX_cleanup");
(void)ctx;
return 0;
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_EVP_CIPHER_CTX_init(WOLFSSL_EVP_CIPHER_CTX* ctx)
{
WOLFSSL_ENTER("EVP_CIPHER_CTX_init");
if (ctx) {
ctx->cipherType = 0xff; /* no init */
ctx->keyLen = 0;
ctx->enc = 1; /* start in encrypt mode */
}
2016-06-27 10:53:34 -07:00
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_EVP_CIPHER_CTX_cleanup(WOLFSSL_EVP_CIPHER_CTX* ctx)
{
WOLFSSL_ENTER("EVP_CIPHER_CTX_cleanup");
if (ctx) {
ctx->cipherType = 0xff; /* no more init */
ctx->keyLen = 0;
}
return SSL_SUCCESS;
2012-04-26 14:58:29 -07:00
}
2016-06-27 10:53:34 -07:00
/* return SSL_SUCCESS on ok, 0 on failure to match API compatibility */
int wolfSSL_EVP_CipherInit(WOLFSSL_EVP_CIPHER_CTX* ctx,
const WOLFSSL_EVP_CIPHER* type, byte* key,
byte* iv, int enc)
2012-04-26 14:58:29 -07:00
{
2016-06-27 10:53:34 -07:00
int ret = -1; /* failure local, during function 0 means success
because internal functions work that way */
(void)key;
2016-06-27 10:53:34 -07:00
(void)iv;
(void)enc;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EVP_CipherInit");
if (ctx == NULL) {
WOLFSSL_MSG("no ctx");
return 0; /* failure */
}
2016-06-27 10:53:34 -07:00
if (type == NULL && ctx->cipherType == 0xff) {
WOLFSSL_MSG("no type set");
return 0; /* failure */
}
#ifndef NO_AES
if (ctx->cipherType == AES_128_CBC_TYPE ||
(type && XSTRNCMP(type, EVP_AES_128_CBC, EVP_AES_SIZE) == 0)) {
WOLFSSL_MSG(EVP_AES_128_CBC);
ctx->cipherType = AES_128_CBC_TYPE;
ctx->keyLen = 16;
2016-10-13 22:10:50 +09:00
ctx->block_size = 16;
2016-06-27 10:53:34 -07:00
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
if (ret != 0)
return ret;
}
if (iv && key == NULL) {
ret = wc_AesSetIV(&ctx->cipher.aes, iv);
if (ret != 0)
return ret;
}
}
else if (ctx->cipherType == AES_192_CBC_TYPE ||
(type && XSTRNCMP(type, EVP_AES_192_CBC, EVP_AES_SIZE) == 0)) {
WOLFSSL_MSG(EVP_AES_192_CBC);
ctx->cipherType = AES_192_CBC_TYPE;
ctx->keyLen = 24;
2016-10-13 22:10:50 +09:00
ctx->block_size = 16;
2016-06-27 10:53:34 -07:00
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
if (ret != 0)
return ret;
}
if (iv && key == NULL) {
ret = wc_AesSetIV(&ctx->cipher.aes, iv);
if (ret != 0)
return ret;
}
}
else if (ctx->cipherType == AES_256_CBC_TYPE ||
(type && XSTRNCMP(type, EVP_AES_256_CBC, EVP_AES_SIZE) == 0)) {
WOLFSSL_MSG(EVP_AES_256_CBC);
ctx->cipherType = AES_256_CBC_TYPE;
ctx->keyLen = 32;
2016-10-13 22:10:50 +09:00
ctx->block_size = 16;
2016-06-27 10:53:34 -07:00
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
if (ret != 0)
return ret;
}
if (iv && key == NULL) {
ret = wc_AesSetIV(&ctx->cipher.aes, iv);
if (ret != 0)
return ret;
}
2016-06-27 10:53:34 -07:00
}
#ifdef WOLFSSL_AES_COUNTER
else if (ctx->cipherType == AES_128_CTR_TYPE ||
(type && XSTRNCMP(type, EVP_AES_128_CTR, EVP_AES_SIZE) == 0)) {
WOLFSSL_MSG(EVP_AES_128_CTR);
ctx->cipherType = AES_128_CTR_TYPE;
ctx->keyLen = 16;
2016-10-13 22:10:50 +09:00
ctx->block_size = 16;
2016-06-27 10:53:34 -07:00
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
2016-10-13 22:10:50 +09:00
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION));
2016-06-27 10:53:34 -07:00
if (ret != 0)
return ret;
}
if (iv && key == NULL) {
ret = wc_AesSetIV(&ctx->cipher.aes, iv);
if (ret != 0)
return ret;
}
}
else if (ctx->cipherType == AES_192_CTR_TYPE ||
(type && XSTRNCMP(type, EVP_AES_192_CTR, EVP_AES_SIZE) == 0)) {
WOLFSSL_MSG(EVP_AES_192_CTR);
ctx->cipherType = AES_192_CTR_TYPE;
ctx->keyLen = 24;
2016-10-13 22:10:50 +09:00
ctx->block_size = 16;
2016-06-27 10:53:34 -07:00
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
2016-10-13 22:10:50 +09:00
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION));
2016-06-27 10:53:34 -07:00
if (ret != 0)
return ret;
}
if (iv && key == NULL) {
ret = wc_AesSetIV(&ctx->cipher.aes, iv);
if (ret != 0)
return ret;
}
}
else if (ctx->cipherType == AES_256_CTR_TYPE ||
(type && XSTRNCMP(type, EVP_AES_256_CTR, EVP_AES_SIZE) == 0)) {
WOLFSSL_MSG(EVP_AES_256_CTR);
ctx->cipherType = AES_256_CTR_TYPE;
ctx->keyLen = 32;
2016-10-13 22:10:50 +09:00
ctx->block_size = 16;
2016-06-27 10:53:34 -07:00
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
2016-10-13 22:10:50 +09:00
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION));
2016-06-27 10:53:34 -07:00
if (ret != 0)
return ret;
}
if (iv && key == NULL) {
ret = wc_AesSetIV(&ctx->cipher.aes, iv);
if (ret != 0)
return ret;
}
}
#endif /* WOLFSSL_AES_CTR */
2016-10-13 22:10:50 +09:00
else if (ctx->cipherType == AES_128_ECB_TYPE ||
(type && XSTRNCMP(type, EVP_AES_128_ECB, EVP_AES_SIZE) == 0)) {
WOLFSSL_MSG(EVP_AES_128_ECB);
ctx->cipherType = AES_128_ECB_TYPE;
ctx->keyLen = 16;
ctx->block_size = 16;
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, NULL,
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
}
if (ret != 0)
return ret;
}
else if (ctx->cipherType == AES_192_ECB_TYPE ||
(type && XSTRNCMP(type, EVP_AES_192_ECB, EVP_AES_SIZE) == 0)) {
WOLFSSL_MSG(EVP_AES_192_ECB);
ctx->cipherType = AES_192_ECB_TYPE;
ctx->keyLen = 24;
ctx->block_size = 16;
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
if(ctx->enc)
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, NULL,
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
}
if (ret != 0)
return ret;
}
else if (ctx->cipherType == AES_256_ECB_TYPE ||
(type && XSTRNCMP(type, EVP_AES_256_ECB, EVP_AES_SIZE) == 0)) {
WOLFSSL_MSG(EVP_AES_256_ECB);
ctx->cipherType = AES_256_ECB_TYPE;
ctx->keyLen = 32;
ctx->block_size = 16;
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, NULL,
ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
}
if (ret != 0)
return ret;
}
2016-06-27 10:53:34 -07:00
#endif /* NO_AES */
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_DES3
if (ctx->cipherType == DES_CBC_TYPE ||
(type && XSTRNCMP(type, EVP_DES_CBC, EVP_DES_SIZE) == 0)) {
WOLFSSL_MSG(EVP_DES_CBC);
ctx->cipherType = DES_CBC_TYPE;
ctx->keyLen = 8;
2016-10-13 22:10:50 +09:00
ctx->block_size = 8;
2016-06-27 10:53:34 -07:00
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_Des_SetKey(&ctx->cipher.des, key, iv,
ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
if (ret != 0)
return ret;
}
if (iv && key == NULL)
wc_Des_SetIV(&ctx->cipher.des, iv);
}
2016-10-13 22:10:50 +09:00
#ifdef WOLFSSL_DES_ECB
else if (ctx->cipherType == DES_ECB_TYPE ||
(type && XSTRNCMP(type, EVP_DES_ECB, EVP_DES_SIZE) == 0)) {
WOLFSSL_MSG(EVP_DES_ECB);
ctx->cipherType = DES_ECB_TYPE;
ctx->keyLen = 8;
ctx->block_size = 8;
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_Des_SetKey(&ctx->cipher.des, key, NULL,
ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
if (ret != 0)
return ret;
}
}
#endif
2016-06-27 10:53:34 -07:00
else if (ctx->cipherType == DES_EDE3_CBC_TYPE ||
(type &&
XSTRNCMP(type, EVP_DES_EDE3_CBC, EVP_DES_EDE3_SIZE) == 0)) {
WOLFSSL_MSG(EVP_DES_EDE3_CBC);
ctx->cipherType = DES_EDE3_CBC_TYPE;
ctx->keyLen = 24;
2016-10-13 22:10:50 +09:00
ctx->block_size = 8;
2016-06-27 10:53:34 -07:00
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_Des3_SetKey(&ctx->cipher.des3, key, iv,
ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
if (ret != 0)
return ret;
}
if (iv && key == NULL) {
ret = wc_Des3_SetIV(&ctx->cipher.des3, iv);
if (ret != 0)
return ret;
}
2012-05-07 16:35:23 -07:00
}
2016-10-13 22:10:50 +09:00
else if (ctx->cipherType == DES_EDE3_ECB_TYPE ||
(type &&
XSTRNCMP(type, EVP_DES_EDE3_ECB, EVP_DES_EDE3_SIZE) == 0)) {
WOLFSSL_MSG(EVP_DES_EDE3_ECB);
ctx->cipherType = DES_EDE3_ECB_TYPE;
ctx->keyLen = 24;
ctx->block_size = 8;
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_Des3_SetKey(&ctx->cipher.des3, key, NULL,
ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
if (ret != 0)
return ret;
}
}
2016-06-27 10:53:34 -07:00
#endif /* NO_DES3 */
#ifndef NO_RC4
if (ctx->cipherType == ARC4_TYPE || (type &&
XSTRNCMP(type, "ARC4", 4) == 0)) {
WOLFSSL_MSG("ARC4");
ctx->cipherType = ARC4_TYPE;
if (ctx->keyLen == 0) /* user may have already set */
ctx->keyLen = 16; /* default to 128 */
if (key)
wc_Arc4SetKey(&ctx->cipher.arc4, key, ctx->keyLen);
ret = 0; /* success */
}
#endif /* NO_RC4 */
#ifdef HAVE_IDEA
if (ctx->cipherType == IDEA_CBC_TYPE ||
(type && XSTRNCMP(type, EVP_IDEA_CBC, EVP_IDEA_SIZE) == 0)) {
WOLFSSL_MSG(EVP_IDEA_CBC);
ctx->cipherType = IDEA_CBC_TYPE;
ctx->keyLen = IDEA_KEY_SIZE;
if (enc == 0 || enc == 1)
ctx->enc = enc ? 1 : 0;
if (key) {
ret = wc_IdeaSetKey(&ctx->cipher.idea, key, (word16)ctx->keyLen,
iv, ctx->enc ? IDEA_ENCRYPTION :
IDEA_DECRYPTION);
if (ret != 0)
return ret;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (iv && key == NULL)
wc_IdeaSetIV(&ctx->cipher.idea, iv);
}
#endif /* HAVE_IDEA */
if (ctx->cipherType == NULL_CIPHER_TYPE || (type &&
XSTRNCMP(type, "NULL", 4) == 0)) {
WOLFSSL_MSG("NULL cipher");
ctx->cipherType = NULL_CIPHER_TYPE;
ctx->keyLen = 0;
ret = 0; /* success */
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
if (ret == 0)
return SSL_SUCCESS;
else
return 0; /* overall failure */
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_EVP_CIPHER_CTX_key_length(WOLFSSL_EVP_CIPHER_CTX* ctx)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EVP_CIPHER_CTX_key_length");
if (ctx)
return ctx->keyLen;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
return 0; /* failure */
}
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_EVP_CIPHER_CTX_set_key_length(WOLFSSL_EVP_CIPHER_CTX* ctx,
int keylen)
{
WOLFSSL_ENTER("wolfSSL_EVP_CIPHER_CTX_set_key_length");
if (ctx)
ctx->keyLen = keylen;
else
return 0; /* failure */
return SSL_SUCCESS;
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_EVP_Cipher(WOLFSSL_EVP_CIPHER_CTX* ctx, byte* dst, byte* src,
word32 len)
{
2016-06-27 10:53:34 -07:00
int ret = 0;
WOLFSSL_ENTER("wolfSSL_EVP_Cipher");
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
if (ctx == NULL || dst == NULL || src == NULL) {
WOLFSSL_MSG("Bad function argument");
return 0; /* failure */
}
2016-06-27 10:53:34 -07:00
if (ctx->cipherType == 0xff) {
WOLFSSL_MSG("no init");
return 0; /* failure */
}
switch (ctx->cipherType) {
#ifndef NO_AES
#ifdef HAVE_AES_CBC
case AES_128_CBC_TYPE :
case AES_192_CBC_TYPE :
case AES_256_CBC_TYPE :
WOLFSSL_MSG("AES CBC");
if (ctx->enc)
ret = wc_AesCbcEncrypt(&ctx->cipher.aes, dst, src, len);
else
ret = wc_AesCbcDecrypt(&ctx->cipher.aes, dst, src, len);
break;
2016-06-27 10:53:34 -07:00
#endif /* HAVE_AES_CBC */
#ifdef HAVE_AES_ECB
2016-10-13 22:10:50 +09:00
case AES_128_ECB_TYPE :
case AES_192_ECB_TYPE :
case AES_256_ECB_TYPE :
WOLFSSL_MSG("AES ECB");
if (ctx->enc)
ret = wc_AesEcbEncrypt(&ctx->cipher.aes, dst, src, len);
else
ret = wc_AesEcbDecrypt(&ctx->cipher.aes, dst, src, len);
break;
#endif
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_AES_COUNTER
case AES_128_CTR_TYPE :
case AES_192_CTR_TYPE :
case AES_256_CTR_TYPE :
WOLFSSL_MSG("AES CTR");
wc_AesCtrEncrypt(&ctx->cipher.aes, dst, src, len);
break;
2016-06-27 10:53:34 -07:00
#endif /* WOLFSSL_AES_COUNTER */
#endif /* NO_AES */
#ifndef NO_DES3
case DES_CBC_TYPE :
if (ctx->enc)
wc_Des_CbcEncrypt(&ctx->cipher.des, dst, src, len);
else
wc_Des_CbcDecrypt(&ctx->cipher.des, dst, src, len);
break;
2016-10-13 22:10:50 +09:00
#ifdef WOLFSSL_DES_ECB
case DES_ECB_TYPE :
if (ctx->enc)
wc_Des_EbcEncrypt(&ctx->cipher.des, dst, src, len);
else
wc_Des_EbcDecrypt(&ctx->cipher.des, dst, src, len);
break;
#endif
2016-06-27 10:53:34 -07:00
case DES_EDE3_CBC_TYPE :
if (ctx->enc)
ret = wc_Des3_CbcEncrypt(&ctx->cipher.des3, dst, src, len);
else
ret = wc_Des3_CbcDecrypt(&ctx->cipher.des3, dst, src, len);
break;
2016-10-13 22:10:50 +09:00
#ifdef WOLFSSL_DES_ECB
case DES_EDE3_ECB_TYPE :
if (ctx->enc)
ret = wc_Des3_EcbEncrypt(&ctx->cipher.des3, dst, src, len);
else
ret = wc_Des3_EcbDecrypt(&ctx->cipher.des3, dst, src, len);
break;
#endif
2016-06-27 10:53:34 -07:00
#endif
#ifndef NO_RC4
case ARC4_TYPE :
wc_Arc4Process(&ctx->cipher.arc4, dst, src, len);
break;
2016-06-27 10:53:34 -07:00
#endif
#ifdef HAVE_IDEA
case IDEA_CBC_TYPE :
if (ctx->enc)
wc_IdeaCbcEncrypt(&ctx->cipher.idea, dst, src, len);
else
wc_IdeaCbcDecrypt(&ctx->cipher.idea, dst, src, len);
break;
2016-06-27 10:53:34 -07:00
#endif
case NULL_CIPHER_TYPE :
XMEMCPY(dst, src, len);
2012-05-01 16:12:12 -07:00
break;
2016-06-27 10:53:34 -07:00
default: {
WOLFSSL_MSG("bad type");
return 0; /* failure */
}
}
2016-06-27 10:53:34 -07:00
if (ret != 0) {
WOLFSSL_MSG("wolfSSL_EVP_Cipher failure");
return 0; /* failuer */
}
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_EVP_Cipher success");
return SSL_SUCCESS; /* success */
2012-04-26 14:58:29 -07:00
}
2016-10-13 22:10:50 +09:00
#include "wolfcrypt/src/evp.c"
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
/* store for external read of iv, SSL_SUCCESS on success */
int wolfSSL_StoreExternalIV(WOLFSSL_EVP_CIPHER_CTX* ctx)
{
WOLFSSL_ENTER("wolfSSL_StoreExternalIV");
2016-06-27 10:53:34 -07:00
if (ctx == NULL) {
WOLFSSL_MSG("Bad function argument");
return SSL_FATAL_ERROR;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
switch (ctx->cipherType) {
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_AES
case AES_128_CBC_TYPE :
case AES_192_CBC_TYPE :
case AES_256_CBC_TYPE :
WOLFSSL_MSG("AES CBC");
XMEMCPY(ctx->iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
break;
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_AES_COUNTER
case AES_128_CTR_TYPE :
case AES_192_CTR_TYPE :
case AES_256_CTR_TYPE :
WOLFSSL_MSG("AES CTR");
XMEMCPY(ctx->iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
break;
#endif /* WOLFSSL_AES_COUNTER */
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#endif /* NO_AES */
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_DES3
case DES_CBC_TYPE :
WOLFSSL_MSG("DES CBC");
XMEMCPY(ctx->iv, &ctx->cipher.des.reg, DES_BLOCK_SIZE);
break;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
case DES_EDE3_CBC_TYPE :
WOLFSSL_MSG("DES EDE3 CBC");
XMEMCPY(ctx->iv, &ctx->cipher.des3.reg, DES_BLOCK_SIZE);
break;
#endif
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifdef HAVE_IDEA
case IDEA_CBC_TYPE :
WOLFSSL_MSG("IDEA CBC");
XMEMCPY(ctx->iv, &ctx->cipher.idea.reg, IDEA_BLOCK_SIZE);
break;
#endif
case ARC4_TYPE :
WOLFSSL_MSG("ARC4");
break;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
case NULL_CIPHER_TYPE :
WOLFSSL_MSG("NULL");
break;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
default: {
WOLFSSL_MSG("bad type");
return SSL_FATAL_ERROR;
}
}
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2012-04-26 14:58:29 -07:00
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
/* set internal IV from external, SSL_SUCCESS on success */
int wolfSSL_SetInternalIV(WOLFSSL_EVP_CIPHER_CTX* ctx)
2012-05-07 16:35:23 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_SetInternalIV");
2016-06-27 10:53:34 -07:00
if (ctx == NULL) {
WOLFSSL_MSG("Bad function argument");
return SSL_FATAL_ERROR;
2012-05-07 16:35:23 -07:00
}
2016-06-27 10:53:34 -07:00
switch (ctx->cipherType) {
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_AES
case AES_128_CBC_TYPE :
case AES_192_CBC_TYPE :
case AES_256_CBC_TYPE :
WOLFSSL_MSG("AES CBC");
XMEMCPY(&ctx->cipher.aes.reg, ctx->iv, AES_BLOCK_SIZE);
break;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_AES_COUNTER
case AES_128_CTR_TYPE :
case AES_192_CTR_TYPE :
case AES_256_CTR_TYPE :
WOLFSSL_MSG("AES CTR");
XMEMCPY(&ctx->cipher.aes.reg, ctx->iv, AES_BLOCK_SIZE);
break;
#endif
2015-03-20 11:25:22 -06:00
2016-06-27 10:53:34 -07:00
#endif /* NO_AES */
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_DES3
case DES_CBC_TYPE :
WOLFSSL_MSG("DES CBC");
XMEMCPY(&ctx->cipher.des.reg, ctx->iv, DES_BLOCK_SIZE);
break;
2016-06-27 10:53:34 -07:00
case DES_EDE3_CBC_TYPE :
WOLFSSL_MSG("DES EDE3 CBC");
XMEMCPY(&ctx->cipher.des3.reg, ctx->iv, DES_BLOCK_SIZE);
break;
#endif
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifdef HAVE_IDEA
case IDEA_CBC_TYPE :
WOLFSSL_MSG("IDEA CBC");
XMEMCPY(&ctx->cipher.idea.reg, ctx->iv, IDEA_BLOCK_SIZE);
break;
#endif
case ARC4_TYPE :
WOLFSSL_MSG("ARC4");
break;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
case NULL_CIPHER_TYPE :
WOLFSSL_MSG("NULL");
break;
default: {
WOLFSSL_MSG("bad type");
return SSL_FATAL_ERROR;
}
}
return SSL_SUCCESS;
2012-05-07 16:35:23 -07:00
}
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_EVP_DigestInit(WOLFSSL_EVP_MD_CTX* ctx,
const WOLFSSL_EVP_MD* type)
2012-05-07 16:35:23 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("EVP_DigestInit");
if (XSTRNCMP(type, "SHA256", 6) == 0) {
ctx->macType = SHA256;
wolfSSL_SHA256_Init((SHA256_CTX*)&ctx->hash);
}
#ifdef WOLFSSL_SHA224
else if (XSTRNCMP(type, "SHA224", 6) == 0) {
ctx->macType = SHA224;
wolfSSL_SHA224_Init((SHA224_CTX*)&ctx->hash);
}
#endif
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SHA384
else if (XSTRNCMP(type, "SHA384", 6) == 0) {
ctx->macType = SHA384;
wolfSSL_SHA384_Init((SHA384_CTX*)&ctx->hash);
}
#endif
#ifdef WOLFSSL_SHA512
else if (XSTRNCMP(type, "SHA512", 6) == 0) {
ctx->macType = SHA512;
wolfSSL_SHA512_Init((SHA512_CTX*)&ctx->hash);
}
#endif
#ifndef NO_MD5
else if (XSTRNCMP(type, "MD5", 3) == 0) {
ctx->macType = MD5;
wolfSSL_MD5_Init((MD5_CTX*)&ctx->hash);
}
#endif
#ifndef NO_SHA
/* has to be last since would pick or 224, 256, 384, or 512 too */
2016-06-27 10:53:34 -07:00
else if (XSTRNCMP(type, "SHA", 3) == 0) {
ctx->macType = SHA;
wolfSSL_SHA_Init((SHA_CTX*)&ctx->hash);
}
#endif /* NO_SHA */
else
return BAD_FUNC_ARG;
2015-03-20 11:25:22 -06:00
return SSL_SUCCESS;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_EVP_DigestUpdate(WOLFSSL_EVP_MD_CTX* ctx, const void* data,
unsigned long sz)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("EVP_DigestUpdate");
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
switch (ctx->macType) {
#ifndef NO_MD5
case MD5:
wolfSSL_MD5_Update((MD5_CTX*)&ctx->hash, data,
(unsigned long)sz);
break;
#endif
#ifndef NO_SHA
case SHA:
wolfSSL_SHA_Update((SHA_CTX*)&ctx->hash, data,
(unsigned long)sz);
break;
#endif
#ifdef WOLFSSL_SHA224
case SHA224:
wolfSSL_SHA224_Update((SHA224_CTX*)&ctx->hash, data,
(unsigned long)sz);
break;
#endif
2016-06-27 10:53:34 -07:00
#ifndef NO_SHA256
case SHA256:
wolfSSL_SHA256_Update((SHA256_CTX*)&ctx->hash, data,
(unsigned long)sz);
break;
#endif
#ifdef WOLFSSL_SHA384
case SHA384:
wolfSSL_SHA384_Update((SHA384_CTX*)&ctx->hash, data,
(unsigned long)sz);
break;
#endif
#ifdef WOLFSSL_SHA512
case SHA512:
wolfSSL_SHA512_Update((SHA512_CTX*)&ctx->hash, data,
(unsigned long)sz);
break;
#endif
default:
return BAD_FUNC_ARG;
}
2012-05-07 16:35:23 -07:00
return SSL_SUCCESS;
2012-05-07 16:35:23 -07:00
}
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_EVP_DigestFinal(WOLFSSL_EVP_MD_CTX* ctx, unsigned char* md,
unsigned int* s)
2011-02-05 11:14:47 -08:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("EVP_DigestFinal");
switch (ctx->macType) {
#ifndef NO_MD5
case MD5:
wolfSSL_MD5_Final(md, (MD5_CTX*)&ctx->hash);
if (s) *s = MD5_DIGEST_SIZE;
break;
#endif
#ifndef NO_SHA
case SHA:
wolfSSL_SHA_Final(md, (SHA_CTX*)&ctx->hash);
if (s) *s = SHA_DIGEST_SIZE;
break;
#endif
#ifdef WOLFSSL_SHA224
case SHA224:
wolfSSL_SHA224_Final(md, (SHA224_CTX*)&ctx->hash);
if (s) *s = SHA224_DIGEST_SIZE;
break;
#endif
2016-06-27 10:53:34 -07:00
#ifndef NO_SHA256
case SHA256:
wolfSSL_SHA256_Final(md, (SHA256_CTX*)&ctx->hash);
if (s) *s = SHA256_DIGEST_SIZE;
break;
#endif
#ifdef WOLFSSL_SHA384
case SHA384:
wolfSSL_SHA384_Final(md, (SHA384_CTX*)&ctx->hash);
if (s) *s = SHA384_DIGEST_SIZE;
break;
#endif
#ifdef WOLFSSL_SHA512
case SHA512:
wolfSSL_SHA512_Final(md, (SHA512_CTX*)&ctx->hash);
if (s) *s = SHA512_DIGEST_SIZE;
break;
#endif
default:
return BAD_FUNC_ARG;
}
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_EVP_DigestFinal_ex(WOLFSSL_EVP_MD_CTX* ctx, unsigned char* md,
unsigned int* s)
2011-02-05 11:14:47 -08:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("EVP_DigestFinal_ex");
return EVP_DigestFinal(ctx, md, s);
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
unsigned char* wolfSSL_HMAC(const WOLFSSL_EVP_MD* evp_md, const void* key,
int key_len, const unsigned char* d, int n,
unsigned char* md, unsigned int* md_len)
2011-02-05 11:14:47 -08:00
{
2016-06-27 10:53:34 -07:00
int type;
unsigned char* ret = NULL;
#ifdef WOLFSSL_SMALL_STACK
Hmac* hmac = NULL;
#else
Hmac hmac[1];
#endif
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("HMAC");
if (!md)
return NULL; /* no static buffer support */
if (XSTRNCMP(evp_md, "MD5", 3) == 0)
type = MD5;
else if (XSTRNCMP(evp_md, "SHA", 3) == 0)
type = SHA;
else
return NULL;
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
hmac = (Hmac*)XMALLOC(sizeof(Hmac), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (hmac == NULL)
return NULL;
#endif
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (wc_HmacSetKey(hmac, type, (const byte*)key, key_len) == 0)
if (wc_HmacUpdate(hmac, d, n) == 0)
if (wc_HmacFinal(hmac, md) == 0) {
if (md_len)
*md_len = (type == MD5) ? (int)MD5_DIGEST_SIZE
: (int)SHA_DIGEST_SIZE;
ret = md;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(hmac, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return ret;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
void wolfSSL_ERR_clear_error(void)
{
/* TODO: */
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_RAND_status(void)
{
return SSL_SUCCESS; /* wolfCrypt provides enough seed internally */
}
2014-03-27 15:43:54 -07:00
2011-02-05 11:14:47 -08:00
2014-09-08 21:34:07 -03:00
2016-06-27 10:53:34 -07:00
void wolfSSL_RAND_add(const void* add, int len, double entropy)
{
(void)add;
(void)len;
(void)entropy;
2016-06-27 10:53:34 -07:00
/* wolfSSL seeds/adds internally, use explicit RNG if you want
to take control */
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
#ifndef NO_DES3
/* SSL_SUCCESS on ok */
int wolfSSL_DES_key_sched(WOLFSSL_const_DES_cblock* key,
WOLFSSL_DES_key_schedule* schedule)
{
WOLFSSL_ENTER("DES_key_sched");
XMEMCPY(schedule, key, sizeof(const_DES_cblock));
return SSL_SUCCESS;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
void wolfSSL_DES_cbc_encrypt(const unsigned char* input,
unsigned char* output, long length,
WOLFSSL_DES_key_schedule* schedule,
WOLFSSL_DES_cblock* ivec, int enc)
{
Des myDes;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("DES_cbc_encrypt");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
/* OpenSSL compat, no ret */
wc_Des_SetKey(&myDes, (const byte*)schedule, (const byte*)ivec, !enc);
2012-04-26 14:58:29 -07:00
2016-06-27 10:53:34 -07:00
if (enc)
wc_Des_CbcEncrypt(&myDes, output, input, (word32)length);
else
wc_Des_CbcDecrypt(&myDes, output, input, (word32)length);
2012-04-26 14:58:29 -07:00
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
/* WOLFSSL_DES_key_schedule is a unsigned char array of size 8 */
void wolfSSL_DES_ede3_cbc_encrypt(const unsigned char* input,
unsigned char* output, long sz,
WOLFSSL_DES_key_schedule* ks1,
WOLFSSL_DES_key_schedule* ks2,
WOLFSSL_DES_key_schedule* ks3,
WOLFSSL_DES_cblock* ivec, int enc)
{
Des3 des;
byte key[24];/* EDE uses 24 size key */
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_DES_ede3_cbc_encrypt");
2016-06-27 10:53:34 -07:00
XMEMSET(key, 0, sizeof(key));
XMEMCPY(key, *ks1, DES_BLOCK_SIZE);
XMEMCPY(&key[DES_BLOCK_SIZE], *ks2, DES_BLOCK_SIZE);
XMEMCPY(&key[DES_BLOCK_SIZE * 2], *ks3, DES_BLOCK_SIZE);
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (enc) {
wc_Des3_SetKey(&des, key, (const byte*)ivec, DES_ENCRYPTION);
wc_Des3_CbcEncrypt(&des, output, input, (word32)sz);
}
else {
wc_Des3_SetKey(&des, key, (const byte*)ivec, DES_DECRYPTION);
wc_Des3_CbcDecrypt(&des, output, input, (word32)sz);
}
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
/* correctly sets ivec for next call */
void wolfSSL_DES_ncbc_encrypt(const unsigned char* input,
unsigned char* output, long length,
WOLFSSL_DES_key_schedule* schedule, WOLFSSL_DES_cblock* ivec,
int enc)
{
Des myDes;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("DES_ncbc_encrypt");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
/* OpenSSL compat, no ret */
wc_Des_SetKey(&myDes, (const byte*)schedule, (const byte*)ivec, !enc);
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (enc)
wc_Des_CbcEncrypt(&myDes, output, input, (word32)length);
else
wc_Des_CbcDecrypt(&myDes, output, input, (word32)length);
2015-03-20 10:59:54 -06:00
2016-06-27 10:53:34 -07:00
XMEMCPY(ivec, output + length - sizeof(DES_cblock), sizeof(DES_cblock));
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#endif /* NO_DES3 */
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
void wolfSSL_ERR_free_strings(void)
{
/* handled internally */
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_ERR_remove_state(unsigned long state)
{
/* TODO: GetErrors().Remove(); */
(void)state;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
void wolfSSL_EVP_cleanup(void)
{
/* nothing to do here */
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
void wolfSSL_cleanup_all_ex_data(void)
{
/* nothing to do here */
}
2015-02-26 10:40:32 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_clear(WOLFSSL* ssl)
{
(void)ssl;
/* TODO: GetErrors().Remove(); */
return SSL_SUCCESS;
}
2015-02-26 10:40:32 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_SSL_SESSION_set_timeout(WOLFSSL_SESSION* ses, long t)
{
word32 tmptime;
if (!ses || t < 0)
return BAD_FUNC_ARG;
2015-02-26 10:40:32 -07:00
2016-06-27 10:53:34 -07:00
tmptime = t & 0xFFFFFFFF;
2015-02-26 10:40:32 -07:00
2016-06-27 10:53:34 -07:00
ses->timeout = tmptime;
2015-02-26 10:40:32 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_set_mode(WOLFSSL_CTX* ctx, long mode)
{
/* SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER is wolfSSL default mode */
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SSL_CTX_set_mode");
if (mode == SSL_MODE_ENABLE_PARTIAL_WRITE)
ctx->partialWrite = 1;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return mode;
}
2015-02-26 10:40:32 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_SSL_get_mode(WOLFSSL* ssl)
{
/* TODO: */
(void)ssl;
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_get_mode(WOLFSSL_CTX* ctx)
{
/* TODO: */
(void)ctx;
return 0;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_CTX_set_default_read_ahead(WOLFSSL_CTX* ctx, int m)
{
/* TODO: maybe? */
(void)ctx;
(void)m;
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
int wolfSSL_CTX_set_session_id_context(WOLFSSL_CTX* ctx,
const unsigned char* sid_ctx,
unsigned int sid_ctx_len)
{
/* No application specific context needed for wolfSSL */
(void)ctx;
(void)sid_ctx;
(void)sid_ctx_len;
return SSL_SUCCESS;
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_sess_get_cache_size(WOLFSSL_CTX* ctx)
{
/* TODO: maybe? */
(void)ctx;
return (~0);
}
2016-06-27 10:53:34 -07:00
unsigned long wolfSSL_ERR_get_error_line_data(const char** file, int* line,
const char** data, int *flags)
{
/* Not implemented */
(void)file;
(void)line;
(void)data;
(void)flags;
return 0;
}
2016-06-27 10:53:34 -07:00
#endif /* OPENSSL_EXTRA */
2016-06-27 10:53:34 -07:00
#if defined(KEEP_PEER_CERT)
2016-06-27 10:53:34 -07:00
WOLFSSL_X509* wolfSSL_get_peer_certificate(WOLFSSL* ssl)
{
WOLFSSL_ENTER("SSL_get_peer_certificate");
if (ssl->peerCert.issuer.sz)
return &ssl->peerCert;
else
return 0;
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
#endif /* KEEP_PEER_CERT */
2016-06-27 10:53:34 -07:00
#ifndef NO_CERTS
#if defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || defined(OPENSSL_EXTRA)
2016-06-27 10:53:34 -07:00
/* user externally called free X509, if dynamic go ahead with free, otherwise
* don't */
static void ExternalFreeX509(WOLFSSL_X509* x509)
{
WOLFSSL_ENTER("ExternalFreeX509");
if (x509) {
if (x509->dynamicMemory) {
FreeX509(x509);
XFREE(x509, x509->heap, DYNAMIC_TYPE_X509);
} else {
WOLFSSL_MSG("free called on non dynamic object, not freeing");
}
}
}
2016-06-27 10:53:34 -07:00
#endif /* KEEP_PEER_CERT || SESSION_CERTS || OPENSSSL_EXTRA */
2016-06-27 10:53:34 -07:00
#if defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)
2016-06-27 10:53:34 -07:00
void wolfSSL_FreeX509(WOLFSSL_X509* x509)
{
WOLFSSL_ENTER("wolfSSL_FreeX509");
ExternalFreeX509(x509);
}
2016-06-27 10:53:34 -07:00
/* return the next, if any, altname from the peer cert */
char* wolfSSL_X509_get_next_altname(WOLFSSL_X509* cert)
{
char* ret = NULL;
WOLFSSL_ENTER("wolfSSL_X509_get_next_altname");
/* don't have any to work with */
if (cert == NULL || cert->altNames == NULL)
return NULL;
2016-06-27 10:53:34 -07:00
/* already went through them */
if (cert->altNamesNext == NULL)
return NULL;
ret = cert->altNamesNext->name;
cert->altNamesNext = cert->altNamesNext->next;
return ret;
}
2016-06-27 10:53:34 -07:00
WOLFSSL_X509_NAME* wolfSSL_X509_get_issuer_name(WOLFSSL_X509* cert)
{
WOLFSSL_ENTER("X509_get_issuer_name");
if(cert)
return &cert->issuer;
return NULL;
}
2016-06-27 10:53:34 -07:00
WOLFSSL_X509_NAME* wolfSSL_X509_get_subject_name(WOLFSSL_X509* cert)
{
WOLFSSL_ENTER("wolfSSL_X509_get_subject_name");
if(cert)
return &cert->subject;
return NULL;
}
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_get_isCA(WOLFSSL_X509* x509)
{
int isCA = 0;
WOLFSSL_ENTER("wolfSSL_X509_get_isCA");
if (x509 != NULL)
isCA = x509->isCa;
WOLFSSL_LEAVE("wolfSSL_X509_get_isCA", isCA);
return isCA;
}
#ifdef OPENSSL_EXTRA
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_ext_isSet_by_NID(WOLFSSL_X509* x509, int nid)
{
int isSet = 0;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_ext_isSet_by_NID");
if (x509 != NULL) {
switch (nid) {
case BASIC_CA_OID: isSet = x509->basicConstSet; break;
case ALT_NAMES_OID: isSet = x509->subjAltNameSet; break;
case AUTH_KEY_OID: isSet = x509->authKeyIdSet; break;
case SUBJ_KEY_OID: isSet = x509->subjKeyIdSet; break;
case KEY_USAGE_OID: isSet = x509->keyUsageSet; break;
#ifdef WOLFSSL_SEP
case CERT_POLICY_OID: isSet = x509->certPolicySet; break;
#endif /* WOLFSSL_SEP */
}
}
2016-06-27 10:53:34 -07:00
WOLFSSL_LEAVE("wolfSSL_X509_ext_isSet_by_NID", isSet);
return isSet;
}
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_ext_get_critical_by_NID(WOLFSSL_X509* x509, int nid)
{
int crit = 0;
WOLFSSL_ENTER("wolfSSL_X509_ext_get_critical_by_NID");
if (x509 != NULL) {
switch (nid) {
case BASIC_CA_OID: crit = x509->basicConstCrit; break;
case ALT_NAMES_OID: crit = x509->subjAltNameCrit; break;
case AUTH_KEY_OID: crit = x509->authKeyIdCrit; break;
case SUBJ_KEY_OID: crit = x509->subjKeyIdCrit; break;
case KEY_USAGE_OID: crit = x509->keyUsageCrit; break;
#ifdef WOLFSSL_SEP
case CERT_POLICY_OID: crit = x509->certPolicyCrit; break;
#endif /* WOLFSSL_SEP */
}
}
2016-06-27 10:53:34 -07:00
WOLFSSL_LEAVE("wolfSSL_X509_ext_get_critical_by_NID", crit);
return crit;
}
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_get_isSet_pathLength(WOLFSSL_X509* x509)
{
int isSet = 0;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_get_isSet_pathLength");
2016-06-27 10:53:34 -07:00
if (x509 != NULL)
isSet = x509->basicConstPlSet;
2016-06-27 10:53:34 -07:00
WOLFSSL_LEAVE("wolfSSL_X509_get_isSet_pathLength", isSet);
2016-06-27 10:53:34 -07:00
return isSet;
}
2016-06-27 10:53:34 -07:00
word32 wolfSSL_X509_get_pathLength(WOLFSSL_X509* x509)
{
word32 pathLength = 0;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_get_pathLength");
2016-06-27 10:53:34 -07:00
if (x509 != NULL)
pathLength = x509->pathLength;
2016-06-27 10:53:34 -07:00
WOLFSSL_LEAVE("wolfSSL_X509_get_pathLength", pathLength);
2016-06-27 10:53:34 -07:00
return pathLength;
}
2016-06-27 10:53:34 -07:00
unsigned int wolfSSL_X509_get_keyUsage(WOLFSSL_X509* x509)
{
word16 usage = 0;
2014-07-10 11:18:49 -06:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_get_keyUsage");
2012-05-03 08:18:59 -07:00
2016-06-27 10:53:34 -07:00
if (x509 != NULL)
usage = x509->keyUsage;
2012-05-03 09:57:17 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_LEAVE("wolfSSL_X509_get_keyUsage", usage);
2016-06-27 10:53:34 -07:00
return usage;
}
2013-01-15 15:20:30 -08:00
2016-06-27 10:53:34 -07:00
byte* wolfSSL_X509_get_authorityKeyID(WOLFSSL_X509* x509,
byte* dst, int* dstLen)
{
byte *id = NULL;
int copySz = 0;
WOLFSSL_ENTER("wolfSSL_X509_get_authorityKeyID");
if (x509 != NULL) {
if (x509->authKeyIdSet) {
copySz = min(dstLen != NULL ? *dstLen : 0,
(int)x509->authKeyIdSz);
id = x509->authKeyId;
}
if (dst != NULL && dstLen != NULL && id != NULL && copySz > 0) {
XMEMCPY(dst, id, copySz);
id = dst;
*dstLen = copySz;
}
}
2016-06-27 10:53:34 -07:00
WOLFSSL_LEAVE("wolfSSL_X509_get_authorityKeyID", copySz);
return id;
}
byte* wolfSSL_X509_get_subjectKeyID(WOLFSSL_X509* x509,
byte* dst, int* dstLen)
{
byte *id = NULL;
int copySz = 0;
WOLFSSL_ENTER("wolfSSL_X509_get_subjectKeyID");
if (x509 != NULL) {
if (x509->subjKeyIdSet) {
copySz = min(dstLen != NULL ? *dstLen : 0,
(int)x509->subjKeyIdSz);
id = x509->subjKeyId;
}
if (dst != NULL && dstLen != NULL && id != NULL && copySz > 0) {
XMEMCPY(dst, id, copySz);
id = dst;
*dstLen = copySz;
}
}
2014-09-08 21:34:07 -03:00
2016-06-27 10:53:34 -07:00
WOLFSSL_LEAVE("wolfSSL_X509_get_subjectKeyID", copySz);
2016-06-27 10:53:34 -07:00
return id;
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_NAME_entry_count(WOLFSSL_X509_NAME* name)
{
int count = 0;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_NAME_entry_count");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (name != NULL)
count = name->fullName.entryCount;
2016-06-27 10:53:34 -07:00
WOLFSSL_LEAVE("wolfSSL_X509_NAME_entry_count", count);
return count;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_NAME_get_text_by_NID(WOLFSSL_X509_NAME* name,
int nid, char* buf, int len)
{
char *text = NULL;
int textSz = 0;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_NAME_get_text_by_NID");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
switch (nid) {
case ASN_COMMON_NAME:
text = name->fullName.fullName + name->fullName.cnIdx;
textSz = name->fullName.cnLen;
break;
case ASN_SUR_NAME:
text = name->fullName.fullName + name->fullName.snIdx;
textSz = name->fullName.snLen;
break;
case ASN_SERIAL_NUMBER:
text = name->fullName.fullName + name->fullName.serialIdx;
textSz = name->fullName.serialLen;
break;
case ASN_COUNTRY_NAME:
text = name->fullName.fullName + name->fullName.cIdx;
textSz = name->fullName.cLen;
break;
case ASN_LOCALITY_NAME:
text = name->fullName.fullName + name->fullName.lIdx;
textSz = name->fullName.lLen;
break;
case ASN_STATE_NAME:
text = name->fullName.fullName + name->fullName.stIdx;
textSz = name->fullName.stLen;
break;
case ASN_ORG_NAME:
text = name->fullName.fullName + name->fullName.oIdx;
textSz = name->fullName.oLen;
break;
case ASN_ORGUNIT_NAME:
text = name->fullName.fullName + name->fullName.ouIdx;
textSz = name->fullName.ouLen;
break;
default:
break;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (buf != NULL && text != NULL) {
textSz = min(textSz, len);
XMEMCPY(buf, text, textSz);
buf[textSz] = '\0';
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_LEAVE("wolfSSL_X509_NAME_get_text_by_NID", textSz);
return textSz;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_NAME_get_index_by_NID(WOLFSSL_X509_NAME* name,
int nid, int pos)
{
int ret = -1;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_NAME_get_index_by_NID");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (name == NULL) {
return BAD_FUNC_ARG;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
/* these index values are already stored in DecodedName
use those when available */
if (name->fullName.fullName && name->fullName.fullNameLen > 0) {
switch (nid) {
case ASN_COMMON_NAME:
ret = name->fullName.cnIdx;
break;
default:
WOLFSSL_MSG("NID not yet implemented");
break;
}
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_LEAVE("wolfSSL_X509_NAME_get_index_by_NID", ret);
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
(void)pos;
(void)nid;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return ret;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ASN1_STRING* wolfSSL_X509_NAME_ENTRY_get_data(
WOLFSSL_X509_NAME_ENTRY* in)
{
WOLFSSL_ENTER("wolfSSL_X509_NAME_ENTRY_get_data");
return in->value;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
char* wolfSSL_ASN1_STRING_data(WOLFSSL_ASN1_STRING* asn)
{
WOLFSSL_ENTER("wolfSSL_ASN1_STRING_data");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (asn) {
return asn->data;
}
else {
return NULL;
}
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_ASN1_STRING_length(WOLFSSL_ASN1_STRING* asn)
{
WOLFSSL_ENTER("wolfSSL_ASN1_STRING_length");
2016-06-27 10:53:34 -07:00
if (asn) {
return asn->length;
}
else {
return 0;
}
}
#endif
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
/* copy name into in buffer, at most sz bytes, if buffer is null will
malloc buffer, call responsible for freeing */
char* wolfSSL_X509_NAME_oneline(WOLFSSL_X509_NAME* name, char* in, int sz)
{
int copySz = min(sz, name->sz);
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_NAME_oneline");
if (!name->sz) return in;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (!in) {
#ifdef WOLFSSL_STATIC_MEMORY
WOLFSSL_MSG("Using static memory -- please pass in a buffer");
return NULL;
#else
in = (char*)XMALLOC(name->sz, NULL, DYNAMIC_TYPE_OPENSSL);
if (!in ) return in;
copySz = name->sz;
#endif
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (copySz == 0)
return in;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
XMEMCPY(in, name->name, copySz - 1);
in[copySz - 1] = 0;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return in;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_get_signature_type(WOLFSSL_X509* x509)
{
int type = 0;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_get_signature_type");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (x509 != NULL)
type = x509->sigOID;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return type;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_get_signature(WOLFSSL_X509* x509,
unsigned char* buf, int* bufSz)
{
WOLFSSL_ENTER("wolfSSL_X509_get_signature");
if (x509 == NULL || bufSz == NULL || *bufSz < (int)x509->sig.length)
return SSL_FATAL_ERROR;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (buf != NULL)
XMEMCPY(buf, x509->sig.buffer, x509->sig.length);
*bufSz = x509->sig.length;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
/* write X509 serial number in unsigned binary to buffer
buffer needs to be at least EXTERNAL_SERIAL_SIZE (32) for all cases
return SSL_SUCCESS on success */
int wolfSSL_X509_get_serial_number(WOLFSSL_X509* x509,
byte* in, int* inOutSz)
{
WOLFSSL_ENTER("wolfSSL_X509_get_serial_number");
if (x509 == NULL || in == NULL ||
inOutSz == NULL || *inOutSz < x509->serialSz)
return BAD_FUNC_ARG;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
XMEMCPY(in, x509->serial, x509->serialSz);
*inOutSz = x509->serialSz;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
const byte* wolfSSL_X509_get_der(WOLFSSL_X509* x509, int* outSz)
{
WOLFSSL_ENTER("wolfSSL_X509_get_der");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (x509 == NULL || outSz == NULL)
return NULL;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
*outSz = (int)x509->derCert->length;
return x509->derCert->buffer;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_version(WOLFSSL_X509* x509)
{
WOLFSSL_ENTER("wolfSSL_X509_version");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (x509 == NULL)
return 0;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return x509->version;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
const byte* wolfSSL_X509_notBefore(WOLFSSL_X509* x509)
{
WOLFSSL_ENTER("wolfSSL_X509_notBefore");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (x509 == NULL)
return NULL;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return x509->notBefore;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
const byte* wolfSSL_X509_notAfter(WOLFSSL_X509* x509)
{
WOLFSSL_ENTER("wolfSSL_X509_notAfter");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (x509 == NULL)
return NULL;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return x509->notAfter;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SEP
/* copy oid into in buffer, at most *inOutSz bytes, if buffer is null will
malloc buffer, call responsible for freeing. Actual size returned in
*inOutSz. Requires inOutSz be non-null */
byte* wolfSSL_X509_get_device_type(WOLFSSL_X509* x509, byte* in, int *inOutSz)
{
2016-06-27 10:53:34 -07:00
int copySz;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_get_dev_type");
if (inOutSz == NULL) return NULL;
if (!x509->deviceTypeSz) return in;
2016-06-27 10:53:34 -07:00
copySz = min(*inOutSz, x509->deviceTypeSz);
2016-06-27 10:53:34 -07:00
if (!in) {
#ifdef WOLFSSL_STATIC_MEMORY
WOLFSSL_MSG("Using static memory -- please pass in a buffer");
return NULL;
#else
in = (byte*)XMALLOC(x509->deviceTypeSz, 0, DYNAMIC_TYPE_OPENSSL);
if (!in) return in;
copySz = x509->deviceTypeSz;
#endif
}
2016-06-27 10:53:34 -07:00
XMEMCPY(in, x509->deviceType, copySz);
*inOutSz = copySz;
return in;
}
2016-06-27 10:53:34 -07:00
byte* wolfSSL_X509_get_hw_type(WOLFSSL_X509* x509, byte* in, int* inOutSz)
{
2016-06-27 10:53:34 -07:00
int copySz;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_get_hw_type");
if (inOutSz == NULL) return NULL;
if (!x509->hwTypeSz) return in;
2016-06-27 10:53:34 -07:00
copySz = min(*inOutSz, x509->hwTypeSz);
2016-06-27 10:53:34 -07:00
if (!in) {
#ifdef WOLFSSL_STATIC_MEMORY
WOLFSSL_MSG("Using static memory -- please pass in a buffer");
return NULL;
#else
in = (byte*)XMALLOC(x509->hwTypeSz, 0, DYNAMIC_TYPE_OPENSSL);
if (!in) return in;
copySz = x509->hwTypeSz;
#endif
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
XMEMCPY(in, x509->hwType, copySz);
*inOutSz = copySz;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return in;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
byte* wolfSSL_X509_get_hw_serial_number(WOLFSSL_X509* x509,byte* in,
int* inOutSz)
{
2016-06-27 10:53:34 -07:00
int copySz;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_get_hw_serial_number");
if (inOutSz == NULL) return NULL;
if (!x509->hwTypeSz) return in;
2016-06-27 10:53:34 -07:00
copySz = min(*inOutSz, x509->hwSerialNumSz);
2016-06-27 10:53:34 -07:00
if (!in) {
#ifdef WOLFSSL_STATIC_MEMORY
WOLFSSL_MSG("Using static memory -- please pass in a buffer");
return NULL;
#else
in = (byte*)XMALLOC(x509->hwSerialNumSz, 0, DYNAMIC_TYPE_OPENSSL);
if (!in) return in;
copySz = x509->hwSerialNumSz;
#endif
}
2016-06-27 10:53:34 -07:00
XMEMCPY(in, x509->hwSerialNum, copySz);
*inOutSz = copySz;
2016-06-27 10:53:34 -07:00
return in;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#endif /* WOLFSSL_SEP */
2016-10-29 13:12:26 -06:00
/* require OPENSSL_EXTRA since wolfSSL_X509_free is wrapped by OPENSSL_EXTRA */
#if !defined(NO_CERTS) && defined(OPENSSL_EXTRA)
/* return 1 on success 0 on fail */
int wolfSSL_sk_X509_push(STACK_OF(WOLFSSL_X509_NAME)* sk, WOLFSSL_X509* x509)
{
WOLFSSL_STACK* node;
if (sk == NULL || x509 == NULL) {
return SSL_FAILURE;
2016-10-29 13:12:26 -06:00
}
/* no previous values in stack */
if (sk->data.x509 == NULL) {
sk->data.x509 = x509;
sk->num += 1;
return SSL_SUCCESS;
2016-10-29 13:12:26 -06:00
}
/* stack already has value(s) create a new node and add more */
node = (WOLFSSL_STACK*)XMALLOC(sizeof(WOLFSSL_STACK), NULL,
DYNAMIC_TYPE_X509);
if (node == NULL) {
WOLFSSL_MSG("Memory error");
return SSL_FAILURE;
2016-10-29 13:12:26 -06:00
}
XMEMSET(node, 0, sizeof(WOLFSSL_STACK));
/* push new x509 onto head of stack */
node->data.x509 = sk->data.x509;
node->next = sk->next;
sk->next = node;
sk->data.x509 = x509;
sk->num += 1;
return SSL_SUCCESS;
2016-10-29 13:12:26 -06:00
}
WOLFSSL_X509* wolfSSL_sk_X509_pop(STACK_OF(WOLFSSL_X509_NAME)* sk) {
WOLFSSL_STACK* node;
WOLFSSL_X509* x509;
if (sk == NULL) {
return NULL;
}
node = sk->next;
x509 = sk->data.x509;
if (node != NULL) { /* update sk and remove node from stack */
sk->data.x509 = node->data.x509;
sk->next = node->next;
XFREE(node, NULL, DYNAMIC_TYPE_X509);
}
else { /* last x509 in stack */
sk->data.x509 = NULL;
}
if (sk->num > 0) {
sk->num -= 1;
}
return x509;
}
/* free structure for x509 stack */
void wolfSSL_sk_X509_free(STACK_OF(WOLFSSL_X509_NAME)* sk) {
WOLFSSL_STACK* node;
if (sk == NULL) {
return;
}
/* parse through stack freeing each node */
node = sk->next;
while (sk->num > 1) {
WOLFSSL_STACK* tmp = node;
node = node->next;
wolfSSL_X509_free(tmp->data.x509);
XFREE(tmp, NULL, DYNAMIC_TYPE_X509);
sk->num -= 1;
}
/* free head of stack */
if (sk->num == 1) {
wolfSSL_X509_free(sk->data.x509);
}
XFREE(sk, NULL, DYNAMIC_TYPE_X509);
}
#endif /* NO_CERTS && OPENSSL_EXTRA */
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_X509* wolfSSL_X509_d2i(WOLFSSL_X509** x509, const byte* in, int len)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_X509 *newX509 = NULL;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_d2i");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (in != NULL && len != 0) {
#ifdef WOLFSSL_SMALL_STACK
DecodedCert* cert = NULL;
#else
DecodedCert cert[1];
#endif
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (cert == NULL)
return NULL;
#endif
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
InitDecodedCert(cert, (byte*)in, len, NULL);
if (ParseCertRelative(cert, CERT_TYPE, 0, NULL) == 0) {
newX509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), NULL,
DYNAMIC_TYPE_X509);
if (newX509 != NULL) {
InitX509(newX509, 1, NULL);
if (CopyDecodedToX509(newX509, cert) != 0) {
XFREE(newX509, NULL, DYNAMIC_TYPE_X509);
newX509 = NULL;
}
}
}
FreeDecodedCert(cert);
#ifdef WOLFSSL_SMALL_STACK
XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
if (x509 != NULL)
*x509 = newX509;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return newX509;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#ifndef NO_FILESYSTEM
#ifndef NO_STDIO_FILESYSTEM
WOLFSSL_X509* wolfSSL_X509_d2i_fp(WOLFSSL_X509** x509, XFILE file)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_X509* newX509 = NULL;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_d2i_fp");
2016-06-27 10:53:34 -07:00
if (file != XBADFILE) {
byte* fileBuffer = NULL;
long sz = 0;
2016-06-27 10:53:34 -07:00
XFSEEK(file, 0, XSEEK_END);
sz = XFTELL(file);
XREWIND(file);
if (sz < 0) {
WOLFSSL_MSG("Bad tell on FILE");
return NULL;
}
fileBuffer = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE);
if (fileBuffer != NULL) {
int ret = (int)XFREAD(fileBuffer, sz, 1, file);
if (ret > 0) {
newX509 = wolfSSL_X509_d2i(NULL, fileBuffer, (int)sz);
}
2016-06-27 10:53:34 -07:00
XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
}
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
if (x509 != NULL)
*x509 = newX509;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return newX509;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#endif /* NO_STDIO_FILESYSTEM */
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_X509* wolfSSL_X509_load_certificate_file(const char* fname, int format)
{
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
byte staticBuffer[1]; /* force heap usage */
#else
byte staticBuffer[FILE_BUFFER_SIZE];
#endif
byte* fileBuffer = staticBuffer;
int dynamic = 0;
int ret;
long sz = 0;
XFILE file;
WOLFSSL_X509* x509 = NULL;
/* Check the inputs */
if ((fname == NULL) ||
(format != SSL_FILETYPE_ASN1 && format != SSL_FILETYPE_PEM))
return NULL;
file = XFOPEN(fname, "rb");
if (file == XBADFILE)
return NULL;
XFSEEK(file, 0, XSEEK_END);
sz = XFTELL(file);
XREWIND(file);
if (sz > (long)sizeof(staticBuffer)) {
fileBuffer = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE);
if (fileBuffer == NULL) {
XFCLOSE(file);
return NULL;
}
dynamic = 1;
}
else if (sz < 0) {
XFCLOSE(file);
return NULL;
}
ret = (int)XFREAD(fileBuffer, sz, 1, file);
if (ret < 0) {
XFCLOSE(file);
if (dynamic)
XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
return NULL;
}
XFCLOSE(file);
x509 = wolfSSL_X509_load_certificate_buffer(fileBuffer, (int)sz, format);
if (dynamic)
XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
return x509;
}
#endif /* NO_FILESYSTEM */
WOLFSSL_X509* wolfSSL_X509_load_certificate_buffer(
const unsigned char* buf, int sz, int format)
{
int ret;
WOLFSSL_X509* x509 = NULL;
DerBuffer* der = NULL;
WOLFSSL_ENTER("wolfSSL_X509_load_certificate_ex");
2016-06-27 10:53:34 -07:00
if (format == SSL_FILETYPE_PEM) {
int ecc = 0;
#ifdef WOLFSSL_SMALL_STACK
EncryptedInfo* info = NULL;
#else
EncryptedInfo info[1];
#endif
#ifdef WOLFSSL_SMALL_STACK
info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (info == NULL) {
return NULL;
}
#endif
info->set = 0;
info->ctx = NULL;
info->consumed = 0;
if (PemToDer(buf, sz, CERT_TYPE, &der, NULL, info, &ecc) != 0) {
2016-06-27 10:53:34 -07:00
FreeDer(&der);
}
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
}
else {
ret = AllocDer(&der, (word32)sz, CERT_TYPE, NULL);
if (ret == 0) {
XMEMCPY(der->buffer, buf, sz);
2016-06-27 10:53:34 -07:00
}
}
/* At this point we want `der` to have the certificate in DER format */
/* ready to be decoded. */
if (der != NULL && der->buffer != NULL) {
#ifdef WOLFSSL_SMALL_STACK
DecodedCert* cert = NULL;
#else
DecodedCert cert[1];
#endif
#ifdef WOLFSSL_SMALL_STACK
cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (cert != NULL)
#endif
{
InitDecodedCert(cert, der->buffer, der->length, NULL);
if (ParseCertRelative(cert, CERT_TYPE, 0, NULL) == 0) {
x509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), NULL,
DYNAMIC_TYPE_X509);
if (x509 != NULL) {
InitX509(x509, 1, NULL);
if (CopyDecodedToX509(x509, cert) != 0) {
XFREE(x509, NULL, DYNAMIC_TYPE_X509);
x509 = NULL;
}
}
}
FreeDecodedCert(cert);
#ifdef WOLFSSL_SMALL_STACK
XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
}
FreeDer(&der);
}
return x509;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#endif /* KEEP_PEER_CERT || SESSION_CERTS */
/* OPENSSL_EXTRA is needed for wolfSSL_X509_d21 function
KEEP_OUR_CERT is to insure ability for returning ssl certificate */
#if defined(OPENSSL_EXTRA) && defined(KEEP_OUR_CERT)
WOLFSSL_X509* wolfSSL_get_certificate(WOLFSSL* ssl)
{
2016-06-27 10:53:34 -07:00
if (ssl == NULL) {
return NULL;
}
if (ssl->buffers.weOwnCert) {
if (ssl->ourCert == NULL) {
if (ssl->buffers.certificate == NULL) {
WOLFSSL_MSG("Certificate buffer not set!");
return NULL;
}
2016-06-27 10:53:34 -07:00
ssl->ourCert = wolfSSL_X509_d2i(NULL,
ssl->buffers.certificate->buffer,
ssl->buffers.certificate->length);
}
return ssl->ourCert;
}
else { /* if cert not owned get parent ctx cert or return null */
if (ssl->ctx) {
if (ssl->ctx->ourCert == NULL) {
if (ssl->ctx->certificate == NULL) {
WOLFSSL_MSG("Ctx Certificate buffer not set!");
return NULL;
}
2016-06-27 10:53:34 -07:00
ssl->ctx->ourCert = wolfSSL_X509_d2i(NULL,
ssl->ctx->certificate->buffer,
ssl->ctx->certificate->length);
}
return ssl->ctx->ourCert;
}
}
return NULL;
}
2016-06-27 10:53:34 -07:00
#endif /* OPENSSL_EXTRA && KEEP_OUR_CERT */
#endif /* NO_CERTS */
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#ifdef OPENSSL_EXTRA
int wolfSSL_set_session_id_context(WOLFSSL* ssl, const unsigned char* id,
unsigned int len)
{
2016-06-27 10:53:34 -07:00
(void)ssl;
(void)id;
(void)len;
return 0;
}
void wolfSSL_set_connect_state(WOLFSSL* ssl)
{
(void)ssl;
/* client by default */
}
#endif
int wolfSSL_get_shutdown(const WOLFSSL* ssl)
{
WOLFSSL_ENTER("wolfSSL_get_shutdown");
/* in OpenSSL, SSL_SENT_SHUTDOWN = 1, when closeNotifySent *
* SSL_RECEIVED_SHUTDOWN = 2, from close notify or fatal err */
return ((ssl->options.closeNotify||ssl->options.connReset) << 1)
| (ssl->options.sentNotify);
}
int wolfSSL_session_reused(WOLFSSL* ssl)
{
return ssl->options.resuming;
}
#ifdef OPENSSL_EXTRA
void wolfSSL_SESSION_free(WOLFSSL_SESSION* session)
{
/* No need to free since cache is static */
(void)session;
}
#endif
const char* wolfSSL_get_version(WOLFSSL* ssl)
{
WOLFSSL_ENTER("SSL_get_version");
if (ssl->version.major == SSLv3_MAJOR) {
switch (ssl->version.minor) {
case SSLv3_MINOR :
return "SSLv3";
case TLSv1_MINOR :
return "TLSv1";
case TLSv1_1_MINOR :
return "TLSv1.1";
case TLSv1_2_MINOR :
return "TLSv1.2";
default:
return "unknown";
}
}
else if (ssl->version.major == DTLS_MAJOR) {
switch (ssl->version.minor) {
case DTLS_MINOR :
return "DTLS";
case DTLSv1_2_MINOR :
return "DTLSv1.2";
default:
return "unknown";
}
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
return "unknown";
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
/* current library version */
const char* wolfSSL_lib_version(void)
{
2016-06-27 10:53:34 -07:00
return LIBWOLFSSL_VERSION_STRING;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
/* current library version in hex */
word32 wolfSSL_lib_version_hex(void)
{
2016-06-27 10:53:34 -07:00
return LIBWOLFSSL_VERSION_HEX;
}
int wolfSSL_get_current_cipher_suite(WOLFSSL* ssl)
{
WOLFSSL_ENTER("SSL_get_current_cipher_suite");
if (ssl)
return (ssl->options.cipherSuite0 << 8) | ssl->options.cipherSuite;
return 0;
}
WOLFSSL_CIPHER* wolfSSL_get_current_cipher(WOLFSSL* ssl)
{
WOLFSSL_ENTER("SSL_get_current_cipher");
if (ssl)
return &ssl->cipher;
else
return NULL;
}
const char* wolfSSL_CIPHER_get_name(const WOLFSSL_CIPHER* cipher)
{
WOLFSSL_ENTER("SSL_CIPHER_get_name");
if (cipher == NULL || cipher->ssl == NULL) {
return NULL;
}
return wolfSSL_get_cipher_name_from_suite(cipher->ssl->options.cipherSuite,
cipher->ssl->options.cipherSuite0);
}
const char* wolfSSL_SESSION_CIPHER_get_name(WOLFSSL_SESSION* session)
{
if (session == NULL) {
return NULL;
}
#ifdef SESSION_CERTS
return wolfSSL_get_cipher_name_from_suite(session->cipherSuite,
session->cipherSuite0);
#else
return NULL;
2016-06-27 10:53:34 -07:00
#endif
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
const char* wolfSSL_get_cipher(WOLFSSL* ssl)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_get_cipher");
return wolfSSL_CIPHER_get_name(wolfSSL_get_current_cipher(ssl));
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
/* gets cipher name in the format DHE-RSA-... rather then TLS_DHE... */
const char* wolfSSL_get_cipher_name(WOLFSSL* ssl)
{
2016-06-27 10:53:34 -07:00
/* get access to cipher_name_idx in internal.c */
return wolfSSL_get_cipher_name_internal(ssl);
}
2011-02-05 11:14:47 -08:00
#ifdef OPENSSL_EXTRA
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
char* wolfSSL_CIPHER_description(WOLFSSL_CIPHER* cipher, char* in, int len)
{
2016-06-27 10:53:34 -07:00
(void)cipher;
(void)in;
(void)len;
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#ifndef NO_SESSION_CACHE
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_SESSION* wolfSSL_get1_session(WOLFSSL* ssl)
{
2016-06-27 10:53:34 -07:00
/* sessions are stored statically, no need for reference count */
return wolfSSL_get_session(ssl);
}
2016-06-27 10:53:34 -07:00
#endif /* NO_SESSION_CACHE */
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#ifndef NO_CERTS
void wolfSSL_X509_free(WOLFSSL_X509* x509)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_free");
ExternalFreeX509(x509);
}
2016-06-27 10:53:34 -07:00
#endif /* NO_CERTS */
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
/* was do nothing */
/*
void OPENSSL_free(void* buf)
{
2016-06-27 10:53:34 -07:00
(void)buf;
}
2016-06-27 10:53:34 -07:00
*/
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_OCSP_parse_url(char* url, char** host, char** port, char** path,
int* ssl)
{
2016-06-27 10:53:34 -07:00
(void)url;
(void)host;
(void)port;
(void)path;
(void)ssl;
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_METHOD* wolfSSLv2_client_method(void)
{
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_METHOD* wolfSSLv2_server_method(void)
{
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#ifndef NO_MD4
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
void wolfSSL_MD4_Init(WOLFSSL_MD4_CTX* md4)
{
2016-06-27 10:53:34 -07:00
/* make sure we have a big enough buffer */
typedef char ok[sizeof(md4->buffer) >= sizeof(Md4) ? 1 : -1];
(void) sizeof(ok);
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("MD4_Init");
wc_InitMd4((Md4*)md4);
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
void wolfSSL_MD4_Update(WOLFSSL_MD4_CTX* md4, const void* data,
unsigned long len)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("MD4_Update");
wc_Md4Update((Md4*)md4, (const byte*)data, (word32)len);
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
void wolfSSL_MD4_Final(unsigned char* digest, WOLFSSL_MD4_CTX* md4)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("MD4_Final");
wc_Md4Final((Md4*)md4, digest);
}
2016-06-27 10:53:34 -07:00
#endif /* NO_MD4 */
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_BIO* wolfSSL_BIO_pop(WOLFSSL_BIO* top)
{
2016-06-27 10:53:34 -07:00
(void)top;
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_BIO_pending(WOLFSSL_BIO* bio)
{
2016-06-27 10:53:34 -07:00
(void)bio;
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_mem(void)
{
2016-06-27 10:53:34 -07:00
static WOLFSSL_BIO_METHOD meth;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("BIO_s_mem");
meth.type = BIO_MEMORY;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return &meth;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_BIO_METHOD* wolfSSL_BIO_f_base64(void)
{
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
void wolfSSL_BIO_set_flags(WOLFSSL_BIO* bio, int flags)
{
2016-06-27 10:53:34 -07:00
(void)bio;
(void)flags;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
void wolfSSL_RAND_screen(void)
{
2011-02-05 11:14:47 -08:00
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
const char* wolfSSL_RAND_file_name(char* fname, unsigned long len)
{
2016-06-27 10:53:34 -07:00
(void)fname;
(void)len;
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_RAND_write_file(const char* fname)
{
2016-06-27 10:53:34 -07:00
(void)fname;
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_RAND_load_file(const char* fname, long len)
{
2016-06-27 10:53:34 -07:00
(void)fname;
/* wolfCrypt provides enough entropy internally or will report error */
if (len == -1)
return 1024;
else
return (int)len;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_RAND_egd(const char* path)
{
2016-06-27 10:53:34 -07:00
(void)path;
return 0;
}
2011-02-05 11:14:47 -08:00
2015-03-20 10:59:54 -06:00
2016-06-27 10:53:34 -07:00
WOLFSSL_COMP_METHOD* wolfSSL_COMP_zlib(void)
{
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_COMP_METHOD* wolfSSL_COMP_rle(void)
{
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_COMP_add_compression_method(int method, void* data)
{
2016-06-27 10:53:34 -07:00
(void)method;
(void)data;
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
void wolfSSL_set_dynlock_create_callback(WOLFSSL_dynlock_value* (*f)(
const char*, int))
{
(void)f;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
void wolfSSL_set_dynlock_lock_callback(
void (*f)(int, WOLFSSL_dynlock_value*, const char*, int))
{
(void)f;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
void wolfSSL_set_dynlock_destroy_callback(
void (*f)(WOLFSSL_dynlock_value*, const char*, int))
{
(void)f;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
const char* wolfSSL_X509_verify_cert_error_string(long err)
{
2016-06-27 10:53:34 -07:00
return wolfSSL_ERR_reason_error_string(err);
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_LOOKUP_add_dir(WOLFSSL_X509_LOOKUP* lookup, const char* dir,
long len)
{
2016-06-27 10:53:34 -07:00
(void)lookup;
(void)dir;
(void)len;
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_LOOKUP_load_file(WOLFSSL_X509_LOOKUP* lookup,
const char* file, long len)
{
2016-06-27 10:53:34 -07:00
(void)lookup;
(void)file;
(void)len;
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_X509_LOOKUP_METHOD* wolfSSL_X509_LOOKUP_hash_dir(void)
{
2016-06-27 10:53:34 -07:00
return 0;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_X509_LOOKUP_METHOD* wolfSSL_X509_LOOKUP_file(void)
{
return 0;
}
2011-10-04 09:29:10 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_X509_LOOKUP* wolfSSL_X509_STORE_add_lookup(WOLFSSL_X509_STORE* store,
WOLFSSL_X509_LOOKUP_METHOD* m)
{
(void)store;
(void)m;
return 0;
}
2011-10-04 09:29:10 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_CERTS
2016-10-29 13:12:26 -06:00
#if !defined(NO_ASN) && !defined(NO_PWDBASED)
WC_PKCS12* wolfSSL_d2i_PKCS12_bio(WOLFSSL_BIO* bio, WC_PKCS12** pkcs12)
{
WC_PKCS12* localPkcs12 = NULL;
const unsigned char* mem = NULL;
int ret;
word32 size;
WOLFSSL_ENTER("wolfSSL_d2i_PKCS12_bio");
if (bio == NULL) {
WOLFSSL_MSG("Bad Function Argument bio is NULL");
return NULL;
}
localPkcs12 = wc_PKCS12_new();
if (localPkcs12 == NULL) {
WOLFSSL_MSG("Memory error");
return NULL;
}
if (pkcs12 != NULL) {
*pkcs12 = localPkcs12;
}
ret = wolfSSL_BIO_get_mem_data(bio, &mem);
if (mem == NULL || ret <= 0) {
WOLFSSL_MSG("Failed to get data from bio struct");
wc_PKCS12_free(localPkcs12);
if (pkcs12 != NULL) {
*pkcs12 = NULL;
}
return NULL;
}
size = ret;
ret = wc_d2i_PKCS12(mem, size, localPkcs12);
if (ret <= 0) {
WOLFSSL_MSG("Failed to get PKCS12 sequence");
wc_PKCS12_free(localPkcs12);
if (pkcs12 != NULL) {
*pkcs12 = NULL;
}
return NULL;
}
return localPkcs12;
}
/* return 1 on success, 0 on failure */
int wolfSSL_PKCS12_parse(WC_PKCS12* pkcs12, const char* psw,
WOLFSSL_EVP_PKEY** pkey, WOLFSSL_X509** cert, STACK_OF(WOLFSSL_X509)** ca)
{
DecodedCert DeCert;
void* heap = NULL;
2016-10-29 13:12:26 -06:00
int ret;
byte* certData = NULL;
word32 certDataSz;
byte* pk = NULL;
word32 pkSz;
WC_DerCertList* certList = NULL;
2016-10-29 13:12:26 -06:00
WOLFSSL_ENTER("wolfSSL_PKCS12_parse");
if (pkcs12 == NULL || psw == NULL || pkey == NULL || cert == NULL) {
WOLFSSL_MSG("Bad argument value");
return 0;
}
heap = wc_PKCS12_GetHeap(pkcs12);
2016-10-29 13:12:26 -06:00
*pkey = NULL;
*cert = NULL;
if (ca == NULL) {
ret = wc_PKCS12_parse(pkcs12, psw, &pk, &pkSz, &certData, &certDataSz,
NULL);
}
else {
*ca = NULL;
ret = wc_PKCS12_parse(pkcs12, psw, &pk, &pkSz, &certData, &certDataSz,
&certList);
}
if (ret < 0) {
WOLFSSL_LEAVE("wolfSSL_PKCS12_parse", ret);
return 0;
}
/* Decode cert and place in X509 stack struct */
if (certList != NULL) {
WC_DerCertList* current = certList;
2016-10-29 13:12:26 -06:00
*ca = (STACK_OF(WOLFSSL_X509)*)XMALLOC(sizeof(STACK_OF(WOLFSSL_X509)),
heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
if (*ca == NULL) {
if (pk != NULL) {
XFREE(pk, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
}
if (certData != NULL) {
XFREE(*cert, heap, DYNAMIC_TYPE_PKCS); *cert = NULL;
2016-10-29 13:12:26 -06:00
}
/* Free up WC_DerCertList and move on */
2016-10-29 13:12:26 -06:00
while (current != NULL) {
WC_DerCertList* next = current->next;
2016-10-29 13:12:26 -06:00
XFREE(current->buffer, heap, DYNAMIC_TYPE_PKCS);
XFREE(current, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
current = next;
}
return 0;
}
XMEMSET(*ca, 0, sizeof(STACK_OF(WOLFSSL_X509)));
/* add list of DER certs as X509's to stack */
while (current != NULL) {
WC_DerCertList* toFree = current;
2016-10-29 13:12:26 -06:00
WOLFSSL_X509* x509;
x509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), heap,
2016-10-29 13:12:26 -06:00
DYNAMIC_TYPE_PKCS);
InitX509(x509, 1, heap);
InitDecodedCert(&DeCert, current->buffer, current->bufferSz, heap);
2016-10-29 13:12:26 -06:00
if (ParseCertRelative(&DeCert, CERT_TYPE, NO_VERIFY, NULL) != 0) {
WOLFSSL_MSG("Issue with parsing certificate");
FreeDecodedCert(&DeCert);
wolfSSL_X509_free(x509);
}
else {
if ((ret = CopyDecodedToX509(x509, &DeCert)) != 0) {
WOLFSSL_MSG("Failed to copy decoded cert");
FreeDecodedCert(&DeCert);
wolfSSL_X509_free(x509);
wolfSSL_sk_X509_free(*ca); *ca = NULL;
if (pk != NULL) {
XFREE(pk, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
}
if (certData != NULL) {
XFREE(certData, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
}
/* Free up WC_DerCertList */
2016-10-29 13:12:26 -06:00
while (current != NULL) {
WC_DerCertList* next = current->next;
2016-10-29 13:12:26 -06:00
XFREE(current->buffer, heap, DYNAMIC_TYPE_PKCS);
XFREE(current, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
current = next;
}
return 0;
}
FreeDecodedCert(&DeCert);
if (wolfSSL_sk_X509_push(*ca, x509) != 1) {
WOLFSSL_MSG("Failed to push x509 onto stack");
wolfSSL_X509_free(x509);
wolfSSL_sk_X509_free(*ca); *ca = NULL;
if (pk != NULL) {
XFREE(pk, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
}
if (certData != NULL) {
XFREE(certData, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
}
/* Free up WC_DerCertList */
2016-10-29 13:12:26 -06:00
while (current != NULL) {
WC_DerCertList* next = current->next;
2016-10-29 13:12:26 -06:00
XFREE(current->buffer, heap, DYNAMIC_TYPE_PKCS);
XFREE(current, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
current = next;
}
return 0;
}
}
current = current->next;
XFREE(toFree->buffer, heap, DYNAMIC_TYPE_PKCS);
XFREE(toFree, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
}
}
/* Decode cert and place in X509 struct */
if (certData != NULL) {
*cert = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), heap,
2016-10-29 13:12:26 -06:00
DYNAMIC_TYPE_PKCS);
if (*cert == NULL) {
if (pk != NULL) {
XFREE(pk, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
}
if (ca != NULL) {
wolfSSL_sk_X509_free(*ca); *ca = NULL;
}
XFREE(certData, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
return 0;
}
InitX509(*cert, 1, heap);
InitDecodedCert(&DeCert, certData, certDataSz, heap);
2016-10-29 13:12:26 -06:00
if (ParseCertRelative(&DeCert, CERT_TYPE, NO_VERIFY, NULL) != 0) {
WOLFSSL_MSG("Issue with parsing certificate");
}
if ((ret = CopyDecodedToX509(*cert, &DeCert)) != 0) {
WOLFSSL_MSG("Failed to copy decoded cert");
FreeDecodedCert(&DeCert);
if (pk != NULL) {
XFREE(pk, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
}
if (ca != NULL) {
wolfSSL_sk_X509_free(*ca); *ca = NULL;
}
wolfSSL_X509_free(*cert); *cert = NULL;
return 0;
}
FreeDecodedCert(&DeCert);
XFREE(certData, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
}
/* get key type */
ret = BAD_STATE_E;
if (pk != NULL) { /* decode key if present */
*pkey = (WOLFSSL_EVP_PKEY*)XMALLOC(sizeof(WOLFSSL_EVP_PKEY),
heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
if (*pkey == NULL) {
wolfSSL_X509_free(*cert); *cert = NULL;
if (ca != NULL) {
wolfSSL_sk_X509_free(*ca); *ca = NULL;
}
XFREE(pk, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
return 0;
}
#ifndef NO_RSA
{
word32 keyIdx = 0;
RsaKey key;
if (wc_InitRsaKey(&key, heap) != 0) {
2016-10-29 13:12:26 -06:00
ret = BAD_STATE_E;
}
else {
if ((ret = wc_RsaPrivateKeyDecode(pk, &keyIdx, &key, pkSz))
== 0) {
(*pkey)->type = RSAk;
WOLFSSL_MSG("Found PKCS12 RSA key");
}
wc_FreeRsaKey(&key);
}
}
#endif /* NO_RSA */
#ifdef HAVE_ECC
{
word32 keyIdx = 0;
ecc_key key;
if (ret != 0) { /* if is in fail state check if ECC key */
if (wc_ecc_init(&key) != 0) {
wolfSSL_X509_free(*cert); *cert = NULL;
if (ca != NULL) {
wolfSSL_sk_X509_free(*ca); *ca = NULL;
}
XFREE(*pkey, heap, DYNAMIC_TYPE_PKCS); *pkey = NULL;
XFREE(pk, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
return 0;
}
if ((ret = wc_EccPrivateKeyDecode(pk, &keyIdx, &key, pkSz))
!= 0) {
wolfSSL_X509_free(*cert); *cert = NULL;
if (ca != NULL) {
wolfSSL_sk_X509_free(*ca); *ca = NULL;
}
XFREE(*pkey, heap, DYNAMIC_TYPE_PKCS); *pkey = NULL;
XFREE(pk, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
WOLFSSL_MSG("Bad PKCS12 key format");
return 0;
}
(*pkey)->type = ECDSAk;
(*pkey)->pkey_curve = key.dp->oidSum;
wc_ecc_free(&key);
WOLFSSL_MSG("Found PKCS12 ECC key");
}
}
#else
if (ret != 0) { /* if is in fail state and no ECC then fail */
wolfSSL_X509_free(*cert); *cert = NULL;
if (ca != NULL) {
wolfSSL_sk_X509_free(*ca); *ca = NULL;
}
XFREE(*pkey, heap, DYNAMIC_TYPE_PKCS); *pkey = NULL;
XFREE(pk, heap, DYNAMIC_TYPE_PKCS);
2016-10-29 13:12:26 -06:00
WOLFSSL_MSG("Bad PKCS12 key format");
return 0;
}
#endif /* HAVE_ECC */
(*pkey)->save_type = 0;
(*pkey)->pkey_sz = pkSz;
(*pkey)->pkey.ptr = (char*)pk;
}
(void)ret;
(void)ca;
return 1;
}
#endif /* !defined(NO_ASN) && !defined(NO_PWDBASED) */
/* no-op function. Was initially used for adding encryption algorithms available
* for PKCS12 */
void wolfSSL_PKCS12_PBE_add(void)
{
WOLFSSL_ENTER("wolfSSL_PKCS12_PBE_add");
}
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_STORE_add_cert(WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509)
2014-09-08 21:34:07 -03:00
{
2016-06-27 10:53:34 -07:00
int result = SSL_FATAL_ERROR;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_STORE_add_cert");
if (store != NULL && store->cm != NULL && x509 != NULL
&& x509->derCert != NULL) {
DerBuffer* derCert = NULL;
2014-09-08 21:34:07 -03:00
2016-06-27 10:53:34 -07:00
result = AllocDer(&derCert, x509->derCert->length,
x509->derCert->type, NULL);
if (result == 0) {
/* AddCA() frees the buffer. */
XMEMCPY(derCert->buffer,
x509->derCert->buffer, x509->derCert->length);
result = AddCA(store->cm, &derCert, WOLFSSL_USER_CA, 1);
}
}
2016-06-27 10:53:34 -07:00
WOLFSSL_LEAVE("wolfSSL_X509_STORE_add_cert", result);
2016-06-27 10:53:34 -07:00
if (result != SSL_SUCCESS) {
result = SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
return result;
}
2014-09-08 21:34:07 -03:00
2016-06-27 10:53:34 -07:00
WOLFSSL_X509_STORE* wolfSSL_X509_STORE_new(void)
{
WOLFSSL_X509_STORE* store = NULL;
store = (WOLFSSL_X509_STORE*)XMALLOC(sizeof(WOLFSSL_X509_STORE), NULL,
DYNAMIC_TYPE_X509_STORE);
if (store != NULL) {
store->cm = wolfSSL_CertManagerNew();
if (store->cm == NULL) {
XFREE(store, NULL, DYNAMIC_TYPE_X509_STORE);
store = NULL;
}
2016-06-27 10:53:34 -07:00
}
2016-06-27 10:53:34 -07:00
return store;
}
2014-09-08 21:34:07 -03:00
2016-06-27 10:53:34 -07:00
void wolfSSL_X509_STORE_free(WOLFSSL_X509_STORE* store)
{
if (store != NULL) {
if (store->cm != NULL)
wolfSSL_CertManagerFree(store->cm);
XFREE(store, NULL, DYNAMIC_TYPE_X509_STORE);
}
2016-06-27 10:53:34 -07:00
}
2014-09-08 21:34:07 -03:00
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_STORE_set_default_paths(WOLFSSL_X509_STORE* store)
{
(void)store;
return SSL_SUCCESS;
2014-09-08 21:34:07 -03:00
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_STORE_get_by_subject(WOLFSSL_X509_STORE_CTX* ctx, int idx,
WOLFSSL_X509_NAME* name, WOLFSSL_X509_OBJECT* obj)
{
(void)ctx;
(void)idx;
(void)name;
(void)obj;
return 0;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_X509_STORE_CTX* wolfSSL_X509_STORE_CTX_new(void)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_X509_STORE_CTX* ctx = (WOLFSSL_X509_STORE_CTX*)XMALLOC(
sizeof(WOLFSSL_X509_STORE_CTX), NULL,
DYNAMIC_TYPE_X509_CTX);
if (ctx != NULL)
wolfSSL_X509_STORE_CTX_init(ctx, NULL, NULL, NULL);
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
return ctx;
}
2012-05-07 16:35:23 -07:00
2012-05-17 17:44:54 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_STORE_CTX_init(WOLFSSL_X509_STORE_CTX* ctx,
WOLFSSL_X509_STORE* store, WOLFSSL_X509* x509, STACK_OF(WOLFSSL_X509)* sk)
{
(void)sk;
WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_init");
if (ctx != NULL) {
ctx->store = store;
ctx->current_cert = x509;
ctx->domain = NULL;
ctx->ex_data = NULL;
ctx->userCtx = NULL;
ctx->error = 0;
ctx->error_depth = 0;
ctx->discardSessionCerts = 0;
return SSL_SUCCESS;
2012-05-07 16:35:23 -07:00
}
2016-06-27 10:53:34 -07:00
return SSL_FATAL_ERROR;
}
2012-05-07 16:35:23 -07:00
2012-05-07 19:36:19 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_X509_STORE_CTX_free(WOLFSSL_X509_STORE_CTX* ctx)
{
2016-06-27 10:53:34 -07:00
if (ctx != NULL) {
if (ctx->store != NULL)
wolfSSL_X509_STORE_free(ctx->store);
if (ctx->current_cert != NULL)
wolfSSL_FreeX509(ctx->current_cert);
XFREE(ctx, NULL, DYNAMIC_TYPE_X509_CTX);
}
}
2012-05-07 19:36:19 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_X509_STORE_CTX_cleanup(WOLFSSL_X509_STORE_CTX* ctx)
{
(void)ctx;
}
2012-05-07 19:36:19 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_verify_cert(WOLFSSL_X509_STORE_CTX* ctx)
{
if (ctx != NULL && ctx->store != NULL && ctx->store->cm != NULL
&& ctx->current_cert != NULL && ctx->current_cert->derCert != NULL) {
return wolfSSL_CertManagerVerifyBuffer(ctx->store->cm,
ctx->current_cert->derCert->buffer,
ctx->current_cert->derCert->length,
SSL_FILETYPE_ASN1);
2014-09-15 20:32:00 -03:00
}
2016-06-27 10:53:34 -07:00
return SSL_FATAL_ERROR;
}
#endif /* NO_CERTS */
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ASN1_TIME* wolfSSL_X509_CRL_get_lastUpdate(WOLFSSL_X509_CRL* crl)
{
(void)crl;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ASN1_TIME* wolfSSL_X509_CRL_get_nextUpdate(WOLFSSL_X509_CRL* crl)
{
2016-06-27 10:53:34 -07:00
(void)crl;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_EVP_PKEY* wolfSSL_X509_get_pubkey(WOLFSSL_X509* x509)
{
WOLFSSL_EVP_PKEY* key = NULL;
if (x509 != NULL) {
key = (WOLFSSL_EVP_PKEY*)XMALLOC(
sizeof(WOLFSSL_EVP_PKEY), x509->heap,
DYNAMIC_TYPE_PUBLIC_KEY);
if (key != NULL) {
key->type = x509->pubKeyOID;
key->save_type = 0;
key->pkey.ptr = (char*)XMALLOC(
x509->pubKey.length, x509->heap,
DYNAMIC_TYPE_PUBLIC_KEY);
if (key->pkey.ptr == NULL) {
XFREE(key, x509->heap, DYNAMIC_TYPE_PUBLIC_KEY);
return NULL;
}
XMEMCPY(key->pkey.ptr,
x509->pubKey.buffer, x509->pubKey.length);
key->pkey_sz = x509->pubKey.length;
#ifdef HAVE_ECC
key->pkey_curve = (int)x509->pkCurveOID;
#endif /* HAVE_ECC */
}
}
return key;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_CRL_verify(WOLFSSL_X509_CRL* crl, WOLFSSL_EVP_PKEY* key)
{
2016-06-27 10:53:34 -07:00
(void)crl;
(void)key;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_X509_STORE_CTX_set_error(WOLFSSL_X509_STORE_CTX* ctx, int err)
{
(void)ctx;
2016-06-27 10:53:34 -07:00
(void)err;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_X509_OBJECT_free_contents(WOLFSSL_X509_OBJECT* obj)
{
(void)obj;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_EVP_PKEY_free(WOLFSSL_EVP_PKEY* key)
{
2016-06-27 10:53:34 -07:00
if (key != NULL) {
if (key->pkey.ptr != NULL)
XFREE(key->pkey.ptr, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
XFREE(key, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
}
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_cmp_current_time(const WOLFSSL_ASN1_TIME* asnTime)
{
2016-06-27 10:53:34 -07:00
(void)asnTime;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_sk_X509_REVOKED_num(WOLFSSL_X509_REVOKED* revoked)
{
(void)revoked;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_X509_REVOKED* wolfSSL_X509_CRL_get_REVOKED(WOLFSSL_X509_CRL* crl)
{
(void)crl;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_X509_REVOKED* wolfSSL_sk_X509_REVOKED_value(
WOLFSSL_X509_REVOKED* revoked, int value)
{
2016-06-27 10:53:34 -07:00
(void)revoked;
(void)value;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ASN1_INTEGER* wolfSSL_X509_get_serialNumber(WOLFSSL_X509* x509)
{
(void)x509;
return 0;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_ASN1_TIME_print(WOLFSSL_BIO* bio, const WOLFSSL_ASN1_TIME* asnTime)
{
2016-06-27 10:53:34 -07:00
(void)bio;
(void)asnTime;
return 0;
}
2012-05-01 16:12:12 -07:00
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#if defined(WOLFSSL_MYSQL_COMPATIBLE)
char* wolfSSL_ASN1_TIME_to_string(WOLFSSL_ASN1_TIME* time, char* buf, int len)
{
2016-06-27 10:53:34 -07:00
int format;
int dateLen;
byte* date = (byte*)time;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_ASN1_TIME_to_string");
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (time == NULL || buf == NULL || len < 5) {
WOLFSSL_MSG("Bad argument");
return NULL;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
format = *date; date++;
dateLen = *date; date++;
if (dateLen > len) {
2016-06-27 15:44:52 -06:00
WOLFSSL_MSG("Length of date is longer then buffer");
return NULL;
2016-06-27 10:53:34 -07:00
}
2012-05-07 16:35:23 -07:00
2016-06-27 15:44:52 -06:00
if (!GetTimeString(date, format, buf, len)) {
return NULL;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
return buf;
}
2016-06-27 10:53:34 -07:00
#endif /* WOLFSSL_MYSQL_COMPATIBLE */
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_ASN1_INTEGER_cmp(const WOLFSSL_ASN1_INTEGER* a,
const WOLFSSL_ASN1_INTEGER* b)
{
2016-06-27 10:53:34 -07:00
(void)a;
(void)b;
return 0;
}
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_ASN1_INTEGER_get(const WOLFSSL_ASN1_INTEGER* i)
{
2016-06-27 10:53:34 -07:00
(void)i;
return 0;
}
2012-05-02 15:11:20 -07:00
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
void* wolfSSL_X509_STORE_CTX_get_ex_data(WOLFSSL_X509_STORE_CTX* ctx, int idx)
{
WOLFSSL_ENTER("wolfSSL_X509_STORE_CTX_get_ex_data");
#if defined(FORTRESS) || defined(HAVE_STUNNEL)
if (ctx != NULL && idx == 0)
return ctx->ex_data;
#else
(void)ctx;
(void)idx;
#endif
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_get_ex_data_X509_STORE_CTX_idx(void)
{
WOLFSSL_ENTER("wolfSSL_get_ex_data_X509_STORE_CTX_idx");
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_CTX_set_info_callback(WOLFSSL_CTX* ctx,
void (*f)(const WOLFSSL* ssl, int type, int val))
{
(void)ctx;
(void)f;
}
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
unsigned long wolfSSL_ERR_peek_error(void)
{
2016-06-27 10:53:34 -07:00
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_ERR_GET_REASON(unsigned long err)
{
(void)err;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
char* wolfSSL_alert_type_string_long(int alertID)
{
(void)alertID;
return 0;
}
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
char* wolfSSL_alert_desc_string_long(int alertID)
{
2016-06-27 10:53:34 -07:00
(void)alertID;
return 0;
}
2012-05-07 16:35:23 -07:00
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
char* wolfSSL_state_string_long(const WOLFSSL* ssl)
{
(void)ssl;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_PEM_def_callback(char* name, int num, int w, void* key)
{
(void)name;
(void)num;
(void)w;
(void)key;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_sess_accept(WOLFSSL_CTX* ctx)
{
2016-06-27 10:53:34 -07:00
(void)ctx;
return 0;
}
2015-07-21 11:10:28 -07:00
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_sess_connect(WOLFSSL_CTX* ctx)
{
(void)ctx;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_sess_accept_good(WOLFSSL_CTX* ctx)
{
(void)ctx;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_sess_connect_good(WOLFSSL_CTX* ctx)
{
(void)ctx;
return 0;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_sess_accept_renegotiate(WOLFSSL_CTX* ctx)
{
2016-06-27 10:53:34 -07:00
(void)ctx;
return 0;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_sess_connect_renegotiate(WOLFSSL_CTX* ctx)
{
(void)ctx;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_sess_hits(WOLFSSL_CTX* ctx)
{
(void)ctx;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_sess_cb_hits(WOLFSSL_CTX* ctx)
{
(void)ctx;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_sess_cache_full(WOLFSSL_CTX* ctx)
{
(void)ctx;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_sess_misses(WOLFSSL_CTX* ctx)
{
2016-06-27 10:53:34 -07:00
(void)ctx;
return 0;
}
2012-05-01 16:12:12 -07:00
2015-12-21 16:11:02 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_sess_timeouts(WOLFSSL_CTX* ctx)
{
(void)ctx;
return 0;
2015-07-01 12:24:11 -07:00
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_sess_number(WOLFSSL_CTX* ctx)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
(void)ctx;
return 0;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_DES3
void wolfSSL_DES_set_key(WOLFSSL_const_DES_cblock* myDes,
WOLFSSL_DES_key_schedule* key)
{
#ifdef WOLFSSL_CHECK_DESKEY
wolfSSL_DES_set_key_checked(myDes, key);
#else
wolfSSL_DES_set_key_unchecked(myDes, key);
#endif
}
/* return true in fail case (1) */
static int DES_check(word32 mask, word32 mask2, unsigned char* key)
{
word32 value[2];
/* sanity check on length made in wolfSSL_DES_set_key_checked */
value[0] = mask;
value[1] = mask2;
return (XMEMCMP(value, key, sizeof(value)) == 0)? 1: 0;
}
/* check that the key is odd parity and is not a weak key */
void wolfSSL_DES_set_key_checked(WOLFSSL_const_DES_cblock* myDes,
WOLFSSL_DES_key_schedule* key)
{
if (myDes == NULL || key == NULL) {
WOLFSSL_MSG("Bad argument passed to wolfSSL_DES_set_key_checked");
}
else {
word32 i, mask, mask2;
word32 sz = sizeof(WOLFSSL_DES_key_schedule);
/* sanity check before call to DES_check */
if (sz != (sizeof(word32) * 2)) {
WOLFSSL_MSG("Unexpected WOLFSSL_DES_key_schedule size");
return;
}
/* check odd parity */
for (i = 0; i < sz; i++) {
unsigned char c = *((unsigned char*)key + i);
if (((c & 0x01) ^
((c >> 1) & 0x01) ^
((c >> 2) & 0x01) ^
((c >> 3) & 0x01) ^
((c >> 4) & 0x01) ^
((c >> 5) & 0x01) ^
((c >> 6) & 0x01) ^
((c >> 7) & 0x01)) != 1) {
WOLFSSL_MSG("Odd parity test fail");
return;
}
}
/* check is not weak. Weak key list from Nist
"Recommendation for the Triple
Data Encryption Algorithm
(TDEA) Block Cipher" */
mask = 0x01010101; mask2 = 0x01010101;
if (DES_check(mask, mask2, *key)) {
WOLFSSL_MSG("Weak key found");
return;
}
mask = 0xFEFEFEFE; mask2 = 0xFEFEFEFE;
if (DES_check(mask, mask2, *key)) {
WOLFSSL_MSG("Weak key found");
return;
}
mask = 0xE0E0E0E0; mask2 = 0xF1F1F1F1;
if (DES_check(mask, mask2, *key)) {
WOLFSSL_MSG("Weak key found");
return;
}
mask = 0x1F1F1F1F; mask2 = 0x0E0E0E0E;
if (DES_check(mask, mask2, *key)) {
WOLFSSL_MSG("Weak key found");
return;
}
/* semi-weak *key check (list from same Nist paper) */
mask = 0x011F011F; mask2 = 0x010E010E;
if (DES_check(mask, mask2, *key) ||
DES_check(ByteReverseWord32(mask), ByteReverseWord32(mask2), *key)) {
WOLFSSL_MSG("Weak key found");
return;
}
mask = 0x01E001E0; mask2 = 0x01F101F1;
if (DES_check(mask, mask2, *key) ||
DES_check(ByteReverseWord32(mask), ByteReverseWord32(mask2), *key)) {
WOLFSSL_MSG("Weak key found");
return;
}
mask = 0x01FE01FE; mask2 = 0x01FE01FE;
if (DES_check(mask, mask2, *key) ||
DES_check(ByteReverseWord32(mask), ByteReverseWord32(mask2), *key)) {
WOLFSSL_MSG("Weak key found");
return;
}
mask = 0x1FE01FE0; mask2 = 0x0EF10EF1;
if (DES_check(mask, mask2, *key) ||
DES_check(ByteReverseWord32(mask), ByteReverseWord32(mask2), *key)) {
WOLFSSL_MSG("Weak key found");
return;
}
mask = 0x1FFE1FFE; mask2 = 0x0EFE0EFE;
if (DES_check(mask, mask2, *key) ||
DES_check(ByteReverseWord32(mask), ByteReverseWord32(mask2), *key)) {
WOLFSSL_MSG("Weak key found");
return;
}
/* passed tests, now copy over key */
XMEMCPY(key, myDes, sizeof(WOLFSSL_const_DES_cblock));
}
}
2016-06-27 10:53:34 -07:00
void wolfSSL_DES_set_key_unchecked(WOLFSSL_const_DES_cblock* myDes,
WOLFSSL_DES_key_schedule* key)
{
if (myDes != NULL && key != NULL) {
XMEMCPY(key, myDes, sizeof(WOLFSSL_const_DES_cblock));
}
2016-06-27 10:53:34 -07:00
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_DES_set_odd_parity(WOLFSSL_DES_cblock* myDes)
{
(void)myDes;
WOLFSSL_STUB("wolfSSL_DES_set_odd_parity");
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_DES_ecb_encrypt(WOLFSSL_DES_cblock* desa,
WOLFSSL_DES_cblock* desb, WOLFSSL_DES_key_schedule* key, int len)
{
2016-06-27 10:53:34 -07:00
(void)desa;
(void)desb;
(void)key;
(void)len;
WOLFSSL_STUB("wolfSSL_DES_ecb_encrypt");
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#endif /* NO_DES3 */
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_BIO_printf(WOLFSSL_BIO* bio, const char* format, ...)
{
(void)bio;
(void)format;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_ASN1_UTCTIME_print(WOLFSSL_BIO* bio, const WOLFSSL_ASN1_UTCTIME* a)
{
(void)bio;
(void)a;
return 0;
}
2012-05-02 15:11:20 -07:00
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_sk_num(WOLFSSL_X509_REVOKED* rev)
{
(void)rev;
return 0;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
void* wolfSSL_sk_value(WOLFSSL_X509_REVOKED* rev, int i)
{
2016-06-27 10:53:34 -07:00
(void)rev;
(void)i;
return 0;
}
2012-05-17 17:44:54 -07:00
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
/* stunnel 4.28 needs */
void wolfSSL_CTX_sess_set_get_cb(WOLFSSL_CTX* ctx,
WOLFSSL_SESSION*(*f)(WOLFSSL*, unsigned char*, int, int*))
{
(void)ctx;
(void)f;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_CTX_sess_set_new_cb(WOLFSSL_CTX* ctx,
int (*f)(WOLFSSL*, WOLFSSL_SESSION*))
{
(void)ctx;
(void)f;
}
2012-05-17 17:44:54 -07:00
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_CTX_sess_set_remove_cb(WOLFSSL_CTX* ctx, void (*f)(WOLFSSL_CTX*,
WOLFSSL_SESSION*))
{
(void)ctx;
(void)f;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_i2d_SSL_SESSION(WOLFSSL_SESSION* sess, unsigned char** p)
{
2016-06-27 10:53:34 -07:00
(void)sess;
(void)p;
return sizeof(WOLFSSL_SESSION);
}
2012-05-17 17:44:54 -07:00
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_SESSION* wolfSSL_d2i_SSL_SESSION(WOLFSSL_SESSION** sess,
const unsigned char** p, long i)
{
(void)p;
(void)i;
if (sess)
return *sess;
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_SESSION_get_timeout(const WOLFSSL_SESSION* sess)
{
WOLFSSL_ENTER("wolfSSL_SESSION_get_timeout");
return sess->timeout;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
long wolfSSL_SESSION_get_time(const WOLFSSL_SESSION* sess)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_SESSION_get_time");
return sess->bornOn;
}
2012-05-17 17:44:54 -07:00
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
#endif /* OPENSSL_EXTRA */
#ifdef KEEP_PEER_CERT
char* wolfSSL_X509_get_subjectCN(WOLFSSL_X509* x509)
{
if (x509 == NULL)
return NULL;
return x509->subjectCN;
}
2016-06-27 10:53:34 -07:00
#endif /* KEEP_PEER_CERT */
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
#ifdef OPENSSL_EXTRA
#ifdef FORTRESS
int wolfSSL_cmp_peer_cert_to_file(WOLFSSL* ssl, const char *fname)
{
2016-06-27 10:53:34 -07:00
int ret = SSL_FATAL_ERROR;
WOLFSSL_ENTER("wolfSSL_cmp_peer_cert_to_file");
if (ssl != NULL && fname != NULL)
{
#ifdef WOLFSSL_SMALL_STACK
EncryptedInfo* info = NULL;
byte staticBuffer[1]; /* force heap usage */
#else
EncryptedInfo info[1];
byte staticBuffer[FILE_BUFFER_SIZE];
#endif
byte* myBuffer = staticBuffer;
int dynamic = 0;
XFILE file = XBADFILE;
long sz = 0;
int eccKey = 0;
WOLFSSL_CTX* ctx = ssl->ctx;
WOLFSSL_X509* peer_cert = &ssl->peerCert;
DerBuffer* fileDer = NULL;
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
file = XFOPEN(fname, "rb");
if (file == XBADFILE)
return SSL_BAD_FILE;
2014-07-10 11:18:49 -06:00
2016-06-27 10:53:34 -07:00
XFSEEK(file, 0, XSEEK_END);
sz = XFTELL(file);
XREWIND(file);
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (sz > (long)sizeof(staticBuffer)) {
WOLFSSL_MSG("Getting dynamic buffer");
myBuffer = (byte*)XMALLOC(sz, ctx->heap, DYNAMIC_TYPE_FILE);
dynamic = 1;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (info == NULL)
ret = MEMORY_E;
else
#endif
{
info->set = 0;
info->ctx = ctx;
info->consumed = 0;
2016-06-27 10:53:34 -07:00
if ((myBuffer != NULL) &&
(sz > 0) &&
(XFREAD(myBuffer, sz, 1, file) > 0) &&
(PemToDer(myBuffer, sz, CERT_TYPE,
&fileDer, ctx->heap, info, &eccKey) == 0) &&
(fileDer->length != 0) &&
(fileDer->length == peer_cert->derCert->length) &&
(XMEMCMP(peer_cert->derCert->buffer, fileDer->buffer,
fileDer->length) == 0))
{
ret = 0;
}
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
}
2016-06-27 10:53:34 -07:00
FreeDer(&fileDer);
2016-06-27 10:53:34 -07:00
if (dynamic)
XFREE(myBuffer, ctx->heap, DYNAMIC_TYPE_FILE);
2016-06-27 10:53:34 -07:00
XFCLOSE(file);
}
2015-11-02 15:51:01 -03:00
2016-06-27 10:53:34 -07:00
return ret;
}
2016-06-27 10:53:34 -07:00
#endif
2012-05-07 16:35:23 -07:00
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
static WC_RNG globalRNG;
static int initGlobalRNG = 0;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_RAND_seed(const void* seed, int len)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_RAND_seed");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
(void)seed;
(void)len;
if (initGlobalRNG == 0) {
if (wc_InitRng(&globalRNG) < 0) {
WOLFSSL_MSG("wolfSSL Init Global RNG failed");
return 0;
}
initGlobalRNG = 1;
}
2015-07-01 12:24:11 -07:00
return SSL_SUCCESS;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_RAND_bytes(unsigned char* buf, int num)
{
int ret = 0;
int initTmpRng = 0;
WC_RNG* rng = NULL;
#ifdef WOLFSSL_SMALL_STACK
WC_RNG* tmpRNG = NULL;
#else
WC_RNG tmpRNG[1];
#endif
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_RAND_bytes");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmpRNG == NULL)
return ret;
#endif
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (wc_InitRng(tmpRNG) == 0) {
rng = tmpRNG;
initTmpRng = 1;
}
2016-06-27 10:53:34 -07:00
else if (initGlobalRNG)
rng = &globalRNG;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (rng) {
if (wc_RNG_GenerateBlock(rng, buf, num) != 0)
WOLFSSL_MSG("Bad wc_RNG_GenerateBlock");
else
ret = SSL_SUCCESS;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (initTmpRng)
wc_FreeRng(tmpRNG);
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return ret;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
WOLFSSL_BN_CTX* wolfSSL_BN_CTX_new(void)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
static int ctx; /* wolfcrypt doesn't now need ctx */
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_CTX_new");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return (WOLFSSL_BN_CTX*)&ctx;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_BN_CTX_init(WOLFSSL_BN_CTX* ctx)
2015-07-01 12:24:11 -07:00
{
(void)ctx;
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_CTX_init");
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_BN_CTX_free(WOLFSSL_BN_CTX* ctx)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
(void)ctx;
WOLFSSL_MSG("wolfSSL_BN_CTX_free");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* do free since static ctx that does nothing */
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
static void InitwolfSSL_BigNum(WOLFSSL_BIGNUM* bn)
{
WOLFSSL_MSG("InitwolfSSL_BigNum");
if (bn) {
bn->neg = 0;
bn->internal = NULL;
}
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
WOLFSSL_BIGNUM* wolfSSL_BN_new(void)
{
WOLFSSL_BIGNUM* external;
mp_int* mpi;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_new");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
mpi = (mp_int*) XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
if (mpi == NULL) {
WOLFSSL_MSG("wolfSSL_BN_new malloc mpi failure");
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
external = (WOLFSSL_BIGNUM*) XMALLOC(sizeof(WOLFSSL_BIGNUM), NULL,
DYNAMIC_TYPE_BIGINT);
if (external == NULL) {
WOLFSSL_MSG("wolfSSL_BN_new malloc WOLFSSL_BIGNUM failure");
XFREE(mpi, NULL, DYNAMIC_TYPE_BIGINT);
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
InitwolfSSL_BigNum(external);
external->internal = mpi;
if (mp_init(mpi) != MP_OKAY) {
wolfSSL_BN_free(external);
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return external;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_BN_free(WOLFSSL_BIGNUM* bn)
{
WOLFSSL_MSG("wolfSSL_BN_free");
if (bn) {
if (bn->internal) {
mp_clear((mp_int*)bn->internal);
XFREE(bn->internal, NULL, DYNAMIC_TYPE_BIGINT);
bn->internal = NULL;
}
XFREE(bn, NULL, DYNAMIC_TYPE_BIGINT);
bn = NULL;
}
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_BN_clear_free(WOLFSSL_BIGNUM* bn)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_clear_free");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
wolfSSL_BN_free(bn);
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_BN_sub(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* a,
const WOLFSSL_BIGNUM* b)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_sub");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (r == NULL || a == NULL || b == NULL)
return 0;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (mp_sub((mp_int*)a->internal,(mp_int*)b->internal,
(mp_int*)r->internal) == MP_OKAY)
return SSL_SUCCESS;
WOLFSSL_MSG("wolfSSL_BN_sub mp_sub failed");
return 0;
2015-07-01 12:24:11 -07:00
}
2015-07-16 14:19:11 -06:00
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_BN_mod(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* a,
const WOLFSSL_BIGNUM* b, const WOLFSSL_BN_CTX* c)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
(void)c;
WOLFSSL_MSG("wolfSSL_BN_mod");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (r == NULL || a == NULL || b == NULL)
return 0;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (mp_mod((mp_int*)a->internal,(mp_int*)b->internal,
(mp_int*)r->internal) == MP_OKAY)
return SSL_SUCCESS;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_mod mp_mod failed");
return 0;
2015-07-01 12:24:11 -07:00
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
const WOLFSSL_BIGNUM* wolfSSL_BN_value_one(void)
{
2016-06-27 10:53:34 -07:00
static WOLFSSL_BIGNUM* bn_one = NULL;
WOLFSSL_MSG("wolfSSL_BN_value_one");
if (bn_one == NULL) {
bn_one = wolfSSL_BN_new();
if (bn_one)
mp_set_int((mp_int*)bn_one->internal, 1);
2012-05-01 16:12:12 -07:00
}
2016-06-27 10:53:34 -07:00
return bn_one;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
/* return compliant with OpenSSL
* size of BIGNUM in bytes, 0 if error */
int wolfSSL_BN_num_bytes(const WOLFSSL_BIGNUM* bn)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_num_bytes");
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (bn == NULL || bn->internal == NULL)
return SSL_FAILURE;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
return mp_unsigned_bin_size((mp_int*)bn->internal);
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
/* return compliant with OpenSSL
* size of BIGNUM in bits, 0 if error */
int wolfSSL_BN_num_bits(const WOLFSSL_BIGNUM* bn)
{
WOLFSSL_MSG("wolfSSL_BN_num_bits");
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (bn == NULL || bn->internal == NULL)
return SSL_FAILURE;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
return mp_count_bits((mp_int*)bn->internal);
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
/* return compliant with OpenSSL
* 1 if BIGNUM is zero, 0 else */
int wolfSSL_BN_is_zero(const WOLFSSL_BIGNUM* bn)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_is_zero");
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (bn == NULL || bn->internal == NULL)
return SSL_FAILURE;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (mp_iszero((mp_int*)bn->internal) == MP_YES)
return SSL_SUCCESS;
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
/* return compliant with OpenSSL
* 1 if BIGNUM is one, 0 else */
int wolfSSL_BN_is_one(const WOLFSSL_BIGNUM* bn)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_is_one");
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (bn == NULL || bn->internal == NULL)
return SSL_FAILURE;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (mp_cmp_d((mp_int*)bn->internal, 1) == MP_EQ)
return SSL_SUCCESS;
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
/* return compliant with OpenSSL
* 1 if BIGNUM is odd, 0 else */
int wolfSSL_BN_is_odd(const WOLFSSL_BIGNUM* bn)
{
WOLFSSL_MSG("wolfSSL_BN_is_odd");
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (bn == NULL || bn->internal == NULL)
return SSL_FAILURE;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (mp_isodd((mp_int*)bn->internal) == MP_YES)
return SSL_SUCCESS;
return SSL_FAILURE;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
/* return compliant with OpenSSL
* -1 if a < b, 0 if a == b and 1 if a > b
*/
2016-06-27 10:53:34 -07:00
int wolfSSL_BN_cmp(const WOLFSSL_BIGNUM* a, const WOLFSSL_BIGNUM* b)
{
2016-06-27 10:53:34 -07:00
int ret;
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_cmp");
if (a == NULL || a->internal == NULL || b == NULL || b->internal == NULL)
return SSL_FATAL_ERROR;
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
ret = mp_cmp((mp_int*)a->internal, (mp_int*)b->internal);
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
return (ret == MP_EQ ? 0 : (ret == MP_GT ? 1 : -1));
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
/* return compliant with OpenSSL
* length of BIGNUM in bytes, -1 if error */
int wolfSSL_BN_bn2bin(const WOLFSSL_BIGNUM* bn, unsigned char* r)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_bn2bin");
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (bn == NULL || bn->internal == NULL) {
WOLFSSL_MSG("NULL bn error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
if (r == NULL)
return mp_unsigned_bin_size((mp_int*)bn->internal);
2016-06-27 10:53:34 -07:00
if (mp_to_unsigned_bin((mp_int*)bn->internal, r) != MP_OKAY) {
WOLFSSL_MSG("mp_to_unsigned_bin error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
return mp_unsigned_bin_size((mp_int*)bn->internal);
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_BIGNUM* wolfSSL_BN_bin2bn(const unsigned char* str, int len,
WOLFSSL_BIGNUM* ret)
{
int weOwn = 0;
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_bin2bn");
2016-06-27 10:53:34 -07:00
/* if ret is null create a BN */
if (ret == NULL) {
ret = wolfSSL_BN_new();
weOwn = 1;
if (ret == NULL)
return NULL;
}
2016-06-27 10:53:34 -07:00
/* check ret and ret->internal then read in value */
if (ret && ret->internal) {
if (mp_read_unsigned_bin((mp_int*)ret->internal, str, len) != 0) {
WOLFSSL_MSG("mp_read_unsigned_bin failure");
if (weOwn)
wolfSSL_BN_free(ret);
return NULL;
2012-05-07 16:35:23 -07:00
}
2012-05-01 16:12:12 -07:00
}
2016-06-27 10:53:34 -07:00
return ret;
}
2016-06-27 10:53:34 -07:00
/* return compliant with OpenSSL
* 1 if success, 0 if error */
int wolfSSL_mask_bits(WOLFSSL_BIGNUM* bn, int n)
{
(void)bn;
(void)n;
WOLFSSL_MSG("wolfSSL_BN_mask_bits");
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2012-05-01 16:12:12 -07:00
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_BN_rand(WOLFSSL_BIGNUM* bn, int bits, int top, int bottom)
{
2016-06-27 10:53:34 -07:00
int ret = 0;
int len = bits / 8;
int initTmpRng = 0;
WC_RNG* rng = NULL;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
2016-06-27 10:53:34 -07:00
WC_RNG* tmpRNG = NULL;
byte* buff = NULL;
#else
2016-06-27 10:53:34 -07:00
WC_RNG tmpRNG[1];
byte buff[1024];
#endif
2016-06-27 10:53:34 -07:00
(void)top;
(void)bottom;
WOLFSSL_MSG("wolfSSL_BN_rand");
2016-06-27 10:53:34 -07:00
if (bits % 8)
len++;
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
buff = (byte*)XMALLOC(1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
tmpRNG = (WC_RNG*) XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (buff == NULL || tmpRNG == NULL) {
XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
#endif
2016-06-27 10:53:34 -07:00
if (bn == NULL || bn->internal == NULL)
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("Bad function arguments");
2016-06-27 10:53:34 -07:00
else if (wc_InitRng(tmpRNG) == 0) {
rng = tmpRNG;
initTmpRng = 1;
}
2016-06-27 10:53:34 -07:00
else if (initGlobalRNG)
rng = &globalRNG;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (rng) {
if (wc_RNG_GenerateBlock(rng, buff, len) != 0)
WOLFSSL_MSG("Bad wc_RNG_GenerateBlock");
else {
buff[0] |= 0x80 | 0x40;
buff[len-1] |= 0x01;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (mp_read_unsigned_bin((mp_int*)bn->internal,buff,len) != MP_OKAY)
WOLFSSL_MSG("mp read bin failed");
else
ret = SSL_SUCCESS;
}
2012-05-07 16:35:23 -07:00
}
2016-06-27 10:53:34 -07:00
if (initTmpRng)
wc_FreeRng(tmpRNG);
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
return ret;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if bit set, 0 else
*/
int wolfSSL_BN_is_bit_set(const WOLFSSL_BIGNUM* bn, int n)
{
if (bn == NULL || bn->internal == NULL) {
WOLFSSL_MSG("bn NULL error");
return SSL_FAILURE;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (n > DIGIT_BIT) {
WOLFSSL_MSG("input bit count too large");
return SSL_FAILURE;
2012-05-07 16:35:23 -07:00
}
2016-06-27 10:53:34 -07:00
return mp_is_bit_set((mp_int*)bn->internal, (mp_digit)n);
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 else
*/
int wolfSSL_BN_set_bit(WOLFSSL_BIGNUM* bn, int n)
{
2016-06-27 10:53:34 -07:00
if (bn == NULL || bn->internal == NULL) {
WOLFSSL_MSG("bn NULL error");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
if (mp_set_bit((mp_int*)bn->internal, n) != MP_OKAY) {
WOLFSSL_MSG("mp_set_int error");
return SSL_FAILURE;
2012-05-07 16:35:23 -07:00
}
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_BN_hex2bn(WOLFSSL_BIGNUM** bn, const char* str)
{
2016-06-27 10:53:34 -07:00
int ret = 0;
word32 decSz = 1024;
#ifdef WOLFSSL_SMALL_STACK
byte* decoded = NULL;
#else
byte decoded[1024];
#endif
WOLFSSL_MSG("wolfSSL_BN_hex2bn");
#ifdef WOLFSSL_SMALL_STACK
decoded = (byte*)XMALLOC(decSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (decoded == NULL)
return ret;
#endif
if (str == NULL)
WOLFSSL_MSG("Bad function argument");
else if (Base16_Decode((byte*)str, (int)XSTRLEN(str), decoded, &decSz) < 0)
WOLFSSL_MSG("Bad Base16_Decode error");
else if (bn == NULL)
ret = decSz;
else {
if (*bn == NULL)
*bn = wolfSSL_BN_new();
if (*bn == NULL)
WOLFSSL_MSG("BN new failed");
else if (wolfSSL_BN_bin2bn(decoded, decSz, *bn) == NULL)
WOLFSSL_MSG("Bad bin2bn error");
else
ret = SSL_SUCCESS;
2012-05-01 16:12:12 -07:00
}
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return ret;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_BIGNUM* wolfSSL_BN_dup(const WOLFSSL_BIGNUM* bn)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_BIGNUM* ret;
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_dup");
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (bn == NULL || bn->internal == NULL) {
WOLFSSL_MSG("bn NULL error");
return NULL;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
ret = wolfSSL_BN_new();
if (ret == NULL) {
WOLFSSL_MSG("bn new error");
return NULL;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (mp_copy((mp_int*)bn->internal, (mp_int*)ret->internal) != MP_OKAY) {
WOLFSSL_MSG("mp_copy error");
wolfSSL_BN_free(ret);
return NULL;
2012-05-01 16:12:12 -07:00
}
2016-06-27 10:53:34 -07:00
ret->neg = bn->neg;
2016-06-27 10:53:34 -07:00
return ret;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_BIGNUM* wolfSSL_BN_copy(WOLFSSL_BIGNUM* r, const WOLFSSL_BIGNUM* bn)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_copy");
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
if (mp_copy((mp_int*)bn->internal, (mp_int*)r->internal) != MP_OKAY) {
2014-12-19 11:27:01 -07:00
WOLFSSL_MSG("mp_copy error");
2016-06-27 10:53:34 -07:00
return NULL;
2012-05-02 15:11:20 -07:00
}
2016-06-27 10:53:34 -07:00
r->neg = bn->neg;
return r;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 else
*/
int wolfSSL_BN_set_word(WOLFSSL_BIGNUM* bn, WOLFSSL_BN_ULONG w)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_set_word");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (mp_set_int((mp_int*)bn->internal, w) != MP_OKAY) {
WOLFSSL_MSG("mp_init_set_int error");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
return SSL_SUCCESS;
2015-07-01 12:24:11 -07:00
}
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* number length in decimal if success, 0 if error
*/
int wolfSSL_BN_dec2bn(WOLFSSL_BIGNUM** bn, const char* str)
{
2016-06-27 10:53:34 -07:00
(void)bn;
(void)str;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_dec2bn");
return SSL_FAILURE;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
char *wolfSSL_BN_bn2dec(const WOLFSSL_BIGNUM *bn)
{
int len = 0;
char *buf;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_bn2dec");
if (bn == NULL || bn->internal == NULL) {
WOLFSSL_MSG("bn NULL error");
return NULL;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (mp_radix_size((mp_int*)bn->internal, 10, &len) != MP_OKAY) {
WOLFSSL_MSG("mp_radix_size failure");
return NULL;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
buf = (char*) XMALLOC(len, NULL, DYNAMIC_TYPE_ECC);
if (buf == NULL) {
WOLFSSL_MSG("wolfSSL_BN_bn2hex malloc buffer failure");
return NULL;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (mp_toradix((mp_int*)bn->internal, buf, 10) != MP_OKAY) {
XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
return NULL;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
return buf;
}
#else
char* wolfSSL_BN_bn2dec(const WOLFSSL_BIGNUM* bn)
{
(void)bn;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_bn2dec");
return NULL;
}
2016-06-27 10:53:34 -07:00
#endif /* defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) */
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 else
*/
int wolfSSL_BN_lshift(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *bn, int n)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_lshift");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (r == NULL || r->internal == NULL || bn == NULL || bn->internal == NULL){
WOLFSSL_MSG("bn NULL error");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (mp_mul_2d((mp_int*)bn->internal, n, (mp_int*)r->internal) != MP_OKAY) {
WOLFSSL_MSG("mp_mul_2d error");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 else
*/
int wolfSSL_BN_rshift(WOLFSSL_BIGNUM *r, const WOLFSSL_BIGNUM *bn, int n)
{
WOLFSSL_MSG("wolfSSL_BN_rshift");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (r == NULL || r->internal == NULL || bn == NULL || bn->internal == NULL){
WOLFSSL_MSG("bn NULL error");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
if (mp_div_2d((mp_int*)bn->internal, n,
(mp_int*)r->internal, NULL) != MP_OKAY) {
WOLFSSL_MSG("mp_mul_2d error");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 else
*/
int wolfSSL_BN_add_word(WOLFSSL_BIGNUM *bn, WOLFSSL_BN_ULONG w)
{
WOLFSSL_MSG("wolfSSL_BN_add_word");
if (bn == NULL || bn->internal == NULL) {
WOLFSSL_MSG("bn NULL error");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (mp_add_d((mp_int*)bn->internal, w, (mp_int*)bn->internal) != MP_OKAY) {
WOLFSSL_MSG("mp_add_d error");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
return SSL_SUCCESS;
2015-07-01 12:24:11 -07:00
}
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 else
*/
int wolfSSL_BN_add(WOLFSSL_BIGNUM *r, WOLFSSL_BIGNUM *a, WOLFSSL_BIGNUM *b)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_add");
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
if (r == NULL || r->internal == NULL || a == NULL || a->internal == NULL ||
b == NULL || b->internal == NULL) {
WOLFSSL_MSG("bn NULL error");
return SSL_FAILURE;
}
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
if (mp_add((mp_int*)a->internal, (mp_int*)b->internal,
(mp_int*)r->internal) != MP_OKAY) {
WOLFSSL_MSG("mp_add_d error");
return SSL_FAILURE;
}
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_KEY_GEN
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if prime, 0 if not, -1 if error
*/
int wolfSSL_BN_is_prime_ex(const WOLFSSL_BIGNUM *bn, int nbchecks,
WOLFSSL_BN_CTX *ctx, WOLFSSL_BN_GENCB *cb)
{
int res;
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
(void)ctx;
(void)cb;
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_is_prime_ex");
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
if (bn == NULL || bn->internal == NULL) {
WOLFSSL_MSG("bn NULL error");
return SSL_FATAL_ERROR;
}
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
if (mp_prime_is_prime((mp_int*)bn->internal, nbchecks, &res) != MP_OKAY) {
WOLFSSL_MSG("mp_prime_is_prime error");
return SSL_FATAL_ERROR;
}
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
if (res != MP_YES) {
WOLFSSL_MSG("mp_prime_is_prime not prime");
return SSL_FAILURE;
}
2012-05-07 16:35:23 -07:00
return SSL_SUCCESS;
}
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* (bn mod w) if success, -1 if error
*/
WOLFSSL_BN_ULONG wolfSSL_BN_mod_word(const WOLFSSL_BIGNUM *bn,
WOLFSSL_BN_ULONG w)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_BN_ULONG ret = 0;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_mod_word");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (bn == NULL || bn->internal == NULL) {
WOLFSSL_MSG("bn NULL error");
return (WOLFSSL_BN_ULONG)SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (mp_mod_d((mp_int*)bn->internal, w, &ret) != MP_OKAY) {
WOLFSSL_MSG("mp_add_d error");
return (WOLFSSL_BN_ULONG)SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return ret;
}
#endif /* #ifdef WOLFSSL_KEY_GEN */
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
#if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
char *wolfSSL_BN_bn2hex(const WOLFSSL_BIGNUM *bn)
{
int len = 0;
char *buf;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_bn2hex");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (bn == NULL || bn->internal == NULL) {
WOLFSSL_MSG("bn NULL error");
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (mp_radix_size((mp_int*)bn->internal, 16, &len) != MP_OKAY) {
WOLFSSL_MSG("mp_radix_size failure");
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
buf = (char*) XMALLOC(len, NULL, DYNAMIC_TYPE_ECC);
if (buf == NULL) {
WOLFSSL_MSG("wolfSSL_BN_bn2hex malloc buffer failure");
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (mp_toradix((mp_int*)bn->internal, buf, 16) != MP_OKAY) {
XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return buf;
2015-07-01 12:24:11 -07:00
}
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_FILESYSTEM
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
2016-06-27 10:53:34 -07:00
int wolfSSL_BN_print_fp(FILE *fp, const WOLFSSL_BIGNUM *bn)
{
2016-06-27 10:53:34 -07:00
char *buf;
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_print_fp");
2016-06-27 10:53:34 -07:00
if (fp == NULL || bn == NULL || bn->internal == NULL) {
WOLFSSL_MSG("bn NULL error");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
buf = wolfSSL_BN_bn2hex(bn);
if (buf == NULL) {
WOLFSSL_MSG("wolfSSL_BN_bn2hex failure");
return SSL_FAILURE;
}
2012-05-02 15:11:20 -07:00
2016-06-27 10:53:34 -07:00
fprintf(fp, "%s", buf);
XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
#endif /* !defined(NO_FILESYSTEM) */
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
#else /* defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) */
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
char *wolfSSL_BN_bn2hex(const WOLFSSL_BIGNUM *bn)
{
(void)bn;
2012-05-17 17:44:54 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_bn2hex not implemented");
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
return (char*)"";
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_FILESYSTEM
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
2016-06-27 10:53:34 -07:00
int wolfSSL_BN_print_fp(FILE *fp, const WOLFSSL_BIGNUM *bn)
{
2016-06-27 10:53:34 -07:00
(void)fp;
(void)bn;
2012-05-17 17:44:54 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_BN_print_fp not implemented");
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
#endif /* !defined(NO_FILESYSTEM) */
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
#endif /* defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) */
WOLFSSL_BIGNUM *wolfSSL_BN_CTX_get(WOLFSSL_BN_CTX *ctx)
{
2016-06-27 10:53:34 -07:00
/* ctx is not used, return new Bignum */
(void)ctx;
2012-05-17 17:44:54 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_BN_CTX_get");
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
return wolfSSL_BN_new();
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_BN_CTX_start(WOLFSSL_BN_CTX *ctx)
{
2016-06-27 10:53:34 -07:00
(void)ctx;
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_BN_CTX_start");
WOLFSSL_MSG("wolfSSL_BN_CTX_start TBD");
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_DH
static void InitwolfSSL_DH(WOLFSSL_DH* dh)
{
if (dh) {
dh->p = NULL;
dh->g = NULL;
dh->q = NULL;
2016-06-27 10:53:34 -07:00
dh->pub_key = NULL;
dh->priv_key = NULL;
dh->internal = NULL;
dh->inSet = 0;
dh->exSet = 0;
}
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_DH* wolfSSL_DH_new(void)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_DH* external;
DhKey* key;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_DH_new");
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
key = (DhKey*) XMALLOC(sizeof(DhKey), NULL, DYNAMIC_TYPE_DH);
if (key == NULL) {
WOLFSSL_MSG("wolfSSL_DH_new malloc DhKey failure");
return NULL;
}
2016-06-27 10:53:34 -07:00
external = (WOLFSSL_DH*) XMALLOC(sizeof(WOLFSSL_DH), NULL,
DYNAMIC_TYPE_DH);
if (external == NULL) {
WOLFSSL_MSG("wolfSSL_DH_new malloc WOLFSSL_DH failure");
XFREE(key, NULL, DYNAMIC_TYPE_DH);
return NULL;
}
2016-06-27 10:53:34 -07:00
InitwolfSSL_DH(external);
wc_InitDhKey(key);
external->internal = key;
return external;
}
void wolfSSL_DH_free(WOLFSSL_DH* dh)
{
WOLFSSL_MSG("wolfSSL_DH_free");
if (dh) {
if (dh->internal) {
wc_FreeDhKey((DhKey*)dh->internal);
XFREE(dh->internal, NULL, DYNAMIC_TYPE_DH);
dh->internal = NULL;
}
2016-06-27 10:53:34 -07:00
wolfSSL_BN_free(dh->priv_key);
wolfSSL_BN_free(dh->pub_key);
wolfSSL_BN_free(dh->g);
wolfSSL_BN_free(dh->p);
wolfSSL_BN_free(dh->q);
2016-06-27 10:53:34 -07:00
InitwolfSSL_DH(dh); /* set back to NULLs for safety */
XFREE(dh, NULL, DYNAMIC_TYPE_DH);
}
2016-06-27 10:53:34 -07:00
}
2016-06-27 10:53:34 -07:00
static int SetDhInternal(WOLFSSL_DH* dh)
{
int ret = SSL_FATAL_ERROR;
int pSz = 1024;
int gSz = 1024;
2015-07-01 12:24:11 -07:00
#ifdef WOLFSSL_SMALL_STACK
2016-06-27 10:53:34 -07:00
unsigned char* p = NULL;
unsigned char* g = NULL;
#else
2016-06-27 10:53:34 -07:00
unsigned char p[1024];
unsigned char g[1024];
#endif
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SetDhInternal");
if (dh == NULL || dh->p == NULL || dh->g == NULL)
WOLFSSL_MSG("Bad function arguments");
else if (wolfSSL_BN_bn2bin(dh->p, NULL) > pSz)
WOLFSSL_MSG("Bad p internal size");
else if (wolfSSL_BN_bn2bin(dh->g, NULL) > gSz)
WOLFSSL_MSG("Bad g internal size");
else {
#ifdef WOLFSSL_SMALL_STACK
p = (unsigned char*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
g = (unsigned char*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (p == NULL || g == NULL) {
XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(g, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
2016-06-27 10:53:34 -07:00
#endif
pSz = wolfSSL_BN_bn2bin(dh->p, p);
gSz = wolfSSL_BN_bn2bin(dh->g, g);
if (pSz <= 0 || gSz <= 0)
WOLFSSL_MSG("Bad BN2bin set");
else if (wc_DhSetKey((DhKey*)dh->internal, p, pSz, g, gSz) < 0)
WOLFSSL_MSG("Bad DH SetKey");
else {
2016-06-27 10:53:34 -07:00
dh->inSet = 1;
ret = SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(g, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
}
2015-07-01 12:24:11 -07:00
return ret;
}
2012-05-07 16:35:23 -07:00
/* return code compliant with OpenSSL :
2016-06-27 10:53:34 -07:00
* DH prime size in bytes if success, 0 if error
*/
2016-06-27 10:53:34 -07:00
int wolfSSL_DH_size(WOLFSSL_DH* dh)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_DH_size");
2016-06-27 10:53:34 -07:00
if (dh == NULL)
return SSL_FATAL_ERROR;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return wolfSSL_BN_num_bytes(dh->p);
}
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
int wolfSSL_DH_generate_key(WOLFSSL_DH* dh)
{
int ret = SSL_FAILURE;
word32 pubSz = 768;
word32 privSz = 768;
int initTmpRng = 0;
WC_RNG* rng = NULL;
#ifdef WOLFSSL_SMALL_STACK
2016-06-27 10:53:34 -07:00
unsigned char* pub = NULL;
unsigned char* priv = NULL;
WC_RNG* tmpRNG = NULL;
#else
2016-06-27 10:53:34 -07:00
unsigned char pub [768];
unsigned char priv[768];
WC_RNG tmpRNG[1];
#endif
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_DH_generate_key");
#ifdef WOLFSSL_SMALL_STACK
2016-06-27 10:53:34 -07:00
tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
pub = (unsigned char*)XMALLOC(pubSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
priv = (unsigned char*)XMALLOC(privSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmpRNG == NULL || pub == NULL || priv == NULL) {
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(priv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
#endif
2016-06-27 10:53:34 -07:00
if (dh == NULL || dh->p == NULL || dh->g == NULL)
WOLFSSL_MSG("Bad function arguments");
else if (dh->inSet == 0 && SetDhInternal(dh) != SSL_SUCCESS)
WOLFSSL_MSG("Bad DH set internal");
else if (wc_InitRng(tmpRNG) == 0) {
rng = tmpRNG;
initTmpRng = 1;
}
else {
WOLFSSL_MSG("Bad RNG Init, trying global");
if (initGlobalRNG == 0)
WOLFSSL_MSG("Global RNG no Init");
else
rng = &globalRNG;
}
if (rng) {
if (wc_DhGenerateKeyPair((DhKey*)dh->internal, rng, priv, &privSz,
pub, &pubSz) < 0)
WOLFSSL_MSG("Bad wc_DhGenerateKeyPair");
else {
if (dh->pub_key)
wolfSSL_BN_free(dh->pub_key);
dh->pub_key = wolfSSL_BN_new();
if (dh->pub_key == NULL) {
WOLFSSL_MSG("Bad DH new pub");
}
if (dh->priv_key)
wolfSSL_BN_free(dh->priv_key);
dh->priv_key = wolfSSL_BN_new();
if (dh->priv_key == NULL) {
WOLFSSL_MSG("Bad DH new priv");
}
if (dh->pub_key && dh->priv_key) {
if (wolfSSL_BN_bin2bn(pub, pubSz, dh->pub_key) == NULL)
WOLFSSL_MSG("Bad DH bn2bin error pub");
else if (wolfSSL_BN_bin2bn(priv, privSz, dh->priv_key) == NULL)
WOLFSSL_MSG("Bad DH bn2bin error priv");
else
ret = SSL_SUCCESS;
}
}
2016-06-27 10:53:34 -07:00
}
2016-06-27 10:53:34 -07:00
if (initTmpRng)
wc_FreeRng(tmpRNG);
#ifdef WOLFSSL_SMALL_STACK
2016-06-27 10:53:34 -07:00
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(priv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return ret;
}
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* size of shared secret if success, -1 if error
*/
int wolfSSL_DH_compute_key(unsigned char* key, WOLFSSL_BIGNUM* otherPub,
WOLFSSL_DH* dh)
{
2016-06-27 10:53:34 -07:00
int ret = SSL_FATAL_ERROR;
word32 keySz = 0;
word32 pubSz = 1024;
word32 privSz = 1024;
2014-12-19 11:27:01 -07:00
#ifdef WOLFSSL_SMALL_STACK
2016-06-27 10:53:34 -07:00
unsigned char* pub = NULL;
unsigned char* priv = NULL;
#else
2016-06-27 10:53:34 -07:00
unsigned char pub [1024];
unsigned char priv[1024];
#endif
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_DH_compute_key");
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
pub = (unsigned char*)XMALLOC(pubSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (pub == NULL)
return ret;
2016-06-27 10:53:34 -07:00
priv = (unsigned char*)XMALLOC(privSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (priv == NULL) {
XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
#endif
2016-06-27 10:53:34 -07:00
if (dh == NULL || dh->priv_key == NULL || otherPub == NULL)
WOLFSSL_MSG("Bad function arguments");
else if ((keySz = (word32)DH_size(dh)) == 0)
WOLFSSL_MSG("Bad DH_size");
else if (wolfSSL_BN_bn2bin(dh->priv_key, NULL) > (int)privSz)
WOLFSSL_MSG("Bad priv internal size");
else if (wolfSSL_BN_bn2bin(otherPub, NULL) > (int)pubSz)
WOLFSSL_MSG("Bad otherPub size");
else {
privSz = wolfSSL_BN_bn2bin(dh->priv_key, priv);
pubSz = wolfSSL_BN_bn2bin(otherPub, pub);
2016-06-27 10:53:34 -07:00
if (privSz <= 0 || pubSz <= 0)
WOLFSSL_MSG("Bad BN2bin set");
else if (wc_DhAgree((DhKey*)dh->internal, key, &keySz,
priv, privSz, pub, pubSz) < 0)
WOLFSSL_MSG("wc_DhAgree failed");
else
2016-06-27 10:53:34 -07:00
ret = (int)keySz;
}
2015-07-01 12:24:11 -07:00
#ifdef WOLFSSL_SMALL_STACK
2016-06-27 10:53:34 -07:00
XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(priv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2015-07-01 12:24:11 -07:00
#endif
2015-07-01 12:24:11 -07:00
return ret;
}
2016-06-27 10:53:34 -07:00
#endif /* NO_DH */
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_DSA
static void InitwolfSSL_DSA(WOLFSSL_DSA* dsa)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
if (dsa) {
dsa->p = NULL;
dsa->q = NULL;
dsa->g = NULL;
dsa->pub_key = NULL;
dsa->priv_key = NULL;
dsa->internal = NULL;
dsa->inSet = 0;
dsa->exSet = 0;
}
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_DSA* wolfSSL_DSA_new(void)
{
WOLFSSL_DSA* external;
DsaKey* key;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_DSA_new");
key = (DsaKey*) XMALLOC(sizeof(DsaKey), NULL, DYNAMIC_TYPE_DSA);
if (key == NULL) {
WOLFSSL_MSG("wolfSSL_DSA_new malloc DsaKey failure");
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
external = (WOLFSSL_DSA*) XMALLOC(sizeof(WOLFSSL_DSA), NULL,
DYNAMIC_TYPE_DSA);
if (external == NULL) {
WOLFSSL_MSG("wolfSSL_DSA_new malloc WOLFSSL_DSA failure");
XFREE(key, NULL, DYNAMIC_TYPE_DSA);
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
InitwolfSSL_DSA(external);
InitDsaKey(key);
external->internal = key;
return external;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_DSA_free(WOLFSSL_DSA* dsa)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_DSA_free");
2016-06-27 10:53:34 -07:00
if (dsa) {
if (dsa->internal) {
FreeDsaKey((DsaKey*)dsa->internal);
XFREE(dsa->internal, NULL, DYNAMIC_TYPE_DSA);
dsa->internal = NULL;
}
wolfSSL_BN_free(dsa->priv_key);
wolfSSL_BN_free(dsa->pub_key);
wolfSSL_BN_free(dsa->g);
wolfSSL_BN_free(dsa->q);
wolfSSL_BN_free(dsa->p);
InitwolfSSL_DSA(dsa); /* set back to NULLs for safety */
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
XFREE(dsa, NULL, DYNAMIC_TYPE_DSA);
dsa = NULL;
}
2016-06-27 10:53:34 -07:00
}
#endif /* NO_DSA */
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_RSA
static void InitwolfSSL_Rsa(WOLFSSL_RSA* rsa)
{
if (rsa) {
rsa->n = NULL;
rsa->e = NULL;
rsa->d = NULL;
rsa->p = NULL;
rsa->q = NULL;
rsa->dmp1 = NULL;
rsa->dmq1 = NULL;
rsa->iqmp = NULL;
rsa->internal = NULL;
rsa->inSet = 0;
rsa->exSet = 0;
}
2016-06-27 10:53:34 -07:00
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_RSA* wolfSSL_RSA_new(void)
{
WOLFSSL_RSA* external;
RsaKey* key;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_RSA_new");
2016-06-27 10:53:34 -07:00
key = (RsaKey*) XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_RSA);
if (key == NULL) {
WOLFSSL_MSG("wolfSSL_RSA_new malloc RsaKey failure");
return NULL;
}
2016-06-27 10:53:34 -07:00
external = (WOLFSSL_RSA*) XMALLOC(sizeof(WOLFSSL_RSA), NULL,
DYNAMIC_TYPE_RSA);
if (external == NULL) {
WOLFSSL_MSG("wolfSSL_RSA_new malloc WOLFSSL_RSA failure");
XFREE(key, NULL, DYNAMIC_TYPE_RSA);
return NULL;
}
2016-06-27 10:53:34 -07:00
InitwolfSSL_Rsa(external);
if (wc_InitRsaKey(key, NULL) != 0) {
WOLFSSL_MSG("InitRsaKey WOLFSSL_RSA failure");
XFREE(external, NULL, DYNAMIC_TYPE_RSA);
XFREE(key, NULL, DYNAMIC_TYPE_RSA);
return NULL;
}
2016-06-27 10:53:34 -07:00
external->internal = key;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
return external;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_RSA_free(WOLFSSL_RSA* rsa)
{
WOLFSSL_MSG("wolfSSL_RSA_free");
if (rsa) {
if (rsa->internal) {
wc_FreeRsaKey((RsaKey*)rsa->internal);
XFREE(rsa->internal, NULL, DYNAMIC_TYPE_RSA);
rsa->internal = NULL;
}
2016-06-27 10:53:34 -07:00
wolfSSL_BN_free(rsa->iqmp);
wolfSSL_BN_free(rsa->dmq1);
wolfSSL_BN_free(rsa->dmp1);
wolfSSL_BN_free(rsa->q);
wolfSSL_BN_free(rsa->p);
wolfSSL_BN_free(rsa->d);
wolfSSL_BN_free(rsa->e);
wolfSSL_BN_free(rsa->n);
InitwolfSSL_Rsa(rsa); /* set back to NULLs for safety */
XFREE(rsa, NULL, DYNAMIC_TYPE_RSA);
rsa = NULL;
}
}
#endif /* NO_RSA */
#if (!defined(NO_RSA) && !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)) \
|| !defined(NO_DSA) || defined(HAVE_ECC)
static int SetIndividualExternal(WOLFSSL_BIGNUM** bn, mp_int* mpi)
{
WOLFSSL_MSG("Entering SetIndividualExternal");
if (mpi == NULL || bn == NULL) {
WOLFSSL_MSG("mpi NULL error");
return SSL_FATAL_ERROR;
}
if (*bn == NULL) {
*bn = wolfSSL_BN_new();
if (*bn == NULL) {
WOLFSSL_MSG("SetIndividualExternal alloc failed");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (mp_copy(mpi, (mp_int*)((*bn)->internal)) != MP_OKAY) {
WOLFSSL_MSG("mp_copy error");
return SSL_FATAL_ERROR;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
static int SetIndividualInternal(WOLFSSL_BIGNUM* bn, mp_int* mpi)
{
WOLFSSL_MSG("Entering SetIndividualInternal");
2016-06-27 10:53:34 -07:00
if (bn == NULL || bn->internal == NULL) {
WOLFSSL_MSG("bn NULL error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
if (mpi == NULL || (mp_init(mpi) != MP_OKAY)) {
WOLFSSL_MSG("mpi NULL error");
return SSL_FATAL_ERROR;
}
if (mp_copy((mp_int*)bn->internal, mpi) != MP_OKAY) {
WOLFSSL_MSG("mp_copy error");
return SSL_FATAL_ERROR;
}
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
#endif /* !NO_RSA && !NO_DSA */
2012-05-17 17:44:54 -07:00
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_DSA
/* wolfSSL -> OpenSSL */
static int SetDsaExternal(WOLFSSL_DSA* dsa)
{
2016-06-27 10:53:34 -07:00
DsaKey* key;
WOLFSSL_MSG("Entering SetDsaExternal");
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (dsa == NULL || dsa->internal == NULL) {
WOLFSSL_MSG("dsa key NULL error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
key = (DsaKey*)dsa->internal;
if (SetIndividualExternal(&dsa->p, &key->p) != SSL_SUCCESS) {
WOLFSSL_MSG("dsa p key error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&dsa->q, &key->q) != SSL_SUCCESS) {
WOLFSSL_MSG("dsa q key error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&dsa->g, &key->g) != SSL_SUCCESS) {
WOLFSSL_MSG("dsa g key error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&dsa->pub_key, &key->y) != SSL_SUCCESS) {
WOLFSSL_MSG("dsa y key error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&dsa->priv_key, &key->x) != SSL_SUCCESS) {
WOLFSSL_MSG("dsa x key error");
return SSL_FATAL_ERROR;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
dsa->exSet = 1;
return SSL_SUCCESS;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
/* Openssl -> WolfSSL */
static int SetDsaInternal(WOLFSSL_DSA* dsa)
{
2016-06-27 10:53:34 -07:00
DsaKey* key;
WOLFSSL_MSG("Entering SetDsaInternal");
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (dsa == NULL || dsa->internal == NULL) {
WOLFSSL_MSG("dsa key NULL error");
return SSL_FATAL_ERROR;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
key = (DsaKey*)dsa->internal;
if (dsa->p != NULL &&
SetIndividualInternal(dsa->p, &key->p) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa p key error");
return SSL_FATAL_ERROR;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (dsa->q != NULL &&
SetIndividualInternal(dsa->q, &key->q) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa q key error");
return SSL_FATAL_ERROR;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (dsa->g != NULL &&
SetIndividualInternal(dsa->g, &key->g) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa g key error");
return SSL_FATAL_ERROR;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (dsa->pub_key != NULL) {
if (SetIndividualInternal(dsa->pub_key, &key->y) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa pub_key error");
return SSL_FATAL_ERROR;
}
/* public key */
key->type = DSA_PUBLIC;
2012-05-01 16:12:12 -07:00
}
2016-06-27 10:53:34 -07:00
if (dsa->priv_key != NULL) {
if (SetIndividualInternal(dsa->priv_key, &key->x) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa priv_key error");
return SSL_FATAL_ERROR;
}
/* private key */
key->type = DSA_PRIVATE;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
dsa->inSet = 1;
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
#endif /* NO_DSA */
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
#if !defined(NO_RSA)
#if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
/* WolfSSL -> OpenSSL */
static int SetRsaExternal(WOLFSSL_RSA* rsa)
{
2016-06-27 10:53:34 -07:00
RsaKey* key;
WOLFSSL_MSG("Entering SetRsaExternal");
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (rsa == NULL || rsa->internal == NULL) {
WOLFSSL_MSG("rsa key NULL error");
return SSL_FATAL_ERROR;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
key = (RsaKey*)rsa->internal;
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&rsa->n, &key->n) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa n key error");
return SSL_FATAL_ERROR;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&rsa->e, &key->e) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa e key error");
return SSL_FATAL_ERROR;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&rsa->d, &key->d) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa d key error");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&rsa->p, &key->p) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa p key error");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&rsa->q, &key->q) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa q key error");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&rsa->dmp1, &key->dP) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa dP key error");
return SSL_FATAL_ERROR;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&rsa->dmq1, &key->dQ) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa dQ key error");
return SSL_FATAL_ERROR;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&rsa->iqmp, &key->u) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa u key error");
return SSL_FATAL_ERROR;
2012-05-01 16:12:12 -07:00
}
2016-06-27 10:53:34 -07:00
rsa->exSet = 1;
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
/* Openssl -> WolfSSL */
static int SetRsaInternal(WOLFSSL_RSA* rsa)
{
2016-06-27 10:53:34 -07:00
RsaKey* key;
WOLFSSL_MSG("Entering SetRsaInternal");
if (rsa == NULL || rsa->internal == NULL) {
WOLFSSL_MSG("rsa key NULL error");
return SSL_FATAL_ERROR;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
key = (RsaKey*)rsa->internal;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (SetIndividualInternal(rsa->n, &key->n) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa n key error");
return SSL_FATAL_ERROR;
2012-05-01 16:12:12 -07:00
}
2016-06-27 10:53:34 -07:00
if (SetIndividualInternal(rsa->e, &key->e) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa e key error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
/* public key */
key->type = RSA_PUBLIC;
2016-06-27 10:53:34 -07:00
if (rsa->d != NULL) {
if (SetIndividualInternal(rsa->d, &key->d) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa d key error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
/* private key */
key->type = RSA_PRIVATE;
}
2016-06-27 10:53:34 -07:00
if (rsa->p != NULL &&
SetIndividualInternal(rsa->p, &key->p) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa p key error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
if (rsa->q != NULL &&
SetIndividualInternal(rsa->q, &key->q) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa q key error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
if (rsa->dmp1 != NULL &&
SetIndividualInternal(rsa->dmp1, &key->dP) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa dP key error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
if (rsa->dmq1 != NULL &&
SetIndividualInternal(rsa->dmq1, &key->dQ) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa dQ key error");
return SSL_FATAL_ERROR;
}
if (rsa->iqmp != NULL &&
SetIndividualInternal(rsa->iqmp, &key->u) != SSL_SUCCESS) {
WOLFSSL_MSG("rsa u key error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
rsa->inSet = 1;
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
#endif /* HAVE_USER_RSA */
2016-06-27 10:53:34 -07:00
/* return compliant with OpenSSL
* 1 if success, 0 if error
*/
int wolfSSL_RSA_generate_key_ex(WOLFSSL_RSA* rsa, int bits, WOLFSSL_BIGNUM* bn,
void* cb)
{
2016-06-27 10:53:34 -07:00
int ret = SSL_FAILURE;
2016-06-27 10:53:34 -07:00
(void)cb;
(void)bn;
(void)bits;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_RSA_generate_key_ex");
if (rsa == NULL || rsa->internal == NULL) {
/* bit size checked during make key call */
WOLFSSL_MSG("bad arguments");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_KEY_GEN
{
#ifdef WOLFSSL_SMALL_STACK
WC_RNG* rng = NULL;
#else
WC_RNG rng[1];
#endif
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (rng == NULL)
return SSL_FAILURE;
#endif
2016-06-27 10:53:34 -07:00
if (wc_InitRng(rng) < 0)
WOLFSSL_MSG("RNG init failed");
else if (wc_MakeRsaKey((RsaKey*)rsa->internal,
bits, 65537, rng) != MP_OKAY)
WOLFSSL_MSG("wc_MakeRsaKey failed");
else if (SetRsaExternal(rsa) != SSL_SUCCESS)
WOLFSSL_MSG("SetRsaExternal failed");
else {
rsa->inSet = 1;
ret = SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
wc_FreeRng(rng);
#ifdef WOLFSSL_SMALL_STACK
XFREE(rng, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
}
#else
WOLFSSL_MSG("No Key Gen built in");
#endif
return ret;
}
2016-06-27 10:53:34 -07:00
/* SSL_SUCCESS on ok */
int wolfSSL_RSA_blinding_on(WOLFSSL_RSA* rsa, WOLFSSL_BN_CTX* bn)
{
2016-06-27 10:53:34 -07:00
(void)rsa;
(void)bn;
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_RSA_blinding_on");
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS; /* on by default */
}
2016-06-27 10:53:34 -07:00
/* return compliant with OpenSSL
* size of encrypted data if success , -1 if error
*/
int wolfSSL_RSA_public_encrypt(int len, unsigned char* fr,
unsigned char* to, WOLFSSL_RSA* rsa, int padding)
{
(void)len;
(void)fr;
(void)to;
(void)rsa;
(void)padding;
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_RSA_public_encrypt");
2016-06-27 10:53:34 -07:00
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
/* return compliant with OpenSSL
* size of plain recovered data if success , -1 if error
*/
int wolfSSL_RSA_private_decrypt(int len, unsigned char* fr,
unsigned char* to, WOLFSSL_RSA* rsa, int padding)
{
(void)len;
(void)fr;
(void)to;
(void)rsa;
(void)padding;
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_RSA_private_decrypt");
2016-06-27 10:53:34 -07:00
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
/* return compliant with OpenSSL
* RSA modulus size in bytes, -1 if error
*/
int wolfSSL_RSA_size(const WOLFSSL_RSA* rsa)
{
WOLFSSL_MSG("wolfSSL_RSA_size");
2016-06-27 10:53:34 -07:00
if (rsa == NULL)
return SSL_FATAL_ERROR;
2016-06-27 10:53:34 -07:00
return wolfSSL_BN_num_bytes(rsa->n);
}
2016-06-27 10:53:34 -07:00
#endif /* NO_RSA */
2016-06-27 10:53:34 -07:00
#ifndef NO_DSA
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
2016-06-27 10:53:34 -07:00
int wolfSSL_DSA_generate_key(WOLFSSL_DSA* dsa)
{
2016-06-27 10:53:34 -07:00
int ret = SSL_FAILURE;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_DSA_generate_key");
2016-06-27 10:53:34 -07:00
if (dsa == NULL || dsa->internal == NULL) {
WOLFSSL_MSG("Bad arguments");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
if (dsa->inSet == 0) {
WOLFSSL_MSG("No DSA internal set, do it");
2016-06-27 10:53:34 -07:00
if (SetDsaInternal(dsa) != SSL_SUCCESS) {
WOLFSSL_MSG("SetDsaInternal failed");
return ret;
}
}
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_KEY_GEN
{
int initTmpRng = 0;
WC_RNG *rng = NULL;
#ifdef WOLFSSL_SMALL_STACK
WC_RNG *tmpRNG = NULL;
#else
WC_RNG tmpRNG[1];
#endif
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmpRNG == NULL)
return SSL_FATAL_ERROR;
#endif
if (wc_InitRng(tmpRNG) == 0) {
rng = tmpRNG;
initTmpRng = 1;
}
else {
WOLFSSL_MSG("Bad RNG Init, trying global");
if (initGlobalRNG == 0)
WOLFSSL_MSG("Global RNG no Init");
else
rng = &globalRNG;
}
2016-06-27 10:53:34 -07:00
if (rng) {
if (wc_MakeDsaKey(rng, (DsaKey*)dsa->internal) != MP_OKAY)
WOLFSSL_MSG("wc_MakeDsaKey failed");
else if (SetDsaExternal(dsa) != SSL_SUCCESS)
WOLFSSL_MSG("SetDsaExternal failed");
else
ret = SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
if (initTmpRng)
wc_FreeRng(tmpRNG);
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
}
2016-06-27 10:53:34 -07:00
#else /* WOLFSSL_KEY_GEN */
WOLFSSL_MSG("No Key Gen built in");
#endif
return ret;
}
2012-05-01 16:12:12 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
2016-06-27 10:53:34 -07:00
int wolfSSL_DSA_generate_parameters_ex(WOLFSSL_DSA* dsa, int bits,
unsigned char* seed, int seedLen,
int* counterRet,
unsigned long* hRet, void* cb)
{
2016-06-27 10:53:34 -07:00
int ret = SSL_FAILURE;
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
(void)bits;
(void)seed;
(void)seedLen;
(void)counterRet;
(void)hRet;
(void)cb;
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_DSA_generate_parameters_ex");
2015-03-20 10:59:54 -06:00
2016-06-27 10:53:34 -07:00
if (dsa == NULL || dsa->internal == NULL) {
WOLFSSL_MSG("Bad arguments");
return SSL_FAILURE;
}
2012-05-17 17:44:54 -07:00
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_KEY_GEN
{
int initTmpRng = 0;
WC_RNG *rng = NULL;
#ifdef WOLFSSL_SMALL_STACK
WC_RNG *tmpRNG = NULL;
#else
WC_RNG tmpRNG[1];
#endif
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmpRNG == NULL)
return SSL_FATAL_ERROR;
#endif
if (wc_InitRng(tmpRNG) == 0) {
rng = tmpRNG;
initTmpRng = 1;
}
else {
WOLFSSL_MSG("Bad RNG Init, trying global");
if (initGlobalRNG == 0)
WOLFSSL_MSG("Global RNG no Init");
else
rng = &globalRNG;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (rng) {
if (wc_MakeDsaParameters(rng, bits,
(DsaKey*)dsa->internal) != MP_OKAY)
WOLFSSL_MSG("wc_MakeDsaParameters failed");
else if (SetDsaExternal(dsa) != SSL_SUCCESS)
WOLFSSL_MSG("SetDsaExternal failed");
else
ret = SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
if (initTmpRng)
wc_FreeRng(tmpRNG);
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
}
#else /* WOLFSSL_KEY_GEN */
WOLFSSL_MSG("No Key Gen built in");
#endif
2016-06-27 10:53:34 -07:00
return ret;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
/* return SSL_SUCCESS on success, < 0 otherwise */
int wolfSSL_DSA_do_sign(const unsigned char* d, unsigned char* sigRet,
WOLFSSL_DSA* dsa)
{
2016-06-27 10:53:34 -07:00
int ret = SSL_FATAL_ERROR;
int initTmpRng = 0;
WC_RNG* rng = NULL;
#ifdef WOLFSSL_SMALL_STACK
WC_RNG* tmpRNG = NULL;
#else
WC_RNG tmpRNG[1];
#endif
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_DSA_do_sign");
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (d == NULL || sigRet == NULL || dsa == NULL) {
WOLFSSL_MSG("Bad function arguments");
return ret;
}
2016-06-27 10:53:34 -07:00
if (dsa->inSet == 0)
{
WOLFSSL_MSG("No DSA internal set, do it");
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
if (SetDsaInternal(dsa) != SSL_SUCCESS) {
WOLFSSL_MSG("SetDsaInternal failed");
return ret;
}
2012-05-01 16:12:12 -07:00
}
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmpRNG == NULL)
return SSL_FATAL_ERROR;
2016-06-27 10:53:34 -07:00
#endif
2016-06-27 10:53:34 -07:00
if (wc_InitRng(tmpRNG) == 0) {
rng = tmpRNG;
initTmpRng = 1;
}
2016-06-27 10:53:34 -07:00
else {
WOLFSSL_MSG("Bad RNG Init, trying global");
if (initGlobalRNG == 0)
WOLFSSL_MSG("Global RNG no Init");
else
rng = &globalRNG;
}
2016-06-27 10:53:34 -07:00
if (rng) {
if (DsaSign(d, sigRet, (DsaKey*)dsa->internal, rng) < 0)
WOLFSSL_MSG("DsaSign failed");
else
ret = SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
if (initTmpRng)
wc_FreeRng(tmpRNG);
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2016-06-27 10:53:34 -07:00
return ret;
}
2012-05-01 16:12:12 -07:00
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_DSA_do_verify(const unsigned char* d, unsigned char* sig,
WOLFSSL_DSA* dsa, int *dsacheck)
{
int ret = SSL_FATAL_ERROR;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_DSA_do_verify");
2016-06-27 10:53:34 -07:00
if (d == NULL || sig == NULL || dsa == NULL) {
WOLFSSL_MSG("Bad function arguments");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
if (dsa->inSet == 0)
{
WOLFSSL_MSG("No DSA internal set, do it");
2016-06-27 10:53:34 -07:00
if (SetDsaInternal(dsa) != SSL_SUCCESS) {
WOLFSSL_MSG("SetDsaInternal failed");
return SSL_FATAL_ERROR;
}
}
2016-06-27 10:53:34 -07:00
ret = DsaVerify(d, sig, (DsaKey*)dsa->internal, dsacheck);
if (ret != 0 || *dsacheck != 1) {
WOLFSSL_MSG("DsaVerify failed");
return ret;
}
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
#endif /* NO_DSA */
2016-06-27 10:53:34 -07:00
#ifndef NO_RSA
/* return SSL_SUCCES on ok, 0 otherwise */
int wolfSSL_RSA_sign(int type, const unsigned char* m,
unsigned int mLen, unsigned char* sigRet,
unsigned int* sigLen, WOLFSSL_RSA* rsa)
{
2016-06-27 10:53:34 -07:00
word32 outLen;
word32 signSz;
int initTmpRng = 0;
WC_RNG* rng = NULL;
int ret = 0;
#ifdef WOLFSSL_SMALL_STACK
WC_RNG* tmpRNG = NULL;
byte* encodedSig = NULL;
#else
WC_RNG tmpRNG[1];
byte encodedSig[MAX_ENCODED_SIG_SZ];
#endif
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_RSA_sign");
2016-06-27 10:53:34 -07:00
if (m == NULL || sigRet == NULL || sigLen == NULL || rsa == NULL) {
WOLFSSL_MSG("Bad function arguments");
return 0;
}
2016-06-27 10:53:34 -07:00
switch (type) {
#ifdef WOLFSSL_MD2
case NID_md2: type = MD2h; break;
#endif
#ifndef NO_MD5
case NID_md5: type = MD5h; break;
#endif
#ifndef NO_SHA
case NID_sha1: type = SHAh; break;
#endif
#ifndef NO_SHA256
case NID_sha256: type = SHA256h; break;
#endif
#ifdef WOLFSSL_SHA384
case NID_sha384: type = SHA384h; break;
#endif
#ifdef WOLFSSL_SHA512
case NID_sha512: type = SHA512h; break;
#endif
default:
WOLFSSL_MSG("This NID (md type) not configured or not implemented");
return 0;
}
2016-06-27 10:53:34 -07:00
if (rsa->inSet == 0)
{
WOLFSSL_MSG("No RSA internal set, do it");
2016-06-27 10:53:34 -07:00
if (SetRsaInternal(rsa) != SSL_SUCCESS) {
WOLFSSL_MSG("SetRsaInternal failed");
return 0;
}
}
2016-06-27 10:53:34 -07:00
outLen = (word32)wolfSSL_BN_num_bytes(rsa->n);
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmpRNG == NULL)
return 0;
2016-06-27 10:53:34 -07:00
encodedSig = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (encodedSig == NULL) {
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return 0;
}
#endif
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (outLen == 0)
WOLFSSL_MSG("Bad RSA size");
else if (wc_InitRng(tmpRNG) == 0) {
rng = tmpRNG;
initTmpRng = 1;
}
2016-06-27 10:53:34 -07:00
else {
WOLFSSL_MSG("Bad RNG Init, trying global");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (initGlobalRNG == 0)
WOLFSSL_MSG("Global RNG no Init");
else
rng = &globalRNG;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (rng) {
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
signSz = wc_EncodeSignature(encodedSig, m, mLen, type);
if (signSz == 0) {
WOLFSSL_MSG("Bad Encode Signature");
}
else {
*sigLen = wc_RsaSSL_Sign(encodedSig, signSz, sigRet, outLen,
(RsaKey*)rsa->internal, rng);
if (*sigLen <= 0)
WOLFSSL_MSG("Bad Rsa Sign");
else
ret = SSL_SUCCESS;
}
}
2016-06-27 10:53:34 -07:00
if (initTmpRng)
wc_FreeRng(tmpRNG);
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(encodedSig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2016-06-27 10:53:34 -07:00
if (ret == SSL_SUCCESS)
WOLFSSL_MSG("wolfSSL_RSA_sign success");
else {
WOLFSSL_MSG("wolfSSL_RSA_sign failed");
}
2016-06-27 10:53:34 -07:00
return ret;
}
2016-06-27 10:53:34 -07:00
int wolfSSL_RSA_public_decrypt(int flen, unsigned char* from,
unsigned char* to, WOLFSSL_RSA* rsa, int padding)
{
int tlen = 0;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_RSA_public_decrypt");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (rsa == NULL || rsa->internal == NULL || from == NULL) {
WOLFSSL_MSG("Bad function arguments");
return 0;
}
2016-06-27 10:53:34 -07:00
if (padding != RSA_PKCS1_PADDING) {
WOLFSSL_MSG("wolfSSL_RSA_public_decrypt unsupported padding");
return 0;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (rsa->inSet == 0)
{
WOLFSSL_MSG("No RSA internal set, do it");
2016-06-27 10:53:34 -07:00
if (SetRsaInternal(rsa) != SSL_SUCCESS) {
WOLFSSL_MSG("SetRsaInternal failed");
return 0;
}
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* size of 'to' buffer must be size of RSA key */
tlen = wc_RsaSSL_Verify(from, flen, to, wolfSSL_RSA_size(rsa),
(RsaKey*)rsa->internal);
if (tlen <= 0)
WOLFSSL_MSG("wolfSSL_RSA_public_decrypt failed");
else {
WOLFSSL_MSG("wolfSSL_RSA_public_decrypt success");
}
2016-06-27 10:53:34 -07:00
return tlen;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
/* generate p-1 and q-1, SSL_SUCCESS on ok */
int wolfSSL_RSA_GenAdd(WOLFSSL_RSA* rsa)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
int err;
mp_int tmp;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_RsaGenAdd");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (rsa == NULL || rsa->p == NULL || rsa->q == NULL || rsa->d == NULL ||
rsa->dmp1 == NULL || rsa->dmq1 == NULL) {
WOLFSSL_MSG("rsa no init error");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (mp_init(&tmp) != MP_OKAY) {
WOLFSSL_MSG("mp_init error");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
err = mp_sub_d((mp_int*)rsa->p->internal, 1, &tmp);
if (err != MP_OKAY) {
WOLFSSL_MSG("mp_sub_d error");
}
else
err = mp_mod((mp_int*)rsa->d->internal, &tmp,
(mp_int*)rsa->dmp1->internal);
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (err != MP_OKAY) {
WOLFSSL_MSG("mp_mod error");
}
else
err = mp_sub_d((mp_int*)rsa->q->internal, 1, &tmp);
if (err != MP_OKAY) {
WOLFSSL_MSG("mp_sub_d error");
}
else
err = mp_mod((mp_int*)rsa->d->internal, &tmp,
(mp_int*)rsa->dmq1->internal);
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
mp_clear(&tmp);
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (err == MP_OKAY)
return SSL_SUCCESS;
else
return SSL_FATAL_ERROR;
}
#endif /* NO_RSA */
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_HMAC_Init(WOLFSSL_HMAC_CTX* ctx, const void* key, int keylen,
const EVP_MD* type)
{
WOLFSSL_MSG("wolfSSL_HMAC_Init");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (ctx == NULL) {
WOLFSSL_MSG("no ctx on init");
return;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (type) {
WOLFSSL_MSG("init has type");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (XSTRNCMP(type, "MD5", 3) == 0) {
WOLFSSL_MSG("md5 hmac");
ctx->type = MD5;
}
else if (XSTRNCMP(type, "SHA256", 6) == 0) {
WOLFSSL_MSG("sha256 hmac");
ctx->type = SHA256;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* has to be last since would pick or 256, 384, or 512 too */
else if (XSTRNCMP(type, "SHA", 3) == 0) {
WOLFSSL_MSG("sha hmac");
ctx->type = SHA;
}
else {
WOLFSSL_MSG("bad init type");
}
}
2016-06-27 10:53:34 -07:00
if (key && keylen) {
WOLFSSL_MSG("keying hmac");
wc_HmacSetKey(&ctx->hmac, ctx->type, (const byte*)key, (word32)keylen);
/* OpenSSL compat, no error */
}
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_HMAC_Update(WOLFSSL_HMAC_CTX* ctx, const unsigned char* data,
int len)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_HMAC_Update");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (ctx && data) {
WOLFSSL_MSG("updating hmac");
wc_HmacUpdate(&ctx->hmac, data, (word32)len);
/* OpenSSL compat, no error */
}
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_HMAC_Final(WOLFSSL_HMAC_CTX* ctx, unsigned char* hash,
unsigned int* len)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_HMAC_Final");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (ctx && hash) {
WOLFSSL_MSG("final hmac");
wc_HmacFinal(&ctx->hmac, hash);
/* OpenSSL compat, no error */
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (len) {
WOLFSSL_MSG("setting output len");
switch (ctx->type) {
case MD5:
*len = MD5_DIGEST_SIZE;
break;
2016-06-27 10:53:34 -07:00
case SHA:
*len = SHA_DIGEST_SIZE;
break;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
case SHA256:
*len = SHA256_DIGEST_SIZE;
break;
default:
WOLFSSL_MSG("bad hmac type");
}
}
}
2016-06-27 10:53:34 -07:00
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_HMAC_cleanup(WOLFSSL_HMAC_CTX* ctx)
{
(void)ctx;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_HMAC_cleanup");
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
const WOLFSSL_EVP_MD* wolfSSL_EVP_get_digestbynid(int id)
{
WOLFSSL_MSG("wolfSSL_get_digestbynid");
2016-06-27 10:53:34 -07:00
switch(id) {
#ifndef NO_MD5
case NID_md5:
return wolfSSL_EVP_md5();
#endif
2016-06-27 10:53:34 -07:00
#ifndef NO_SHA
case NID_sha1:
return wolfSSL_EVP_sha1();
#endif
default:
WOLFSSL_MSG("Bad digest id value");
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_RSA* wolfSSL_EVP_PKEY_get1_RSA(WOLFSSL_EVP_PKEY* key)
{
(void)key;
WOLFSSL_MSG("wolfSSL_EVP_PKEY_get1_RSA not implemented");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return NULL;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
WOLFSSL_DSA* wolfSSL_EVP_PKEY_get1_DSA(WOLFSSL_EVP_PKEY* key)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
(void)key;
WOLFSSL_MSG("wolfSSL_EVP_PKEY_get1_DSA not implemented");
2016-06-27 10:53:34 -07:00
return NULL;
}
2016-06-27 10:53:34 -07:00
WOLFSSL_EC_KEY* wolfSSL_EVP_PKEY_get1_EC_KEY(WOLFSSL_EVP_PKEY* key)
{
(void)key;
WOLFSSL_MSG("wolfSSL_EVP_PKEY_get1_EC_KEY not implemented");
2016-06-27 10:53:34 -07:00
return NULL;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
void* wolfSSL_EVP_X_STATE(const WOLFSSL_EVP_CIPHER_CTX* ctx)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_EVP_X_STATE");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (ctx) {
switch (ctx->cipherType) {
case ARC4_TYPE:
WOLFSSL_MSG("returning arc4 state");
return (void*)&ctx->cipher.arc4.x;
default:
WOLFSSL_MSG("bad x state type");
return 0;
}
}
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_EVP_X_STATE_LEN(const WOLFSSL_EVP_CIPHER_CTX* ctx)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_EVP_X_STATE_LEN");
2016-06-27 10:53:34 -07:00
if (ctx) {
switch (ctx->cipherType) {
case ARC4_TYPE:
WOLFSSL_MSG("returning arc4 state size");
return sizeof(Arc4);
default:
WOLFSSL_MSG("bad x state type");
return 0;
}
}
return 0;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
#ifndef NO_DES3
void wolfSSL_3des_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, int doset,
unsigned char* iv, int len)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
(void)len;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_3des_iv");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (ctx == NULL || iv == NULL) {
WOLFSSL_MSG("Bad function argument");
return;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (doset)
wc_Des3_SetIV(&ctx->cipher.des3, iv); /* OpenSSL compat, no ret */
else
XMEMCPY(iv, &ctx->cipher.des3.reg, DES_BLOCK_SIZE);
}
#endif /* NO_DES3 */
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_AES
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_aes_ctr_iv(WOLFSSL_EVP_CIPHER_CTX* ctx, int doset,
unsigned char* iv, int len)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
(void)len;
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_aes_ctr_iv");
if (ctx == NULL || iv == NULL) {
WOLFSSL_MSG("Bad function argument");
return;
}
2016-06-27 10:53:34 -07:00
if (doset)
wc_AesSetIV(&ctx->cipher.aes, iv); /* OpenSSL compat, no ret */
else
XMEMCPY(iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
#endif /* NO_AES */
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
const WOLFSSL_EVP_MD* wolfSSL_EVP_ripemd160(void)
{
WOLFSSL_MSG("wolfSSL_ripemd160");
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_EVP_MD_size(const WOLFSSL_EVP_MD* type)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_EVP_MD_size");
2016-06-27 10:53:34 -07:00
if (type == NULL) {
WOLFSSL_MSG("No md type arg");
return BAD_FUNC_ARG;
}
2016-06-27 10:53:34 -07:00
if (XSTRNCMP(type, "SHA256", 6) == 0) {
return SHA256_DIGEST_SIZE;
}
2016-06-27 10:53:34 -07:00
#ifndef NO_MD5
else if (XSTRNCMP(type, "MD5", 3) == 0) {
return MD5_DIGEST_SIZE;
}
#endif
#ifdef WOLFSSL_SHA224
else if (XSTRNCMP(type, "SHA224", 6) == 0) {
return SHA224_DIGEST_SIZE;
}
#endif
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SHA384
else if (XSTRNCMP(type, "SHA384", 6) == 0) {
return SHA384_DIGEST_SIZE;
}
#endif
#ifdef WOLFSSL_SHA512
else if (XSTRNCMP(type, "SHA512", 6) == 0) {
return SHA512_DIGEST_SIZE;
}
2016-06-27 10:53:34 -07:00
#endif
#ifndef NO_SHA
/* has to be last since would pick or 256, 384, or 512 too */
else if (XSTRNCMP(type, "SHA", 3) == 0) {
return SHA_DIGEST_SIZE;
}
#endif
2016-06-27 10:53:34 -07:00
return BAD_FUNC_ARG;
}
2016-06-27 10:53:34 -07:00
int wolfSSL_EVP_CIPHER_CTX_iv_length(const WOLFSSL_EVP_CIPHER_CTX* ctx)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_EVP_CIPHER_CTX_iv_length");
2016-06-27 10:53:34 -07:00
switch (ctx->cipherType) {
2016-06-27 10:53:34 -07:00
case AES_128_CBC_TYPE :
case AES_192_CBC_TYPE :
case AES_256_CBC_TYPE :
WOLFSSL_MSG("AES CBC");
return AES_BLOCK_SIZE;
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_AES_COUNTER
case AES_128_CTR_TYPE :
case AES_192_CTR_TYPE :
case AES_256_CTR_TYPE :
WOLFSSL_MSG("AES CTR");
return AES_BLOCK_SIZE;
#endif
2016-06-27 10:53:34 -07:00
case DES_CBC_TYPE :
WOLFSSL_MSG("DES CBC");
return DES_BLOCK_SIZE;
2016-06-27 10:53:34 -07:00
case DES_EDE3_CBC_TYPE :
WOLFSSL_MSG("DES EDE3 CBC");
return DES_BLOCK_SIZE;
#ifdef HAVE_IDEA
case IDEA_CBC_TYPE :
WOLFSSL_MSG("IDEA CBC");
return IDEA_BLOCK_SIZE;
#endif
2016-06-27 10:53:34 -07:00
case ARC4_TYPE :
WOLFSSL_MSG("ARC4");
return 0;
case NULL_CIPHER_TYPE :
WOLFSSL_MSG("NULL");
return 0;
default: {
WOLFSSL_MSG("bad type");
}
}
2016-06-27 10:53:34 -07:00
return 0;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_OPENSSL_free(void* p)
{
WOLFSSL_MSG("wolfSSL_OPENSSL_free");
XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
}
2016-06-27 10:53:34 -07:00
#if defined(WOLFSSL_KEY_GEN)
static int EncryptDerKey(byte *der, int *derSz, const EVP_CIPHER* cipher,
unsigned char* passwd, int passwdSz, byte **cipherInfo)
{
2016-06-27 10:53:34 -07:00
int ret, paddingSz;
word32 idx, cipherInfoSz;
#ifdef WOLFSSL_SMALL_STACK
EncryptedInfo* info = NULL;
#else
EncryptedInfo info[1];
#endif
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("EncryptDerKey");
if (der == NULL || derSz == NULL || cipher == NULL ||
passwd == NULL || cipherInfo == NULL)
return BAD_FUNC_ARG;
#ifdef WOLFSSL_SMALL_STACK
info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (info == NULL) {
WOLFSSL_MSG("malloc failed");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
#endif
info->set = 0;
info->ctx = NULL;
info->consumed = 0;
2016-06-27 10:53:34 -07:00
/* set iv size */
if (XSTRNCMP(cipher, "DES", 3) == 0)
info->ivSz = DES_IV_SIZE;
else if (XSTRNCMP(cipher, "AES", 3) == 0)
info->ivSz = AES_IV_SIZE;
else {
WOLFSSL_MSG("unsupported cipher");
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* set the cipher name on info */
XSTRNCPY(info->name, cipher, NAME_SZ);
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* Generate a random salt */
if (wolfSSL_RAND_bytes(info->iv, info->ivSz) != SSL_SUCCESS) {
WOLFSSL_MSG("generate iv failed");
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* add the padding before encryption */
paddingSz = ((*derSz)/info->ivSz + 1) * info->ivSz - (*derSz);
if (paddingSz == 0)
paddingSz = info->ivSz;
XMEMSET(der+(*derSz), (byte)paddingSz, paddingSz);
(*derSz) += paddingSz;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* encrypt buffer */
if (wolfssl_encrypt_buffer_key(der, *derSz,
passwd, passwdSz, info) != SSL_SUCCESS) {
WOLFSSL_MSG("encrypt key failed");
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* create cipher info : 'cipher_name,Salt(hex)' */
cipherInfoSz = (word32)(2*info->ivSz + XSTRLEN(info->name) + 2);
*cipherInfo = (byte*)XMALLOC(cipherInfoSz, NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (*cipherInfo == NULL) {
WOLFSSL_MSG("malloc failed");
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
XSTRNCPY((char*)*cipherInfo, info->name, cipherInfoSz);
XSTRNCAT((char*)*cipherInfo, ",", 1);
2016-06-27 10:53:34 -07:00
idx = (word32)XSTRLEN((char*)*cipherInfo);
cipherInfoSz -= idx;
ret = Base16_Encode(info->iv, info->ivSz, *cipherInfo+idx, &cipherInfoSz);
#ifdef WOLFSSL_SMALL_STACK
XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
if (ret != 0) {
WOLFSSL_MSG("Base16_Encode failed");
XFREE(*cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
#endif /* defined(WOLFSSL_KEY_GEN) */
#if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
2015-07-01 12:24:11 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
2016-06-27 10:53:34 -07:00
int wolfSSL_PEM_write_mem_RSAPrivateKey(RSA* rsa, const EVP_CIPHER* cipher,
unsigned char* passwd, int passwdSz,
unsigned char **pem, int *plen)
{
2016-06-27 10:53:34 -07:00
byte *derBuf, *tmp, *cipherInfo = NULL;
int der_max_len = 0, derSz = 0;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_PEM_write_mem_RSAPrivateKey");
2016-06-27 10:53:34 -07:00
if (pem == NULL || plen == NULL || rsa == NULL || rsa->internal == NULL) {
WOLFSSL_MSG("Bad function arguments");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (rsa->inSet == 0) {
WOLFSSL_MSG("No RSA internal set, do it");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (SetRsaInternal(rsa) != SSL_SUCCESS) {
WOLFSSL_MSG("SetRsaInternal failed");
return SSL_FAILURE;
}
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* 5 > size of n, d, p, q, d%(p-1), d(q-1), 1/q%p, e + ASN.1 additional
* informations
*/
der_max_len = 5 * wolfSSL_RSA_size(rsa) + AES_BLOCK_SIZE;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
derBuf = (byte*)XMALLOC(der_max_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (derBuf == NULL) {
WOLFSSL_MSG("malloc failed");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
/* Key to DER */
derSz = wc_RsaKeyToDer((RsaKey*)rsa->internal, derBuf, der_max_len);
if (derSz < 0) {
WOLFSSL_MSG("wc_RsaKeyToDer failed");
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* encrypt DER buffer if required */
if (passwd != NULL && passwdSz > 0 && cipher != NULL) {
int ret;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
ret = EncryptDerKey(derBuf, &derSz, cipher,
passwd, passwdSz, &cipherInfo);
if (ret != SSL_SUCCESS) {
WOLFSSL_MSG("EncryptDerKey failed");
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* tmp buffer with a max size */
*plen = (derSz * 2) + sizeof(BEGIN_RSA_PRIV) +
sizeof(END_RSA_PRIV) + HEADER_ENCRYPTED_KEY_SIZE;
}
2016-06-27 10:53:34 -07:00
else /* tmp buffer with a max size */
*plen = (derSz * 2) + sizeof(BEGIN_RSA_PRIV) + sizeof(END_RSA_PRIV);
tmp = (byte*)XMALLOC(*plen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmp == NULL) {
WOLFSSL_MSG("malloc failed");
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (cipherInfo != NULL)
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
/* DER to PEM */
*plen = wc_DerToPemEx(derBuf, derSz, tmp, *plen, cipherInfo, PRIVATEKEY_TYPE);
if (*plen <= 0) {
WOLFSSL_MSG("wc_DerToPemEx failed");
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (cipherInfo != NULL)
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (cipherInfo != NULL)
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
*pem = (byte*)XMALLOC((*plen)+1, NULL, DYNAMIC_TYPE_KEY);
if (*pem == NULL) {
WOLFSSL_MSG("malloc failed");
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
XMEMSET(*pem, 0, (*plen)+1);
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (XMEMCPY(*pem, tmp, *plen) == NULL) {
WOLFSSL_MSG("XMEMCPY failed");
XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_SUCCESS;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
#ifndef NO_FILESYSTEM
/* return code compliant with OpenSSL :
2016-06-27 10:53:34 -07:00
* 1 if success, 0 if error
*/
2016-06-27 10:53:34 -07:00
int wolfSSL_PEM_write_RSAPrivateKey(FILE *fp, WOLFSSL_RSA *rsa,
const EVP_CIPHER *enc,
unsigned char *kstr, int klen,
pem_password_cb *cb, void *u)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
byte *pem;
int plen, ret;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
(void)cb;
(void)u;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_PEM_write_RSAPrivateKey");
2016-06-27 10:53:34 -07:00
if (fp == NULL || rsa == NULL || rsa->internal == NULL) {
WOLFSSL_MSG("Bad function arguments");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
ret = wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, enc, kstr, klen, &pem, &plen);
if (ret != SSL_SUCCESS) {
WOLFSSL_MSG("wolfSSL_PEM_write_mem_RSAPrivateKey failed");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
ret = (int)XFWRITE(pem, plen, 1, fp);
if (ret != 1) {
WOLFSSL_MSG("RSA private key file write failed");
return SSL_FAILURE;
}
XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
return SSL_SUCCESS;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
#endif /* NO_FILESYSTEM */
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_PEM_write_bio_RSAPrivateKey(WOLFSSL_BIO* bio, RSA* rsa,
const EVP_CIPHER* cipher,
unsigned char* passwd, int len,
pem_password_cb cb, void* arg)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
(void)bio;
(void)rsa;
(void)cipher;
(void)passwd;
(void)len;
(void)cb;
(void)arg;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_PEM_write_bio_RSAPrivateKey not implemented");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
#endif /* defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) */
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
#ifdef HAVE_ECC
/* EC_POINT Openssl -> WolfSSL */
static int SetECPointInternal(WOLFSSL_EC_POINT *p)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
ecc_point* point;
WOLFSSL_ENTER("SetECPointInternal");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (p == NULL || p->internal == NULL) {
WOLFSSL_MSG("ECPoint NULL error");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
point = (ecc_point*)p->internal;
if (p->X != NULL && SetIndividualInternal(p->X, point->x) != SSL_SUCCESS) {
WOLFSSL_MSG("ecc point X error");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (p->Y != NULL && SetIndividualInternal(p->Y, point->y) != SSL_SUCCESS) {
WOLFSSL_MSG("ecc point Y error");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (p->Z != NULL && SetIndividualInternal(p->Z, point->z) != SSL_SUCCESS) {
WOLFSSL_MSG("ecc point Z error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
p->inSet = 1;
return SSL_SUCCESS;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
/* EC_POINT WolfSSL -> OpenSSL */
static int SetECPointExternal(WOLFSSL_EC_POINT *p)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
ecc_point* point;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SetECPointExternal");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (p == NULL || p->internal == NULL) {
WOLFSSL_MSG("ECPoint NULL error");
return SSL_FATAL_ERROR;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
point = (ecc_point*)p->internal;
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&p->X, point->x) != SSL_SUCCESS) {
WOLFSSL_MSG("ecc point X error");
return SSL_FATAL_ERROR;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&p->Y, point->y) != SSL_SUCCESS) {
WOLFSSL_MSG("ecc point Y error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
if (SetIndividualExternal(&p->Z, point->z) != SSL_SUCCESS) {
WOLFSSL_MSG("ecc point Z error");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
p->exSet = 1;
return SSL_SUCCESS;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
/* EC_KEY wolfSSL -> OpenSSL */
static int SetECKeyExternal(WOLFSSL_EC_KEY* eckey)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
ecc_key* key;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SetECKeyExternal");
2016-06-27 10:53:34 -07:00
if (eckey == NULL || eckey->internal == NULL) {
WOLFSSL_MSG("ec key NULL error");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
key = (ecc_key*)eckey->internal;
2016-06-27 10:53:34 -07:00
/* set group (nid and idx) */
eckey->group->curve_nid = ecc_sets[key->idx].id;
2016-06-27 10:53:34 -07:00
eckey->group->curve_idx = key->idx;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (eckey->pub_key->internal != NULL) {
/* set the internal public key */
if (wc_ecc_copy_point(&key->pubkey,
(ecc_point*)eckey->pub_key->internal) != MP_OKAY) {
WOLFSSL_MSG("SetECKeyExternal ecc_copy_point failed");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
/* set the external pubkey (point) */
if (SetECPointExternal(eckey->pub_key) != SSL_SUCCESS) {
WOLFSSL_MSG("SetECKeyExternal SetECPointExternal failed");
return SSL_FATAL_ERROR;
}
}
2016-06-27 10:53:34 -07:00
/* set the external privkey */
if (key->type == ECC_PRIVATEKEY) {
if (SetIndividualExternal(&eckey->priv_key, &key->k) != SSL_SUCCESS) {
WOLFSSL_MSG("ec priv key error");
return SSL_FATAL_ERROR;
}
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
eckey->exSet = 1;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* EC_KEY Openssl -> WolfSSL */
static int SetECKeyInternal(WOLFSSL_EC_KEY* eckey)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
ecc_key* key;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("SetECKeyInternal");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (eckey == NULL || eckey->internal == NULL) {
WOLFSSL_MSG("ec key NULL error");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
key = (ecc_key*)eckey->internal;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* validate group */
if ((eckey->group->curve_idx < 0) ||
(wc_ecc_is_valid_idx(eckey->group->curve_idx) == 0)) {
WOLFSSL_MSG("invalid curve idx");
return SSL_FATAL_ERROR;
}
/* set group (idx of curve and corresponding domain parameters) */
key->idx = eckey->group->curve_idx;
key->dp = &ecc_sets[key->idx];
/* set pubkey (point) */
if (eckey->pub_key != NULL) {
if (SetECPointInternal(eckey->pub_key) != SSL_SUCCESS) {
WOLFSSL_MSG("ec key pub error");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* public key */
key->type = ECC_PUBLICKEY;
}
2016-06-27 10:53:34 -07:00
/* set privkey */
if (eckey->priv_key != NULL) {
if (SetIndividualInternal(eckey->priv_key, &key->k) != SSL_SUCCESS) {
WOLFSSL_MSG("ec key priv error");
return SSL_FATAL_ERROR;
}
/* private key */
key->type = ECC_PRIVATEKEY;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
eckey->inSet = 1;
return SSL_SUCCESS;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
WOLFSSL_EC_POINT *wolfSSL_EC_KEY_get0_public_key(const WOLFSSL_EC_KEY *key)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_KEY_get0_public_key");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (key == NULL) {
WOLFSSL_MSG("wolfSSL_EC_KEY_get0_group Bad arguments");
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return key->pub_key;
}
2016-06-27 10:53:34 -07:00
const WOLFSSL_EC_GROUP *wolfSSL_EC_KEY_get0_group(const WOLFSSL_EC_KEY *key)
{
WOLFSSL_ENTER("wolfSSL_EC_KEY_get0_group");
if (key == NULL) {
WOLFSSL_MSG("wolfSSL_EC_KEY_get0_group Bad arguments");
return NULL;
}
2016-06-27 10:53:34 -07:00
return key->group;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
2016-06-27 10:53:34 -07:00
int wolfSSL_EC_KEY_set_private_key(WOLFSSL_EC_KEY *key,
const WOLFSSL_BIGNUM *priv_key)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_KEY_set_private_key");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (key == NULL || priv_key == NULL) {
WOLFSSL_MSG("Bad arguments");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* free key if previously set */
if (key->priv_key != NULL)
wolfSSL_BN_free(key->priv_key);
key->priv_key = wolfSSL_BN_dup(priv_key);
if (key->priv_key == NULL) {
WOLFSSL_MSG("key ecc priv key NULL");
return SSL_FAILURE;
}
if (SetECKeyInternal(key) != SSL_SUCCESS) {
WOLFSSL_MSG("SetECKeyInternal failed");
wolfSSL_BN_free(key->priv_key);
return SSL_FAILURE;
}
return SSL_SUCCESS;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_BIGNUM *wolfSSL_EC_KEY_get0_private_key(const WOLFSSL_EC_KEY *key)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_KEY_get0_private_key");
2016-06-27 10:53:34 -07:00
if (key == NULL) {
WOLFSSL_MSG("wolfSSL_EC_KEY_get0_private_key Bad arguments");
return NULL;
}
2016-06-27 10:53:34 -07:00
return key->priv_key;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
WOLFSSL_EC_KEY *wolfSSL_EC_KEY_new_by_curve_name(int nid)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_EC_KEY *key;
int x;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_KEY_new_by_curve_name");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
key = wolfSSL_EC_KEY_new();
if (key == NULL) {
WOLFSSL_MSG("wolfSSL_EC_KEY_new failure");
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* set the nid of the curve */
key->group->curve_nid = nid;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* search and set the corresponding internal curve idx */
for (x = 0; ecc_sets[x].size != 0; x++)
if (ecc_sets[x].id == key->group->curve_nid) {
2016-06-27 10:53:34 -07:00
key->group->curve_idx = x;
break;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return key;
}
2016-06-27 10:53:34 -07:00
static void InitwolfSSL_ECKey(WOLFSSL_EC_KEY* key)
{
if (key) {
key->group = NULL;
key->pub_key = NULL;
key->priv_key = NULL;
key->internal = NULL;
key->inSet = 0;
key->exSet = 0;
}
2016-06-27 10:53:34 -07:00
}
2016-06-27 10:53:34 -07:00
WOLFSSL_EC_KEY *wolfSSL_EC_KEY_new(void)
{
WOLFSSL_EC_KEY *external;
ecc_key* key;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_KEY_new");
2016-06-27 10:53:34 -07:00
external = (WOLFSSL_EC_KEY*)XMALLOC(sizeof(WOLFSSL_EC_KEY), NULL,
DYNAMIC_TYPE_ECC);
if (external == NULL) {
WOLFSSL_MSG("wolfSSL_EC_KEY_new malloc WOLFSSL_EC_KEY failure");
return NULL;
}
XMEMSET(external, 0, sizeof(WOLFSSL_EC_KEY));
2016-06-27 10:53:34 -07:00
InitwolfSSL_ECKey(external);
external->internal = (ecc_key*)XMALLOC(sizeof(ecc_key), NULL,
DYNAMIC_TYPE_ECC);
if (external->internal == NULL) {
WOLFSSL_MSG("wolfSSL_EC_KEY_new malloc ecc key failure");
wolfSSL_EC_KEY_free(external);
return NULL;
}
2016-06-27 10:53:34 -07:00
XMEMSET(external->internal, 0, sizeof(ecc_key));
2016-06-27 10:53:34 -07:00
wc_ecc_init((ecc_key*)external->internal);
/* public key */
external->pub_key = (WOLFSSL_EC_POINT*)XMALLOC(sizeof(WOLFSSL_EC_POINT),
NULL, DYNAMIC_TYPE_ECC);
if (external->pub_key == NULL) {
WOLFSSL_MSG("wolfSSL_EC_KEY_new malloc WOLFSSL_EC_POINT failure");
wolfSSL_EC_KEY_free(external);
return NULL;
}
2016-06-27 10:53:34 -07:00
XMEMSET(external->pub_key, 0, sizeof(WOLFSSL_EC_POINT));
2016-06-27 10:53:34 -07:00
key = (ecc_key*)external->internal;
external->pub_key->internal = (ecc_point*)&key->pubkey;
/* curve group */
external->group = (WOLFSSL_EC_GROUP*)XMALLOC(sizeof(WOLFSSL_EC_GROUP), NULL,
DYNAMIC_TYPE_ECC);
if (external->group == NULL) {
WOLFSSL_MSG("wolfSSL_EC_KEY_new malloc WOLFSSL_EC_GROUP failure");
wolfSSL_EC_KEY_free(external);
return NULL;
}
2016-06-27 10:53:34 -07:00
XMEMSET(external->group, 0, sizeof(WOLFSSL_EC_GROUP));
2016-06-27 10:53:34 -07:00
/* private key */
external->priv_key = wolfSSL_BN_new();
if (external->priv_key == NULL) {
WOLFSSL_MSG("wolfSSL_BN_new failure");
wolfSSL_EC_KEY_free(external);
return NULL;
}
2016-06-27 10:53:34 -07:00
return external;
}
void wolfSSL_EC_KEY_free(WOLFSSL_EC_KEY *key)
{
WOLFSSL_ENTER("wolfSSL_EC_KEY_free");
if (key != NULL) {
if (key->internal != NULL) {
wc_ecc_free((ecc_key*)key->internal);
XFREE(key->internal, NULL, DYNAMIC_TYPE_ECC);
}
wolfSSL_BN_free(key->priv_key);
wolfSSL_EC_POINT_free(key->pub_key);
wolfSSL_EC_GROUP_free(key->group);
InitwolfSSL_ECKey(key); /* set back to NULLs for safety */
XFREE(key, NULL, DYNAMIC_TYPE_ECC);
key = NULL;
}
2016-06-27 10:53:34 -07:00
}
int wolfSSL_EC_KEY_set_group(WOLFSSL_EC_KEY *key, WOLFSSL_EC_GROUP *group)
{
(void)key;
(void)group;
WOLFSSL_ENTER("wolfSSL_EC_KEY_set_group");
WOLFSSL_MSG("wolfSSL_EC_KEY_set_group TBD");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return -1;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
int wolfSSL_EC_KEY_generate_key(WOLFSSL_EC_KEY *key)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
int initTmpRng = 0;
WC_RNG* rng = NULL;
#ifdef WOLFSSL_SMALL_STACK
WC_RNG* tmpRNG = NULL;
#else
WC_RNG tmpRNG[1];
#endif
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_KEY_generate_key");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (key == NULL || key->internal == NULL ||
key->group == NULL || key->group->curve_idx < 0) {
WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key Bad arguments");
return 0;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmpRNG == NULL)
return 0;
#endif
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (wc_InitRng(tmpRNG) == 0) {
rng = tmpRNG;
initTmpRng = 1;
}
else {
WOLFSSL_MSG("Bad RNG Init, trying global");
if (initGlobalRNG == 0)
WOLFSSL_MSG("Global RNG no Init");
else
rng = &globalRNG;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (rng == NULL) {
WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key failed to set RNG");
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return 0;
}
2015-07-01 12:24:11 -07:00
if (wc_ecc_make_key_ex(rng, 0, (ecc_key*)key->internal,
key->group->curve_nid) != MP_OKAY) {
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key wc_ecc_make_key failed");
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return 0;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (initTmpRng)
wc_FreeRng(tmpRNG);
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (SetECKeyExternal(key) != SSL_SUCCESS) {
WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key SetECKeyExternal failed");
return 0;
}
2016-06-27 10:53:34 -07:00
return 1;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_EC_KEY_set_asn1_flag(WOLFSSL_EC_KEY *key, int asn1_flag)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
(void)key;
(void)asn1_flag;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_KEY_set_asn1_flag");
WOLFSSL_MSG("wolfSSL_EC_KEY_set_asn1_flag TBD");
2015-07-01 12:24:11 -07:00
}
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
2016-06-27 10:53:34 -07:00
int wolfSSL_EC_KEY_set_public_key(WOLFSSL_EC_KEY *key,
const WOLFSSL_EC_POINT *pub)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
ecc_point *pub_p, *key_p;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_KEY_set_public_key");
2015-11-02 15:51:01 -03:00
2016-06-27 10:53:34 -07:00
if (key == NULL || key->internal == NULL ||
pub == NULL || pub->internal == NULL) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order Bad arguments");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (key->inSet == 0) {
if (SetECKeyInternal(key) != SSL_SUCCESS) {
WOLFSSL_MSG("SetECKeyInternal failed");
return SSL_FAILURE;
}
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (pub->inSet == 0) {
if (SetECPointInternal((WOLFSSL_EC_POINT *)pub) != SSL_SUCCESS) {
WOLFSSL_MSG("SetECPointInternal failed");
return SSL_FAILURE;
}
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
pub_p = (ecc_point*)pub->internal;
key_p = (ecc_point*)key->pub_key->internal;
2016-06-27 10:53:34 -07:00
/* create new point if required */
if (key_p == NULL)
key_p = wc_ecc_new_point();
if (key_p == NULL) {
WOLFSSL_MSG("key ecc point NULL");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
if (wc_ecc_copy_point(pub_p, key_p) != MP_OKAY) {
WOLFSSL_MSG("ecc_copy_point failure");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
if (SetECKeyExternal(key) != SSL_SUCCESS) {
WOLFSSL_MSG("SetECKeyInternal failed");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
#if defined(DEBUG_WOLFSSL) && !defined(NO_FILESYSTEM)
wolfssl_EC_POINT_dump("pub", pub);
wolfssl_EC_POINT_dump("key->pub_key", key->pub_key);
#endif
return SSL_SUCCESS;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
/* End EC_KEY */
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
#if defined(DEBUG_WOLFSSL) && !defined(NO_FILESYSTEM)
void wolfssl_EC_POINT_dump(const char *msg, const WOLFSSL_EC_POINT *p)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
char *num;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfssl_EC_POINT_dump");
if (p == NULL) {
fprintf(stderr, "%s = NULL", msg);
return ;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
fprintf(stderr, "%s:\n\tinSet=%d, exSet=%d\n", msg, p->inSet, p->exSet);
num = wolfSSL_BN_bn2hex(p->X);
fprintf(stderr, "\tX = %s\n", num);
XFREE(num, NULL, DYNAMIC_TYPE_ECC);
num = wolfSSL_BN_bn2hex(p->Y);
fprintf(stderr, "\tY = %s\n", num);
XFREE(num, NULL, DYNAMIC_TYPE_ECC);
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
#endif
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* Start EC_GROUP */
2015-07-01 12:24:11 -07:00
/* return code compliant with OpenSSL :
2016-06-27 10:53:34 -07:00
* 0 if equal, 1 if not and -1 in case of error
*/
2016-06-27 10:53:34 -07:00
int wolfSSL_EC_GROUP_cmp(const WOLFSSL_EC_GROUP *a, const WOLFSSL_EC_GROUP *b,
WOLFSSL_BN_CTX *ctx)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
(void)ctx;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_GROUP_cmp");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (a == NULL || b == NULL) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_cmp Bad arguments");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* ok */
if ((a->curve_idx == b->curve_idx) && (a->curve_nid == b->curve_nid))
return 0;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* ko */
return 1;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
void wolfSSL_EC_GROUP_free(WOLFSSL_EC_GROUP *group)
{
WOLFSSL_ENTER("wolfSSL_EC_GROUP_free");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
XFREE(group, NULL, DYNAMIC_TYPE_ECC);
group = NULL;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_EC_GROUP_set_asn1_flag(WOLFSSL_EC_GROUP *group, int flag)
{
(void)group;
(void)flag;
WOLFSSL_ENTER("wolfSSL_EC_GROUP_set_asn1_flag");
WOLFSSL_MSG("wolfSSL_EC_GROUP_set_asn1_flag TBD");
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_EC_GROUP *wolfSSL_EC_GROUP_new_by_curve_name(int nid)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_EC_GROUP *g;
int x;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_GROUP_new_by_curve_name");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* curve group */
g = (WOLFSSL_EC_GROUP*) XMALLOC(sizeof(WOLFSSL_EC_GROUP), NULL,
DYNAMIC_TYPE_ECC);
if (g == NULL) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_new_by_curve_name malloc failure");
return NULL;
}
XMEMSET(g, 0, sizeof(WOLFSSL_EC_GROUP));
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* set the nid of the curve */
g->curve_nid = nid;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* search and set the corresponding internal curve idx */
for (x = 0; ecc_sets[x].size != 0; x++)
if (ecc_sets[x].id == g->curve_nid) {
2016-06-27 10:53:34 -07:00
g->curve_idx = x;
break;
}
return g;
}
/* return code compliant with OpenSSL :
* the curve nid if success, 0 if error
*/
int wolfSSL_EC_GROUP_get_curve_name(const WOLFSSL_EC_GROUP *group)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_GROUP_get_curve_name");
2016-06-27 10:53:34 -07:00
if (group == NULL) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_get_curve_name Bad arguments");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
return group->curve_nid;
}
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* the degree of the curve if success, 0 if error
*/
int wolfSSL_EC_GROUP_get_degree(const WOLFSSL_EC_GROUP *group)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_GROUP_get_degree");
2016-06-27 10:53:34 -07:00
if (group == NULL || group->curve_idx < 0) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_get_degree Bad arguments");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
switch(group->curve_nid) {
case NID_secp112r1:
case NID_secp112r2:
2016-06-27 10:53:34 -07:00
return 112;
case NID_secp128r1:
case NID_secp128r2:
2016-06-27 10:53:34 -07:00
return 128;
case NID_secp160k1:
2016-06-27 10:53:34 -07:00
case NID_secp160r1:
case NID_secp160r2:
case NID_brainpoolP160r1:
2016-06-27 10:53:34 -07:00
return 160;
case NID_secp192k1:
case NID_brainpoolP192r1:
2016-06-27 10:53:34 -07:00
case NID_X9_62_prime192v1:
return 192;
case NID_secp224k1:
2016-06-27 10:53:34 -07:00
case NID_secp224r1:
case NID_brainpoolP224r1:
2016-06-27 10:53:34 -07:00
return 224;
case NID_secp256k1:
case NID_brainpoolP256r1:
2016-06-27 10:53:34 -07:00
case NID_X9_62_prime256v1:
return 256;
case NID_brainpoolP320r1:
return 320;
2016-06-27 10:53:34 -07:00
case NID_secp384r1:
case NID_brainpoolP384r1:
2016-06-27 10:53:34 -07:00
return 384;
case NID_secp521r1:
case NID_brainpoolP512r1:
2016-06-27 10:53:34 -07:00
return 521;
default:
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
}
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
2016-06-27 10:53:34 -07:00
int wolfSSL_EC_GROUP_get_order(const WOLFSSL_EC_GROUP *group,
WOLFSSL_BIGNUM *order, WOLFSSL_BN_CTX *ctx)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
(void)ctx;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (group == NULL || order == NULL || order->internal == NULL) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order NULL error");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (mp_init((mp_int*)order->internal) != MP_OKAY) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order mp_init failure");
return SSL_FAILURE;
}
if (mp_read_radix((mp_int*)order->internal,
ecc_sets[group->curve_idx].order, 16) != MP_OKAY) {
WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order mp_read order failure");
mp_clear((mp_int*)order->internal);
return SSL_FAILURE;
}
return SSL_SUCCESS;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
/* End EC_GROUP */
/* Start EC_POINT */
2015-07-01 12:24:11 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
2016-06-27 10:53:34 -07:00
int wolfSSL_ECPoint_i2d(const WOLFSSL_EC_GROUP *group,
const WOLFSSL_EC_POINT *p,
unsigned char *out, unsigned int *len)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
int err;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_ECPoint_i2d");
2016-06-27 10:53:34 -07:00
if (group == NULL || p == NULL || len == NULL) {
WOLFSSL_MSG("wolfSSL_ECPoint_i2d NULL error");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (p->inSet == 0) {
WOLFSSL_MSG("No ECPoint internal set, do it");
2016-06-27 10:53:34 -07:00
if (SetECPointInternal((WOLFSSL_EC_POINT *)p) != SSL_SUCCESS) {
WOLFSSL_MSG("SetECPointInternal SetECPointInternal failed");
return SSL_FAILURE;
}
}
2016-06-27 10:53:34 -07:00
#if defined(DEBUG_WOLFSSL) && !defined(NO_FILESYSTEM)
if (out != NULL) {
wolfssl_EC_POINT_dump("i2d p", p);
}
#endif
err = wc_ecc_export_point_der(group->curve_idx, (ecc_point*)p->internal,
out, len);
if (err != MP_OKAY && !(out == NULL && err == LENGTH_ONLY_E)) {
WOLFSSL_MSG("wolfSSL_ECPoint_i2d wc_ecc_export_point_der failed");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
int wolfSSL_ECPoint_d2i(unsigned char *in, unsigned int len,
const WOLFSSL_EC_GROUP *group, WOLFSSL_EC_POINT *p)
{
WOLFSSL_ENTER("wolfSSL_ECPoint_d2i");
if (group == NULL || p == NULL || p->internal == NULL || in == NULL) {
WOLFSSL_MSG("wolfSSL_ECPoint_d2i NULL error");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (wc_ecc_import_point_der(in, len, group->curve_idx,
(ecc_point*)p->internal) != MP_OKAY) {
WOLFSSL_MSG("wc_ecc_import_point_der failed");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (p->exSet == 0) {
WOLFSSL_MSG("No ECPoint external set, do it");
if (SetECPointExternal(p) != SSL_SUCCESS) {
WOLFSSL_MSG("SetECPointExternal failed");
return SSL_FAILURE;
}
}
2016-06-27 10:53:34 -07:00
#if defined(DEBUG_WOLFSSL) && !defined(NO_FILESYSTEM)
wolfssl_EC_POINT_dump("d2i p", p);
#endif
return SSL_SUCCESS;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
WOLFSSL_EC_POINT *wolfSSL_EC_POINT_new(const WOLFSSL_EC_GROUP *group)
2015-07-01 12:24:11 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_EC_POINT *p;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_POINT_new");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (group == NULL) {
WOLFSSL_MSG("wolfSSL_EC_POINT_new NULL error");
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
p = (WOLFSSL_EC_POINT *)XMALLOC(sizeof(WOLFSSL_EC_POINT), NULL,
DYNAMIC_TYPE_ECC);
if (p == NULL) {
WOLFSSL_MSG("wolfSSL_EC_POINT_new malloc ecc point failure");
return NULL;
}
2016-06-27 10:53:34 -07:00
XMEMSET(p, 0, sizeof(WOLFSSL_EC_POINT));
2016-06-27 10:53:34 -07:00
p->internal = wc_ecc_new_point();
if (p->internal == NULL) {
WOLFSSL_MSG("ecc_new_point failure");
XFREE(p, NULL, DYNAMIC_TYPE_ECC);
return NULL;
}
2016-06-27 10:53:34 -07:00
return p;
}
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
int wolfSSL_EC_POINT_get_affine_coordinates_GFp(const WOLFSSL_EC_GROUP *group,
const WOLFSSL_EC_POINT *point,
WOLFSSL_BIGNUM *x,
WOLFSSL_BIGNUM *y,
WOLFSSL_BN_CTX *ctx)
{
2016-06-27 10:53:34 -07:00
(void)ctx;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_POINT_get_affine_coordinates_GFp");
2016-06-27 10:53:34 -07:00
if (group == NULL || point == NULL || point->internal == NULL ||
x == NULL || y == NULL) {
WOLFSSL_MSG("wolfSSL_EC_POINT_get_affine_coordinates_GFp NULL error");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
if (point->inSet == 0) {
WOLFSSL_MSG("No ECPoint internal set, do it");
2016-06-27 10:53:34 -07:00
if (SetECPointInternal((WOLFSSL_EC_POINT *)point) != SSL_SUCCESS) {
WOLFSSL_MSG("SetECPointInternal failed");
return SSL_FAILURE;
}
}
2016-06-27 10:53:34 -07:00
BN_copy(x, point->X);
BN_copy(y, point->Y);
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
int wolfSSL_EC_POINT_mul(const WOLFSSL_EC_GROUP *group, WOLFSSL_EC_POINT *r,
const WOLFSSL_BIGNUM *n, const WOLFSSL_EC_POINT *q,
const WOLFSSL_BIGNUM *m, WOLFSSL_BN_CTX *ctx)
{
mp_int a, prime;
(void)ctx;
(void)n;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_POINT_mul");
2016-06-27 10:53:34 -07:00
if (group == NULL || r == NULL || r->internal == NULL ||
q == NULL || q->internal == NULL || m == NULL) {
WOLFSSL_MSG("wolfSSL_EC_POINT_mul NULL error");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
if (q->inSet == 0) {
WOLFSSL_MSG("No ECPoint internal set, do it");
2016-06-27 10:53:34 -07:00
if (SetECPointInternal((WOLFSSL_EC_POINT *)q) != SSL_SUCCESS) {
WOLFSSL_MSG("SetECPointInternal failed");
return SSL_FAILURE;
}
}
2016-06-27 10:53:34 -07:00
/* read the curve prime and a */
if (mp_init_multi(&prime, &a, NULL, NULL, NULL, NULL) != MP_OKAY) {
WOLFSSL_MSG("wolfSSL_EC_POINT_mul init 'prime/A' failed");
return SSL_FAILURE;
}
if (mp_read_radix(&prime, ecc_sets[group->curve_idx].prime, 16) != MP_OKAY){
WOLFSSL_MSG("wolfSSL_EC_POINT_mul read 'prime' curve value failed");
return SSL_FAILURE;
}
if (mp_read_radix(&a, ecc_sets[group->curve_idx].Af, 16) != MP_OKAY){
WOLFSSL_MSG("wolfSSL_EC_POINT_mul read 'A' curve value failed");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
/* r = q * m % prime */
if (wc_ecc_mulmod((mp_int*)m->internal, (ecc_point*)q->internal,
(ecc_point*)r->internal, &a, &prime, 1) != MP_OKAY) {
WOLFSSL_MSG("ecc_mulmod failure");
mp_clear(&prime);
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
mp_clear(&a);
mp_clear(&prime);
2016-06-27 10:53:34 -07:00
/* set the external value for the computed point */
if (SetECPointInternal(r) != SSL_SUCCESS) {
WOLFSSL_MSG("SetECPointInternal failed");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_EC_POINT_clear_free(WOLFSSL_EC_POINT *p)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_POINT_clear_free");
2016-06-27 10:53:34 -07:00
wolfSSL_EC_POINT_free(p);
}
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 0 if equal, 1 if not and -1 in case of error
*/
int wolfSSL_EC_POINT_cmp(const WOLFSSL_EC_GROUP *group,
const WOLFSSL_EC_POINT *a, const WOLFSSL_EC_POINT *b,
WOLFSSL_BN_CTX *ctx)
{
2016-06-27 10:53:34 -07:00
int ret;
2016-06-27 10:53:34 -07:00
(void)ctx;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_POINT_cmp");
2016-06-27 10:53:34 -07:00
if (group == NULL || a == NULL || a->internal == NULL || b == NULL ||
b->internal == NULL) {
WOLFSSL_MSG("wolfSSL_EC_POINT_cmp Bad arguments");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
ret = wc_ecc_cmp_point((ecc_point*)a->internal, (ecc_point*)b->internal);
if (ret == MP_EQ)
return 0;
else if (ret == MP_LT || ret == MP_GT)
return 1;
2016-06-27 10:53:34 -07:00
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_EC_POINT_free(WOLFSSL_EC_POINT *p)
{
WOLFSSL_ENTER("wolfSSL_EC_POINT_free");
2016-06-27 10:53:34 -07:00
if (p != NULL) {
if (p->internal == NULL) {
wc_ecc_del_point((ecc_point*)p->internal);
XFREE(p->internal, NULL, DYNAMIC_TYPE_ECC);
p->internal = NULL;
}
2015-11-02 15:51:01 -03:00
2016-06-27 10:53:34 -07:00
wolfSSL_BN_free(p->X);
wolfSSL_BN_free(p->Y);
wolfSSL_BN_free(p->Z);
p->X = NULL;
p->Y = NULL;
p->Z = NULL;
p->inSet = p->exSet = 0;
XFREE(p, NULL, DYNAMIC_TYPE_ECC);
p = NULL;
}
}
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if point at infinity, 0 else
*/
int wolfSSL_EC_POINT_is_at_infinity(const WOLFSSL_EC_GROUP *group,
const WOLFSSL_EC_POINT *point)
{
2016-06-27 10:53:34 -07:00
int ret;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_POINT_is_at_infinity");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (group == NULL || point == NULL || point->internal == NULL) {
WOLFSSL_MSG("wolfSSL_EC_POINT_is_at_infinity NULL error");
return SSL_FAILURE;
}
if (point->inSet == 0) {
WOLFSSL_MSG("No ECPoint internal set, do it");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (SetECPointInternal((WOLFSSL_EC_POINT *)point) != SSL_SUCCESS) {
WOLFSSL_MSG("SetECPointInternal failed");
return SSL_FAILURE;
2016-06-27 10:53:34 -07:00
}
}
2016-06-27 10:53:34 -07:00
ret = wc_ecc_point_is_at_infinity((ecc_point*)point->internal);
if (ret <= 0) {
WOLFSSL_MSG("ecc_point_is_at_infinity failure");
return SSL_FAILURE;
}
2012-05-17 17:44:54 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
/* End EC_POINT */
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
/* Start ECDSA_SIG */
void wolfSSL_ECDSA_SIG_free(WOLFSSL_ECDSA_SIG *sig)
{
WOLFSSL_ENTER("wolfSSL_ECDSA_SIG_free");
2016-06-27 10:53:34 -07:00
if (sig) {
wolfSSL_BN_free(sig->r);
wolfSSL_BN_free(sig->s);
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
XFREE(sig, NULL, DYNAMIC_TYPE_ECC);
}
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ECDSA_SIG *wolfSSL_ECDSA_SIG_new(void)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ECDSA_SIG *sig;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_ECDSA_SIG_new");
2016-06-27 10:53:34 -07:00
sig = (WOLFSSL_ECDSA_SIG*) XMALLOC(sizeof(WOLFSSL_ECDSA_SIG), NULL,
DYNAMIC_TYPE_ECC);
if (sig == NULL) {
WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new malloc ECDSA signature failure");
return NULL;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
sig->s = NULL;
sig->r = wolfSSL_BN_new();
if (sig->r == NULL) {
WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new malloc ECDSA r failure");
wolfSSL_ECDSA_SIG_free(sig);
return NULL;
}
2016-06-27 10:53:34 -07:00
sig->s = wolfSSL_BN_new();
if (sig->s == NULL) {
WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new malloc ECDSA s failure");
wolfSSL_ECDSA_SIG_free(sig);
return NULL;
}
2016-06-27 10:53:34 -07:00
return sig;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* return signature structure on success, NULL otherwise */
WOLFSSL_ECDSA_SIG *wolfSSL_ECDSA_do_sign(const unsigned char *d, int dlen,
WOLFSSL_EC_KEY *key)
{
WOLFSSL_ECDSA_SIG *sig = NULL;
int initTmpRng = 0;
WC_RNG* rng = NULL;
#ifdef WOLFSSL_SMALL_STACK
WC_RNG* tmpRNG = NULL;
#else
WC_RNG tmpRNG[1];
#endif
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_ECDSA_do_sign");
2016-06-27 10:53:34 -07:00
if (d == NULL || key == NULL || key->internal == NULL) {
WOLFSSL_MSG("wolfSSL_ECDSA_do_sign Bad arguments");
return NULL;
}
2016-06-27 10:53:34 -07:00
/* set internal key if not done */
if (key->inSet == 0)
{
WOLFSSL_MSG("wolfSSL_ECDSA_do_sign No EC key internal set, do it");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (SetECKeyInternal(key) != SSL_SUCCESS) {
WOLFSSL_MSG("wolfSSL_ECDSA_do_sign SetECKeyInternal failed");
return NULL;
}
}
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmpRNG == NULL)
return NULL;
#endif
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (wc_InitRng(tmpRNG) == 0) {
rng = tmpRNG;
initTmpRng = 1;
}
else {
WOLFSSL_MSG("wolfSSL_ECDSA_do_sign Bad RNG Init, trying global");
if (initGlobalRNG == 0)
WOLFSSL_MSG("wolfSSL_ECDSA_do_sign Global RNG no Init");
else
rng = &globalRNG;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (rng) {
mp_int sig_r, sig_s;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (mp_init_multi(&sig_r, &sig_s, NULL, NULL, NULL, NULL) == MP_OKAY) {
if (wc_ecc_sign_hash_ex(d, dlen, rng, (ecc_key*)key->internal,
&sig_r, &sig_s) != MP_OKAY) {
WOLFSSL_MSG("wc_ecc_sign_hash_ex failed");
}
else {
/* put signature blob in ECDSA structure */
sig = wolfSSL_ECDSA_SIG_new();
if (sig == NULL)
WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new failed");
else if (SetIndividualExternal(&(sig->r), &sig_r)!=SSL_SUCCESS){
WOLFSSL_MSG("ecdsa r key error");
wolfSSL_ECDSA_SIG_free(sig);
sig = NULL;
}
else if (SetIndividualExternal(&(sig->s), &sig_s)!=SSL_SUCCESS){
WOLFSSL_MSG("ecdsa s key error");
wolfSSL_ECDSA_SIG_free(sig);
sig = NULL;
}
2016-06-27 10:53:34 -07:00
}
mp_clear(&sig_r);
mp_clear(&sig_s);
2016-06-27 10:53:34 -07:00
}
}
2016-06-27 10:53:34 -07:00
if (initTmpRng)
wc_FreeRng(tmpRNG);
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2016-06-27 10:53:34 -07:00
return sig;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 for a valid signature, 0 for an invalid signature and -1 on error
*/
int wolfSSL_ECDSA_do_verify(const unsigned char *d, int dlen,
const WOLFSSL_ECDSA_SIG *sig, WOLFSSL_EC_KEY *key)
{
2016-06-27 10:53:34 -07:00
int check_sign = 0;
2015-11-02 15:51:01 -03:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_ECDSA_do_verify");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (d == NULL || sig == NULL || key == NULL || key->internal == NULL) {
WOLFSSL_MSG("wolfSSL_ECDSA_do_verify Bad arguments");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
/* set internal key if not done */
if (key->inSet == 0)
{
WOLFSSL_MSG("No EC key internal set, do it");
2016-06-27 10:53:34 -07:00
if (SetECKeyInternal(key) != SSL_SUCCESS) {
WOLFSSL_MSG("SetECKeyInternal failed");
return SSL_FATAL_ERROR;
}
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (wc_ecc_verify_hash_ex((mp_int*)sig->r->internal,
(mp_int*)sig->s->internal, d, dlen, &check_sign,
(ecc_key *)key->internal) != MP_OKAY) {
WOLFSSL_MSG("wc_ecc_verify_hash failed");
return SSL_FATAL_ERROR;
}
else if (check_sign == 0) {
WOLFSSL_MSG("wc_ecc_verify_hash incorrect signature detected");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
/* End ECDSA_SIG */
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* Start ECDH */
/* return code compliant with OpenSSL :
* length of computed key if success, -1 if error
*/
int wolfSSL_ECDH_compute_key(void *out, size_t outlen,
const WOLFSSL_EC_POINT *pub_key,
WOLFSSL_EC_KEY *ecdh,
void *(*KDF) (const void *in, size_t inlen,
void *out, size_t *outlen))
{
2016-06-27 10:53:34 -07:00
word32 len;
(void)KDF;
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
(void)KDF;
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_ECDH_compute_key");
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (out == NULL || pub_key == NULL || pub_key->internal == NULL ||
ecdh == NULL || ecdh->internal == NULL) {
WOLFSSL_MSG("Bad function arguments");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* set internal key if not done */
if (ecdh->inSet == 0)
{
WOLFSSL_MSG("No EC key internal set, do it");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (SetECKeyInternal(ecdh) != SSL_SUCCESS) {
WOLFSSL_MSG("SetECKeyInternal failed");
return SSL_FATAL_ERROR;
}
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
len = (word32)outlen;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (wc_ecc_shared_secret_ssh((ecc_key*)ecdh->internal,
(ecc_point*)pub_key->internal,
(byte *)out, &len) != MP_OKAY) {
WOLFSSL_MSG("wc_ecc_shared_secret failed");
return SSL_FATAL_ERROR;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return len;
2015-07-01 12:24:11 -07:00
}
2016-06-27 10:53:34 -07:00
/* End ECDH */
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
#if !defined(NO_FILESYSTEM)
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
int wolfSSL_PEM_write_EC_PUBKEY(FILE *fp, WOLFSSL_EC_KEY *x)
{
2016-06-27 10:53:34 -07:00
(void)fp;
(void)x;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_PEM_write_EC_PUBKEY not implemented");
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
#endif /* NO_FILESYSTEM */
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
#if defined(WOLFSSL_KEY_GEN)
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
int wolfSSL_PEM_write_bio_ECPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_EC_KEY* ecc,
const EVP_CIPHER* cipher,
unsigned char* passwd, int len,
pem_password_cb cb, void* arg)
{
2016-06-27 10:53:34 -07:00
(void)bio;
(void)ecc;
(void)cipher;
(void)passwd;
(void)len;
(void)cb;
(void)arg;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_PEM_write_bio_ECPrivateKey not implemented");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
int wolfSSL_PEM_write_mem_ECPrivateKey(WOLFSSL_EC_KEY* ecc,
const EVP_CIPHER* cipher,
unsigned char* passwd, int passwdSz,
unsigned char **pem, int *plen)
{
2016-06-27 10:53:34 -07:00
byte *derBuf, *tmp, *cipherInfo = NULL;
int der_max_len = 0, derSz = 0;
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_PEM_write_mem_ECPrivateKey");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (pem == NULL || plen == NULL || ecc == NULL || ecc->internal == NULL) {
WOLFSSL_MSG("Bad function arguments");
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (ecc->inSet == 0) {
WOLFSSL_MSG("No ECC internal set, do it");
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (SetECKeyInternal(ecc) != SSL_SUCCESS) {
WOLFSSL_MSG("SetDsaInternal failed");
return SSL_FAILURE;
}
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
/* 4 > size of pub, priv + ASN.1 additional informations
*/
der_max_len = 4 * wc_ecc_size((ecc_key*)ecc->internal) + AES_BLOCK_SIZE;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
derBuf = (byte*)XMALLOC(der_max_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (derBuf == NULL) {
WOLFSSL_MSG("malloc failed");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
/* Key to DER */
derSz = wc_EccKeyToDer((ecc_key*)ecc->internal, derBuf, der_max_len);
if (derSz < 0) {
WOLFSSL_MSG("wc_DsaKeyToDer failed");
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
/* encrypt DER buffer if required */
if (passwd != NULL && passwdSz > 0 && cipher != NULL) {
int ret;
2016-06-27 10:53:34 -07:00
ret = EncryptDerKey(derBuf, &derSz, cipher,
passwd, passwdSz, &cipherInfo);
if (ret != SSL_SUCCESS) {
WOLFSSL_MSG("EncryptDerKey failed");
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
2016-06-27 10:53:34 -07:00
/* tmp buffer with a max size */
*plen = (derSz * 2) + sizeof(BEGIN_EC_PRIV) +
sizeof(END_EC_PRIV) + HEADER_ENCRYPTED_KEY_SIZE;
}
else /* tmp buffer with a max size */
*plen = (derSz * 2) + sizeof(BEGIN_EC_PRIV) + sizeof(END_EC_PRIV);
2016-06-27 10:53:34 -07:00
tmp = (byte*)XMALLOC(*plen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmp == NULL) {
WOLFSSL_MSG("malloc failed");
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (cipherInfo != NULL)
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
/* DER to PEM */
*plen = wc_DerToPemEx(derBuf, derSz, tmp, *plen, cipherInfo, ECC_PRIVATEKEY_TYPE);
if (*plen <= 0) {
WOLFSSL_MSG("wc_DerToPemEx failed");
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (cipherInfo != NULL)
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (cipherInfo != NULL)
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2016-06-27 10:53:34 -07:00
*pem = (byte*)XMALLOC((*plen)+1, NULL, DYNAMIC_TYPE_KEY);
if (*pem == NULL) {
WOLFSSL_MSG("malloc failed");
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
XMEMSET(*pem, 0, (*plen)+1);
2016-06-27 10:53:34 -07:00
if (XMEMCPY(*pem, tmp, *plen) == NULL) {
WOLFSSL_MSG("XMEMCPY failed");
XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
2012-05-07 16:35:23 -07:00
}
2016-06-27 10:53:34 -07:00
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
#ifndef NO_FILESYSTEM
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
int wolfSSL_PEM_write_ECPrivateKey(FILE *fp, WOLFSSL_EC_KEY *ecc,
const EVP_CIPHER *enc,
unsigned char *kstr, int klen,
pem_password_cb *cb, void *u)
{
byte *pem;
int plen, ret;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
(void)cb;
(void)u;
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_PEM_write_ECPrivateKey");
if (fp == NULL || ecc == NULL || ecc->internal == NULL) {
WOLFSSL_MSG("Bad function arguments");
return SSL_FAILURE;
2012-05-07 16:35:23 -07:00
}
2016-06-27 10:53:34 -07:00
ret = wolfSSL_PEM_write_mem_ECPrivateKey(ecc, enc, kstr, klen, &pem, &plen);
if (ret != SSL_SUCCESS) {
WOLFSSL_MSG("wolfSSL_PEM_write_mem_ECPrivateKey failed");
return SSL_FAILURE;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
ret = (int)XFWRITE(pem, plen, 1, fp);
if (ret != 1) {
WOLFSSL_MSG("ECC private key file write failed");
return SSL_FAILURE;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
return SSL_SUCCESS;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#endif /* NO_FILESYSTEM */
#endif /* defined(WOLFSSL_KEY_GEN) */
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#endif /* HAVE_ECC */
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
#ifndef NO_DSA
2016-06-27 10:53:34 -07:00
#if defined(WOLFSSL_KEY_GEN)
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
int wolfSSL_PEM_write_bio_DSAPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_DSA* dsa,
const EVP_CIPHER* cipher,
unsigned char* passwd, int len,
pem_password_cb cb, void* arg)
{
(void)bio;
(void)dsa;
(void)cipher;
(void)passwd;
(void)len;
(void)cb;
(void)arg;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_PEM_write_bio_DSAPrivateKey not implemented");
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
const EVP_CIPHER* cipher,
unsigned char* passwd, int passwdSz,
unsigned char **pem, int *plen)
{
byte *derBuf, *tmp, *cipherInfo = NULL;
int der_max_len = 0, derSz = 0;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_PEM_write_mem_DSAPrivateKey");
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (pem == NULL || plen == NULL || dsa == NULL || dsa->internal == NULL) {
WOLFSSL_MSG("Bad function arguments");
return SSL_FAILURE;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
if (dsa->inSet == 0) {
WOLFSSL_MSG("No DSA internal set, do it");
2016-06-27 10:53:34 -07:00
if (SetDsaInternal(dsa) != SSL_SUCCESS) {
WOLFSSL_MSG("SetDsaInternal failed");
return SSL_FAILURE;
}
}
2016-06-27 10:53:34 -07:00
/* 4 > size of pub, priv, p, q, g + ASN.1 additional informations
*/
der_max_len = 4 * wolfSSL_BN_num_bytes(dsa->g) + AES_BLOCK_SIZE;
2016-06-27 10:53:34 -07:00
derBuf = (byte*)XMALLOC(der_max_len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (derBuf == NULL) {
WOLFSSL_MSG("malloc failed");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
/* Key to DER */
derSz = wc_DsaKeyToDer((DsaKey*)dsa->internal, derBuf, der_max_len);
if (derSz < 0) {
WOLFSSL_MSG("wc_DsaKeyToDer failed");
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
/* encrypt DER buffer if required */
if (passwd != NULL && passwdSz > 0 && cipher != NULL) {
int ret;
2012-05-07 16:35:23 -07:00
2016-06-27 10:53:34 -07:00
ret = EncryptDerKey(derBuf, &derSz, cipher,
passwd, passwdSz, &cipherInfo);
if (ret != SSL_SUCCESS) {
WOLFSSL_MSG("EncryptDerKey failed");
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return ret;
}
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
/* tmp buffer with a max size */
*plen = (derSz * 2) + sizeof(BEGIN_DSA_PRIV) +
sizeof(END_DSA_PRIV) + HEADER_ENCRYPTED_KEY_SIZE;
}
2016-06-27 10:53:34 -07:00
else /* tmp buffer with a max size */
*plen = (derSz * 2) + sizeof(BEGIN_DSA_PRIV) + sizeof(END_DSA_PRIV);
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
tmp = (byte*)XMALLOC(*plen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmp == NULL) {
WOLFSSL_MSG("malloc failed");
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (cipherInfo != NULL)
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
/* DER to PEM */
*plen = wc_DerToPemEx(derBuf, derSz, tmp, *plen, cipherInfo, DSA_PRIVATEKEY_TYPE);
if (*plen <= 0) {
WOLFSSL_MSG("wc_DerToPemEx failed");
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (cipherInfo != NULL)
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (cipherInfo != NULL)
XFREE(cipherInfo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
*pem = (byte*)XMALLOC((*plen)+1, NULL, DYNAMIC_TYPE_KEY);
if (*pem == NULL) {
WOLFSSL_MSG("malloc failed");
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
XMEMSET(*pem, 0, (*plen)+1);
2015-07-01 12:24:11 -07:00
2016-06-27 10:53:34 -07:00
if (XMEMCPY(*pem, tmp, *plen) == NULL) {
WOLFSSL_MSG("XMEMCPY failed");
XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2012-05-01 16:12:12 -07:00
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#ifndef NO_FILESYSTEM
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
int wolfSSL_PEM_write_DSAPrivateKey(FILE *fp, WOLFSSL_DSA *dsa,
const EVP_CIPHER *enc,
unsigned char *kstr, int klen,
pem_password_cb *cb, void *u)
{
byte *pem;
int plen, ret;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
(void)cb;
(void)u;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_PEM_write_DSAPrivateKey");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
if (fp == NULL || dsa == NULL || dsa->internal == NULL) {
WOLFSSL_MSG("Bad function arguments");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
ret = wolfSSL_PEM_write_mem_DSAPrivateKey(dsa, enc, kstr, klen, &pem, &plen);
if (ret != SSL_SUCCESS) {
WOLFSSL_MSG("wolfSSL_PEM_write_mem_DSAPrivateKey failed");
return SSL_FAILURE;
}
ret = (int)XFWRITE(pem, plen, 1, fp);
if (ret != 1) {
WOLFSSL_MSG("DSA private key file write failed");
return SSL_FAILURE;
}
XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
return SSL_SUCCESS;
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
#endif /* NO_FILESYSTEM */
#endif /* defined(WOLFSSL_KEY_GEN) */
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#ifndef NO_FILESYSTEM
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
int wolfSSL_PEM_write_DSA_PUBKEY(FILE *fp, WOLFSSL_DSA *x)
2011-02-05 11:14:47 -08:00
{
2016-06-27 10:53:34 -07:00
(void)fp;
(void)x;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_PEM_write_DSA_PUBKEY not implemented");
2011-02-05 11:14:47 -08:00
return SSL_FAILURE;
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
#endif /* NO_FILESYSTEM */
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
#endif /* #ifndef NO_DSA */
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_EVP_PKEY* wolfSSL_PEM_read_bio_PrivateKey(WOLFSSL_BIO* bio,
WOLFSSL_EVP_PKEY** key, pem_password_cb cb, void* arg)
2011-02-05 11:14:47 -08:00
{
2016-06-27 10:53:34 -07:00
(void)bio;
(void)key;
(void)cb;
(void)arg;
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_PEM_read_bio_PrivateKey not implemented");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return NULL;
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
int wolfSSL_EVP_PKEY_type(int type)
{
2016-06-27 10:53:34 -07:00
(void)type;
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_EVP_PKEY_type not implemented");
2016-06-27 10:53:34 -07:00
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
#if !defined(NO_FILESYSTEM)
WOLFSSL_EVP_PKEY *wolfSSL_PEM_read_PUBKEY(FILE *fp, EVP_PKEY **x,
pem_password_cb *cb, void *u)
{
(void)fp;
(void)x;
(void)cb;
(void)u;
WOLFSSL_MSG("wolfSSL_PEM_read_PUBKEY not implemented");
return NULL;
}
2016-06-27 10:53:34 -07:00
#endif /* NO_FILESYSTEM */
2016-06-27 10:53:34 -07:00
#ifndef NO_RSA
2016-06-27 10:53:34 -07:00
#if !defined(NO_FILESYSTEM)
WOLFSSL_RSA *wolfSSL_PEM_read_RSAPublicKey(FILE *fp, WOLFSSL_RSA **x,
pem_password_cb *cb, void *u)
2011-02-05 11:14:47 -08:00
{
2016-06-27 10:53:34 -07:00
(void)fp;
(void)x;
(void)cb;
(void)u;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_PEM_read_RSAPublicKey not implemented");
2011-02-05 11:14:47 -08:00
return NULL;
2011-02-05 11:14:47 -08:00
}
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
int wolfSSL_PEM_write_RSAPublicKey(FILE *fp, WOLFSSL_RSA *x)
{
(void)fp;
(void)x;
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_PEM_write_RSAPublicKey not implemented");
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2011-02-05 11:14:47 -08:00
2016-06-27 10:53:34 -07:00
/* return code compliant with OpenSSL :
* 1 if success, 0 if error
*/
int wolfSSL_PEM_write_RSA_PUBKEY(FILE *fp, WOLFSSL_RSA *x)
2014-08-11 16:29:19 -06:00
{
2016-06-27 10:53:34 -07:00
(void)fp;
(void)x;
2016-06-27 10:53:34 -07:00
WOLFSSL_MSG("wolfSSL_PEM_write_RSA_PUBKEY not implemented");
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
#endif /* NO_FILESYSTEM */
2016-06-27 10:53:34 -07:00
/* return SSL_SUCCESS if success, SSL_FATAL_ERROR if error */
int wolfSSL_RSA_LoadDer(WOLFSSL_RSA* rsa, const unsigned char* derBuf, int derSz)
{
word32 idx = 0;
int ret;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_RSA_LoadDer");
2016-06-27 10:53:34 -07:00
if (rsa == NULL || rsa->internal == NULL || derBuf == NULL || derSz <= 0) {
WOLFSSL_MSG("Bad function arguments");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
ret = wc_RsaPrivateKeyDecode(derBuf, &idx, (RsaKey*)rsa->internal, derSz);
if (ret < 0) {
WOLFSSL_MSG("RsaPrivateKeyDecode failed");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
if (SetRsaExternal(rsa) != SSL_SUCCESS) {
WOLFSSL_MSG("SetRsaExternal failed");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
rsa->inSet = 1;
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
#endif /* NO_RSA */
2016-06-27 10:53:34 -07:00
#ifndef NO_DSA
/* return SSL_SUCCESS if success, SSL_FATAL_ERROR if error */
int wolfSSL_DSA_LoadDer(WOLFSSL_DSA* dsa, const unsigned char* derBuf, int derSz)
{
2016-06-27 10:53:34 -07:00
word32 idx = 0;
int ret;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_DSA_LoadDer");
2016-06-27 10:53:34 -07:00
if (dsa == NULL || dsa->internal == NULL || derBuf == NULL || derSz <= 0) {
WOLFSSL_MSG("Bad function arguments");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
ret = DsaPrivateKeyDecode(derBuf, &idx, (DsaKey*)dsa->internal, derSz);
if (ret < 0) {
WOLFSSL_MSG("DsaPrivateKeyDecode failed");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
if (SetDsaExternal(dsa) != SSL_SUCCESS) {
WOLFSSL_MSG("SetDsaExternal failed");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
dsa->inSet = 1;
return SSL_SUCCESS;
}
#endif /* NO_DSA */
2016-06-27 10:53:34 -07:00
#ifdef HAVE_ECC
/* return SSL_SUCCESS if success, SSL_FATAL_ERROR if error */
int wolfSSL_EC_KEY_LoadDer(WOLFSSL_EC_KEY* key,
const unsigned char* derBuf, int derSz)
{
2016-06-27 10:53:34 -07:00
word32 idx = 0;
int ret;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC_KEY_LoadDer");
2016-06-27 10:53:34 -07:00
if (key == NULL || key->internal == NULL || derBuf == NULL || derSz <= 0) {
WOLFSSL_MSG("Bad function arguments");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
ret = wc_EccPrivateKeyDecode(derBuf, &idx, (ecc_key*)key->internal, derSz);
if (ret < 0) {
WOLFSSL_MSG("wc_EccPrivateKeyDecode failed");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
if (SetECKeyExternal(key) != SSL_SUCCESS) {
WOLFSSL_MSG("SetECKeyExternal failed");
return SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
key->inSet = 1;
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
#endif /* HAVE_ECC */
2016-06-27 10:53:34 -07:00
#endif /* OPENSSL_EXTRA */
2016-06-27 10:53:34 -07:00
#ifdef SESSION_CERTS
2016-06-27 10:53:34 -07:00
/* Get peer's certificate chain */
WOLFSSL_X509_CHAIN* wolfSSL_get_peer_chain(WOLFSSL* ssl)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_get_peer_chain");
if (ssl)
return &ssl->session.chain;
return 0;
}
2016-06-27 10:53:34 -07:00
/* Get peer's certificate chain total count */
int wolfSSL_get_chain_count(WOLFSSL_X509_CHAIN* chain)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_get_chain_count");
if (chain)
return chain->count;
2016-06-27 10:53:34 -07:00
return 0;
}
2016-06-27 10:53:34 -07:00
/* Get peer's ASN.1 DER certificate at index (idx) length in bytes */
int wolfSSL_get_chain_length(WOLFSSL_X509_CHAIN* chain, int idx)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_get_chain_length");
if (chain)
return chain->certs[idx].length;
2016-06-27 10:53:34 -07:00
return 0;
}
2016-06-27 10:53:34 -07:00
/* Get peer's ASN.1 DER certificate at index (idx) */
byte* wolfSSL_get_chain_cert(WOLFSSL_X509_CHAIN* chain, int idx)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_get_chain_cert");
if (chain)
return chain->certs[idx].buffer;
2016-06-27 10:53:34 -07:00
return 0;
}
2016-06-27 10:53:34 -07:00
/* Get peer's wolfSSL X509 certificate at index (idx) */
WOLFSSL_X509* wolfSSL_get_chain_X509(WOLFSSL_X509_CHAIN* chain, int idx)
{
2016-06-27 10:53:34 -07:00
int ret;
WOLFSSL_X509* x509 = NULL;
#ifdef WOLFSSL_SMALL_STACK
DecodedCert* cert = NULL;
#else
DecodedCert cert[1];
#endif
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_get_chain_X509");
if (chain != NULL) {
#ifdef WOLFSSL_SMALL_STACK
cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
DYNAMIC_TYPE_TMP_BUFFER);
if (cert != NULL)
#endif
{
InitDecodedCert(cert, chain->certs[idx].buffer,
chain->certs[idx].length, NULL);
if ((ret = ParseCertRelative(cert, CERT_TYPE, 0, NULL)) != 0)
WOLFSSL_MSG("Failed to parse cert");
else {
x509 = (WOLFSSL_X509*)XMALLOC(sizeof(WOLFSSL_X509), NULL,
DYNAMIC_TYPE_X509);
if (x509 == NULL) {
WOLFSSL_MSG("Failed alloc X509");
}
else {
InitX509(x509, 1, NULL);
2016-06-27 10:53:34 -07:00
if ((ret = CopyDecodedToX509(x509, cert)) != 0) {
WOLFSSL_MSG("Failed to copy decoded");
XFREE(x509, NULL, DYNAMIC_TYPE_X509);
x509 = NULL;
}
}
}
2016-06-27 10:53:34 -07:00
FreeDecodedCert(cert);
#ifdef WOLFSSL_SMALL_STACK
XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
}
}
2016-06-27 10:53:34 -07:00
return x509;
}
2016-06-27 10:53:34 -07:00
/* Get peer's PEM certificate at index (idx), output to buffer if inLen big
enough else return error (-1). If buffer is NULL only calculate
outLen. Output length is in *outLen SSL_SUCCESS on ok */
int wolfSSL_get_chain_cert_pem(WOLFSSL_X509_CHAIN* chain, int idx,
unsigned char* buf, int inLen, int* outLen)
{
2016-06-27 10:53:34 -07:00
const char header[] = "-----BEGIN CERTIFICATE-----\n";
const char footer[] = "-----END CERTIFICATE-----\n";
2016-06-27 10:53:34 -07:00
int headerLen = sizeof(header) - 1;
int footerLen = sizeof(footer) - 1;
int i;
int err;
word32 szNeeded = 0;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_get_chain_cert_pem");
if (!chain || !outLen || idx < 0 || idx >= wolfSSL_get_chain_count(chain))
return BAD_FUNC_ARG;
2016-06-27 10:53:34 -07:00
/* Null output buffer return size needed in outLen */
if(!buf) {
if(Base64_Encode(chain->certs[idx].buffer, chain->certs[idx].length,
NULL, &szNeeded) != LENGTH_ONLY_E)
return SSL_FAILURE;
*outLen = szNeeded + headerLen + footerLen;
return LENGTH_ONLY_E;
}
2016-06-27 10:53:34 -07:00
/* don't even try if inLen too short */
if (inLen < headerLen + footerLen + chain->certs[idx].length)
return BAD_FUNC_ARG;
2016-06-27 10:53:34 -07:00
/* header */
if (XMEMCPY(buf, header, headerLen) == NULL)
return SSL_FATAL_ERROR;
2016-06-27 10:53:34 -07:00
i = headerLen;
2016-06-27 10:53:34 -07:00
/* body */
*outLen = inLen; /* input to Base64_Encode */
if ( (err = Base64_Encode(chain->certs[idx].buffer,
chain->certs[idx].length, buf + i, (word32*)outLen)) < 0)
return err;
i += *outLen;
/* footer */
if ( (i + footerLen) > inLen)
return BAD_FUNC_ARG;
if (XMEMCPY(buf + i, footer, footerLen) == NULL)
return SSL_FATAL_ERROR;
*outLen += headerLen + footerLen;
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
/* get session ID */
const byte* wolfSSL_get_sessionID(const WOLFSSL_SESSION* session)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_get_sessionID");
if (session)
return session->sessionID;
2014-02-11 13:08:12 -08:00
return NULL;
}
2014-02-11 13:08:12 -08:00
2016-06-27 10:53:34 -07:00
#endif /* SESSION_CERTS */
2016-06-27 10:53:34 -07:00
#ifdef HAVE_FUZZER
void wolfSSL_SetFuzzerCb(WOLFSSL* ssl, CallbackFuzzer cbf, void* fCtx)
{
if (ssl) {
ssl->fuzzerCb = cbf;
ssl->fuzzerCtx = fCtx;
}
}
2016-06-27 10:53:34 -07:00
#endif
2015-07-15 13:48:31 -06:00
2016-06-27 10:53:34 -07:00
#ifndef NO_CERTS
#ifdef HAVE_PK_CALLBACKS
2016-06-27 10:53:34 -07:00
#ifdef HAVE_ECC
2016-06-27 10:53:34 -07:00
void wolfSSL_CTX_SetEccSignCb(WOLFSSL_CTX* ctx, CallbackEccSign cb)
{
if (ctx)
ctx->EccSignCb = cb;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_SetEccSignCtx(WOLFSSL* ssl, void *ctx)
{
2016-06-27 10:53:34 -07:00
if (ssl)
ssl->EccSignCtx = ctx;
}
2016-06-27 10:53:34 -07:00
void* wolfSSL_GetEccSignCtx(WOLFSSL* ssl)
{
2016-06-27 10:53:34 -07:00
if (ssl)
return ssl->EccSignCtx;
return NULL;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_CTX_SetEccVerifyCb(WOLFSSL_CTX* ctx, CallbackEccVerify cb)
{
2016-06-27 10:53:34 -07:00
if (ctx)
ctx->EccVerifyCb = cb;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_SetEccVerifyCtx(WOLFSSL* ssl, void *ctx)
{
2016-06-27 10:53:34 -07:00
if (ssl)
ssl->EccVerifyCtx = ctx;
}
2015-07-15 13:48:31 -06:00
2016-06-27 10:53:34 -07:00
void* wolfSSL_GetEccVerifyCtx(WOLFSSL* ssl)
{
2016-06-27 10:53:34 -07:00
if (ssl)
return ssl->EccVerifyCtx;
2016-06-27 10:53:34 -07:00
return NULL;
}
void wolfSSL_CTX_SetEccSharedSecretCb(WOLFSSL_CTX* ctx, CallbackEccSharedSecret cb)
{
if (ctx)
ctx->EccSharedSecretCb = cb;
}
void wolfSSL_SetEccSharedSecretCtx(WOLFSSL* ssl, void *ctx)
{
if (ssl)
ssl->EccSharedSecretCtx = ctx;
}
void* wolfSSL_GetEccSharedSecretCtx(WOLFSSL* ssl)
{
if (ssl)
return ssl->EccSharedSecretCtx;
return NULL;
}
2016-06-27 10:53:34 -07:00
#endif /* HAVE_ECC */
2016-06-27 10:53:34 -07:00
#ifndef NO_RSA
2016-06-27 10:53:34 -07:00
void wolfSSL_CTX_SetRsaSignCb(WOLFSSL_CTX* ctx, CallbackRsaSign cb)
{
2016-06-27 10:53:34 -07:00
if (ctx)
ctx->RsaSignCb = cb;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_SetRsaSignCtx(WOLFSSL* ssl, void *ctx)
{
2016-06-27 10:53:34 -07:00
if (ssl)
ssl->RsaSignCtx = ctx;
}
2016-06-27 10:53:34 -07:00
void* wolfSSL_GetRsaSignCtx(WOLFSSL* ssl)
{
2016-06-27 10:53:34 -07:00
if (ssl)
return ssl->RsaSignCtx;
return NULL;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_CTX_SetRsaVerifyCb(WOLFSSL_CTX* ctx, CallbackRsaVerify cb)
{
2016-06-27 10:53:34 -07:00
if (ctx)
ctx->RsaVerifyCb = cb;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_SetRsaVerifyCtx(WOLFSSL* ssl, void *ctx)
{
2016-06-27 10:53:34 -07:00
if (ssl)
ssl->RsaVerifyCtx = ctx;
}
2015-07-15 13:48:31 -06:00
2016-06-27 10:53:34 -07:00
void* wolfSSL_GetRsaVerifyCtx(WOLFSSL* ssl)
{
2016-06-27 10:53:34 -07:00
if (ssl)
return ssl->RsaVerifyCtx;
return NULL;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_CTX_SetRsaEncCb(WOLFSSL_CTX* ctx, CallbackRsaEnc cb)
{
if (ctx)
2016-06-27 10:53:34 -07:00
ctx->RsaEncCb = cb;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_SetRsaEncCtx(WOLFSSL* ssl, void *ctx)
{
2016-06-27 10:53:34 -07:00
if (ssl)
ssl->RsaEncCtx = ctx;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
void* wolfSSL_GetRsaEncCtx(WOLFSSL* ssl)
2015-07-14 14:56:26 -06:00
{
2016-06-27 10:53:34 -07:00
if (ssl)
return ssl->RsaEncCtx;
return NULL;
2015-07-14 14:56:26 -06:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_CTX_SetRsaDecCb(WOLFSSL_CTX* ctx, CallbackRsaDec cb)
{
2016-06-27 10:53:34 -07:00
if (ctx)
ctx->RsaDecCb = cb;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
void wolfSSL_SetRsaDecCtx(WOLFSSL* ssl, void *ctx)
2015-07-14 14:56:26 -06:00
{
2016-06-27 10:53:34 -07:00
if (ssl)
ssl->RsaDecCtx = ctx;
2015-07-14 14:56:26 -06:00
}
2016-06-27 10:53:34 -07:00
void* wolfSSL_GetRsaDecCtx(WOLFSSL* ssl)
2015-07-14 14:56:26 -06:00
{
2016-06-27 10:53:34 -07:00
if (ssl)
return ssl->RsaDecCtx;
return NULL;
2015-07-14 14:56:26 -06:00
}
2016-06-27 10:53:34 -07:00
#endif /* NO_RSA */
#endif /* HAVE_PK_CALLBACKS */
#endif /* NO_CERTS */
#ifdef WOLFSSL_HAVE_WOLFSCEP
/* Used by autoconf to see if wolfSCEP is available */
void wolfSSL_wolfSCEP(void) {}
#endif
#ifdef WOLFSSL_HAVE_CERT_SERVICE
/* Used by autoconf to see if cert service is available */
void wolfSSL_cert_service(void) {}
#endif
#ifdef OPENSSL_EXTRA /*Lighttp compatibility*/
#if defined(HAVE_LIGHTY) || defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(HAVE_STUNNEL)
2016-06-27 10:53:34 -07:00
unsigned char *wolfSSL_SHA1(const unsigned char *d, size_t n, unsigned char *md)
{
(void) *d; (void) n; (void) *md;
WOLFSSL_ENTER("wolfSSL_SHA1");
WOLFSSL_STUB("wolfssl_SHA1");
return NULL;
}
char wolfSSL_CTX_use_certificate(WOLFSSL_CTX *ctx, WOLFSSL_X509 *x) {
(void)ctx;
(void)x;
WOLFSSL_ENTER("wolfSSL_CTX_use_certificate");
WOLFSSL_STUB("wolfSSL_CTX_use_certificate");
return 0;
}
int wolfSSL_CTX_use_PrivateKey(WOLFSSL_CTX *ctx, WOLFSSL_EVP_PKEY *pkey) {
(void)ctx;
(void)pkey;
WOLFSSL_ENTER("wolfSSL_CTX_use_PrivateKey");
WOLFSSL_STUB("wolfSSL_CTX_use_PrivateKey");
return 0;
}
int wolfSSL_BIO_read_filename(WOLFSSL_BIO *b, const char *name) {
(void)b;
(void)name;
WOLFSSL_ENTER("wolfSSL_BIO_read_filename");
WOLFSSL_STUB("wolfSSL_BIO_read_filename");
return 0;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_file(void) {
WOLFSSL_ENTER("wolfSSL_BIO_s_file");
WOLFSSL_STUB("wolfSSL_BIO_s_file");
2016-06-27 10:53:34 -07:00
return NULL;
}
2015-07-14 14:56:26 -06:00
#ifdef HAVE_ECC
2016-06-27 10:53:34 -07:00
const char * wolf_OBJ_nid2sn(int n) {
int i;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolf_OBJ_nid2sn");
/* find based on NID and return name */
for (i = 0; i < ecc_sets[i].size; i++) {
if (n == ecc_sets[i].id) {
return ecc_sets[i].name;
}
}
return NULL;
2016-06-27 10:53:34 -07:00
}
2016-06-27 10:53:34 -07:00
int wolf_OBJ_obj2nid(const WOLFSSL_ASN1_OBJECT *o) {
(void)o;
WOLFSSL_ENTER("wolf_OBJ_obj2nid");
WOLFSSL_STUB("wolf_OBJ_obj2nid");
2016-06-27 10:53:34 -07:00
return 0;
}
2016-06-27 10:53:34 -07:00
int wolf_OBJ_sn2nid(const char *sn) {
int i;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolf_OBJ_osn2nid");
/* find based on name and return NID */
for (i = 0; i < ecc_sets[i].size; i++) {
if (XSTRNCMP(sn, ecc_sets[i].name, ECC_MAXNAME) == 0) {
return ecc_sets[i].id;
}
}
return -1;
2016-06-27 10:53:34 -07:00
}
#endif /* HAVE_ECC */
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
WOLFSSL_X509 *PEM_read_bio_WOLFSSL_X509(WOLFSSL_BIO *bp, WOLFSSL_X509 **x, pem_password_cb *cb, void *u) {
(void)bp;
(void)x;
(void)cb;
(void)u;
WOLFSSL_ENTER("PEM_read_bio_WOLFSSL_X509");
WOLFSSL_STUB("PEM_read_bio_WOLFSSL_X509");
return NULL;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_CTX_set_verify_depth(WOLFSSL_CTX *ctx, int depth) {
(void)ctx;
(void)depth;
WOLFSSL_ENTER("wolfSSL_CTX_set_verify_depth");
WOLFSSL_STUB("wolfSSL_CTX_set_verify_depth");
}
2016-06-27 10:53:34 -07:00
void* wolfSSL_get_app_data( const WOLFSSL *ssl)
{
/* checkout exdata stuff... */
(void)ssl;
WOLFSSL_ENTER("wolfSSL_get_app_data");
WOLFSSL_STUB("wolfSSL_get_app_data");
return 0;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_set_app_data(WOLFSSL *ssl, void *arg) {
(void)ssl;
(void)arg;
WOLFSSL_ENTER("wolfSSL_set_app_data");
WOLFSSL_STUB("wolfSSL_set_app_data");
}
2016-06-27 10:53:34 -07:00
WOLFSSL_ASN1_OBJECT * wolfSSL_X509_NAME_ENTRY_get_object(WOLFSSL_X509_NAME_ENTRY *ne) {
(void)ne;
WOLFSSL_ENTER("wolfSSL_X509_NAME_ENTRY_get_object");
WOLFSSL_STUB("wolfSSL_X509_NAME_ENTRY_get_object");
2016-06-27 10:53:34 -07:00
return NULL;
}
2016-06-27 10:53:34 -07:00
WOLFSSL_X509_NAME_ENTRY *wolfSSL_X509_NAME_get_entry(
WOLFSSL_X509_NAME *name, int loc) {
2015-07-16 16:46:37 -07:00
2016-06-27 10:53:34 -07:00
int maxLoc = name->fullName.fullNameLen;
2015-07-16 16:46:37 -07:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_X509_NAME_get_entry");
2015-07-16 16:46:37 -07:00
2016-06-27 10:53:34 -07:00
if (loc < 0 || loc > maxLoc) {
WOLFSSL_MSG("Bad argument");
return NULL;
}
2016-06-27 10:53:34 -07:00
/* common name index case */
if (loc == name->fullName.cnIdx) {
/* get CN shortcut from x509 since it has null terminator */
name->cnEntry.data.data = name->x509->subjectCN;
name->cnEntry.data.length = name->fullName.cnLen;
name->cnEntry.data.type = ASN_COMMON_NAME;
name->cnEntry.set = 1;
return &(name->cnEntry);
}
2016-06-27 10:53:34 -07:00
/* additionall cases to check for go here */
WOLFSSL_MSG("Entry not found or implemented");
(void)name;
(void)loc;
return NULL;
}
2016-06-27 10:53:34 -07:00
#ifndef NO_CERTS
void wolfSSL_X509_NAME_free(WOLFSSL_X509_NAME *name){
FreeX509Name(name, NULL);
WOLFSSL_ENTER("wolfSSL_X509_NAME_free");
WOLFSSL_STUB("wolfSSL_X509_NAME_free");
}
2016-06-27 10:53:34 -07:00
#endif /* NO_CERTS */
void wolfSSL_sk_X509_NAME_pop_free(STACK_OF(WOLFSSL_X509_NAME)* sk, void f (WOLFSSL_X509_NAME*)){
(void) sk;
(void) f;
WOLFSSL_ENTER("wolfSSL_sk_X509_NAME_pop_free");
WOLFSSL_STUB("wolfSSL_sk_X509_NAME_pop_free");
}
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_check_private_key(WOLFSSL_X509 *x509, WOLFSSL_EVP_PKEY *key){
(void) x509;
(void) key;
WOLFSSL_ENTER("wolfSSL_X509_check_private_key");
WOLFSSL_STUB("wolfSSL_X509_check_private_key");
return SSL_SUCCESS;
}
2016-06-27 10:53:34 -07:00
STACK_OF(WOLFSSL_X509_NAME) *wolfSSL_dup_CA_list( STACK_OF(WOLFSSL_X509_NAME) *sk ){
(void) sk;
WOLFSSL_ENTER("wolfSSL_dup_CA_list");
WOLFSSL_STUB("wolfSSL_dup_CA_list");
return NULL;
}
#endif /* HAVE_LIGHTY || WOLFSSL_MYSQL_COMPATIBLE || HAVE_STUNNEL */
2016-06-27 10:53:34 -07:00
#endif
#ifdef OPENSSL_EXTRA
void* wolfSSL_CTX_get_ex_data(const WOLFSSL_CTX* ctx, int idx)
{
WOLFSSL_ENTER("wolfSSL_CTX_get_ex_data");
#ifdef HAVE_STUNNEL
if(ctx != NULL && idx < MAX_EX_DATA && idx >= 0) {
return ctx->ex_data[idx];
}
#else
(void)ctx;
(void)idx;
#endif
return NULL;
}
2015-07-16 16:46:37 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_CTX_get_ex_new_index(long idx, void* arg, void* a, void* b,
void* c)
{
WOLFSSL_ENTER("wolfSSL_CTX_get_ex_new_index");
(void)idx;
(void)arg;
(void)a;
(void)b;
(void)c;
return 0;
2015-07-16 16:46:37 -07:00
}
2016-06-27 10:53:34 -07:00
int wolfSSL_CTX_set_ex_data(WOLFSSL_CTX* ctx, int idx, void* data)
2015-07-16 16:46:37 -07:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_set_ex_data");
#ifdef HAVE_STUNNEL
if (ctx != NULL && idx < MAX_EX_DATA)
{
ctx->ex_data[idx] = data;
return SSL_SUCCESS;
}
#else
(void)ctx;
(void)idx;
(void)data;
#endif
return SSL_FAILURE;
2016-06-27 10:53:34 -07:00
}
int wolfSSL_set_ex_data(WOLFSSL* ssl, int idx, void* data)
{
WOLFSSL_ENTER("wolfSSL_set_ex_data");
#if defined(FORTRESS) || defined(HAVE_STUNNEL)
if (ssl != NULL && idx < MAX_EX_DATA)
{
ssl->ex_data[idx] = data;
return SSL_SUCCESS;
}
#else
2016-06-27 10:53:34 -07:00
(void)ssl;
(void)idx;
(void)data;
#endif
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2015-07-16 16:46:37 -07:00
2016-06-27 10:53:34 -07:00
int wolfSSL_get_ex_new_index(long idx, void* data, void* cb1, void* cb2,
void* cb3)
{
WOLFSSL_ENTER("wolfSSL_get_ex_new_index");
(void)idx;
(void)data;
(void)cb1;
(void)cb2;
(void)cb3;
return 0;
}
2015-07-16 16:46:37 -07:00
2016-06-27 10:53:34 -07:00
void* wolfSSL_get_ex_data(const WOLFSSL* ssl, int idx)
{
WOLFSSL_ENTER("wolfSSL_get_ex_data");
#if defined(FORTRESS) || defined(HAVE_STUNNEL)
if (ssl != NULL && idx < MAX_EX_DATA && idx >= 0)
return ssl->ex_data[idx];
#else
(void)ssl;
(void)idx;
#endif
return 0;
}
#endif /* OPENSSL_EXTRA */
2015-07-16 16:46:37 -07:00
2016-06-27 10:53:34 -07:00
#if defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL) \
|| defined(WOLFSSL_MYSQL_COMPATIBLE)
char * wolf_OBJ_nid2ln(int n) {
(void)n;
WOLFSSL_ENTER("wolf_OBJ_nid2ln");
WOLFSSL_STUB("wolf_OBJ_nid2ln");
2015-07-16 16:46:37 -07:00
2016-06-27 10:53:34 -07:00
return NULL;
}
2015-07-16 16:46:37 -07:00
2016-06-27 10:53:34 -07:00
int wolf_OBJ_txt2nid(const char* s) {
(void)s;
WOLFSSL_ENTER("wolf_OBJ_txt2nid");
WOLFSSL_STUB("wolf_OBJ_txt2nid");
2015-07-16 16:46:37 -07:00
2016-06-27 10:53:34 -07:00
return 0;
2015-07-16 16:46:37 -07:00
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
WOLFSSL_BIO *wolfSSL_BIO_new_file(const char *filename, const char *mode) {
(void)filename;
(void)mode;
WOLFSSL_ENTER("wolfSSL_BIO_new_file");
WOLFSSL_STUB("wolfSSL_BIO_new_file");
2016-06-27 10:53:34 -07:00
return NULL;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
WOLFSSL_DH *wolfSSL_PEM_read_bio_DHparams(WOLFSSL_BIO *bp, WOLFSSL_DH **x, pem_password_cb *cb, void *u)
{
(void) bp;
(void) x;
(void) cb;
(void) u;
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_PEM_read_bio_DHparams");
WOLFSSL_STUB("wolfSSL_PEM_read_bio_DHparams");
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
return NULL;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
int PEM_write_bio_WOLFSSL_X509(WOLFSSL_BIO *bp, WOLFSSL_X509 *x) {
(void)bp;
(void)x;
WOLFSSL_ENTER("PEM_write_bio_WOLFSSL_X509");
WOLFSSL_STUB("PEM_write_bio_WOLFSSL_X509");
return 0;
}
#ifndef NO_DH
/* Intialize ctx->dh with dh's params. Return SSL_SUCCESS on ok */
long wolfSSL_CTX_set_tmp_dh(WOLFSSL_CTX* ctx, WOLFSSL_DH* dh)
2015-07-14 14:56:26 -06:00
{
2016-06-27 10:53:34 -07:00
int pSz, gSz;
byte *p, *g;
int ret=0;
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_set_tmp_dh");
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
if(!ctx || !dh)
return BAD_FUNC_ARG;
/* Get needed size for p and g */
pSz = wolfSSL_BN_bn2bin(dh->p, NULL);
gSz = wolfSSL_BN_bn2bin(dh->g, NULL);
if(pSz <= 0 || gSz <= 0)
return SSL_FATAL_ERROR;
p = (byte*)XMALLOC(pSz, ctx->heap, DYNAMIC_TYPE_DH);
if(!p)
return MEMORY_E;
2016-06-27 10:53:34 -07:00
g = (byte*)XMALLOC(gSz, ctx->heap, DYNAMIC_TYPE_DH);
if(!g) {
XFREE(p, ctx->heap, DYNAMIC_TYPE_DH);
return MEMORY_E;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
pSz = wolfSSL_BN_bn2bin(dh->p, p);
gSz = wolfSSL_BN_bn2bin(dh->g, g);
2016-06-27 10:53:34 -07:00
if(pSz >= 0 && gSz >= 0) /* Conversion successful */
ret = wolfSSL_CTX_SetTmpDH(ctx, p, pSz, g, gSz);
2016-06-27 10:53:34 -07:00
XFREE(p, ctx->heap, DYNAMIC_TYPE_DH);
XFREE(g, ctx->heap, DYNAMIC_TYPE_DH);
return pSz > 0 && gSz > 0 ? ret : SSL_FATAL_ERROR;
}
2016-06-27 10:53:34 -07:00
#endif /* NO_DH */
#endif /* HAVE_LIGHTY || HAVE_STUNNEL || WOLFSSL_MYSQL_COMPATIBLE */
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
/* stunnel compatibility functions*/
#if defined(OPENSSL_EXTRA) && defined(HAVE_STUNNEL)
void WOLFSSL_ERR_remove_thread_state(void* pid)
{
(void) pid;
return;
}
2016-06-27 10:53:34 -07:00
int wolfSSL_SESSION_set_ex_data(WOLFSSL_SESSION* session, int idx, void* data)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_SESSION_set_ex_data");
if(session != NULL && idx < MAX_EX_DATA) {
session->ex_data[idx] = data;
return SSL_SUCCESS;
}
return SSL_FAILURE;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
int wolfSSL_SESSION_get_ex_new_index(long idx, void* data, void* cb1,
void* cb2, CRYPTO_free_func* cb3)
2015-07-14 14:56:26 -06:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_SESSION_get_ex_new_index");
(void)idx;
(void)cb1;
(void)cb2;
(void)cb3;
if(XSTRNCMP((const char*)data, "redirect index", 14) == 0) {
return 0;
}
2016-06-27 10:53:34 -07:00
else if(XSTRNCMP((const char*)data, "addr index", 10) == 0) {
return 1;
}
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
void* wolfSSL_SESSION_get_ex_data(const WOLFSSL_SESSION* session, int idx)
{
WOLFSSL_ENTER("wolfSSL_SESSION_get_ex_data");
if (session != NULL && idx < MAX_EX_DATA && idx >= 0)
return session->ex_data[idx];
return NULL;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
int wolfSSL_CRYPTO_set_mem_ex_functions(void *(*m) (size_t, const char *, int),
void *(*r) (void *, size_t, const char *,
int), void (*f) (void *))
{
(void) m;
(void) r;
(void) f;
WOLFSSL_ENTER("wolfSSL_CRYPTO_set_mem_ex_functions");
WOLFSSL_STUB("wolfSSL_CRYPTO_set_mem_ex_functions");
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
WOLFSSL_DH *wolfSSL_DH_generate_parameters(int prime_len, int generator,
void (*callback) (int, int, void *), void *cb_arg)
{
(void)prime_len;
(void)generator;
(void)callback;
(void)cb_arg;
WOLFSSL_ENTER("wolfSSL_DH_generate_parameters");
WOLFSSL_STUB("wolfSSL_DH_generate_parameters");
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
return NULL;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
int wolfSSL_DH_generate_parameters_ex(WOLFSSL_DH* dh, int prime_len, int generator,
void (*callback) (int, int, void *))
{
(void)prime_len;
(void)generator;
(void)callback;
(void)dh;
WOLFSSL_ENTER("wolfSSL_DH_generate_parameters_ex");
WOLFSSL_STUB("wolfSSL_DH_generate_parameters_ex");
2016-06-27 10:53:34 -07:00
return -1;
2015-07-14 14:56:26 -06:00
}
2016-06-27 10:53:34 -07:00
void wolfSSL_ERR_load_crypto_strings(void)
2015-07-14 14:56:26 -06:00
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_ERR_load_crypto_strings");
WOLFSSL_ENTER("wolfSSL_ERR_load_crypto_strings");
return;
}
2016-06-27 10:53:34 -07:00
unsigned long wolfSSL_ERR_peek_last_error(void)
{
2016-06-27 10:53:34 -07:00
unsigned long l = 0UL;
WOLFSSL_ENTER("wolfSSL_ERR_peek_last_error");
WOLFSSL_STUB("wolfSSL_ERR_peek_last_error");
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
return l;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
int wolfSSL_FIPS_mode(void)
{
WOLFSSL_ENTER("wolfSSL_FIPS_mode");
WOLFSSL_STUB("wolfSSL_FIPS_mode");
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
int wolfSSL_FIPS_mode_set(int r)
{
(void)r;
WOLFSSL_ENTER("wolfSSL_FIPS_mode_set");
WOLFSSL_STUB("wolfSSL_FIPS_mode_set");
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
int wolfSSL_RAND_set_rand_method(const void *meth)
2015-07-14 14:56:26 -06:00
{
2016-06-27 10:53:34 -07:00
(void) meth;
WOLFSSL_ENTER("wolfSSL_RAND_set_rand_method");
WOLFSSL_STUB("wolfSSL_RAND_set_rand_method");
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
int wolfSSL_CIPHER_get_bits(const WOLFSSL_CIPHER *c, int *alg_bits)
{
int ret = SSL_FAILURE;
WOLFSSL_ENTER("wolfSSL_CIPHER_get_bits");
if(c != NULL && c->ssl != NULL) {
ret = 8 * c->ssl->specs.key_size;
if(alg_bits != NULL) {
*alg_bits = ret;
2015-07-14 14:56:26 -06:00
}
}
2016-06-27 10:53:34 -07:00
return ret;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
int wolfSSL_sk_X509_NAME_num(const STACK_OF(WOLFSSL_X509_NAME) *s)
{
(void) s;
WOLFSSL_ENTER("wolfSSL_sk_X509_NAME_num");
WOLFSSL_STUB("wolfSSL_sk_X509_NAME_num");
return SSL_FAILURE;
2015-07-14 14:56:26 -06:00
}
2016-06-27 10:53:34 -07:00
int wolfSSL_sk_X509_num(const STACK_OF(WOLFSSL_X509) *s)
2015-07-14 14:56:26 -06:00
{
2016-06-27 10:53:34 -07:00
(void) s;
WOLFSSL_ENTER("wolfSSL_sk_X509_num");
WOLFSSL_STUB("wolfSSL_sk_X509_num");
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_NAME_print_ex(WOLFSSL_BIO* bio, WOLFSSL_X509_NAME* nm,
int indent, unsigned long flags)
{
(void)bio;
(void)nm;
(void)indent;
(void)flags;
WOLFSSL_ENTER("wolfSSL_X509_NAME_print_ex");
WOLFSSL_STUB("wolfSSL_X509_NAME_print_ex");
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
WOLFSSL_ASN1_BIT_STRING* wolfSSL_X509_get0_pubkey_bitstr(const WOLFSSL_X509* x)
{
(void)x;
WOLFSSL_ENTER("wolfSSL_X509_get0_pubkey_bitstr");
WOLFSSL_STUB("wolfSSL_X509_get0_pubkey_bitstr");
2016-06-27 10:53:34 -07:00
return NULL;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
int wolfSSL_CTX_add_session(WOLFSSL_CTX* ctx, WOLFSSL_SESSION* session)
{
(void)ctx;
(void)session;
WOLFSSL_ENTER("wolfSSL_CTX_add_session");
WOLFSSL_STUB("wolfSSL_CTX_add_session");
2016-06-27 10:53:34 -07:00
return SSL_SUCCESS;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
int wolfSSL_get_state(const WOLFSSL* ssl)
{
(void)ssl;
WOLFSSL_ENTER("wolfSSL_get_state");
WOLFSSL_STUB("wolfSSL_get_state");
2016-06-27 10:53:34 -07:00
return SSL_FAILURE;
}
2015-07-14 14:56:26 -06:00
void* wolfSSL_sk_X509_NAME_value(const STACK_OF(WOLFSSL_X509_NAME)* sk, int i)
2016-06-27 10:53:34 -07:00
{
(void)sk;
(void)i;
WOLFSSL_ENTER("wolfSSL_sk_X509_NAME_value");
WOLFSSL_STUB("wolfSSL_sk_X509_NAME_value");
2016-06-27 10:53:34 -07:00
return NULL;
2015-07-14 14:56:26 -06:00
}
2016-06-27 10:53:34 -07:00
void* wolfSSL_sk_X509_value(STACK_OF(WOLFSSL_X509)* sk, int i)
{
(void)sk;
(void)i;
WOLFSSL_ENTER("wolfSSL_sk_X509_value");
WOLFSSL_STUB("wolfSSL_sk_X509_value");
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
return NULL;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
int wolfSSL_version(WOLFSSL* ssl)
{
WOLFSSL_ENTER("wolfSSL_version");
if (ssl->version.major == SSLv3_MAJOR) {
switch (ssl->version.minor) {
case SSLv3_MINOR :
return SSL3_VERSION;
case TLSv1_MINOR :
case TLSv1_1_MINOR :
case TLSv1_2_MINOR :
return TLS1_VERSION;
default:
return SSL_FAILURE;
}
}
else if (ssl->version.major == DTLS_MAJOR) {
switch (ssl->version.minor) {
case DTLS_MINOR :
case DTLSv1_2_MINOR :
return DTLS1_VERSION;
default:
return SSL_FAILURE;
}
}
return SSL_FAILURE;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
STACK_OF(WOLFSSL_X509)* wolfSSL_get_peer_cert_chain(const WOLFSSL* ssl)
{
(void)ssl;
WOLFSSL_ENTER("wolfSSL_get_peer_cert_chain");
WOLFSSL_STUB("wolfSSL_get_peer_cert_chain");
2016-06-27 10:53:34 -07:00
return NULL;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_get_options(WOLFSSL_CTX* ctx)
{
(void)ctx;
WOLFSSL_ENTER("wolfSSL_CTX_get_options");
WOLFSSL_STUB("wolfSSL_CTX_get_options");
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
return 0;
}
2015-07-14 14:56:26 -06:00
2016-06-27 10:53:34 -07:00
WOLFSSL_CTX* wolfSSL_get_SSL_CTX(WOLFSSL* ssl)
{
WOLFSSL_ENTER("wolfSSL_get_SSL_CTX");
return ssl->ctx;
}
2016-06-27 10:53:34 -07:00
int wolfSSL_X509_NAME_get_sz(WOLFSSL_X509_NAME* name)
{
WOLFSSL_ENTER("wolfSSL_X509_NAME_get_sz");
if(!name)
return -1;
return name->sz;
}
2016-06-27 10:53:34 -07:00
const byte* wolfSSL_SESSION_get_id(WOLFSSL_SESSION* sess, unsigned int* idLen)
{
WOLFSSL_ENTER("wolfSSL_SESSION_get_id");
WOLFSSL_STUB("wolfSSL_SESSION_get_id");
if(!sess || !idLen) {
WOLFSSL_MSG("Bad func args. Please provide idLen");
return NULL;
}
2016-06-27 10:53:34 -07:00
*idLen = sess->sessionIDSz;
return sess->sessionID;
}
2016-06-27 10:53:34 -07:00
#ifdef HAVE_SNI
int wolfSSL_set_tlsext_host_name(WOLFSSL* ssl, const char* host_name)
{
int ret;
WOLFSSL_ENTER("wolfSSL_set_tlsext_host_name");
ret = wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
host_name, XSTRLEN(host_name));
WOLFSSL_LEAVE("wolfSSL_set_tlsext_host_name", ret);
return ret;
}
2016-06-27 10:53:34 -07:00
#ifndef NO_WOLFSSL_SERVER
const char * wolfSSL_get_servername(WOLFSSL* ssl, byte type)
{
2016-06-27 10:53:34 -07:00
void * serverName = NULL;
if (ssl == NULL)
return NULL;
TLSX_SNI_GetRequest(ssl->extensions, type, &serverName);
return (const char *)serverName;
}
#endif /* NO_WOLFSSL_SERVER */
#endif /* HAVE_SNI */
2016-06-27 10:53:34 -07:00
WOLFSSL_CTX* wolfSSL_set_SSL_CTX(WOLFSSL* ssl, WOLFSSL_CTX* ctx)
{
if (ssl && ctx && SetSSL_CTX(ssl, ctx) == SSL_SUCCESS)
return ssl->ctx;
return NULL;
}
2016-06-27 10:53:34 -07:00
VerifyCallback wolfSSL_CTX_get_verify_callback(WOLFSSL_CTX* ctx)
{
WOLFSSL_ENTER("wolfSSL_CTX_get_verify_callback");
if(ctx)
return ctx->verifyCallback;
return NULL;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_CTX_set_servername_callback(WOLFSSL_CTX* ctx, CallbackSniRecv cb)
{
WOLFSSL_ENTER("wolfSSL_CTX_set_servername_callback");
if (ctx)
ctx->sniRecvCb = cb;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_CTX_set_servername_arg(WOLFSSL_CTX* ctx, void* arg)
{
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_CTX_set_servername_arg");
if (ctx)
ctx->sniRecvCbArg = arg;
}
2016-06-27 10:53:34 -07:00
long wolfSSL_CTX_clear_options(WOLFSSL_CTX* ctx, long opt)
{
WOLFSSL_ENTER("SSL_CTX_clear_options");
WOLFSSL_STUB("SSL_CTX_clear_options");
(void)ctx;
(void)opt;
return opt;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_THREADID_set_callback(void(*threadid_func)(void*))
{
WOLFSSL_ENTER("wolfSSL_THREADID_set_callback");
WOLFSSL_STUB("wolfSSL_THREADID_set_callback");
(void)threadid_func;
return;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_THREADID_set_numeric(void* id, unsigned long val)
{
WOLFSSL_ENTER("wolfSSL_THREADID_set_numeric");
WOLFSSL_STUB("wolfSSL_THREADID_set_numeric");
(void)id;
(void)val;
return;
}
2016-06-27 10:53:34 -07:00
WOLFSSL_X509* wolfSSL_X509_STORE_get1_certs(WOLFSSL_X509_STORE_CTX* ctx,
WOLFSSL_X509_NAME* name)
{
WOLFSSL_ENTER("wolfSSL_X509_STORE_get1_certs");
WOLFSSL_STUB("wolfSSL_X509_STORE_get1_certs");
(void)ctx;
(void)name;
return NULL;
}
2016-06-27 10:53:34 -07:00
void wolfSSL_sk_X509_pop_free(STACK_OF(WOLFSSL_X509)* sk, void f (WOLFSSL_X509*)){
(void) sk;
(void) f;
WOLFSSL_ENTER("wolfSSL_sk_X509_pop_free");
WOLFSSL_STUB("wolfSSL_sk_X509_pop_free");
}
2016-06-27 10:53:34 -07:00
#endif /* OPENSSL_EXTRA and HAVE_STUNNEL */
2016-06-27 10:53:34 -07:00
#if (defined(OPENSSL_EXTRA) && defined(HAVE_STUNNEL)) \
|| defined(WOLFSSL_MYSQL_COMPATIBLE)
int wolfSSL_CTX_get_verify_mode(WOLFSSL_CTX* ctx)
{
int mode = 0;
WOLFSSL_ENTER("wolfSSL_CTX_get_verify_mode");
2016-06-27 10:53:34 -07:00
if(!ctx)
return SSL_FATAL_ERROR;
2016-06-27 10:53:34 -07:00
if (ctx->verifyPeer)
mode |= SSL_VERIFY_PEER;
else if (ctx->verifyNone)
mode |= SSL_VERIFY_NONE;
2016-06-27 10:53:34 -07:00
if (ctx->failNoCert)
mode |= SSL_VERIFY_FAIL_IF_NO_PEER_CERT;
2016-06-27 10:53:34 -07:00
if (ctx->failNoCertxPSK)
mode |= SSL_VERIFY_FAIL_EXCEPT_PSK;
2016-06-27 10:53:34 -07:00
WOLFSSL_LEAVE("wolfSSL_CTX_get_verify_mode", mode);
return mode;
}
#endif
2016-06-27 10:53:34 -07:00
#if defined(OPENSSL_EXTRA) && defined(HAVE_CURVE25519)
/* return 1 if success, 0 if error
* output keys are little endian format
*/
int wolfSSL_EC25519_generate_key(unsigned char *priv, unsigned int *privSz,
unsigned char *pub, unsigned int *pubSz)
{
#ifndef WOLFSSL_KEY_GEN
WOLFSSL_MSG("No Key Gen built in");
(void) priv;
(void) privSz;
(void) pub;
(void) pubSz;
return SSL_FAILURE;
#else /* WOLFSSL_KEY_GEN */
int ret = SSL_FAILURE;
int initTmpRng = 0;
WC_RNG *rng = NULL;
#ifdef WOLFSSL_SMALL_STACK
WC_RNG *tmpRNG = NULL;
#else
WC_RNG tmpRNG[1];
#endif
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC25519_generate_key");
2016-06-27 10:53:34 -07:00
if (priv == NULL || privSz == NULL || *privSz < CURVE25519_KEYSIZE ||
pub == NULL || pubSz == NULL || *pubSz < CURVE25519_KEYSIZE) {
WOLFSSL_MSG("Bad arguments");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmpRNG == NULL)
return SSL_FAILURE;
#endif
if (wc_InitRng(tmpRNG) == 0) {
rng = tmpRNG;
initTmpRng = 1;
}
else {
WOLFSSL_MSG("Bad RNG Init, trying global");
if (initGlobalRNG == 0)
WOLFSSL_MSG("Global RNG no Init");
else
rng = &globalRNG;
}
2016-06-27 10:53:34 -07:00
if (rng) {
curve25519_key key;
2016-06-27 10:53:34 -07:00
if (wc_curve25519_init(&key) != MP_OKAY)
WOLFSSL_MSG("wc_curve25519_init failed");
else if (wc_curve25519_make_key(rng, CURVE25519_KEYSIZE, &key)!=MP_OKAY)
WOLFSSL_MSG("wc_curve25519_make_key failed");
/* export key pair */
else if (wc_curve25519_export_key_raw_ex(&key, priv, privSz, pub,
pubSz, EC25519_LITTLE_ENDIAN)
!= MP_OKAY)
WOLFSSL_MSG("wc_curve25519_export_key_raw_ex failed");
else
ret = SSL_SUCCESS;
2016-06-27 10:53:34 -07:00
wc_curve25519_free(&key);
}
2016-06-27 10:53:34 -07:00
if (initTmpRng)
wc_FreeRng(tmpRNG);
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
2016-06-27 10:53:34 -07:00
return ret;
#endif /* WOLFSSL_KEY_GEN */
}
2016-06-27 10:53:34 -07:00
/* return 1 if success, 0 if error
* input and output keys are little endian format
*/
int wolfSSL_EC25519_shared_key(unsigned char *shared, unsigned int *sharedSz,
const unsigned char *priv, unsigned int privSz,
const unsigned char *pub, unsigned int pubSz)
{
#ifndef WOLFSSL_KEY_GEN
WOLFSSL_MSG("No Key Gen built in");
(void) shared;
(void) sharedSz;
(void) priv;
(void) privSz;
(void) pub;
(void) pubSz;
return SSL_FAILURE;
#else /* WOLFSSL_KEY_GEN */
int ret = SSL_FAILURE;
curve25519_key privkey, pubkey;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_EC25519_shared_key");
2016-06-27 10:53:34 -07:00
if (shared == NULL || sharedSz == NULL || *sharedSz < CURVE25519_KEYSIZE ||
priv == NULL || privSz < CURVE25519_KEYSIZE ||
pub == NULL || pubSz < CURVE25519_KEYSIZE) {
WOLFSSL_MSG("Bad arguments");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
/* import private key */
if (wc_curve25519_init(&privkey) != MP_OKAY) {
WOLFSSL_MSG("wc_curve25519_init privkey failed");
return ret;
}
if (wc_curve25519_import_private_ex(priv, privSz, &privkey,
EC25519_LITTLE_ENDIAN) != MP_OKAY) {
WOLFSSL_MSG("wc_curve25519_import_private_ex failed");
wc_curve25519_free(&privkey);
return ret;
}
2016-06-27 10:53:34 -07:00
/* import public key */
if (wc_curve25519_init(&pubkey) != MP_OKAY) {
WOLFSSL_MSG("wc_curve25519_init pubkey failed");
wc_curve25519_free(&privkey);
return ret;
}
if (wc_curve25519_import_public_ex(pub, pubSz, &pubkey,
EC25519_LITTLE_ENDIAN) != MP_OKAY) {
WOLFSSL_MSG("wc_curve25519_import_public_ex failed");
wc_curve25519_free(&privkey);
wc_curve25519_free(&pubkey);
return ret;
}
2016-06-27 10:53:34 -07:00
if (wc_curve25519_shared_secret_ex(&privkey, &pubkey,
shared, sharedSz,
EC25519_LITTLE_ENDIAN) != MP_OKAY)
WOLFSSL_MSG("wc_curve25519_shared_secret_ex failed");
else
ret = SSL_SUCCESS;
2016-06-27 10:53:34 -07:00
wc_curve25519_free(&privkey);
wc_curve25519_free(&pubkey);
2016-06-27 10:53:34 -07:00
return ret;
#endif /* WOLFSSL_KEY_GEN */
}
#endif /* OPENSSL_EXTRA && HAVE_CURVE25519 */
2016-06-27 10:53:34 -07:00
#if defined(OPENSSL_EXTRA) && defined(HAVE_ED25519)
/* return 1 if success, 0 if error
* output keys are little endian format
*/
int wolfSSL_ED25519_generate_key(unsigned char *priv, unsigned int *privSz,
unsigned char *pub, unsigned int *pubSz)
{
#ifndef WOLFSSL_KEY_GEN
WOLFSSL_MSG("No Key Gen built in");
(void) priv;
(void) privSz;
(void) pub;
(void) pubSz;
return SSL_FAILURE;
#else /* WOLFSSL_KEY_GEN */
int ret = SSL_FAILURE;
int initTmpRng = 0;
WC_RNG *rng = NULL;
#ifdef WOLFSSL_SMALL_STACK
WC_RNG *tmpRNG = NULL;
#else
WC_RNG tmpRNG[1];
#endif
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_ED25519_generate_key");
2016-06-27 10:53:34 -07:00
if (priv == NULL || privSz == NULL || *privSz < ED25519_PRV_KEY_SIZE ||
pub == NULL || pubSz == NULL || *pubSz < ED25519_PUB_KEY_SIZE) {
WOLFSSL_MSG("Bad arguments");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
tmpRNG = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (tmpRNG == NULL)
return SSL_FATAL_ERROR;
#endif
if (wc_InitRng(tmpRNG) == 0) {
rng = tmpRNG;
initTmpRng = 1;
}
else {
WOLFSSL_MSG("Bad RNG Init, trying global");
if (initGlobalRNG == 0)
WOLFSSL_MSG("Global RNG no Init");
else
rng = &globalRNG;
}
2016-06-27 10:53:34 -07:00
if (rng) {
ed25519_key key;
2016-06-27 10:53:34 -07:00
if (wc_ed25519_init(&key) != MP_OKAY)
WOLFSSL_MSG("wc_ed25519_init failed");
else if (wc_ed25519_make_key(rng, ED25519_KEY_SIZE, &key)!=MP_OKAY)
WOLFSSL_MSG("wc_ed25519_make_key failed");
/* export private key */
else if (wc_ed25519_export_key(&key, priv, privSz, pub, pubSz)!=MP_OKAY)
WOLFSSL_MSG("wc_ed25519_export_key failed");
else
ret = SSL_SUCCESS;
2016-06-27 10:53:34 -07:00
wc_ed25519_free(&key);
}
2016-06-27 10:53:34 -07:00
if (initTmpRng)
wc_FreeRng(tmpRNG);
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_SMALL_STACK
XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return ret;
2016-06-27 10:53:34 -07:00
#endif /* WOLFSSL_KEY_GEN */
}
2016-06-27 10:53:34 -07:00
/* return 1 if success, 0 if error
* input and output keys are little endian format
* priv is a buffer containing private and public part of key
*/
int wolfSSL_ED25519_sign(const unsigned char *msg, unsigned int msgSz,
const unsigned char *priv, unsigned int privSz,
unsigned char *sig, unsigned int *sigSz)
{
2016-06-27 10:53:34 -07:00
#ifndef WOLFSSL_KEY_GEN
WOLFSSL_MSG("No Key Gen built in");
(void) msg;
(void) msgSz;
(void) priv;
(void) privSz;
(void) sig;
(void) sigSz;
return SSL_FAILURE;
#else /* WOLFSSL_KEY_GEN */
ed25519_key key;
int ret = SSL_FAILURE;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_ED25519_sign");
2016-06-27 10:53:34 -07:00
if (priv == NULL || privSz != ED25519_PRV_KEY_SIZE ||
msg == NULL || sig == NULL || *sigSz < ED25519_SIG_SIZE) {
WOLFSSL_MSG("Bad arguments");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
/* import key */
if (wc_ed25519_init(&key) != MP_OKAY) {
WOLFSSL_MSG("wc_curve25519_init failed");
return ret;
}
if (wc_ed25519_import_private_key(priv, privSz/2,
priv+(privSz/2), ED25519_PUB_KEY_SIZE,
&key) != MP_OKAY){
WOLFSSL_MSG("wc_ed25519_import_private failed");
wc_ed25519_free(&key);
return ret;
}
2016-06-27 10:53:34 -07:00
if (wc_ed25519_sign_msg(msg, msgSz, sig, sigSz, &key) != MP_OKAY)
WOLFSSL_MSG("wc_curve25519_shared_secret_ex failed");
else
ret = SSL_SUCCESS;
2016-06-27 10:53:34 -07:00
wc_ed25519_free(&key);
2016-06-27 10:53:34 -07:00
return ret;
#endif /* WOLFSSL_KEY_GEN */
}
2016-06-27 10:53:34 -07:00
/* return 1 if success, 0 if error
* input and output keys are little endian format
* pub is a buffer containing public part of key
*/
int wolfSSL_ED25519_verify(const unsigned char *msg, unsigned int msgSz,
const unsigned char *pub, unsigned int pubSz,
const unsigned char *sig, unsigned int sigSz)
{
2016-06-27 10:53:34 -07:00
#ifndef WOLFSSL_KEY_GEN
WOLFSSL_MSG("No Key Gen built in");
(void) msg;
(void) msgSz;
(void) pub;
(void) pubSz;
(void) sig;
(void) sigSz;
return SSL_FAILURE;
#else /* WOLFSSL_KEY_GEN */
ed25519_key key;
int ret = SSL_FAILURE, check = 0;
2016-06-27 10:53:34 -07:00
WOLFSSL_ENTER("wolfSSL_ED25519_verify");
2016-06-27 10:53:34 -07:00
if (pub == NULL || pubSz != ED25519_PUB_KEY_SIZE ||
msg == NULL || sig == NULL || sigSz != ED25519_SIG_SIZE) {
WOLFSSL_MSG("Bad arguments");
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
/* import key */
if (wc_ed25519_init(&key) != MP_OKAY) {
WOLFSSL_MSG("wc_curve25519_init failed");
return ret;
}
if (wc_ed25519_import_public(pub, pubSz, &key) != MP_OKAY){
WOLFSSL_MSG("wc_ed25519_import_public failed");
wc_ed25519_free(&key);
return ret;
}
2016-06-27 10:53:34 -07:00
if ((ret = wc_ed25519_verify_msg((byte*)sig, sigSz, msg, msgSz,
&check, &key)) != MP_OKAY) {
WOLFSSL_MSG("wc_ed25519_verify_msg failed");
}
2016-06-27 10:53:34 -07:00
else if (!check)
WOLFSSL_MSG("wc_ed25519_verify_msg failed (signature invalid)");
else
ret = SSL_SUCCESS;
2016-06-27 10:53:34 -07:00
wc_ed25519_free(&key);
2016-06-27 10:53:34 -07:00
return ret;
#endif /* WOLFSSL_KEY_GEN */
}
2016-06-27 10:53:34 -07:00
#endif /* OPENSSL_EXTRA && HAVE_ED25519 */
2016-06-27 10:53:34 -07:00
#ifdef WOLFSSL_JNI
2016-06-27 10:53:34 -07:00
int wolfSSL_set_jobject(WOLFSSL* ssl, void* objPtr)
{
WOLFSSL_ENTER("wolfSSL_set_jobject");
if (ssl != NULL)
2016-06-27 10:53:34 -07:00
{
ssl->jObjectRef = objPtr;
return SSL_SUCCESS;
}
return SSL_FAILURE;
}
2016-06-27 10:53:34 -07:00
void* wolfSSL_get_jobject(WOLFSSL* ssl)
{
WOLFSSL_ENTER("wolfSSL_get_jobject");
if (ssl != NULL)
return ssl->jObjectRef;
return NULL;
}
2016-06-27 10:53:34 -07:00
#endif /* WOLFSSL_JNI */
#ifdef WOLFSSL_ASYNC_CRYPT
int wolfSSL_CTX_AsyncPoll(WOLFSSL_CTX* ctx, WOLF_EVENT** events, int maxEvents,
WOLF_EVENT_FLAG flags, int* eventCount)
{
if (ctx == NULL) {
2016-06-27 10:53:34 -07:00
return BAD_FUNC_ARG;
}
return wolfAsync_EventQueuePoll(&ctx->event_queue, NULL,
events, maxEvents, flags, eventCount);
}
int wolfSSL_AsyncPoll(WOLFSSL* ssl, WOLF_EVENT_FLAG flags)
{
int ret, eventCount = 0;
WOLF_EVENT* events[1];
if (ssl == NULL) {
return BAD_FUNC_ARG;
}
/* not filtering on "ssl", since its the asyncDev */
ret = wolfAsync_EventQueuePoll(&ssl->ctx->event_queue, NULL,
events, sizeof(events)/sizeof(events), flags, &eventCount);
if (ret == 0 && eventCount > 0) {
ret = 1; /* Success */
}
2016-06-27 10:53:34 -07:00
return ret;
}
#endif /* WOLFSSL_ASYNC_CRYPT */
2015-09-10 16:24:25 -07:00
#endif /* WOLFCRYPT_ONLY */