Lecture 3 Programming with OpenSSL



Similar documents
Forward Secrecy: How to Secure SSL from Attacks by Government Agencies

Netzwerksicherheit Übung 6 SSL/TLS, OpenSSL

Chapter 17. Transport-Level Security

[SMO-SFO-ICO-PE-046-GU-

Secure Socket Layer. Carlo U. Nicola, SGI FHNW With extracts from publications of : William Stallings.

Transport Level Security

Configuring SSL Termination

Security. Contents. S Wireless Personal, Local, Metropolitan, and Wide Area Networks 1

Learning Network Security with SSL The OpenSSL Way

Secure Sockets Layer (SSL ) / Transport Layer Security (TLS) Network Security Products S31213

Chapter 10. Network Security

CIS 6930 Emerging Topics in Network Security. Topic 2. Network Security Primitives

Secure Socket Layer (SSL) and Transport Layer Security (TLS)

Lecture 9: Application of Cryptography

Symmetric and Public-key Crypto Due April , 11:59PM

Network Security. Computer Networking Lecture 08. March 19, HKU SPACE Community College. HKU SPACE CC CN Lecture 08 1/23

SBClient SSL. Ehab AbuShmais

Network Security. Abusayeed Saifullah. CS 5600 Computer Networks. These slides are adapted from Kurose and Ross 8-1

CS 758: Cryptography / Network Security

Lukasz Pater CMMS Administrator and Developer

Domino and Internet. Security. IBM Collaboration Solutions. Ask the Experts 12/16/2014

Chapter 7 Transport-Level Security

Asymmetric Encryption

Implementing SSL Security on a PowerExchange Network

Security. Learning Objectives. This module will help you...

Package PKI. July 28, 2015

Secure Socket Layer. Introduction Overview of SSL What SSL is Useful For

Overview of CSS SSL. SSL Cryptography Overview CHAPTER

Overview. SSL Cryptography Overview CHAPTER 1

Security Protocols HTTPS/ DNSSEC TLS. Internet (IPSEC) Network (802.1x) Application (HTTP,DNS) Transport (TCP/UDP) Transport (TCP/UDP) Internet (IP)

SSL/TLS: The Ugly Truth

MatrixSSL Developer s Guide

Secure Socket Layer (TLS) Carlo U. Nicola, SGI FHNW With extracts from publications of : William Stallings.

Security Policy for Oracle Advanced Security Option Cryptographic Module

CRYPTOGRAPHY IN NETWORK SECURITY

Network Security Essentials Chapter 5

How To Understand And Understand The Ssl Protocol ( And Its Security Features (Protocol)

Cryptographic Services Guide

A Brief Guide to Certificate Management

Network Security [2] Plain text Encryption algorithm Public and private key pair Cipher text Decryption algorithm. See next slide

Computer Networks 1 (Mạng Máy Tính 1) Lectured by: Dr. Phạm Trần Vũ MEng. Nguyễn CaoĐạt

3.2: Transport Layer: SSL/TLS Secure Socket Layer (SSL) Transport Layer Security (TLS) Protocol

Final exam review, Fall 2005 FSU (CIS-5357) Network Security

Lecture 9 - Network Security TDTS (ht1)

Einführung in SSL mit Wireshark

SECURITY IN NETWORKS

First Semester Examinations 2011/12 INTERNET PRINCIPLES

Virtual Private Network (VPN) Lab

Client Server Registration Protocol

TLS and SRTP for Skype Connect. Technical Datasheet

Waspmote Encryption Libraries. Programming guide

OpenSSL (lab notes) Definition: OpenSSL is an open-source library containing cryptographic tools.

Encrypted Connections

SSL Protect your users, start with yourself

mod_ssl Cryptographic Techniques

IT Networks & Security CERT Luncheon Series: Cryptography

Factory Application Certificates and Keys Products: SB700EX, SB70LC

GNUTLS. a Transport Layer Security Library This is a Draft document Applies to GnuTLS by Nikos Mavroyanopoulos

Security. Friends and Enemies. Overview Plaintext Cryptography functions. Secret Key (DES) Symmetric Key

Creation and Management of Certificates

Cisco Expressway Certificate Creation and Use

Managing the SSL Certificate for the ESRS HTTPS Listener Service Technical Notes P/N REV A01 January 14, 2011

Cisco TelePresence VCS Certificate Creation and Use

Web Security Considerations

Chapter 8. Network Security

Apache, SSL and Digital Signatures Using FreeBSD

Apache Security with SSL Using Ubuntu

Real-Time Communication Security: SSL/TLS. Guevara Noubir CSU610

Communication Systems SSL

Communication Systems 16 th lecture. Chair of Communication Systems Department of Applied Sciences University of Freiburg 2009

Authenticity of Public Keys

SecureDoc Disk Encryption Cryptographic Engine

Communication Security for Applications

Savitribai Phule Pune University

Module 7 Security CS655! 7-1!

Configuring Secure Socket Layer (SSL)

CSCE 465 Computer & Network Security

Network-Enabled Devices, AOS v.5.x.x. Content and Purpose of This Guide...1 User Management...2 Types of user accounts2

INF3510 Information Security University of Oslo Spring Lecture 9 Communication Security. Audun Jøsang

Understanding digital certificates

Chapter 7: Network security

Working with Certificate and Key Files in MatrixSSL

Security Protocols/Standards

Cryptography and Network Security Sicurezza delle reti e dei sistemi informatici SSL/TSL

2014 IBM Corporation

EXAM questions for the course TTM Information Security May Part 1

Outline. Transport Layer Security (TLS) Security Protocols (bmevihim132)

Common Pitfalls in Cryptography for Software Developers. OWASP AppSec Israel July The OWASP Foundation

A PKI case study: Implementing the Server-based Certificate Validation Protocol

Security OpenSSL SSL. Roberta Daidone.

, ) I Transport Layer Security

Network Security. Gaurav Naik Gus Anderson. College of Engineering. Drexel University, Philadelphia, PA. Drexel University. College of Engineering

Key Management Interoperability Protocol (KMIP)

RELEASE NOTES. Table of Contents. Scope of the Document. [Latest Official] ADYTON Release corrections. ADYTON Release 2.12.

Authentication applications Kerberos X.509 Authentication services E mail security IP security Web security

FIPS Non- Proprietary Security Policy. McAfee SIEM Cryptographic Module, Version 1.0

Overview of Cryptographic Tools for Data Security. Murat Kantarcioglu

Introduction to Cryptography

TLS/SSL in distributed systems. Eugen Babinciuc

Package PKI. February 20, 2013

Transcription:

Lecture 3 Programming with OpenSSL Patrick P. C. Lee Tsinghua Summer Course 2010 3-1

Roadmap OpenSSL Why Cryptosystems Fail? Tsinghua Summer Course 2010 3-2

SSL and OpenSSL SSL is the security protocol behind secure HTTP (HTTPS). It can secure any protocol that works over TCP Will cover the theoretical details in Web Security module OpenSSL is a full-featured implementation of SSL, including TLS (transport layer security) Tsinghua Summer Course 2010 3-3

Overview of OpenSSL OpenSSL is an open-source implementation of cryptographic functions. It includes executable commands with cryptographic functions, and a library of APIs with which programmers can use to develop cryptographic applications. Its design follows the object-oriented principle. Each cryptographic algorithm is built upon a context, an object that holds the necessary parameters. Tsinghua Summer Course 2010 3-4

Installation of OpenSSL How to install: Get the latest stable source from http://www.openssl.org, and run (as root): #./config # make # make test # make install How to compile (as normal users): % gcc Wall test_openssl.c o test_openssl -lcrypto Tsinghua Summer Course 2010 3-5

BIGNUM Arbitrary Precision Math Public key cryptography handles very large integers. Standard C data types are not enough BIGNUM package has virtually no limits on upper bounds of numbers Header file: #include <openssl/bn.h> Reference: http://www.openssl.org/docs/crypto/bn.html Tsinghua Summer Course 2010 3-6

Initializing & Destroying BIGNUMs A BIGNUM is an object (or context) that contains dynamically allocated memory BIGNUM static_bn, *dynamic_bn; /* initialize a static BIGNUM */ BN_init(&static_bn); /* allocate a dynamic BIGNUM */ dynamic_bn = BN_new(); /* free the BIGNUMs */ BN_free(dynamic_bn); BN_free(&static_bn); Tsinghua Summer Course 2010 3-7

Copying BIGNUMs Deep copy is required when you copy a BIGNUM object BIGNUM a, b, *c; /* wrong way */ a = b; *c = b; /* right away */ BN_copy(&a, &b); /* copies b to a */ c = BN_dup(&b); /* creates c and initialize it to same value as b */ Tsinghua Summer Course 2010 3-8

BIGNUM to Binary Sometimes you need to convert a BIGNUM into binary representation for: Store it in a file Send it via a socket connection Similarly, we can convert a BIGNUM into a decimal or hexadecimal representation Tsinghua Summer Course 2010 3-9

BIGNUM to Binary How to convert? BIGNUM* num; /* converting from BIGNUM to binary */ len = BN_num_bytes(num) buf = (unsigned char*)calloc(len, sizeof(unsigned char)); len = BN_bn2bin(num, buf); /* converting from binary to BIGNUM */ BN_bin2bn(buf, len, num); num = BN_bin2bn(buf, len, NULL); Tsinghua Summer Course 2010 3-10

BIGNUM to Binary Pitfall if you don t pay attention to the actual length of the binary string. Suppose I work on 1024-bit RSA const int RSA_SIZE = 128; /* in bytes */ BIGNUM* num; /*.. set num to 3 as public key */ /* converting from BIGNUM to binary */ buf = (unsigned char*)calloc(rsa_size, sizeof(unsigned char)); BN_bn2bin(num, buf); /* converting from binary to BIGNUM (use 128 bytes directly) */ BN_bin2bn(buf, RSA_SIZE, num); num = BN_bin2bn(buf, RSA_SIZE, NULL); /* WRONG RESULT: num <> 3 */ Tsinghua Summer Course 2010 3-11

BIGNUM to Binary The binary representation of BIGNUM is in big-endian format. After BN_bn2bin(), Address 0 1 2 127 3 0 0 0 After BN_bin2bn(), num = 3 * 2 127 * 8 Tsinghua Summer Course 2010 3-12

BIGNUM to Binary You can store the actual length separately. Or, to save the overhead of storing the length, move the significant string to the right const int RSA_SIZE = 128; BIGNUM* num; /*.. set num to 3 as public key */ /* converting from BIGNUM to binary */ buf = (unsigned char*)calloc(rsa_size, sizeof(unsigned char)); BN_bn2bin(num, buf + RSA_SIZE BN_num_bytes(num)); Tsinghua Summer Course 2010 3-13

Math Operations of BIGNUM Many operations are included in BIGNUM package, including modular arithmetic. Example: BN_mod_exp() BIGNUM *r, *g, *x, *p; BN_CTX* ctx = BN_CTX_new(); /* store temporary results */ /*.. call BN_new() on r, g, x, p */ /* r = g^x mod p */ BN_mod_exp(r, g, x, p, ctx); /* when done, free r, g, x, p, and ctx */ BN_CTX stores temporary values of operations so as to improve the performance. Tsinghua Summer Course 2010 3-14

Generating Prime BIGNUM BN_generate_prime() generates pseudorandom prime numbers Instead of factoring, check if a number is prime after a number of primality tests The generation is quite efficient BIGNUM* prime = BN_generate_prime(NULL, bits, safe, NULL, NULL, callback, NULL); Safe primes p is prime and (p-1)/2 is also prime. Tsinghua Summer Course 2010 3-15

Symmetric Key Crypto in OpenSSL We choose AES with CBC We need to pad the last block if the plaintext length is not a multiple of block size (i.e., 128 bits / 16 bytes) In general, you can add a few more junk blocks to decouple the plaintext length and ciphertext length, with a tradeoff of longer ciphertext Tsinghua Summer Course 2010 3-16

AES with CBC in OpenSSL Find the actual length of the encrypted message. Make it a multiple of 128 bits #include <openssl/aes.h>... unsigned int message_len = strlen((char*)input_string) + 1; // including \0 unsigned encrypt_len = (message_len % AES_BLOCK_SIZE == 0)? message_len : (message_len / AES_BLOCK_SIZE + 1) * AES_BLOCK_SIZE; Define the key (assuming 128 bits) unsigned char key[16]; AES aes; int ret = AES_set_encrypt_key(key, 128, &aes); // ret < 0 error Define the IV: unsigned char iv[aes_block_size]; memset(iv, 0, AES_BLOCK_SIZE); Tsinghua Summer Course 2010 3-17

AES with CBC in OpenSSL Encrypt the plaintext (note that iv will be updated) AES_cbc_encrypt(input_string, encrypt_string, encrypt_len, &aes, iv, AES_ENCRYPT); Decrypt the ciphertext. The decryption side must synchronize on the iv and key. iv can be sent in plain, but key must be sent securely AES_set_decrypt_key(key, 128, &aes); memset(iv, 0, AES_BLOCK_SIZE);... AES_cbc_encrypt(encrypt_string, decrypt_string, encrypt_len, &aes, iv, AES_DECRYPT); Tsinghua Summer Course 2010 3-18

Public Key Crypto in OpenSSL Focus on: RSA for public key encryption Diffie-Hellman for key management DSA for digital signatures Public key crypto operates on BIGNUMs typedef struct { BIGNUM *n; // public modulus BIGNUM *e; // public exponent BIGNUM *d; // private exponent BIGNUM *p; // secret prime factor BIGNUM *q; // secret prime factor // } RSA; Tsinghua Summer Course 2010 3-19

RSA in OpenSSL Bob first generates the RSA keys, e.g., 1024 bits and exponent 3. #include <openssl/rsa.h> RSA* rsa = RSA_generate_key(1024, 3, NULL, NULL); Bob passes public keys to Alice #include <openssl/bn.h> unsigned char* n_b = (unsigned char*)calloc(rsa_size(rsa), sizeof(unsigned char)); unsigned char* e_b = (unsigned char*)calloc(rsa_size(rsa), sizeof(unsigned char)); int n_size = BN_bn2bin(rsa->n, n_b); int b_size = BN_bn2bin(rsa->e, e_b); Alice constructs the RSA context from public params RSA* encrypt_rsa = RSA_new(); encrypt_rsa->n = BN_bin2bn(n_b, n_size, NULL); encrypt_rsa->e = BN_bin2bn(e_b, b_size, NULL); Tsinghua Summer Course 2010 3-20

RSA in OpenSSL Alice can now encrypt data. unsigned char* encrypt_string = (unsigned char*) calloc(rsa_size(encrypt_rsa), sizeof(unsigned char)); int encrypt_size = RSA_public_encrypt(strlen((char*)input_string), input_string, encrypt_string, encrypt_rsa, RSA_PKCS1_OAEP_PADDING); RSA_PKCS1_OAEP_PADDING is recommended. The size of the input message block must be smaller than RSA(size) - 41. Bob can then decrypt unsigned char* decrypt_string = (unsigned char*)calloc(rsa_size(rsa), sizeof(unsigned char)); int decrypt_size = RSA_private_decrypt(encrypt_size, encrypt_string, decrypt_string, rsa, RSA_PKCS1_OAEP_PADDING); Tsinghua Summer Course 2010 3-21

RSA in OpenSSL Padding encodes packets before encryption Padding schemes in RSA RSA_PKCS1_PADDING: plaintext length smaller than RSA_size(rsa) 11 RSA_PKCS1_OAEP_PADDING: plaintext length smaller than RSA_size(rsa) 41 RSA_SSLV23_PADDING: rarely used RSA_NO_PADDING: Assumes the caller performs padding. Plaintext length must equal to RSA_size(rsa), not recommended. Tsinghua Summer Course 2010 3-22

RSA in OpenSSL Instead of generating parameters in a program, you can do it in command line and save parameters into a PEM file for permanent use PEM file is a base64-encoded file format that represents keys in a file E.g., generate 1024-bit parameters % openssl genrsa out rsa1024.pem 1024 The private key contains public key info as well. In program, call PEM_read_RSAPrivateKey() RSA* rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL)); Tsinghua Summer Course 2010 3-23

