use of com.mindbright.asn1.ASN1Object in project SpringRemote by HaleyWang.
the class SSH2KeyPairFile method writeKeyPair.
public static byte[] writeKeyPair(ASCIIArmour armour, String password, SecureRandom random, KeyPair keyPair) throws SSH2FatalException {
ASN1Object pem;
PublicKey publicKey = keyPair.getPublic();
int headType;
if (publicKey instanceof DSAPublicKey) {
DSAPublicKey pubKey = (DSAPublicKey) keyPair.getPublic();
DSAPrivateKey prvKey = (DSAPrivateKey) keyPair.getPrivate();
DSAParams params = pubKey.getParams();
pem = new PEMDSAPrivate(0, params.getP(), params.getQ(), params.getG(), pubKey.getY(), prvKey.getX());
headType = TYPE_PEM_DSA;
} else if (publicKey instanceof RSAPublicKey) {
RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateCrtKey prvKey = (RSAPrivateCrtKey) keyPair.getPrivate();
pem = new PEMRSAPrivate(0, pubKey.getModulus(), pubKey.getPublicExponent(), prvKey.getPrivateExponent(), prvKey.getPrimeP(), prvKey.getPrimeQ(), prvKey.getCrtCoefficient());
headType = TYPE_PEM_RSA;
} else if (publicKey instanceof ECPublicKey) {
ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey prvKey = (ECPrivateKey) keyPair.getPrivate();
pem = new PEMECPrivate(pubKey, prvKey);
headType = TYPE_PEM_EC;
} else {
throw new SSH2FatalException("Unsupported key type: " + publicKey);
}
armour.setHeaderLine(BEGIN_PRV_KEY[headType]);
armour.setTailLine(END_PRV_KEY[headType]);
ByteArrayOutputStream enc = new ByteArrayOutputStream(128);
ASN1DER der = new ASN1DER();
try {
der.encode(enc, pem);
} catch (IOException e) {
throw new SSH2FatalException("Error while DER encoding");
}
byte[] keyBlob = enc.toByteArray();
if (password != null && password.length() > 0) {
byte[] iv = new byte[16];
random.setSeed(keyBlob);
for (int i = 0; i < iv.length; i++) {
byte[] r = new byte[1];
do {
random.nextBytes(r);
iv[i] = r[0];
} while (iv[i] == 0x00);
}
armour.setHeaderField(PRV_PROCTYPE, "4,ENCRYPTED");
armour.setHeaderField(PRV_DEKINFO, "AES-128-CBC," + HexDump.toString(iv).toUpperCase());
int encLen = (16 - (keyBlob.length % 16)) + keyBlob.length;
byte[] encBuf = new byte[encLen];
doCipher(Cipher.ENCRYPT_MODE, "AES/CBC/PKCS5Padding", password, keyBlob, keyBlob.length, encBuf, iv);
keyBlob = encBuf;
}
return keyBlob;
}
use of com.mindbright.asn1.ASN1Object in project SpringRemote by HaleyWang.
the class AttributeTypeAndValue method valueAsString.
private String valueAsString() {
ASN1Object vo = value.getValue();
String vs = "<unknown>";
if (vo instanceof DirectoryString) {
vs = ((DirectoryString) vo).getString();
} else if (vo instanceof ASN1CharString) {
vs = ((ASN1CharString) vo).getValue();
} else if (vo instanceof ASN1PrintableString) {
vs = ((ASN1PrintableString) vo).getValue();
} else if (vo instanceof ASN1BitString) {
vs = ((ASN1BitString) vo).toPrintableString();
}
return vs;
}
use of com.mindbright.asn1.ASN1Object in project SpringRemote by HaleyWang.
the class X509Certificate method getExtensionWithOID.
private ASN1Object getExtensionWithOID(String oid, Class<?> c) {
try {
Extensions es = certificate.tbsCertificate.extensions;
for (int i = 0; i < es.getCount(); i++) {
Extension e = (Extension) es.getComponent(i);
if (e.extnID.getString().equals(oid)) {
ASN1DER der = new ASN1DER();
ByteArrayInputStream ba = new ByteArrayInputStream(e.extnValue.getRaw());
ASN1Object obj = (ASN1Object) c.newInstance();
der.decode(ba, obj);
return obj;
}
}
} catch (Throwable t) {
}
return null;
}
use of com.mindbright.asn1.ASN1Object in project SpringRemote by HaleyWang.
the class PKCS12KeyStore method getAttribute.
private String getAttribute(SafeBag safeBag, String attrType) {
int cnt = safeBag.bagAttributes.getCount();
String value = null;
for (int i = 0; i < cnt; i++) {
Attribute a = (Attribute) safeBag.bagAttributes.getComponent(i);
if (attrType.equals(a.type.getString())) {
ASN1Object v = a.values.getComponent(0);
if (v instanceof ASN1CharString) {
value = ((ASN1CharString) v).getValue();
} else if (v instanceof ASN1OctetString) {
value = HexDump.toString(((ASN1OctetString) v).getRaw());
} else {
value = v.toString();
}
}
}
return value;
}
use of com.mindbright.asn1.ASN1Object in project titan.EclipsePlug-ins by eclipse.
the class Undefined_FieldSpecification method classifyFieldSpecification.
private void classifyFieldSpecification(final CompilationTimeStamp timestamp) {
final IReferenceChain temporalReferenceChain = ReferenceChain.getInstance(IReferenceChain.CIRCULARREFERENCE, true);
if (isOptional && (null != defaultSetting1 || null != mDefaultSetting)) {
location.reportSemanticError("OPTIONAL and DEFAULT are mutually exclusive");
isOptional = false;
}
if (temporalReferenceChain.add(this) && null != governorReference) {
governorReference.setMyScope(myObjectClass.getMyScope());
if (null != defaultSetting1) {
defaultSetting1.setMyScope(myObjectClass.getMyScope());
}
if (identifier.isvalidAsnObjectSetFieldReference() && governorReference.refersToSettingType(timestamp, Setting_type.S_OC, temporalReferenceChain)) {
ObjectSet defaultObjectset = null;
if (null != mDefaultSetting) {
defaultObjectset = new ObjectSet_definition(mDefaultSetting);
}
final ObjectClass_refd oc = new ObjectClass_refd(governorReference);
oc.setLocation(governorReference.getLocation());
fieldSpecification = new ObjectSet_FieldSpecification(identifier, oc, isOptional, defaultObjectset);
} else if (identifier.isvalidAsnObjectFieldReference() && governorReference.refersToSettingType(timestamp, Setting_type.S_OC, temporalReferenceChain)) {
ASN1Object defaultObject = null;
if (null != defaultSetting1) {
defaultObject = new ReferencedObject(defaultSetting1);
} else if (null != mDefaultSetting) {
defaultObject = new Object_Definition(mDefaultSetting);
}
fieldSpecification = new Object_FieldSpecification(identifier, new ObjectClass_refd(governorReference), isOptional, defaultObject);
} else if (identifier.isvalidAsnValueFieldReference() && (governorReference.refersToSettingType(timestamp, Setting_type.S_T, temporalReferenceChain) || governorReference.refersToSettingType(timestamp, Setting_type.S_VS, temporalReferenceChain))) {
IValue defaultValue = null;
if (null != defaultSetting1) {
if (defaultSetting1 instanceof Defined_Reference && null == defaultSetting1.getModuleIdentifier()) {
defaultValue = new Undefined_LowerIdentifier_Value(defaultSetting1.getId().newInstance());
} else {
defaultValue = new Referenced_Value(defaultSetting1);
}
} else if (null != mDefaultSetting) {
defaultValue = new Undefined_Block_Value(mDefaultSetting);
}
fieldSpecification = new FixedTypeValue_FieldSpecification(identifier, new Referenced_Type(governorReference), false, isOptional, null != defaultSetting1 && null != mDefaultSetting, defaultValue);
}
}
if (null == fieldSpecification) {
location.reportSemanticError(CANNOTRECOGNISE);
fieldSpecification = new Erroneous_FieldSpecification(identifier, isOptional, null != defaultSetting1 || null != mDefaultSetting);
} else {
if (null != myObjectClass) {
fieldSpecification.setMyObjectClass(myObjectClass);
}
}
fieldSpecification.setFullNameParent(getNameParent());
fieldSpecification.setLocation(location);
temporalReferenceChain.release();
}
Aggregations