public override string ToString(bool verbose)
        {
            if (_cert == null)
            {
                return("System.Security.Cryptography.X509Certificates.X509Certificate2");
            }

            string        nl = Environment.NewLine;
            StringBuilder sb = new StringBuilder();

            // the non-verbose X509Certificate2 == verbose X509Certificate
            if (!verbose)
            {
                sb.AppendFormat("[Subject]{0}  {1}{0}{0}", nl, GetSubjectName(false));
                sb.AppendFormat("[Issuer]{0}  {1}{0}{0}", nl, GetIssuerName(false));
                sb.AppendFormat("[Not Before]{0}  {1}{0}{0}", nl, GetValidFrom().ToLocalTime());
                sb.AppendFormat("[Not After]{0}  {1}{0}{0}", nl, GetValidUntil().ToLocalTime());
                sb.AppendFormat("[Thumbprint]{0}  {1}{0}", nl, X509Helper.ToHexString(GetCertHash()));
                sb.Append(nl);
                return(sb.ToString());
            }

            sb.AppendFormat("[Version]{0}  V{1}{0}{0}", nl, Version);
            sb.AppendFormat("[Subject]{0}  {1}{0}{0}", nl, GetSubjectName(false));
            sb.AppendFormat("[Issuer]{0}  {1}{0}{0}", nl, GetIssuerName(false));
            sb.AppendFormat("[Serial Number]{0}  {1}{0}{0}", nl, GetSerialNumber());
            sb.AppendFormat("[Not Before]{0}  {1}{0}{0}", nl, GetValidFrom().ToLocalTime());
            sb.AppendFormat("[Not After]{0}  {1}{0}{0}", nl, GetValidUntil().ToLocalTime());
            sb.AppendFormat("[Thumbprint]{0}  {1}{0}", nl, X509Helper.ToHexString(GetCertHash()));
            sb.AppendFormat("[Signature Algorithm]{0}  {1}({2}){0}{0}", nl, SignatureAlgorithm.FriendlyName,
                            SignatureAlgorithm.Value);

            AsymmetricAlgorithm key = PublicKey.Key;

            sb.AppendFormat("[Public Key]{0}  Algorithm: ", nl);
            if (key is RSA)
            {
                sb.Append("RSA");
            }
            else if (key is DSA)
            {
                sb.Append("DSA");
            }
            else
            {
                sb.Append(key.ToString());
            }
            sb.AppendFormat("{0}  Length: {1}{0}  Key Blob: ", nl, key.KeySize);
            AppendBuffer(sb, PublicKey.EncodedKeyValue.RawData);
            sb.AppendFormat("{0}  Parameters: ", nl);
            AppendBuffer(sb, PublicKey.EncodedParameters.RawData);
            sb.Append(nl);

            return(sb.ToString());
        }
Example #2
0
 public static byte[] Encode(AsymmetricAlgorithm aa)
 {
     if (aa is RSA)
     {
         return(Encode((RSA)aa));
     }
     if (aa is DSA)
     {
         return(Encode((DSA)aa));
     }
     throw new CryptographicException("Unknown asymmetric algorithm {0}", aa.ToString());
 }
Example #3
0
 public virtual byte[] Sign(AsymmetricAlgorithm aa)
 {
     if (aa is RSA)
     {
         return(this.Sign(aa as RSA));
     }
     if (aa is DSA)
     {
         return(this.Sign(aa as DSA));
     }
     throw new NotSupportedException("Unknown Asymmetric Algorithm " + aa.ToString());
 }
