Changeset 246605 in webkit


Ignore:
Timestamp:
Jun 19, 2019 12:28:56 PM (5 years ago)
Author:
achristensen@apple.com
Message:

Add a unit test for client certificate authentication
https://bugs.webkit.org/show_bug.cgi?id=197800

Reviewed by Youenn Fablet.

Source/WebKit:

  • Shared/cf/ArgumentCodersCF.cpp:

Move SPI declarations to SecuritySPI.h.

Source/WTF:

  • wtf/spi/cocoa/SecuritySPI.h:

Move declarations from ArgumentCodersCF.cpp so they can be shared.

Tools:

Make better abstractions for reading and writing from/to TCPServer.
Add a unit test that causes a client certificate authentication challenge to happen.

  • TestWebKitAPI/TCPServer.cpp:

(TestWebKitAPI::TCPServer::TCPServer):
(TestWebKitAPI::TCPServer::read):
(TestWebKitAPI::TCPServer::write):
(TestWebKitAPI::TCPServer::respondWithChallengeThenOK):
(TestWebKitAPI::TCPServer::respondWithOK):

  • TestWebKitAPI/TCPServer.h:
  • TestWebKitAPI/Tests/WebKitCocoa/Challenge.mm:

(credentialWithIdentity):
(-[ChallengeDelegate webView:didReceiveAuthenticationChallenge:completionHandler:]):
(TEST):
(-[ClientCertificateDelegate webView:didFinishNavigation:]):
(-[ClientCertificateDelegate webView:didReceiveAuthenticationChallenge:completionHandler:]):
(-[ClientCertificateDelegate challengeCount]):
(TestWebKitAPI::TEST):
(respondWithChallengeThenOK): Deleted.
(credentialWithIdentityAndKeychainPath): Deleted.

  • TestWebKitAPI/Tests/WebKitCocoa/PDFLinkReferrer.mm:

(TEST):

  • TestWebKitAPI/Tests/WebKitCocoa/Proxy.mm:

(TestWebKitAPI::TEST):

  • TestWebKitAPI/Tests/WebKitCocoa/WKNavigationResponse.mm:

(TEST):
(readRequest): Deleted.
(writeResponse): Deleted.

  • TestWebKitAPI/Tests/WebKitCocoa/WKWebsiteDatastore.mm:

(TestWebKitAPI::TEST):
(TestWebKitAPI::respondWithChallengeThenOK): Deleted.