Diffie-Hellman in OpenSSL Header file: #include <openssl/dh.h> A DH struct: typedef struct { BIGNUM *p; // prime number BIGNUM *g; // generator BIGNUM *pub_key; // public key BIGNUM *priv_key; // private key } DH; Tsinghua Summer Course 2010 3-24

Diffie-Hellman in OpenSSL Generate DH parameters in command line (e.g., prime p and generator g) % openssl dhparam out dh1024.pem 1024 Read parameters from the file #include <openssl/dh.h> #include <openssl/pem.h> FILE* fp = fopen( dh1024.pem, r ); DH* dh1 = PEM_read_DHparams(fp, NULL, NULL, NULL); Tsinghua Summer Course 2010 3-25

Diffie-Hellman in OpenSSL No keys are in the file. You need to generate keys separately DH_generate_key(dh1); Compute the secret key, assuming public key was sent over network and reconstructed into BIGNUM object unsigned char* key1 = (unsigned char*)calloc(dh_size(dh1), sizeof(unsigned char)); unsigned char* key2 = (unsigned char*)calloc(dh_size(dh2), sizeof(unsigned char)); DH_compute_key(key1, dh2_pub_key, dh1); DH_compute_key(key2, dh1_pub_key, dh2); Tsinghua Summer Course 2010 3-26

