use of java.security.spec.X509EncodedKeySpec in project j2objc by google.
the class SubjectPublicKeyInfo method getPublicKey.
/**
* Returns the PublicKey corresponding to this SubjectPublicKeyInfo
* instance.
*/
public PublicKey getPublicKey() {
if (publicKey == null) {
final byte[] encoded = getEncoded();
final KeySpec keySpec = new X509EncodedKeySpec(encoded);
/* Try using the algorithm name first. */
final String algName = algorithmID.getAlgorithmName();
publicKey = generateKeyForAlgorithm(keySpec, algName);
/*
* Fall back to using the algorithm OID if it's not the same as the
* algorithm name.
*/
final String algOid = algorithmID.getAlgorithm();
if (publicKey == null && !algOid.equals(algName)) {
publicKey = generateKeyForAlgorithm(keySpec, algOid);
}
/*
* Encode this as an X.509 public key since we didn't have any
* KeyFactory that could handle this algorithm name or OID. Perhaps
* the thing that's using this can decode it.
*/
if (publicKey == null) {
publicKey = new X509PublicKey(algOid, encoded, subjectPublicKey);
}
}
return publicKey;
}
use of java.security.spec.X509EncodedKeySpec in project bigbluebutton by bigbluebutton.
the class RSA_SHA1 method getPublicKeyFromPem.
private PublicKey getPublicKeyFromPem(String pem) throws GeneralSecurityException, IOException {
InputStream stream = new ByteArrayInputStream(pem.getBytes("UTF-8"));
PEMReader reader = new PEMReader(stream);
byte[] bytes = reader.getDerBytes();
PublicKey pubKey;
if (PEMReader.PUBLIC_X509_MARKER.equals(reader.getBeginMarker())) {
KeySpec keySpec = new X509EncodedKeySpec(bytes);
KeyFactory fac = KeyFactory.getInstance("RSA");
pubKey = fac.generatePublic(keySpec);
} else if (PEMReader.CERTIFICATE_X509_MARKER.equals(reader.getBeginMarker())) {
pubKey = getPublicKeyFromDerCert(bytes);
} else {
throw new IOException("Invalid PEM fileL: Unknown marker for " + " public key or cert " + reader.getBeginMarker());
}
return pubKey;
}
use of java.security.spec.X509EncodedKeySpec in project bigbluebutton by bigbluebutton.
the class RSA_SHA1 method getPublicKeyFromDer.
private PublicKey getPublicKeyFromDer(byte[] publicKeyObject) throws GeneralSecurityException {
KeyFactory fac = KeyFactory.getInstance("RSA");
EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(publicKeyObject);
return fac.generatePublic(pubKeySpec);
}
use of java.security.spec.X509EncodedKeySpec in project XobotOS by xamarin.
the class DSAKeyFactoryImpl method engineGetKeySpec.
/**
* This method returns a specification for the supplied key.
*
* The specification will be returned in the form of an object of the type
* specified by keySpec.
*
* @param key -
* either DSAPrivateKey or DSAPublicKey
* @param keySpec -
* either DSAPrivateKeySpec.class or DSAPublicKeySpec.class
*
* @return either a DSAPrivateKeySpec or a DSAPublicKeySpec
*
* @throws InvalidKeySpecException
* if "keySpec" is not a specification for DSAPublicKey or
* DSAPrivateKey
*/
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
BigInteger p, q, g, x, y;
if (key != null) {
if (keySpec == null) {
throw new NullPointerException("keySpec == null");
}
if (key instanceof DSAPrivateKey) {
DSAPrivateKey privateKey = (DSAPrivateKey) key;
if (keySpec.equals(DSAPrivateKeySpec.class)) {
x = privateKey.getX();
DSAParams params = privateKey.getParams();
p = params.getP();
q = params.getQ();
g = params.getG();
return (T) (new DSAPrivateKeySpec(x, p, q, g));
}
if (keySpec.equals(PKCS8EncodedKeySpec.class)) {
return (T) (new PKCS8EncodedKeySpec(key.getEncoded()));
}
throw new InvalidKeySpecException("'keySpec' is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec");
}
if (key instanceof DSAPublicKey) {
DSAPublicKey publicKey = (DSAPublicKey) key;
if (keySpec.equals(DSAPublicKeySpec.class)) {
y = publicKey.getY();
DSAParams params = publicKey.getParams();
p = params.getP();
q = params.getQ();
g = params.getG();
return (T) (new DSAPublicKeySpec(y, p, q, g));
}
if (keySpec.equals(X509EncodedKeySpec.class)) {
return (T) (new X509EncodedKeySpec(key.getEncoded()));
}
throw new InvalidKeySpecException("'keySpec' is neither DSAPublicKeySpec nor X509EncodedKeySpec");
}
}
throw new InvalidKeySpecException("'key' is neither DSAPublicKey nor DSAPrivateKey");
}
use of java.security.spec.X509EncodedKeySpec in project platform_frameworks_base by android.
the class PackageParser method parsePublicKey.
public static final PublicKey parsePublicKey(final String encodedPublicKey) {
if (encodedPublicKey == null) {
Slog.w(TAG, "Could not parse null public key");
return null;
}
EncodedKeySpec keySpec;
try {
final byte[] encoded = Base64.decode(encodedPublicKey, Base64.DEFAULT);
keySpec = new X509EncodedKeySpec(encoded);
} catch (IllegalArgumentException e) {
Slog.w(TAG, "Could not parse verifier public key; invalid Base64");
return null;
}
/* First try the key as an RSA key. */
try {
final KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
Slog.wtf(TAG, "Could not parse public key: RSA KeyFactory not included in build");
} catch (InvalidKeySpecException e) {
// Not a RSA public key.
}
/* Now try it as a ECDSA key. */
try {
final KeyFactory keyFactory = KeyFactory.getInstance("EC");
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
Slog.wtf(TAG, "Could not parse public key: EC KeyFactory not included in build");
} catch (InvalidKeySpecException e) {
// Not a ECDSA public key.
}
/* Now try it as a DSA key. */
try {
final KeyFactory keyFactory = KeyFactory.getInstance("DSA");
return keyFactory.generatePublic(keySpec);
} catch (NoSuchAlgorithmException e) {
Slog.wtf(TAG, "Could not parse public key: DSA KeyFactory not included in build");
} catch (InvalidKeySpecException e) {
// Not a DSA public key.
}
/* Not a supported key type */
return null;
}
Aggregations