TlsClientCertificateVerify.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Transport Security Layer (TLS)
  2. // Copyright (c) 2003-2004 Carlos Guzman Alvarez
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Security.Cryptography.X509Certificates;
  25. using System.Security.Cryptography;
  26. using Mono.Security.Cryptography;
  27. namespace Mono.Security.Protocol.Tls.Handshake.Client
  28. {
  29. internal class TlsClientCertificateVerify : HandshakeMessage
  30. {
  31. #region Constructors
  32. public TlsClientCertificateVerify(Context context)
  33. : base(context, HandshakeType.CertificateVerify)
  34. {
  35. }
  36. #endregion
  37. #region Methods
  38. public override void Update()
  39. {
  40. base.Update();
  41. this.Reset();
  42. }
  43. #endregion
  44. #region Protected Methods
  45. protected override void ProcessAsSsl3()
  46. {
  47. AsymmetricAlgorithm privKey = null;
  48. ClientContext context = (ClientContext)this.Context;
  49. privKey = context.SslStream.RaisePrivateKeySelection(
  50. context.ClientSettings.ClientCertificate,
  51. context.ClientSettings.TargetHost);
  52. if (privKey == null)
  53. {
  54. throw new TlsException(AlertDescription.UserCancelled, "Client certificate Private Key unavailable.");
  55. }
  56. else
  57. {
  58. SslHandshakeHash hash = new SslHandshakeHash(context.MasterSecret);
  59. hash.TransformFinalBlock(
  60. context.HandshakeMessages.ToArray(),
  61. 0,
  62. (int)context.HandshakeMessages.Length);
  63. // CreateSignature uses ((RSA)privKey).DecryptValue which is not implemented
  64. // in RSACryptoServiceProvider. Other implementations likely implement DecryptValue
  65. // so we will try the CreateSignature method.
  66. byte[] signature = null;
  67. if (!(privKey is RSACryptoServiceProvider))
  68. {
  69. try
  70. {
  71. signature = hash.CreateSignature((RSA)privKey);
  72. }
  73. catch (NotImplementedException)
  74. { }
  75. }
  76. // If DecryptValue is not implemented, then try to export the private
  77. // key and let the RSAManaged class do the DecryptValue
  78. if (signature == null)
  79. {
  80. // RSAManaged of the selected ClientCertificate
  81. // (at this moment the first one)
  82. RSA rsa = this.getClientCertRSA((RSA)privKey);
  83. // Write message
  84. signature = hash.CreateSignature(rsa);
  85. }
  86. this.Write((short)signature.Length);
  87. this.Write(signature, 0, signature.Length);
  88. }
  89. }
  90. protected override void ProcessAsTls1()
  91. {
  92. AsymmetricAlgorithm privKey = null;
  93. ClientContext context = (ClientContext)this.Context;
  94. privKey = context.SslStream.RaisePrivateKeySelection(
  95. context.ClientSettings.ClientCertificate,
  96. context.ClientSettings.TargetHost);
  97. if (privKey == null)
  98. {
  99. throw new TlsException(AlertDescription.UserCancelled, "Client certificate Private Key unavailable.");
  100. }
  101. else
  102. {
  103. // Compute handshake messages hash
  104. MD5SHA1 hash = new MD5SHA1();
  105. hash.ComputeHash(
  106. context.HandshakeMessages.ToArray(),
  107. 0,
  108. (int)context.HandshakeMessages.Length);
  109. // CreateSignature uses ((RSA)privKey).DecryptValue which is not implemented
  110. // in RSACryptoServiceProvider. Other implementations likely implement DecryptValue
  111. // so we will try the CreateSignature method.
  112. byte[] signature = null;
  113. if (!(privKey is RSACryptoServiceProvider))
  114. {
  115. try
  116. {
  117. signature = hash.CreateSignature((RSA)privKey);
  118. }
  119. catch (NotImplementedException)
  120. { }
  121. }
  122. // If DecryptValue is not implemented, then try to export the private
  123. // key and let the RSAManaged class do the DecryptValue
  124. if (signature == null)
  125. {
  126. // RSAManaged of the selected ClientCertificate
  127. // (at this moment the first one)
  128. RSA rsa = this.getClientCertRSA((RSA)privKey);
  129. // Write message
  130. signature = hash.CreateSignature(rsa);
  131. }
  132. this.Write((short)signature.Length);
  133. this.Write(signature, 0, signature.Length);
  134. }
  135. }
  136. #endregion
  137. #region Private methods
  138. private RSA getClientCertRSA(RSA privKey)
  139. {
  140. RSAParameters rsaParams = new RSAParameters();
  141. RSAParameters privateParams = privKey.ExportParameters(true);
  142. // for RSA m_publickey contains 2 ASN.1 integers
  143. // the modulus and the public exponent
  144. ASN1 pubkey = new ASN1 (this.Context.ClientSettings.Certificates[0].GetPublicKey());
  145. ASN1 modulus = pubkey [0];
  146. if ((modulus == null) || (modulus.Tag != 0x02))
  147. {
  148. return null;
  149. }
  150. ASN1 exponent = pubkey [1];
  151. if (exponent.Tag != 0x02)
  152. {
  153. return null;
  154. }
  155. rsaParams.Modulus = this.getUnsignedBigInteger(modulus.Value);
  156. rsaParams.Exponent = exponent.Value;
  157. // Set private key parameters
  158. rsaParams.D = privateParams.D;
  159. rsaParams.DP = privateParams.DP;
  160. rsaParams.DQ = privateParams.DQ;
  161. rsaParams.InverseQ = privateParams.InverseQ;
  162. rsaParams.P = privateParams.P;
  163. rsaParams.Q = privateParams.Q;
  164. // BUG: MS BCL 1.0 can't import a key which
  165. // isn't the same size as the one present in
  166. // the container.
  167. int keySize = (rsaParams.Modulus.Length << 3);
  168. RSAManaged rsa = new RSAManaged(keySize);
  169. rsa.ImportParameters (rsaParams);
  170. return (RSA)rsa;
  171. }
  172. private byte[] getUnsignedBigInteger(byte[] integer)
  173. {
  174. if (integer [0] == 0x00)
  175. {
  176. // this first byte is added so we're sure it's an unsigned integer
  177. // however we can't feed it into RSAParameters or DSAParameters
  178. int length = integer.Length - 1;
  179. byte[] uinteger = new byte [length];
  180. Buffer.BlockCopy (integer, 1, uinteger, 0, length);
  181. return uinteger;
  182. }
  183. else
  184. {
  185. return integer;
  186. }
  187. }
  188. #endregion
  189. }
  190. }