use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project LinLong-Java by zhenwei1108.
the class EncKeyWithID method toASN1Primitive.
/**
* <pre>
* EncKeyWithID ::= SEQUENCE {
* privateKey PrivateKeyInfo,
* identifier CHOICE {
* string UTF8String,
* generalName GeneralName
* } OPTIONAL
* }
* </pre>
*
* @return an ASN.1 primitive composition of this EncKeyWithID.
*/
public ASN1Primitive toASN1Primitive() {
ASN1EncodableVector v = new ASN1EncodableVector(2);
v.add(privKeyInfo);
if (identifier != null) {
v.add(identifier);
}
return new DERSequence(v);
}
use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project LinLong-Java by zhenwei1108.
the class BCECPrivateKey method populateFromPrivKeyInfo.
private void populateFromPrivKeyInfo(PrivateKeyInfo info) throws IOException {
X962Parameters params = X962Parameters.getInstance(info.getPrivateKeyAlgorithm().getParameters());
ECCurve curve = EC5Util.getCurve(configuration, params);
ecSpec = EC5Util.convertToSpec(params, curve);
ASN1Encodable privKey = info.parsePrivateKey();
if (privKey instanceof ASN1Integer) {
ASN1Integer derD = ASN1Integer.getInstance(privKey);
this.d = derD.getValue();
} else {
com.github.zhenwei.core.asn1.sec.ECPrivateKey ec = com.github.zhenwei.core.asn1.sec.ECPrivateKey.getInstance(privKey);
this.d = ec.getKey();
this.publicKey = ec.getPublicKey();
}
}
use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project LinLong-Java by zhenwei1108.
the class BCDSTU4145PrivateKey method getEncoded.
/**
* Return a PKCS8 representation of the key. The sequence returned represents a full
* PrivateKeyInfo object.
*
* @return a PKCS8 representation of the key.
*/
public byte[] getEncoded() {
X962Parameters params;
int orderBitLength;
if (ecSpec instanceof ECNamedCurveSpec) {
ASN1ObjectIdentifier curveOid = ECUtil.getNamedCurveOid(((ECNamedCurveSpec) ecSpec).getName());
if (// guess it's the OID
curveOid == null) {
curveOid = new ASN1ObjectIdentifier(((ECNamedCurveSpec) ecSpec).getName());
}
params = new X962Parameters(curveOid);
orderBitLength = ECUtil.getOrderBitLength(WeGooProvider.CONFIGURATION, ecSpec.getOrder(), this.getS());
} else if (ecSpec == null) {
params = new X962Parameters(DERNull.INSTANCE);
orderBitLength = ECUtil.getOrderBitLength(WeGooProvider.CONFIGURATION, null, this.getS());
} else {
ECCurve curve = EC5Util.convertCurve(ecSpec.getCurve());
X9ECParameters ecP = new X9ECParameters(curve, new X9ECPoint(EC5Util.convertPoint(curve, ecSpec.getGenerator()), withCompression), ecSpec.getOrder(), BigInteger.valueOf(ecSpec.getCofactor()), ecSpec.getCurve().getSeed());
params = new X962Parameters(ecP);
orderBitLength = ECUtil.getOrderBitLength(WeGooProvider.CONFIGURATION, ecSpec.getOrder(), this.getS());
}
PrivateKeyInfo info;
com.github.zhenwei.core.asn1.sec.ECPrivateKey keyStructure;
if (publicKey != null) {
keyStructure = new com.github.zhenwei.core.asn1.sec.ECPrivateKey(orderBitLength, this.getS(), publicKey, params);
} else {
keyStructure = new com.github.zhenwei.core.asn1.sec.ECPrivateKey(orderBitLength, this.getS(), params);
}
try {
if (algorithm.equals("DSTU4145")) {
info = new PrivateKeyInfo(new AlgorithmIdentifier(UAObjectIdentifiers.dstu4145be, params.toASN1Primitive()), keyStructure.toASN1Primitive());
} else {
info = new PrivateKeyInfo(new AlgorithmIdentifier(X9ObjectIdentifiers.id_ecPublicKey, params.toASN1Primitive()), keyStructure.toASN1Primitive());
}
return info.getEncoded(ASN1Encoding.DER);
} catch (IOException e) {
return null;
}
}
use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project LinLong-Java by zhenwei1108.
the class BcFKSKeyStoreSpi method engineGetKey.
public Key engineGetKey(String alias, char[] password) throws NoSuchAlgorithmException, UnrecoverableKeyException {
ObjectData ent = (ObjectData) entries.get(alias);
if (ent != null) {
if (ent.getType().equals(PRIVATE_KEY) || ent.getType().equals(PROTECTED_PRIVATE_KEY)) {
PrivateKey cachedKey = (PrivateKey) privateKeyCache.get(alias);
if (cachedKey != null) {
return cachedKey;
}
EncryptedPrivateKeyData encPrivData = EncryptedPrivateKeyData.getInstance(ent.getData());
EncryptedPrivateKeyInfo encInfo = EncryptedPrivateKeyInfo.getInstance(encPrivData.getEncryptedPrivateKeyInfo());
try {
PrivateKeyInfo pInfo = PrivateKeyInfo.getInstance(decryptData("PRIVATE_KEY_ENCRYPTION", encInfo.getEncryptionAlgorithm(), password, encInfo.getEncryptedData()));
KeyFactory kFact = helper.createKeyFactory(getPublicKeyAlg(pInfo.getPrivateKeyAlgorithm().getAlgorithm()));
PrivateKey privateKey = kFact.generatePrivate(new PKCS8EncodedKeySpec(pInfo.getEncoded()));
// check that the key pair and the certificate public key are consistent
// TODO: new ConsistentKeyPair(engineGetCertificate(alias).getPublicKey(), privateKey);
privateKeyCache.put(alias, privateKey);
return privateKey;
} catch (Exception e) {
throw new UnrecoverableKeyException("BCFKS KeyStore unable to recover private key (" + alias + "): " + e.getMessage());
}
} else if (ent.getType().equals(SECRET_KEY) || ent.getType().equals(PROTECTED_SECRET_KEY)) {
EncryptedSecretKeyData encKeyData = EncryptedSecretKeyData.getInstance(ent.getData());
try {
SecretKeyData keyData = SecretKeyData.getInstance(decryptData("SECRET_KEY_ENCRYPTION", encKeyData.getKeyEncryptionAlgorithm(), password, encKeyData.getEncryptedKeyData()));
SecretKeyFactory kFact = helper.createSecretKeyFactory(keyData.getKeyAlgorithm().getId());
return kFact.generateSecret(new SecretKeySpec(keyData.getKeyBytes(), keyData.getKeyAlgorithm().getId()));
} catch (Exception e) {
throw new UnrecoverableKeyException("BCFKS KeyStore unable to recover secret key (" + alias + "): " + e.getMessage());
}
} else {
throw new UnrecoverableKeyException("BCFKS KeyStore unable to recover secret key (" + alias + "): type not recognized");
}
}
return null;
}
use of com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo in project LinLong-Java by zhenwei1108.
the class WeGooKeyProtector method recover.
public Key recover(EncryptedPrivateKeyInfo var1) throws UnrecoverableKeyException {
AlgorithmId var7 = var1.getAlgorithm();
if (!var7.getOID().toString().equals("1.3.6.1.4.1.42.2.17.1.1")) {
throw new UnrecoverableKeyException("Unsupported key protection algorithm");
} else {
byte[] var8 = var1.getEncryptedData();
byte[] var9 = new byte[20];
System.arraycopy(var8, 0, var9, 0, 20);
int var6 = var8.length - 20 - 20;
int var4 = var6 / 20;
if (var6 % 20 != 0) {
++var4;
}
byte[] var10 = new byte[var6];
System.arraycopy(var8, 20, var10, 0, var6);
byte[] var11 = new byte[var10.length];
int var2 = 0;
int var5 = 0;
byte[] var3;
for (var3 = var9; var2 < var4; var5 += 20) {
this.md.update(this.passwdBytes);
this.md.update(var3);
var3 = this.md.digest();
this.md.reset();
if (var2 < var4 - 1) {
System.arraycopy(var3, 0, var11, var5, var3.length);
} else {
System.arraycopy(var3, 0, var11, var5, var11.length - var5);
}
++var2;
}
byte[] var12 = new byte[var10.length];
for (var2 = 0; var2 < var12.length; ++var2) {
var12[var2] = (byte) (var10[var2] ^ var11[var2]);
}
this.md.update(this.passwdBytes);
Arrays.fill(this.passwdBytes, (byte) 0);
this.passwdBytes = null;
this.md.update(var12);
var3 = this.md.digest();
this.md.reset();
for (var2 = 0; var2 < var3.length; ++var2) {
if (var3[var2] != var8[20 + var6 + var2]) {
throw new UnrecoverableKeyException("Cannot recover key");
}
}
try {
// return PKCS8Key.parseKey(new DerValue(var12));
PrivateKeyInfo info = PrivateKeyInfo.getInstance(var12);
if (info == null) {
throw new UnrecoverableKeyException("Recover key can not null");
}
KeyPairAlgEnum algEnum = KeyPairAlgEnum.match(info.getPrivateKeyAlgorithm().getAlgorithm());
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(var12);
KeyFactory factory = KeyFactory.getInstance(algEnum.getAlg(), new WeGooProvider());
return factory.generatePrivate(spec);
} catch (Exception var14) {
throw new UnrecoverableKeyException(var14.getMessage());
}
}
}
Aggregations