<?xml version="1.0" encoding="UTF-8"?>

<!-- *****************************************************   -->
<!--  Ant Build Script to Generate OpenXRI Certificates      -->
<!--  @author McCauley Reed                 		     -->
<!-- *****************************************************   -->

<project name="XRI Root Directory Management" basedir="." default="info">
	<description>Open Xri CA</description>

	<property name="certs_dist_dir" value="${basedir}/dist/certs"/>
	
	<property name="ca.cert.file" value="${certs_dist_dir}/openxri_ca_cert.pem"/>
	<property name="ca.cnf.file" value="${certs_dist_dir}/openxri_ca.cnf"/>
	<property name="certs.dir" value="${certs_dist_dir}/certs"/>
	<property name="cn.default" value="XriServer1"/>
	<property name="crl.dir" value="${certs_dist_dir}/crl"/>
	<property name="dist.dir" value="${certs_dist_dir}/dist"/>	
	<property name="index.file" value="${certs_dist_dir}/index.txt"/>
	<property name="password" value="openxri"/>	
	<property name="private.dir" value="${certs_dist_dir}/private"/>
	<property name="random.file" value="${certs_dist_dir}/.rnd"/>	
	<property name="serial.file" value="${certs_dist_dir}/serial"/>
	<property name="subject" value="/CN=${cn.name}/OU=OpenXri/O=Visa/ST=CA/C=US"/>
	
	<property environment="env"/>
	<property name="openssl_binary" value="${env.OPENSSL_BINARY}"/>

	<target name="info" description="Environment setup information">
		<echo>Make sure OPENXRI_CA_HOME is set to this CA's home directory</echo>
		<echo>Note: "ant init" creates the CA files</echo>
		<echo>Note: "ant make_pair" generates a PKI key pair using a prompted-for CN value</echo>
	</target>
	
	<target name="clean" depends="info" description="Remove CA environment">
		<delete quiet="yes" includeEmptyDirs="true">
			<fileset dir="${certs.dir}"/>
			<fileset dir="${crl.dir}"/>
			<fileset dir="${dist.dir}"/>
			<fileset dir="${private.dir}"/>
		</delete>
		<delete>
			<fileset dir="${certs_dist_dir}" includes="*.pem"/>
		</delete>
		<delete file="${index.file}"/>
		<delete>
			<fileset dir="${certs_dist_dir}" includes="*.old,*.attr"/>
		</delete>
		<delete file="${random.file}"/>
		<delete file="${serial.file}"/>
		
		<delete quiet="yes" includeEmptyDirs="true">
			<fileset dir="${certs_dist_dir}"/>
		</delete>
		
	</target>
	
	<target name="init" depends="info" description="Create CA environment">
	
		<mkdir dir="${certs_dist_dir}"/>
		<mkdir dir="${certs.dir}"/>
		<mkdir dir="${crl.dir}"/>
		<mkdir dir="${dist.dir}"/>		
		<mkdir dir="${private.dir}"/>
		<touch file="${index.file}"/>

		<echo>Create serial number file</echo>				
		<exec dir="${certs_dist_dir}" executable="cmd.exe" os="Windows 2000, Windows XP" output="${serial.file}">
			<arg line="/c echo 01"/>
		</exec>
		<exec dir="${certs_dist_dir}" executable="echo" os="Linux" output="${serial.file}">
			<arg line="01"/>
		</exec>

		<echo>Create CA private key and self-signed certificate</echo>		
		<exec dir="${certs_dist_dir}" executable="cmd.exe" os="Windows 2000, Windows XP">
			<arg line="/c ${openssl_binary} req -config ${ca.cnf.file} -x509 -newkey rsa -out ${ca.cert.file} -outform PEM -passout pass:${password}"/>
		</exec>
		<exec dir="${certs_dist_dir}" executable="openssl" os="Linux">
			<arg line="req -config ${ca.cnf.file} -x509 -newkey rsa -out ${ca.cert.file} -outform PEM -passout pass:${password}"/>
		</exec>

	</target>

	<target name="dump_ca" description="Show CA certificate">
		<exec dir="${certs_dist_dir}" executable="cmd.exe" os="Windows 2000, Windows XP">
			<arg line="/c ${openssl_binary} x509 -in ${ca.cert.file} -text -noout"/>
		</exec>
		
		<exec dir="${certs_dist_dir}" executable="openssl" os="Linux">
			<arg line="x509 -in ${ca.cert.file} -text -noout"/>
		</exec>
	</target>
	
	<target name="make_pair" description="Create PKI key pair">
		<input	message="Please enter common-name (CN)"
			addproperty="cn.name"
			defaultvalue="XriServer1"/>
		<echo>Creating certificate with this DN: /CN=${cn.name}/OU=OpenXri/O=Visa/ST=CA/C=US...</echo>

		<echo>Creating private key and CSR for ${cn.name}...</echo>
		<exec dir="${certs_dist_dir}" executable="cmd.exe" os="Windows 2000, Windows XP">
			<arg line="/c ${openssl_binary} req -newkey rsa:2048 -keyout ${dist.dir}/${cn.name}_key.pem -keyform PEM -out ${dist.dir}/${cn.name}_csr.pem -subj /CN=${cn.name}/OU=OpenXri/O=Visa/ST=CA/C=US -batch -passin pass:${password} -passout pass:${password}"/>
		</exec>
		<exec dir="${certs_dist_dir}" executable="openssl" os="Linux">
			<arg line="req -newkey rsa:2048 -keyout ${dist.dir}/${cn.name}_key.pem -keyform PEM -out ${dist.dir}/${cn.name}_csr.pem -subj /CN=${cn.name}/OU=OpenXri/O=Visa/ST=CA/C=US -batch -passin pass:${password} -passout pass:${password}"/>
		</exec>
		
		
		<echo>Creating certificate from CSR for ${cn.name}...</echo>
		<exec dir="${certs_dist_dir}" executable="cmd.exe" os="Windows 2000, Windows XP">
			<arg line="/c ${openssl_binary} ca -config ${ca.cnf.file} -in ${dist.dir}/${cn.name}_csr.pem -batch -passin pass:${password}"/>
		</exec>
		<exec dir="${certs_dist_dir}" executable="openssl" os="Linux">
			<arg line="ca -config ${ca.cnf.file} -in ${dist.dir}/${cn.name}_csr.pem -batch -passin pass:${password}"/>
		</exec>

		<echo>Creating PKCS8 formatted private key for ${cn.name}...</echo>
		<exec dir="${certs_dist_dir}" executable="cmd.exe" os="Windows 2000, Windows XP">
			<arg line="/c ${openssl_binary} pkcs8 -nocrypt -in ${dist.dir}/${cn.name}_key.pem -inform PEM -topk8 -outform DER -out ${dist.dir}/${cn.name}_key.p8 -passin pass:openxri"/>
		</exec>
		<exec dir="${certs_dist_dir}" executable="openssl" os="Linux">
			<arg line="pkcs8 -nocrypt -in ${dist.dir}/${cn.name}_key.pem -inform PEM -topk8 -outform DER -out ${dist.dir}/${cn.name}_key.p8 -passin pass:openxri"/>
		</exec>
		<echo>Key pair generation for ${cn.name} successful</echo>
	</target>
</project>