Example #4
0
		public bool VerifySignature (AsymmetricAlgorithm aa) 
		{
			if (aa == null)
				throw new ArgumentNullException ("aa");

			if (aa is RSA)
				return VerifySignature (aa as RSA);
			else if (aa is DSA)
				return VerifySignature (aa as DSA);
			else 
				throw new NotSupportedException ("Unknown Asymmetric Algorithm " + aa.ToString ());
		}
 public bool VerifySignature(AsymmetricAlgorithm aa)
 {
     if (aa == null)
     {
         throw new ArgumentNullException("aa");
     }
     if (aa is System.Security.Cryptography.RSA)
     {
         return(this.VerifySignature(aa as System.Security.Cryptography.RSA));
     }
     if (!(aa is System.Security.Cryptography.DSA))
     {
         throw new NotSupportedException("Unknown Asymmetric Algorithm " + aa.ToString());
     }
     return(this.VerifySignature(aa as System.Security.Cryptography.DSA));
 }
Example #6
0
        /// <summary>Displays an X.509 certificate in text format.</summary>
        /// <returns>The certificate information.</returns>
        /// <param name="verbose">true to display the public key, private key, extensions, and so forth; false to display information that is similar to the <see cref="T:System.Security.Cryptography.X509Certificates.X509Certificate2" /> class, including thumbprint, serial number, subject and issuer names, and so on. </param>
        public override string ToString(bool verbose)
        {
            if (this._cert == null)
            {
                return("System.Security.Cryptography.X509Certificates.X509Certificate2");
            }
            if (!verbose)
            {
                return(base.ToString(true));
            }
            string        newLine       = Environment.NewLine;
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.AppendFormat("[Version]{0}  V{1}{0}{0}", newLine, this.Version);
            stringBuilder.AppendFormat("[Subject]{0}  {1}{0}{0}", newLine, base.Subject);
            stringBuilder.AppendFormat("[Issuer]{0}  {1}{0}{0}", newLine, base.Issuer);
            stringBuilder.AppendFormat("[Serial Number]{0}  {1}{0}{0}", newLine, this.SerialNumber);
            stringBuilder.AppendFormat("[Not Before]{0}  {1}{0}{0}", newLine, this.NotBefore);
            stringBuilder.AppendFormat("[Not After]{0}  {1}{0}{0}", newLine, this.NotAfter);
            stringBuilder.AppendFormat("[Thumbprint]{0}  {1}{0}{0}", newLine, this.Thumbprint);
            stringBuilder.AppendFormat("[Signature Algorithm]{0}  {1}({2}){0}{0}", newLine, this.SignatureAlgorithm.FriendlyName, this.SignatureAlgorithm.Value);
            AsymmetricAlgorithm key = this.PublicKey.Key;

            stringBuilder.AppendFormat("[Public Key]{0}  Algorithm: ", newLine);
            if (key is RSA)
            {
                stringBuilder.Append("RSA");
            }
            else if (key is DSA)
            {
                stringBuilder.Append("DSA");
            }
            else
            {
                stringBuilder.Append(key.ToString());
            }
            stringBuilder.AppendFormat("{0}  Length: {1}{0}  Key Blob: ", newLine, key.KeySize);
            X509Certificate2.AppendBuffer(stringBuilder, this.PublicKey.EncodedKeyValue.RawData);
            stringBuilder.AppendFormat("{0}  Parameters: ", newLine);
            X509Certificate2.AppendBuffer(stringBuilder, this.PublicKey.EncodedParameters.RawData);
            stringBuilder.Append(newLine);
            return(stringBuilder.ToString());
        }
        /* SubjectPublicKeyInfo  ::=  SEQUENCE  {
         *      algorithm            AlgorithmIdentifier,
         *      subjectPublicKey     BIT STRING  }
         */
        private ASN1 SubjectPublicKeyInfo()
        {
            ASN1 keyInfo = new ASN1(0x30);

            if (aa is RSA)
            {
                keyInfo.Add(PKCS7.AlgorithmIdentifier("1.2.840.113549.1.1.1"));
                RSAParameters p = (aa as RSA).ExportParameters(false);

                /* RSAPublicKey ::= SEQUENCE {
                 *       modulus            INTEGER,    -- n
                 *       publicExponent     INTEGER  }  -- e
                 */
                ASN1 key = new ASN1(0x30);
                key.Add(ASN1Convert.FromUnsignedBigInteger(p.Modulus));
                key.Add(ASN1Convert.FromUnsignedBigInteger(p.Exponent));
                keyInfo.Add(new ASN1(UniqueIdentifier(key.GetBytes())));
            }
            else if (aa is DSA)
            {
                DSAParameters p = (aa as DSA).ExportParameters(false);

                /* Dss-Parms  ::=  SEQUENCE  {
                 *       p             INTEGER,
                 *       q             INTEGER,
                 *       g             INTEGER  }
                 */
                ASN1 param = new ASN1(0x30);
                param.Add(ASN1Convert.FromUnsignedBigInteger(p.P));
                param.Add(ASN1Convert.FromUnsignedBigInteger(p.Q));
                param.Add(ASN1Convert.FromUnsignedBigInteger(p.G));
                keyInfo.Add(PKCS7.AlgorithmIdentifier("1.2.840.10040.4.1", param));
                ASN1 key = keyInfo.Add(new ASN1(0x03));
                // DSAPublicKey ::= INTEGER  -- public key, y
                key.Add(ASN1Convert.FromUnsignedBigInteger(p.Y));
            }
            else
            {
                throw new NotSupportedException("Unknown Asymmetric Algorithm " + aa.ToString());
            }
            return(keyInfo);
        }
