use of com.github.zhenwei.core.asn1.DEROctetString in project LinLong-Java by zhenwei1108.
the class KeyFactorySpi method engineGetKeySpec.
protected KeySpec engineGetKeySpec(Key key, Class spec) throws InvalidKeySpecException {
if (spec.isAssignableFrom(OpenSSHPrivateKeySpec.class) && key instanceof BCEdDSAPrivateKey) {
try {
//
// The DEROctetString at element 2 is an encoded DEROctetString with the private key value
// within it.
//
ASN1Sequence seq = ASN1Sequence.getInstance(key.getEncoded());
ASN1OctetString val = ASN1OctetString.getInstance(seq.getObjectAt(2));
byte[] encoding = ASN1OctetString.getInstance(ASN1Primitive.fromByteArray(val.getOctets())).getOctets();
return new OpenSSHPrivateKeySpec(OpenSSHPrivateKeyUtil.encodePrivateKey(new Ed25519PrivateKeyParameters(encoding)));
} catch (IOException ex) {
throw new InvalidKeySpecException(ex.getMessage(), ex.getCause());
}
} else if (spec.isAssignableFrom(OpenSSHPublicKeySpec.class) && key instanceof BCEdDSAPublicKey) {
try {
byte[] encoding = key.getEncoded();
if (!Arrays.areEqual(Ed25519Prefix, 0, Ed25519Prefix.length, encoding, 0, encoding.length - Ed25519PublicKeyParameters.KEY_SIZE)) {
throw new InvalidKeySpecException("Invalid Ed25519 public key encoding");
}
Ed25519PublicKeyParameters publicKey = new Ed25519PublicKeyParameters(encoding, Ed25519Prefix.length);
return new OpenSSHPublicKeySpec(OpenSSHPublicKeyUtil.encodePublicKey(publicKey));
} catch (IOException ex) {
throw new InvalidKeySpecException(ex.getMessage(), ex.getCause());
}
} else if (spec.isAssignableFrom(RawEncodedKeySpec.class)) {
if (key instanceof XDHPublicKey) {
return new RawEncodedKeySpec(((XDHPublicKey) key).getUEncoding());
}
if (key instanceof EdDSAPublicKey) {
return new RawEncodedKeySpec(((EdDSAPublicKey) key).getPointEncoding());
}
}
return super.engineGetKeySpec(key, spec);
}
use of com.github.zhenwei.core.asn1.DEROctetString in project LinLong-Java by zhenwei1108.
the class BCECGOST3410_2012PublicKey method getEncoded.
public byte[] getEncoded() {
ASN1Encodable params;
SubjectPublicKeyInfo info;
// ecPublicKey.getQ().
BigInteger bX = this.ecPublicKey.getQ().getAffineXCoord().toBigInteger();
BigInteger bY = this.ecPublicKey.getQ().getAffineYCoord().toBigInteger();
// need to detect key size
boolean is512 = (bX.bitLength() > 256);
params = getGostParams();
if (params == null) {
if (ecSpec instanceof ECNamedCurveSpec) {
if (is512) {
params = new GOST3410PublicKeyAlgParameters(ECGOST3410NamedCurves.getOID(((ECNamedCurveSpec) ecSpec).getName()), RosstandartObjectIdentifiers.id_tc26_gost_3411_12_512);
} else {
params = new GOST3410PublicKeyAlgParameters(ECGOST3410NamedCurves.getOID(((ECNamedCurveSpec) ecSpec).getName()), RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256);
}
} else {
// strictly speaking this may not be applicable...
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);
}
}
int encKeySize;
int offset;
ASN1ObjectIdentifier algIdentifier;
if (is512) {
encKeySize = 128;
offset = 64;
algIdentifier = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512;
} else {
encKeySize = 64;
offset = 32;
algIdentifier = RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256;
}
byte[] encKey = new byte[encKeySize];
extractBytes(encKey, encKeySize / 2, 0, bX);
extractBytes(encKey, encKeySize / 2, offset, bY);
try {
info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(algIdentifier, params), new DEROctetString(encKey));
} catch (IOException e) {
return null;
}
return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
}
use of com.github.zhenwei.core.asn1.DEROctetString in project LinLong-Java by zhenwei1108.
the class BCGOST3410PublicKey method getEncoded.
public byte[] getEncoded() {
SubjectPublicKeyInfo info;
byte[] keyEnc = this.getY().toByteArray();
byte[] keyBytes;
if (keyEnc[0] == 0) {
keyBytes = new byte[keyEnc.length - 1];
} else {
keyBytes = new byte[keyEnc.length];
}
for (int i = 0; i != keyBytes.length; i++) {
// must be little endian
keyBytes[i] = keyEnc[keyEnc.length - 1 - i];
}
try {
if (gost3410Spec instanceof GOST3410ParameterSpec) {
if (gost3410Spec.getEncryptionParamSetOID() != null) {
info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94, new GOST3410PublicKeyAlgParameters(new ASN1ObjectIdentifier(gost3410Spec.getPublicKeyParamSetOID()), new ASN1ObjectIdentifier(gost3410Spec.getDigestParamSetOID()), new ASN1ObjectIdentifier(gost3410Spec.getEncryptionParamSetOID()))), new DEROctetString(keyBytes));
} else {
info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94, new GOST3410PublicKeyAlgParameters(new ASN1ObjectIdentifier(gost3410Spec.getPublicKeyParamSetOID()), new ASN1ObjectIdentifier(gost3410Spec.getDigestParamSetOID()))), new DEROctetString(keyBytes));
}
} else {
info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94), new DEROctetString(keyBytes));
}
return KeyUtil.getEncodedSubjectPublicKeyInfo(info);
} catch (IOException e) {
return null;
}
}
use of com.github.zhenwei.core.asn1.DEROctetString in project LinLong-Java by zhenwei1108.
the class X509AttributeCertStoreSelector method match.
/**
* Decides if the given attribute certificate should be selected.
*
* @param obj The attribute certificate which should be checked.
* @return <code>true</code> if the attribute certificate can be selected,
* <code>false</code> otherwise.
*/
public boolean match(Object obj) {
if (!(obj instanceof X509AttributeCertificate)) {
return false;
}
X509AttributeCertificate attrCert = (X509AttributeCertificate) obj;
if (this.attributeCert != null) {
if (!this.attributeCert.equals(attrCert)) {
return false;
}
}
if (serialNumber != null) {
if (!attrCert.getSerialNumber().equals(serialNumber)) {
return false;
}
}
if (holder != null) {
if (!attrCert.getHolder().equals(holder)) {
return false;
}
}
if (issuer != null) {
if (!attrCert.getIssuer().equals(issuer)) {
return false;
}
}
if (attributeCertificateValid != null) {
try {
attrCert.checkValidity(attributeCertificateValid);
} catch (CertificateExpiredException e) {
return false;
} catch (CertificateNotYetValidException e) {
return false;
}
}
if (!targetNames.isEmpty() || !targetGroups.isEmpty()) {
byte[] targetInfoExt = attrCert.getExtensionValue(Extension.targetInformation.getId());
if (targetInfoExt != null) {
TargetInformation targetinfo;
try {
targetinfo = TargetInformation.getInstance(new ASN1InputStream(((DEROctetString) DEROctetString.fromByteArray(targetInfoExt)).getOctets()).readObject());
} catch (IOException e) {
return false;
} catch (IllegalArgumentException e) {
return false;
}
Targets[] targetss = targetinfo.getTargetsObjects();
if (!targetNames.isEmpty()) {
boolean found = false;
for (int i = 0; i < targetss.length; i++) {
Targets t = targetss[i];
Target[] targets = t.getTargets();
for (int j = 0; j < targets.length; j++) {
if (targetNames.contains(GeneralName.getInstance(targets[j].getTargetName()))) {
found = true;
break;
}
}
}
if (!found) {
return false;
}
}
if (!targetGroups.isEmpty()) {
boolean found = false;
for (int i = 0; i < targetss.length; i++) {
Targets t = targetss[i];
Target[] targets = t.getTargets();
for (int j = 0; j < targets.length; j++) {
if (targetGroups.contains(GeneralName.getInstance(targets[j].getTargetGroup()))) {
found = true;
break;
}
}
}
if (!found) {
return false;
}
}
}
}
return true;
}
use of com.github.zhenwei.core.asn1.DEROctetString in project LinLong-Java by zhenwei1108.
the class KeyBuilder method getRealPrivateKey.
/**
* @param [privateKey]
* @return byte[]
* @author zhangzhenwei
* @description 获取裸私钥
* @date 2022/2/11 23:06
* @since 1.0
*/
public byte[] getRealPrivateKey(byte[] privateKey) throws WeGooCryptoException {
try {
PrivateKeyInfo info = PrivateKeyInfo.getInstance(privateKey);
if (info == null) {
throw new WeGooKeyException(IExceptionEnum.params_err);
}
KeyPairAlgEnum algEnum = KeyPairAlgEnum.match(info.getPrivateKeyAlgorithm().getAlgorithm());
// SM2 算法
if (algEnum.getAlg().equals(KeyPairAlgEnum.SM2_256.getAlg())) {
DLSequence dlSequence = (DLSequence) DLSequence.fromByteArray(privateKey);
byte[] priKeys = ((DEROctetString) dlSequence.getObjectAt(2)).getOctets();
dlSequence = (DLSequence) DLSequence.fromByteArray(priKeys);
DEROctetString derPriKey = (DEROctetString) dlSequence.getObjectAt(1);
return derPriKey.getOctets();
} else {
return info.getPrivateKey().getOctets();
}
} catch (WeGooKeyException e) {
throw e;
} catch (Exception e) {
throw new WeGooKeyException(KeyExceptionMessageEnum.parse_private_key_err, e);
}
}
Aggregations