Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Base / MS / Internal / IO / Packaging / Certificate.cs / 1305600 / Certificate.cs
//------------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description:
// Handles serialization to/from X509 Certificate part (X509v3 = ASN.1 DER format)
//
// History:
// 03/22/2004: BruceMac: Initial Implementation
//
//-----------------------------------------------------------------------------
using System;
using System.Diagnostics; // for Assert
using System.Security.Cryptography.X509Certificates;
using System.Windows; // For Exception strings - SRID
using System.IO.Packaging;
using System.IO; // for Stream
using MS.Internal; // For ContentType
using MS.Internal.WindowsBase;
namespace MS.Internal.IO.Packaging
{
///
/// CertificatePart
///
internal class CertificatePart
{
#region Internal Members
//-----------------------------------------------------
//
// Internal Properties
//
//-----------------------------------------------------
///
/// Type of relationship to a Certificate Part
///
static internal string RelationshipType
{
get
{
return _certificatePartRelationshipType;
}
}
///
/// Prefix of auto-generated Certificate Part names
///
static internal string PartNamePrefix
{
get
{
return _certificatePartNamePrefix;
}
}
///
/// Extension of Certificate Part file names
///
static internal string PartNameExtension
{
get
{
return _certificatePartNameExtension;
}
}
///
/// ContentType of Certificate Parts
///
static internal ContentType ContentType
{
get
{
return _certificatePartContentType;
}
}
internal Uri Uri
{
get
{
return _part.Uri;
}
}
//------------------------------------------------------
//
// Internal Methods
//
//-----------------------------------------------------
///
/// Certificate to associate with this Certificate Part
///
///
/// stream is too large
internal X509Certificate2 GetCertificate()
{
// lazy init
if (_certificate == null)
{
// obtain from the part
using (Stream s = _part.GetStream())
{
if (s.Length > 0)
{
// throw if stream is beyond any reasonable length
if (s.Length > _maximumCertificateStreamLength)
throw new FileFormatException(SR.Get(SRID.CorruptedData));
// X509Certificate constructor desires a byte array
Byte[] byteArray = new Byte[s.Length];
PackagingUtilities.ReliableRead(s, byteArray, 0, (int)s.Length);
_certificate = new X509Certificate2(byteArray);
}
}
}
return _certificate;
}
internal void SetCertificate(X509Certificate2 certificate)
{
if (certificate == null)
throw new ArgumentNullException("certificate");
_certificate = certificate;
// persist to the part
Byte[] byteArray = _certificate.GetRawCertData();
// FileMode.Create will ensure that the stream will shrink if overwritten
using (Stream s = _part.GetStream(FileMode.Create, FileAccess.Write))
{
s.Write(byteArray, 0, byteArray.Length);
}
}
///
/// CertificatePart constructor
///
internal CertificatePart(Package container, Uri partName)
{
if (container == null)
throw new ArgumentNullException("container");
if (partName == null)
throw new ArgumentNullException("partName");
partName = PackUriHelper.ValidatePartUri(partName);
// create if not found
if (container.PartExists(partName))
{
// open the part
_part = container.GetPart(partName);
// ensure the part is of the expected type
if (_part.ValidatedContentType.AreTypeAndSubTypeEqual(_certificatePartContentType) == false)
throw new FileFormatException(SR.Get(SRID.CertificatePartContentTypeMismatch));
}
else
{
// create the part
_part = container.CreatePart(partName, _certificatePartContentType.ToString());
}
}
//------------------------------------------------------
//
// Private Fields
//
//------------------------------------------------------
private PackagePart _part; // part that houses the certificate
private X509Certificate2 _certificate; // certificate itself
// certificate part constants
private static readonly ContentType _certificatePartContentType =
new ContentType("application/vnd.openxmlformats-package.digital-signature-certificate");
private static readonly string _certificatePartNamePrefix = "/package/services/digital-signature/certificate/";
private static readonly string _certificatePartNameExtension = ".cer";
private static readonly string _certificatePartRelationshipType = "http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/certificate";
private static long _maximumCertificateStreamLength = 0x40000; // 4MB
#endregion Private Members
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
// Description:
// Handles serialization to/from X509 Certificate part (X509v3 = ASN.1 DER format)
//
// History:
// 03/22/2004: BruceMac: Initial Implementation
//
//-----------------------------------------------------------------------------
using System;
using System.Diagnostics; // for Assert
using System.Security.Cryptography.X509Certificates;
using System.Windows; // For Exception strings - SRID
using System.IO.Packaging;
using System.IO; // for Stream
using MS.Internal; // For ContentType
using MS.Internal.WindowsBase;
namespace MS.Internal.IO.Packaging
{
///
/// CertificatePart
///
internal class CertificatePart
{
#region Internal Members
//-----------------------------------------------------
//
// Internal Properties
//
//-----------------------------------------------------
///
/// Type of relationship to a Certificate Part
///
static internal string RelationshipType
{
get
{
return _certificatePartRelationshipType;
}
}
///
/// Prefix of auto-generated Certificate Part names
///
static internal string PartNamePrefix
{
get
{
return _certificatePartNamePrefix;
}
}
///
/// Extension of Certificate Part file names
///
static internal string PartNameExtension
{
get
{
return _certificatePartNameExtension;
}
}
///
/// ContentType of Certificate Parts
///
static internal ContentType ContentType
{
get
{
return _certificatePartContentType;
}
}
internal Uri Uri
{
get
{
return _part.Uri;
}
}
//------------------------------------------------------
//
// Internal Methods
//
//-----------------------------------------------------
///
/// Certificate to associate with this Certificate Part
///
///
/// stream is too large
internal X509Certificate2 GetCertificate()
{
// lazy init
if (_certificate == null)
{
// obtain from the part
using (Stream s = _part.GetStream())
{
if (s.Length > 0)
{
// throw if stream is beyond any reasonable length
if (s.Length > _maximumCertificateStreamLength)
throw new FileFormatException(SR.Get(SRID.CorruptedData));
// X509Certificate constructor desires a byte array
Byte[] byteArray = new Byte[s.Length];
PackagingUtilities.ReliableRead(s, byteArray, 0, (int)s.Length);
_certificate = new X509Certificate2(byteArray);
}
}
}
return _certificate;
}
internal void SetCertificate(X509Certificate2 certificate)
{
if (certificate == null)
throw new ArgumentNullException("certificate");
_certificate = certificate;
// persist to the part
Byte[] byteArray = _certificate.GetRawCertData();
// FileMode.Create will ensure that the stream will shrink if overwritten
using (Stream s = _part.GetStream(FileMode.Create, FileAccess.Write))
{
s.Write(byteArray, 0, byteArray.Length);
}
}
///
/// CertificatePart constructor
///
internal CertificatePart(Package container, Uri partName)
{
if (container == null)
throw new ArgumentNullException("container");
if (partName == null)
throw new ArgumentNullException("partName");
partName = PackUriHelper.ValidatePartUri(partName);
// create if not found
if (container.PartExists(partName))
{
// open the part
_part = container.GetPart(partName);
// ensure the part is of the expected type
if (_part.ValidatedContentType.AreTypeAndSubTypeEqual(_certificatePartContentType) == false)
throw new FileFormatException(SR.Get(SRID.CertificatePartContentTypeMismatch));
}
else
{
// create the part
_part = container.CreatePart(partName, _certificatePartContentType.ToString());
}
}
//------------------------------------------------------
//
// Private Fields
//
//------------------------------------------------------
private PackagePart _part; // part that houses the certificate
private X509Certificate2 _certificate; // certificate itself
// certificate part constants
private static readonly ContentType _certificatePartContentType =
new ContentType("application/vnd.openxmlformats-package.digital-signature-certificate");
private static readonly string _certificatePartNamePrefix = "/package/services/digital-signature/certificate/";
private static readonly string _certificatePartNameExtension = ".cer";
private static readonly string _certificatePartRelationshipType = "http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/certificate";
private static long _maximumCertificateStreamLength = 0x40000; // 4MB
#endregion Private Members
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PbrsForward.cs
- TrustManagerMoreInformation.cs
- handlecollector.cs
- FormViewInsertEventArgs.cs
- Configuration.cs
- CompilerHelpers.cs
- ScrollContentPresenter.cs
- OptimalTextSource.cs
- GlobalProxySelection.cs
- SoapCodeExporter.cs
- DesignerAdRotatorAdapter.cs
- StylusPointPropertyId.cs
- TypeSystemHelpers.cs
- TreeViewImageIndexConverter.cs
- TypeDependencyAttribute.cs
- TabPage.cs
- PointAnimation.cs
- ClientSession.cs
- DSACryptoServiceProvider.cs
- PropertyMetadata.cs
- IssuedTokenClientBehaviorsElement.cs
- ScrollContentPresenter.cs
- DesignSurfaceManager.cs
- ConnectionConsumerAttribute.cs
- ModelProperty.cs
- CachedPathData.cs
- DetailsViewRowCollection.cs
- NoClickablePointException.cs
- XmlSerializerAssemblyAttribute.cs
- ConnectionPointCookie.cs
- RoleBoolean.cs
- CodeComment.cs
- BufferedGraphicsContext.cs
- DataViewSettingCollection.cs
- BaseResourcesBuildProvider.cs
- DbConnectionInternal.cs
- DetailsViewActionList.cs
- DbException.cs
- GenerateTemporaryTargetAssembly.cs
- TextEditorTables.cs
- SecurityTraceRecordHelper.cs
- WebBrowserDocumentCompletedEventHandler.cs
- InkCanvas.cs
- ObjectQuery_EntitySqlExtensions.cs
- TransformConverter.cs
- XmlUtil.cs
- ToolStripPanelRow.cs
- EventToken.cs
- XmlSchemaGroup.cs
- OracleSqlParser.cs
- SchemaDeclBase.cs
- SHA512.cs
- InkCanvasFeedbackAdorner.cs
- SemaphoreFullException.cs
- _AutoWebProxyScriptWrapper.cs
- ParseElementCollection.cs
- HierarchicalDataBoundControl.cs
- InternalControlCollection.cs
- DetailsViewCommandEventArgs.cs
- CodeBlockBuilder.cs
- DataGridViewSelectedCellsAccessibleObject.cs
- TreeViewHitTestInfo.cs
- LiteralControl.cs
- XmlC14NWriter.cs
- XmlCustomFormatter.cs
- XmlSchemaCollection.cs
- Point3DAnimationUsingKeyFrames.cs
- WebPartsPersonalization.cs
- PageThemeBuildProvider.cs
- XPathNavigator.cs
- XMLDiffLoader.cs
- SQLUtility.cs
- PolyBezierSegment.cs
- Decimal.cs
- BinarySecretSecurityToken.cs
- DbConnectionStringBuilder.cs
- PortCache.cs
- ResourceProviderFactory.cs
- SubqueryTrackingVisitor.cs
- objectquery_tresulttype.cs
- EnumerableRowCollectionExtensions.cs
- ProfileSettings.cs
- EventLogPermissionHolder.cs
- ToolStripRenderer.cs
- CompilationRelaxations.cs
- ForAllOperator.cs
- Translator.cs
- XmlSchemaAnnotated.cs
- PasswordBoxAutomationPeer.cs
- FloaterParaClient.cs
- QilPatternVisitor.cs
- TreeViewAutomationPeer.cs
- KeyValueSerializer.cs
- SessionStateModule.cs
- Variant.cs
- DesignerWithHeader.cs
- SByte.cs
- SqlServices.cs
- MasterPageCodeDomTreeGenerator.cs
- VoiceInfo.cs