use of org.mozilla.jss.asn1.NULL in project jss by dogtagpki.
the class KeyFactorySpi1_2 method engineGeneratePublic.
@Override
protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException {
if (keySpec instanceof RSAPublicKeySpec) {
RSAPublicKeySpec spec = (RSAPublicKeySpec) keySpec;
// Generate a DER RSA public key
SEQUENCE seq = new SEQUENCE();
seq.addElement(new INTEGER(spec.getModulus()));
seq.addElement(new INTEGER(spec.getPublicExponent()));
return PK11PubKey.fromRaw(PrivateKey.RSA, ASN1Util.encode(seq));
} else if (keySpec instanceof DSAPublicKeySpec) {
// We need to import both the public value and the PQG parameters.
// The only way to get all that information in DER is to send
// a full SubjectPublicKeyInfo. So we encode all the information
// into an SPKI.
DSAPublicKeySpec spec = (DSAPublicKeySpec) keySpec;
SEQUENCE pqg = new SEQUENCE();
pqg.addElement(new INTEGER(spec.getP()));
pqg.addElement(new INTEGER(spec.getQ()));
pqg.addElement(new INTEGER(spec.getG()));
OBJECT_IDENTIFIER oid = null;
try {
oid = SignatureAlgorithm.DSASignature.toOID();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("No such algorithm: " + e.getMessage(), e);
}
AlgorithmIdentifier algID = new AlgorithmIdentifier(oid, pqg);
INTEGER publicValue = new INTEGER(spec.getY());
byte[] encodedPublicValue = ASN1Util.encode(publicValue);
SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(algID, new BIT_STRING(encodedPublicValue, 0));
return PK11PubKey.fromSPKI(ASN1Util.encode(spki));
//
// requires JAVA 1.5
//
// } else if( keySpec instanceof ECPublicKeySpec ) {
// // We need to import both the public value and the curve.
// // The only way to get all that information in DER is to send
// // a full SubjectPublicKeyInfo. So we encode all the information
// // into an SPKI.
//
// ECPublicKeySpec spec = (ECPublicKeySpec) keySpec;
// AlgorithmParameters algParams = getInstance("ECParameters");
//
// algParameters.init(spec.getECParameters());
// OBJECT_IDENTIFIER oid = null;
// try {
// oid = SignatureAlgorithm.ECSignature.toOID();
// } catch(NoSuchAlgorithmException ex ) {
// Assert.notReached("no such algorithm as DSA?");
// }
// AlgorithmIdentifier algID =
// new AlgorithmIdentifier(oid, ecParams.getParams() );
// INTEGER publicValueX = new INTEGER(spec.getW().getAffineX());
// INTEGER publicValueY = new INTEGER(spec.getW().getAffineY());
// byte[] encodedPublicValue;
// encodedPublicValue[0] = EC_UNCOMPRESSED_POINT;
// encodedPublicValue += spec.getW().getAffineX().toByteArray();
// encodedPublicValue += spec.getW().getAffineY().toByteArray();
//
// byte[] encodedPublicValue = ASN1Util.encode(publicValue);
// SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(
// algID, new BIT_STRING(encodedPublicValue, 0) );
//
// return PK11PubKey.fromSPKI( ASN1Util.encode(spki) );
//
// use the following for EC keys in 1.4.2
} else if (keySpec instanceof X509EncodedKeySpec) {
//
// SubjectPublicKeyInfo
//
X509EncodedKeySpec spec = (X509EncodedKeySpec) keySpec;
return PK11PubKey.fromSPKI(spec.getEncoded());
}
throw new InvalidKeySpecException("Unsupported KeySpec type: " + keySpec.getClass().getName());
}
use of org.mozilla.jss.asn1.NULL in project jss by dogtagpki.
the class Name method AVAToString.
private String AVAToString(AVA ava) throws InvalidBERException {
OBJECT_IDENTIFIER oid = ava.getOID();
String type = typeToString(oid);
if (type == null) {
return "";
} else {
return type + "=" + ava.getValue().decodeWith(DirectoryString.getTemplate());
}
}
use of org.mozilla.jss.asn1.NULL in project jss by dogtagpki.
the class PKCS12Util method create_EPKI_with_PBE_PKCS5_PBES2.
public ASN1Value create_EPKI_with_PBE_PKCS5_PBES2(CryptoToken token, PrivateKey privateKey, Password password) throws Exception {
CryptoStore store = token.getCryptoStore();
byte[] bytes = store.getEncryptedPrivateKeyInfo(// password converter
null, password, // alg. To avoid mismatch, use AES_256_CBC.
EncryptionAlgorithm.AES_256_CBC, // iterations (default)
0, privateKey);
return new ANY(bytes);
}
use of org.mozilla.jss.asn1.NULL in project jss by dogtagpki.
the class PKCS12Util method addKeyBag.
/**
* Add a private key to the PKCS #12 object.
*
* The PKCS12KeyInfo object received comes about in two
* different scenarios:
*
* - The private key could be in encrypted byte[] form (e.g.
* when we have merely loaded a PKCS #12 file for inspection
* or e.g. to delete a certificate and its associated key).
* In this case we simply re-use this encrypted private key
* info byte[].
*
* - The private key could be a be an NSS PrivateKey handle. In
* this case we must export the PrivateKey from the token to
* obtain the EncryptedPrivateKeyInfo.
*
* The common final step is to add the encrypted private key
* data to a "Shrouded Key Bag" to the PKCS #12 object.
* Unencrypted key material is never seen.
*/
public void addKeyBag(PKCS12KeyInfo keyInfo, Password password, SEQUENCE encSafeContents) throws Exception {
byte[] keyID = keyInfo.getID();
logger.debug(" - Key ID: " + Utils.HexEncode(keyID));
ASN1Value content;
byte[] epkiBytes = keyInfo.getEncryptedPrivateKeyInfoBytes();
if (epkiBytes != null) {
// private key already encrypted
content = new ANY(epkiBytes);
} else {
PrivateKey privateKey = keyInfo.getPrivateKey();
if (privateKey == null) {
throw new Exception("Missing private key for " + keyInfo.getFriendlyName());
}
CryptoToken token = CryptoManager.getInstance().getInternalKeyStorageToken();
if (keyEncryption == PBEAlgorithm.PBE_SHA1_DES3_CBC) {
content = create_EPKI_with_PBE_SHA1_DES3_CBC(token, privateKey, password);
} else if (keyEncryption == PBEAlgorithm.PBE_PKCS5_PBES2) {
content = create_EPKI_with_PBE_PKCS5_PBES2(token, privateKey, password);
} else {
throw new Exception("Unsupported key encryption: " + keyEncryption);
}
}
SET keyAttrs = createKeyBagAttrs(keyInfo);
SafeBag safeBag = new SafeBag(SafeBag.PKCS8_SHROUDED_KEY_BAG, content, keyAttrs);
encSafeContents.addElement(safeBag);
}
use of org.mozilla.jss.asn1.NULL in project jss by dogtagpki.
the class PKCS12Util method storeCertIntoNSS.
/**
* Store a certificate (and key, if present) in NSSDB.
*/
public void storeCertIntoNSS(PKCS12 pkcs12, Password password, PKCS12CertInfo certInfo, boolean overwrite) throws Exception {
CryptoManager cm = CryptoManager.getInstance();
CryptoToken ct = cm.getInternalKeyStorageToken();
CryptoStore store = ct.getCryptoStore();
String nickname = certInfo.getFriendlyName();
for (X509Certificate cert : cm.findCertsByNickname(nickname)) {
if (!overwrite) {
return;
}
store.deleteCert(cert);
}
X509CertImpl certImpl = certInfo.getCert();
X509Certificate cert;
byte[] keyID = certInfo.getKeyID();
if (keyID != null) {
// cert has key
logger.debug("Importing private key for " + certInfo.getFriendlyName());
PKCS12KeyInfo keyInfo = pkcs12.getKeyInfoByID(keyID);
importKey(pkcs12, password, certInfo.getFriendlyName(), keyInfo);
logger.debug("Importing user certificate " + certInfo.getFriendlyName());
cert = cm.importUserCACertPackage(certImpl.getEncoded(), certInfo.getFriendlyName());
} else {
// cert has no key
logger.debug("Importing CA certificate " + certInfo.getFriendlyName());
// Note: JSS does not preserve CA certificate nickname
cert = cm.importCACertPackage(certImpl.getEncoded());
}
String trustFlags = certInfo.getTrustFlags();
if (trustFlags != null && trustFlagsEnabled) {
PK11Cert pk11Cert = (PK11Cert) cert;
pk11Cert.setTrustFlags(trustFlags);
}
}
Aggregations