DSA in OpenSSL Sender generates DSA parameters (e.g., 1024 bits) #include <openssl/dsa.h> DSA* dsa = DSA_generate_parameters(1024, NULL, 0, NULL, NULL, NULL); And generate the keys DSA_generate_key(dsa); Tsinghua Summer Course 2010 3-27

DSA in OpenSSL To sign a message: unsigned char* sign_string = (unsigned char*)calloc(dsa_size(dsa), sizeof(unsigned char)); int ret = DSA_sign(0, input_string, strlen((char*)input_string), sign_string, &sig_len, dsa); To verify a message int is_valid = DSA_verify(0, input_string, strlen((char*)input_string), sign_string, sig_len, dsa); is_valid = 1 means verified, 0 means wrong signature Tsinghua Summer Course 2010 3-28

Hash Functions in OpenSSL Based on three functions: *Init(): initialize the hash structure *Update(): keep adding messages to be hashed (can be called multiple times) *Final(): compute the hash value Example: md5 #include <openssl/md5.h> MD5_CTX hash_ctx; MD5_Init(&hash_ctx); // initialize char input_string[100]; strcpy(input_string, abcdedfg ); MD5_Update(&hash_ctx, input_string, strlen(input_string)); // update unsigned char hash_ret[16]; MD5_Final(hash_ret, &hash_ctx); // compute the hash vlaue Tsinghua Summer Course 2010 3-29

