2015-01-20 14 views
8

SSL_shutdown पर openssl प्रलेखन कहा गया है कि: इसलिए यह सिफारिश की है,)) फिर से SSL_shutdown (की वापसी मान की जाँच करें और SSL_shutdown (कॉल करने के लिए, अगर द्विदिश बंद अभी तक पूरा (पहले की वापसी मान नहीं है कॉल 0 है)।हैंडलिंग SSL_shutdown सही ढंग से

https://www.openssl.org/docs/ssl/SSL_shutdown.html

मैं नीचे जहाँ मैं SSL_shutdown से वापसी मान 0 के लिए जाँच करें और इसे फिर से फोन एक कोड का टुकड़ा है, जो मैं उपयोग कर रहा है है। मेरा सवाल यह है कि, दूसरी कॉल पर SSL_shutdown के रिटर्न वैल्यू को नजरअंदाज करना ठीक है या हमें 1 (बिडरेक्शनल शटडाउन पूर्ण) लौटाए जाने तक SSL_shutdown को फिर से प्रयास करना चाहिए।

int r = SSL_shutdown(ssl); 
//error handling here if r < 0 
if(!r) 
{ 

    shutdown(fd,1); 
    SSL_shutdown(ssl); //how should I handle return value and error handling here is it required?? 
} 
SSL_free(ssl); 
SSLMap.erase(fd); 
shutdown(fd,2); 
close(fd); 

उत्तर

11

openssl एक अंधेरे कला का एक सा है।

सबसे पहले जिस पृष्ठ पर आपने संदर्भित किया है, उसमें HTML-वापसी का मूल्य खराब है। यहां बताया गया है मानव पेज वास्तव में कहते है:

RETURN VALUES 

    The following return values can occur: 

    0 The shutdown is not yet finished. Call SSL_shutdown() for a second 
     time, if a bidirectional shutdown shall be performed. The output 
     of SSL_get_error(3) may be misleading, as an erroneous 
     SSL_ERROR_SYSCALL may be flagged even though no error occurred. 

    1 The shutdown was successfully completed. The "close notify" alert 
     was sent and the peer's "close notify" alert was received. 

    -1 The shutdown was not successful because a fatal error occurred 
     either at the protocol level or a connection failure occurred. It 
     can also occur if action is need to continue the operation for non- 
     blocking BIOs. Call SSL_get_error(3) with the return value ret to 
     find out the reason. 

आप BIOS को अवरुद्ध किया है, तो चीजें अपेक्षाकृत आसान है। पहली कॉल पर 0 का मतलब है कि यदि आप एक द्विपक्षीय शट डाउन चाहते हैं तो आपको SSL_shutdown पर कॉल करने की आवश्यकता है। ए 1 का मतलब है कि आप कर चुके हैं। ए -1 का मतलब एक त्रुटि है। दूसरी कॉल पर (जो आप केवल 0 करते हैं यदि आप 0 वापस प्राप्त करते हैं), तो एक बिडरेक्शनल शटडाउन शुरू किया जाता है। तर्क बताता है कि आप 0 बार फिर से नहीं प्राप्त कर सकते हैं (क्योंकि यह एक अवरुद्ध बीआईओ है और पहला कदम पूरा कर लेगा)। ए -1 एक त्रुटि इंगित करता है, और एक 1 पूरा होने का संकेत देता है।

आप गैर-अवरुद्ध BIOS है, तो एक ही लागू होता है, सच है कि आप पूरे SSL_ERROR_WANT_READ और SSL_ERROR_WANT_WRITE बेसिरपैर की बात के माध्यम से जाने की जरूरत है, यानी .:

If the underlying BIO is non-blocking, SSL_shutdown() will also return 
    when the underlying BIO could not satisfy the needs of SSL_shutdown() 
    to continue the handshake. In this case a call to SSL_get_error() with 
    the return value of SSL_shutdown() will yield SSL_ERROR_WANT_READ or 
    SSL_ERROR_WANT_WRITE. The calling process then must repeat the call 
    after taking appropriate action to satisfy the needs of SSL_shutdown(). 
    The action depends on the underlying BIO. When using a non-blocking 
    socket, nothing is to be done, but select() can be used to check for 
    the required condition. When using a buffering BIO, like a BIO pair, 
    data must be written into or retrieved out of the BIO before being able 
    to continue. 

तो तुम पुनरावृत्ति के दो स्तर है को बचाने के लिए। SSL_shutdown को 'पहला' समय पर कॉल करें, लेकिन SSL_ERROR_WANT_READ या SSL_ERROR_WANT_WRITE को सामान्य तरीके से select() लूप के आसपास जाने के बाद दोहराएं, और केवल 'पहले' SSL_shutdown को गिनें, यदि आपको गैर SSL_ERROR_WANT_ त्रुटि कोड मिलता है (इस मामले में असफल), या आपको 0 या 1 वापसी मिलती है। अगर आपको 1 रिटर्न मिलता है, तो आपने किया है। अगर आपको 0 रिटर्न मिलता है, और आप एक द्विपक्षीय शट डाउन चाहते हैं, तो आपको दूसरी कॉल करना होगा, जिस पर आपको SSL_ERROR_WANT_READ या SSL_ERROR_WANT_WRITE और फिर से प्रयास करने की आवश्यकता होगी; जो 1 वापस नहीं करना चाहिए, लेकिन 0 या एक त्रुटि लौटा सकता है।

आसान नहीं है।

+0

धन्यवाद! बहुत सराहना की मदद। – cmidi

संबंधित मुद्दे