LoadCert.java
/*
** Module : LoadCert.java
** Abstract : loads one or more certificates into the directory
**
** Copyright (c) 2006-2017, Golden Code Development Corporation.
**
** -#- -I- --Date-- -T- --JPRM-- ----------------Description-----------------
** 001 NVS 20060427 NEW @25818 Created the initial version.
** 002 GES 20070112 CHG @31818 Matched minor directory interface change.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
**
** Additional terms under GNU Affero GPL version 3 section 7:
**
** Under Section 7 of the GNU Affero GPL version 3, the following additional
** terms apply to the works covered under the License. These additional terms
** are non-permissive additional terms allowed under Section 7 of the GNU
** Affero GPL version 3 and may not be removed by you.
**
** 0. Attribution Requirement.
**
** You must preserve all legal notices or author attributions in the covered
** work or Appropriate Legal Notices displayed by works containing the covered
** work. You may not remove from the covered work any author or developer
** credit already included within the covered work.
**
** 1. No License To Use Trademarks.
**
** This license does not grant any license or rights to use the trademarks
** Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
** of Golden Code Development Corporation. You are not authorized to use the
** name Golden Code, FWD, or the names of any author or contributor, for
** publicity purposes without written authorization.
**
** 2. No Misrepresentation of Affiliation.
**
** You may not represent yourself as Golden Code Development Corporation or FWD.
**
** You may not represent yourself for publicity purposes as associated with
** Golden Code Development Corporation, FWD, or any author or contributor to
** the covered work, without written authorization.
**
** 3. No Misrepresentation of Source or Origin.
**
** You may not represent the covered work as solely your work. All modified
** versions of the covered work must be marked in a reasonable way to make it
** clear that the modified work is not originating from Golden Code Development
** Corporation or FWD. All modified versions must contain the notices of
** attribution required in this license.
*/
package com.goldencode.p2j.util;
import java.io.*;
import java.security.*;
import java.security.cert.*;
import com.goldencode.p2j.cfg.*;
import com.goldencode.p2j.directory.*;
/**
* Adds new or replaces existing certificates in the directory.
*/
public class LoadCert
{
/**
* Creates or replaces a certificate under a specified name.
*
* @param directory
* The instance of directory to write to.
* @param oid
* The object ID for the directory object.
* @param file
* A PEM file to read the certificate from.
*/
private static boolean loadSingle(DirectoryService directory,
String oid,
String file)
{
File cerf = new File(file);
if (!cerf.exists())
{
System.err.println("Certificate file " + file + " not found");
return false;
}
byte[] encoded = null;
try
{
// create the certificate
FileInputStream fc = new FileInputStream(cerf);
BufferedInputStream bis = new BufferedInputStream(fc);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) cf.generateCertificate(bis);
bis.close();
bis = null;
fc.close();
fc = null;
cf = null;
encoded = cert.getEncoded();
}
catch (Exception e)
{
System.err.println(e);
return false;
}
// check for existing node
String nodeClass = directory.getNodeClass(oid);
if (nodeClass != null)
{
boolean res = directory.deleteNode(oid);
if (!res)
return false;
}
// create directory object
Attribute[] data = new Attribute[]
{
// certificate data: value (bytearray)
new Attribute(directory.getClassNodeAttribute("bytes", "value"),
new Object[] { encoded }),
};
if (!directory.addNode(oid, "bytes", data))
return false;
return true;
}
/**
* Command line driver.
*
* @param args
* Application command line parameters. File name is the only one
* expected.
*
* @throws ConfigurationException
* In case of errors in configuration file.
*/
public static void main(String[] args)
throws ConfigurationException
{
char[] pass = null;
String conf = null;
boolean syntax = false;
if (args.length < 2)
syntax = true;
else
if (args.length % 2 == 1)
syntax = true;
else
if (args[0].endsWith(".xml") && args.length == 2)
syntax = true;
if (syntax)
{
System.out.println("usage: java LoadCert "
+ "[<config.xml> { <password> | - } ] node file ...");
return;
}
BootstrapConfig bc = null;
int start = 0;
if (args[0].endsWith(".xml"))
{
conf = args[0];
start = 2;
if (!args[1].equals("-"))
pass = args[1].toCharArray();
}
else
conf = "standard_server.xml";
bc = new BootstrapConfig(conf, pass, null, null);
System.out.println("Instantiating DirectoryService");
DirectoryService dir = DirectoryService.createInstance(bc);
System.out.println("DirectoryService is up");
System.out.println("Binding to directory.");
if (!dir.bind())
throw new RuntimeException("bind() failed");
System.out.println("Bound to directory.");
System.out.println("Opening an editing batch...");
if (!dir.openBatch("/security/certificates"))
throw new RuntimeException("openBatch() failed");
System.out.println("Opened an editing batch for /security/certificates");
// perform certificates load
for (int i = start; i < args.length; i += 2)
{
boolean res = loadSingle(dir, args[i], args[i + 1]);
if (res)
System.out.print(" loaded ");
else
System.out.print("* failed ");
System.out.println(args[i] + " from " + args[i + 1]);
}
//----------------------------------------------------------------------
// all done
System.out.println("Closing the batch...");
if (!dir.closeBatch(true))
throw new RuntimeException("closeBatch() failed");
System.out.println("Batch closed.");
System.out.println("Unbinding from directory...");
if (!dir.unbind())
throw new RuntimeException("unbind() failed");
System.out.println("Unbound.");
System.out.println("Done.");
}
}