Commits

Anonymous committed 32a33a2 Draft

minor cleanup

  • Participants
  • Parent commits 46fd561

Comments (0)

Files changed (1)

File crypto/spec/asn1/cms.ceylon

 import ceylon.math.whole { wholeNumber, zero }
 
 shared abstract class ContentType([Integer+] identifier)
-    of dataContentType|signedDataContentType|envelopedDataContentType|
-       compressedDataContentType
-    extends ObjectIdentifier(identifier) {}
+        of dataContentType|signedDataContentType|envelopedDataContentType|
+           compressedDataContentType
+        extends ObjectIdentifier(identifier) {
+    shared formal ContentInfo parse(ASN1Sequence asn1);
+}
 
 ObjectIdentifier pkcs7 = ObjectIdentifier([1, 2, 840, 113549, 1, 7]);
 //id-data OBJECT IDENTIFIER ::= { iso(1) member-body(2)
 //         us(840) rsadsi(113549) pkcs(1) pkcs7(7) 1 }
-shared object dataContentType extends ContentType(pkcs7.sequence.withTrailing(1)) {}
+shared object dataContentType
+        extends ContentType(pkcs7.sequence.withTrailing(1)) {
+    shared actual ArbitraryData parse(ASN1Sequence asn1) => ArbitraryData(asn1);
+}
 //id-signedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
 //         us(840) rsadsi(113549) pkcs(1) pkcs7(7) 2 }
-shared object signedDataContentType extends ContentType(pkcs7.sequence.withTrailing(2)) {}
+shared object signedDataContentType
+        extends ContentType(pkcs7.sequence.withTrailing(2)) {
+    shared actual SignedData parse(ASN1Sequence asn1) => asn1SignedData(asn1);
+}
 //id-envelopedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
 //          us(840) rsadsi(113549) pkcs(1) pkcs7(7) 3 }
-shared object envelopedDataContentType extends ContentType(pkcs7.sequence.withTrailing(3)) {}
+shared object envelopedDataContentType
+        extends ContentType(pkcs7.sequence.withTrailing(3)) {
+    shared actual EnvelopedData parse(ASN1Sequence asn1) => asn1EnvelopedData(asn1);
+}
 //id-ct-compressedData OBJECT IDENTIFIER ::= { iso(1) member-body(2)
 //         us(840) rsadsi(113549) pkcs(1) pkcs-9(9) smime(16) ct(1) 9 }
-shared object compressedDataContentType extends ContentType([1, 2, 840, 113549, 1, 9, 16, 1, 9]) {}
+shared object compressedDataContentType
+        extends ContentType([1, 2, 840, 113549, 1, 9, 16, 1, 9]) {
+    shared actual CompressedData parse(ASN1Sequence asn1) => asn1CompressedData(asn1);
+}
 
 shared abstract class ContentInfo(contentType)
         of ArbitraryData|SignedData|EnvelopedData|CompressedData
     }
 }
 
-class SignedData(digestAlgorithms, encapContentInfo, signerInfos)
+shared class SignedData(digestAlgorithms, encapContentInfo, signerInfos)
         extends ContentInfo(signedDataContentType) {
     shared Set<AlgorithmIdentifier> digestAlgorithms;
     shared EncapsulatedContentInfo encapContentInfo;
     ]);
 }
 
-class SignerInfo(signerIdentifier, digestAlgorithm, signedAttrs,
+shared class SignerInfo(signerIdentifier, digestAlgorithm, signedAttrs,
         signatureAlgorithm, signature)
         satisfies ASN1Representable {
     shared SignerIdentifier signerIdentifier;
     ]);
 }
 
-abstract class SignerIdentifier()
+shared abstract class SignerIdentifier()
         of SubjectKeyIdentifier
         satisfies ASN1Representable {}
 
     shared actual ExplicitTag asn1 => ExplicitTag(0, identifier);
 }
 
-class Attribute(type, values) satisfies ASN1Representable {
+shared class Attribute(type, values) satisfies ASN1Representable {
     shared ObjectIdentifier type;
     shared Set<ASN1Encodable> values;
     
     }
 }
 
-ContentType checkContentType(ObjectIdentifier identifier) {
+ContentType toContentType(ObjectIdentifier identifier) {
     for (contentType in [dataContentType, signedDataContentType,
             envelopedDataContentType, compressedDataContentType]) {
         if (contentType == identifier) {
     assert(wrappedEncContent.contextTag == 0);
     assert(is OctetString encContentString = wrappedEncContent.wrappedObject);
     
-    return EncapsulatedContentInfo(checkContentType(identifier), encContentString);
+    return EncapsulatedContentInfo(toContentType(identifier), encContentString);
 }
 
 SignerIdentifier asn1SignerIdentifier(ExplicitTag tag) {
     assert(asn1.sequence.size == 3);
     
     assert(is ObjectIdentifier identifier = asn1.sequence.first);
-    value contentType = checkContentType(identifier);
+    value contentType = toContentType(identifier);
     
     assert(is ASN1Sequence encEncryptionAlgorithm = asn1.sequence[1]);
     value encryptionAlgorithm = asn1AlgorithmIdentifier(encEncryptionAlgorithm);
     return CompressedData(compressionAlgorithm, encapContentInfo);
 }
 
-ContentInfo createContentInfo(ObjectIdentifier identifier, ASN1Representable content) {
-    assert(is ASN1Decodable content);
-    switch (checkContentType(identifier))
-    case (dataContentType) {
-        return ArbitraryData(content);
-    }
-    case (signedDataContentType) {
-        assert(is ASN1Sequence content);
-        return asn1SignedData(content);
-    }
-    case (envelopedDataContentType) {
-        assert(is ASN1Sequence content);
-        return asn1EnvelopedData(content);
-    }
-    case (compressedDataContentType) {
-        assert(is ASN1Sequence content);
-        return asn1CompressedData(content);
-    }
-}
-
 shared ContentInfo asn1ContentInfo(ASN1Sequence asn1) {
     value seq = asn1.sequence;
     assert(seq.size == 2);
     assert(is ObjectIdentifier identifier = seq.first);
     assert(is ExplicitTag tag = seq.last);
     assert(tag.contextTag == 0);
-    value content = tag.wrappedObject;
+    assert(is ASN1Sequence content = tag.wrappedObject);
 
-    return createContentInfo(identifier, content);
+    return toContentType(identifier).parse(content);
 }