Come verificare il certificato autofirmato per le richieste https utilizzando le "richieste" del modulo sul client e "pallone-riposante" sul server (TLS1.2)

0

Domanda

Qui è quello che ho finora. Utilizzando openssl, ora ho questi file: ca.crt, ca.chiave, ca.srl, server.crt, server.csr, server.chiave.

Ho seguito questo tutorial: https://carolinafernandez.github.io/development/2017/09/13/HTTPS-and-trust-chain-in-Flask

E ora questo server:

from flask import Flask, request
from flask_restful import Resource, Api, reqparse
import psycopg2
import ssl
import sys

app = Flask(__name__)
api = Api(app)

# TODO: https security
HTTPS_ENABLED = True
VERIFY_USER = True

API_HOST = "0.0.0.0"
API_PORT = 8000
API_CRT = "server.crt"
API_KEY = "server.key"
API_CA_T = "ca.crt"

context = None
if(HTTPS_ENABLED):
    context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
    
    if(VERIFY_USER):
        context.verify_mode = ssl.CERT_REQUIRED
        context.load_verify_locations(API_CA_T)

    try:
        context.load_cert_chain(API_CRT, API_KEY)
    except Exception as e:
        sys.exit("Error starting server: {}".format(e))

...[implementation of api]...

if __name__ == '__main__':
    app.run(ssl_context=context, host=API_HOST, port=API_PORT, debug=True)

Sul computer client, ho questo codice. Ho anche copiato ca.crt a macchina:

import os
import requests
import ssl

def test():
    response = requests.get("https://[url of server]:8000/helloworld", verify='ca.crt')
    #response = requests.get("http://[url of server]:8000/helloworld")
    print(response.text);

def print_version():
    print(ssl.OPENSSL_VERSION)

if __name__ == "__main__":
    test()
    #print_version()

Dopo l'avvio del server api e quindi eseguire il codice del client, ottengo questo messaggio di errore al client:

requests.exceptions.SSLError: HTTPSConnectionPool(host='[url of server]', port=8000): Max retries exceeded with url: /helloworld (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1123)')))

Lo scopo di questo è così che posso avere il server sicuro di comunicazione con il server. Per il prossimo futuro, saranno letteralmente un server che invia le richieste https su un altro server. Sto utilizzando il formato di file sbagliato per tutto ciò che il client deve utilizzare per verificare? Io non sono un esperto in TLS con qualsiasi mezzo, così ho don so bene la differenza tra tutte le estensioni dei file (ho visto .file pem essere utilizzato in verificare, per esempio).

All'interno del link che ho mostrato prima, ho provato anche il percorso di produzione del cliente.pem e l'utilizzo che nel campo verifica.

flask flask-restful self-signed tls1.2
2021-11-23 18:09:50
1

Migliore risposta

0

Da il seguente errore, [url of server] deve essere come un vero e proprio host + port per esempio 127.0.0.1:8000

requests.exceptions.SSLError: HTTPSConnectionPool(host='[url of server]', port=8000): Max retries exceeded with url: /helloworld (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1123)')))
2021-12-03 16:30:10

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................