Mind Chasers Inc.

Build and Debug Master Branch of OpenSSL on Ubuntu 18.04

A brief review of cloning, building, and debugging the master branch of the OpenSSL project along with stepping into the crypto library with GDB.

advertisement:

Overview

OpenSSL is the defacto, open source cryptography software package for Linux and other operating systems. It consists of both a command line tool and libraries for cryptography. It has cutting edge support of cryptographic algorithms and an experienced team of developers. However, there seems to be a dearth of recent documentation and examples regarding developing with OpenSSL, especially with the latest API. Therefore, this article is a brief summary on getting started with OpenSSL development.

The openssl executable itself is a command line tool that can be used interactively. The simplest of examples using the openssl executable packaged with Ubuntu 18.04 is shown below:

# make sure we have the latest package installed on Ubuntu:
$ sudo apt upgrade openssl
Reading package lists... Done
Building dependency tree       
Reading state information... Done
openssl is already the newest version (1.1.0g-2ubuntu4.1).
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

$ which openssl
/usr/bin/openssl

$ openssl
OpenSSL> version
OpenSSL 1.1.0g  2 Nov 2017
OpenSSL> quit

Note that the following can be found on openssl.org: "The 1.1.0 series is currently only receiving security fixes and will go out of support on 11th September 2019." Also, Ubuntu 18.10 (Cosmic) makes use of openssl 1.1.1.

OpenSSL relies on two important libraries that are part of the OpenSSL project:

  • libssl provides the client and server-side implementations for SSLv3 and TLS.
  • libcrypto provides general cryptographic and X.509 support needed by SSL/TLS but not logically part of it

We have found that the best way to learn how to work with the OpenSSL libraries is to look at how the openssl application makes use of them itself, and this is shown by example below.

Related Links

Build Master Branch on Ubuntu 18.04

Below we show steps to clone, build, and use the master branch of the OpenSSL repository. We install it to /opt/openssl and make use of it in such a way that it does not collide with the version installed & supported by Ubuntu 18.04. You may wish to build and install your own version of OpenSSL in a virtual environment or container.

$ cd /build/  # /build is where we build our code

$ git clone https://github.com/openssl/openssl.git
$ cd /build/openssl

$find . -name tls1.h | xargs grep TLS1_3_VERSION
# define TLS1_3_VERSION                  0x0304
# define TLS_MAX_VERSION                 TLS1_3_VERSION

$ mkdir build; cd /build/openssl/build

$ ../config -v --prefix=/opt/openssl --openssldir=$HOME/openssl --debug
Operating system: x86_64-whatever-linux2
/usr/bin/env __CNF_CPPDEFINES='' __CNF_CPPINCLUDES='' __CNF_CPPFLAGS='' __CNF_CFLAGS='' __CNF_CXXFLAGS='' __CNF_LDFLAGS='' __CNF_LDLIBS='' /usr/bin/perl ../Configure linux-x86_64 '--prefix=/opt/openssl' '--openssldir=/home/bcochran/openssl' '--debug'
Configuring OpenSSL version 1.1.2-dev (0x10102000L) for linux-x86_64
Using os-specific seed configuration
Creating configdata.pm
Creating Makefile
...

config options used (see INSTALL for extensive list of supported options):

  • --prefix: top of the installation directory tree
  • --openssldir: directory for OpenSSL configuration files, and also the default certificate and key store.
  • --debug: build with debugging symbols

Let's look a little further into how we configured the build. First, let's make sure we configured properly for debugging by looking at CFLAGS and CXXFLAGS

$ perl configdata.pm -m

Makevars:

    AR              = ar
    ARFLAGS         = r
    CC              = gcc
    CFLAGS          = -Wall -O0 -g
    CPPDEFINES      = 
    CPPFLAGS        = 
    CPPINCLUDES     = 
    CXX             = g++
    CXXFLAGS        = -Wall -O0 -g
    HASHBANGPERL    = /usr/bin/env perl
    LDFLAGS         = 
    LDLIBS          = 
    PERL            = /usr/bin/perl
    RANLIB          = ranlib
    RC              = windreslib