Example #8
0
        public bool VerifySignature(AsymmetricAlgorithm aa)
        {
            if (aa == null)
            {
                throw new ArgumentNullException("aa");
            }

            // only validate the signature (in case we don't have the CA certificate)
            if (aa is RSA)
            {
                return(VerifySignature(aa as RSA));
            }
            else if (aa is DSA)
            {
                return(VerifySignature(aa as DSA));
            }
            else
            {
                throw new NotSupportedException("Unknown Asymmetric Algorithm " + aa.ToString());
            }
        }
Example #9
0
            public static byte[] Encode(AsymmetricAlgorithm aa)
            {
                switch (aa)
                {
                case RSA _:
                    return(PKCS8.PrivateKeyInfo.Encode((RSA)aa));

                case DSA _:
                    return(PKCS8.PrivateKeyInfo.Encode((DSA)aa));

                default:
                    throw new CryptographicException("Unknown asymmetric algorithm {0}", aa.ToString());
                }
            }
Example #10
0
        private ASN1 SubjectPublicKeyInfo()
        {
            ASN1 aSN = new ASN1(48);

            if (aa is RSA)
            {
                aSN.Add(PKCS7.AlgorithmIdentifier("1.2.840.113549.1.1.1"));
                RSAParameters rSAParameters = (aa as RSA).ExportParameters(includePrivateParameters: false);
                ASN1          aSN2          = new ASN1(48);
                aSN2.Add(ASN1Convert.FromUnsignedBigInteger(rSAParameters.Modulus));
                aSN2.Add(ASN1Convert.FromUnsignedBigInteger(rSAParameters.Exponent));
                aSN.Add(new ASN1(UniqueIdentifier(aSN2.GetBytes())));
            }
            else
            {
                if (!(aa is DSA))
                {
                    throw new NotSupportedException("Unknown Asymmetric Algorithm " + aa.ToString());
                }
                DSAParameters dSAParameters = (aa as DSA).ExportParameters(includePrivateParameters: false);
                ASN1          aSN3          = new ASN1(48);
                aSN3.Add(ASN1Convert.FromUnsignedBigInteger(dSAParameters.P));
                aSN3.Add(ASN1Convert.FromUnsignedBigInteger(dSAParameters.Q));
                aSN3.Add(ASN1Convert.FromUnsignedBigInteger(dSAParameters.G));
                aSN.Add(PKCS7.AlgorithmIdentifier("1.2.840.10040.4.1", aSN3));
                ASN1 aSN4 = aSN.Add(new ASN1(3));
                aSN4.Add(ASN1Convert.FromUnsignedBigInteger(dSAParameters.Y));
            }
            return(aSN);
        }