diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2019-06-04 16:55:26 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2019-06-04 16:55:26 +0700 |
commit | 3eb93b423abeefbdc03f0ecc38751d76428ba23e (patch) | |
tree | f364f857c54f65eb59462560b1f282b896eecf8b /crypto/src/asn1/x509 | |
parent | Port LinkedCertificate from bc-java (diff) | |
download | BouncyCastle.NET-ed25519-3eb93b423abeefbdc03f0ecc38751d76428ba23e.tar.xz |
Refactoring
Diffstat (limited to 'crypto/src/asn1/x509')
20 files changed, 117 insertions, 280 deletions
diff --git a/crypto/src/asn1/x509/AttributeCertificateInfo.cs b/crypto/src/asn1/x509/AttributeCertificateInfo.cs index 526f8e69b..a62b01981 100644 --- a/crypto/src/asn1/x509/AttributeCertificateInfo.cs +++ b/crypto/src/asn1/x509/AttributeCertificateInfo.cs @@ -136,21 +136,10 @@ namespace Org.BouncyCastle.Asn1.X509 */ public override Asn1Object ToAsn1Object() { - Asn1EncodableVector v = new Asn1EncodableVector( - version, holder, issuer, signature, serialNumber, - attrCertValidityPeriod, attributes); - - if (issuerUniqueID != null) - { - v.Add(issuerUniqueID); - } - - if (extensions != null) - { - v.Add(extensions); - } - - return new DerSequence(v); + Asn1EncodableVector v = new Asn1EncodableVector(version, holder, issuer, signature, serialNumber, + attrCertValidityPeriod, attributes); + v.AddOptional(issuerUniqueID, extensions); + return new DerSequence(v); } } } diff --git a/crypto/src/asn1/x509/AuthorityKeyIdentifier.cs b/crypto/src/asn1/x509/AuthorityKeyIdentifier.cs index d5a9048cc..aca1dc330 100644 --- a/crypto/src/asn1/x509/AuthorityKeyIdentifier.cs +++ b/crypto/src/asn1/x509/AuthorityKeyIdentifier.cs @@ -184,23 +184,10 @@ namespace Org.BouncyCastle.Asn1.X509 public override Asn1Object ToAsn1Object() { Asn1EncodableVector v = new Asn1EncodableVector(); - - if (keyidentifier != null) - { - v.Add(new DerTaggedObject(false, 0, keyidentifier)); - } - - if (certissuer != null) - { - v.Add(new DerTaggedObject(false, 1, certissuer)); - } - - if (certserno != null) - { - v.Add(new DerTaggedObject(false, 2, certserno)); - } - - return new DerSequence(v); + v.AddOptionalTagged(false, 0, keyidentifier); + v.AddOptionalTagged(false, 1, certissuer); + v.AddOptionalTagged(false, 2, certserno); + return new DerSequence(v); } public override string ToString() diff --git a/crypto/src/asn1/x509/BasicConstraints.cs b/crypto/src/asn1/x509/BasicConstraints.cs index 098801f22..079294d1b 100644 --- a/crypto/src/asn1/x509/BasicConstraints.cs +++ b/crypto/src/asn1/x509/BasicConstraints.cs @@ -106,18 +106,9 @@ namespace Org.BouncyCastle.Asn1.X509 public override Asn1Object ToAsn1Object() { Asn1EncodableVector v = new Asn1EncodableVector(); - - if (cA != null) - { - v.Add(cA); - } - - if (pathLenConstraint != null) // yes some people actually do this when cA is false... - { - v.Add(pathLenConstraint); - } - - return new DerSequence(v); + v.AddOptional(cA, + pathLenConstraint); // yes some people actually do this when cA is false... + return new DerSequence(v); } public override string ToString() diff --git a/crypto/src/asn1/x509/CertificatePair.cs b/crypto/src/asn1/x509/CertificatePair.cs index da9236010..69861e1dc 100644 --- a/crypto/src/asn1/x509/CertificatePair.cs +++ b/crypto/src/asn1/x509/CertificatePair.cs @@ -126,22 +126,13 @@ namespace Org.BouncyCastle.Asn1.X509 * * @return a DERObject */ - public override Asn1Object ToAsn1Object() - { - Asn1EncodableVector vec = new Asn1EncodableVector(); - - if (forward != null) - { - vec.Add(new DerTaggedObject(0, forward)); - } - - if (reverse != null) - { - vec.Add(new DerTaggedObject(1, reverse)); - } - - return new DerSequence(vec); - } + public override Asn1Object ToAsn1Object() + { + Asn1EncodableVector v = new Asn1EncodableVector(); + v.AddOptionalTagged(true, 0, forward); + v.AddOptionalTagged(true, 1, reverse); + return new DerSequence(v); + } /** * @return Returns the forward. diff --git a/crypto/src/asn1/x509/DistributionPoint.cs b/crypto/src/asn1/x509/DistributionPoint.cs index 40814c7a8..54ab930a5 100644 --- a/crypto/src/asn1/x509/DistributionPoint.cs +++ b/crypto/src/asn1/x509/DistributionPoint.cs @@ -92,29 +92,16 @@ namespace Org.BouncyCastle.Asn1.X509 get { return cRLIssuer; } } - public override Asn1Object ToAsn1Object() + public override Asn1Object ToAsn1Object() { Asn1EncodableVector v = new Asn1EncodableVector(); - if (distributionPoint != null) - { - // - // as this is a CHOICE it must be explicitly tagged - // - v.Add(new DerTaggedObject(0, distributionPoint)); - } - - if (reasons != null) - { - v.Add(new DerTaggedObject(false, 1, reasons)); - } - - if (cRLIssuer != null) - { - v.Add(new DerTaggedObject(false, 2, cRLIssuer)); - } + // As this is a CHOICE it must be explicitly tagged + v.AddOptionalTagged(true, 0, distributionPoint); - return new DerSequence(v); + v.AddOptionalTagged(false, 1, reasons); + v.AddOptionalTagged(false, 2, cRLIssuer); + return new DerSequence(v); } public override string ToString() diff --git a/crypto/src/asn1/x509/GeneralSubtree.cs b/crypto/src/asn1/x509/GeneralSubtree.cs index e918a0277..a42ec4643 100644 --- a/crypto/src/asn1/x509/GeneralSubtree.cs +++ b/crypto/src/asn1/x509/GeneralSubtree.cs @@ -178,11 +178,7 @@ namespace Org.BouncyCastle.Asn1.X509 v.Add(new DerTaggedObject(false, 0, minimum)); } - if (maximum != null) - { - v.Add(new DerTaggedObject(false, 1, maximum)); - } - + v.AddOptionalTagged(false, 1, maximum); return new DerSequence(v); } } diff --git a/crypto/src/asn1/x509/Holder.cs b/crypto/src/asn1/x509/Holder.cs index 6e5315b80..b67c0b6c7 100644 --- a/crypto/src/asn1/x509/Holder.cs +++ b/crypto/src/asn1/x509/Holder.cs @@ -224,36 +224,23 @@ namespace Org.BouncyCastle.Asn1.X509 * } * </pre> */ - public override Asn1Object ToAsn1Object() - { - if (version == 1) - { - Asn1EncodableVector v = new Asn1EncodableVector(); - - if (baseCertificateID != null) - { - v.Add(new DerTaggedObject(false, 0, baseCertificateID)); - } - - if (entityName != null) - { - v.Add(new DerTaggedObject(false, 1, entityName)); - } - - if (objectDigestInfo != null) - { - v.Add(new DerTaggedObject(false, 2, objectDigestInfo)); - } - - return new DerSequence(v); - } + public override Asn1Object ToAsn1Object() + { + if (version == 1) + { + Asn1EncodableVector v = new Asn1EncodableVector(); + v.AddOptionalTagged(false, 0, baseCertificateID); + v.AddOptionalTagged(false, 1, entityName); + v.AddOptionalTagged(false, 2, objectDigestInfo); + return new DerSequence(v); + } - if (entityName != null) - { - return new DerTaggedObject(false, 1, entityName); - } + if (entityName != null) + { + return new DerTaggedObject(false, 1, entityName); + } - return new DerTaggedObject(false, 0, baseCertificateID); - } + return new DerTaggedObject(false, 0, baseCertificateID); + } } } diff --git a/crypto/src/asn1/x509/IetfAttrSyntax.cs b/crypto/src/asn1/x509/IetfAttrSyntax.cs index e719865b3..05313b1af 100644 --- a/crypto/src/asn1/x509/IetfAttrSyntax.cs +++ b/crypto/src/asn1/x509/IetfAttrSyntax.cs @@ -147,15 +147,9 @@ namespace Org.BouncyCastle.Asn1.X509 public override Asn1Object ToAsn1Object() { Asn1EncodableVector v = new Asn1EncodableVector(); - - if (policyAuthority != null) - { - v.Add(new DerTaggedObject(0, policyAuthority)); - } - - v.Add(new DerSequence(values)); - - return new DerSequence(v); + v.AddOptionalTagged(true, 0, policyAuthority); + v.Add(new DerSequence(values)); + return new DerSequence(v); } } } diff --git a/crypto/src/asn1/x509/IssuerSerial.cs b/crypto/src/asn1/x509/IssuerSerial.cs index 1e47e022b..2c26339bd 100644 --- a/crypto/src/asn1/x509/IssuerSerial.cs +++ b/crypto/src/asn1/x509/IssuerSerial.cs @@ -84,17 +84,11 @@ namespace Org.BouncyCastle.Asn1.X509 * } * </pre> */ - public override Asn1Object ToAsn1Object() - { - Asn1EncodableVector v = new Asn1EncodableVector( - issuer, serial); - - if (issuerUid != null) - { - v.Add(issuerUid); - } - - return new DerSequence(v); - } + public override Asn1Object ToAsn1Object() + { + Asn1EncodableVector v = new Asn1EncodableVector(issuer, serial); + v.AddOptional(issuerUid); + return new DerSequence(v); + } } } diff --git a/crypto/src/asn1/x509/NameConstraints.cs b/crypto/src/asn1/x509/NameConstraints.cs index 0c5fea8b3..b8cc7c0ab 100644 --- a/crypto/src/asn1/x509/NameConstraints.cs +++ b/crypto/src/asn1/x509/NameConstraints.cs @@ -100,21 +100,12 @@ namespace Org.BouncyCastle.Asn1.X509 * NameConstraints ::= SEQUENCE { permittedSubtrees [0] GeneralSubtrees * OPTIONAL, excludedSubtrees [1] GeneralSubtrees OPTIONAL } */ - public override Asn1Object ToAsn1Object() - { - Asn1EncodableVector v = new Asn1EncodableVector(); - - if (permitted != null) - { - v.Add(new DerTaggedObject(false, 0, permitted)); - } - - if (excluded != null) - { - v.Add(new DerTaggedObject(false, 1, excluded)); - } - - return new DerSequence(v); - } + public override Asn1Object ToAsn1Object() + { + Asn1EncodableVector v = new Asn1EncodableVector(); + v.AddOptionalTagged(false, 0, permitted); + v.AddOptionalTagged(false, 1, excluded); + return new DerSequence(v); + } } } diff --git a/crypto/src/asn1/x509/ObjectDigestInfo.cs b/crypto/src/asn1/x509/ObjectDigestInfo.cs index 9cd9a5f4c..190243cc6 100644 --- a/crypto/src/asn1/x509/ObjectDigestInfo.cs +++ b/crypto/src/asn1/x509/ObjectDigestInfo.cs @@ -162,18 +162,12 @@ namespace Org.BouncyCastle.Asn1.X509 * * </pre> */ - public override Asn1Object ToAsn1Object() + public override Asn1Object ToAsn1Object() { Asn1EncodableVector v = new Asn1EncodableVector(digestedObjectType); - - if (otherObjectTypeID != null) - { - v.Add(otherObjectTypeID); - } - - v.Add(digestAlgorithm, objectDigest); - - return new DerSequence(v); + v.AddOptional(otherObjectTypeID); + v.Add(digestAlgorithm, objectDigest); + return new DerSequence(v); } } } diff --git a/crypto/src/asn1/x509/PolicyInformation.cs b/crypto/src/asn1/x509/PolicyInformation.cs index 29d245084..90db29a36 100644 --- a/crypto/src/asn1/x509/PolicyInformation.cs +++ b/crypto/src/asn1/x509/PolicyInformation.cs @@ -68,13 +68,8 @@ namespace Org.BouncyCastle.Asn1.X509 public override Asn1Object ToAsn1Object() { Asn1EncodableVector v = new Asn1EncodableVector(policyIdentifier); - - if (policyQualifiers != null) - { - v.Add(policyQualifiers); - } - - return new DerSequence(v); + v.AddOptional(policyQualifiers); + return new DerSequence(v); } } } diff --git a/crypto/src/asn1/x509/PrivateKeyUsagePeriod.cs b/crypto/src/asn1/x509/PrivateKeyUsagePeriod.cs index a3d7a3608..89e8de6cb 100644 --- a/crypto/src/asn1/x509/PrivateKeyUsagePeriod.cs +++ b/crypto/src/asn1/x509/PrivateKeyUsagePeriod.cs @@ -64,21 +64,12 @@ namespace Org.BouncyCastle.Asn1.X509 get { return _notAfter; } } - public override Asn1Object ToAsn1Object() - { - Asn1EncodableVector v = new Asn1EncodableVector(); - - if (_notBefore != null) - { - v.Add(new DerTaggedObject(false, 0, _notBefore)); - } - - if (_notAfter != null) - { - v.Add(new DerTaggedObject(false, 1, _notAfter)); - } - - return new DerSequence(v); - } + public override Asn1Object ToAsn1Object() + { + Asn1EncodableVector v = new Asn1EncodableVector(); + v.AddOptionalTagged(false, 0, _notBefore); + v.AddOptionalTagged(false, 1, _notAfter); + return new DerSequence(v); + } } } diff --git a/crypto/src/asn1/x509/RoleSyntax.cs b/crypto/src/asn1/x509/RoleSyntax.cs index 48c3c6cae..b5b217b6b 100644 --- a/crypto/src/asn1/x509/RoleSyntax.cs +++ b/crypto/src/asn1/x509/RoleSyntax.cs @@ -190,19 +190,13 @@ namespace Org.BouncyCastle.Asn1.X509 * } * </pre> */ - public override Asn1Object ToAsn1Object() - { - Asn1EncodableVector v = new Asn1EncodableVector(); - - if (this.roleAuthority != null) - { - v.Add(new DerTaggedObject(false, 0, roleAuthority)); - } - - v.Add(new DerTaggedObject(true, 1, roleName)); - - return new DerSequence(v); - } + public override Asn1Object ToAsn1Object() + { + Asn1EncodableVector v = new Asn1EncodableVector(); + v.AddOptionalTagged(false, 0, roleAuthority); + v.Add(new DerTaggedObject(true, 1, roleName)); + return new DerSequence(v); + } public override string ToString() { diff --git a/crypto/src/asn1/x509/UserNotice.cs b/crypto/src/asn1/x509/UserNotice.cs index f40916434..6311c7693 100644 --- a/crypto/src/asn1/x509/UserNotice.cs +++ b/crypto/src/asn1/x509/UserNotice.cs @@ -112,19 +112,9 @@ namespace Org.BouncyCastle.Asn1.X509 public override Asn1Object ToAsn1Object() { - Asn1EncodableVector av = new Asn1EncodableVector(); - - if (noticeRef != null) - { - av.Add(noticeRef); - } - - if (explicitText != null) - { - av.Add(explicitText); - } - - return new DerSequence(av); + Asn1EncodableVector v = new Asn1EncodableVector(); + v.AddOptional(noticeRef, explicitText); + return new DerSequence(v); } } } diff --git a/crypto/src/asn1/x509/V2Form.cs b/crypto/src/asn1/x509/V2Form.cs index 2c6e54a77..53475ffbe 100644 --- a/crypto/src/asn1/x509/V2Form.cs +++ b/crypto/src/asn1/x509/V2Form.cs @@ -115,22 +115,9 @@ namespace Org.BouncyCastle.Asn1.X509 public override Asn1Object ToAsn1Object() { Asn1EncodableVector v = new Asn1EncodableVector(); - - if (issuerName != null) - { - v.Add(issuerName); - } - - if (baseCertificateID != null) - { - v.Add(new DerTaggedObject(false, 0, baseCertificateID)); - } - - if (objectDigestInfo != null) - { - v.Add(new DerTaggedObject(false, 1, objectDigestInfo)); - } - + v.AddOptional(issuerName); + v.AddOptionalTagged(false, 0, baseCertificateID); + v.AddOptionalTagged(false, 1, objectDigestInfo); return new DerSequence(v); } } diff --git a/crypto/src/asn1/x509/qualified/BiometricData.cs b/crypto/src/asn1/x509/qualified/BiometricData.cs index bb70c342c..816ffd26d 100644 --- a/crypto/src/asn1/x509/qualified/BiometricData.cs +++ b/crypto/src/asn1/x509/qualified/BiometricData.cs @@ -94,17 +94,11 @@ namespace Org.BouncyCastle.Asn1.X509.Qualified get { return sourceDataUri; } } - public override Asn1Object ToAsn1Object() + public override Asn1Object ToAsn1Object() { - Asn1EncodableVector seq = new Asn1EncodableVector( - typeOfBiometricData, hashAlgorithm, biometricDataHash); - - if (sourceDataUri != null) - { - seq.Add(sourceDataUri); - } - - return new DerSequence(seq); + Asn1EncodableVector v = new Asn1EncodableVector(typeOfBiometricData, hashAlgorithm, biometricDataHash); + v.AddOptional(sourceDataUri); + return new DerSequence(v); } } } diff --git a/crypto/src/asn1/x509/qualified/QCStatement.cs b/crypto/src/asn1/x509/qualified/QCStatement.cs index a8e214cbf..bfa515392 100644 --- a/crypto/src/asn1/x509/qualified/QCStatement.cs +++ b/crypto/src/asn1/x509/qualified/QCStatement.cs @@ -69,16 +69,11 @@ namespace Org.BouncyCastle.Asn1.X509.Qualified get { return qcStatementInfo; } } - public override Asn1Object ToAsn1Object() + public override Asn1Object ToAsn1Object() { - Asn1EncodableVector seq = new Asn1EncodableVector(qcStatementId); - - if (qcStatementInfo != null) - { - seq.Add(qcStatementInfo); - } - - return new DerSequence(seq); + Asn1EncodableVector v = new Asn1EncodableVector(qcStatementId); + v.AddOptional(qcStatementInfo); + return new DerSequence(v); } } } diff --git a/crypto/src/asn1/x509/qualified/SemanticsInformation.cs b/crypto/src/asn1/x509/qualified/SemanticsInformation.cs index 5fe5f936c..379e6d1d1 100644 --- a/crypto/src/asn1/x509/qualified/SemanticsInformation.cs +++ b/crypto/src/asn1/x509/qualified/SemanticsInformation.cs @@ -104,21 +104,17 @@ namespace Org.BouncyCastle.Asn1.X509.Qualified return nameRegistrationAuthorities; } - public override Asn1Object ToAsn1Object() + public override Asn1Object ToAsn1Object() { - Asn1EncodableVector seq = new Asn1EncodableVector(); + Asn1EncodableVector v = new Asn1EncodableVector(); + v.AddOptional(semanticsIdentifier); - if (this.semanticsIdentifier != null) + if (null != nameRegistrationAuthorities) { - seq.Add(semanticsIdentifier); + v.Add(new DerSequence(nameRegistrationAuthorities)); } - if (this.nameRegistrationAuthorities != null) - { - seq.Add(new DerSequence(nameRegistrationAuthorities)); - } - - return new DerSequence(seq); + return new DerSequence(v); } } } diff --git a/crypto/src/asn1/x509/sigi/PersonalData.cs b/crypto/src/asn1/x509/sigi/PersonalData.cs index dba345c42..0e0bb5365 100644 --- a/crypto/src/asn1/x509/sigi/PersonalData.cs +++ b/crypto/src/asn1/x509/sigi/PersonalData.cs @@ -181,31 +181,25 @@ namespace Org.BouncyCastle.Asn1.X509.SigI * * @return an Asn1Object */ - public override Asn1Object ToAsn1Object() - { - Asn1EncodableVector vec = new Asn1EncodableVector(); - vec.Add(nameOrPseudonym); - if (nameDistinguisher != null) - { - vec.Add(new DerTaggedObject(false, 0, new DerInteger(nameDistinguisher))); - } - if (dateOfBirth != null) - { - vec.Add(new DerTaggedObject(false, 1, dateOfBirth)); - } - if (placeOfBirth != null) - { - vec.Add(new DerTaggedObject(true, 2, placeOfBirth)); - } - if (gender != null) - { - vec.Add(new DerTaggedObject(false, 3, new DerPrintableString(gender, true))); - } - if (postalAddress != null) - { - vec.Add(new DerTaggedObject(true, 4, postalAddress)); - } - return new DerSequence(vec); - } + public override Asn1Object ToAsn1Object() + { + Asn1EncodableVector v = new Asn1EncodableVector(nameOrPseudonym); + + if (null != nameDistinguisher) + { + v.Add(new DerTaggedObject(false, 0, new DerInteger(nameDistinguisher))); + } + + v.AddOptionalTagged(false, 1, dateOfBirth); + v.AddOptionalTagged(true, 2, placeOfBirth); + + if (null != gender) + { + v.Add(new DerTaggedObject(false, 3, new DerPrintableString(gender, true))); + } + + v.AddOptionalTagged(true, 4, postalAddress); + return new DerSequence(v); + } } } |