use of com.google.showcase.v1beta1.Sequence in project OpenAM by OpenRock.
the class SecureLogHelperJSSImpl method readFromSecretStore.
/**
* Returns matched secret data from from the secret Storage.
* At a time there are only 3 things in logger's secure store file
* - initialkey, currentkey and current signature
* In the verifier secure store file there is just the initial key of the
* logger and the currentKey
* @param filename file for secret storage
* @param dataType The kind of data to be read, whether it is a
* signature or a key
* @param password password for the file
* @return secure data that is matched with dataType
* @throws Exception if it fails to read secret data from secret store
*/
byte[] readFromSecretStore(String filename, String dataType, AMPassword password) throws Exception {
// open input file for reading
FileInputStream infile = null;
infile = new FileInputStream(filename);
// Decode the P12 file
PFX.Template pfxt = new PFX.Template();
PFX pfx = (PFX) pfxt.decode(new BufferedInputStream(infile, 2048));
// Verify the MAC on the PFX. This is important to be sure
// it hasn't been tampered with.
StringBuffer reason = new StringBuffer();
MessageDigest md = MessageDigest.getInstance("SHA");
Password jssPasswd = new Password(new String(md.digest(password.getByteCopy()), "UTF-8").toCharArray());
md.reset();
if (!pfx.verifyAuthSafes(jssPasswd, reason)) {
throw new Exception("AuthSafes failed to verify because: " + reason.toString());
}
AuthenticatedSafes authSafes = pfx.getAuthSafes();
SEQUENCE safeContentsSequence = authSafes.getSequence();
byte[] cryptoData = null;
// Loop over contents of the authenticated safes
for (int i = 0; i < safeContentsSequence.size(); i++) {
// The safeContents may or may not be encrypted. We always send
// the password in. It will get used if it is needed. If the
// decryption of the safeContents fails for some reason (like
// a bad password), then this method will throw an exception
SEQUENCE safeContents = authSafes.getSafeContentsAt(jssPasswd, i);
SafeBag safeBag = null;
ASN1Value val = null;
// Go through all the bags in this SafeContents
for (int j = 0; j < safeContents.size(); j++) {
safeBag = (SafeBag) safeContents.elementAt(j);
// look for bag attributes and then choose the key
SET attribs = safeBag.getBagAttributes();
if (attribs == null) {
Debug.error("Bag has no attributes");
} else {
for (int b = 0; b < attribs.size(); b++) {
Attribute a = (Attribute) attribs.elementAt(b);
if (a.getType().equals(SafeBag.FRIENDLY_NAME)) {
// the friendly name attribute is a nickname
BMPString bs = (BMPString) ((ANY) a.getValues().elementAt(0)).decodeWith(BMPString.getTemplate());
if (dataType.equals(bs.toString())) {
// look at the contents of the bag
val = safeBag.getInterpretedBagContent();
break;
}
}
}
}
}
if (val instanceof ANY)
cryptoData = ((ANY) val).getContents();
}
// Close the file
infile.close();
return cryptoData;
}
use of com.google.showcase.v1beta1.Sequence in project OpenAM by OpenRock.
the class SecureLogHelperJSSImpl method writeToSecretStore.
/**
* Writes to the secret Storage. If the data to be written is a key, then
* writes the older signature also. If it is a signature then writes the
* older key also
* @param cryptoMaterial The data to be written to the secret storage
* @param filename The file for secret storage
* @param password The password for the file
* @param dataType The kind of cryptoMaterial, whether it is a signature
* or a key
* @throws Exception if it fails to write secret data from secret store
*/
void writeToSecretStore(byte[] cryptoMaterial, String filename, AMPassword password, String dataType) throws Exception {
byte[] oldDataFromSecretStorage = null;
String oldDataType = null;
MessageDigest md = MessageDigest.getInstance("SHA");
Password jssPasswd = new Password(new String(md.digest(password.getByteCopy()), "UTF-8").toCharArray());
md.reset();
// Do this only when the logger's file is being used
if (filename.equals(logFileName) && loggerInitialized) {
// current signature in the PKCS12 file
if (dataType.equals(currentSignature)) {
oldDataFromSecretStorage = readFromSecretStore(logFileName, currentKey, password);
oldDataType = currentKey;
} else if (dataType.equals(currentKey)) {
// need to read the currentSignature
// for the same reason as above
oldDataFromSecretStorage = readFromSecretStore(logFileName, currentSignature, password);
oldDataType = currentSignature;
}
}
// Start building the new contents by adding the older content first
AuthenticatedSafes newAuthSafes = new AuthenticatedSafes();
if (oldDataFromSecretStorage != null) {
SEQUENCE oldSafeContents = AddToSecretStore(oldDataFromSecretStorage, oldDataType);
// Add the old contents to the existing safe
newAuthSafes.addEncryptedSafeContents(PBEAlgorithm.PBE_SHA1_DES3_CBC, jssPasswd, null, AuthenticatedSafes.DEFAULT_ITERATIONS, oldSafeContents);
}
// not being added for the first time
if ((filename.equals(logFileName)) && !dataType.equals(initialKey) && loggerInitialized) {
byte[] key = readFromSecretStore(filename, initialKey, password);
if (key != null) {
SEQUENCE initialKeySafeContents = AddToSecretStore(key, initialKey);
newAuthSafes.addEncryptedSafeContents(PBEAlgorithm.PBE_SHA1_DES3_CBC, jssPasswd, null, AuthenticatedSafes.DEFAULT_ITERATIONS, initialKeySafeContents);
}
}
if ((filename.equals(verifierFileName)) && !dataType.equals(initialKey) && verifierInitialized) {
byte[] key = readFromSecretStore(filename, initialKey, password);
if (key != null) {
SEQUENCE initialKeySafeContents = AddToSecretStore(key, initialKey);
newAuthSafes.addEncryptedSafeContents(PBEAlgorithm.PBE_SHA1_DES3_CBC, jssPasswd, null, AuthenticatedSafes.DEFAULT_ITERATIONS, initialKeySafeContents);
}
}
// Add the new contents
SEQUENCE encSafeContents = AddToSecretStore(cryptoMaterial, dataType);
// Add the new contents to the existing safe
newAuthSafes.addEncryptedSafeContents(PBEAlgorithm.PBE_SHA1_DES3_CBC, jssPasswd, null, AuthenticatedSafes.DEFAULT_ITERATIONS, encSafeContents);
PFX newpfx = new PFX(newAuthSafes);
newpfx.computeMacData(jssPasswd, null, 5);
// write the new PFX out to the logger
FileOutputStream fos = new FileOutputStream(filename);
newpfx.encode(fos);
fos.close();
}
use of com.google.showcase.v1beta1.Sequence in project jasn1 by openmuc.
the class ModulesTest method encodingDecoding.
@Test
public void encodingDecoding() throws IOException {
ReverseByteArrayOutputStream berOS = new ReverseByteArrayOutputStream(1000);
MyDate1 dateOfHire = new MyDate1();
// MyDate1 dateOfHire = new MyDate1("19710917");
dateOfHire.value = new String("19710917").getBytes();
dateOfHire.encode(berOS, true);
MyInt2 myInt2Encode = new MyInt2(2);
berOS.reset();
myInt2Encode.encode(berOS, true);
MyInt2 myInt2Decode = new MyInt2();
byte[] code = HexConverter.fromShortHexString("a303020102");
InputStream is = new ByteArrayInputStream(code);
myInt2Decode.decode(is, true);
Assert.assertEquals(myInt2Decode.value.intValue(), 2);
PersonnelRecord pr = new PersonnelRecord();
Name name = new Name();
name.setGivenName(new BerVisibleString("givenName".getBytes()));
name.setFamilyName(new BerVisibleString("familyName".getBytes()));
name.setInitial(new BerVisibleString("initial".getBytes()));
pr.setName(name);
pr.setTitle(new BerVisibleString("title".getBytes()));
pr.setNumber(new EmployeeNumberZ(1));
pr.setDateOfHire(new Date("23121981".getBytes()));
pr.setNameOfSpouse(name);
ChildInformation child = new ChildInformation();
child.setName(new Name("child name".getBytes()));
child.setDateOfBirth(new Date("12121912".getBytes()));
PersonnelRecord.Children children = new PersonnelRecord.Children();
List<ChildInformation> childInformation = children.getChildInformation();
childInformation.add(child);
childInformation.add(child);
pr.setTestBitString(new MyBitString(new byte[] { (byte) 0x80, (byte) 0xff }, 10));
pr.setTest(new MyInt(3));
TestChoice testChoice = new TestChoice();
testChoice.setChoiceElement1(child);
pr.setTest2(testChoice);
pr.setTest3(testChoice);
pr.setTest4(testChoice);
pr.setTest5(testChoice);
pr.setTest6(testChoice);
TestSequenceOf testSequenceOf = new TestSequenceOf();
List<BerInteger> berIntegers = testSequenceOf.getBerInteger();
for (int i = 0; i < 10; i++) {
berIntegers.add(new BerInteger(i));
}
pr.setTestSequenceOf(testSequenceOf);
TestSequenceOf2 testSequenceOf2 = new TestSequenceOf2();
List<SEQUENCE> sequences = testSequenceOf2.getSEQUENCE();
for (int i = 0; i < 10; i++) {
SEQUENCE sequence = new SEQUENCE();
sequence.setTest1(new BerInteger(i++));
sequence.setTest2(new BerInteger(i));
sequences.add(sequence);
}
pr.setTestSequenceOf2(testSequenceOf2);
BerEmbeddedPdv berEmbeddedPdv = new BerEmbeddedPdv();
pr.setEmbeddedPdv(berEmbeddedPdv);
System.out.println("PersonnelRecord.toString():\n" + pr);
}
use of com.google.showcase.v1beta1.Sequence in project libSBOLj by SynBioDex.
the class CutExample method main.
public static void main(String[] args) throws Exception {
String prURI = "http://partsregistry.org/";
SBOLDocument document = new SBOLDocument();
document.setDefaultURIprefix(prURI);
document.setTypesInURIs(true);
ComponentDefinition promoter = document.createComponentDefinition("BBa_J23119", "", new HashSet<URI>(Arrays.asList(ComponentDefinition.DNA_REGION)));
promoter.addRole(SequenceOntology.PROMOTER);
promoter.addRole(URI.create("http://identifiers.org/so/SO:0000613"));
promoter.setName("J23119 promoter");
promoter.setDescription("Constitutive promoter");
promoter.addWasDerivedFrom(URI.create("http://partsregistry.org/Part:BBa_J23119"));
document.setDefaultURIprefix(prURI);
Sequence seq = document.createSequence("BBa_J23119", "", "ttgacagctagctcagtcctaggtataatgctagc", URI.create("http://www.chem.qmul.ac.uk/iubmb/misc/naseq.html"));
seq.addWasDerivedFrom(URI.create("http://parts.igem.org/Part:BBa_J23119:Design"));
promoter.addSequence(seq.getIdentity());
promoter.createSequenceAnnotation("cutat10", "cut1", 10, OrientationType.INLINE);
promoter.createSequenceAnnotation("cutat12", "cut2", 12, OrientationType.INLINE);
SBOLWriter.write(document, (System.out));
}
use of com.google.showcase.v1beta1.Sequence in project libSBOLj by SynBioDex.
the class ModuleDefinitionOutput method getSequenceLength.
private static int getSequenceLength(SBOLDocument document, ComponentDefinition componentDef) throws Exception {
if (componentDef.getSequences() != null && componentDef.getSequences().size() > 0) {
Sequence sequence = componentDef.getSequences().iterator().next();
return sequence.getElements().length();
} else {
int total = 0;
for (SequenceAnnotation annotation : componentDef.getSequenceAnnotations()) {
if (annotation.getComponent() != null) {
Component component = annotation.getComponent();
ComponentDefinition subComponentDef = component.getDefinition();
total = total + getSequenceLength(document, subComponentDef);
} else {
throw new Exception("Can't get sequence length for an incomplete design");
}
}
return total;
}
}
Aggregations