From: Matt Caswell Date: Mon, 12 Sep 2016 16:39:55 +0000 (+0100) Subject: Add the ability to set OCSP_RESPID fields X-Git-Tag: OpenSSL_1_1_1-pre1~3473 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=e12c0beb5a652ba0c3a71e633a77fafbb4f86aa4;p=oweals%2Fopenssl.git Add the ability to set OCSP_RESPID fields OCSP_RESPID was made opaque in 1.1.0, but no accessors were provided for setting the name/key value for the OCSP_RESPID. Reviewed-by: Rich Salz --- diff --git a/crypto/ocsp/ocsp_srv.c b/crypto/ocsp/ocsp_srv.c index 443161cd25..5d590bae85 100644 --- a/crypto/ocsp/ocsp_srv.c +++ b/crypto/ocsp/ocsp_srv.c @@ -193,17 +193,10 @@ int OCSP_basic_sign(OCSP_BASICRESP *brsp, rid = &brsp->tbsResponseData.responderId; if (flags & OCSP_RESPID_KEY) { - unsigned char md[SHA_DIGEST_LENGTH]; - X509_pubkey_digest(signer, EVP_sha1(), md, NULL); - if ((rid->value.byKey = ASN1_OCTET_STRING_new()) == NULL) + if (!OCSP_RESPID_set_by_key(rid, signer)) goto err; - if (!(ASN1_OCTET_STRING_set(rid->value.byKey, md, SHA_DIGEST_LENGTH))) - goto err; - rid->type = V_OCSP_RESPID_KEY; - } else { - if (!X509_NAME_set(&rid->value.byName, X509_get_subject_name(signer))) - goto err; - rid->type = V_OCSP_RESPID_NAME; + } else if (!OCSP_RESPID_set_by_name(rid, signer)) { + goto err; } if (!(flags & OCSP_NOTIME) && @@ -222,3 +215,37 @@ int OCSP_basic_sign(OCSP_BASICRESP *brsp, err: return 0; } + +int OCSP_RESPID_set_by_name(OCSP_RESPID *respid, X509 *cert) +{ + if (!X509_NAME_set(&respid->value.byName, X509_get_subject_name(cert))) + return 0; + + respid->type = V_OCSP_RESPID_NAME; + + return 1; +} + +int OCSP_RESPID_set_by_key(OCSP_RESPID *respid, X509 *cert) +{ + ASN1_OCTET_STRING *byKey = NULL; + unsigned char md[SHA_DIGEST_LENGTH]; + + /* RFC2560 requires SHA1 */ + if (!X509_pubkey_digest(cert, EVP_sha1(), md, NULL)) + return 0; + + byKey = ASN1_OCTET_STRING_new(); + if (byKey == NULL) + return 0; + + if (!(ASN1_OCTET_STRING_set(respid->value.byKey, md, SHA_DIGEST_LENGTH))) { + ASN1_OCTET_STRING_free(byKey); + return 0; + } + + respid->type = V_OCSP_RESPID_KEY; + respid->value.byKey = byKey; + + return 1; +} diff --git a/doc/crypto/OCSP_response_status.pod b/doc/crypto/OCSP_response_status.pod index 08738d2515..993fce20c6 100644 --- a/doc/crypto/OCSP_response_status.pod +++ b/doc/crypto/OCSP_response_status.pod @@ -3,7 +3,8 @@ =head1 NAME OCSP_response_status, OCSP_response_get1_basic, OCSP_response_create, -OCSP_RESPONSE_free - OCSP response functions +OCSP_RESPONSE_free, OCSP_RESPID_set_by_name, +OCSP_RESPID_set_by_key - OCSP response functions =head1 SYNOPSIS @@ -14,6 +15,9 @@ OCSP_RESPONSE_free - OCSP response functions OCSP_RESPONSE *OCSP_response_create(int status, OCSP_BASICRESP *bs); void OCSP_RESPONSE_free(OCSP_RESPONSE *resp); + int OCSP_RESPID_set_by_name(OCSP_RESPID *respid, X509 *cert); + int OCSP_RESPID_set_by_key(OCSP_RESPID *respid, X509 *cert); + =head1 DESCRIPTION OCSP_response_status() returns the OCSP response status of B. It returns @@ -30,6 +34,17 @@ B and optionally including basic response B. OCSP_RESPONSE_free() frees up OCSP response B. +OCSP_RESPID_set_by_name() sets the name of the OCSP_RESPID to be the same as the +subject name in the supplied X509 certificate B for the OCSP responder. + +OCSP_RESPID_set_by_key() sets the key of the OCSP_RESPID to be the same as the +key in the supplied X509 certificate B for the OCSP responder. The key is +stored as a SHA1 hash. + +Note that an OCSP_RESPID can only have one of the name, or the key set. Calling +OCSP_RESPID_set_by_name() or OCSP_RESPID_set_by_key() will clear any existing +setting. + =head1 RETURN VALUES OCSP_RESPONSE_status() returns a status value. @@ -42,6 +57,9 @@ if an error occurred. OCSP_RESPONSE_free() does not return a value. +OCSP_RESPID_set_by_name() and OCSP_RESPID_set_by_key() return 1 on success or 0 +on failure. + =head1 NOTES OCSP_response_get1_basic() is only called if the status of a response is @@ -55,6 +73,13 @@ L L L L +L +L + +=head1 HISTORY + +The OCSP_RESPID_set_by_name() and OCSP_RESPID_set_by_key() functions were added +in OpenSSL version 1.1.0a. =head1 COPYRIGHT diff --git a/include/openssl/ocsp.h b/include/openssl/ocsp.h index 119e59149d..b5fdcc8f75 100644 --- a/include/openssl/ocsp.h +++ b/include/openssl/ocsp.h @@ -259,6 +259,8 @@ int OCSP_basic_add1_cert(OCSP_BASICRESP *resp, X509 *cert); int OCSP_basic_sign(OCSP_BASICRESP *brsp, X509 *signer, EVP_PKEY *key, const EVP_MD *dgst, STACK_OF(X509) *certs, unsigned long flags); +int OCSP_RESPID_set_by_name(OCSP_RESPID *respid, X509 *cert); +int OCSP_RESPID_set_by_key(OCSP_RESPID *respid, X509 *cert); X509_EXTENSION *OCSP_crlID_new(const char *url, long *n, char *tim); diff --git a/util/libcrypto.num b/util/libcrypto.num index 8f19a3a951..7289f6b549 100644 --- a/util/libcrypto.num +++ b/util/libcrypto.num @@ -4203,3 +4203,5 @@ ECPKPARAMETERS_free 4153 1_1_0 EXIST::FUNCTION:EC ECPARAMETERS_free 4154 1_1_0 EXIST::FUNCTION:EC ECPKPARAMETERS_new 4155 1_1_0 EXIST::FUNCTION:EC ECPARAMETERS_new 4156 1_1_0 EXIST::FUNCTION:EC +OCSP_RESPID_set_by_name 4157 1_1_0a EXIST::FUNCTION:OCSP +OCSP_RESPID_set_by_key 4158 1_1_0a EXIST::FUNCTION:OCSP