December 8, 2017

Install and Configure Postfix

# yum install postfix mutt

# service postfix start

# adduser student

# su - student

$ mutt

1. Press m to create a new message.
2. In To write student@server1.example.com
3. In Subject write something
4. In Body write something. The default editor is vi, so:
    4.1 enter i for insert
    4.2 now write
    4.3 when finished writing, press ESC
    4.4 to save, press :wq
5. Now send, press y.

Print mail queue

# postqueue -p
Mail queue is empty

Flush mail queue
# postqueue -f

less /var/log/maillog

# netstat -tulpn | grep 25
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      17389/master       

# grep inet_interfaces /etc/postfix/main.cf 
# The inet_interfaces parameter specifies the network interface
#inet_interfaces = all
#inet_interfaces = $myhostname
#inet_interfaces = $myhostname, localhost
inet_interfaces = localhost
# the address list specified with the inet_interfaces parameter.
# receives mail on (see the inet_interfaces parameter).
# to $mydestination, $inet_interfaces or $proxy_interfaces.
# - destinations that match $inet_interfaces or $proxy_interfaces,
# unknown@[$inet_interfaces] or unknown@[$proxy_interfaces] is returned
    

# vi /etc/postfix/main.cf 
...
inet_interfaces = all
...

# service postfix restart

# netstat -tulpn | grep 25
tcp        0      0 0.0.0.0:25                  0.0.0.0:*                   LISTEN      17558/master   

--------------------
Step 1: Install Packages
--------------------
# yum install sendmail sendmail-cf dovecot m4

--------------------
Step 2: Configure sendmail to receive external mails
--------------------

Edit /etc/mail/sendmail.mc

2.1 Comment out sendmail to listen to all network adresses. To comment out in sendmail, put 
'dnl' at the beginning of the line.

dnl DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA')dnl

2.2 We will use our local hostname as mail domain, so change 'localhost.localdomain' to your 
hostname, mine is server1.example.com.

