Skip to content

Commit

Permalink
init sssd support
Browse files Browse the repository at this point in the history
- wolfSSL_BIO_reset: fix return
- CheckCertCRL_ex: return CRL_CERT_DATE_ERR instead of ASN_AFTER_DATE_E
- CheckCertCRL_ex: return most relevant error code
- i2d/d2i APIs: correct parameters handling and return codes
- Custom ASN1 structures: major refactor to make it much more versatile
- Use WOLFSSL_ASSERT_SIZEOF_GE where applicable
- wolfSSL_EVP_SignFinal: implement ecc
- wolfSSL_EVP_VerifyFinal: implement ecc
- OBJ_NAME_do_all: bring closer to OpenSSL functionality
- Implement:
  - OCSP_REQ_CTX API
  - d2i_ECPKParameters
  - wolfSSL_sk_insert
  - OCSP_parse_url
  - X509_STORE_set1_param
  - X509_get0_subject_key_id
  - X509_OBJECT_retrieve_by_subject
  • Loading branch information
julek-wolfssl committed Jul 24, 2024
1 parent a34ea32 commit 2945d70
Show file tree
Hide file tree
Showing 28 changed files with 2,437 additions and 628 deletions.
8 changes: 4 additions & 4 deletions src/bio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1704,13 +1704,13 @@ int wolfSSL_BIO_reset(WOLFSSL_BIO *bio)
if (XFSEEK(bio->ptr.fh, 0, XSEEK_SET) != 0)
return WOLFSSL_BIO_ERROR;
else
return 0;
return WOLFSSL_SUCCESS;
#endif

case WOLFSSL_BIO_BIO:
bio->rdIdx = 0;
bio->wrIdx = 0;
return 0;
return WOLFSSL_SUCCESS;

case WOLFSSL_BIO_MEMORY:
bio->rdIdx = 0;
Expand All @@ -1729,7 +1729,7 @@ int wolfSSL_BIO_reset(WOLFSSL_BIO *bio)
bio->mem_buf->max = 0;
}
}
return 0;
return WOLFSSL_SUCCESS;

#ifndef WOLFCRYPT_ONLY
case WOLFSSL_BIO_MD:
Expand All @@ -1740,7 +1740,7 @@ int wolfSSL_BIO_reset(WOLFSSL_BIO *bio)
wolfSSL_EVP_MD_CTX_init(bio->ptr.md_ctx);
wolfSSL_EVP_DigestInit(bio->ptr.md_ctx, md);
}
return 0;
return WOLFSSL_SUCCESS;
#endif /* WOLFCRYPT_ONLY */

