use of javax.security.auth.x500.X500Principal in project jdk8u_jdk by JetBrains.
the class KeywordMap method main.
public static void main(String[] args) throws Exception {
X500Principal p = null;
Map<String, String> m = null;
// test null keywordMap
try {
p = new X500Principal("CN=user", null);
throw new Exception("expected NullPointerException for null keywordMap");
} catch (NullPointerException npe) {
}
// test improperly specified OID
m = Collections.singletonMap("FOO", "FOO");
try {
p = new X500Principal("FOO=user", m);
throw new Exception("expected IllegalArgumentException for bad OID");
} catch (IllegalArgumentException iae) {
}
// ignore improperly specified keyword
m = Collections.singletonMap("?*&", "FOO");
p = new X500Principal("CN=user", m);
// throw exception if no mapping for keyword
m = Collections.singletonMap("BAR", "1.2.3");
try {
p = new X500Principal("FOO=user", m);
throw new Exception("expected IllegalArgumentExc for keyword with no mapping");
} catch (IllegalArgumentException iae) {
}
// don't match keyword in lower-case
m = Collections.singletonMap("foo", "1.2.3");
try {
p = new X500Principal("FOO=user", m);
throw new Exception("expected IllegalArgumentExc for wrong-case keyword mapping");
} catch (IllegalArgumentException iae) {
}
// allow duplicate OID mappings
m = new HashMap<String, String>();
m.put("FOO", "1.2.3");
m.put("BAR", "1.2.3");
p = new X500Principal("BAR=user", m);
// override builtin keywords
m = Collections.singletonMap("CN", "1.2.3");
p = new X500Principal("CN=user", m);
if (!p.getName().startsWith("1.2.3")) {
throw new Exception("mapping did not override builtin keyword");
}
// override builtin OIDs
m = Collections.singletonMap("FOO", "2.5.4.3");
p = new X500Principal("FOO=sean", m);
if (!p.getName().startsWith("CN")) {
throw new Exception("mapping did not override builtin OID");
}
}
use of javax.security.auth.x500.X500Principal in project jdk8u_jdk by JetBrains.
the class DerIsConstructor method main.
public static void main(String[] args) {
try {
// create 2 different X500Principals
X500Principal p = new X500Principal("o=sun, cn=duke");
X500Principal p2 = new X500Principal("o=sun, cn=dukette");
// get the encoded bytes for the 2 principals
byte[] encoded = p.getEncoded();
byte[] encoded2 = p2.getEncoded();
// create a ByteArrayInputStream with the
// encodings from the 2 principals
byte[] all = new byte[encoded.length + encoded2.length];
System.arraycopy(encoded, 0, all, 0, encoded.length);
System.arraycopy(encoded2, 0, all, encoded.length, encoded2.length);
ByteArrayInputStream bais = new ByteArrayInputStream(all);
// create 2 new X500Principals from the ByteArrayInputStream
X500Principal pp = new X500Principal(bais);
X500Principal pp2 = new X500Principal(bais);
// sanity check the 2 new principals
if (p.equals(pp) && p2.equals(pp2) && !pp.equals(pp2)) {
System.out.println("Test 1 passed");
} else {
throw new SecurityException("Test 1 failed");
}
// corrupt the ByteArrayInputStream and see if the
// mark/reset worked
byte[] all2 = new byte[all.length];
System.arraycopy(all, 0, all2, 0, all.length);
all2[encoded.length + 2] = (byte) -1;
bais = new ByteArrayInputStream(all2);
// this should work
X500Principal ppp = new X500Principal(bais);
// this should throw an IOException due to stream corruption
int origAvailable = bais.available();
try {
X500Principal ppp2 = new X500Principal(bais);
throw new SecurityException("Test 2 (part a) failed");
} catch (IllegalArgumentException iae) {
if (bais.available() == origAvailable) {
System.out.println("Test 2 passed");
} else {
throw new SecurityException("Test 2 (part b) failed");
}
}
} catch (Exception e) {
e.printStackTrace();
throw new SecurityException(e.getMessage());
}
}
use of javax.security.auth.x500.X500Principal in project symmetric-ds by JumpMind.
the class BouncyCastleSecurityService method generateV1Certificate.
public X509Certificate generateV1Certificate(String host, KeyPair pair) throws Exception {
host = host == null ? AppUtils.getHostName() : host;
String certString = String.format("CN=%s, OU=SymmetricDS, O=JumpMind, L=Unknown, ST=Unknown, C=Unknown", host);
log.info("Installing a default SSL certificate: {}", certString);
X509V1CertificateGenerator certGen = new X509V1CertificateGenerator();
certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
certGen.setIssuerDN(new X500Principal(certString));
certGen.setNotBefore(new Date(System.currentTimeMillis() - 86400000));
certGen.setNotAfter(new Date(System.currentTimeMillis() + 788400000000l));
certGen.setSubjectDN(new X500Principal(certString));
certGen.setPublicKey(pair.getPublic());
certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
return certGen.generate(pair.getPrivate(), "BC");
}
use of javax.security.auth.x500.X500Principal in project mobile-center-sdk-android by Microsoft.
the class CryptoRsaHandler method generateKey.
/*
* We don't run this code prior to Android 4.4 hence no 4.3 secure random problem.
*/
@Override
@SuppressWarnings("deprecation")
@SuppressLint({ "InlinedApi", "TrulyRandom" })
public void generateKey(CryptoUtils.ICryptoFactory cryptoFactory, String alias, Context context) throws Exception {
Calendar writeExpiry = Calendar.getInstance();
writeExpiry.add(Calendar.YEAR, ENCRYPT_KEY_LIFETIME_IN_YEARS);
KeyPairGenerator generator = KeyPairGenerator.getInstance(KeyProperties.KEY_ALGORITHM_RSA, ANDROID_KEY_STORE);
generator.initialize(new KeyPairGeneratorSpec.Builder(context).setAlias(alias).setSubject(new X500Principal("CN=" + alias)).setStartDate(new Date()).setEndDate(writeExpiry.getTime()).setSerialNumber(BigInteger.TEN).setKeySize(RSA_KEY_SIZE).build());
generator.generateKeyPair();
}
use of javax.security.auth.x500.X500Principal in project android_frameworks_base by ResurrectionRemix.
the class WiFiKeyManager method enableClientAuth.
public void enableClientAuth(List<String> issuerNames) throws GeneralSecurityException, IOException {
Set<X500Principal> acceptedIssuers = new HashSet<>();
for (String issuerName : issuerNames) {
acceptedIssuers.add(new X500Principal(issuerName));
}
Enumeration<String> aliases = mKeyStore.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
Certificate cert = mKeyStore.getCertificate(alias);
if ((cert instanceof X509Certificate) && mKeyStore.getKey(alias, null) != null) {
X509Certificate x509Certificate = (X509Certificate) cert;
X500Principal issuer = x509Certificate.getIssuerX500Principal();
if (acceptedIssuers.contains(issuer)) {
mAliases.put(issuer, new String[] { alias, cert.getPublicKey().getAlgorithm() });
}
}
}
if (mAliases.isEmpty()) {
throw new IOException("No aliases match requested issuers: " + issuerNames);
}
}
Aggregations