// in case we just want to timestamp the file public bool Timestamp(string fileName) { try { AuthenticodeDeformatter def = new AuthenticodeDeformatter(fileName); byte[] signature = def.Signature; if (signature != null) { Open(fileName); PKCS7.ContentInfo ci = new PKCS7.ContentInfo(signature); pkcs7 = new PKCS7.SignedData(ci.Content); byte[] response = Timestamp(pkcs7.SignerInfo.Signature); ASN1 ts = new ASN1(Convert.FromBase64String(Encoding.ASCII.GetString(response))); // insert new certificates and countersignature into the original signature ASN1 asn = new ASN1(signature); ASN1 content = asn.Element(1, 0xA0); if (content == null) { return(false); } ASN1 signedData = content.Element(0, 0x30); if (signedData == null) { return(false); } // add the supplied certificates inside our signature ASN1 certificates = signedData.Element(3, 0xA0); if (certificates == null) { certificates = new ASN1(0xA0); signedData.Add(certificates); } for (int i = 0; i < ts[1][0][3].Count; i++) { certificates.Add(ts[1][0][3][i]); } // add an unauthentified attribute to our signature ASN1 signerInfoSet = signedData[signedData.Count - 1]; ASN1 signerInfo = signerInfoSet[0]; ASN1 unauthenticated = signerInfo[signerInfo.Count - 1]; if (unauthenticated.Tag != 0xA1) { unauthenticated = new ASN1(0xA1); signerInfo.Add(unauthenticated); } unauthenticated.Add(Attribute(countersignature, ts[1][0][4][0])); return(Save(fileName, asn.GetBytes())); } } catch (Exception e) { Console.WriteLine(e); } return(false); }
public bool Timestamp(string fileName) { try { AuthenticodeDeformatter authenticodeDeformatter = new AuthenticodeDeformatter(fileName); byte[] signature = authenticodeDeformatter.Signature; if (signature != null) { base.Open(fileName); PKCS7.ContentInfo contentInfo = new PKCS7.ContentInfo(signature); this.pkcs7 = new PKCS7.SignedData(contentInfo.Content); byte[] bytes = this.Timestamp(this.pkcs7.SignerInfo.Signature); ASN1 asn = new ASN1(Convert.FromBase64String(Encoding.ASCII.GetString(bytes))); ASN1 asn2 = new ASN1(signature); ASN1 asn3 = asn2.Element(1, 160); if (asn3 == null) { return(false); } ASN1 asn4 = asn3.Element(0, 48); if (asn4 == null) { return(false); } ASN1 asn5 = asn4.Element(3, 160); if (asn5 == null) { asn5 = new ASN1(160); asn4.Add(asn5); } for (int i = 0; i < asn[1][0][3].Count; i++) { asn5.Add(asn[1][0][3][i]); } ASN1 asn6 = asn4[asn4.Count - 1]; ASN1 asn7 = asn6[0]; ASN1 asn8 = asn7[asn7.Count - 1]; if (asn8.Tag != 161) { asn8 = new ASN1(161); asn7.Add(asn8); } asn8.Add(this.Attribute("1.2.840.113549.1.9.6", asn[1][0][4][0])); return(this.Save(fileName, asn2.GetBytes())); } } catch (Exception value) { Console.WriteLine(value); } return(false); }
public bool Timestamp(string fileName) { try { AuthenticodeDeformatter authenticodeDeformatter = new AuthenticodeDeformatter(fileName); byte[] signature = authenticodeDeformatter.Signature; if (signature != null) { Open(fileName); PKCS7.ContentInfo contentInfo = new PKCS7.ContentInfo(signature); pkcs7 = new PKCS7.SignedData(contentInfo.Content); byte[] bytes = Timestamp(pkcs7.SignerInfo.Signature); ASN1 aSN = new ASN1(Convert.FromBase64String(Encoding.ASCII.GetString(bytes))); ASN1 aSN2 = new ASN1(signature); ASN1 aSN3 = aSN2.Element(1, 160); if (aSN3 == null) { return(false); } ASN1 aSN4 = aSN3.Element(0, 48); if (aSN4 == null) { return(false); } ASN1 aSN5 = aSN4.Element(3, 160); if (aSN5 == null) { aSN5 = new ASN1(160); aSN4.Add(aSN5); } for (int i = 0; i < aSN[1][0][3].Count; i++) { aSN5.Add(aSN[1][0][3][i]); } ASN1 aSN6 = aSN4[aSN4.Count - 1]; ASN1 aSN7 = aSN6[0]; ASN1 aSN8 = aSN7[aSN7.Count - 1]; if (aSN8.Tag != 161) { aSN8 = new ASN1(161); aSN7.Add(aSN8); } aSN8.Add(Attribute("1.2.840.113549.1.9.6", aSN[1][0][4][0])); return(Save(fileName, aSN2.GetBytes())); } } catch (Exception value) { Console.WriteLine(value); } return(false); }
// that's were the real job is! private void Parse(byte[] data) { try { decoder = new ASN1(data); // Certificate if (decoder.Tag != 0x30) { throw new CryptographicException(encoding_error); } // Certificate / TBSCertificate if (decoder [0].Tag != 0x30) { throw new CryptographicException(encoding_error); } ASN1 tbsCertificate = decoder [0]; int tbs = 0; // Certificate / TBSCertificate / Version ASN1 v = decoder [0][tbs]; version = 1; // DEFAULT v1 if ((v.Tag == 0xA0) && (v.Count > 0)) { // version (optional) is present only in v2+ certs version += v [0].Value [0]; // zero based tbs++; } // Certificate / TBSCertificate / CertificateSerialNumber ASN1 sn = decoder [0][tbs++]; if (sn.Tag != 0x02) { throw new CryptographicException(encoding_error); } serialnumber = sn.Value; Array.Reverse(serialnumber, 0, serialnumber.Length); // Certificate / TBSCertificate / AlgorithmIdentifier tbs++; // ASN1 signatureAlgo = tbsCertificate.Element (tbs++, 0x30); issuer = tbsCertificate.Element(tbs++, 0x30); m_issuername = X501.ToString(issuer); ASN1 validity = tbsCertificate.Element(tbs++, 0x30); ASN1 notBefore = validity [0]; m_from = ASN1Convert.ToDateTime(notBefore); ASN1 notAfter = validity [1]; m_until = ASN1Convert.ToDateTime(notAfter); subject = tbsCertificate.Element(tbs++, 0x30); m_subject = X501.ToString(subject); ASN1 subjectPublicKeyInfo = tbsCertificate.Element(tbs++, 0x30); ASN1 algorithm = subjectPublicKeyInfo.Element(0, 0x30); ASN1 algo = algorithm.Element(0, 0x06); m_keyalgo = ASN1Convert.ToOid(algo); // parameters ANY DEFINED BY algorithm OPTIONAL // so we dont ask for a specific (Element) type and return DER ASN1 parameters = algorithm [1]; m_keyalgoparams = ((algorithm.Count > 1) ? parameters.GetBytes() : null); ASN1 subjectPublicKey = subjectPublicKeyInfo.Element(1, 0x03); // we must drop th first byte (which is the number of unused bits // in the BITSTRING) int n = subjectPublicKey.Length - 1; m_publickey = new byte [n]; Buffer.BlockCopy(subjectPublicKey.Value, 1, m_publickey, 0, n); // signature processing byte[] bitstring = decoder [2].Value; // first byte contains unused bits in first byte signature = new byte [bitstring.Length - 1]; Buffer.BlockCopy(bitstring, 1, signature, 0, signature.Length); algorithm = decoder [1]; algo = algorithm.Element(0, 0x06); m_signaturealgo = ASN1Convert.ToOid(algo); parameters = algorithm [1]; if (parameters != null) { m_signaturealgoparams = parameters.GetBytes(); } else { m_signaturealgoparams = null; } // Certificate / TBSCertificate / issuerUniqueID ASN1 issuerUID = tbsCertificate.Element(tbs, 0x81); if (issuerUID != null) { tbs++; issuerUniqueID = issuerUID.Value; } // Certificate / TBSCertificate / subjectUniqueID ASN1 subjectUID = tbsCertificate.Element(tbs, 0x82); if (subjectUID != null) { tbs++; subjectUniqueID = subjectUID.Value; } // Certificate / TBSCertificate / Extensions ASN1 extns = tbsCertificate.Element(tbs, 0xA3); if ((extns != null) && (extns.Count == 1)) { extensions = new X509ExtensionCollection(extns [0]); } else { extensions = new X509ExtensionCollection(null); } // keep a copy of the original data m_encodedcert = (byte[])data.Clone(); } catch (Exception ex) { throw new CryptographicException(encoding_error, ex); } }
private void Parse(byte[] data) { try { this.decoder = new ASN1(data); if (this.decoder.Tag != 0x30) { throw new CryptographicException(encoding_error); } if (this.decoder[0].Tag != 0x30) { throw new CryptographicException(encoding_error); } ASN1 asn = this.decoder[0]; int index = 0; ASN1 asn2 = this.decoder[0][index]; this.version = 1; if ((asn2.Tag == 160) && (asn2.Count > 0)) { this.version += asn2[0].Value[0]; index++; } ASN1 asn3 = this.decoder[0][index++]; if (asn3.Tag != 2) { throw new CryptographicException(encoding_error); } this.serialnumber = asn3.Value; Array.Reverse(this.serialnumber, 0, this.serialnumber.Length); index++; this.issuer = asn.Element(index++, 0x30); this.m_issuername = X501.ToString(this.issuer); ASN1 asn4 = asn.Element(index++, 0x30); ASN1 time = asn4[0]; this.m_from = ASN1Convert.ToDateTime(time); ASN1 asn6 = asn4[1]; this.m_until = ASN1Convert.ToDateTime(asn6); this.subject = asn.Element(index++, 0x30); this.m_subject = X501.ToString(this.subject); ASN1 asn7 = asn.Element(index++, 0x30); ASN1 asn8 = asn7.Element(0, 0x30); ASN1 asn9 = asn8.Element(0, 6); this.m_keyalgo = ASN1Convert.ToOid(asn9); ASN1 asn10 = asn8[1]; this.m_keyalgoparams = (asn8.Count <= 1) ? null : asn10.GetBytes(); ASN1 asn11 = asn7.Element(1, 3); int count = asn11.Length - 1; this.m_publickey = new byte[count]; Buffer.BlockCopy(asn11.Value, 1, this.m_publickey, 0, count); byte[] src = this.decoder[2].Value; this.signature = new byte[src.Length - 1]; Buffer.BlockCopy(src, 1, this.signature, 0, this.signature.Length); asn8 = this.decoder[1]; asn9 = asn8.Element(0, 6); this.m_signaturealgo = ASN1Convert.ToOid(asn9); asn10 = asn8[1]; if (asn10 != null) { this.m_signaturealgoparams = asn10.GetBytes(); } else { this.m_signaturealgoparams = null; } ASN1 asn12 = asn.Element(index, 0x81); if (asn12 != null) { index++; this.issuerUniqueID = asn12.Value; } ASN1 asn13 = asn.Element(index, 130); if (asn13 != null) { index++; this.subjectUniqueID = asn13.Value; } ASN1 asn14 = asn.Element(index, 0xa3); if ((asn14 != null) && (asn14.Count == 1)) { this.extensions = new X509ExtensionCollection(asn14[0]); } else { this.extensions = new X509ExtensionCollection(null); } this.m_encodedcert = (byte[])data.Clone(); } catch (Exception exception) { throw new CryptographicException(encoding_error, exception); } }
private void Parse(byte[] data) { try { this.decoder = new ASN1(data); if (this.decoder.Tag != 48) { throw new CryptographicException(X509Certificate.encoding_error); } if (this.decoder[0].Tag != 48) { throw new CryptographicException(X509Certificate.encoding_error); } ASN1 asn = this.decoder[0]; int num = 0; ASN1 asn2 = this.decoder[0][num]; this.version = 1; if (asn2.Tag == 160 && asn2.Count > 0) { this.version += (int)asn2[0].Value[0]; num++; } ASN1 asn3 = this.decoder[0][num++]; if (asn3.Tag != 2) { throw new CryptographicException(X509Certificate.encoding_error); } this.serialnumber = asn3.Value; Array.Reverse(this.serialnumber, 0, this.serialnumber.Length); num++; this.issuer = asn.Element(num++, 48); this.m_issuername = X501.ToString(this.issuer); ASN1 asn4 = asn.Element(num++, 48); ASN1 time = asn4[0]; this.m_from = ASN1Convert.ToDateTime(time); ASN1 time2 = asn4[1]; this.m_until = ASN1Convert.ToDateTime(time2); this.subject = asn.Element(num++, 48); this.m_subject = X501.ToString(this.subject); ASN1 asn5 = asn.Element(num++, 48); ASN1 asn6 = asn5.Element(0, 48); ASN1 asn7 = asn6.Element(0, 6); this.m_keyalgo = ASN1Convert.ToOid(asn7); ASN1 asn8 = asn6[1]; this.m_keyalgoparams = ((asn6.Count <= 1) ? null : asn8.GetBytes()); ASN1 asn9 = asn5.Element(1, 3); int num2 = asn9.Length - 1; this.m_publickey = new byte[num2]; Buffer.BlockCopy(asn9.Value, 1, this.m_publickey, 0, num2); byte[] value = this.decoder[2].Value; this.signature = new byte[value.Length - 1]; Buffer.BlockCopy(value, 1, this.signature, 0, this.signature.Length); asn6 = this.decoder[1]; asn7 = asn6.Element(0, 6); this.m_signaturealgo = ASN1Convert.ToOid(asn7); asn8 = asn6[1]; if (asn8 != null) { this.m_signaturealgoparams = asn8.GetBytes(); } else { this.m_signaturealgoparams = null; } ASN1 asn10 = asn.Element(num, 129); if (asn10 != null) { num++; this.issuerUniqueID = asn10.Value; } ASN1 asn11 = asn.Element(num, 130); if (asn11 != null) { num++; this.subjectUniqueID = asn11.Value; } ASN1 asn12 = asn.Element(num, 163); if (asn12 != null && asn12.Count == 1) { this.extensions = new X509ExtensionCollection(asn12[0]); } else { this.extensions = new X509ExtensionCollection(null); } this.m_encodedcert = (byte[])data.Clone(); } catch (Exception inner) { throw new CryptographicException(X509Certificate.encoding_error, inner); } }
private void Parse(byte[] data) { try { decoder = new ASN1(data); if (decoder.Tag != 48) { throw new CryptographicException(encoding_error); } if (decoder[0].Tag != 48) { throw new CryptographicException(encoding_error); } ASN1 aSN = decoder[0]; int num = 0; ASN1 aSN2 = decoder[0][num]; version = 1; if (aSN2.Tag == 160 && aSN2.Count > 0) { version += aSN2[0].Value[0]; num++; } ASN1 aSN3 = decoder[0][num++]; if (aSN3.Tag != 2) { throw new CryptographicException(encoding_error); } serialnumber = aSN3.Value; Array.Reverse(serialnumber, 0, serialnumber.Length); num++; issuer = aSN.Element(num++, 48); m_issuername = X501.ToString(issuer); ASN1 aSN4 = aSN.Element(num++, 48); ASN1 time = aSN4[0]; m_from = ASN1Convert.ToDateTime(time); ASN1 time2 = aSN4[1]; m_until = ASN1Convert.ToDateTime(time2); subject = aSN.Element(num++, 48); m_subject = X501.ToString(subject); ASN1 aSN5 = aSN.Element(num++, 48); ASN1 aSN6 = aSN5.Element(0, 48); ASN1 asn = aSN6.Element(0, 6); m_keyalgo = ASN1Convert.ToOid(asn); ASN1 aSN7 = aSN6[1]; m_keyalgoparams = ((aSN6.Count <= 1) ? null : aSN7.GetBytes()); ASN1 aSN8 = aSN5.Element(1, 3); int num7 = aSN8.Length - 1; m_publickey = new byte[num7]; Buffer.BlockCopy(aSN8.Value, 1, m_publickey, 0, num7); byte[] value = decoder[2].Value; signature = new byte[value.Length - 1]; Buffer.BlockCopy(value, 1, signature, 0, signature.Length); aSN6 = decoder[1]; asn = aSN6.Element(0, 6); m_signaturealgo = ASN1Convert.ToOid(asn); aSN7 = aSN6[1]; if (aSN7 != null) { m_signaturealgoparams = aSN7.GetBytes(); } else { m_signaturealgoparams = null; } ASN1 aSN9 = aSN.Element(num, 129); if (aSN9 != null) { num++; issuerUniqueID = aSN9.Value; } ASN1 aSN10 = aSN.Element(num, 130); if (aSN10 != null) { num++; subjectUniqueID = aSN10.Value; } ASN1 aSN11 = aSN.Element(num, 163); if (aSN11 != null && aSN11.Count == 1) { extensions = new X509ExtensionCollection(aSN11[0]); } else { extensions = new X509ExtensionCollection(null); } m_encodedcert = (byte[])data.Clone(); } catch (Exception inner) { throw new CryptographicException(encoding_error, inner); IL_035f :; } }