Search in sources :

Example 61 with DEROctetString

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);
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) EdDSAPublicKey(com.github.zhenwei.provider.jcajce.interfaces.EdDSAPublicKey) OpenSSHPrivateKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPrivateKeySpec) IOException(java.io.IOException) Ed25519PrivateKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PrivateKeyParameters) XDHPublicKey(com.github.zhenwei.provider.jcajce.interfaces.XDHPublicKey) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) RawEncodedKeySpec(com.github.zhenwei.provider.jcajce.spec.RawEncodedKeySpec) OpenSSHPublicKeySpec(com.github.zhenwei.provider.jcajce.spec.OpenSSHPublicKeySpec) Ed25519PublicKeyParameters(com.github.zhenwei.core.crypto.params.Ed25519PublicKeyParameters) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 62 with DEROctetString

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);
}
Also used : X9ECParameters(com.github.zhenwei.core.asn1.x9.X9ECParameters) GOST3410PublicKeyAlgParameters(com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters) IOException(java.io.IOException) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECPoint(java.security.spec.ECPoint) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier) X962Parameters(com.github.zhenwei.core.asn1.x9.X962Parameters) X9ECPoint(com.github.zhenwei.core.asn1.x9.X9ECPoint) ECCurve(com.github.zhenwei.core.math.ec.ECCurve) BigInteger(java.math.BigInteger) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ECNamedCurveSpec(com.github.zhenwei.provider.jce.spec.ECNamedCurveSpec)

Example 63 with DEROctetString

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;
    }
}
Also used : GOST3410PublicKeyAlgParameters(com.github.zhenwei.core.asn1.cryptopro.GOST3410PublicKeyAlgParameters) IOException(java.io.IOException) GOST3410ParameterSpec(com.github.zhenwei.provider.jce.spec.GOST3410ParameterSpec) SubjectPublicKeyInfo(com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 64 with DEROctetString

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;
}
Also used : CertificateNotYetValidException(java.security.cert.CertificateNotYetValidException) ASN1InputStream(com.github.zhenwei.core.asn1.ASN1InputStream) CertificateExpiredException(java.security.cert.CertificateExpiredException) Targets(com.github.zhenwei.core.asn1.x509.Targets) IOException(java.io.IOException) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) Target(com.github.zhenwei.core.asn1.x509.Target) TargetInformation(com.github.zhenwei.core.asn1.x509.TargetInformation)

Example 65 with DEROctetString

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);
    }
}
Also used : WeGooKeyException(com.github.zhenwei.sdk.exception.WeGooKeyException) DLSequence(com.github.zhenwei.core.asn1.DLSequence) KeyPairAlgEnum(com.github.zhenwei.sdk.enums.KeyPairAlgEnum) PrivateKeyInfo(com.github.zhenwei.core.asn1.pkcs.PrivateKeyInfo) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) BaseWeGooException(com.github.zhenwei.sdk.exception.BaseWeGooException) WeGooCryptoException(com.github.zhenwei.sdk.exception.WeGooCryptoException) WeGooKeyException(com.github.zhenwei.sdk.exception.WeGooKeyException)

Aggregations

DEROctetString (org.bouncycastle.asn1.DEROctetString)139 IOException (java.io.IOException)104 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)86 ASN1EncodableVector (com.github.zhenwei.core.asn1.ASN1EncodableVector)49 DERSequence (org.bouncycastle.asn1.DERSequence)48 ASN1EncodableVector (org.bouncycastle.asn1.ASN1EncodableVector)46 DERSequence (com.github.zhenwei.core.asn1.DERSequence)44 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)39 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)32 BigInteger (java.math.BigInteger)27 Extension (org.bouncycastle.asn1.x509.Extension)27 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)26 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)26 AlgorithmIdentifier (org.bouncycastle.asn1.x509.AlgorithmIdentifier)24 ASN1Integer (org.bouncycastle.asn1.ASN1Integer)23 DERTaggedObject (org.bouncycastle.asn1.DERTaggedObject)21 ASN1Integer (com.github.zhenwei.core.asn1.ASN1Integer)20 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)19 Extensions (org.bouncycastle.asn1.x509.Extensions)19 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)18