use of org.mozilla.jss.crypto.Signature in project jss by dogtagpki.
the class CertificationRequest method verify.
/**
* Verifies the signature on this CertificationRequest, using the given public
* key and CryptoToken. Does not indicate the CertificationRequest is valid at
* any specific time.
*/
public void verify(PublicKey key, CryptoToken token) throws NoSuchAlgorithmException, CertificateException, TokenException, SignatureException, InvalidKeyException {
Signature sig = token.getSignatureContext(SignatureAlgorithm.fromOID(algId.getOID()));
sig.initVerify(key);
sig.update(infoEncoding);
if (!sig.verify(signature)) {
throw new CertificateException("Signature is invalid");
}
}
use of org.mozilla.jss.crypto.Signature in project jss by dogtagpki.
the class SigTest method main.
public static void main(String[] args) throws Exception {
CryptoToken token;
CryptoManager manager;
byte[] data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
byte[] signature;
Signature signer;
Signature signerPSS;
PublicKey pubk;
KeyPairGenerator kpgen;
KeyPair keyPair;
if (args.length < 2 || args.length > 3) {
usage();
System.exit(1);
}
manager = CryptoManager.getInstance();
manager.setPasswordCallback(new FilePasswordCallback(args[1]));
/* Print out list of available tokens */
Enumeration<CryptoToken> en = manager.getAllTokens();
System.out.println("Available tokens:");
while (en.hasMoreElements()) {
PK11Token p = (PK11Token) en.nextElement();
System.out.println(" token : " + p.getName());
}
if (args.length >= 3) {
token = manager.getTokenByName(args[2]);
} else {
// get default internal key storage token
token = manager.getInternalKeyStorageToken();
}
// Generate an RSA keypair
kpgen = token.getKeyPairGenerator(KeyPairAlgorithm.RSA);
kpgen.initialize(Policy.RSA_MINIMUM_KEY_SIZE);
KeyPairGeneratorSpi.Usage[] usages = { KeyPairGeneratorSpi.Usage.SIGN, KeyPairGeneratorSpi.Usage.VERIFY };
KeyPairGeneratorSpi.Usage[] usages_mask = { KeyPairGeneratorSpi.Usage.SIGN, KeyPairGeneratorSpi.Usage.VERIFY };
kpgen.setKeyPairUsages(usages, usages_mask);
keyPair = kpgen.genKeyPair();
// RSA SHA256
signer = token.getSignatureContext(SignatureAlgorithm.RSASignatureWithSHA256Digest);
System.out.println("Created a signing context");
signer.initSign((org.mozilla.jss.crypto.PrivateKey) keyPair.getPrivate());
System.out.println("initialized the signing operation");
signer.update(data);
System.out.println("updated signature with data");
signature = signer.sign();
System.out.println("Successfully signed!");
signer.initVerify(keyPair.getPublic());
System.out.println("initialized verification");
signer.update(data);
System.out.println("updated verification with data");
if (signer.verify(signature)) {
System.out.println("Signature Verified Successfully!");
} else {
throw new Exception("ERROR: Signature failed to verify.");
}
signerPSS = token.getSignatureContext(SignatureAlgorithm.RSAPSSSignatureWithSHA256Digest);
signerPSS.initSign((org.mozilla.jss.crypto.PrivateKey) keyPair.getPrivate());
signerPSS.update(data);
signature = signerPSS.sign();
System.out.println("PSS Successfully signed!");
signerPSS.initVerify(keyPair.getPublic());
signerPSS.update(data);
System.out.println("updated verification with data");
if (signerPSS.verify(signature)) {
System.out.println("PSS Signature Verified Successfully!");
} else {
throw new Exception("ERROR: PSS Signature failed to verify.");
}
System.out.println("SigTest passed.");
}
use of org.mozilla.jss.crypto.Signature in project jss by dogtagpki.
the class SignerInfo method verifyWithSignedAttributes.
/**
* Verifies a SignerInfo with signed attributes. If signed
* attributes are present, then two particular attributes must
* be present: <ul>
* <li>PKCS #9 Content-Type, the type of content that is being signed.
* This must match the contentType parameter.
* <li>PKCS #9 Message-Digest, the digest of the content that is being
* signed. This must match the messageDigest parameter.
* </ul>
* After these two attributes are verified to be both present and correct,
* the encryptedDigest field of the SignerInfo is verified to be the
* signature of the contents octets of the DER encoding of the
* signedAttributes field.
*/
private void verifyWithSignedAttributes(byte[] messageDigest, OBJECT_IDENTIFIER contentType, PublicKey pubkey) throws NotInitializedException, NoSuchAlgorithmException, InvalidKeyException, TokenException, SignatureException {
int numAttrib = signedAttributes.size();
if (numAttrib < 2) {
throw new SignatureException("At least two signed attributes must be present:" + " content-type and message-digest");
}
// go through the signed attributes, verifying the
// interesting ones
boolean foundContentType = false;
boolean foundMessageDigest = false;
for (int i = 0; i < numAttrib; i++) {
if (!(signedAttributes.elementAt(i) instanceof Attribute)) {
throw new SignatureException("Element of signedAttributes is not an Attribute");
}
Attribute attrib = (Attribute) signedAttributes.elementAt(i);
if (attrib.getType().equals(CONTENT_TYPE)) {
// content-type. Compare with what was passed in.
SET vals = attrib.getValues();
if (vals.size() != 1) {
throw new SignatureException("Content-Type attribute " + " does not have exactly one value");
}
ASN1Value val = vals.elementAt(0);
OBJECT_IDENTIFIER ctype;
try {
if (val instanceof OBJECT_IDENTIFIER) {
ctype = (OBJECT_IDENTIFIER) val;
} else if (val instanceof ANY) {
ctype = (OBJECT_IDENTIFIER) ((ANY) val).decodeWith(OBJECT_IDENTIFIER.getTemplate());
} else {
// what the heck is it? not what it's supposed to be
throw new InvalidBERException("Content-Type signed attribute has unexpected" + " content type");
}
} catch (InvalidBERException e) {
throw new SignatureException("Content-Type signed attribute does not have " + "OBJECT IDENTIFIER value");
}
// contentType parameter
if (!ctype.equals(contentType)) {
throw new SignatureException("Content-type in signed attributes does not " + "match content-type being verified");
}
// content type is A-OK
foundContentType = true;
} else if (attrib.getType().equals(MESSAGE_DIGEST)) {
SET vals = attrib.getValues();
if (vals.size() != 1) {
throw new SignatureException("Message-digest attribute does not have" + " exactly one value");
}
ASN1Value val = vals.elementAt(0);
byte[] mdigest;
try {
if (val instanceof OCTET_STRING) {
mdigest = ((OCTET_STRING) val).toByteArray();
} else if (val instanceof ANY) {
OCTET_STRING os;
os = (OCTET_STRING) ((ANY) val).decodeWith(OCTET_STRING.getTemplate());
mdigest = os.toByteArray();
} else {
// what the heck is it? not what it's supposed to be
throw new InvalidBERException("Content-Type signed attribute has unexpected" + " content type");
}
} catch (InvalidBERException e) {
throw new SignatureException("Message-digest attribute does not" + " have OCTET STRING value");
}
// message digest being verified
if (!byteArraysAreSame(mdigest, messageDigest)) {
throw new SignatureException("Message-digest attribute does not" + " match message digest being verified");
}
// message digest is A-OK
foundMessageDigest = true;
}
// we don't care about other attributes
}
if (!foundContentType) {
throw new SignatureException("Signed attributes does not contain" + " PKCS #9 content-type attribute");
}
if (!foundMessageDigest) {
throw new SignatureException("Signed attributes does not contain" + " PKCS #9 message-digest attribute");
}
SignatureAlgorithm sigAlg = SignatureAlgorithm.fromOID(digestEncryptionAlgorithm.getOID());
// All the signed attributes are present and correct.
// Now verify the signature.
CryptoToken token = CryptoManager.getInstance().getInternalCryptoToken();
Signature sig;
// verify the contents octets of the DER encoded signed attribs
byte[] encoding = ASN1Util.encode(signedAttributes);
byte[] toBeVerified;
if (sigAlg.getRawAlg() == SignatureAlgorithm.RSASignature) {
// create DigestInfo structure
SEQUENCE digestInfo = createDigestInfo(encoding, true);
toBeVerified = ASN1Util.encode(digestInfo);
sig = token.getSignatureContext(SignatureAlgorithm.RSASignature);
} else {
toBeVerified = encoding;
sig = token.getSignatureContext(sigAlg);
}
sig.initVerify(pubkey);
sig.update(toBeVerified);
if (!sig.verify(encryptedDigest.toByteArray())) {
// signature is invalid
throw new SignatureException("encryptedDigest was not the correct" + " signature of the contents octets of the DER-encoded" + " signed attributes");
}
// SUCCESSFULLY VERIFIED
}
Aggregations