Add the ability to set OCSP_RESPID fields
authorMatt Caswell <matt@openssl.org>
Mon, 12 Sep 2016 16:39:55 +0000 (17:39 +0100)
committerMatt Caswell <matt@openssl.org>
Thu, 22 Sep 2016 08:28:07 +0000 (09:28 +0100)
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 <rsalz@openssl.org>
crypto/ocsp/ocsp_srv.c
doc/crypto/OCSP_response_status.pod
include/openssl/ocsp.h
util/libcrypto.num

index 443161cd257e4ab6a3ca918903103c623bda2e68..5d590bae85a81791f479e7b6180770a1b4f886b0 100644 (file)
@@ -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;
+}
index 08738d25155386a9d5eda001f05fbaba219f91d8..993fce20c63c5aa3ff5b41b1128b278bf2f752ea 100644 (file)
@@ -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<resp>. It returns
@@ -30,6 +34,17 @@ B<status> and optionally including basic response B<bs>.
 
 OCSP_RESPONSE_free() frees up OCSP response B<resp>.
 
+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<cert> 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<cert> 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<OCSP_request_add1_nonce(3)>
 L<OCSP_REQUEST_new(3)>
 L<OCSP_response_find_status(3)>
 L<OCSP_sendreq_new(3)>
+L<OCSP_RESPID_new(3)>
+L<OCSP_RESPID_free(3)>
+
+=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
 
index 119e59149db3eb4eaaebce9f62051609116bd05b..b5fdcc8f75947d16a29a34c5875c2a7fc59e99b8 100644 (file)
@@ -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);
 
index 8f19a3a95163662832118f660dfc4798c0a480c4..7289f6b549de5eac56a0ebc172e54802b12cbcd0 100644 (file)
@@ -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