...

Let's also look at what's enabled and disabled:

$ perl configdata.pm -o

Enabled features:

    afalgeng
    aria
    asm
    async
    autoalginit
    autoerrinit
    autoload-config
    bf
    blake2
    camellia
    capieng
    cast
    chacha
    cmac
    cms
    comp
    ct
    deprecated
    des
    dgram
    dh
    dsa
    dso
    dtls
    dynamic-engine
    ec
    ec2m
    ecdh
    ecdsa
    engine
    err
    filenames
    gost
    hw(-.+)?
    idea
    makedepend
    md4
    mdc2
    multiblock
    nextprotoneg
    ocb
    ocsp
    pic
    poly1305
    posix-io
    psk
    rc2
    rc4
    rdrand
    rfc3779
    rmd160
    scrypt
    seed
    shared
    siphash
    sm2
    sm3
    sm4
    sock
    srp
    srtp
    sse2
    ssl
    static-engine
    stdio
    tests
    threads
    tls
    ts
    ui-console
    whirlpool
    tls1
    tls1-method
    tls1_1
    tls1_1-method
    tls1_2
    tls1_2-method
    tls1_3
    dtls1
    dtls1-method
    dtls1_2
    dtls1_2-method

Disabled features:

    asan                    [default] OPENSSL_NO_ASAN
    crypto-mdebug           [default] OPENSSL_NO_CRYPTO_MDEBUG
    crypto-mdebug-backtrace [default] OPENSSL_NO_CRYPTO_MDEBUG_BACKTRACE
    devcryptoeng            [default] OPENSSL_NO_DEVCRYPTOENG
    ec_nistp_64_gcc_128     [default] OPENSSL_NO_EC_NISTP_64_GCC_128
    egd                     [default] OPENSSL_NO_EGD
    external-tests          [default] OPENSSL_NO_EXTERNAL_TESTS
    fuzz-libfuzzer          [default] OPENSSL_NO_FUZZ_LIBFUZZER
    fuzz-afl                [default] OPENSSL_NO_FUZZ_AFL
    heartbeats              [default] OPENSSL_NO_HEARTBEATS
    md2                     [default] OPENSSL_NO_MD2 (skip crypto/md2)
    msan                    [default] OPENSSL_NO_MSAN
    rc5                     [default] OPENSSL_NO_RC5 (skip crypto/rc5)
    sctp                    [default] OPENSSL_NO_SCTP
    ssl-trace               [default] OPENSSL_NO_SSL_TRACE
    ubsan                   [default] OPENSSL_NO_UBSAN
    unit-test               [default] OPENSSL_NO_UNIT_TEST
    weak-ssl-ciphers        [default] OPENSSL_NO_WEAK_SSL_CIPHERS
    zlib                    [default] 
    zlib-dynamic            [default] 
    ssl3                    [default] OPENSSL_NO_SSL3
    ssl3-method             [default] OPENSSL_NO_SSL3_METHOD
$ make

$ make install

$ export PATH=/opt/openssl/bin:$PATH
$ export LD_LIBRARY_PATH=/build/openssl/build

$ which openssl
/opt/openssl/bin/openssl

$ openssl
OpenSSL> version
OpenSSL 1.1.2-dev  xx XXX xxxx
OpenSSL> quit

Note that the two exports above are temporary. It is probably most conveinent to add these two lines to a file (e.g., openssl-env) that you would then source:

$ which openssl
/usr/bin/openssl

$ source openssl-env

$ which openssl
/opt/openssl/bin/openssl