default:
Expand Down
10 changes: 5 additions & 5 deletions src/crl.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ static int CheckCertCRLList(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial,
break;
}
else if (foundEntry == 0) {
ret = ASN_AFTER_DATE_E;
ret = CRL_CERT_DATE_ERR;
}
}
}
Expand Down Expand Up @@ -481,8 +481,8 @@ int CheckCertCRL_ex(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial,
if (foundEntry == 0) {
/* perform embedded lookup */
if (crl->crlIOCb) {
ret = crl->crlIOCb(crl, (const char*)extCrlInfo, extCrlInfoSz);
if (ret == WOLFSSL_CBIO_ERR_WANT_READ) {
int cbRet = crl->crlIOCb(crl, (const char*)extCrlInfo, extCrlInfoSz);
if (cbRet == WOLFSSL_CBIO_ERR_WANT_READ) {
ret = OCSP_WANT_READ;
}
else if (ret >= 0) {
Expand All @@ -505,9 +505,9 @@ int CheckCertCRL_ex(WOLFSSL_CRL* crl, byte* issuerHash, byte* serial,
/* When not set the folder or not use hash_dir, do nothing. */
if ((foundEntry == 0) && (ret != WC_NO_ERR_TRACE(OCSP_WANT_READ))) {
if (crl->cm != NULL && crl->cm->x509_store_p != NULL) {
ret = LoadCertByIssuer(crl->cm->x509_store_p,
int loadRet = LoadCertByIssuer(crl->cm->x509_store_p,
(WOLFSSL_X509_NAME*)issuerName, X509_LU_CRL);
if (ret == WOLFSSL_SUCCESS) {
if (loadRet == WOLFSSL_SUCCESS) {
/* try again */
ret = CheckCertCRLList(crl, issuerHash, serial, serialSz,
serialHash, &foundEntry);
Expand Down
2 changes: 2 additions & 0 deletions src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -4572,6 +4572,8 @@ void FreeX509(WOLFSSL_X509* x509)
x509->authKeyId = NULL;
XFREE(x509->subjKeyId, x509->heap, DYNAMIC_TYPE_X509_EXT);
x509->subjKeyId = NULL;
wolfSSL_ASN1_STRING_free(x509->subjKeyIdStr);
x509->subjKeyIdStr = NULL;
if (x509->authInfo != NULL) {
XFREE(x509->authInfo, x509->heap, DYNAMIC_TYPE_X509_EXT);
x509->authInfo = NULL;
Expand Down
202 changes: 202 additions & 0 deletions src/ocsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,9 @@ int wolfSSL_i2d_OCSP_REQUEST(OcspRequest* request, unsigned char** data)
{
int size;

if (request == NULL)
return BAD_FUNC_ARG;

size = EncodeOcspRequest(request, NULL, 0);
if (size <= 0 || data == NULL)
return size;
Expand Down Expand Up @@ -1392,6 +1395,205 @@ WOLFSSL_OCSP_SINGLERESP* wolfSSL_OCSP_resp_get0(WOLFSSL_OCSP_BASICRESP *bs, int
return single;
}

/*******************************************************************************
* START OF WOLFSSL_OCSP_REQ_CTX API
******************************************************************************/

enum ocspReqStates {
ORS_INVALID = 0,
ORS_HEADER_ADDED,
ORS_REQ_DONE
};

WOLFSSL_OCSP_REQ_CTX* wolfSSL_OCSP_REQ_CTX_new(WOLFSSL_BIO *bio, int maxline)
{
WOLFSSL_OCSP_REQ_CTX* ret = NULL;

WOLFSSL_ENTER("wolfSSL_OCSP_REQ_CTX_new");

if (maxline <= 0)
maxline = OCSP_MAX_REQUEST_SZ;

ret = (WOLFSSL_OCSP_REQ_CTX*)XMALLOC(sizeof(*ret), NULL,
DYNAMIC_TYPE_OPENSSL);
if (ret != NULL) {
XMEMSET(ret, 0, sizeof(*ret));
ret->buf = (byte*)XMALLOC(maxline, NULL, DYNAMIC_TYPE_OPENSSL);
if (ret->buf == NULL)
goto error;
ret->resp = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
ret->bufLen = maxline;
ret->bio = bio;
}

return ret;
error:
wolfSSL_OCSP_REQ_CTX_free(ret);
return NULL;
}

void wolfSSL_OCSP_REQ_CTX_free(WOLFSSL_OCSP_REQ_CTX *ctx)
{
WOLFSSL_ENTER("wolfSSL_OCSP_REQ_CTX_free");
if (ctx != NULL) {
if (ctx->buf != NULL)
XFREE(ctx->buf, NULL, DYNAMIC_TYPE_OPENSSL);
if (ctx->resp != NULL)
wolfSSL_BIO_free(ctx->resp);
XFREE(ctx, NULL, DYNAMIC_TYPE_OPENSSL);
}
}

WOLFSSL_OCSP_REQ_CTX* wolfSSL_OCSP_sendreq_new(WOLFSSL_BIO *bio,
const char *path, OcspRequest *req, int maxline)
{
WOLFSSL_OCSP_REQ_CTX* ret = NULL;

WOLFSSL_ENTER("wolfSSL_OCSP_sendreq_new");

ret = wolfSSL_OCSP_REQ_CTX_new(bio, maxline);
if (ret == NULL)
return NULL;

if (wolfSSL_OCSP_REQ_CTX_http(ret, "POST", path) != WOLFSSL_SUCCESS)
goto error;

if (req != NULL &&
wolfSSL_OCSP_REQ_CTX_set1_req(ret, req) != WOLFSSL_SUCCESS)
goto error;

return ret;
error:
wolfSSL_OCSP_REQ_CTX_free(ret);
return NULL;
}

int wolfSSL_OCSP_REQ_CTX_add1_header(WOLFSSL_OCSP_REQ_CTX *ctx,
const char *name, const char *value)
{
WOLFSSL_ENTER("wolfSSL_OCSP_REQ_CTX_add1_header");

if (name == NULL) {
WOLFSSL_MSG("Bad parameter");
return WOLFSSL_FAILURE;
}
if (wolfSSL_BIO_puts(ctx->resp, name) <= 0) {
WOLFSSL_MSG("wolfSSL_BIO_puts error");
return WOLFSSL_FAILURE;
}
if (value != NULL) {
if (wolfSSL_BIO_write(ctx->resp, ": ", 2) != 2) {
WOLFSSL_MSG("wolfSSL_BIO_write error");
return WOLFSSL_FAILURE;
}
if (wolfSSL_BIO_puts(ctx->resp, value) <= 0) {
WOLFSSL_MSG("wolfSSL_BIO_puts error");
return WOLFSSL_FAILURE;
}
}
if (wolfSSL_BIO_write(ctx->resp, "\r\n", 2) != 2) {
WOLFSSL_MSG("wolfSSL_BIO_write error");
return WOLFSSL_FAILURE;
}

ctx->state = ORS_HEADER_ADDED;

return WOLFSSL_SUCCESS;
}

int wolfSSL_OCSP_REQ_CTX_http(WOLFSSL_OCSP_REQ_CTX *ctx, const char *op,
const char *path)
{
static const char http_hdr[] = "%s %s HTTP/1.0\r\n";

WOLFSSL_ENTER("wolfSSL_OCSP_REQ_CTX_http");

if (ctx == NULL || op == NULL) {
WOLFSSL_MSG("Bad parameter");
return WOLFSSL_FAILURE;
}

if (path == NULL)
path = "/";

if (wolfSSL_BIO_printf(ctx->resp, http_hdr, op, path) <= 0) {
WOLFSSL_MSG("WOLFSSL_OCSP_REQ_CTX: wolfSSL_BIO_printf error");
return WOLFSSL_FAILURE;
}

ctx->state = ORS_HEADER_ADDED;

return WOLFSSL_SUCCESS;
}

int wolfSSL_OCSP_REQ_CTX_set1_req(WOLFSSL_OCSP_REQ_CTX *ctx, OcspRequest *req)
{
static const char req_hdr[] =
"Content-Type: application/ocsp-request\r\n"
"Content-Length: %d\r\n\r\n";
/* Should be enough to hold Content-Length */
char req_hdr_buf[sizeof(req_hdr) + 10];
int req_hdr_buf_len;
int req_len = wolfSSL_i2d_OCSP_REQUEST(req, NULL);

WOLFSSL_ENTER("wolfSSL_OCSP_REQ_CTX_set1_req");

if (ctx == NULL || req == NULL) {
WOLFSSL_MSG("Bad parameters");
return WOLFSSL_FAILURE;
}

if (req_len <= 0) {
WOLFSSL_MSG("wolfSSL_OCSP_REQ_CTX_set1_req: request len error");
return WOLFSSL_FAILURE;
}

req_hdr_buf_len =
XSNPRINTF(req_hdr_buf, sizeof(req_hdr_buf), req_hdr, req_len);
if (req_hdr_buf_len >= (int)sizeof(req_hdr_buf)) {
WOLFSSL_MSG("wolfSSL_OCSP_REQ_CTX_set1_req: request too long");
return WOLFSSL_FAILURE;
}

if (wolfSSL_BIO_write(ctx->resp, req_hdr_buf, req_hdr_buf_len) <= 0) {
WOLFSSL_MSG("wolfSSL_OCSP_REQ_CTX_set1_req: wolfSSL_BIO_write error");
return WOLFSSL_FAILURE;
}

if (wolfSSL_i2d_OCSP_REQUEST_bio(ctx->resp, req) <= 0) {
WOLFSSL_MSG("wolfSSL_OCSP_REQ_CTX_set1_req: request i2d error");
return WOLFSSL_FAILURE;
}

ctx->state = ORS_REQ_DONE;

return WOLFSSL_SUCCESS;
}


int wolfSSL_OCSP_REQ_CTX_nbio(WOLFSSL_OCSP_REQ_CTX *ctx)
{
WOLFSSL_ENTER("wolfSSL_OCSP_REQ_CTX_nbio");
(void)ctx;
// TODO implement
WOLFSSL_MSG("Error wolfSSL_OCSP_REQ_CTX_nbio not yet implemented");
return WOLFSSL_FAILURE;
}

int wolfSSL_OCSP_sendreq_nbio(OcspResponse **presp, WOLFSSL_OCSP_REQ_CTX *ctx)
{
WOLFSSL_ENTER("wolfSSL_OCSP_sendreq_nbio");
(void)presp;
(void)ctx;
// TODO implement
WOLFSSL_MSG("Error wolfSSL_OCSP_sendreq_nbio not yet implemented");
return WOLFSSL_FAILURE;
}

/*******************************************************************************
* END OF WOLFSSL_OCSP_REQ_CTX API
******************************************************************************/

#endif /* OPENSSL_ALL || APACHE_HTTPD || WOLFSSL_HAPROXY */

#ifdef OPENSSL_EXTRA
Expand Down
19 changes: 17 additions & 2 deletions src/pk.c
Original file line number Diff line number Diff line change
Expand Up @@ -9176,13 +9176,19 @@ void wolfSSL_EC_GROUP_free(WOLFSSL_EC_GROUP *group)
* @return NULL on error.
*/
static WOLFSSL_EC_GROUP* wolfssl_ec_group_d2i(WOLFSSL_EC_GROUP** group,
const unsigned char* in, long inSz)
const unsigned char** in_pp, long inSz)
{
int err = 0;
WOLFSSL_EC_GROUP* ret = NULL;
word32 idx = 0;
word32 oid = 0;
int id = 0;
const unsigned char* in;

if (in_pp == NULL || *in_pp == NULL)
return NULL;

in = *in_pp;

/* Use the group passed in. */
if ((group != NULL) && (*group != NULL)) {
Expand Down Expand Up @@ -9231,6 +9237,9 @@ static WOLFSSL_EC_GROUP* wolfssl_ec_group_d2i(WOLFSSL_EC_GROUP** group,
}
ret = NULL;
}
else {
*in_pp += idx;
}
return ret;
}

Expand Down Expand Up @@ -9262,7 +9271,8 @@ WOLFSSL_EC_GROUP* wolfSSL_PEM_read_bio_ECPKParameters(WOLFSSL_BIO* bio,
}
if (!err) {
/* Create EC group from DER encoding. */
ret = wolfssl_ec_group_d2i(group, der->buffer, der->length);
const byte** p = (const byte**)&der->buffer;
ret = wolfssl_ec_group_d2i(group, p, der->length);
if (ret == NULL) {
WOLFSSL_ERROR_MSG("Error loading DER buffer into WOLFSSL_EC_GROUP");
}
Expand All @@ -9273,6 +9283,11 @@ WOLFSSL_EC_GROUP* wolfSSL_PEM_read_bio_ECPKParameters(WOLFSSL_BIO* bio,
return ret;
}

WOLFSSL_EC_GROUP *wolfSSL_d2i_ECPKParameters(WOLFSSL_EC_GROUP **out,
const unsigned char **in, long len)
{
return wolfssl_ec_group_d2i(out, in, len);
}
#endif /* !NO_BIO */

#if defined(OPENSSL_ALL) && !defined(NO_CERTS)
Expand Down
Loading

0 comments on commit 2945d70

Please sign in to comment.