#!/bin/bash

#
# certs2cadir - Find all certificates in the path specified and put them
# into a CApath type setup

# This work is licensed under the Creative Commons Attribution-Share
# Alike 3.0 Unported License. To view a copy of this license, visit
# http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to
# Creative Commons, 171 Second Street, Suite 300, San Francisco,
# California, 94105, USA.

certdir="~/.certs"
certs=0

if [ "x${1}" = "x" ]; then
	echo "Usage: ${0} <scan path>"
	exit 1
fi

# make sure the output dir exists
if [ ! -d "${certdir}" ]; then
	mkdir -p "${certdir}"
done

# find all the certs
find "${1}" -print | while read -r FILE
do
	# try PEM
	hash=`openssl x509 -in "${FILE}" -noout -subject_hash 2>&1`
	if [ 0 -eq $? ]; then
		openssl x509 -in "${FILE}" -out "${certdir}/${hash}.0" -text
		certs=$(($certs + 1))
		continue
	fi

	# try DER
	hash=`openssl x509 -in "${FILE}" -inform DER -noout -subject_hash 2>&1`
	if [ 0 -eq $? ]; then
		openssl x509 -in "${FILE}" -inform DER -out "${certdir}/${hash}.0" -text
		certs=$(($certs + 1))
		continue
	fi
done

echo "Parsed certificates"


