use of org.gudy.bouncycastle.asn1.DERObjectIdentifier in project nhin-d by DirectProject.
the class CertGenerator method createCertFromCSR.
public static X509Certificate createCertFromCSR(PKCS10CertificationRequest certReq, CertCreateFields signerCert) throws Exception {
certReq.verify();
final CertificationRequestInfo reqInfo = certReq.getCertificationRequestInfo();
final X509V3CertificateGenerator v1CertGen = new X509V3CertificateGenerator();
final Calendar start = Calendar.getInstance();
final Calendar end = Calendar.getInstance();
end.add(Calendar.YEAR, 3);
v1CertGen.setSerialNumber(BigInteger.valueOf(generatePositiveRandom()));
// issuer is the parent cert
v1CertGen.setIssuerDN(signerCert.getSignerCert().getSubjectX500Principal());
v1CertGen.setNotBefore(start.getTime());
v1CertGen.setNotAfter(end.getTime());
v1CertGen.setSubjectDN(new X509Principal(reqInfo.getSubject().toString()));
v1CertGen.setPublicKey(certReq.getPublicKey());
v1CertGen.setSignatureAlgorithm("SHA256WithRSAEncryption");
final ASN1Set attributesAsn1Set = reqInfo.getAttributes();
X509Extensions certificateRequestExtensions = null;
for (int i = 0; i < attributesAsn1Set.size(); ++i) {
// There should be only only one attribute in the set. (that is, only
// the `Extension Request`, but loop through to find it properly)
final DEREncodable derEncodable = attributesAsn1Set.getObjectAt(i);
if (derEncodable instanceof DERSequence) {
final Attribute attribute = new Attribute((DERSequence) attributesAsn1Set.getObjectAt(i));
if (attribute.getAttrType().equals(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest)) {
// The `Extension Request` attribute is present.
final ASN1Set attributeValues = attribute.getAttrValues();
// Assume that it is the first value of the set.
if (attributeValues.size() >= 1) {
certificateRequestExtensions = new X509Extensions((ASN1Sequence) attributeValues.getObjectAt(0));
// No need to search any more.
//break;
}
}
}
}
@SuppressWarnings("unchecked") Enumeration<DERObjectIdentifier> oids = certificateRequestExtensions.oids();
while (oids.hasMoreElements()) {
DERObjectIdentifier oid = oids.nextElement();
X509Extension ex = certificateRequestExtensions.getExtension(oid);
v1CertGen.addExtension(oid, ex.isCritical(), X509Extension.convertValueToObject(ex));
}
return v1CertGen.generate((PrivateKey) signerCert.getSignerKey(), CryptoExtensions.getJCEProviderName());
}
use of org.gudy.bouncycastle.asn1.DERObjectIdentifier in project nhin-d by DirectProject.
the class ExtendedKeyUsageExtensionField method injectReferenceValue.
/**
* {@inheritDoc}
*/
@Override
public void injectReferenceValue(X509Certificate value) throws PolicyProcessException {
this.certificate = value;
final DERObject exValue = getExtensionValue(value);
if (exValue == null) {
if (isRequired())
throw new PolicyRequiredException("Extention " + getExtentionIdentifier().getDisplay() + " is marked as required by is not present.");
else {
final Collection<String> emptyList = Collections.emptyList();
this.policyValue = PolicyValueFactory.getInstance(emptyList);
return;
}
}
final ExtendedKeyUsage usages = ExtendedKeyUsage.getInstance(exValue);
@SuppressWarnings("unchecked") final Collection<DERObjectIdentifier> purposeList = usages.getUsages();
final Collection<String> usageList = new ArrayList<String>();
for (DERObjectIdentifier purpose : purposeList) usageList.add(purpose.getId());
this.policyValue = PolicyValueFactory.getInstance(usageList);
}
use of org.gudy.bouncycastle.asn1.DERObjectIdentifier in project XobotOS by xamarin.
the class PrivateKeyFactory method createKey.
/**
* Create a private key parameter from the passed in PKCS8 PrivateKeyInfo object.
*
* @param keyInfo the PrivateKeyInfo object containing the key material
* @return a suitable private key parameter
* @throws IOException on an error decoding the key
*/
public static AsymmetricKeyParameter createKey(PrivateKeyInfo keyInfo) throws IOException {
AlgorithmIdentifier algId = keyInfo.getAlgorithmId();
if (algId.getAlgorithm().equals(PKCSObjectIdentifiers.rsaEncryption)) {
RSAPrivateKeyStructure keyStructure = new RSAPrivateKeyStructure((ASN1Sequence) keyInfo.getPrivateKey());
return new RSAPrivateCrtKeyParameters(keyStructure.getModulus(), keyStructure.getPublicExponent(), keyStructure.getPrivateExponent(), keyStructure.getPrime1(), keyStructure.getPrime2(), keyStructure.getExponent1(), keyStructure.getExponent2(), keyStructure.getCoefficient());
} else // else if (algId.getObjectId().equals(X9ObjectIdentifiers.dhpublicnumber))
if (algId.getObjectId().equals(PKCSObjectIdentifiers.dhKeyAgreement)) {
DHParameter params = new DHParameter((ASN1Sequence) keyInfo.getAlgorithmId().getParameters());
DERInteger derX = (DERInteger) keyInfo.getPrivateKey();
BigInteger lVal = params.getL();
int l = lVal == null ? 0 : lVal.intValue();
DHParameters dhParams = new DHParameters(params.getP(), params.getG(), null, l);
return new DHPrivateKeyParameters(derX.getValue(), dhParams);
} else // END android-removed
if (algId.getObjectId().equals(X9ObjectIdentifiers.id_dsa)) {
DERInteger derX = (DERInteger) keyInfo.getPrivateKey();
DEREncodable de = keyInfo.getAlgorithmId().getParameters();
DSAParameters parameters = null;
if (de != null) {
DSAParameter params = DSAParameter.getInstance(de.getDERObject());
parameters = new DSAParameters(params.getP(), params.getQ(), params.getG());
}
return new DSAPrivateKeyParameters(derX.getValue(), parameters);
} else if (algId.getObjectId().equals(X9ObjectIdentifiers.id_ecPublicKey)) {
X962Parameters params = new X962Parameters((DERObject) keyInfo.getAlgorithmId().getParameters());
ECDomainParameters dParams = null;
if (params.isNamedCurve()) {
DERObjectIdentifier oid = (DERObjectIdentifier) params.getParameters();
X9ECParameters ecP = X962NamedCurves.getByOID(oid);
if (ecP == null) {
ecP = SECNamedCurves.getByOID(oid);
if (ecP == null) {
ecP = NISTNamedCurves.getByOID(oid);
// BEGIN android-removed
// if (ecP == null)
// {
// ecP = TeleTrusTNamedCurves.getByOID(oid);
// }
// END android-removed
}
}
dParams = new ECDomainParameters(ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
} else {
X9ECParameters ecP = new X9ECParameters((ASN1Sequence) params.getParameters());
dParams = new ECDomainParameters(ecP.getCurve(), ecP.getG(), ecP.getN(), ecP.getH(), ecP.getSeed());
}
ECPrivateKeyStructure ec = new ECPrivateKeyStructure((ASN1Sequence) keyInfo.getPrivateKey());
return new ECPrivateKeyParameters(ec.getKey(), dParams);
} else {
throw new RuntimeException("algorithm identifier in key not recognised");
}
}
use of org.gudy.bouncycastle.asn1.DERObjectIdentifier in project XobotOS by xamarin.
the class X509Name method equals.
/**
* test for equality - note: case is ignored.
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof X509Name || obj instanceof ASN1Sequence)) {
return false;
}
DERObject derO = ((DEREncodable) obj).getDERObject();
if (this.getDERObject().equals(derO)) {
return true;
}
X509Name other;
try {
other = X509Name.getInstance(obj);
} catch (IllegalArgumentException e) {
return false;
}
int orderingSize = ordering.size();
if (orderingSize != other.ordering.size()) {
return false;
}
boolean[] indexes = new boolean[orderingSize];
int start, end, delta;
if (// guess forward
ordering.elementAt(0).equals(other.ordering.elementAt(0))) {
start = 0;
end = orderingSize;
delta = 1;
} else // guess reversed - most common problem
{
start = orderingSize - 1;
end = -1;
delta = -1;
}
for (int i = start; i != end; i += delta) {
boolean found = false;
DERObjectIdentifier oid = (DERObjectIdentifier) ordering.elementAt(i);
String value = (String) values.elementAt(i);
for (int j = 0; j < orderingSize; j++) {
if (indexes[j]) {
continue;
}
DERObjectIdentifier oOid = (DERObjectIdentifier) other.ordering.elementAt(j);
if (oid.equals(oOid)) {
String oValue = (String) other.values.elementAt(j);
if (equivalentStrings(value, oValue)) {
indexes[j] = true;
found = true;
break;
}
}
}
if (!found) {
return false;
}
}
return true;
}
use of org.gudy.bouncycastle.asn1.DERObjectIdentifier in project XobotOS by xamarin.
the class X509Name method equals.
/**
* @param inOrder if true the order of both X509 names must be the same,
* as well as the values associated with each element.
*/
public boolean equals(Object obj, boolean inOrder) {
if (!inOrder) {
return this.equals(obj);
}
if (obj == this) {
return true;
}
if (!(obj instanceof X509Name || obj instanceof ASN1Sequence)) {
return false;
}
DERObject derO = ((DEREncodable) obj).getDERObject();
if (this.getDERObject().equals(derO)) {
return true;
}
X509Name other;
try {
other = X509Name.getInstance(obj);
} catch (IllegalArgumentException e) {
return false;
}
int orderingSize = ordering.size();
if (orderingSize != other.ordering.size()) {
return false;
}
for (int i = 0; i < orderingSize; i++) {
DERObjectIdentifier oid = (DERObjectIdentifier) ordering.elementAt(i);
DERObjectIdentifier oOid = (DERObjectIdentifier) other.ordering.elementAt(i);
if (oid.equals(oOid)) {
String value = (String) values.elementAt(i);
String oValue = (String) other.values.elementAt(i);
if (!equivalentStrings(value, oValue)) {
return false;
}
} else {
return false;
}
}
return true;
}
Aggregations