org.bouncycastle.asn1.DERObject Java Examples

The following examples show how to use org.bouncycastle.asn1.DERObject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: X509Util.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Convert types returned by Bouncy Castle X509ExtensionUtil.getSubjectAlternativeNames(X509Certificate) to be
 * consistent with what is documented for: java.security.cert.X509Certificate#getSubjectAlternativeNames.
 * 
 * @param nameType the alt name type
 * @param nameValue the alt name value
 * @return converted representation of name value, based on type
 */
private static Object convertAltNameType(Integer nameType, Object nameValue) {
    Logger log = getLogger();
    if (DIRECTORY_ALT_NAME.equals(nameType) || DNS_ALT_NAME.equals(nameType) || RFC822_ALT_NAME.equals(nameType)
            || URI_ALT_NAME.equals(nameType) || REGISTERED_ID_ALT_NAME.equals(nameType)) {

        // these are just strings in the appropriate format already, return as-is
        return nameValue;
    }

    if (IP_ADDRESS_ALT_NAME.equals(nameType)) {
        // this is a byte[], IP addr in network byte order
        return IPAddressHelper.addressToString((byte[]) nameValue);
    }

    if (EDI_PARTY_ALT_NAME.equals(nameType) || X400ADDRESS_ALT_NAME.equals(nameType)
            || OTHER_ALT_NAME.equals(nameType)) {

        // these have no defined representation, just return a DER-encoded byte[]
        return ((DERObject) nameValue).getDEREncoded();
    }

    log.warn("Encountered unknown alt name type '{}', adding as-is", nameType);
    return nameValue;
}
 
Example #2
Source File: BouncyCastleOpenSSLKey.java    From swift-k with Apache License 2.0 5 votes vote down vote up
protected PrivateKey getKey(String alg, byte [] data) 
throws GeneralSecurityException {
if (alg.equals("RSA")) {
    try {
	ByteArrayInputStream bis = new ByteArrayInputStream(data);
	DERInputStream derin = new DERInputStream(bis);
	DERObject keyInfo = derin.readObject();
	
	DERObjectIdentifier rsa_oid = PKCSObjectIdentifiers.rsaEncryption;    	   
	AlgorithmIdentifier rsa = new AlgorithmIdentifier(rsa_oid);
	PrivateKeyInfo pkeyinfo = new PrivateKeyInfo(rsa, keyInfo);
	DERObject derkey = pkeyinfo.getDERObject();		
	
	byte[] keyData = BouncyCastleUtil.toByteArray(derkey);

	// The DER object needs to be mangled to 
	// create a proper ProvateKeyInfo object 
	PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(keyData);
	KeyFactory kfac = KeyFactory.getInstance("RSA");
	
	return kfac.generatePrivate(spec);
    } catch (IOException e) {
	// that should never happen
	return null;
    }
    
} else {
    return null;
}
   }
 
Example #3
Source File: AutoCA.java    From swift-k with Apache License 2.0 4 votes vote down vote up
private DEREncodable getAuthorityKeyIdentifier(PublicKey caPub) throws IOException {
    DERObject derKey = new ASN1InputStream(caPub.getEncoded()).readObject();
    return new AuthorityKeyIdentifier(new SubjectPublicKeyInfo((ASN1Sequence) derKey));
}
 
Example #4
Source File: AutoCA.java    From swift-k with Apache License 2.0 4 votes vote down vote up
private DEREncodable getSubjectKeyInfo(PublicKey userPub) throws IOException {
    // convert key to bouncy castle format and get subject key identifier
    DERObject derKey = new ASN1InputStream(userPub.getEncoded()).readObject();
    return new SubjectKeyIdentifier(new SubjectPublicKeyInfo((ASN1Sequence) derKey));
}