$ ldd /opt/openssl/bin/openssl 
	linux-vdso.so.1 (0x00007ffc036cd000)
	libssl.so.1.1 => /build/openssl/build/libssl.so.1.1 (0x00007fab60647000)
	libcrypto.so.1.1 => /build/openssl/build/libcrypto.so.1.1 (0x00007fab6012c000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fab5ff0d000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fab5fb1c000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fab5f918000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fab608f6000)

Now your set up to experiment with the master branch of OpenSSL in your current shell whenever your wish on your Ubuntu 18.04 system.

Also, take note of the configuration files set up by install in our home directory:

$ tree ~/openssl
/home/<username>/openssl
├── certs
├── ct_log_list.cnf
├── ct_log_list.cnf.dist
├── misc
│   ├── CA.pl
│   ├── tsget -> tsget.pl
│   └── tsget.pl
├── openssl.cnf
├── openssl.cnf.dist
└── private

Debugging genrsa

Generating an RSA private key is a common and interesting function for the openssl application. Below we'll set up a breakpoint in an RSA crypto library function, which will be called when we invoke genrasa:

$ gdb -v
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git

$ gdb openssl
...
Reading symbols from openssl...done.
(gdb) break main
Breakpoint 1 at 0x4d375: file ../apps/openssl.c, line 121.

(gdb) run
Starting program: /opt/openssl/bin/openssl
...
Breakpoint 1, main (argc=1, argv=0x7fffffffe438) at ../apps/openssl.c:121
121	{

(gdb) break rsa_builtin_keygen
Breakpoint 2 at 0x7ffff77dd290: file ../crypto/rsa/rsa_gen.c, line 66.

(gdb) cont
Continuing.
OpenSSL> genrsa
Generating RSA private key, 2048 bit long modulus (2 primes)

Breakpoint 2, rsa_builtin_keygen (rsa=0x555555846c00, bits=2048, primes=2, e_value=0x555555846be0, cb=0x555555846bc0)
    at ../crypto/rsa/rsa_gen.c:66
66	{
(gdb) backtrace
#0  rsa_builtin_keygen (rsa=0x555555846c00, bits=2048, primes=2, e_value=0x555555846be0, cb=0x555555846bc0)
    at ../crypto/rsa/rsa_gen.c:66
#1  0x00007ffff77dd262 in RSA_generate_multi_prime_key (rsa=0x555555846c00, bits=2048, primes=2, e_value=0x555555846be0, 
    cb=0x555555846bc0) at ../crypto/rsa/rsa_gen.c:61
#2  0x000055555559c89e in genrsa_main (argc=0, argv=0x5555558453d8) at ../apps/genrsa.c:153
#3  0x00005555555a23d0 in do_cmd (prog=0x555555841780, argc=1, argv=0x5555558453d0) at ../apps/openssl.c:620
#4  0x00005555555a1764 in main (argc=1, argv=0x7fffffffe438) at ../apps/openssl.c:243
(gdb) list
61	    return rsa_builtin_keygen(rsa, bits, primes, e_value, cb);
62	}
63	
64	static int rsa_builtin_keygen(RSA *rsa, int bits, int primes, BIGNUM *e_value,
65	                              BN_GENCB *cb)
66	{
67	    BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *tmp, *prime;
68	    int ok = -1, n = 0, bitsr[RSA_MAX_PRIME_NUM], bitse = 0;
69	    int i = 0, quo = 0, rmd = 0, adj = 0, retries = 0;
70	    RSA_PRIME_INFO *pinfo = NULL;

Now at this point, you can step through the RSA key generation routine, print variables, dump memory, etc. If you need help with GDB, then consult its documentation page.

We update our site daily with new content related to our open source approach to network security and system design. If you would like to be notified about these changes, then please follow us on Twitter and join our mailing list.

Related articles on this site:

share
mailing list:

Please help us improve this article by adding your comment or question:

For enhanced features and capabilities, please sign in or authenticate using a popular third party

your email address will be kept private
previous month
next month
Su
Mo
Tu
Wd
Th
Fr
Sa
loading