use of org.mozilla.jss.asn1.INTEGER in project jss by dogtagpki.
the class SSLClientAuth method makeCert.
/**
* Method that generates a certificate for given credential
*
* @param issuerName
* @param subjectName
* @param serialNumber
* @param privKey
* @param pubKey
* @param rand
* @param extensions
* @throws java.lang.Exception
* @return Certificate
*/
public static Certificate makeCert(String issuerName, String subjectName, int serialNumber, PrivateKey privKey, PublicKey pubKey, int rand, SEQUENCE extensions) throws Exception {
AlgorithmIdentifier sigAlgID = new AlgorithmIdentifier(sigAlg.toOID());
Name issuer = new Name();
issuer.addCountryName("US");
issuer.addOrganizationName("Mozilla");
issuer.addOrganizationalUnitName("JSS Testing" + rand);
issuer.addCommonName(issuerName);
Name subject = new Name();
subject.addCountryName("US");
subject.addOrganizationName("Mozilla");
subject.addOrganizationalUnitName("JSS Testing" + rand);
subject.addCommonName(subjectName);
Calendar cal = Calendar.getInstance();
Date notBefore = cal.getTime();
cal.add(Calendar.YEAR, 1);
Date notAfter = cal.getTime();
SubjectPublicKeyInfo.Template spkiTemp = new SubjectPublicKeyInfo.Template();
SubjectPublicKeyInfo spki = (SubjectPublicKeyInfo) ASN1Util.decode(spkiTemp, pubKey.getEncoded());
CertificateInfo info = new CertificateInfo(CertificateInfo.v3, new INTEGER(serialNumber), sigAlgID, issuer, notBefore, notAfter, subject, spki);
if (extensions != null) {
info.setExtensions(extensions);
}
return new Certificate(info, privKey, sigAlg);
}
use of org.mozilla.jss.asn1.INTEGER in project jss by dogtagpki.
the class GenerateTestCert method makeCert.
/**
* Method that generates a certificate for given credential
*
* @param issuerName
* @param subjectName
* @param serialNumber
* @param privKey
* @param pubKey
* @param rand
* @param extensions
* @throws java.lang.Exception
* @return
*/
private Certificate makeCert(String issuerName, String subjectName, int serialNumber, PrivateKey privKey, PublicKey pubKey, int rand, SEQUENCE extensions) throws Exception {
AlgorithmIdentifier sigAlgID = new AlgorithmIdentifier(sigAlg.toOID());
Name issuer = new Name();
issuer.addCountryName("US");
issuer.addOrganizationName("Mozilla");
issuer.addOrganizationalUnitName("JSS Testing" + rand);
issuer.addCommonName(issuerName);
Name subject = new Name();
subject.addCountryName("US");
subject.addOrganizationName("Mozilla");
subject.addOrganizationalUnitName("JSS Testing" + rand);
subject.addCommonName(subjectName);
Calendar cal = Calendar.getInstance();
Date notBefore = cal.getTime();
cal.add(Calendar.YEAR, 1);
Date notAfter = cal.getTime();
SubjectPublicKeyInfo.Template spkiTemp = new SubjectPublicKeyInfo.Template();
SubjectPublicKeyInfo spki = (SubjectPublicKeyInfo) ASN1Util.decode(spkiTemp, pubKey.getEncoded());
CertificateInfo info = new CertificateInfo(CertificateInfo.v3, new INTEGER(serialNumber), sigAlgID, issuer, notBefore, notAfter, subject, spki);
if (extensions != null) {
info.setExtensions(extensions);
}
return new Certificate(info, privKey, sigAlg);
}
use of org.mozilla.jss.asn1.INTEGER in project jss by dogtagpki.
the class CertTemplate method main.
public static void main(String[] args) {
try {
CertTemplate ct = new CertTemplate();
Name name;
ct.setVersion(new INTEGER(5));
ct.setSerialNumber(new INTEGER(13112));
name = new Name();
name.addCommonName("You");
name.addStateOrProvinceName("California");
ct.setIssuer(name);
ct.setNotBefore(new Date());
name = new Name();
name.addCommonName("Me");
name.addCountryName("US");
ct.setSubject(name);
ct.setIssuerUID(new BIT_STRING(new byte[] { 0x00, 0x01 }, 0));
System.out.println("Constructed CertTemplate:");
byte[] encoded = ASN1Util.encode(ct);
try (FileOutputStream fos = new FileOutputStream("certTemplate")) {
fos.write(encoded);
}
ct.print(System.out, 0);
CertTemplate newCt = (CertTemplate) ASN1Util.decode(CertTemplate.getTemplate(), encoded);
System.out.println("\nDecoded CertTemplate:");
newCt.print(System.out, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.mozilla.jss.asn1.INTEGER 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.INTEGER in project jss by dogtagpki.
the class LraPopWitness method addBodyPartId.
/**
* Adds a BodyPartID to the bodyIds SEQUENCE.
*/
public void addBodyPartId(int id) {
INTEGER id1 = new INTEGER(id);
assert (id1.compareTo(BODYIDMAX) <= 0);
bodyIds.addElement(id1);
}
Aggregations