Beispiel #1
0
        public static SignatureAlgorithmDataRecord FromSignatureAlgorithm(ISignatureAlgorithm signatureAlgorithm)
        {
            if (signatureAlgorithm == null)
            {
                throw new ArgumentNullException(nameof(signatureAlgorithm));
            }

            switch (signatureAlgorithm)
            {
            case RSASignatureAlgorithm rsa:
                return(new SignatureAlgorithmDataRecord {
                    Type = rsa.Name,
                    HashAlgorithm = rsa.HashAlgorithm.Name,
                    Parameter = rsa.GetPublicKey().ToXml()
                });

            case ECDsaSignatureAlgorithm ecdsa:
                return(new SignatureAlgorithmDataRecord {
                    Type = ecdsa.Name,
                    HashAlgorithm = ecdsa.HashAlgorithm.Name,
                    Parameter = ecdsa.GetPublicKey().ToXml()
                });

            case HMACSignatureAlgorithm hmac:
                return(new SignatureAlgorithmDataRecord {
                    Type = hmac.Name,
                    HashAlgorithm = hmac.HashAlgorithm.Name,
                    Parameter = Encoding.UTF8.GetString(hmac.Key)
                });

            default:
                throw new NotSupportedException($"The specified signature algorithm of type {signatureAlgorithm.GetType().Name} cannot be serialized.");
            }
        }
Beispiel #2
0
        public void SetSignatureAlgorithm(ClientDataRecord dataRecord, ISignatureAlgorithm signatureAlgorithm, SharedSecretEncryptionKey encryptionKey)
        {
            if (dataRecord == null)
            {
                throw new ArgumentNullException(nameof(dataRecord));
            }
            if (signatureAlgorithm == null)
            {
                throw new ArgumentNullException(nameof(signatureAlgorithm));
            }

            switch (signatureAlgorithm)
            {
            case RSASignatureAlgorithm rsa:
                dataRecord.SigType                 = rsa.Name;
                dataRecord.SigHashAlgorithm        = rsa.HashAlgorithm.Name;
                dataRecord.SigParameter            = rsa.GetPublicKey().ToXml();
                dataRecord.IsSigParameterEncrypted = false;
                break;

            case ECDsaSignatureAlgorithm ecdsa:
                dataRecord.SigType                 = ecdsa.Name;
                dataRecord.SigHashAlgorithm        = ecdsa.HashAlgorithm.Name;
                dataRecord.SigParameter            = ecdsa.GetPublicKey().ToXml();
                dataRecord.IsSigParameterEncrypted = false;
                break;

            case HMACSignatureAlgorithm hmac:
                dataRecord.SigType                 = hmac.Name;
                dataRecord.SigHashAlgorithm        = hmac.HashAlgorithm.Name;
                dataRecord.SigParameter            = GetParameterWithEncryption(hmac, encryptionKey, out var isEncrypted);
                dataRecord.IsSigParameterEncrypted = isEncrypted;
                break;

            default:
                throw new NotSupportedException($"The specified signature algorithm of type {signatureAlgorithm.GetType().Name} cannot be serialized.");
            }
        }
Beispiel #3
0
        public SignatureAlgorithmDataRecord FromSignatureAlgorithm(ISignatureAlgorithm signatureAlgorithm, SharedSecretEncryptionKey encryptionKey)
        {
            if (signatureAlgorithm == null)
            {
                throw new ArgumentNullException(nameof(signatureAlgorithm));
            }

            switch (signatureAlgorithm)
            {
            case RSASignatureAlgorithm rsa:
                return(new SignatureAlgorithmDataRecord {
                    Type = rsa.Name,
                    Hash = rsa.HashAlgorithm.Name,
                    Param = rsa.GetPublicKey().ToXml(),
                    Encrypted = false
                });

            case ECDsaSignatureAlgorithm ecdsa:
                return(new SignatureAlgorithmDataRecord {
                    Type = ecdsa.Name,
                    Hash = ecdsa.HashAlgorithm.Name,
                    Param = ecdsa.GetPublicKey().ToXml(),
                    Encrypted = false
                });

            case HMACSignatureAlgorithm hmac:
                var paramValue = GetParameterWithEncryption(hmac, encryptionKey, out var isEncrypted);
                var xmlString  = new XElement("Secret", paramValue).ToString();
                return(new SignatureAlgorithmDataRecord {
                    Type = hmac.Name,
                    Hash = hmac.HashAlgorithm.Name,
                    Param = xmlString,
                    Encrypted = isEncrypted
                });

            default:
                throw new NotSupportedException($"The specified signature algorithm of type {signatureAlgorithm.GetType().Name} cannot be serialized.");
            }
        }