try {
int numOfPassports=passports.size();
// create an empty sighead and sigtail segment for each required signature
for (int idx=0;idx<numOfPassports;idx++) {
SEG sighead=SEGFactory.getInstance().createSEG("SigHeadUser","SigHead",msgName,numOfPassports-1-idx,gen.getSyntax());
SEG sigtail=SEGFactory.getInstance().createSEG("SigTailUser","SigTail",msgName,idx,gen.getSyntax());
List<MultipleSyntaxElements> msgelements=msg.getChildContainers();
List<SyntaxElement> sigheads=((MultipleSEGs)(msgelements.get(1))).getElements();
List<SyntaxElement> sigtails=((MultipleSEGs)(msgelements.get(msgelements.size()-2))).getElements();
// insert sighead segment in msg
if ((numOfPassports-1-idx)<sigheads.size()) {
SEGFactory.getInstance().unuseObject(sigheads.get(numOfPassports-1-idx));
} else {
for (int i=sigheads.size()-1;i<numOfPassports-1-idx;i++) {
sigheads.add(null);
}
}
sigheads.set(numOfPassports-1-idx,sighead);
// insert sigtail segment in message
if (idx<sigtails.size()) {
SEGFactory.getInstance().unuseObject(sigtails.get(idx));
} else {
for (int i=sigtails.size()-1;i<idx;i++) {
sigtails.add(null);
}
}
sigtails.set(idx,sigtail);
}
// fill all sighead and sigtail segments
for (int idx=0;idx<numOfPassports;idx++) {
HBCIPassportInternal passport=passports.getPassport(idx);
String role=passports.getRole(idx);
setParam("secfunc",passport.getSigFunction());
setParam("cid",passport.getCID());
setParam("role",role);
setParam("range","1");
setParam("keyblz",passport.getBLZ());
setParam("keycountry",passport.getCountry());
setParam("keyuserid",passport.getMySigKeyName());
setParam("keynum",passport.getMySigKeyNum());
setParam("keyversion",passport.getMySigKeyVersion());
setParam("sysid",passport.getSysId());
setParam("sigid",passport.getSigId().toString());
setParam("sigalg",passport.getSigAlg());
setParam("sigmode",passport.getSigMode());
setParam("hashalg",passport.getHashAlg());
passport.incSigId();
passport.saveChanges();
List<MultipleSyntaxElements> msgelements=msg.getChildContainers();
List<SyntaxElement> sigheads=((MultipleSEGs)(msgelements.get(1))).getElements();
List<SyntaxElement> sigtails=((MultipleSEGs)(msgelements.get(msgelements.size()-2))).getElements();
SEG sighead=(SEG)sigheads.get(numOfPassports-1-idx);
SEG sigtail=(SEG)sigtails.get(idx);
fillSigHead(sighead);
fillSigTail(sighead,sigtail);
}
msg.enumerateSegs(0,SyntaxElement.ALLOW_OVERWRITE);
msg.validate();
msg.enumerateSegs(1,SyntaxElement.ALLOW_OVERWRITE);
// calculate signatures for each segment
for (int idx=0;idx<numOfPassports;idx++) {
HBCIPassportInternal passport=passports.getPassport(idx);
List<MultipleSyntaxElements> msgelements=msg.getChildContainers();
List<SyntaxElement> sigtails=((MultipleSEGs)(msgelements.get(msgelements.size()-2))).getElements();
SEG sigtail=(SEG)sigtails.get(idx);
/* first calculate hash-result, then sign the hashresult. In
* most cases, the hash() step will be executed by the signature
* algorithm, so the hash() call returns the message as-is.
* Currently the only exception is PKCS#1-10, where an extra
* round of hashing must be executed before applying the
* signature process */
String hashdata=collectHashData(idx);
byte[] hashresult=passport.hash(hashdata.getBytes(Comm.ENCODING));
byte[] signature=passport.sign(hashresult);
if (passport.needUserSig()) {
String pintan=new String(signature,Comm.ENCODING);
int pos=pintan.indexOf("|");
if (pos!=-1) {
// wenn �berhaupt eine signatur existiert
// (wird f�r server ben�tigt)
String pin=pintan.substring(0,pos);
msg.propagateValue(sigtail.getPath()+".UserSig.pin",pin,
SyntaxElement.DONT_TRY_TO_CREATE,
SyntaxElement.DONT_ALLOW_OVERWRITE);
if (pos<pintan.length()-1) {
String tan=pintan.substring(pos+1);
msg.propagateValue(sigtail.getPath()+".UserSig.tan",tan,
SyntaxElement.DONT_TRY_TO_CREATE,
SyntaxElement.DONT_ALLOW_OVERWRITE);
}
}
} else { // normale signatur
msg.propagateValue(sigtail.getPath()+".sig","B"+new String(signature,Comm.ENCODING),
SyntaxElement.DONT_TRY_TO_CREATE,
SyntaxElement.DONT_ALLOW_OVERWRITE);
}
msg.validate();