Certificates in OpenSSL Goal: verify a signature using a public key certificate issued by a certificate authority Main idea: generate certificates in command line, and call the certs from programs through APIs Tsinghua Summer Course 2010 3-30

Certificates in OpenSSL Step 1: we need a CA. First, create a self-signed certificate % openssl req -x509 -newkey rsa -out cacert.pem -outform PEM days 365 \ -key cakey.pem Let s use 5470 as the passphrase. Specify a one-year expiration period using -days The Certificate is encoded in PEM format. You can see the content with the command: % openssl x509 in cacert.pem text noout Default configuration is used for certificate generation. Can provide our own configuration. Two files created: cakey.pem private key of CA cacert.pem certificate of CA Tsinghua Summer Course 2010 3-31

Certificates in OpenSSL Step 2: When a user wants to apply for a certificate, the CA will generate a new public/private key pair and the corresponding certificate request. % openssl genpkey -algorithm RSA -out key.pem -aes-128-cbc \ -pkeyopt rsa_keygen_bits:1024 % openssl req -new -key key.pem -keyform PEM -out req.pem \ -outform PEM The above command will encrypt the private key file (key.pem) with a passphrase when the certificate is issued. It will prompt for a passphrase. Let s use 5470 for the passphrase. Tsinghua Summer Course 2010 3-32

Certificates in OpenSSL Step 3: The CA will then generate a certificate based on the request. % openssl ca -in req.pem -out cert.pem -config ca.conf See ca.conf for the format. Note that before calling the command, we must have prepared two files: index.txt (which could be empty) and serial (which stores a number, e.g., 01). The private key file (key.pem) and the certificate (cert.pem) will be given to the user. Tsinghua Summer Course 2010 3-33

How should I sign/verify? People who sign: Use the private key to sign Issue the public key certificate People who verify: Use the public key in the certificate to verify Here, we demonstrate the use of EVP API: EVP API provides a common interface to every cipher OpenSSL exports When you change to a new cipher algorithm, you only register the new algorithm during initialization Tsinghua Summer Course 2010 3-34

Signing with Cert in OpenSSL Step 1: load the private key file. Since the file is encrypted, we need to specify which encryption algorithm is used. To save troubles, we just call OpenSSL add all algorithms() to include all possible cipher and digest algorithms. #include <openssl/evp.h> #include <openssl/pem.h>... OpenSSL_add_all_algorithms();... // load the private key FILE* fp = fopen("key.pem", "r")) == NULL); EVP_PKEY* priv_key = PEM_read_PrivateKey(fp, NULL, NULL, (char*) 5470"); fclose(fp); if (priv_key == NULL) { fprintf(stderr, "cannot read private key.\n"); exit(-1); } Tsinghua Summer Course 2010 3-35