LOCAL_DOMAIN(`localhost.localdomain')dnl

--------------------
Step 3. Recompile Sendmail using m4
--------------------

# m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf

--------------------
Step 4: Configure Dovecot to fetch emails
--------------------

4.1 Edit /etc/dovecot/dovecot.conf

#Protocols we want to be serving.
protocols = pop3

# A comma separated list of IPs or hosts where to listen in for connections.
listen = *, ::

4.2 Edit /etc/dovecot/conf.d/10-auth.conf

disable_plaintext_auth = no

#!include auth-system.conf.ext
!include auth-passwdfile.conf.ext

4.3 Add User

# echo "$USER:{PLAIN}password:$UID:$GROUPS::$HOME" > /etc/dovecot/users

Example:
magkar:{PLAIN}password:500:500::/home/magkar

Here I use an existing account on mail server, if you need to create a new user, use command 
useradd to create a new user and passwd to set password:

# useradd student1
# passwd student1

4.4 Last step. Verify installation by running 'dovecot -n'
# dovecot -n
# 2.0.9: /etc/dovecot/dovecot.conf
# OS: Linux 2.6.32-358.14.1.el6.x86_64 x86_64 Red Hat Enterprise Linux Server release 6.4 (Santiago) 
disable_plaintext_auth = no
mbox_write_locks = fcntl
passdb {
  args = scheme=CRYPT username_format=%u /etc/dovecot/users
  driver = passwd-file
}
protocols = pop3
ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
ssl_key = </etc/pki/dovecot/private/dovecot.pem
userdb {
  args = username_format=%u /etc/dovecot/users
  driver = passwd-file
}

--------------------
Step 5: Restart sendmail and dovecot service
--------------------

# service dovecot restart
# service sendmail restart

If this is a fresh installation, either of the services are started, so stopping them will fail. 
Verify this by restarting the services again.

--------------------
Step 6: Testing the installation
--------------------

Thunderbird

email: magkar@server1.example.com

POP3 
Host: server1.example.com
Port: 110
No SSL
username: magkar
password: password
Send password cleartext

SMTP
Host: server1.example.com
Port: 25
NO AUTHENTICATION

Add static dns to /etc/hosts
server1.example.com    192.168.1.10 


--------------------
Reference
--------------------

http://wiki2.dovecot.org/BasicConfiguration
http://wiki2.dovecot.org/FindMailLocation
http://www.telnetport25.com/2012/02/configuring-e-mail-notifications-in-nagios-core/

SELinux

------------
What is SELinux Boolean?
------------
"Booleans allow parts of SELinux policy to be changed at runtime, without any knowledge of 
SELinux policy writing. This allows changes, such as allowing services access to NFS volumes, 
without reloading or recompiling SELinux policy." 
[https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/
Security-Enhanced_Linux/sect-Security-Enhanced_Linux-Working_with_SELinux-Booleans.html]

------------
Install semanage
------------

# yum install policycoreutils-python

------------
Working with SELinux boolean
------------

Previously you could get all SELinux Boolean with

# getsebool -a

But with RHEL 6, there is a better way, that also returns a comment for each boolean

# semanage boolean -l

To permently change a SELinux boolean

# setsebool -P httpd_can_network_connect on

------------
Reference 
------------
http://wiki.centos.org/TipsAndTricks/SelinuxBooleans

SELinux te policy file
http://oss.tresys.com/repos/refpolicy/archive/strict/domains/program/unused/nrpe.te

============
SELinux Process
============

# ps auxZ | grep nrpe
unconfined_u:system_r:nrpe_t:s0 nrpe 1234 0.0 0.0 41320 1340 ? Ss Jan13 0:14 /usr/sbin/nrpe -c /etc/nagios/nrpe.cfg -d

============
SELinux Files
============
# chcon -v --type=httpd_sys_content_t /html/index.html
context of /html/index.html changed to user_u:object_r:httpd_sys_content_t

Test

Make persistent

# semanage fcontext -a -t httpd_sys_content_t "/html(/.*)?" 


# touch /.autorelabel
# reboot 

============
SELinux Ports
============
5.4. Allowing Access to a Port

We may want a service such as Apache to be allowed to bind and listen for incoming 
connections on a non-standard port. By default, the SELinux policy will only allow 
services access to recognized ports associated with those services. If we wanted to 
allow Apache to listen on tcp port 81, we can add a rule to allow that using the 'semanage' command:

# semanage port -a -t http_port_t -p tcp 81 

A full list of ports that services are permitted access by SELinux can be obtained with:

# semanage port -l 

op5

------------------
Download OP5
------------------

op5-monitor-6.2.0.1-20131024.tar.gz

[http://www.op5.com/download-op5-monitor/]

------------------
Query RPM Package
------------------

Det finns två rpm i denna tar boll

# ll *nrpe*
-rw-rw-r--. 1 500 500 23068 Oct 24 10:17 nrpe-2.13.3-op5.1.x86_64.rpm
-rw-rw-r--. 1 500 500 11992 Oct 24 10:17 nrpe-client-2.13.3-op5.1.x86_64.rpm

Filer som RPM innehåller

# rpm -qpl nrpe-2.13.3-op5.1.x86_64.rpm
warning: nrpe-2.13.3-op5.1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID f6e3ade7: NOKEY
/etc/init.d/nrpe
/etc/nrpe.conf
/etc/nrpe.d
/etc/nrpe.d/op5_commands.cfg
/usr/sbin/nrpe

RPM Dependency/Requires [-R,--requires]

# rpm -qpR nrpe-2.13.3-op5.1.x86_64.rpm 
warning: nrpe-2.13.3-op5.1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID f6e3ade7: NOKEY
/bin/sh  
/bin/sh  
config(nrpe) = 2.13.3-op5.1
libc.so.6()(64bit)  
libc.so.6(GLIBC_2.2.5)(64bit)  
libc.so.6(GLIBC_2.3)(64bit)  
libc.so.6(GLIBC_2.3.4)(64bit)  
libc.so.6(GLIBC_2.4)(64bit)  
libcrypto.so.10()(64bit)  
libnsl.so.1()(64bit)  
libssl.so.10()(64bit)  
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(FileDigests) <= 4.6.0-1
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
rpmlib(VersionedDependencies) <= 3.0.3-1
rtld(GNU_HASH)  
rpmlib(PayloadIsXz) <= 5.2-1

RPM installation scripts

# rpm -qp --scripts nrpe-2.13.3-op5.1.x86_64.rpm
warning: nrpe-2.13.3-op5.1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID f6e3ade7: NOKEY
postinstall scriptlet (using /bin/sh):
/sbin/chkconfig --add nrpe || :
/sbin/service nrpe stop || :
/sbin/service nrpe start || :

# Move command definitions to 'include_dir' if upgrading
if [ $1 -eq 2 ]; then
   grep -q '^command\[' /etc/nrpe.conf || :
   if [ $? -eq 0 ]; then
          echo "" >> /etc/nrpe.d/op5_commands.cfg
          echo "# Imported from /etc/nrpe.cfg" >> /etc/nrpe.d/op5_commands.cfg
          grep '^command\[' /etc/nrpe.conf >> /etc/nrpe.d/op5_commands.cfg || :
          sed '/^[\#]\?[ tab]\?command\[\[*/d' -i /etc/nrpe.conf || :
          echo "" >> /etc/nrpe.conf
          echo "# NOTE!" >> /etc/nrpe.conf
          echo "# Command definitions have meed moved to 'include_dir'." >> /etc/nrpe.conf
          echo "# Any commands defined in this file will be moved by future upgrades." >> /etc/nrpe.conf
          echo "" >> /etc/nrpe.conf
   fi

   grep -q '^include_dir' /etc/nrpe.conf || :
   if [ $? -ne 0 ]; then
          echo "# In order to make remote config with conf_nrpe work, you need to" >> /etc/nrpe.conf
          echo "# create the following directory. It needs to be read/writeable by" >> /etc/nrpe.conf
          echo "# nrpe_user specified above. " >> /etc/nrpe.conf
          echo "# All command definitions should be placed in the 'include_dir'" >> /etc/nrpe.conf
          echo "# NOTE: files in 'include_dir' must have a '.cfg' suffix." >> /etc/nrpe.conf
          echo "include_dir=/etc/nrpe.d" >> /etc/nrpe.conf
   fi
fi


------------------
NRPE RPM Installation
------------------
# rpm -ipvh nrpe-2.13.3-op5.1.x86_64.rpm
warning: nrpe-2.13.3-op5.1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID f6e3ade7: NOKEY
Preparing...                ########################################### [100%]
   1:nrpe                   ########################################### [100%]
nrpe doesn't seem to be running.
Starting nrpe in daemon mode ... done

Check process 

# ps auxZ | grep nrpe
unconfined_u:system_r:nrpe_t:s0 nobody 1271 0.0 0.0 39364 1364 ? Ss 13:27 0:00 /usr/sbin/nrpe -c /etc/nrpe.conf -d
unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023 root 1274 0.0 0.0 103244 832 pts/0 S+ 13:27 0:00 grep nrpe

------------------
NRPE RPM Configuration
------------------

# vi /etc/nrpe.conf
...
allowed_hosts=127.0.0.1,192.168.122.93
...

Restart NRPE to let configuration changes take effect

# service nrpe restart

------------------
Test NRPE Installation
------------------

From server 

# /usr/lib64/nagios/plugins/check_nrpe -H 192.168.122.12
NRPE v2.13

------------------
Plugin RPM Installation
------------------

# cat /etc/nrpe.d/op5_commands.cfg 
################################################################################
#
# op5-nrpe command configuration file
#

# COMMAND DEFINITIONS
# Syntax:
# command[]=
#
command[users]=/opt/plugins/check_users -w 5 -c 10
...

# ll *plugins*
-rw-rw-r--. 1 500 500 417248 Oct 24 10:17 plugins-community-2.8.5-op5.1.x86_64.rpm
-rw-rw-r--. 1 500 500  47920 Oct 24 10:17 plugins-metadata-2.8.7-op5.1.x86_64.rpm
-rw-rw-r--. 1 500 500 594020 Oct 24 10:17 plugins-nagios-2.6.5.1-op5.1.x86_64.rpm
-rw-rw-r--. 1 500 500  94088 Oct 24 10:17 plugins-nagios-local-2.6.5.1-op5.1.x86_64.rpm
-rw-rw-r--. 1 500 500   3160 Oct 24 10:17 plugins-op5-3.0.0-op5.1.el6.x86_64.rpm

# rpm -qpl plugins-community-2.8.5-op5.1.x86_64.rpm | grep check_users
warning: plugins-community-2.8.5-op5.1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID f6e3ade7: NOKEY

# rpm -qpl plugins-nagios-2.6.5.1-op5.1.x86_64.rpm | grep check_users
warning: plugins-nagios-2.6.5.1-op5.1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID f6e3ade7: NOKEY

# rpm -qpl plugins-nagios-local-2.6.5.1-op5.1.x86_64.rpm | grep check_users
warning: plugins-nagios-local-2.6.5.1-op5.1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID f6e3ade7: NOKEY
/opt/plugins/check_users

# rpm -qpl plugins-op5-3.0.0-op5.1.el6.x86_64.rpm | grep check_users
warning: plugins-op5-3.0.0-op5.1.el6.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID f6e3ade7: NOKEY





# rpm -ipvh plugins-nagios-local-2.6.5.1-op5.1.x86_64.rpm
warning: plugins-nagios-local-2.6.5.1-op5.1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID f6e3ade7: NOKEY
error: Failed dependencies:
 perl(Exporter) is needed by plugins-nagios-local-2.6.5.1-op5.1.x86_64


# rpm -qpR plugins-nagios-local-2.6.5.1-op5.1.x86_64.rpm
warning: plugins-nagios-local-2.6.5.1-op5.1.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID f6e3ade7: NOKEY
/bin/sh  
libc.so.6()(64bit)  
libc.so.6(GLIBC_2.2.5)(64bit)  
libc.so.6(GLIBC_2.3)(64bit)  
libc.so.6(GLIBC_2.3.4)(64bit)  
libc.so.6(GLIBC_2.4)(64bit)  
libc.so.6(GLIBC_2.8)(64bit)  
libdl.so.2()(64bit)  
libm.so.6()(64bit)  
libm.so.6(GLIBC_2.2.5)(64bit)  
libpthread.so.0()(64bit)  
libpthread.so.0(GLIBC_2.2.5)(64bit)  
perl(Exporter)  
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(FileDigests) <= 4.6.0-1
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
rtld(GNU_HASH)  
rpmlib(PayloadIsXz) <= 5.2-1

Måste installera perl

# rpm -q --provides perl | grep Exporter
perl(Exporter) = 5.63
perl(Exporter::Heavy)  


-----------------------
Troubleshooting
-----------------------

# less /var/log/messages
...
nrpe[2703]: Error: Could not complete SSL handshake. 1
...

Från server enbart testa NRPE kommunikationen, genom att anropa NRPE utan kommando

# /usr/lib64/nagios/plugins/check_nrpe -H 192.168.122.12
NRPE v2.13

------------------
Plugin SELinux Problems
------------------

plugins does not seem to work with SELinux

On client set SELinux to Permissive

# setenforce 0

Double check that audit deamon is installed and running

# service auditd status
auditd (pid  983) is running...

sealert:

yum install setroubleshoot-server


semanage:
audit2allow:

yum install policycoreutils-python

check_log

-------------------
Introduction
-------------------

In mine previous blogs I have discussed how to setup the 

- Server [http://magnus-k-karlsson.blogspot.se/2014/01/install-nagios-core-35-on-rhel-6-from.html]
- Client/Agent [http://magnus-k-karlsson.blogspot.se/2014/01/install-nagios-agent-nrpe-on-rhel-6.html]

In this blog I will show you how to install and configure the check_log plugin. 

A good documentation overview site is https://www.nagios-plugins.org/doc/man/index.html.

-------------------
check_log
-------------------

#! /bin/sh
#
# Log file pattern detector plugin for Nagios
# Written by Ethan Galstad (nagios@nagios.org)
# Last Modified: 07-31-1999
#
# Usage: ./check_log   
#
# Description:
#
# This plugin will scan a log file (specified by the  option)
# for a specific pattern (specified by the  option).  Successive
# calls to the plugin script will only report *new* pattern matches in the
# log file, since an copy of the log file from the previous run is saved
# to .
#
# Output:
#
# On the first run of the plugin, it will return an OK state with a message
# of "Log check data initialized".  On successive runs, it will return an OK
# state if *no* pattern matches have been found in the *difference* between the
# log file and the older copy of the log file.  If the plugin detects any 
# pattern matches in the log diff, it will return a CRITICAL state and print
# out a message is the following format: "(x) last_match", where "x" is the
# total number of pattern matches found in the file and "last_match" is the
# last entry in the log file which matches the pattern.
#
# Notes:
#
# If you use this plugin make sure to keep the following in mind:
#
#    1.  The "max_attempts" value for the service should be 1, as this
#        will prevent Nagios from retrying the service check (the
#        next time the check is run it will not produce the same results).
#
#    2.  The "notify_recovery" value for the service should be 0, so that
#        Nagios does not notify you of "recoveries" for the check.  Since
#        pattern matches in the log file will only be reported once and not
#        the next time, there will always be "recoveries" for the service, even
#        though recoveries really don't apply to this type of check.
#
#    3.  You *must* supply a different  for each service that
#        you define to use this plugin script - even if the different services
#        check the same  for pattern matches.  This is necessary
#        because of the way the script operates.
#
# Examples:
#
# Check for login failures in the syslog...
#
#   check_log /var/log/messages ./check_log.badlogins.old "LOGIN FAILURE"
#
# Check for port scan alerts generated by Psionic's PortSentry software...
#
#   check_log /var/log/message ./check_log.portscan.old "attackalert"
#

-------------------
Agent/Client Configuration check_log for JBoss EAP 6 Standalone
-------------------

The standard log file for JBoss EAP 6 running in standalone mode is

# ll /var/log/jbossas/standalone/server.log

First let pay attention to the third prerequisites for check_log and create a new "old" log file for check_log.

# touch /var/log/jbossas/standalone/server.log.check_log

# chmod 640 /var/log/jbossas/standalone/*

In the NRPE configuration file we see that there is a configuration directory for NRPE

# cat /etc/nagios/nrpe.cfg
...
# INCLUDE CONFIG DIRECTORY
# This directive allows you to include definitions from config files (with a
# .cfg extension) in one or more directories (with recursion).

include_dir=/etc/nrpe.d/

And there we will put our command for the check_log plugin.

# vi /etc/nrpe.d/check_jboss_log.cfg
command[check_jboss_log]=/usr/lib64/nagios/plugins/check_log -F /var/log/jbossas/standalone/server.log \
-O /var/log/jbossas/standalone/server.log.check_log -q "WARN"

Finally restart the nrpe deamon to make the new configuration take effect.

# service nrpe restart



http://mgrepl.fedorapeople.org/Blog/nagios.html

-------------------
Server Configuration
-------------------

# vi /etc/nagios/conf.d/virtual1.example.com.cfg

Block Cipher (Encryption) Cheat Sheet

Block Cipher
INPUT: a plaintext block and a key
OUTPUT: a ciphertext block of same length

  • A block cipher can be inverted (decrypted) with the key
  • Even if you know the plaintext and ciphertext it should be hard to recover the key
Bad, do not use Good, do use
DES AES
3DES Also OK, if AES is not available:
your own algorithm CAST (in PGP)
  Twofish, Blowfish

SSL/TLS Attacks

Padding oracles in CBC mode Compression

  • Vaudenay 2002
  • Boneh/Brumley 2003
  • BEAST 2011
  • Lucky13 2013
  • POODLE 2014
  • Lucky Microseconds 2015

RSA PKCS1-1.5

  • Bleichenbacher 1998
  • Jager 2015
  • DROWN 2016

MD5 & SHA1

  • CA forgery attack 2008
  • SLOTH 2016

Compression

  • CRIME 2012
  • BREACH 2013

Renegotiation

  • Marsh Ray Attack 2009
  • Renegotiation DoS 2011
  • 3Shake 2014

Export-grade ciphers

  • FREAK 2014
  • LogJam and WeakDH 2015
  • Sweet32 2016

Other

  • RC4 2013
  • Nonce reuse 2016

December 7, 2017

Crypto Key Length

keylength.com

Crypt Libraries

Library Website Written in
Libgcrypt gnupg.org/related_software/libgcrypt/ C
GnuTLS gnutls.org C
cryptlib cryptlib.com C
Botan botan.randombit.net C++
Crypto++® Library 5.6.5 cryptopp.com C++
Microsoft CryptoAPI
OpenSSL openssl.org C, assembly
Network Security Services, NSS developer.mozilla.org/en-US/docs/NSS C, assembly
NaCl nacl.cr.yp.to
Bouncy Castle bouncycastle.org Java and C#
libtomcrypt libtom.net C

Crypto Attacks Side-Channel Attacks

  • Timing Leaks - attacks based on measuring how much time various computations takes. For example algorithm known and start to guess key, if return fast the first bytes wrong if reply takes, you know that the first bytes are correct.
    • Mitigation: Constant time implementation
  • Error Oracles - return error leaks information, e.g. encoding typical error, etc. See also Bleichenbacher's attack on PKCS#1 and DROWN attack against SSLv2.
    • Mitigation: Ensure error does leak sensitive information, such as timing, interruptions, etc.
  • Software Bugs - all software contains bugs.
    • Mitigation: Patch or use more failsafe crypto
  • Optimizations - too clever solutions, can sometime be too clever
    • Mitigation: Do not try to outsmart yourself. Do not optimize if the outcome is marginal.

Comparing Elliptic-Curve Cryptography (ECC) and RSA

Reference: https://www.researchgate.net/publication/255970113_A_Survey_of_the_Elliptic_Curve_Integrated_Encryption_Scheme

Use standard curves only.

  • Curve25519
  • Curve448

Diffie–Hellman key exchange (DH)

"Diffie–Hellman key exchange (DH)[nb 1] is a method of securely exchanging cryptographic keys" [1]

"Although Diffie–Hellman key agreement itself is a non-authenticated key-agreement protocol, it provides the basis for a variety of authenticated protocols, and is used to provide forward secrecy in Transport Layer Security's ephemeral modes (referred to as EDH or DHE depending on the cipher suite)." [1]

"Ephemeral key exchanges are based on Diffie-Hellman and use per-session, temporary keys during the initial SSL/TLS handshake. They provide Perfect Forward Secrecy, PFS, which means a compromise of the server's long term signing key does not compromise the confidentiality of past session (see following rule). When the server uses an ephemeral key, the server will sign the temporary key with its long term key (the long term key is the customary key available in its certificate)." [2]

For Apache httpd do the following. First generate DH-parameters to get unique prime numbers. The size should be same as the key length in your server certificate.

# openssl dhparam 2048 -out dhparam2048.pem

Then configure Apache httpd to use them.

SSLOpenSSLConfCmd DHParameters 

Problem with DH: DH relies on that is hard to calculate discrete logarithm [3], this was used in the Logjam attack [4].

Reference:
[1] https://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange [2] https://www.owasp.org/index.php/Transport_Layer_Protection_Cheat_Sheet [3] https://en.wikipedia.org/wiki/Discrete_logarithm [4] https://en.wikipedia.org/wiki/Logjam_(computer_security)

December 6, 2017

Asymmetric Encryption with RSA and OAEP (Optimal Asymmetric Encryption Padding)

The preferred padding with RSA cipher algorithm is OAEP (Optimal Asymmetric Encryption Padding).

Java Example:

First generate public and private RSA keys with some length, here we use 2048 bits.

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(2048, new SecureRandom());
KeyPair pair = keyGen.generateKeyPair();
PrivateKey privateKey = pair.getPrivate();
PublicKey publicKey = pair.getPublic();

Then we we encrypt our plaintext

byte[] plaintext = "Secret goes here".getBytes(StandardCharsets.UTF_8);

// While using asymmetric ciphers, use ECB as the mode of operation, 
// which essentially is a hack behind-the-scenes, meaning ignore this value.
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] ciphertext = cipher.doFinal(plaintext);

Then we can print our ciphertext with Base64 encoding

String encoded = new String(Base64.getEncoder().encode(ciphertext), StandardCharsets.UTF_8);
System.out.println("Encrypted : " + encoded);

And finally decrypt it

// While using asymmetric ciphers, use ECB as the mode of operation, 
// which essentially is a hack behind-the-scenes, meaning ignore this value.
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-512AndMGF1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] plaintext = cipher.doFinal(ciphertext); 

System.out.println("Decrypted : " + new String(plaintext, StandardCharsets.UTF_8));

Asymetric RSA Trapdoor Permutation

The RSA algorithm works the following

  • Public Key: a large number n and exponent e
  • Private Key: primes p and q such that n = p * q.

The RSA trapdoor permutation: x -> xe mod n = y

So the strength in RSA is built around practical difficulty of factoring large integers.

Reference: https://en.wikipedia.org/wiki/RSA_Factoring_Challenge

See also https://www.theinquirer.net/inquirer/news/3019326/roca-rsa-encryption-key-flaw-puts-millions-of-devices-at-risk.

Symmetric Authenticated Encryption (AE) or Authenticated Encryption with Associated Data (AEAD)

You can do more in Symmetric Encryption (Cipher) and that is Symmetric Authenticated Encryption (AE) or Authenticated Encryption with Associated Data (AEAD) which simultaneously provides confidentiality, integrity, and authenticity assurances on the data. And this can be done in three ways

  • Encrypt-and-MAC (as in SSH originally)
  • MAC-then-Encrypt (as in TLSv1.0, v1.1, v1.2)
  • Encrypt-then-MAC (as in IPSec and TLSv1.3)

The recommended way is Encrypt-then-MAC, because if the package is malicious, you first want to do MAC validation and then encrypt, not the other way around. What if ciphertext contains evil code?

Belows shows a sequence diagram of the Encrypt-then-MAC, not that the key below is symmetric. Which means this signature does NOT offer non-repudiation as in with asymmetric keys.

Recommended AEAD are: AES-GSM and AES-CCM.

Reference: https://en.wikipedia.org/wiki/Authenticated_encryption#Encrypt-then-MAC_(EtM)

Block Cipher - Encrypt More Than 1 Block

If you are going to encrypt (cipher) more than one block, you need a mode of operation for that.

For example the preferred cipher AES (Advanced Encryption Standard) uses

Key sizes  128, 192 or 256 bits
Block sizes  128 bits

Electronic CodeBook, ECB

Cipher Block Chaining, CBC with random IVs

Counter, CTR with unique nonces (Number only used ONCE)

Counter with CBC-MAC, CCM

Galois/Counter Mode, GCM

Hash Functions Cheat Sheet

In previous blog I wrote about Deterministic Random Bit Generator, DRBG that makes arbitrary output from a short fixed length seed.

And the inverse of that is Hash Function

INPUT: arbitrarily long data
OUTPUT: short fixed length digest (e.g. 256 bit)

Hash functions are used for

Digest Signatures
Sign( Hash( message, salt ) )

Hash Key Derivation Function, HKDF
HKDF( randomness )

Password Based Hashing and Key Derivation Funtion, PBKDF
PBKDF( password, salt, iteration count )

Challenge Response Protocol
Hash( key, challenge )

Deterministic Random Bit Generator, DRBG
Hash( key, nonce, 1 ) || Hash( key, nonce, 2 ) || ...

Message Authentication Code, MAC
Hash( key, message )

Bad, do not use Good, do use
MD4, MD5 BLAKE2
SHA-1 (shattered.io) SHA-3
CRC SHA-2 (224, 256, 384, 512)
your own algorithm  

RNG/DRBG/PRNG Cheat Sheet

  • RNG (Random Number Generator) produces random bits
    • non-deterministic
    • uses external analog source
  • DRBG (Deterministic Random Bit Generator) produces pseudorandom bits
    • deterministic
    • from a seed (taken from RNG)
  • PRNG (Pseudo-Random Number Generator) produces pseudorandom bits
    • non-deterministic
    • from a seeds from an RNG to maintain entropy pool
    • produces output bits using a DRBG
Analog Source + seed --> RNG --> 010..10 --> PRNG (DRBG is a component of PRNG) --> 0101011100..01 

Deterministic Random Bit Generator, DRBG
INPUT: short fixed-length seed (e.g. 256 bit)
OUTPUT: arbitrarily long

Bad, do not use Good, do use
rand(3) /dev/urandom
random(3) Linux getrandom()
PHP rand() Java SecureRandom
your own algorithm Node.js crypto.randomBytes
&nbrp; WINAPI BCryptGenRandom
&nbrp; openssl rand

How to Test Randomness (Entropy)

You can use ent http://www.fourmilab.ch/random/. On Ubuntu you can install via

$ sudo apt-get install ent
$ man ent
ent(1)                                                                                                              ent(1)

NAME
       ent - pseudorandom number sequence test

SYNOPSIS
       ent [options] [file]

DESCRIPTION
       ENT Logo

       ent performs a variety of tests on the stream of bytes in file (or standard input if no file is specified) and pro‐
       duces output on standard output; for example:

       Entropy = 7.980627 bits per character.

       Optimum compression would reduce the size
       of this 51768 character file by 0 percent.

       Chi square distribution for 51768 samples is 1542.26, and randomly
       would exceed this value 0.01 percent of the times.

       Arithmetic mean value of data bytes is 125.93 (127.5 = random).
       Monte Carlo value for Pi is 3.169834647 (error 0.90 percent).
       Serial correlation coefficient is 0.004249 (totally uncorrelated = 0.0).

       The values calculated are as follows:
...

And to test it

$ dd if=/dev/urandom bs=128 count=1 > 128bits

$ ent 128bits 
Entropy = 6.576057 bits per byte.

Optimum compression would reduce the size
of this 128 byte file by 17 percent.

Chi square distribution for 128 samples is 244.00, and randomly
would exceed this value 67.89 percent of the times.

Arithmetic mean value of data bytes is 132.7656 (127.5 = random).
Monte Carlo value for Pi is 3.238095238 (error 3.07 percent).
Serial correlation coefficient is 0.124230 (totally uncorrelated = 0.0).

Random in Linux

  • Syscall available since kernel 3.17
  • Only blocks at initialization if insufficient entropy
$ man 2 getrandom
GETRANDOM(2)                                     Linux Programmer's Manual                                    GETRANDOM(2)

NAME
       getrandom - obtain a series of random bytes

SYNOPSIS
       #include <linux/random.h>

       int getrandom(void *buf, size_t buflen, unsigned int flags);
...

Source Linux kernel random.c

Cryptographically Secure Pseudo-Random Number Generator (CSPRNG) in Linux and Java

Introduction

What is random? Example is the below bits randomly generated?

0100 1101 1101 0011

The probability for that is 1/2^16.

And to measure the uncertainty we introduce the notion entropy. And in Linux you can get the current entropy by:

$ cat /proc/sys/kernel/random/entropy_avail 
942

And in Linux there are two way to get random data

/dev/urandom (non-blocking)
/dev/random  (blocking)

So to get 128 bits from /dev/urandom you can do

$ dd if=/dev/urandom bs=128 count=1 2> /dev/null | od -t x1 -An
 fa 17 5c ae d2 6a 3f 82 8b 42 41 d9 72 bb 64 a1
 86 62 bc 04 65 e6 df 45 ce cb 24 fe 97 8f 14 25
 43 a2 0f 8a 43 07 b0 e4 b8 97 7f fe 24 32 b0 26
 6f 4e 12 d3 9b bc b4 40 e8 74 9e 03 6d cc 2f 02
 6f 54 dc 98 6a c8 fa 12 6c ed de 0e cd 89 16 d6
 2b b7 14 07 a9 c4 75 21 8e 79 04 48 c2 a9 7e b2
 1e a5 43 41 63 5a 82 01 ae 19 87 d2 06 d1 49 be
 5f ab 0d 0e 75 15 f1 50 b6 ba f6 40 c3 5e 0f 30

So the randomness in the Linux Kernel can be illustrated as

Entropy Sources:
 - Keyboard                                |--> Blocking entropy pool     --> /dev/random (blocking)
 - Interrupts   --> Primary Entropy Pool --|
 - Disk                                    |--> Non-blocking entropy pool --> /dev/urandom (non-blocking)
 - Mouse

Java

In Java things are more simple, always use java.security.SecureRandom.

// blocking random
SecureRandom nonBlockingSecureRandom = new SecureRandom();
System.out.println("Provider : " + nonBlockingSecureRandom.getProvider());
System.out.println("Algorithm : " + nonBlockingSecureRandom.getAlgorithm());
byte[] nonBlockingRandomBytes = new byte[20];
// SecureRandom randomizer should always be self-seeded
nonBlockingSecureRandom.nextBytes(nonBlockingRandomBytes);
System.out.println("Bytes : " + Arrays.toString(nonBlockingRandomBytes)
);

// blocking random
SecureRandom blockingSecureRandom = SecureRandom.getInstanceStrong();
System.out.println("Provider : " + blockingSecureRandom.getProvider());
System.out.println("Algorithm : " + blockingSecureRandom.getAlgorithm());
byte[] blockingRandomBytes = new byte[20];
// SecureRandom randomizer should always be self-seeded
blockingSecureRandom.nextBytes(blockingRandomBytes);
System.out.println("Bytes : " + Arrays.toString(blockingRandomBytes));

And produces when run on Linux.

Provider : SUN version 1.8
Algorithm : NativePRNG
Bytes : [114, -84, -57, -60, 76, -75, 49, -122, -12, 108, -3, 126, -29, -9, 81, 117, -108, -27, -14, 13]
Provider : SUN version 1.8
Algorithm : NativePRNGBlocking
Bytes : [55, 90, -125, 46, 18, 29, 51, 54, 10, 90, -57, 68, 30, -56, 32, -99, 43, 98, -113, -46]

You can also configure the default behaviour for SecureRandom in

$JAVA_HOME/lib/security/java.security
...
#
# Sun Provider SecureRandom seed source.
#
# Select the primary source of seed data for the "SHA1PRNG" and
# "NativePRNG" SecureRandom implementations in the "Sun" provider.
# (Other SecureRandom implementations might also use this property.)
#
# On Unix-like systems (for example, Solaris/Linux/MacOS), the
# "NativePRNG" and "SHA1PRNG" implementations obtains seed data from
# special device files such as file:/dev/random.
#
# On Windows systems, specifying the URLs "file:/dev/random" or
# "file:/dev/urandom" will enable the native Microsoft CryptoAPI seeding
# mechanism for SHA1PRNG.
#
# By default, an attempt is made to use the entropy gathering device
# specified by the "securerandom.source" Security property.  If an
# exception occurs while accessing the specified URL:
#
#     SHA1PRNG:
#         the traditional system/thread activity algorithm will be used.
#
#     NativePRNG:
#         a default value of /dev/random will be used.  If neither
#         are available, the implementation will be disabled.
#         "file" is the only currently supported protocol type.
#
# The entropy gathering device can also be specified with the System
# property "java.security.egd". For example:
#
#   % java -Djava.security.egd=file:/dev/random MainClass
#
# Specifying this System property will override the
# "securerandom.source" Security property.
#
# In addition, if "file:/dev/random" or "file:/dev/urandom" is
# specified, the "NativePRNG" implementation will be more preferred than
# SHA1PRNG in the Sun provider.
#
securerandom.source=file:/dev/random

#
# A list of known strong SecureRandom implementations.
#
# To help guide applications in selecting a suitable strong
# java.security.SecureRandom implementation, Java distributions should
# indicate a list of known strong implementations using the property.
#
# This is a comma-separated list of algorithm and/or algorithm:provider
# entries.
#
securerandom.strongAlgorithms=NativePRNGBlocking:SUN
...

December 4, 2017

Ubuntu 16.04 Disk Usage Analyzer

To be able to scan your entire file system you need to start the Disk Usage Analyzer with sudo.

$ sudo baobab

December 3, 2017

Getting Started with Python on Ubuntu 16.04

Prerequisite

Ubuntu 16.04

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial

Interactive Mode

$ python
Python 2.7.12 (default, Nov 20 2017, 18:23:56) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1 + 1
2

IDE - IDLE

$ sudo apt-get install idle
$ idle
File -> New File 
File -> Save as... -> hello.py
#!/usr/bin/env python3

name = input("Name: ")
print ("Hello " + name)

Reference

The Python Tutorial - https://docs.python.org/3/tutorial/

November 30, 2017

Dogtag Certificate System 10 pkispawn Configuraiton file

Introduction

Reference from pki_default.cfg(5) that ships with pki-server version 10.

# man 5 pki_default.cfg
pki_default.cfg(5)                      PKI Default Instance Configuration                      pki_default.cfg(5)

NAME
       pki_default.cfg - Certificate Server instance default config file.

LOCATION
       /etc/pki/default.cfg

DESCRIPTION
       This  file  contains  the  default settings for a Certificate Server instance created using pkispawn.  This
       file should not be edited, as it can  be  modified  when  the  Certificate  Server  packages  are  updated.
       Instead, when setting up a Certificate Server instance, a user should provide pkispawn with a configuration
       file containing overrides to the defaults in /etc/pki/default.cfg.  See pkispawn(8) for details.

SECTIONS
       default.cfg contains parameters that are grouped into sections.  These sections are stacked, so that param‐
       eters  defined in earlier sections can be overwritten by parameters defined in later sections. The sections
       are read in the following order: [DEFAULT], [Tomcat], and  the  subsystem  section  ([CA],  [KRA],  [OCSP],
       [TKS], or [TPS]). This allows the ability to specify parameters to be shared by all subsystems in [DEFAULT]
       or [Tomcat], and subsystem-specific customization.

       There are a small number of bootstrap parameters which are passed in the configuration  file  by  pkispawn.
       Other parameter's values can be interpolated tokens rather than explicit values. For example:

       pki_ca_signing_nickname=caSigningCert cert-%(pki_instance_name)s CA

       This  substitutes  the  value of pki_instance_name into the parameter value.  It is possible to interpolate
       any non-password parameter within a section or in [DEFAULT]. Any parameter used in interpolation  can  ONLY
       be  overridden  within  the  same section.  So, for example, pki_instance_name should only be overridden in
       [DEFAULT]; otherwise, interpolations can fail.

       Note:  Any non-password related parameter values in the configuration file that needs to contain a %  char‐
              acter  must  be properly escaped.  For example, a value of foo%bar would be specified as foo%%bar in
              the configuration file.

PRE-CHECK PARAMETERS
       Once the configuration parameters have been constructed from the above  sections  and  overrides,  pkispawn
       will  perform  a series of basic tests to determine if the parameters being passed in are valid and consis‐
       tent, before starting any installation.  In pre-check mode, these tests  are  executed  and  then  pkispawn
       exits.

       It  is  possible  to  disable specific tests by setting the directives below.  While all these tests should
       pass to ensure a successful installation, it may be reasonable to skip tests in pre-check mode.

       pki_skip_ds_verify

              Skip verification of the Directory Server credentials.  In this test, pkispawn attempts to  bind  to
              the  directory server instance for the internal database using the provided credentials.  This could
              be skipped if the directory server instance does not yet exist  or  is  inaccessible.   Defaults  to
              False.

       pki_skip_sd_verify

              Skip verification of the security domain user/password.  In this test, pkispawn attempts to log onto
              the security domain using the provided credentials.  This can be skipped if the security  domain  is
              unavailable. Defaults to False.

GENERAL INSTANCE PARAMETERS
       The parameters described below, as well as the parameters located in the following sections, can be custom‐
       ized as part of a deployment.  This list is not exhaustive.

       pki_instance_name

              Name of the instance. The instance is located at /var/lib/pki/.  For Java subsystems,
              the default is specified as pki-tomcat.

       pki_https_port, pki_http_port

              Secure  and unsecure ports.  Defaults to standard Tomcat ports 8443 and 8080, respectively, for Java
              subsystems.

       pki_ajp_port, pki_tomcat_server_port

              Ports for Tomcat subsystems.  Defaults to standard Tomcat ports of 8009 and 8005, respectively.

       pki_ajp_host

              Host on which to listen for AJP requests.  Defaults to localhost to listen to local traffic only.

       pki_proxy_http_port, pki_proxy_https_port, pki_enable_proxy

              Ports for an Apache proxy server. Certificate Server instances can be run  behind  an  Apache  proxy
              server,  which will communicate with the Tomcat instance through the AJP port.  See the Red Hat Cer‐
              tificate System documentation  at  https://access.redhat.com/knowledge/docs/Red_Hat_Certificate_Sys‐
              tem/ for details.

       pki_user, pki_group, pki_audit_group

              Specifies  the  default  administrative user, group, and auditor group identities for PKI instances.
              The default user and group are both specified as pkiuser, and the default audit group  is  specified
              as pkiaudit.

       pki_token_name, pki_token_password

              The  token  and  password where this instance's system certificate and keys are stored.  Defaults to
              the NSS internal software token.

       pki_hsm_enable, pki_hsm_libfile, pki_hsm_modulename

              If an optional hardware security module (HSM) is being utilized (rather than  the  default  software
              security  module  included  in  NSS),  then  the  pki_hsm_enable parameter must be set to 'True' (by
              default this parameter is 'False'), and values must be supplied for both the pki_hsm_libfile (e.  g.
              - pki_hsm_libfile=/opt/nfast/toolkits/pkcs11/libcknfast.so) and pki_hsm_modulename parameters (e. g.
              - pki_hsm_modulename=nethsm).

   SYSTEM CERTIFICATE PARAMETERS
       pkispawn sets up a number of system certificates for each subsystem.  The  system  certificates  which  are
       required differ between subsystems.  Each system certificate is denoted by a tag, as noted below.  The dif‐
       ferent system certificates are:

              * signing certificate ("ca_signing").  Used to sign other certificates.  Required for CA.

              * OCSP signing certificate ("ocsp_signing" in CA, "signing" in OCSP).  Used to sign CRLs.   Required
              for OCSP and CA.

              * storage certificate ("storage").  Used to encrypt keys for storage in KRA.  Required for KRA only.

              *  transport certificate ("transport").  Used to encrypt keys in transport to the KRA.  Required for
              KRA only.

              * subsystem certificate ("subsystem").  Used to communicate between subsystems within  the  security
              domain.  Issued by the security domain CA.  Required for all subsystems.

              * server certificate ("sslserver").  Used for communication with the server.  One server certificate
              is required for each Certificate Server instance.

              * audit signing certificate ("audit_signing").  Used to sign audit logs.  Required for  all  subsys‐
              tems except the RA.

       Each system certificate can be customized using the parameters below:

       pki__key_type, pki__keysize, pki__key_algorithm

              Characteristics   of  the  private  key.  See  the  Red  Hat  Certificate  System  documentation  at
              https://access.redhat.com/knowledge/docs/Red_Hat_Certificate_System/  for  possible  options.    The
              defaults are RSA for the type, 2048 bits for the key size, and SHA256withRSA for the algorithm.

       pki__signing_algorithm

              For signing certificates, the algorithm used for signing.  Defaults to SHA256withRSA.

       pki__token

              Location  where  the  certificate and private key are stored.  Defaults to the internal software NSS
              token database.

       pki__nickname

              Nickname for the certificate in the token database.

       pki__subject_dn

              Subject DN for the certificate.  The  subject  DN  for  the  SSL  Server  certificate  must  include
              CN=.

   ADMIN USER PARAMETERS
       pkispawn creates a bootstrap administrative user that is a member of all the necessary groups to administer
       the installed subsystem.  On a security domain CA, the CA administrative user  is  also  a  member  of  the
       groups  required  to  register  a  new subsystem on the security domain.  The certificate and keys for this
       administrative user are stored in a PKCS #12 file in pki_client_dir, and can be imported into a browser  to
       administer the system.

       pki_admin_name, pki_admin_uid

              Name and UID of this administrative user.  Defaults to caadmin for CA, kraadmin for KRA, etc.

       pki_admin_password

              Password  for  the  admin  user.   This  password is used to log into the pki-console (unless client
              authentication is enabled), as well as log into the security domain CA.

       pki_admin_email

              Email address for the admin user.

       pki_admin_dualkey, pki_admin_keysize, pki_admin_key_type

              Settings for the administrator certificate and keys.

       pki_admin_subject_dn

              Subject   DN   for   the   administrator   certificate.    Defaults   to    cn=PKI    Administrator,
              e=%(pki_admin_email)s, o=%(pki_security_domain_name)s.

       pki_admin_nickname
              Nickname for the administrator certificate.

       pki_import_admin_cert

              Set to True to import an existing admin certificate for the admin user, rather than generating a new
              one.  A subsystem-specific administrator will still be created within  the  subsystem's  LDAP  tree.
              This  is useful to allow multiple subsystems within the same instance to be more easily administered
              from the same browser by using a single certificate.

              By default, this is set to False for CA subsystems and true for KRA, OCSP, TKS, and TPS  subsystems.
              In this case, the admin certificate is read from the file ca_admin.cert in pki_client_dir.

              Note that cloned subsystems do not create a new administrative user.  The administrative user of the
              master subsystem is used instead, and the details of this master  user  are  replicated  during  the
              install.

       pki_client_admin_cert_p12

              Location for the PKCS #12 file containing the administrative user's certificate and keys.  For a CA,
              this defaults to ca_admin_cert.p12 in the pki_client_dir directory.

   BACKUP PARAMETERS
       pki_backup_keys, pki_backup_password

              Set to True to back up the subsystem certificates and keys to a PKCS #12 file.  This  file  will  be
              located  in  /var/lib/pki//alias.  pki_backup_password is the password of the PKCS#12
              file.

       Important:
              Since HSM keys are stored in the HSM (hardware), they cannot be backed up to a PKCS #12 file  (soft‐
              ware).   Therefore,  if  pki_hsm_enable  is  set to True, pki_backup_keys should be set to False and
              pki_backup_password should be left unset (the default values in /etc/pki/default.cfg).   Failure  to
              do so will result in pkispawn reporting this error and exiting.

   CLIENT DIRECTORY PARAMETERS
       pki_client_dir

              This  is  the  location where all client data used during the installation is stored.  At the end of
              the invocation of pkispawn, the administrative user's certificate and keys are stored in a PKCS  #12
              file in this location.

              Note:  When using an HSM, it is currently recommended to NOT specify a value for pki_client_dir that
              is different from the default value.

       pki_client_database_dir, pki_client_database_password

              Location where an NSS token database is created in order to generate a key  for  the  administrative
              user.  Usually, the data in this location is removed at the end of the installation, as the keys and
              certificates are stored in a PKCS #12 file in pki_client_dir.

       pki_client_database_purge

              Set to True to remove pki_client_database_dir at the end of the installation.  Defaults to True.

   INTERNAL DATABASE PARAMETERS

       pki_ds_hostname, pki_ds_ldap_port, pki_ds_ldaps_port

              Hostname and ports for the internal database.  Defaults to localhost, 389, and 636, respectively.

       pki_ds_bind_dn, pki_ds_password

              Credentials to connect to the database  during  installation.   Directory  Manager-level  access  is
              required during installation to set up the relevant schema and database.  During the installation, a
              more restricted Certificate Server user is set up to client authentication connections to the  data‐
              base.   Some  additional configuration is required, including setting up the directory server to use
              SSL.  See the documentation for details.

       pki_ds_secure_connection

              Sets whether to require connections to the Directory Server using LDAPS.  This requires  SSL  to  be
              set up on the Directory Server first.  Defaults to false.

       pki_ds_secure_connection_ca_nickname

              Once  a  Directory  Server  CA  certificate  has  been imported into the PKI security databases (see
              pki_ds_secure_connection_ca_pem_file), pki_ds_secure_connection_ca_nickname will contain  the  nick‐
              name  under  which  it  is stored.  The default.cfg file contains a default value for this nickname.
              This parameter is only utilized when pki_ds_secure_connection has been set to true.

       pki_ds_secure_connection_ca_pem_file

              The pki_ds_secure_connection_ca_pem_file parameter will consist of the fully-qualified path  includ‐
              ing  the  filename of a file which contains an exported copy of a Directory Server's CA certificate.
              While this parameter is only utilized when pki_ds_secure_connection has been set to  true,  a  valid
              value is required for this parameter whenever this condition exists.

       pki_ds_remove_data

              Sets  whether  to  remove  any  data from the base DN before starting the installation.  Defaults to
              True.

       pki_ds_base_dn

              The base DN for the internal database.  It is advised that the Certificate Server have its own  base
              DN  for its internal database.  If the base DN does not exist, it will be created during the running
              of pkispawn.  For a cloned subsystem, the base DN for the clone subsystem MUST be the  same  as  for
              the master subsystem.

       pki_ds_database

              Name  of  the back-end database.  It is advised that the Certificate Server have its own base DN for
              its internal database.  If the back-end does not exist, it will be created  during  the  running  of
              pkispawn.

   ISSUING CA PARAMETERS

       pki_issuing_ca_hostname, pki_issuing_ca_https_port, pki_issuing_ca_uri

              Hostname  and port, or URI of the issuing CA.  Required for installations of subordinate CA and non-
              CA subsystems.  This should point to the CA that will issue the relevant system certificates for the
              subsystem.   In  a default install, this defaults to the CA subsystem within the same instance.  The
              URI has the format https://:.

   MISCELLANEOUS PARAMETERS

       pki_restart_configured_instance

              Sets whether to restart the instance after configuration is complete.  Defaults to True.

       pki_enable_access_log

              Located in the [Tomcat] section, this variable determines whether the instance will enable (True) or
              disable (False) Tomcat access logging.  Defaults to True.

       pki_enable_java_debugger

              Sets  whether  to  attach  a  Java  debugger  such  as  Eclipse to the instance for troubleshooting.
              Defaults to False.

       pki_enable_on_system_boot

              Sets whether or not PKI instances should be started upon system boot.

              Currently, if this PKI subsystem exists within a shared instance, and  it  has  been  configured  to
              start  upon  system  boot,  then  ALL  other previously configured PKI subsystems within this shared
              instance will start upon system boot.

              Similarly, if this PKI subsystem exists within a shared instance, and it has been configured to  NOT
              start  upon  system  boot,  then  ALL  other previously configured PKI subsystems within this shared
              instance will NOT start upon system boot.

              Additionally, if more than one PKI instance exists, no  granularity  exists  which  allows  one  PKI
              instance  to  be enabled while another PKI instance is disabled (i.e. - PKI instances are either all
              enabled or all disabled).  To provide this capability, the PKI instances  must  reside  on  separate
              machines.

              Defaults to True (see the following note on why this was previously 'False').

       Note:  Since  this parameter did not exist prior to Dogtag 10.2.3, the default behavior of PKI instances in
              Dogtag 10.2.2 and prior was False.  To manually enable this behavior, obtain  superuser  privileges,
              and  execute 'systemctl enable pki-tomcatd.target'; to manually disable this behavior, execute 'sys‐
              temctl disable pki-tomcatd.target'.

       pki_security_manager

              Enables the Java security manager policies provided by  the  JDK  to  be  used  with  the  instance.
              Defaults to True.

   SECURITY DOMAIN PARAMETERS
       The  security  domain  is  a  component  that  facilitates  communication between subsystems.  The first CA
       installed hosts this component and is used to register subsequent  subsystems  with  the  security  domain.
       These  subsystems can communicate with each other using their subsystem certificate, which is issued by the
       security domain CA.  For more information about the security domain component, see the Red Hat  Certificate
       System documentation at https://access.redhat.com/knowledge/docs/Red_Hat_Certificate_System/.

       pki_security_domain_hostname, pki_security_domain_https_port

              Location of the security domain.  Required for KRA, OCSP, TKS, and TPS subsystems and for CA subsys‐
              tems joining a security domain.  Defaults to the location  of  the  CA  subsystem  within  the  same
              instance.

       pki_security_domain_user, pki_security_domain_password

              Administrative  user  of  the security domain.  Required for KRA, OCSP, TKS, and TPS subsystems, and
              for CA subsystems joining a security domain.  Defaults to the administrative user for the CA subsys‐
              tem within the same instance (caadmin).

       pki_security_domain_name

              The name of the security domain. This is required for the security domain CA.

   CLONE PARAMETERS
       pki_clone

              Installs a clone, rather than original, subsystem.

       pki_clone_pkcs12_password, pki_clone_pkcs12_path

              Location and password of the PKCS #12 file containing the system certificates for the master subsys‐
              tem being cloned.  This file should be readable by the user that the Certificate Server  is  running
              as  (default  of  pkiuser),  and  have the correct selinux context (pki_tomcat_cert_t).  This can be
              achieved by placing the file in /var/lib/pki//alias.

       Important:
              Since HSM keys are stored in the HSM (hardware), they cannot be copied to a  PKCS  #12  file  (soft‐
              ware).  For the case of clones using an HSM, this means that the HSM keys must be shared between the
              master and its clones.  Therefore, if pki_hsm_enable is set to True, both pki_clone_pkcs12_path  and
              pki_clone_pkcs12_password  should be left unset (the default values in /etc/pki/default.cfg).  Fail‐
              ure to do so will result in pkispawn reporting this error and exiting.

       pki_clone_setup_replication

              Defaults to True.  If set to False, the installer does not set up replication  agreements  from  the
              master  to  the clone as part of the subsystem configuration.  In this case, it is expected that the
              top level suffix already exists, and that the data has already been replicated.  This option is use‐
              ful  if you want to use other tools to create and manage your replication topology, or if the baseDN
              is already replicated as part of a top-level suffix.

       pki_clone_reindex_data

              Defaults to False.  This parameter is only  relevant  when  pki_clone_setup_replication  is  set  to
              False.   In  this  case,  it is expected that the database has been prepared and replicated as noted
              above.  Part of that preparation could involve adding indexes and indexing the data.  If  you  would
              like  the  Dogtag  installer  to  add  the indexes and reindex the data instead, set pki_clone_rein‐
              dex_data to True.

       pki_clone_replication_master_port, pki_clone_replication_clone_port

              Ports on which replication occurs.  These are the ports on the master and  clone  databases  respec‐
              tively.  Defaults to the internal database port.

       pki_clone_replicate_schema

              Replicate  schema  when  the replication agreement is set up and the new instance (consumer) is ini‐
              tialized.  Otherwise, the schema must be installed in the clone as a separate step beforehand.  This
              does not usually have to be changed.  Defaults to True.

       pki_clone_replication_security

              The  type  of security used for the replication data.  This can be set to SSL (using LDAPS), TLS, or
              None.  Defaults to None.  For SSL and TLS, SSL must be set up for the database instances beforehand.

       pki_master_hostname, pki_master_https_port, pki_clone_uri

              Hostname and port, or URI of the subsystem being cloned.  The URI  format  is  https://:  where  the default master hostname and https port are set to be the secu‐
              rity domain's hostname and https port.

   CA SERIAL NUMBER PARAMETERS

       pki_serial_number_range_start, pki_serial_number_range_end

              Sets the range of serial numbers to be used when issuing certificates.  Values here are  hexadecimal
              (without the 0x prefix).  It is useful to override these values when migrating data from another CA,
              so that serial number conflicts do not occur.  Defaults to 1 and 10000000 respectively.

       pki_request_number_range_start, pki_request_number_range_end

              Sets the range of request numbers to be used by the CA.  Values here are decimal.  It is  useful  to
              override  these  values when migrating data from another CA, so that request number conflicts do not
              occur.  Defaults to 1 and 10000000 respectively.

       pki_replica_number_range_start, pki_replica_number_range_end

              Sets the range of replica numbers to be used by the CA.  These numbers are used to identify database
              replicas in a replication topology.  Values here are decimal.  Defaults to 1 and 100 respectively.

   EXTERNAL CA CERTIFICATE PARAMETERS

       pki_external

              Sets whether the new CA will have a signing certificate that will be issued by an external CA.  This
              is a two step process.  In the first step, a CSR to be presented to the external  CA  is  generated.
              In  the  second  step,  the  issued  signing  certificate  and certificate chain are provided to the
              pkispawn utility to complete the installation.  Defaults to False.

       pki_external_csr_path

              Required in the first step of the external CA signing process.  The  CSR  will  be  printed  to  the
              screen and stored in this location.

       pki_external_step_two

              Specifies that this is the second step of the external CA process.  Defaults to False.

       pki_external_ca_cert_path, pki_external_ca_cert_chain_path

              Required  for  the  second  step of the external CA signing process.  This is the location of the CA
              signing cert (as issued by the external CA) and the external CA's certificate chain.

   SUBORDINATE CA CERTIFICATE PARAMETERS

       pki_subordinate

              Specifies whether the new CA which will be a subordinate of another CA.  The master CA is  specified
              by pki_issuing_ca.  Defaults to False.

       pki_subordinate_create_new_security_domain

              Set to True if the subordinate CA will host its own security domain.  Defaults to False.

       pki_subordinate_security_domain_name

              Used when pki_subordinate_create_security_domain is set to True.  Specifies the name of the security
              domain to be hosted on the subordinate CA.

   STANDALONE PKI PARAMETERS
       A stand-alone PKI subsystem is defined as a non-CA PKI subsystem that does not contain a CA as  a  part  of
       its deployment, and functions as its own security domain.  Currently, only stand-alone KRAs are supported.

       pki_standalone

              Sets  whether or not the new PKI subsystem will be stand-alone.  This is a two step process.  In the
              first step, CSRs for each of this stand-alone PKI subsystem's certificates will be generated so that
              they  may be presented to the external CA.  In the second step, the issued certificates, external CA
              certificate, and external CA certificate chain are provided to the pkispawn utility to complete  the
              installation.  Defaults to False.

       pki_external_admin_csr_path

              Will  be generated by the first step of a stand-alone PKI process.  This is the location of the file
              containing the administrator's CSR (which will be  presented  to  the  external  CA).   Defaults  to
              '%(pki_instance_configuration_path)s/%(pki_subsystem_type)s_admin.csr'.

       pki_external_audit_signing_csr_path

              Will  be generated by the first step of a stand-alone PKI process.  This is the location of the file
              containing the audit signing CSR (which  will  be  presented  to  the  external  CA).   Defaults  to
              '%(pki_instance_configuration_path)s/%(pki_subsystem_type)s_audit_signing.csr'.

       pki_external_sslserver_csr_path

              Will  be generated by the first step of a stand-alone PKI process.  This is the location of the file
              containing the SSL  server  CSR  (which  will  be  presented  to  the  external  CA).   Defaults  to
              '%(pki_instance_configuration_path)s/%(pki_subsystem_type)s_sslserver.csr'.

       pki_external_storage_csr_path

              [KRA  ONLY]  Will be generated by the first step of a stand-alone KRA process.  This is the location
              of the file containing the storage CSR (which will be presented to the external  CA).   Defaults  to
              '%(pki_instance_configuration_path)s/kra_storage.csr'.

       pki_external_subsystem_csr_path

              Will  be generated by the first step of a stand-alone PKI process.  This is the location of the file
              containing  the  subsystem  CSR  (which  will  be  presented  to  the  external  CA).   Defaults  to
              '%(pki_instance_configuration_path)s/%(pki_subsystem_type)s_subsystem.csr'.

       pki_external_transport_csr_path

              [KRA  ONLY]  Will be generated by the first step of a stand-alone KRA process.  This is the location
              of the file containing the transport CSR (which will be presented to the external CA).  Defaults  to
              '%(pki_instance_configuration_path)s/kra_transport.csr'.

       pki_external_step_two

              Specifies that this is the second step of a standalone PKI process.  Defaults to False.

       pki_external_ca_cert_chain_path

              Required  for  the  second step of a stand-alone PKI process.  This is the location of the file con‐
              taining the  external  CA  signing  certificate  (as  issued  by  the  external  CA).   Defaults  to
              '%(pki_instance_configuration_path)s/external_ca.cert'.

       pki_external_ca_cert_path

              Required  for  the  second step of a stand-alone PKI process.  This is the location of the file con‐
              taining the  external  CA's  certificate  chain  (as  issued  by  the  external  CA).   Defaults  to
              '%(pki_instance_configuration_path)s/external_ca_chain.cert'.

       pki_external_admin_cert_path

              Required  for  the  second step of a stand-alone PKI process.  This is the location of the file con‐
              taining  the  administrator's  certificate  (as  issued  by   the   external   CA).    Defaults   to
              '%(pki_instance_configuration_path)s/%(pki_subsystem_type)s_admin.cert'.

       pki_external_audit_signing_cert_path

              Required  for  the  second step of a stand-alone PKI process.  This is the location of the file con‐
              taining  the  audit  signing  certificate  (as  issued   by   the   external   CA).    Defaults   to
              '%(pki_instance_configuration_path)s/%(pki_subsystem_type)s_audit_signing.cert'.

       pki_external_sslserver_cert_path

              Required  for  the  second step of a stand-alone PKI process.  This is the location of the file con‐
              taining the sslserver certificate (as issued by the external CA).  Defaults to  '%(pki_instance_con‐
              figuration_path)s/%(pki_subsystem_type)s_sslserver.cert'.

       pki_external_storage_cert_path

              [KRA  ONLY]  Required for the second step of a stand-alone KRA process.  This is the location of the
              file  containing  the  storage  certificate  (as  issued  by  the   external   CA).    Defaults   to
              '%(pki_instance_configuration_path)s/kra_storage.cert'.

       pki_external_subsystem_cert_path

              Required  for  the  second step of a stand-alone PKI process.  This is the location of the file con‐
              taining the subsystem certificate (as issued by the external CA).  Defaults to  '%(pki_instance_con‐
              figuration_path)s/%(pki_subsystem_type)s_subsystem.cert'.

       pki_external_transport_cert_path

              [KRA  ONLY]  Required for the second step of a stand-alone KRA process.  This is the location of the
              file  containing  the  transport  certificate  (as  issued  by  the  external  CA).    Defaults   to
              '%(pki_instance_configuration_path)s/kra_transport.cert'.

   TPS PARAMETERS

       pki_authdb_basedn

              Specifies the base DN of TPS authentication database.

       pki_authdb_hostname

              Specifies the hostname of TPS authentication database. Defaults to localhost.

       pki_authdb_port

              Specifies the port number of TPS authentication database. Defaults to 389.

       pki_authdb_secure_conn

              Specifies whether to use a secure connection to TPS authentication database.  Defaults to False.

       pki_enable_server_side_keygen

              Specifies  whether to enable server-side key generation. Defaults to False.  The location of the KRA
              instance should be specified in the pki_kra_uri parameter.

       pki_ca_uri

              Specifies the URI of the CA instance used by TPS to create and revoke user certificates. Defaults to
              the instance in which the TPS is running.

       pki_kra_uri

              Specifies  the  URI of the KRA instance used by TPS to archive and recover keys. Required if server-
              side key generation is enabled using the pki_enable_server_side_keygen parameter.  Defaults  to  the
              instance in which the TPS is running.

       pki_tks_uri

              Specifies  the  URI  of  the  TKS  instance used by TPS to generate symmetric keys.  Defaults to the
              instance in which the TPS is running.

AUTHORS
       Ade Lee .  pkispawn was written by the Dogtag project.

COPYRIGHT
       Copyright (c) 2012 Red Hat, Inc. This is licensed under the GNU General Public License, version 2  (GPLv2).
       A copy of this license is available at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

SEE ALSO
       pkispawn(8)

version 1.0                                      December 13, 2012                              pki_default.cfg(5)

Apache Directory GUI Tool for Managing LDAP Server

Apache Directory is good GUI tool for managing general LDAP server, but it is particularly designed for ApacheDS.

http://directory.apache.org/studio/

curl Usage

HTTP request method:
  • -X POST
  • -X PUT
HTTP request Content-Type header
  • -H "Content-Type: application/x-www-form-urlencoded"
  • -H "Content-Type: application/json"
HTTP request body/data
  • form urlencoded: -d "param1=value1&param2=value2"
  • json: -d '{"key1":"value1", "key2":"value2"}'
Examples

curl -X GET http://www.google.se

curl -d "param1=value1&param2=value2" -H "Content-Type: application/x-www-form-urlencoded" -X POST http://localhost:3000/data

curl -d '{"key1":"value1", "key2":"value2"}' -H "Content-Type: application/json" -X POST http://localhost:3000/data

https://gist.github.com/subfuzion/08c5d85437d5d4f00e58

November 29, 2017

Installing and Testing Dogtag Certificate System 10 on CentOS 7

Introduction

Dogtag Certificate System is CA and is the upstream project for Red Hat Certificate System.

Prerequisite

CentOS 7
# cat /etc/redhat-release 
CentOS Linux release 7.2.1511 (Core) 
Check that FQDN is set, otherwise set it with:
# hostnamectl set-hostname dogtag.magnuskkarlsson.se
Set firewall rules otherwise disable it.
# systemctl stop firewalld; systemctl disable firewalld
If running in a lab environment without DNS set dns in hosts file.
# ip addr show
...
    inet 192.168.122.63/24 brd 192.168.122.255 scope global dynamic eth0
...

# echo "192.168.122.63 dogtag.magnuskkarlsson.se" >> /etc/hosts

389 Directory Server - Installation of just Base DS

Dogtag Certificate System requires ldap server 389 Directory Server, DS. On CentOS there is a light version (389-ds-base) and the full blown solution (389-ds). Here we stick to the light version.
  • 389-ds-base - 389 Directory Server is an LDAPv3 compliant server. The base package includes the LDAP server and command line utilities for server administration.
  • 389-ds - The 389 Directory Server, Administration Server, and Console Suite provide the LDAPv3 server, the httpd daemon used to administer the server, and the console GUI application used for server and user/group administration.
# yum install 389-ds-base -y
The installation rpm creates a default user for 389 DS.
# grep dirsrv /etc/passwd; grep dirsrv /etc/group
dirsrv:x:389:389:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin
dirsrv:x:389:
Now configure 389 DS.
# setup-ds.pl --silent\
  General.FullMachineName='dogtag.magnuskkarlsson.se'\
  General.SuiteSpotUserID=dirsrv\
  General.SuiteSpotGroup=dirsrv\
  slapd.ServerPort=389\
  slapd.ServerIdentifier=pki-tomcat\
  slapd.Suffix=dc=magnuskkarlsson,dc=se\
  slapd.RootDN="cn=Directory Manager"\
  slapd.RootDNPwd=redhat123
Your new DS instance 'pki-tomcat' was successfully created.
Exiting . . .
Log file is '/tmp/setupKwxW67.log'
Property Comment Default
FullMachineName Specifies the fully qualified domain name of the machine on which you are installing the server. The default is the local host name.
SuiteSpotUserID Specifies the user name as which the Directory Server instance runs. This parameter does not apply to the user as which the Administration Server runs. This should be changed for most deployments. The default is user nobody on Linux and Solaris and daemon on HP-UX.
SuiteSpotGroup Specifies the group as which the servers will run. This should be changed for most deployments. The default is group nobodyon Linux and Solaris and daemon on HP-UX.
ServerPort Specifies the port the server will use for LDAP connections. For information on selecting server port numbers.
ServerIdentifier Specifies the server identifier. This value is used as part of the name of the directory in which the Directory Server instance is installed.
For example, if the machine's hostname is phonebook, then this name is the default, and selecting it installs the Directory Server instance in a directory labeled slapd-phonebook.
Suffix Specifies the suffix under which to store the directory data.
RootDN Specifies the distinguished name used by the Directory Manager.
RootDNPwd Specifies the Directory Manager's password.
And check configuration log file.
# cat /tmp/setupKwxW67.log
[17/11/27:14:20:40] - [Setup] Info Your new DS instance 'pki-tomcat' was successfully created.
[17/11/27:14:20:40] - [Setup] Success Exiting . . .
Log file is '/tmp/setupKwxW67.log'
To start and stop the 389 DS server.
# systemctl status dirsrv@pki-tomcat
And to verify the installation.
# ldapsearch -x -h dogtag.magnuskkarlsson.se -p 389 -s base -b "" "objectclass=*" 
# extended LDIF
#
# LDAPv3
# base <> with scope baseObject
# filter: objectclass=*
# requesting: ALL
#

#
dn:
objectClass: top
defaultnamingcontext: dc=magnuskkarlsson,dc=se
dataversion: 020171127105053
netscapemdsuffix: cn=ldap://dc=dogtag,dc=magnuskkarlsson,dc=se:389

# search result
search: 2
result: 0 Success

# numResponses: 2
# numEntries: 1
Reference:

Dogtag Certificate System

Now when 389 DS is up running continue with the installation of Dogtag.
# yum install pki-ca -y
Then configure it.
# pkispawn 

IMPORTANT:

    Interactive installation currently only exists for very basic deployments!

    For example, deployments intent upon using advanced features such as:

        * Cloning,
        * Elliptic Curve Cryptography (ECC),
        * External CA,
        * Hardware Security Module (HSM),
        * Subordinate CA,
        * etc.,

    must provide the necessary override parameters in a separate
    configuration file.

    Run 'man pkispawn' for details.

Subsystem (CA/KRA/OCSP/TKS/TPS) [CA]: 

Tomcat:
  Instance [pki-tomcat]: 
  HTTP port [8080]: 
  Secure HTTP port [8443]: 
  AJP port [8009]: 
  Management port [8005]: 

Administrator:
  Username [caadmin]: 
  Password: 
  Verify password: 
  Import certificate (Yes/No) [N]? 
  Export certificate to [/root/.dogtag/pki-tomcat/ca_admin.cert]: 

Directory Server:
  Hostname [dogtag.magnuskkarlsson.se]: 
  Use a secure LDAPS connection (Yes/No/Quit) [N]? 
  LDAP Port [389]: 
  Bind DN [cn=Directory Manager]: 
  Password: 
  Base DN [o=pki-tomcat-CA]: 

Security Domain:
  Name [magnuskkarlsson.se Security Domain]: 

Begin installation (Yes/No/Quit)? Yes

Log file: /var/log/pki/pki-ca-spawn.20171127145419.log
Installing CA into /var/lib/pki/pki-tomcat.
Storing deployment configuration into /etc/sysconfig/pki/tomcat/pki-tomcat/ca/deployment.cfg.
Notice: Trust flag u is set automatically if the private key is present.
Created symlink from /etc/systemd/system/multi-user.target.wants/pki-tomcatd.target to /usr/lib/systemd/system/pki-tomcatd.target.

    ==========================================================================
                                INSTALLATION SUMMARY
    ==========================================================================

      Administrator's username:             caadmin
      Administrator's PKCS #12 file:
            /root/.dogtag/pki-tomcat/ca_admin_cert.p12

      To check the status of the subsystem:
            systemctl status pki-tomcatd@pki-tomcat.service

      To restart the subsystem:
            systemctl restart pki-tomcatd@pki-tomcat.service

      The URL for the subsystem is:
            https://dogtag.magnuskkarlsson.se:8443/ca

      PKI instances will be enabled upon system boot

    ==========================================================================
Checkout the used configuration for the interactive configuration.
# cat /etc/sysconfig/pki/tomcat/pki-tomcat/ca/deployment.cfg
[DEFAULT]
pki_instance_name = pki-tomcat

[CA]
pki_http_port = 8080
pki_https_port = 8443
pki_ajp_port = 8009
pki_tomcat_server_port = 8005
pki_admin_uid = caadmin
pki_admin_password = XXXXXXXX
pki_backup_password = XXXXXXXX
pki_client_database_password = XXXXXXXX
pki_client_pkcs12_password = XXXXXXXX
pki_import_admin_cert = False
pki_client_admin_cert = /root/.dogtag/pki-tomcat/ca_admin.cert
pki_ds_hostname = dogtag.magnuskkarlsson.se
pki_ds_ldap_port = 389
pki_ds_bind_dn = cn=Directory Manager
pki_ds_password = XXXXXXXX
pki_ds_base_dn = o=pki-tomcat-CA
pki_security_domain_name = magnuskkarlsson.se Security Domain
pki_client_pin = XXXXXXXX
pki_clone_pkcs12_password = XXXXXXXX
pki_external_pkcs12_password = XXXXXXXX
pki_pkcs12_password = XXXXXXXX
pki_one_time_pin = XXXXXXXX
pki_pin = XXXXXXXX
pki_replication_password = XXXXXXXX
pki_security_domain_password = XXXXXXXX
pki_server_pkcs12_password = XXXXXXXX
pki_token_password = XXXXXXXX

Overview of the installed Dogtag instance.

# pkidaemon status pki-tomcat
Status for pki-tomcat: pki-tomcat is running ..

    [CA Status Definitions]
    Unsecure URL        = http://dogtagsofthsm.magnuskkarlsson.se:8080/ca/ee/ca
    Secure Agent URL    = https://dogtagsofthsm.magnuskkarlsson.se:8443/ca/agent/ca
    Secure EE URL       = https://dogtagsofthsm.magnuskkarlsson.se:8443/ca/ee/ca
    Secure Admin URL    = https://dogtagsofthsm.magnuskkarlsson.se:8443/ca/services
    PKI Console Command = pkiconsole https://dogtagsofthsm.magnuskkarlsson.se:8443/ca
    Tomcat Port         = 8005 (for shutdown)

    [CA Configuration Definitions]
    PKI Instance Name:   pki-tomcat

    PKI Subsystem Type:  Root CA (Security Domain)

    Registered PKI Security Domain Information:
    ==========================================================================
    Name:  magnuskkarlsson.se Security Domain
    URL:   https://dogtagsofthsm.magnuskkarlsson.se:8443
    ==========================================================================

Test Dogtag Certificate System

Now lets test it! First copy admin p12 file (/root/.dogtag/pki-tomcat/ca_admin_cert.p12) to your remote test machine and import it to your web browser.
Then we will test the CA by creating a new certificate signing request, but first we need to create a new private key.
$ openssl genrsa -out mynewkey.key.pem 2048
Then create the certificate signing request.
$ openssl req -new -key mynewkey.key.pem -out mynewkey.cert.pem
Now we are ready to sign the request. Open https://dogtag.magnuskkarlsson.se:8443/ca/ee/ca/
On the Enrollment/Renewal tab select Manual Server Certificate Enrollment and on that page copy your certificate signing request.
After submitting we need to approve that submitting. Change url to https://dogtag.magnuskkarlsson.se:8443/ca/services and click on Agent Services.

November 25, 2017

Java EE 7 Concurrency Utilities (JSR 236) and Example

Introduction

The Concurrency Utilities (JSR 236) is completely new in Java EE 7 and is also new in the Java EE mindset. In previous Java EE version the idea of creating new threads was forbidden and the motivation behind it was that thread was error prune and the standard components in Java EE should be enough. In Java EE 7 that has changed and the developer is free again to create threads.

But the above reason holds still true, that threads can help your application to scale better and increase performance, but it can also introduce:

  • Deadlocks
  • Thread starvation
  • Concurrent accessing of shared resources

Reference Oracle The Java EE 7 Tutorial Chapter 56.1 Concurrency Basics

And if you are new to Threads you should also read the Java Standard Edition Tutorial about Threads Oracle Java Tutorial Lesson: Concurrency. Which also points out the following pitfalls

  • Thread Interference describes how errors are introduced when multiple threads access shared data.
  • Memory Consistency Errors describes errors that result from inconsistent views of shared memory.
  • Synchronized Methods describes a simple idiom that can effectively prevent thread interference and memory consistency errors.
  • Implicit Locks and Synchronization describes a more general synchronization idiom, and describes how synchronization is based on implicit locks.
  • Atomic Access talks about the general idea of operations that can't be interfered with by other threads.

Reference Oracle Java Tutorial Synchronization

And if you are dead serious about threads you should read the book Java Concurrency in Practice by Brian Goetz.

Out first Thread

So now when we knew that we must pay extra attention when writing our threads, lets create a new thread. This can be done from either: java.lang.Runnable, java.lang.Thread or java.util.concurrent.Callable. The first two are there for backward compatibility and when writing new code always use java.util.concurrent.Callable.

package se.magnuskkarlsson.example.javaee7.task.control;

import java.util.concurrent.Callable;
import java.util.logging.Logger;

public class TaskCallable implements Callable<Integer> {

    private final Logger log = Logger.getLogger(TaskCallable.class.getName());

    private final String id;

    public TaskCallable(String id) {
        this.id = id;
    }
    
    @Override
    public Integer call() throws Exception {
        log.info("[" + id + "] Starting a long running task");
        for (int i = 0; i < 3; ++i) {
            log.info("[" + id + "] Analysing task...");
            // We simulate now a long running task
            Thread.sleep(3000);
        }
        log.info("[" + id + "] Finished a long running task");
        // return some dummy data
        return (int) (Math.random() * 100);
    }

}

Then we want to test. Lets test it with a simple JAX-RS class.

package se.magnuskkarlsson.example.javaee7.task.boundary;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Startup;
import javax.enterprise.concurrent.ManagedExecutorService;
import javax.inject.Singleton;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import se.magnuskkarlsson.example.javaee7.task.control.TaskCallable;

@Startup
// we need to have only one and same instance, so we can clean up ManagedExecutorService
@Singleton
// Designates that a session bean exposes a no-interface view.
// This annotation is required if a session bean exposes any other client views 
// (local, remote, no-interface, 2.x Remote Home, 2.x Local Home, **Web Service**)
@LocalBean
@Path("/task")
public class TaskREST {

    @Resource
    ManagedExecutorService managedExecutorService;
    
    @PreDestroy
    public void destroy() {
        managedExecutorService.shutdownNow();
    }
    
    @POST
    public void create(@QueryParam("id") String id) {
        managedExecutorService.submit(new TaskCallable(id));
    }
    
}

And finally test with simple CURL commands.

$ curl -X POST http://localhost:8080/example-javaee7/rest/task?id=foo
$ curl -X POST http://localhost:8080/example-javaee7/rest/task?id=bar
$ curl -X POST http://localhost:8080/example-javaee7/rest/task?id=code
$ curl -X POST http://localhost:8080/example-javaee7/rest/task?id=nisse

Now we can see in the log that task are started, ran and finished.

21:45:12,129 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-1) [foo] Starting a long running task
21:45:12,130 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-1) [foo] Analysing task...
21:45:15,130 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-1) [foo] Analysing task...
21:45:18,131 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-1) [foo] Analysing task...
21:45:21,132 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-1) [foo] Finished a long running task
21:45:33,249 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-2) [bar] Starting a long running task
21:45:33,250 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-2) [bar] Analysing task...
21:45:35,288 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-3) [code] Starting a long running task
21:45:35,289 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-3) [code] Analysing task...
21:45:36,250 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-2) [bar] Analysing task...
21:45:38,209 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-4) [nisse] Starting a long running task
21:45:38,210 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-4) [nisse] Analysing task...
21:45:38,290 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-3) [code] Analysing task...
21:45:39,251 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-2) [bar] Analysing task...
21:45:41,210 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-4) [nisse] Analysing task...
21:45:41,290 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-3) [code] Analysing task...
21:45:42,251 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-2) [bar] Finished a long running task
21:45:44,210 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-4) [nisse] Analysing task...
21:45:44,290 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-3) [code] Finished a long running task
21:45:47,211 INFO  [se.magnuskkarlsson.example.javaee7.task.control.TaskCallable] (EE-ManagedExecutorService-default-Thread-4) [nisse] Finished a long running task

Summary

The main class in Concurrency Utilities (JSR 236) are:

  • javax.enterprise.concurrent.ManagedExecutorService - which you can <T> Future<T> submit(Callable<T> task) task. java.util.concurrent.Future can also be preemptive shutdown by calling java.util.concurrent.Future#cancel(true).
  • The other that is just like above, but for the difference it can schedule threads for later, is javax.enterprise.concurrent.ManagedScheduledExecutorService.