Actualizado el 21 de Marzo del 2018 (Publicado el 15 de Marzo del 2018)
754 visualizaciones desde el 15 de Marzo del 2018
145,9 KB
22 paginas
Creado hace 13a (03/06/2011)
INGENIERÍA DE PROTOCOLOS DE COMUNICACIONES -
IMPLEMENTACIÓN DE PROTOCOLOS
SOCKETS SEGUROS CON
SOCKETS SEGUROS CON
SOCKETS SEGUROS CON
SOCKETS SEGUROS CON
LIBRERÍAS DE OPENSSL
LIBRERÍAS DE OPENSSL
INGENIERÍA DE PROTOCOLOS DE COMUNICACIONES -
IMPLEMENTACIÓN DE PROTOCOLOS
PROTOCOLO DE SEGURIDAD SSL
PROTOCOLO DE SEGURIDAD SSL
PROTOCOLO DE SEGURIDAD SSL
PROTOCOLO DE SEGURIDAD SSL
FASE 1
FASE 2
FASE 3
FASE 4
INGENIERÍA DE PROTOCOLOS DE COMUNICACIONES -
IMPLEMENTACIÓN DE PROTOCOLOS
Implementación del Protocolo
Implementación del Protocolo
SSL en OpenSSL
SSL en
OpenSSL
Se utilizan los objetos “SSL_CTX” y “SSL”
– SSL_CTX (Objeto de Contexto): Se utiliza para configurar los
parámetros del protocolo
parámetros del protocolo
– SSL: El objeto SSL se asocia al objeto de contexto y hereda
sus parámetros
– El objeto SSL se asocia también a un socket TCP
– El objeto SSL se asocia también a un socket TCP
convencional
Para cargar los certificados de clave pública y
l
claves privadas se utilizan las funciones:
– SSL_CTX_use_certificate() y SSL_CTX_use_PrivateKey()
i d
tili
l
f
i
Para verificar el certificado del servidor (u
Para verificar el certificado del servidor (u
opcionalmente el del cliente) se usan las clases:
– SSL_CTX_set_verify() y SSL_CTX_load_verify_locations();
INGENIERÍA DE PROTOCOLOS DE COMUNICACIONES -
IMPLEMENTACIÓN DE PROTOCOLOS
Conexión SSL
Variables y Funciones OpenSSL
Variables y Funciones OpenSSL
#include <openssl/ssl.h>
Variable de Contexto
SSL_CTX * m_ctx
Se inicializa la variable de contexto
Se inicializa la variable de contexto
SSL_CTX *SSL_CTX_new(SSL_METHOD *meth);
Esta función verifica el certificado recibido de la otra entidad
void SSL CTX set verify(SSL CTX *ctx,int mode,
void SSL_CTX_set_verify(SSL_CTX ctx,int mode,
int (*callback)(int, X509 STORE CTX *));
int ( callback)(int, X509_STORE_CTX ));
Esta función carga los certificados que la entidad utiliza para verificar el certificado recibido de la otra parte
int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *Cafile, const char *CApath);
S
t
Se crea un objeto SSL que se inicializa con los valores de la variable de contexto
SSL *
bj t SSL
SSL_new(SSL_CTX *ctx);
i bl d
d l
t
i
i
i
li
l
l
Se asocia un socket tradicional al objeto SSL. El protocolo SSL se implementará en el socket
int
int
SSL set fd(SSL *s int socket);
SSL_set_fd(SSL s, int socket);
Se inicia el protocolo SSL del lado del cliente
int
SSL_connect(SSL *ssl);
Se envían datos por socket seguro
int
Se reciben datos por el socket seguro
int
SSL_read(SSL *ssl,void *buf,int num);
SSL_write(SSL *ssl,const void *buf,int num);
INGENIERÍA DE PROTOCOLOS DE COMUNICACIONES -
IMPLEMENTACIÓN DE PROTOCOLOS
Conexión SSL
Variables y Funciones OpenSSL
Variables y Funciones OpenSSL
#include <openssl/ssl.h>
Esta función se utiliza siempre en el servidor para cargar el certificado del protocolo SSL. Opcionalmente la
puede utilizar el cliente para autenticarse frente al servidor
SSL_CTX_use_certificate(m_ctx, cert)
tili
id
ti
d
li
t
t
f
l
t
l
p
Esta función se utiliza siempre en el servidor para cargar la clave privada del certificado del protocolo SSL.
Opcionalmente la puede utilizar el cliente para cargar la clave privada del certificado que presenta al servidor
cuando hay autenticación del cliente.
SSL_CTX_use_PrivateKey(m_ctx, pkey)
p
p
g
p
q
p
Esta función la utiliza la entidad servidor para iniciar el protocolo SSL si todo ha ido bien (se han verificado los
Esta función la utiliza la entidad servidor para iniciar el protocolo SSL si todo ha ido bien (se han verificado los
certificados y se han recibido correctamente los mensajes del protocolo) se pueden intercambiar
datos de forma segura
if (SSL_accept(ssl) < 0) { .... }
Esta función permite la obtención del certificado de la otra entidad a partir de la conexión SSL
X509 *
SSL_get_peer_certificate(const SSL *s);
INGENIERÍA DE PROTOCOLOS DE COMUNICACIONES -
IMPLEMENTACIÓN DE PROTOCOLOS
Certificados Necesarios para una Conexión
Certificados Necesarios para una Conexión
SSL sin Autenticación de Cliente
SSL sin Autenticación de Cliente
openssl>
(Generación de un par clave pública‐privada)
genrsa ‐out ClavePrivada.pem 4096
i d
l
Generación de un Certificado Autofirmado)
req ‐new ‐x509 ‐days 3650 ‐key ClavePrivada.pem ‐out Certificado.pem
P i d
t C tifi d
3650 k
509 d
Cl
(Creamos un certificado en formato p12, con la clave privada incorporada
protegida con un clave cifrada con 3des (“ppppp)
protegida con un clave cifrada con 3des ( ppppp)
pkcs12 ‐export ‐in Certificado.pem ‐inkey ClavePrivada.pem ‐out cert.p12
Ficheros Obtenidos:
•cert.p12: Certificado de Clave pública+Clave privada
•Certificado.pem: Certificado de Clave Pública
Certificado.pem: Certificado de Clave Pública
•ClavePrivada.pem: Clave privada
INGENIERÍA DE PROTOCOLOS DE COMUNICACIONES -
IMPLEMENTACIÓN DE PROTOCOLOS
Conexión SSL sin Autenticación de Cliente
Certificado Servidor Autofirmado
Certificado Servidor Autofirmado
Cliente
Conexión SSL
Servidor
CAFILE
“Certificado.pem”
“cert.p12”
CERTIFICADO SERVIDOR
Certificado Servidor
#define CAFILE "Certificado.pem
SSL_CTX * m_ctx = SSL_CTX_new(SSLv23_method());
SSL_CTX_set_verify(m_ctx,SSL_VERIFY_PEER|SSL_VERIFY_F
AIL IF NO PEER CERT NULL);
AIL_IF_NO_PEER_CERT, NULL);
SSL_CTX_load_verify_locations(m_ctx, CAFILE, CADIR)
SSL * m_ssl=SSL_new(m_ctx);
s=connectTCP(host pto ser ice)
s=connectTCP(host,pto_service);
SSL_set_fd(m_ssl,s);
if (SSL_connect(m_ssl) == -1) {}
SSL_write(m_ssl,,)/ SSL_read(m_ssl,,);
Clave Privada + Certificado Servidor
SSL_CTX *
m_ctx=SSL_CTX_new(SSLv23_server_method());
if (!GetCertAndPrivateKey(&cert, &pkey))
SSL_CTX_use_certificate(m_ctx, cert)
SSL_CTX_use_PrivateKey(m_ctx, pkey)
SSL *ssl = SSL_new(m_ctx);
sock = passiveTCP(service,5);
*pssock=accept(sock,(&fsin,(int FAR*)&alen);
SSL_set_fd(ssl,*pssock);
if (SSL_accept(ssl) < 0) { .... }
SSL_read(ssl,,)/SSL_write(ssl,,)
INGENIERÍA DE PROTOCOLOS DE COMUNICACIONES -
IMPLEMENTACIÓN DE PROTOCOLOS
Conexión SSL con Autenticación de Cliente
C tifi d Cli
d
Certificado Cliente/Servidor Autofirmado
id A t fi
t /S
Cliente
Cliente
Conexión SSL
CERT CLIENTE= CERT SERVIDOR
Servidor
Servidor
“Certificado.pem”
“cert.p12”
“Certificado.pem”
“cert.p12”
CAFILE
CERTIFICADO CLIENTE
CAFILE
CERTIFICADO SERVIDOR
Certificado Servidor
Clave Privada + Cert Cliente
Certificado Cliente
Clave Privada + Cert Servidor
#define CAFILE "Certificado.pem
SSL_CTX * m_ctx = SSL_CTX_new(SSLv23_method());
if (!GetCertAndPri ateKe (&cert &pke ))
if (!GetCertAndPrivateKey(&cert, &pkey))
SSL_CTX_use_certificate(m_ctx, cert)
SSL_CTX_use_PrivateKey(m_ctx, pkey)
SSL_CTX_set_verify(m_ctx, ……);
SSL_CTX_load_verify_locations(m_ctx, CAFILE, CADIR)
SSL * m_ssl=SSL_new(m_ctx);
s=connectTCP(host,pto service);
s connectTCP(host,pto_service);
SSL_set_fd(m_ssl,s);
if (SSL_connect(m_ssl) == -1) {}
SSL_write(m_ssl,,)/ SSL_read(m_ssl,,);
# d fi
# define CAFILE "Certificado.pem
SSL_CTX * m_ctx=SSL_CTX_new(SSLv23_server_method());
CAFILE "C tifi d
if (!GetCertAndPrivateKey(&cert, &pkey))
SSL_CTX_use_certificate(m_ctx, cert)
)
SSL_CTX_use_PrivateKey(m_ctx, pkey)
(
SSL_CTX_set_verify(m_ctx,SSL_VERIFY_PEER|SSL_VERIFY_
FAIL_IF_NO_PEER_CERT, NULL);
SSL CTX load verify locations(m ctx CAFILE CADIR)
SSL_CTX_load_verify_locations(m_ctx, CAFILE, CADIR)
SSL *ssl = SSL_new(m_ctx);
sock = passiveTCP(service,5);
*
)
*pssock=accept(sock,(&fsin,(int FAR*)&alen);
(i t FAR*)& l
k (&f i
k
t(
SSL_set_fd(ssl,*pssock);
if (SSL_accept(ssl) < 0) { .... }
SSL_read(ssl,,)/SSL_write(ssl,,)
INGENIERÍA DE PROTOCOLOS DE COMUNICACIONES -
IMPLEMENTACIÓN DE PROTOCOLOS
SOCKETS SEGUROS EN JAVA
SOCKETS SEGUROS EN JAVA
SOCKETS SEGUROS EN JAVA
SOCKETS SEGUROS EN JAVA
INGENIERÍA DE PROTOCOLOS DE COMUNICACIONES -
IMPLEMENTACIÓN DE PROTOCOLOS
Conexión SSL
Conexión SSL
A t fi
C tifi d S
d
id A t fi
d (I)(I)
id
Autofirmado (I)(I)
Certificado Servidor Autofirmado
C tifi d S
Certificado Servidor
Cliente
Conexión SSL
Servidor
Truststore file
“AlmacenTrust”
“AlmacenSR”
Keystore File
Certificado Servidor
(TrustCertEntry)
(
y)
Clave Privada + Certificado Servidor
(Key Entry)
(
y)
y
System.setProperty("javax.net.ssl.trustStore","AlmacenTrust");
SSLSocketFactory sslsocketfactory = (SSLSocketFactory)
SSLS k tF t
lt()
SSLSocketFactory.getDefault();
tD f
SSLSocket sslsocket = (SSLSocket)
sslsocketfactory.createSocket(dirip, ptoint);
System.setProperty("javax.net.ssl.keyStore","AlmacenSR");
System.setProperty("javax.net.ssl.keyStorePassword","oooooo");
SSLS
SSLServerSocketFactory sslserversocketfactory =
(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
S k tF t
k tf
t
l
SSLServerSocket sslserversocket =(SSLServerSocket)
sslserversocketfactory.createServerSocket(9999);
SLSocket sslsocket = (SSLSocket) sslserversocket.accept();
INGENIERÍA DE PROTOCOLOS DE COMUNICACIONES -
IMPLEMENTACIÓN DE PROTOCOLOS
Conexión SSL
Conexión SSL
A t fi
C tifi d S
d
id A t fi
d (II)(II)
id
Autofirmado (II)(II)
Certificado Servidor Autofirmado
C tifi d S
Certificado Servidor
1. Generamos el par de claves pública y secreta y se
almacenan en el almacen AlmacenSR
keytool -genkey -alias CertificadoAutofirmado -keyalg RSA -validity "100”
-keystore AlmacenSR -keypass oooooo -storepass oooooo
keytool -list -v -keystore AlmacenSR
2 Generamos en un fichero .cer el certificado de clave publica
keytool -export -alias CertificadoAutofirmado -keystore AlmacenSR -rfc -file
CertAutofirmado.cer
INGENIERÍA DE PROTOCOLOS DE COMUNICACIONES -
IMPLEMENTACIÓN DE PROTOCOLOS
Conexión SSL
Conexión SSL
d (III)
A t fi
C tifi d S
d
id A t fi
id
Autofirmado (III)
(III)
(III)
Certificado Servidor Autofirmado
C tifi d S
Certificado Servidor
3. Metemos el Certificado Autofirmado en un almacén para
que lo use el Cl
Comentarios de: Sockets seguros con librerías de OpenSSL (0)
No hay comentarios