📄 encodebase64binary.java
字号:
/*
* Copyright (c) 2006, University of Kent
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 1. Neither the name of the University of Kent nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED.
*
* 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
* IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
* SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
* SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
* GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
* TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
* IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
* SERIOUS FAULTS, IN THIS SOFTWARE.
*
* 5. This license is governed, except to the extent that local laws
* necessarily apply, by the laws of England and Wales.
*/
/*
* EncodeBase64Binary.java
*
* Created on 11 April 2006, 19:21
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package issrg.web.service;
import issrg.pba.PbaException;
import issrg.pba.rbac.CustomisePERMIS;
import issrg.utils.repository.AttributeRepository;
import issrg.utils.repository.MultiRepository;
import issrg.utils.repository.VirtualRepository;
import org.apache.commons.codec.binary.Base64;
import java.io.*;
import java.util.*;
import issrg.utils.RFC2253NameParser;
import issrg.security.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import issrg.config.files.*;
/**
*
* @author ls97
*/
public class EncodeBase64Binary {
/** Creates a new instance of EncodeBase64Binary */
public EncodeBase64Binary() {
this.process("init.bat");
}
/**
*
* @author Linying Su
*/
private void process(String inputFilename){
Hashtable files = new Hashtable();
InputStream in = System.in;
try{
if (files.get(inputFilename)!=null) throw new IOException("can't process "+inputFilename+" recursively");
ValidFileName name = new ValidFileName();
in = new FileInputStream(name.getName(inputFilename));
}catch(IOException ioe){
System.err.println(ioe.getMessage());
return;
}catch (ValidFileNameException ve) {
System.err.println(ve.getMessage());
return;
}
files.put(inputFilename, inputFilename);
System.out.println("Processing instructions from "+inputFilename);
Hashtable setup = new Hashtable();
String prev=""; // this is the instruction in the previous loop
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String s=null;
int line=0;
try{
while(true){
try{
s=br.readLine();
boolean breakNow=s==null;
if (breakNow){
s="ini: clear"; // this line will cause the final processing of the data collected since the last "ini:"
}else{
line++;
System.out.print("# "+line+"\n"+s+"\n# ");
}
if (s==""){
System.out.println("empty line - ignored");
}else if (s.trim().startsWith("#")){
System.out.println("comment - ignored");
}else{
String instruction = s.substring(0, 4).intern();
boolean newInstr = instruction!="...:";
if (newInstr) prev=instruction;
else instruction=prev.intern();
String rhs = s.substring(4);
int idx=rhs.indexOf("=");
String var=rhs, val="";
if (idx>0){ // found an assignment - then split it into variable and value
var=rhs.substring(0, idx).trim().intern();
if ((idx+1)<rhs.length()) val=rhs.substring(idx+1);
}
rhs=rhs.trim().intern();
// now rhs is the right-hand-side of the instruction
// var is the variable in the assignment, val is the value of the variable.
if (instruction=="inc:"){ // include another batch file
System.out.println("include batch "+rhs);
process(rhs); // call it recursively
System.out.println("# resume processing of "+inputFilename);
}else if (instruction=="ini:"){ // setting initialisation parameters for PERMIS RBAC
if (newInstr){ // a new "ini:" instruction group encountered, or end of file
// now is the time to go through all the combinations
// ignore
if (breakNow) break; // end of file reached
}
if (rhs=="init"){
System.out.println("initialising PERMIS RBAC...");
//CustomisePERMIS.setSystemClock("issrg.web.service.PermisWebService");
try{
String rootca = (String)setup.get("rootca");
byte [] rootcaPKC = null;
Vector url = (Vector)setup.get("url");
Vector pkc = (Vector)setup.get("pkc");
Vector ac = (Vector)setup.get("ac");
String acattribute = (String)setup.get("acattribute");
String pkcattribute = (String)setup.get("pkcattribute");
if (acattribute==null) acattribute=CustomisePERMIS.getAttributeCertificateAttribute();
else CustomisePERMIS.setAttributeCertificateAttribute(acattribute);
if (pkcattribute==null) pkcattribute=CustomisePERMIS.getUserCertificateAttribute();
else CustomisePERMIS.setUserCertificateAttribute(pkcattribute);
if (rootca!=null && pkc==null) pkc=new Vector();
if (rootca!=null) pkc.add(0, rootca);
AttributeRepository [] pr = new AttributeRepository[(url==null?0:url.size())+(pkc==null?0:1)];
if (url!=null){
System.out.println("# connecting to repositories...");
for (int i=url.size(); i-->0; ){
System.out.print("# "+url.get(i)+"...");
System.out.println("ok");
}
System.out.println("# done");
}
if (ac!=null){
System.out.println("# loading ACs from files...");
VirtualRepository vr=new VirtualRepository();
for(int i=ac.size(); i-->0; ){
System.out.print("# "+ac.get(i)+"...");
ValidFileName name = new ValidFileName();
String fName = name.getName((String)ac.get(i));
File f=new File(fName);
byte [] b=new byte[(int)f.length()];
new FileInputStream(f).read(b);
System.out.print(issrg.ac.AttributeCertificate.getHolderDN(b));
Base64 base64 = new Base64();
byte [] bb = base64.encode(b);
String str = new String(bb);
System.out.println("===========");
System.out.println(str);
}
System.out.println("# done");
}
if (pkc!=null){ // now rootca is always the first in the list
System.out.println("# loading PKCs from files...");
VirtualRepository vr=new VirtualRepository();
pr[pr.length-1]=vr;
for(int i=pkc.size(); i-->0; ){ // rootca PKC will always be loaded last
System.out.print("# "+pkc.get(i)+"...");
File f=new File((String)pkc.get(i));
rootcaPKC=new byte[(int)f.length()];
new FileInputStream(f).read(rootcaPKC);
vr.populate(new iaik.x509.X509Certificate(rootcaPKC).getSubjectDN().getName(), pkcattribute, rootcaPKC);
System.out.println("ok");
}
System.out.println("# done");
// now rootcaPKC is the byte array pointing to the last PKC read - the root CA PKC
}
if (rootcaPKC!=null){
System.out.println("# setting signature verification...");
DefaultVerifier dv = new DefaultVerifier();
dv.setRootCA(rootcaPKC);
dv.setPKCRepository(new PKCRepository(new MultiRepository(pr)));
System.out.println("# done");
} else{
System.out.println("# signature verification is disabled");
}
System.out.println("# creating PERMIS RBAC object...");
System.out.println("# done");
} catch (Exception pe){
System.out.println("# failed");
pe.printStackTrace();
}
} else if (rhs=="clear"){
System.out.println("reset initialisation parameters for PERMIS RBAC");
setup = new Hashtable();
} else{
System.out.println("setting initialisation parameters for PERMIS RBAC");
//soa | oid | rootca | url | pkc | ac
if (var=="soa" || var=="oid" || var=="rootca" || var=="acattribute" || var=="pkcattribute"){
if (setup.get(var)!=null) System.out.println("# "+var+" is already set to "+setup.get(var)+" - ignored");
else setup.put(var, val);
} else if (var=="url" || var=="pkc" || var=="ac"){
Vector v=(Vector)setup.get(var);
if (v==null){
System.out.println("# set "+var+" to "+val);
v=new Vector();
setup.put(var, v);
} else{
System.out.println("# add "+val+" to collection of "+var);
}
if (var=="url") {
issrg.utils.ParsedURL pu=issrg.utils.ParsedURL.parseURL(val);
if (pu!=null) v.add(val);
} else v.add(val);
} else{
throw new Exception("unknown setting: "+var);
}
}
} else{
throw new Exception("Invalid instruction line");
}
}
}catch(PbaException pe){
System.out.println("Error occured while processing line "+line+": "+pe.getMessage());
// don't print the stack trace and don't break the loop - PbaExceptions are part of testing
}
}
}catch(Exception e){
System.out.println("Stopped @ "+line+": "+s);
e.printStackTrace();
}
files.remove(inputFilename);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -