1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
//
// System.Security.Cryptography.Pkcs.SignedCms class
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#if NET_2_0
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography.Xml;
using System.Text;
using Mono.Security;
using Mono.Security.X509;
namespace System.Security.Cryptography.Pkcs {
public sealed class SignedCms {
private ContentInfo _content;
private bool _detached;
private SignerInfoCollection _info;
private X509Certificate2Collection _certs;
private SubjectIdentifierType _type;
private int _version;
// constructors
public SignedCms ()
{
_certs = new X509Certificate2Collection ();
_info = new SignerInfoCollection ();
}
public SignedCms (ContentInfo content)
: this (content, false)
{
}
public SignedCms (ContentInfo content, bool detached)
: this ()
{
if (content == null)
throw new ArgumentNullException ("content");
_content = content;
_detached = detached;
}
public SignedCms (SubjectIdentifierType signerIdentifierType) : this ()
{
_type = signerIdentifierType;
_version = 0;
}
public SignedCms (SubjectIdentifierType signerIdentifierType, ContentInfo content)
: this (content, false)
{
_type = signerIdentifierType;
_version = 0;
}
public SignedCms (SubjectIdentifierType signerIdentifierType, ContentInfo content, bool detached)
: this (content, detached)
{
_type = signerIdentifierType;
_version = 0;
}
// properties
public X509Certificate2Collection Certificates {
get { return _certs; }
}
public ContentInfo ContentInfo {
get {
if (_content == null) {
Oid oid = new Oid (PKCS7.Oid.data);
_content = new ContentInfo (oid, new byte [0]);
}
return _content;
}
}
public bool Detached {
get { return _detached; }
}
public SignerInfoCollection SignerInfos {
get { return _info; }
}
public int Version {
get { return _version; }
}
// methods
public void CheckSignature (bool verifySignatureOnly)
{
foreach (SignerInfo si in _info) {
si.CheckSignature (verifySignatureOnly);
}
}
public void CheckSignature (X509Certificate2Collection extraStore, bool verifySignatureOnly)
{
foreach (SignerInfo si in _info) {
si.CheckSignature (extraStore, verifySignatureOnly);
}
}
[MonoTODO]
public void CheckHash ()
{
throw new InvalidOperationException ("");
}
[MonoTODO]
public void ComputeSignature ()
{
throw new CryptographicException ("");
}
[MonoTODO]
public void ComputeSignature (CmsSigner signer)
{
ComputeSignature ();
}
[MonoTODO]
public void ComputeSignature (CmsSigner signer, bool silent)
{
ComputeSignature ();
}
private string ToString (byte[] array, bool reverse)
{
StringBuilder sb = new StringBuilder ();
if (reverse) {
for (int i=array.Length - 1; i >= 0; i--)
sb.Append (array [i].ToString ("X2"));
} else {
for (int i=0; i < array.Length; i++)
sb.Append (array [i].ToString ("X2"));
}
return sb.ToString ();
}
private byte[] GetKeyIdentifier (Mono.Security.X509.X509Certificate x509)
{
// if present in certificate return value of the SubjectKeyIdentifier
Mono.Security.X509.X509Extension extn = x509.Extensions ["2.5.29.14"];
if (extn != null) {
ASN1 bs = new ASN1 (extn.Value.Value);
return bs.Value;
}
// strangely DEPRECATED keyAttributes isn't used here (like KeyUsage)
// if not then we must calculate the SubjectKeyIdentifier ourselve
// Note: MS does that hash on the complete subjectPublicKeyInfo (unlike PKIX)
// http://groups.google.ca/groups?selm=e7RqM%24plCHA.1488%40tkmsftngp02&oe=UTF-8&output=gplain
ASN1 subjectPublicKeyInfo = new ASN1 (0x30);
ASN1 algo = subjectPublicKeyInfo.Add (new ASN1 (0x30));
algo.Add (new ASN1 (CryptoConfig.EncodeOID (x509.KeyAlgorithm)));
// FIXME: does it work for DSA certs (without an 2.5.29.14 extension ?)
algo.Add (new ASN1 (x509.KeyAlgorithmParameters));
byte[] pubkey = x509.PublicKey;
byte[] bsvalue = new byte [pubkey.Length + 1]; // add unused bits (0) before the public key
Array.Copy (pubkey, 0, bsvalue, 1, pubkey.Length);
subjectPublicKeyInfo.Add (new ASN1 (0x03, bsvalue));
SHA1 sha = SHA1.Create ();
return sha.ComputeHash (subjectPublicKeyInfo.GetBytes ());
}
[MonoTODO("incomplete - missing attributes")]
public void Decode (byte[] encodedMessage)
{
PKCS7.ContentInfo ci = new PKCS7.ContentInfo (encodedMessage);
if (ci.ContentType != PKCS7.Oid.signedData)
throw new Exception ("");
PKCS7.SignedData sd = new PKCS7.SignedData (ci.Content);
SubjectIdentifierType type = SubjectIdentifierType.Unknown;
object o = null;
X509Certificate2 x509 = null;
if (sd.SignerInfo.Certificate != null) {
x509 = new X509Certificate2 (sd.SignerInfo.Certificate.RawData);
}
else if ((sd.SignerInfo.IssuerName != null) && (sd.SignerInfo.SerialNumber != null)) {
byte[] serial = sd.SignerInfo.SerialNumber;
Array.Reverse (serial); // ???
type = SubjectIdentifierType.IssuerAndSerialNumber;
X509IssuerSerial xis = new X509IssuerSerial ();
xis.IssuerName = sd.SignerInfo.IssuerName;
xis.SerialNumber = ToString (serial, true);
o = xis;
// TODO: move to a FindCertificate (issuer, serial, collection)
foreach (Mono.Security.X509.X509Certificate x in sd.Certificates) {
if (x.IssuerName == sd.SignerInfo.IssuerName) {
if (ToString (x.SerialNumber, true) == xis.SerialNumber) {
x509 = new X509Certificate2 (x.RawData);
break;
}
}
}
}
else if (sd.SignerInfo.SubjectKeyIdentifier != null) {
string ski = ToString (sd.SignerInfo.SubjectKeyIdentifier, false);
type = SubjectIdentifierType.SubjectKeyIdentifier;
o = (object) ski;
// TODO: move to a FindCertificate (ski, collection)
foreach (Mono.Security.X509.X509Certificate x in sd.Certificates) {
if (ToString (GetKeyIdentifier (x), false) == ski) {
x509 = new X509Certificate2 (x.RawData);
break;
}
}
}
SignerInfo si = new SignerInfo (sd.SignerInfo.HashName, x509, type, o, sd.SignerInfo.Version);
// si.AuthenticatedAttributes
// si.UnauthenticatedAttributes
_info.Add (si);
ASN1 content = sd.ContentInfo.Content;
Oid oid = new Oid (sd.ContentInfo.ContentType);
_content = new ContentInfo (oid, content[0].Value);
foreach (Mono.Security.X509.X509Certificate x in sd.Certificates) {
_certs.Add (new X509Certificate2 (x.RawData));
}
_version = sd.Version;
}
[MonoTODO]
public byte[] Encode ()
{
/* Mono.Security.X509.X509Certificate x509 = null;
Cms.SignerInfo si = new Cms.SignerInfo ();
switch (_type) {
case SubjectIdentifierType.SubjectKeyIdentifier:
si.SubjectKeyIdentifier = GetKeyIdentifier (x509);
break;
default:
// SubjectIdentifierType.IssuerAndSerialNumber
si.IssuerName = x509.IssuerName;
si.SerialNumber = x509.SerialNumber;
break;
}
Cms.SignedData sd = new Cms.SignedData ();
sd.Version = _version;
sd.SignerInfo = si;
Cms.ContentInfo ci = new Cms.ContentInfo (Cms.signedData);
ci.Content = sd.ASN1;
return ci.GetBytes ();*/
return null;
}
// counterSsignerInfo -> counterSignerInfo
[MonoTODO]
public void RemoveSignature (SignerInfo counterSsignerInfo)
{
}
[MonoTODO]
public void RemoveSignature (int index)
{
}
}
}
#endif
|