use of org.mozilla.jss.asn1.NULL 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.NULL in project jss by dogtagpki.
the class JSSCipherSpi method generateAlgParams.
private AlgorithmParameterSpec generateAlgParams(Algorithm alg, int blockSize) throws InvalidKeyException {
Class<?>[] paramClasses = alg.getParameterClasses();
AlgorithmParameterSpec algParSpec = null;
if (paramClasses == null) {
// no parameters are needed
return null;
}
// generate an IV
byte[] iv = new byte[blockSize];
try {
SecureRandom random = SecureRandom.getInstance("pkcs11prng", "Mozilla-JSS");
random.nextBytes(iv);
} catch (Exception e) {
throw new RuntimeException(e);
}
for (int i = 0; i < paramClasses.length; i++) {
if (paramClasses[i].equals(javax.crypto.spec.IvParameterSpec.class)) {
algParSpec = new javax.crypto.spec.IvParameterSpec(iv);
break;
} else if (paramClasses[i].equals(RC2ParameterSpec.class)) {
algParSpec = new RC2ParameterSpec(keyStrength, iv);
break;
}
}
return algParSpec;
}
use of org.mozilla.jss.asn1.NULL in project jss by dogtagpki.
the class CertReqMsg method encode.
/**
* Encodes this <i>CertReqMsg</i> to the given OutputStream using
* DER encoding, with the given implicit tag.
*/
@Override
public void encode(Tag implicit, OutputStream ostream) throws IOException {
// Assert.notYetImplemented("CertReqMsg encoding");
SEQUENCE sequence = new SEQUENCE();
sequence.addElement(certReq);
if (pop != null)
sequence.addElement(pop);
if (regInfo != null)
sequence.addElement(regInfo);
sequence.encode(implicit, ostream);
}
use of org.mozilla.jss.asn1.NULL in project jss by dogtagpki.
the class CertReqMsg method verify.
public void verify(CryptoToken token) throws SignatureException, InvalidKeyFormatException, NoSuchAlgorithmException, org.mozilla.jss.NotInitializedException, TokenException, java.security.InvalidKeyException, IOException {
ProofOfPossession.Type type = pop.getType();
if (type == ProofOfPossession.SIGNATURE) {
POPOSigningKey sigkey = pop.getSignature();
AlgorithmIdentifier alg = sigkey.getAlgorithmIdentifier();
BIT_STRING sig_from = sigkey.getSignature();
ByteArrayOutputStream bo = new ByteArrayOutputStream();
certReq.encode(bo);
byte[] toBeVerified = bo.toByteArray();
PublicKey pubkey = null;
CertTemplate ct = certReq.getCertTemplate();
if (ct.hasPublicKey()) {
SubjectPublicKeyInfo spi = ct.getPublicKey();
pubkey = spi.toPublicKey();
}
SignatureAlgorithm sigAlg = SignatureAlgorithm.fromOID(alg.getOID());
Signature sig = token.getSignatureContext(sigAlg);
sig.initVerify(pubkey);
sig.update(toBeVerified);
if (sig.verify(sig_from.getBits())) {
// success
return;
} else {
throw new SignatureException("Signed request information does not " + "match signature in POP");
}
} else if (type == ProofOfPossession.KEY_ENCIPHERMENT) {
POPOPrivKey keyEnc = pop.getKeyEncipherment();
POPOPrivKey.Type ptype = keyEnc.getType();
if (ptype == POPOPrivKey.THIS_MESSAGE) {
// BIT_STRING thisMessage = keyEnc.getThisMessage();
// This should be the same as from the archive control
// It's verified by DRM.
} else if (ptype == POPOPrivKey.SUBSEQUENT_MESSAGE) {
new ChallengeResponseException("requested");
}
}
}
use of org.mozilla.jss.asn1.NULL in project jss by dogtagpki.
the class CertTemplate method encode.
@Override
public void encode(Tag t, OutputStream ostream) throws IOException {
SEQUENCE seq = new SEQUENCE();
seq.addElement(Tag.get(0), version);
seq.addElement(Tag.get(1), serialNumber);
seq.addElement(Tag.get(2), signingAlg);
if (issuer != null) {
// issuer is a CHOICE, so it must be EXPLICITly tagged
seq.addElement(new EXPLICIT(Tag.get(3), issuer));
}
if (notBefore != null || notAfter != null) {
SEQUENCE optionalVal = new SEQUENCE();
// notBefore & notAfter are CHOICES, so must be EXPLICITly tagged
if (notBefore != null) {
optionalVal.addElement(new EXPLICIT(Tag.get(0), dateToASN1(notBefore)));
}
if (notAfter != null) {
optionalVal.addElement(new EXPLICIT(Tag.get(1), dateToASN1(notAfter)));
}
seq.addElement(Tag.get(4), optionalVal);
}
if (subject != null) {
// subject is a CHOICE, so it must be EXPLICITly tagged
seq.addElement(new EXPLICIT(Tag.get(5), subject));
}
seq.addElement(Tag.get(6), publicKey);
seq.addElement(Tag.get(7), issuerUID);
seq.addElement(Tag.get(8), subjectUID);
seq.addElement(Tag.get(9), extensions);
seq.encode(t, ostream);
}
Aggregations