Signing with Cert in OpenSSL Step 2: sign the message digest You don t need to sign the whole message. Just sign the (shorter) message digest. int sig_len = 128; // 1024-bit key unsigned char sign_string[128]; EVP_MD_CTX evp_md_ctx; EVP_SignInit(&evp_md_ctx, EVP_sha1()); EVP_SignUpdate(&evp_md_ctx, input_string, strlen((char*)input_string)); if (EVP_SignFinal(&evp_md_ctx, sign_string, &sig_len, priv_key) == 0) { fprintf(stderr, "Unable to sign.\n"); exit(-1); } Tsinghua Summer Course 2010 3-36

Verifying with Cert in OpenSSL Step 1: Before we can verify a message, we need to first obtain the certificate of the signer. The certificate also contains the public key. X509* cert; EVP_PKEY* pub_key; FILE* fp = fopen("cert.pem", "r")); if ((cert = PEM_read_X509(fp, NULL, NULL, NULL)) == NULL) { fprintf(stderr, "cannot read cert file\n"); exit(-1); } fclose(fp); if ((pub_key = X509_get_pubkey(cert)) == NULL) { fprintf(stderr, "cannot read x509 s public key\n"); exit(-1); } Tsinghua Summer Course 2010 3-37

Verifying with Cert in OpenSSL Step 2: verify the message digest EVP_VerifyInit(&evp_md_ctx, EVP_sha1()); EVP_VerifyUpdate(&evp_md_ctx, input_string, strlen((char*)input_string)); if (EVP_VerifyFinal(&evp_md_ctx, sign_string, sig_len, pub_key)) { printf("verified\n"); } else { printf("wrong\n"); } Tsinghua Summer Course 2010 3-38

Roadmap OpenSSL SSL/TLS Programming Why Cryptosystems Fail? Tsinghua Summer Course 2010 3-39

How to implement a network application? Implementation of each protocol layer is located in either the userspace, kernel-space, or the device (hardware) Application Transport Client/Server Program User Network Data Link Physical Kernel Device Tsinghua Summer Course 2010 3-40

How to implement a network application? We need a door so that a network application can send/receive messages to/from the network The door should appear between the user space and kernel space so that details of the kernel space can be hidden Socket is the door! Client/Server Program Application Transport User What is the interface? Kernel Tsinghua Summer Course 2010 3-41

How to implement a network application? A socket is a host-local, application-created, OScontrolled interface (a door ) into which application process can both send and receive messages to/from another application process Similar to a file descriptor, which interfaces between an application and a file One socket is tied to one application process (or thread) An application can create many processes (and hence sockets) Client/Server Program Socket Socket Socket TCP Control UDP Control Tsinghua Summer Course 2010 3-42

Socket programming Socket programming is to build client/server application that communicate using sockets Socket API introduced in BSD4.1 UNIX, 1981 explicitly created, used, released by apps client/server paradigm two types of transport service via socket API: unreliable datagram reliable, byte stream-oriented Tsinghua Summer Course 2010 3-43

Socket-programming using TCP TCP service: reliable transfer of bytes from one process to another. An application may view TCP a reliable, inorder pipe (or stream). controlled by application developer controlled by operating system process socket TCP with buffers, variables internet process socket TCP with buffers, variables controlled by application developer controlled by operating system host or server host or server Tsinghua Summer Course 2010 3-44

Socket programming using TCP Before client contacts server: server process must first be running server must have created socket (door) that welcomes client s contact Client contacts server by: creating client-local TCP socket specifying IP address, port number of server process When client creates socket: client TCP establishes connection to server TCP When contacted by client, server creates new TCP socket for server process to communicate with client allows server to talk with multiple clients source port numbers used to distinguish clients Tsinghua Summer Course 2010 3-45

