use of org.mozilla.jss.asn1.BIT_STRING 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.BIT_STRING 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.BIT_STRING 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.BIT_STRING in project jss by dogtagpki.
the class JSSCipherSpi method engineGetKeySize.
@Override
public int engineGetKeySize(Key key) throws InvalidKeyException {
if (key instanceof PK11PrivKey) {
return ((PK11PrivKey) key).getStrength();
} else if (key instanceof PK11PubKey) {
try {
byte[] encoded = ((PK11PubKey) key).getEncoded();
SubjectPublicKeyInfo.Template spkiTemp = new SubjectPublicKeyInfo.Template();
SubjectPublicKeyInfo spki = (SubjectPublicKeyInfo) ASN1Util.decode(spkiTemp, encoded);
BIT_STRING pk = spki.getSubjectPublicKey();
return pk.getBits().length - pk.getPadCount();
} catch (InvalidBERException e) {
throw new InvalidKeyException("Exception while decoding " + "public key: " + e.getMessage());
}
} else if (key instanceof SecretKeyFacade) {
SymmetricKey symkey = ((SecretKeyFacade) key).key;
return symkey.getLength();
} else {
key = importKey(key);
SymmetricKey symkey = ((SecretKeyFacade) key).key;
return symkey.getLength();
}
}
use of org.mozilla.jss.asn1.BIT_STRING in project jss by dogtagpki.
the class PKIStatusInfo method encode.
@Override
public void encode(Tag implicitTag, OutputStream ostream) throws IOException {
SEQUENCE seq = new SEQUENCE();
seq.addElement(status);
if (statusString.size() > 0) {
seq.addElement(statusString);
}
if (hasFailInfo) {
// convert failInfo to BIT_STRING
byte[] bytes = new byte[2];
bytes[0] = (byte) ((failInfo & 0xff000000) >>> 24);
bytes[1] = (byte) ((failInfo & 0x00ff0000) >>> 16);
// 7 unused bits
int padCount = 7;
BIT_STRING bs = new BIT_STRING(bytes, padCount);
bs.setRemoveTrailingZeroes(true);
seq.addElement(bs);
}
seq.encode(implicitTag, ostream);
}
Aggregations