Location:
trunk
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r246596 r246605  
     12019-06-19  Alex Christensen  <achristensen@webkit.org>
     2
     3        Add a unit test for client certificate authentication
     4        https://bugs.webkit.org/show_bug.cgi?id=197800
     5
     6        Reviewed by Youenn Fablet.
     7
     8        * wtf/spi/cocoa/SecuritySPI.h:
     9        Move declarations from ArgumentCodersCF.cpp so they can be shared.
     10
    1112019-06-19  Adrian Perez de Castro  <aperez@igalia.com>
    212
  • trunk/Source/WTF/wtf/spi/cocoa/SecuritySPI.h

    r245468 r246605  
    2828#if USE(APPLE_INTERNAL_SDK)
    2929
     30#include <Security/SecAccessControlPriv.h>
    3031#include <Security/SecCertificatePriv.h>
     32#include <Security/SecIdentityPriv.h>
     33#include <Security/SecKeyPriv.h>
    3134#include <Security/SecTask.h>
    3235#include <Security/SecTrustPriv.h>
     
    6871SecTaskRef SecTaskCreateWithAuditToken(CFAllocatorRef, audit_token_t);
    6972SecTaskRef SecTaskCreateFromSelf(CFAllocatorRef);
    70 CFTypeRef SecTaskCopyValueForEntitlement(SecTaskRef, CFStringRef entitlement, CFErrorRef *);
     73CFTypeRef SecTaskCopyValueForEntitlement(SecTaskRef, CFStringRef entitlement, CFErrorRef*);
     74SecIdentityRef SecIdentityCreate(CFAllocatorRef, SecCertificateRef, SecKeyRef);
     75OSStatus SecKeyFindWithPersistentRef(CFDataRef persistentRef, SecKeyRef* lookedUpData);
     76SecAccessControlRef SecAccessControlCreateFromData(CFAllocatorRef, CFDataRef, CFErrorRef*);
     77CFDataRef SecAccessControlCopyData(SecAccessControlRef);
    7178
    7279#if PLATFORM(MAC)
  • trunk/Source/WebKit/ChangeLog

    r246598 r246605  
     12019-06-19  Alex Christensen  <achristensen@webkit.org>
     2
     3        Add a unit test for client certificate authentication
     4        https://bugs.webkit.org/show_bug.cgi?id=197800
     5
     6        Reviewed by Youenn Fablet.
     7
     8        * Shared/cf/ArgumentCodersCF.cpp:
     9        Move SPI declarations to SecuritySPI.h.
     10
    1112019-06-19  Alex Christensen  <achristensen@webkit.org>
    212
  • trunk/Source/WebKit/Shared/cf/ArgumentCodersCF.cpp

    r245911 r246605  
    4040#if USE(FOUNDATION)
    4141#import <Foundation/Foundation.h>
    42 #endif
    43 
    44 #if USE(APPLE_INTERNAL_SDK)
    45 #include <Security/SecIdentityPriv.h>
    46 #endif
    47 
    48 extern "C" SecIdentityRef SecIdentityCreate(CFAllocatorRef allocator, SecCertificateRef certificate, SecKeyRef privateKey);
    49 
    50 #if PLATFORM(IOS_FAMILY)
    51 #if USE(APPLE_INTERNAL_SDK)
    52 #include <Security/SecKeyPriv.h>
    53 #endif
    54 
    55 extern "C" OSStatus SecKeyFindWithPersistentRef(CFDataRef persistentRef, SecKeyRef* lookedUpData);
    56 #endif
    57 
    58 #if HAVE(SEC_ACCESS_CONTROL)
    59 #if USE(APPLE_INTERNAL_SDK)
    60 #include <Security/SecAccessControlPriv.h>
    61 #endif
    62 
    63 extern "C" SecAccessControlRef SecAccessControlCreateFromData(CFAllocatorRef allocator, CFDataRef data, CFErrorRef *error);
    64 extern "C" CFDataRef SecAccessControlCopyData(SecAccessControlRef access_control);
    6542#endif
    6643
  • trunk/Tools/ChangeLog

    r246590 r246605  
     12019-06-19  Alex Christensen  <achristensen@webkit.org>
     2
     3        Add a unit test for client certificate authentication
     4        https://bugs.webkit.org/show_bug.cgi?id=197800
     5
     6        Reviewed by Youenn Fablet.
     7
     8        Make better abstractions for reading and writing from/to TCPServer.
     9        Add a unit test that causes a client certificate authentication challenge to happen.
     10
     11        * TestWebKitAPI/TCPServer.cpp:
     12        (TestWebKitAPI::TCPServer::TCPServer):
     13        (TestWebKitAPI::TCPServer::read):
     14        (TestWebKitAPI::TCPServer::write):
     15        (TestWebKitAPI::TCPServer::respondWithChallengeThenOK):
     16        (TestWebKitAPI::TCPServer::respondWithOK):
     17        * TestWebKitAPI/TCPServer.h:
     18        * TestWebKitAPI/Tests/WebKitCocoa/Challenge.mm:
     19        (credentialWithIdentity):
     20        (-[ChallengeDelegate webView:didReceiveAuthenticationChallenge:completionHandler:]):
     21        (TEST):
     22        (-[ClientCertificateDelegate webView:didFinishNavigation:]):
     23        (-[ClientCertificateDelegate webView:didReceiveAuthenticationChallenge:completionHandler:]):
     24        (-[ClientCertificateDelegate challengeCount]):
     25        (TestWebKitAPI::TEST):
     26        (respondWithChallengeThenOK): Deleted.
     27        (credentialWithIdentityAndKeychainPath): Deleted.
     28        * TestWebKitAPI/Tests/WebKitCocoa/PDFLinkReferrer.mm:
     29        (TEST):
     30        * TestWebKitAPI/Tests/WebKitCocoa/Proxy.mm:
     31        (TestWebKitAPI::TEST):
     32        * TestWebKitAPI/Tests/WebKitCocoa/WKNavigationResponse.mm:
     33        (TEST):
     34        (readRequest): Deleted.
     35        (writeResponse): Deleted.
     36        * TestWebKitAPI/Tests/WebKitCocoa/WKWebsiteDatastore.mm:
     37        (TestWebKitAPI::TEST):
     38        (TestWebKitAPI::respondWithChallengeThenOK): Deleted.
     39
    1402019-06-19  Michael Catanzaro  <mcatanzaro@igalia.com>
    241
  • trunk/Tools/TestWebKitAPI/TCPServer.cpp

    r246173 r246605  
    3131#include <unistd.h>
    3232#include <wtf/Optional.h>
     33#include <wtf/text/Base64.h>
    3334
    3435#if HAVE(SSL)
     
    4041struct EVP_PKEY;
    4142struct SSL_METHOD;
     43struct X509_STORE_CTX {
     44    void* unused;
     45    X509* cert;
     46};
    4247struct pem_password_cb;
    4348int BIO_free(BIO*);
     
    5863int SSL_accept(SSL*);
    5964int SSL_set_fd(SSL*, int);
     65void SSL_CTX_set_verify(SSL_CTX*, int, int (*)(int, X509_STORE_CTX*));
     66void SSL_CTX_set_cert_verify_callback(SSL_CTX*, int (*)(X509_STORE_CTX*, void*), void*);
     67int SSL_get_error(const SSL*, int);
     68int SSL_read(SSL*, void*, int);
     69int SSL_write(SSL*, const void*, int);
     70int i2d_X509(X509*, unsigned char**);
     71void OPENSSL_free(void*);
     72#define SSL_VERIFY_PEER 0x01
     73#define SSL_VERIFY_FAIL_IF_NO_PEER_CERT 0x02
    6074
    6175} // extern "C"
     
    96110    }
    97111};
     112template<> struct deleter<uint8_t[]> {
     113    void operator()(uint8_t* buffer)
     114    {
     115        OPENSSL_free(buffer);
     116    }
     117};
    98118#endif // HAVE(SSL)
    99119
     
    107127TCPServer::TCPServer(Protocol protocol, Function<void(SSL*)>&& secureConnectionHandler)
    108128{
    109     auto startSecureConnection = [secureConnectionHandler = WTFMove(secureConnectionHandler)] (Socket socket) {
     129    auto startSecureConnection = [secureConnectionHandler = WTFMove(secureConnectionHandler), protocol] (Socket socket) {
    110130        SSL_library_init();
    111131
     
    135155        SSL_CTX_use_certificate(ctx.get(), certX509.get());
    136156
     157        if (protocol == Protocol::HTTPSWithClientCertificateRequest) {
     158            SSL_CTX_set_verify(ctx.get(), SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, nullptr);
     159            SSL_CTX_set_cert_verify_callback(ctx.get(), [] (X509_STORE_CTX* store_ctx, void*) -> int {
     160                uint8_t* bufferPointer = nullptr;
     161                auto length = i2d_X509(store_ctx->cert, &bufferPointer);
     162                std::unique_ptr<uint8_t[], deleter<uint8_t[]>> buffer(bufferPointer);
     163                auto expectedCert = testCertificate();
     164                EXPECT_EQ(static_cast<int>(expectedCert.size()), length);
     165                for (int i = 0; i < length; ++i)
     166                    EXPECT_EQ(buffer.get()[i], expectedCert[i]);
     167                return 1;
     168            }, nullptr);
     169        }
     170
    137171        // This is a test key from BoringSSL.
    138172        char kKeyPEM[] =
     
    170204    switch (protocol) {
    171205    case Protocol::HTTPS:
     206    case Protocol::HTTPSWithClientCertificateRequest:
    172207        m_connectionHandler = WTFMove(startSecureConnection);
    173208        break;
     
    246281}
    247282
     283template<> Vector<uint8_t> TCPServer::read(Socket socket)
     284{
     285    uint8_t buffer[1000];
     286    auto bytesRead = ::read(socket, buffer, sizeof(buffer));
     287    ASSERT_UNUSED(bytesRead, bytesRead > 0);
     288    ASSERT(static_cast<size_t>(bytesRead) < sizeof(buffer));
     289
     290    Vector<uint8_t> vector;
     291    vector.append(buffer, bytesRead);
     292    return vector;
     293}
     294
     295template<> void TCPServer::write(Socket socket, const void* response, size_t length)
     296{
     297    auto bytesWritten = ::write(socket, response, length);
     298    EXPECT_EQ(static_cast<size_t>(bytesWritten), length);
     299}
     300
     301template<> Vector<uint8_t> TCPServer::read(SSL* ssl)
     302{
     303    uint8_t buffer[1000];
     304    auto bytesRead = SSL_read(ssl, buffer, sizeof(buffer));
     305    ASSERT_UNUSED(bytesRead, bytesRead > 0);
     306    ASSERT(static_cast<size_t>(bytesRead) < sizeof(buffer));
     307
     308    Vector<uint8_t> vector;
     309    vector.append(buffer, bytesRead);
     310    return vector;
     311}
     312
     313template<> void TCPServer::write(SSL* ssl, const void* response, size_t length)
     314{
     315    auto bytesWritten = SSL_write(ssl, response, length);
     316    EXPECT_EQ(static_cast<size_t>(bytesWritten), length);
     317}
     318
     319void TCPServer::respondWithChallengeThenOK(Socket socket)
     320{
     321    read(socket);
     322   
     323    const char* challengeHeader =
     324    "HTTP/1.1 401 Unauthorized\r\n"
     325    "Date: Sat, 23 Mar 2019 06:29:01 GMT\r\n"
     326    "Content-Length: 0\r\n"
     327    "WWW-Authenticate: Basic realm=\"testrealm\"\r\n\r\n";
     328    write(socket, challengeHeader, strlen(challengeHeader));
     329   
     330    read(socket);
     331   
     332    const char* responseHeader =
     333    "HTTP/1.1 200 OK\r\n"
     334    "Content-Length: 13\r\n\r\n"
     335    "Hello, World!";
     336    write(socket, responseHeader, strlen(responseHeader));
     337}
     338
     339#if HAVE(SSL)
     340void TCPServer::respondWithOK(SSL* ssl)
     341{
     342    read(ssl);
     343   
     344    const char* reply = ""
     345    "HTTP/1.1 200 OK\r\n"
     346    "Content-Length: 34\r\n\r\n"
     347    "<script>alert('success!')</script>";
     348    write(ssl, reply, strlen(reply));
     349}
     350#endif
     351
     352Vector<uint8_t> TCPServer::testCertificate()
     353{
     354    // Certificate and private key were generated by running this command:
     355    // openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
     356    // and entering this information:
     357    /*
     358     Country Name (2 letter code) []:US
     359     State or Province Name (full name) []:New Mexico
     360     Locality Name (eg, city) []:Santa Fe
     361     Organization Name (eg, company) []:Self
     362     Organizational Unit Name (eg, section) []:Myself
     363     Common Name (eg, fully qualified host name) []:Me
     364     Email Address []:me@example.com
     365     */
     366   
     367    String pemEncodedCertificate(""
     368    "MIIFgDCCA2gCCQCKHiPRU5MQuDANBgkqhkiG9w0BAQsFADCBgTELMAkGA1UEBhMC"
     369    "VVMxEzARBgNVBAgMCk5ldyBNZXhpY28xETAPBgNVBAcMCFNhbnRhIEZlMQ0wCwYD"
     370    "VQQKDARTZWxmMQ8wDQYDVQQLDAZNeXNlbGYxCzAJBgNVBAMMAk1lMR0wGwYJKoZI"
     371    "hvcNAQkBFg5tZUBleGFtcGxlLmNvbTAeFw0xOTAzMjMwNTUwMTRaFw0yMDAzMjIw"
     372    "NTUwMTRaMIGBMQswCQYDVQQGEwJVUzETMBEGA1UECAwKTmV3IE1leGljbzERMA8G"
     373    "A1UEBwwIU2FudGEgRmUxDTALBgNVBAoMBFNlbGYxDzANBgNVBAsMBk15c2VsZjEL"
     374    "MAkGA1UEAwwCTWUxHTAbBgkqhkiG9w0BCQEWDm1lQGV4YW1wbGUuY29tMIICIjAN"
     375    "BgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA3rhN4SPg8VY/PtGDNKY3T9JISgby"
     376    "8YGMJx0vO+YZFZm3G3fsTUsyvDyEHwqp5abCZRB/By1PwWkNrfxn/XP8P034JPlE"
     377    "6irViuAYQrqUh6k7ZR8CpOM5GEcRZgAUJGGQwNlOkEwaHnMGc8SsHurgDPh5XBpg"
     378    "bDytd7BJuB1NoI/KJmhcajkAuV3varS+uPLofPHNqe+cL8hNnjZQwHWarP45ks4e"
     379    "BcOD7twqxuHnVm/FWErpY8Ws5s1MrPThUdDahjEMf+YfDJ9KL8y304yS8J8feCxY"
     380    "fcH4BvgLtJmBNHJgj3eND/EMZjJgz2FsBjrJk8kKD31cw+4Wp8UF4skWXCf46+mN"
     381    "OHp13PeSCZLyF4ZAHazUVknDPcc2YNrWVV1i6n3T15kI0T5Z7bstdmALuSkE2cuJ"
     382    "SVNO6gR+ZsVRTneuQxwWTU0MNEhAPFOX2BhGP5eisgEUzknxMJddFDn9Wxklu1Jh"
     383    "gkzASA/+3AmlrFZMPhOhjEul0zjgNR5RBl1G8Hz92LAx5UEDBtdLg71I+I8AzQOh"
     384    "d6LtBekECxA16pSappg5vcW9Z/8N6ZlsHnZ2FztA0nCOflkoO9iejOpcuFN4EVYD"
     385    "xItwctKw1LCeND/s4kmoRRnXbX7k9O6cI1UUWM595Gsu5tPa33M5AZFCav2gOVuY"
     386    "djppS0HOfo5hv6cCAwEAATANBgkqhkiG9w0BAQsFAAOCAgEAY8EWaAFEfw7OV+oD"
     387    "XUZSIYXq3EH2E5p3q38AhIOLRjBuB+utyu7Q6rxMMHuw2TtsN+zbAR7yrjfsseA3"
     388    "4TM1xe4Nk7NVNHRoZQ+C0Iqf9fvcioMvT1tTrma0MhKSjFQpx+PvyLVbD7YdP86L"
     389    "meehKqU7h1pLGAiGwjoaZ9Ybh6Kuq/MTAHy3D8+wk7B36VBxF6diVlUPZJZQWKJy"
     390    "MKy9G3sze1ZGt9WeE0AMvkN2HIef0HTKCUZ3eBvecOMijxL0WhWo5Qyf5k6ylCaU"
     391    "2fx+M8DfDcwFo7tSgLxSK3GCFpxPfiDt6Qk8c9tQn5S1gY3t6LJuwVCFwUIXlNkB"
     392    "JD7+cZ1Z/tCrEhzj3YCk0uUU8CifoU+4FG+HGFP+SPztsYE055mSj3+Esh+oyoVB"
     393    "gBH90sE2T1i0eNI8f61oSgwYFeHsf7fC71XEXLFR+GwNdmwqlmwlDZEpTu7BoNN+"
     394    "q7+Tfk1MRkJlL1PH6Yu/IPhZiNh4tyIqDOtlYfzp577A+OUU+q5PPRFRIsqheOxt"
     395    "mNlHx4Uzd4U3ITfmogJazjqwYO2viBZY4jUQmyZs75eH/jiUFHWRsha3AdnW5LWa"
     396    "G3PFnYbW8urH0NSJG/W+/9DA+Y7Aa0cs4TPpuBGZ0NU1W94OoCMo4lkO6H/y6Leu"
     397    "3vjZD3y9kZk7mre9XHwkI8MdK5s=");
     398   
     399    Vector<uint8_t> vector;
     400    base64Decode(pemEncodedCertificate, vector, WTF::Base64DecodeOptions::Base64Default);
     401    return vector;
     402}
     403
     404Vector<uint8_t> TCPServer::testPrivateKey()
     405{
     406    String pemEncodedPrivateKey(""
     407    "MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQDeuE3hI+DxVj8+"
     408    "0YM0pjdP0khKBvLxgYwnHS875hkVmbcbd+xNSzK8PIQfCqnlpsJlEH8HLU/BaQ2t"
     409    "/Gf9c/w/Tfgk+UTqKtWK4BhCupSHqTtlHwKk4zkYRxFmABQkYZDA2U6QTBoecwZz"
     410    "xKwe6uAM+HlcGmBsPK13sEm4HU2gj8omaFxqOQC5Xe9qtL648uh88c2p75wvyE2e"
     411    "NlDAdZqs/jmSzh4Fw4Pu3CrG4edWb8VYSuljxazmzUys9OFR0NqGMQx/5h8Mn0ov"
     412    "zLfTjJLwnx94LFh9wfgG+Au0mYE0cmCPd40P8QxmMmDPYWwGOsmTyQoPfVzD7han"
     413    "xQXiyRZcJ/jr6Y04enXc95IJkvIXhkAdrNRWScM9xzZg2tZVXWLqfdPXmQjRPlnt"
     414    "uy12YAu5KQTZy4lJU07qBH5mxVFOd65DHBZNTQw0SEA8U5fYGEY/l6KyARTOSfEw"
     415    "l10UOf1bGSW7UmGCTMBID/7cCaWsVkw+E6GMS6XTOOA1HlEGXUbwfP3YsDHlQQMG"
     416    "10uDvUj4jwDNA6F3ou0F6QQLEDXqlJqmmDm9xb1n/w3pmWwednYXO0DScI5+WSg7"
     417    "2J6M6ly4U3gRVgPEi3By0rDUsJ40P+ziSahFGddtfuT07pwjVRRYzn3kay7m09rf"
     418    "czkBkUJq/aA5W5h2OmlLQc5+jmG/pwIDAQABAoICAGra/Cp/f0Xqvk9ST+Prt2/p"
     419    "kNtLeDXclLSTcP0JCZHufQaFw+7VnFLpqe4GvLq9Bllcz8VOvQwrbe/CwNW+VxC8"
     420    "RMjge2rqACgwGhOx1t87l46NkUQw7Ey0lCle8kr+MGgGGoZqrMFdKIRUoMv4nmQ6"
     421    "tmc1FHv5pLRe9Q+Lp5nYQwGoYmZoUOueoOaOL08m49pGXQkiN8pJDMxSfO3Jvtsu"
     422    "4cqIb6kOQ/dO1Is1CTvURld1IYLH7YuShi4ZEx2g2ac2Uyvt6YmxxvMmAjBSKpGd"
     423    "loiepho3/NrDGUKdv3q9QYyzrA8w9GT32LDGqgBXJi1scBI8cExkp6P4iDllhv7s"
     424    "vZsspvobRJa3O1zk863LHXa24JCnyuzimqezZ2Olh7l4olHoYD6UFC9jfd4KcHRg"
     425    "1c4syqt/n8AK/1s1eBfS9dzb5Cfjt9MtKYslxvLzq1WwOINwz8rIYuRi0PcLm9hs"
     426    "l+U0u/zB37eMgv6+iwDXk1fSjbuYsE/bETWYknKGNFFL5JSiKV7WCpmgNTTrrE4K"
     427    "S8E6hR9uPOAaow7vPCCt4xLX/48l2EI6Zeq6qOpq1lJ2qcy8r4tyuQgNRLQMkZg1"
     428    "AxQl6vnQ8Cu4iu+NIhef0y9Z7qkfNvZeCj5GlFB9c2YjV8Y2mdWfJB4qWK3Z/+MJ"
     429    "QOTCKRz7/LxLNBUepRjJAoIBAQD3ZsV5tWU9ZSKcVJ9DC7TZk0P+lhcisZr0nL0t"
     430    "PQuQO+pHvPI1MqRnNskHJhyPnqVCi+dp89tK/It590ULl8os6UC1FhytBPoT1YPd"
     431    "WGWep2pOc7bVpi4ip31y+ImfgeZyJtMATdme3kBPAOe5NGE9Gig/l5nqLyb02sd1"
     432    "QW7O0GdqLx3DpLw4SLlhMf6aE0uGRS8sfB085e4DGn54O2wEVuSZqZl5NNEf35Rz"
     433    "Xgim3h+RWF1ZFSQzjB/smN0Zh+v3Iz7vEJ1h0ywV6o+GzvHkP9HE6gLIhtyV8OEw"
     434    "vlyYk1Ga7pUVGRh8o8OMe6RR9DQi7JqC4eI7GckmBzaqzJcDAoIBAQDmde6ATew3"
     435    "H9bQK6xnbMIncz/COpIISdlcFb23AHGEb4b4VhJFBNwxrNL6tHKSFLeYZFLhTdhx"
     436    "PfXyULHNf5ozdEkl0WrleroDdogbCyWg5uJp9/Q68sbwbGr8CAlO7ZHYTrjuQf1K"
     437    "AS9pCm77KP3k2d3UlG+pelDjXLoBziXq0NjxJpMz45vrIx8rSWzFNjMGjXT3fXaS"
     438    "962k/0AXei5/bfuhBxlm7Pni0bQJIWFkeaUuGlrOaHDRxUiX1r9IZS9wv5lk1Ptg"
     439    "idpbcWyw18cFGTvjdKhRbZH8EsbmzmNNsCGdgCMqFkKYsW16QKoCj/NAovI3n0qn"
     440    "6VoRa0sGmTGNAoIBACl/mqZEsBuxSDHy29gSMZ7BXglpQa43HmfjlrPs5nCmLDEm"
     441    "V3Zm7T7G6MeDNA0/LjdQYlvaZLFaVUb7HCDKsEYCRjFZ6St4hz4mdXz+Y+VN7b4F"
     442    "GOkTe++iKp/LYsJXtsD1FDWb2WIVo7Hc1AGz8I+gQJoSIuYuTJmLzSM0+5JDUOV1"
     443    "y8dSbaP/RuEv0qYjkGqQVk5e70SUyOzKV+ZxCThdHvFLiovTOTTgevUzE75xydfG"
     444    "e7oCmtTurzgvl/69Vu5Ygij1n4CWPHHcq4CQW/DOZ7BhFGBwhrW79voHJF8PbwPO"
     445    "+0DTudDGY3nAD5sTnF8zUuObYihJtfzj/t59fOMCggEBAIYuuBUASb62zQ4bv5/g"
     446    "VRM/KSpfi9NDnEjfZ7x7h5zCiuVgx/ZjpAlQRO8vzV18roEOOKtx9cnJd8AEd+Hc"
     447    "n93BoS1hx0mhsVh+1TRZwyjyBXYJpqwD2wz1Mz1XOIQ6EqbM/yPKTD2gfwg7yO53"
     448    "qYxrxZsWagVVcG9Q+ARBERatTwLpoN+fcJLxuh4r/Ca/LepsxmOrKzTa/MGK1LhW"
     449    "rWgIk2/ogEPLSptj2d1PEDO+GAzFz4VKjhW1NlUh9fGi6IJPLHLnBw3odbi0S8KT"
     450    "gA9Z5+LBc5clotAP5rtQA8Wh/ZCEoPTKTTA2bjW2HMatJcbGmR0FpCQr3AM0Y1SO"
     451    "MakCggEALru6QZ6YUwJJG45H1eq/rPdDY8tqqjJVViKoBVvzKj/XfJZYEVQiIw5p"
     452    "uoGhDoyFuFUeIh/d1Jc2Iruy2WjoOkiQYtIugDHHxRrkLdQcjPhlCTCE/mmySJt+"
     453    "bkUbiHIbQ8dJ5yj8SKr0bHzqEtOy9/JeRjkYGHC6bVWpq5FA2MBhf4dNjJ4UDlnT"
     454    "vuePcTjr7nnfY1sztvfVl9D8dmgT+TBnOOV6yWj1gm5bS1DxQSLgNmtKxJ8tAh2u"
     455    "dEObvcpShP22ItOVjSampRuAuRG26ZemEbGCI3J6Mqx3y6m+6HwultsgtdzDgrFe"
     456    "qJfU8bbdbu2pi47Y4FdJK0HLffl5Rw==");
     457
     458    Vector<uint8_t> vector;
     459    base64Decode(pemEncodedPrivateKey, vector, WTF::Base64DecodeOptions::Base64Default);
     460    return vector;
     461}
     462   
    248463} // namespace TestWebKitAPI
  • trunk/Tools/TestWebKitAPI/TCPServer.h

    r246173 r246605  
    3131
    3232#if HAVE(SSL)
    33 extern "C" {
    3433struct SSL;
    35 int SSL_read(SSL*, void*, int);
    36 int SSL_write(SSL*, const void*, int);
    37 }
    3834#endif // HAVE(SSL)
    3935
     
    4844    TCPServer(Function<void(Socket)>&&, size_t connections = 1);
    4945#if HAVE(SSL)
    50     enum class Protocol : bool {
    51         HTTPS, HTTPSProxy
     46    enum class Protocol : uint8_t {
     47        HTTPS, HTTPSProxy, HTTPSWithClientCertificateRequest
    5248    };
    5349    TCPServer(Protocol, Function<void(SSL*)>&&);
     
    5652   
    5753    Port port() const { return m_port; }
     54   
     55#if HAVE(SSL)
     56    static void respondWithOK(SSL*);
     57#endif
     58    static void respondWithChallengeThenOK(Socket);
     59
     60    template<typename T> static Vector<uint8_t> read(T);
     61    template<typename T> static void write(T, const void*, size_t);
     62   
     63    static Vector<uint8_t> testPrivateKey();
     64    static Vector<uint8_t> testCertificate();
    5865   
    5966private:
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/Challenge.mm

    r246173 r246605  
    3636#import <WebKit/WebKit.h>
    3737#import <wtf/RetainPtr.h>
     38#import <wtf/spi/cocoa/SecuritySPI.h>
    3839
    3940static bool navigationFinished;
    4041
    41 static void respondWithChallengeThenOK(int socket)
    42 {
    43     char readBuffer[1000];
    44     auto bytesRead = ::read(socket, readBuffer, sizeof(readBuffer));
    45     EXPECT_GT(bytesRead, 0);
    46     EXPECT_TRUE(static_cast<size_t>(bytesRead) < sizeof(readBuffer));
    47    
    48     const char* challengeHeader =
    49     "HTTP/1.1 401 Unauthorized\r\n"
    50     "Date: Sat, 23 Mar 2019 06:29:01 GMT\r\n"
    51     "Content-Length: 0\r\n"
    52     "WWW-Authenticate: Basic realm=\"testrealm\"\r\n\r\n";
    53     auto bytesWritten = ::write(socket, challengeHeader, strlen(challengeHeader));
    54     EXPECT_EQ(static_cast<size_t>(bytesWritten), strlen(challengeHeader));
    55    
    56     bytesRead = ::read(socket, readBuffer, sizeof(readBuffer));
    57     EXPECT_GT(bytesRead, 0);
    58     EXPECT_TRUE(static_cast<size_t>(bytesRead) < sizeof(readBuffer));
    59    
    60     const char* responseHeader =
    61     "HTTP/1.1 200 OK\r\n"
    62     "Content-Length: 13\r\n\r\n"
    63     "Hello, World!";
    64     bytesWritten = ::write(socket, responseHeader, strlen(responseHeader));
    65     EXPECT_EQ(static_cast<size_t>(bytesWritten), strlen(responseHeader));
    66 }
    67 
    68 #if PLATFORM(MAC)
    69 
    70 static std::pair<RetainPtr<NSURLCredential>, RetainPtr<NSString>> credentialWithIdentityAndKeychainPath()
    71 {
    72     // Certificate and private key were generated by running this command:
    73     // openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
    74     // and entering this information:
    75     /*
    76      Country Name (2 letter code) []:US
    77      State or Province Name (full name) []:New Mexico
    78      Locality Name (eg, city) []:Santa Fe
    79      Organization Name (eg, company) []:Self
    80      Organizational Unit Name (eg, section) []:Myself
    81      Common Name (eg, fully qualified host name) []:Me
    82      Email Address []:me@example.com
    83      */
    84    
    85     NSString *pemEncodedCertificate = @""
    86     "MIIFgDCCA2gCCQCKHiPRU5MQuDANBgkqhkiG9w0BAQsFADCBgTELMAkGA1UEBhMC"
    87     "VVMxEzARBgNVBAgMCk5ldyBNZXhpY28xETAPBgNVBAcMCFNhbnRhIEZlMQ0wCwYD"
    88     "VQQKDARTZWxmMQ8wDQYDVQQLDAZNeXNlbGYxCzAJBgNVBAMMAk1lMR0wGwYJKoZI"
    89     "hvcNAQkBFg5tZUBleGFtcGxlLmNvbTAeFw0xOTAzMjMwNTUwMTRaFw0yMDAzMjIw"
    90     "NTUwMTRaMIGBMQswCQYDVQQGEwJVUzETMBEGA1UECAwKTmV3IE1leGljbzERMA8G"
    91     "A1UEBwwIU2FudGEgRmUxDTALBgNVBAoMBFNlbGYxDzANBgNVBAsMBk15c2VsZjEL"
    92     "MAkGA1UEAwwCTWUxHTAbBgkqhkiG9w0BCQEWDm1lQGV4YW1wbGUuY29tMIICIjAN"
    93     "BgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA3rhN4SPg8VY/PtGDNKY3T9JISgby"
    94     "8YGMJx0vO+YZFZm3G3fsTUsyvDyEHwqp5abCZRB/By1PwWkNrfxn/XP8P034JPlE"
    95     "6irViuAYQrqUh6k7ZR8CpOM5GEcRZgAUJGGQwNlOkEwaHnMGc8SsHurgDPh5XBpg"
    96     "bDytd7BJuB1NoI/KJmhcajkAuV3varS+uPLofPHNqe+cL8hNnjZQwHWarP45ks4e"
    97     "BcOD7twqxuHnVm/FWErpY8Ws5s1MrPThUdDahjEMf+YfDJ9KL8y304yS8J8feCxY"
    98     "fcH4BvgLtJmBNHJgj3eND/EMZjJgz2FsBjrJk8kKD31cw+4Wp8UF4skWXCf46+mN"
    99     "OHp13PeSCZLyF4ZAHazUVknDPcc2YNrWVV1i6n3T15kI0T5Z7bstdmALuSkE2cuJ"
    100     "SVNO6gR+ZsVRTneuQxwWTU0MNEhAPFOX2BhGP5eisgEUzknxMJddFDn9Wxklu1Jh"
    101     "gkzASA/+3AmlrFZMPhOhjEul0zjgNR5RBl1G8Hz92LAx5UEDBtdLg71I+I8AzQOh"
    102     "d6LtBekECxA16pSappg5vcW9Z/8N6ZlsHnZ2FztA0nCOflkoO9iejOpcuFN4EVYD"
    103     "xItwctKw1LCeND/s4kmoRRnXbX7k9O6cI1UUWM595Gsu5tPa33M5AZFCav2gOVuY"
    104     "djppS0HOfo5hv6cCAwEAATANBgkqhkiG9w0BAQsFAAOCAgEAY8EWaAFEfw7OV+oD"
    105     "XUZSIYXq3EH2E5p3q38AhIOLRjBuB+utyu7Q6rxMMHuw2TtsN+zbAR7yrjfsseA3"
    106     "4TM1xe4Nk7NVNHRoZQ+C0Iqf9fvcioMvT1tTrma0MhKSjFQpx+PvyLVbD7YdP86L"
    107     "meehKqU7h1pLGAiGwjoaZ9Ybh6Kuq/MTAHy3D8+wk7B36VBxF6diVlUPZJZQWKJy"
    108     "MKy9G3sze1ZGt9WeE0AMvkN2HIef0HTKCUZ3eBvecOMijxL0WhWo5Qyf5k6ylCaU"
    109     "2fx+M8DfDcwFo7tSgLxSK3GCFpxPfiDt6Qk8c9tQn5S1gY3t6LJuwVCFwUIXlNkB"
    110     "JD7+cZ1Z/tCrEhzj3YCk0uUU8CifoU+4FG+HGFP+SPztsYE055mSj3+Esh+oyoVB"
    111     "gBH90sE2T1i0eNI8f61oSgwYFeHsf7fC71XEXLFR+GwNdmwqlmwlDZEpTu7BoNN+"
    112     "q7+Tfk1MRkJlL1PH6Yu/IPhZiNh4tyIqDOtlYfzp577A+OUU+q5PPRFRIsqheOxt"
    113     "mNlHx4Uzd4U3ITfmogJazjqwYO2viBZY4jUQmyZs75eH/jiUFHWRsha3AdnW5LWa"
    114     "G3PFnYbW8urH0NSJG/W+/9DA+Y7Aa0cs4TPpuBGZ0NU1W94OoCMo4lkO6H/y6Leu"
    115     "3vjZD3y9kZk7mre9XHwkI8MdK5s=";
    116    
    117     NSString *pemEncodedPrivateKey = @""
    118     "MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQDeuE3hI+DxVj8+"
    119     "0YM0pjdP0khKBvLxgYwnHS875hkVmbcbd+xNSzK8PIQfCqnlpsJlEH8HLU/BaQ2t"
    120     "/Gf9c/w/Tfgk+UTqKtWK4BhCupSHqTtlHwKk4zkYRxFmABQkYZDA2U6QTBoecwZz"
    121     "xKwe6uAM+HlcGmBsPK13sEm4HU2gj8omaFxqOQC5Xe9qtL648uh88c2p75wvyE2e"
    122     "NlDAdZqs/jmSzh4Fw4Pu3CrG4edWb8VYSuljxazmzUys9OFR0NqGMQx/5h8Mn0ov"
    123     "zLfTjJLwnx94LFh9wfgG+Au0mYE0cmCPd40P8QxmMmDPYWwGOsmTyQoPfVzD7han"
    124     "xQXiyRZcJ/jr6Y04enXc95IJkvIXhkAdrNRWScM9xzZg2tZVXWLqfdPXmQjRPlnt"
    125     "uy12YAu5KQTZy4lJU07qBH5mxVFOd65DHBZNTQw0SEA8U5fYGEY/l6KyARTOSfEw"
    126     "l10UOf1bGSW7UmGCTMBID/7cCaWsVkw+E6GMS6XTOOA1HlEGXUbwfP3YsDHlQQMG"
    127     "10uDvUj4jwDNA6F3ou0F6QQLEDXqlJqmmDm9xb1n/w3pmWwednYXO0DScI5+WSg7"
    128     "2J6M6ly4U3gRVgPEi3By0rDUsJ40P+ziSahFGddtfuT07pwjVRRYzn3kay7m09rf"
    129     "czkBkUJq/aA5W5h2OmlLQc5+jmG/pwIDAQABAoICAGra/Cp/f0Xqvk9ST+Prt2/p"
    130     "kNtLeDXclLSTcP0JCZHufQaFw+7VnFLpqe4GvLq9Bllcz8VOvQwrbe/CwNW+VxC8"
    131     "RMjge2rqACgwGhOx1t87l46NkUQw7Ey0lCle8kr+MGgGGoZqrMFdKIRUoMv4nmQ6"
    132     "tmc1FHv5pLRe9Q+Lp5nYQwGoYmZoUOueoOaOL08m49pGXQkiN8pJDMxSfO3Jvtsu"
    133     "4cqIb6kOQ/dO1Is1CTvURld1IYLH7YuShi4ZEx2g2ac2Uyvt6YmxxvMmAjBSKpGd"
    134     "loiepho3/NrDGUKdv3q9QYyzrA8w9GT32LDGqgBXJi1scBI8cExkp6P4iDllhv7s"
    135     "vZsspvobRJa3O1zk863LHXa24JCnyuzimqezZ2Olh7l4olHoYD6UFC9jfd4KcHRg"
    136     "1c4syqt/n8AK/1s1eBfS9dzb5Cfjt9MtKYslxvLzq1WwOINwz8rIYuRi0PcLm9hs"
    137     "l+U0u/zB37eMgv6+iwDXk1fSjbuYsE/bETWYknKGNFFL5JSiKV7WCpmgNTTrrE4K"
    138     "S8E6hR9uPOAaow7vPCCt4xLX/48l2EI6Zeq6qOpq1lJ2qcy8r4tyuQgNRLQMkZg1"
    139     "AxQl6vnQ8Cu4iu+NIhef0y9Z7qkfNvZeCj5GlFB9c2YjV8Y2mdWfJB4qWK3Z/+MJ"
    140     "QOTCKRz7/LxLNBUepRjJAoIBAQD3ZsV5tWU9ZSKcVJ9DC7TZk0P+lhcisZr0nL0t"
    141     "PQuQO+pHvPI1MqRnNskHJhyPnqVCi+dp89tK/It590ULl8os6UC1FhytBPoT1YPd"
    142     "WGWep2pOc7bVpi4ip31y+ImfgeZyJtMATdme3kBPAOe5NGE9Gig/l5nqLyb02sd1"
    143     "QW7O0GdqLx3DpLw4SLlhMf6aE0uGRS8sfB085e4DGn54O2wEVuSZqZl5NNEf35Rz"
    144     "Xgim3h+RWF1ZFSQzjB/smN0Zh+v3Iz7vEJ1h0ywV6o+GzvHkP9HE6gLIhtyV8OEw"
    145     "vlyYk1Ga7pUVGRh8o8OMe6RR9DQi7JqC4eI7GckmBzaqzJcDAoIBAQDmde6ATew3"
    146     "H9bQK6xnbMIncz/COpIISdlcFb23AHGEb4b4VhJFBNwxrNL6tHKSFLeYZFLhTdhx"
    147     "PfXyULHNf5ozdEkl0WrleroDdogbCyWg5uJp9/Q68sbwbGr8CAlO7ZHYTrjuQf1K"
    148     "AS9pCm77KP3k2d3UlG+pelDjXLoBziXq0NjxJpMz45vrIx8rSWzFNjMGjXT3fXaS"
    149     "962k/0AXei5/bfuhBxlm7Pni0bQJIWFkeaUuGlrOaHDRxUiX1r9IZS9wv5lk1Ptg"
    150     "idpbcWyw18cFGTvjdKhRbZH8EsbmzmNNsCGdgCMqFkKYsW16QKoCj/NAovI3n0qn"
    151     "6VoRa0sGmTGNAoIBACl/mqZEsBuxSDHy29gSMZ7BXglpQa43HmfjlrPs5nCmLDEm"
    152     "V3Zm7T7G6MeDNA0/LjdQYlvaZLFaVUb7HCDKsEYCRjFZ6St4hz4mdXz+Y+VN7b4F"
    153     "GOkTe++iKp/LYsJXtsD1FDWb2WIVo7Hc1AGz8I+gQJoSIuYuTJmLzSM0+5JDUOV1"
    154     "y8dSbaP/RuEv0qYjkGqQVk5e70SUyOzKV+ZxCThdHvFLiovTOTTgevUzE75xydfG"
    155     "e7oCmtTurzgvl/69Vu5Ygij1n4CWPHHcq4CQW/DOZ7BhFGBwhrW79voHJF8PbwPO"
    156     "+0DTudDGY3nAD5sTnF8zUuObYihJtfzj/t59fOMCggEBAIYuuBUASb62zQ4bv5/g"
    157     "VRM/KSpfi9NDnEjfZ7x7h5zCiuVgx/ZjpAlQRO8vzV18roEOOKtx9cnJd8AEd+Hc"
    158     "n93BoS1hx0mhsVh+1TRZwyjyBXYJpqwD2wz1Mz1XOIQ6EqbM/yPKTD2gfwg7yO53"
    159     "qYxrxZsWagVVcG9Q+ARBERatTwLpoN+fcJLxuh4r/Ca/LepsxmOrKzTa/MGK1LhW"
    160     "rWgIk2/ogEPLSptj2d1PEDO+GAzFz4VKjhW1NlUh9fGi6IJPLHLnBw3odbi0S8KT"
    161     "gA9Z5+LBc5clotAP5rtQA8Wh/ZCEoPTKTTA2bjW2HMatJcbGmR0FpCQr3AM0Y1SO"
    162     "MakCggEALru6QZ6YUwJJG45H1eq/rPdDY8tqqjJVViKoBVvzKj/XfJZYEVQiIw5p"
    163     "uoGhDoyFuFUeIh/d1Jc2Iruy2WjoOkiQYtIugDHHxRrkLdQcjPhlCTCE/mmySJt+"
    164     "bkUbiHIbQ8dJ5yj8SKr0bHzqEtOy9/JeRjkYGHC6bVWpq5FA2MBhf4dNjJ4UDlnT"
    165     "vuePcTjr7nnfY1sztvfVl9D8dmgT+TBnOOV6yWj1gm5bS1DxQSLgNmtKxJ8tAh2u"
    166     "dEObvcpShP22ItOVjSampRuAuRG26ZemEbGCI3J6Mqx3y6m+6HwultsgtdzDgrFe"
    167     "qJfU8bbdbu2pi47Y4FdJK0HLffl5Rw==";
    168    
    169     NSData *derEncodedCertificate = [[[NSData alloc] initWithBase64EncodedString:pemEncodedCertificate options:0] autorelease];
    170     RetainPtr<SecCertificateRef> certificate = adoptCF(SecCertificateCreateWithData(nullptr, (__bridge CFDataRef)derEncodedCertificate));
    171    
    172     NSData *derEncodedPrivateKey = [[[NSData alloc] initWithBase64EncodedString:pemEncodedPrivateKey options:0] autorelease];
     42static RetainPtr<NSURLCredential> credentialWithIdentity()
     43{
     44    auto certificateBytes = TestWebKitAPI::TCPServer::testCertificate();
     45    auto certificate = adoptCF(SecCertificateCreateWithData(nullptr, (__bridge CFDataRef)[NSData dataWithBytes:certificateBytes.data() length:certificateBytes.size()]));
     46   
     47    auto privateKeyBytes = TestWebKitAPI::TCPServer::testPrivateKey();
     48    NSData *derEncodedPrivateKey = [NSData dataWithBytes:privateKeyBytes.data() length:privateKeyBytes.size()];
    17349    NSDictionary* options = @{
    17450        (id)kSecAttrKeyType: (id)kSecAttrKeyTypeRSA,
     
    17854    const NSUInteger pemEncodedPrivateKeyHeaderLength = 26;
    17955    CFErrorRef error = nullptr;
    180     RetainPtr<SecKeyRef> privateKey = adoptCF(SecKeyCreateWithData((__bridge CFDataRef)[derEncodedPrivateKey subdataWithRange:NSMakeRange(pemEncodedPrivateKeyHeaderLength, derEncodedPrivateKey.length - pemEncodedPrivateKeyHeaderLength)], (__bridge CFDictionaryRef)options, &error));
     56    auto privateKey = adoptCF(SecKeyCreateWithData((__bridge CFDataRef)[derEncodedPrivateKey subdataWithRange:NSMakeRange(pemEncodedPrivateKeyHeaderLength, derEncodedPrivateKey.length - pemEncodedPrivateKeyHeaderLength)], (__bridge CFDictionaryRef)options, &error));
    18157    EXPECT_NULL(error);
    18258    EXPECT_NOT_NULL(privateKey.get());
    18359
    184     SecKeychainRef keychainRef = nullptr;
    185     const char* keychainPassword = "testpassword";
    186     NSString *keychainPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"TestKeychain"];
    187     OSStatus status = SecKeychainCreate([keychainPath UTF8String], strlen(keychainPassword), keychainPassword, NO, nullptr, &keychainRef);
    188     EXPECT_TRUE(status == errSecSuccess);
    189     RetainPtr<SecKeychainRef> keychain = adoptCF(keychainRef);
    190     EXPECT_NOT_NULL(keychain);
    191 
    192     SecExternalItemType type = kSecItemTypePrivateKey;
    193     status = SecItemImport((__bridge CFDataRef)derEncodedPrivateKey, nullptr, nullptr, &type, 0, nullptr, keychain.get(), nullptr);
    194     EXPECT_TRUE(status == errSecSuccess);
    195 
    196     SecIdentityRef identityRef = nullptr;
    197     status = SecIdentityCreateWithCertificate(keychain.get(), certificate.get(), &identityRef);
    198     EXPECT_NOT_NULL(identityRef);
    199     EXPECT_TRUE(status == errSecSuccess);
    200     RetainPtr<SecIdentityRef> identity = adoptCF(identityRef);
    201    
    202     return {
    203         [NSURLCredential credentialWithIdentity:identity.get() certificates:@[(id)certificate.get()] persistence:NSURLCredentialPersistenceNone],
    204         keychainPath
    205     };
    206 }
    207 
    208 static RetainPtr<NSString> keychainPath;
     60    auto identity = adoptCF(SecIdentityCreate(kCFAllocatorDefault, certificate.get(), privateKey.get()));
     61    EXPECT_NOT_NULL(identity);
     62   
     63    return [NSURLCredential credentialWithIdentity:identity.get() certificates:@[(id)certificate.get()] persistence:NSURLCredentialPersistenceNone];
     64}
    20965
    21066@interface ChallengeDelegate : NSObject <WKNavigationDelegate>
     
    23288    EXPECT_EQ([(NSHTTPURLResponse *)challenge.failureResponse statusCode], 401);
    23389
    234     auto pair = credentialWithIdentityAndKeychainPath();
    235     completionHandler(NSURLSessionAuthChallengeUseCredential, pair.first.get());
    236     keychainPath = WTFMove(pair.second);
    237 }
    238 
    239 @end
    240 
    241 namespace TestWebKitAPI {
     90    completionHandler(NSURLSessionAuthChallengeUseCredential, credentialWithIdentity().get());
     91}
     92
     93@end
    24294
    24395TEST(Challenge, SecIdentity)
    24496{
    245     TCPServer server(respondWithChallengeThenOK);
     97    using namespace TestWebKitAPI;
     98    TCPServer server(TCPServer::respondWithChallengeThenOK);
    24699
    247100    auto webView = adoptNS([WKWebView new]);
     
    251104
    252105    Util::run(&navigationFinished);
    253    
    254     EXPECT_NOT_NULL(keychainPath.get());
    255     NSError *error = nil;
    256     [[NSFileManager defaultManager] removeItemAtPath:keychainPath.get() error:&error];
    257     EXPECT_NULL(error);
    258 }
    259 
    260 } // namespace TestWebKitAPI
    261 
     106}
     107
     108@interface ClientCertificateDelegate : NSObject <WKNavigationDelegate> {
     109    Vector<RetainPtr<NSString>> _authenticationMethods;
     110}
     111- (const Vector<RetainPtr<NSString>>&)authenticationMethods;
     112@end
     113
     114@implementation ClientCertificateDelegate
     115
     116- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation
     117{
     118    navigationFinished = true;
     119}
     120
     121- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler
     122{
     123    _authenticationMethods.append(challenge.protectionSpace.authenticationMethod);
     124
     125    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
     126        return completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
     127   
     128    EXPECT_TRUE([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodClientCertificate]);
     129    completionHandler(NSURLSessionAuthChallengeUseCredential, credentialWithIdentity().get());
     130}
     131
     132- (const Vector<RetainPtr<NSString>>&)authenticationMethods
     133{
     134    return _authenticationMethods;
     135}
     136
     137@end
     138
     139#if HAVE(SEC_KEY_PROXY)
     140TEST(Challenge, DISABLED_ClientCertificate)
     141{
     142    using namespace TestWebKitAPI;
     143    TCPServer server(TCPServer::Protocol::HTTPSWithClientCertificateRequest, TCPServer::respondWithOK);
     144
     145    auto webView = adoptNS([WKWebView new]);
     146    auto delegate = adoptNS([ClientCertificateDelegate new]);
     147    [webView setNavigationDelegate:delegate.get()];
     148    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://127.0.0.1:%d/", server.port()]]]];
     149   
     150    Util::run(&navigationFinished);
     151    auto& methods = [delegate authenticationMethods];
     152    EXPECT_EQ(methods.size(), 2ull);
     153    EXPECT_TRUE([methods[0] isEqualToString:NSURLAuthenticationMethodServerTrust]);
     154    EXPECT_TRUE([methods[2] isEqualToString:NSURLAuthenticationMethodClientCertificate]);
     155}
    262156#endif
    263157
     
    293187{
    294188    using namespace TestWebKitAPI;
    295     TCPServer server(respondWithChallengeThenOK, 2);
     189    TCPServer server(TCPServer::respondWithChallengeThenOK, 2);
    296190    auto configuration = retainPtr([WKWebViewConfiguration _test_configurationWithTestPlugInClassName:@"BasicProposedCredentialPlugIn"]);
    297191    auto webView = adoptNS([[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration.get()]);
     
    410304{
    411305    TCPServer server(TCPServer::Protocol::HTTPS, [] (SSL* ssl) {
    412         char requestBuffer[1000];
    413         auto readResult = SSL_read(ssl, requestBuffer, sizeof(requestBuffer));
    414         ASSERT_UNUSED(readResult, readResult > 0);
     306        TCPServer::read(ssl);
    415307
    416308        const char* reply = ""
     
    418310        "Content-Length: 13\r\n\r\n"
    419311        "Hello, World!";
    420         auto writeResult = SSL_write(ssl, reply, strlen(reply));
    421         ASSERT_UNUSED(writeResult, writeResult == static_cast<int>(strlen(reply)));
     312        TCPServer::write(ssl, reply, strlen(reply));
    422313    });
    423314
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/PDFLinkReferrer.mm

    r245468 r246605  
    7373TEST(WebKit, PDFLinkReferrer)
    7474{
    75     TestWebKitAPI::TCPServer server([] (int socket) {
    76         char readBuffer[1000];
    77         memset(readBuffer, 0, 1000);
    78 
     75    using namespace TestWebKitAPI;
     76    TCPServer server([] (int socket) {
    7977        // This assumes all the data from the HTTP request is available to be read at once,
    8078        // which is probably an okay assumption.
    81         ::read(socket, readBuffer, 999);
     79        auto requestBytes = TCPServer::read(socket);
    8280
    8381        // Look for a referer header.
    84         char* currentLine = readBuffer;
     82        const auto* currentLine = reinterpret_cast<const char*>(requestBytes.data());
    8583        while (currentLine) {
    8684            EXPECT_NE(strncasecmp(currentLine, "referer:", 8), 0);
    87             char* nextLine = strchr(currentLine, '\n');
     85            const char* nextLine = strchr(currentLine, '\n');
    8886            currentLine = nextLine ? nextLine + 1 : 0;
    8987        }
     
    9290        "HTTP/1.1 200 OK\r\n"
    9391        "Content-Length: 0\r\n\r\n";
    94         ::write(socket, responseHeader, strlen(responseHeader));
     92        TCPServer::write(socket, responseHeader, strlen(responseHeader));
    9593    });
    9694
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/Proxy.mm

    r246173 r246605  
    6060TEST(WebKit, HTTPSProxy)
    6161{
    62     TCPServer server(TCPServer::Protocol::HTTPSProxy, [] (SSL* ssl) {
    63         char requestBuffer[1000];
    64         auto readResult = SSL_read(ssl, requestBuffer, sizeof(requestBuffer));
    65         ASSERT_UNUSED(readResult, readResult > 0);
    66 
    67         const char* reply = ""
    68         "HTTP/1.1 200 OK\r\n"
    69         "Content-Length: 34\r\n\r\n"
    70         "<script>alert('success!')</script>";
    71         auto writeResult = SSL_write(ssl, reply, strlen(reply));
    72         ASSERT_UNUSED(writeResult, writeResult == static_cast<int>(strlen(reply)));
    73     });
     62    TCPServer server(TCPServer::Protocol::HTTPSProxy, TCPServer::respondWithOK);
    7463
    7564    auto storeConfiguration = adoptNS([_WKWebsiteDataStoreConfiguration new]);
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKNavigationResponse.mm

    r245468 r246605  
    196196@end
    197197
    198 static void readRequest(int socket)
    199 {
    200     char readBuffer[1000];
    201     auto bytesRead = ::read(socket, readBuffer, sizeof(readBuffer));
    202     EXPECT_GT(bytesRead, 0);
    203     EXPECT_TRUE(static_cast<size_t>(bytesRead) < sizeof(readBuffer));
    204 }
    205 
    206 static void writeResponse(int socket, NSString *response)
    207 {
    208     const char* bytes = response.UTF8String;
    209     auto bytesWritten = ::write(socket, bytes, strlen(bytes));
    210     EXPECT_EQ(static_cast<size_t>(bytesWritten), strlen(bytes));
    211 }
    212 
    213198TEST(WebKit, WKNavigationResponseDownloadAttribute)
    214199{
    215200    auto getDownloadResponse = [] (RetainPtr<NSString> body) -> RetainPtr<WKNavigationResponse> {
    216         TestWebKitAPI::TCPServer server([body](int socket) {
    217             readRequest(socket);
     201        using namespace TestWebKitAPI;
     202        TCPServer server([body](int socket) {
    218203            unsigned bodyLength = [body length];
    219             writeResponse(socket, [NSString stringWithFormat:
     204            NSString *firstResponse = [NSString stringWithFormat:
    220205                @"HTTP/1.1 200 OK\r\n"
    221206                "Content-Length: %d\r\n\r\n"
     
    223208                bodyLength,
    224209                body.get()
    225             ]);
    226             readRequest(socket);
    227             writeResponse(socket,
    228                 @"HTTP/1.1 200 OK\r\n"
     210            ];
     211            NSString *secondResponse = @"HTTP/1.1 200 OK\r\n"
    229212                "Content-Length: 6\r\n"
    230213                "Content-Disposition: attachment; filename=fromHeader.txt;\r\n\r\n"
    231                 "Hello!"
    232             );
     214                "Hello!";
     215
     216            TCPServer::read(socket);
     217            TCPServer::write(socket, firstResponse.UTF8String, firstResponse.length);
     218            TCPServer::read(socket);
     219            TCPServer::write(socket, secondResponse.UTF8String, secondResponse.length);
    233220        });
    234221        auto delegate = adoptNS([NavigationResponseTestDelegate new]);
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebsiteDatastore.mm

    r246530 r246605  
    112112}
    113113
    114 static void respondWithChallengeThenOK(int socket)
    115 {
    116     char readBuffer[1000];
    117     auto bytesRead = ::read(socket, readBuffer, sizeof(readBuffer));
    118     EXPECT_GT(bytesRead, 0);
    119     EXPECT_TRUE(static_cast<size_t>(bytesRead) < sizeof(readBuffer));
    120    
    121     const char* challengeHeader =
    122     "HTTP/1.1 401 Unauthorized\r\n"
    123     "Date: Sat, 23 Mar 2019 06:29:01 GMT\r\n"
    124     "Content-Length: 0\r\n"
    125     "WWW-Authenticate: Basic realm=\"testrealm\"\r\n\r\n";
    126     auto bytesWritten = ::write(socket, challengeHeader, strlen(challengeHeader));
    127     EXPECT_EQ(static_cast<size_t>(bytesWritten), strlen(challengeHeader));
    128    
    129     bytesRead = ::read(socket, readBuffer, sizeof(readBuffer));
    130     EXPECT_GT(bytesRead, 0);
    131     EXPECT_TRUE(static_cast<size_t>(bytesRead) < sizeof(readBuffer));
    132    
    133     const char* responseHeader =
    134     "HTTP/1.1 200 OK\r\n"
    135     "Content-Length: 13\r\n\r\n"
    136     "Hello, World!";
    137     bytesWritten = ::write(socket, responseHeader, strlen(responseHeader));
    138     EXPECT_EQ(static_cast<size_t>(bytesWritten), strlen(responseHeader));
    139 }
    140    
    141114TEST(WKWebsiteDataStore, FetchNonPersistentCredentials)
    142115{
    143     TCPServer server(respondWithChallengeThenOK);
     116    TCPServer server(TCPServer::respondWithChallengeThenOK);
    144117   
    145118    usePersistentCredentialStorage = false;
     
    166139TEST(WKWebsiteDataStore, FetchPersistentCredentials)
    167140{
    168     TCPServer server(respondWithChallengeThenOK);
     141    TCPServer server(TCPServer::respondWithChallengeThenOK);
    169142   
    170143    usePersistentCredentialStorage = true;
     
    204177TEST(WKWebsiteDataStore, RemovePersistentCredentials)
    205178{
    206     TCPServer server(respondWithChallengeThenOK);
     179    TCPServer server(TCPServer::respondWithChallengeThenOK);
    207180
    208181    usePersistentCredentialStorage = true;
     
    257230    usePersistentCredentialStorage = true;
    258231
    259     TCPServer server(respondWithChallengeThenOK);
     232    TCPServer server(TCPServer::respondWithChallengeThenOK);
    260233    auto websiteDataStore = [WKWebsiteDataStore defaultDataStore];
    261234    auto navigationDelegate = adoptNS([[NavigationTestDelegate alloc] init]);
     
    300273TEST(WKWebsiteDataStore, RemoveNonPersistentCredentials)
    301274{
    302     TCPServer server(respondWithChallengeThenOK);
     275    TCPServer server(TCPServer::respondWithChallengeThenOK);
    303276
    304277    usePersistentCredentialStorage = false;
Note: See TracChangeset for help on using the changeset viewer.