Socket programming with TCP TCP server When a TCP server creates a socket, it needs to specify: Identifier of the socket Connection mode (TCP/UDP) Analogous to when you open a file in C, you need to specify: location of the file access mode (e.g., readonly, write-only) socket bind listen accept send/recv close 3-way handshake share data Operations of socket programming in C/C++ TCP client socket connect send/recv close Tsinghua Summer Course 2010 3-46

SSL/TLS Programming Our goal is to enhance socket programming with Secure Sockets Layer (SSL) and Transport Layer Security (TLS) We only provide templates here, with many subtlties ignored. Tsinghua Summer Course 2010 3-47

SSL/TLS Programming Flow of SSL programming Can be viewed as extensions of socket programming Tsinghua Summer Course 2010 3-48

Steps of SSL/TLS Programming Step 1: Initialize the SSL library to register all cipher and hash algorithms Step 2: Create the SSL context structure e.g., specify the SSL versions Step 3: Set up certificates and keys SSL server server s own certificate (mandatory) CA certificate (optional) SSL client CA s certificate (mandatory) for verifying server s cert client s own certificate (optional) Tsinghua Summer Course 2010 3-49

Steps of SSL/TLS Programming Step 4: Set up certificate verification can specify the chain length (verify_depth) Client can set SSL_VERIFY_PEER to verify server s certificate is indeed issued by CA s cert Step 5: Create SSL structure and TCP/IP sockets, and bind them together Tsinghua Summer Course 2010 3-50

Steps of SSL/TLS Programming Step 6: SSL handshake invoked when client calls SSL_connect() if certificate verification is specified before, the actual verification will be carried out in this phase Step 7: Transmit SSL data Step 8: Shutdown SSL structure See source code Tsinghua Summer Course 2010 3-51

Summary on OpenSSL Low-level design: BIGNUM Use BIGNUM to build your own cryptographic primitive Mid-level design: Cryptographic primitives Use different combinations of cryptographic primitives to design a cryptosystem High-level design: SSL programming Follow the SSL implementation Tsinghua Summer Course 2010 3-52

Roadmap OpenSSL Why Cryptosystems Fail? Tsinghua Summer Course 2010 3-53

Why Cryptosystems Fail? [Anderson 1993] In practice, most frauds were not caused by cryptanalysis, but by implementation errors and management failures Public feedback about how cryptosystems fail is limited Let s use ATM as a case study You provide your bank card and PIN to an ATM machine, and you access your account Tsinghua Summer Course 2010 3-54

Case Study ATM How ATM fraud takes place? Insider knowledge by bank clerks who issue bank cards by technical staff who record customers PINs Outsider attacks observe the PINs entered by customers at ATM queues, and pick up discarded ATM tickets that have full account numbers PINs may not be fully random e.g., digit 1 + digit 3 is equal to digit 2 + digit 4 so that offline ATMs and point-of-sale devices can recognize Tsinghua Summer Course 2010 3-55

Case Study - ATM How to keep keys secret is a major problem in cryptosystem design Standard approach: store keys in a tamper-resistant security module (e.g., trusted computing module) Yet, not all banks use security modules to protect keys (as of 1993) too expensive, too difficult to install, etc. Tsinghua Summer Course 2010 3-56

Open Issues to Equipment Vendors Security products provide raw encryption capabilities, and leave application designers to worry about protocols and integrate into their systems Problems on application designers side: Lack of proper skills Security functions neglected Changing threats Sloppy quality control Tsinghua Summer Course 2010 3-57

Implications for Equipment Vendors Three courses of actions: to design products that can be integrated and maintained by real experts to train and certify client personnel to supply their own personnel to implement, maintain, and manage the system Tsinghua Summer Course 2010 3-58

Wrong Threat Models Threats are misjudged. Assuming criminals have the expertise Assuming customer sites have the expertise in building secure systems Shift to new security paradigm? Tsinghua Summer Course 2010 3-59

Conclusions Cryptosystem designers have limited information of how their system fail, and hence accept wrong threat models As a result, security failures are mainly due to implementation and management errors Tsinghua Summer Course 2010 3-60

References John Viega et al., Network Security with OpenSSL, O Reilly, 2002 HP, SSL Programming Tutorial, http://h71000.www7.hp.com/doc/83final/ba554_ 90007/ch04s03.html Ross Anderson, Why Cryptosystems Fail, ACM CCS 1993 Tsinghua Summer Course 2010 3-61