Option to set current cert to server certificate.
authorDr. Stephen Henson <steve@openssl.org>
Fri, 21 Feb 2014 19:42:03 +0000 (19:42 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Fri, 21 Feb 2014 19:44:09 +0000 (19:44 +0000)
doc/ssl/SSL_CTX_add1_chain_cert.pod
ssl/s3_lib.c
ssl/ssl.h
ssl/ssl_lib.c

index 4e5a46d4bce0798c652e061c3059eef97795e964..e294afe253b866fa4b086e8a4d3d3804dd5cfdd6 100644 (file)
@@ -77,6 +77,14 @@ the first valid certificate or B<SSL_CERT_SET_NEXT> to set the next valid
 certificate after the current certificate. These two operations can be
 used to iterate over all certificates in an B<SSL_CTX> structure.
 
+SSL_set_current_cert() also supports the option B<SSL_CERT_SET_SERVER>.
+If B<ssl> is a server and has sent a certificate to a connected client
+this option sets that certificate to the current certificate and returns 1.
+If the negotiated ciphersuite is anonymous (and thus no certificate will
+be sent) 2 is returned and the current certificate is unchanged. If B<ssl>
+is not a server or a certificate has not been sent 0 is returned and
+the current certificate is unchanged.
+
 All these functions are implemented as macros. Those containing a B<1>
 increment the reference count of the supplied certificate or chain so it must
 be freed at some point after the operation. Those containing a B<0> do
index 4531f4655667586a7bb5837bb011ad943d2c6422..5a1b80bdc245dcd5a091b3c4bc76e2accc8d4be4 100644 (file)
@@ -3432,6 +3432,24 @@ long ssl3_ctrl(SSL *s, int cmd, long larg, void *parg)
                return ssl_cert_select_current(s->cert, (X509 *)parg);
 
        case SSL_CTRL_SET_CURRENT_CERT:
+               if (larg == SSL_CERT_SET_SERVER)
+                       {
+                       CERT_PKEY *cpk;
+                       const SSL_CIPHER *cipher;
+                       if (!s->server)
+                               return 0;
+                       cipher = s->s3->tmp.new_cipher;
+                       if (!cipher)
+                               return 0;
+                       /* No certificate for unauthenticated ciphersuites */
+                       if (cipher->algorithm_auth & SSL_aNULL)
+                               return 2;
+                       cpk = ssl_get_server_send_pkey(s);
+                       if (!cpk)
+                               return 0;
+                       s->cert->key = cpk;
+                       return 1;
+                       }
                return ssl_cert_set_current(s->cert, larg);
 
 #ifndef OPENSSL_NO_EC
index 2c3a9a34b70409146f5e0f6ae6a1038e3d71fe8a..2b87710954a92c98df16c72b52cf503ac027ef46 100644 (file)
--- a/ssl/ssl.h
+++ b/ssl/ssl.h
@@ -1949,6 +1949,7 @@ DECLARE_PEM_rw(SSL_SESSION, SSL_SESSION)
 
 #define SSL_CERT_SET_FIRST                     1
 #define SSL_CERT_SET_NEXT                      2
+#define SSL_CERT_SET_SERVER                    3
 
 #define DTLSv1_get_timeout(ssl, arg) \
        SSL_ctrl(ssl,DTLS_CTRL_GET_TIMEOUT,0, (void *)arg)
index d47f26865bf58f5f66e3f94c2c16aad7a200c535..eeed24de102d459853d29d98f61261ad1682e970 100644 (file)
@@ -2635,6 +2635,8 @@ CERT_PKEY *ssl_get_server_send_pkey(const SSL *s)
        int i;
 
        c = s->cert;
+       if (!s->s3 || !s->s3->tmp.new_cipher)
+               return NULL;
        ssl_set_cert_masks(c, s->s3->tmp.new_cipher);
 
 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL