use of com.unboundid.asn1.ASN1BitString in project jmulticard by ctt-gob-es.
the class PrivateKeyFactory method createKey.
/**
* Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object.
*
* @param keyInfo the PrivateKeyInfo object containing the key material
* @return a suitable private key parameter
* @throws IOException on an error decoding the key
*/
public static AsymmetricKeyParameter createKey(final PrivateKeyInfo keyInfo) throws IOException {
final AlgorithmIdentifier algId = keyInfo.getPrivateKeyAlgorithm();
final ASN1ObjectIdentifier algOID = algId.getAlgorithm();
if (algOID.on(BCObjectIdentifiers.qTESLA)) {
final ASN1OctetString qTESLAPriv = ASN1OctetString.getInstance(keyInfo.parsePrivateKey());
return new QTESLAPrivateKeyParameters(Utils.qTeslaLookupSecurityCategory(keyInfo.getPrivateKeyAlgorithm()), qTESLAPriv.getOctets());
}
if (algOID.equals(BCObjectIdentifiers.sphincs256)) {
return new SPHINCSPrivateKeyParameters(ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets(), Utils.sphincs256LookupTreeAlgName(SPHINCS256KeyParams.getInstance(keyInfo.getPrivateKeyAlgorithm().getParameters())));
}
if (algOID.equals(BCObjectIdentifiers.newHope)) {
return new NHPrivateKeyParameters(convert(ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets()));
}
if (algOID.equals(PKCSObjectIdentifiers.id_alg_hss_lms_hashsig)) {
final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
final ASN1BitString pubKey = keyInfo.getPublicKeyData();
if (Pack.bigEndianToInt(keyEnc, 0) == 1) {
if (pubKey != null) {
final byte[] pubEnc = pubKey.getOctets();
return LMSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length), Arrays.copyOfRange(pubEnc, 4, pubEnc.length));
}
return LMSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
}
if (pubKey != null) {
final byte[] pubEnc = pubKey.getOctets();
return HSSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length), pubEnc);
}
return HSSPrivateKeyParameters.getInstance(Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
}
if (algOID.on(BCObjectIdentifiers.sphincsPlus)) {
final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
final SPHINCSPlusParameters spParams = SPHINCSPlusParameters.getParams(Integers.valueOf(Pack.bigEndianToInt(keyEnc, 0)));
return new SPHINCSPlusPrivateKeyParameters(spParams, Arrays.copyOfRange(keyEnc, 4, keyEnc.length));
}
if (algOID.on(BCObjectIdentifiers.pqc_kem_mceliece)) {
final CMCEPrivateKey cmceKey = CMCEPrivateKey.getInstance(keyInfo.parsePrivateKey());
final CMCEParameters spParams = Utils.mcElieceParamsLookup(keyInfo.getPrivateKeyAlgorithm().getAlgorithm());
return new CMCEPrivateKeyParameters(spParams, cmceKey.getDelta(), cmceKey.getC(), cmceKey.getG(), cmceKey.getAlpha(), cmceKey.getS());
} else if (algOID.on(BCObjectIdentifiers.pqc_kem_frodo)) {
final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
final FrodoParameters spParams = Utils.frodoParamsLookup(keyInfo.getPrivateKeyAlgorithm().getAlgorithm());
return new FrodoPrivateKeyParameters(spParams, keyEnc);
} else if (algOID.on(BCObjectIdentifiers.pqc_kem_saber)) {
final byte[] keyEnc = ASN1OctetString.getInstance(keyInfo.parsePrivateKey()).getOctets();
final SABERParameters spParams = Utils.saberParamsLookup(keyInfo.getPrivateKeyAlgorithm().getAlgorithm());
return new SABERPrivateKeyParameters(spParams, keyEnc);
} else if (algOID.equals(BCObjectIdentifiers.xmss) || algOID.equals(PQCObjectIdentifiers.xmss_mt)) {
// $NON-NLS-1$
throw new IOException("Modificacion para JMultiCard");
} else if (algOID.equals(PQCObjectIdentifiers.mcElieceCca2)) {
final McElieceCCA2PrivateKey mKey = McElieceCCA2PrivateKey.getInstance(keyInfo.parsePrivateKey());
return new McElieceCCA2PrivateKeyParameters(mKey.getN(), mKey.getK(), mKey.getField(), mKey.getGoppaPoly(), mKey.getP(), Utils.getDigestName(mKey.getDigest().getAlgorithm()));
} else {
// $NON-NLS-1$
throw new RuntimeException("algorithm identifier in private key not recognised");
}
}
use of com.unboundid.asn1.ASN1BitString in project openkeystore by cyberphone.
the class XMLSignatureWrapper method readPublicKey.
public static PublicKey readPublicKey(DOMReaderHelper rd) throws IOException {
PublicKey publicKey = null;
try {
if (rd.hasNext(RSA_KEY_VALUE_ELEM)) {
rd.getNext(RSA_KEY_VALUE_ELEM);
rd.getChild();
publicKey = KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(readCryptoBinary(rd, MODULUS_ELEM), readCryptoBinary(rd, EXPONENT_ELEM)));
} else {
rd.getNext(EC_KEY_VALUE_ELEM);
rd.getChild();
rd.getNext(NAMED_CURVE_ELEM);
final String named_curve = rd.getAttributeHelper().getString(URI_ATTR);
if (!named_curve.startsWith(KeyAlgorithms.XML_DSIG_CURVE_PREFIX)) {
throw new IOException("Syntax error: " + named_curve);
}
publicKey = KeyFactory.getInstance("EC").generatePublic(new X509EncodedKeySpec(new ASN1Sequence(new BaseASN1Object[] { new ASN1Sequence(new BaseASN1Object[] { new ASN1ObjectID(EC_NAMED_CURVES_OID), new ASN1ObjectID(named_curve.substring(KeyAlgorithms.XML_DSIG_CURVE_PREFIX.length())) }), new ASN1BitString(rd.getBinary(PUBLIC_KEY_ELEM)) }).encode()));
}
} catch (GeneralSecurityException e) {
throw new IOException(e);
}
rd.getParent();
// Verify that it is one of the supported
KeyAlgorithms.getKeyAlgorithm(publicKey);
return publicKey;
}
use of com.unboundid.asn1.ASN1BitString in project openkeystore by cyberphone.
the class KeyStore2PEMConverter method writePrivateKey.
public void writePrivateKey(PrivateKey privateKey, PublicKey publicKey) throws Exception {
byte[] encoded;
KeyAlgorithms keyAlgorithm = KeyAlgorithms.getKeyAlgorithm(privateKey);
if (privateKey instanceof RSAKey) {
encoded = privateKey.getEncoded();
} else if (privateKey instanceof ECKey) {
JSONObjectReader jwk = JSONParser.parse(new KeyStore2JWKConverter().writePrivateKey(privateKey, publicKey));
encoded = new ASN1Sequence(new BaseASN1Object[] { new ASN1Integer(0), new ASN1Sequence(new BaseASN1Object[] { new ASN1ObjectID(EC_PUBLIC_KEY_OID), new ASN1ObjectID(keyAlgorithm.getECDomainOID()) }), new ASN1OctetString(new ASN1Sequence(new BaseASN1Object[] { new ASN1Integer(1), new ASN1OctetString(jwk.getBinary("d")), new CompositeContextSpecific(1, new ASN1BitString(ArrayUtil.add(new byte[] { 4 }, ArrayUtil.add(jwk.getBinary("x"), jwk.getBinary("y"))))) }).encode()) }).encode();
} else {
encoded = new ASN1Sequence(new BaseASN1Object[] { new ASN1Integer(1), new ASN1Sequence(new ASN1ObjectID(keyAlgorithm.getECDomainOID())), new ASN1OctetString(new ASN1OctetString(OkpSupport.private2RawOkpKey(privateKey, keyAlgorithm)).encode()), new SimpleContextSpecific(1, // BITSTRING unused bits
ArrayUtil.add(// BITSTRING unused bits
new byte[] { 0 }, OkpSupport.public2RawOkpKey(publicKey, keyAlgorithm))) }).encode();
}
writeObject("PRIVATE KEY", encoded);
}
use of com.unboundid.asn1.ASN1BitString in project LinLong-Java by zhenwei1108.
the class BCECGOST3410PublicKey method populateFromPubKeyInfo.
private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
ASN1BitString bits = info.getPublicKeyData();
ASN1OctetString key;
this.algorithm = "ECGOST3410";
try {
key = (ASN1OctetString) ASN1Primitive.fromByteArray(bits.getBytes());
} catch (IOException ex) {
throw new IllegalArgumentException("error recovering public key");
}
byte[] keyEnc = key.getOctets();
byte[] x9Encoding = new byte[65];
x9Encoding[0] = 0x04;
for (int i = 1; i <= 32; ++i) {
x9Encoding[i] = keyEnc[32 - i];
x9Encoding[i + 32] = keyEnc[64 - i];
}
ASN1ObjectIdentifier paramOID;
if (info.getAlgorithm().getParameters() instanceof ASN1ObjectIdentifier) {
paramOID = ASN1ObjectIdentifier.getInstance(info.getAlgorithm().getParameters());
gostParams = paramOID;
} else {
GOST3410PublicKeyAlgParameters params = GOST3410PublicKeyAlgParameters.getInstance(info.getAlgorithm().getParameters());
gostParams = params;
paramOID = params.getPublicKeyParamSet();
}
ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(paramOID));
ECCurve curve = spec.getCurve();
EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
this.ecPublicKey = new ECPublicKeyParameters(curve.decodePoint(x9Encoding), ECUtil.getDomainParameters(null, spec));
this.ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(paramOID), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
}
use of com.unboundid.asn1.ASN1BitString in project LinLong-Java by zhenwei1108.
the class BCECGOST3410_2012PublicKey method populateFromPubKeyInfo.
private void populateFromPubKeyInfo(SubjectPublicKeyInfo info) {
ASN1ObjectIdentifier algOid = info.getAlgorithm().getAlgorithm();
ASN1BitString bits = info.getPublicKeyData();
ASN1OctetString key;
this.algorithm = "ECGOST3410-2012";
try {
key = (ASN1OctetString) ASN1Primitive.fromByteArray(bits.getBytes());
} catch (IOException ex) {
throw new IllegalArgumentException("error recovering public key");
}
byte[] keyEnc = key.getOctets();
int fieldSize = 32;
if (algOid.equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512)) {
fieldSize = 64;
}
int keySize = 2 * fieldSize;
byte[] x9Encoding = new byte[1 + keySize];
x9Encoding[0] = 0x04;
for (int i = 1; i <= fieldSize; ++i) {
x9Encoding[i] = keyEnc[fieldSize - i];
x9Encoding[i + fieldSize] = keyEnc[keySize - i];
}
this.gostParams = GOST3410PublicKeyAlgParameters.getInstance(info.getAlgorithm().getParameters());
ECNamedCurveParameterSpec spec = ECGOST3410NamedCurveTable.getParameterSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()));
ECCurve curve = spec.getCurve();
EllipticCurve ellipticCurve = EC5Util.convertCurve(curve, spec.getSeed());
this.ecPublicKey = new ECPublicKeyParameters(curve.decodePoint(x9Encoding), ECUtil.getDomainParameters(null, spec));
this.ecSpec = new ECNamedCurveSpec(ECGOST3410NamedCurves.getName(gostParams.getPublicKeyParamSet()), ellipticCurve, EC5Util.convertPoint(spec.getG()), spec.getN(), spec.getH());
}
Aggregations