Ejemplo n.º 1
0
        EncryptedPackageEnvelope( 
            string envelopeFileName, 
            PublishLicense publishLicense,
            CryptoProvider cryptoProvider 
            )
        {
            if (envelopeFileName == null)
                throw new ArgumentNullException("envelopeFileName"); 

            ThrowIfRMEncryptionInfoInvalid(publishLicense, cryptoProvider); 
 

            _root = StorageRoot.Open( 
                                    envelopeFileName,
                                    _defaultFileModeForCreate,
                                    _defaultFileAccess,
                                    _defaultFileShare 
                                    );
 
            InitializeRMForCreate(publishLicense, cryptoProvider); 
            EmbedPackage(null);
        } 
Ejemplo n.º 2
0
        internal PublishLicense SignIssuanceLicense(IssuanceLicense issuanceLicense, out UseLicense authorUseLicense)
        {
            CheckDisposed();

            Invariant.Assert(issuanceLicense != null);
            Invariant.Assert(!_envHandle.IsInvalid);

            using (CallbackHandler signIssuanceLicenseCallbackHandler = new CallbackHandler())
            {
                string clientLicensorCertificate = GetClientLicensorCert();

                if (clientLicensorCertificate == null)
                    throw new RightsManagementException(SR.Get(SRID.UserHasNoClientLicensorCert));

                // Trim all the leading and trailing white space characters
                // of the clientLicensorCertificate.
                clientLicensorCertificate = clientLicensorCertificate.Trim();

                // Make sure the clientLicensorCertificate is valid. By trimming white spaces
                // above, if the certificate string is empty or contains only white spaces, it
                // is empty now.
                if (clientLicensorCertificate.Length == 0)
                    throw new RightsManagementException(SR.Get(SRID.UserHasNoClientLicensorCert));

                // Offline publishing supported no Online publishing support 
                int hr = SafeNativeMethods.DRMGetSignedIssuanceLicense(
                    _envHandle,
                    issuanceLicense.Handle,
                    (uint)(SignIssuanceLicenseFlags.Offline |
                                SignIssuanceLicenseFlags.AutoGenerateKey |
                                SignIssuanceLicenseFlags.OwnerLicenseNoPersist),
                    null,
                    0,
                    NativeConstants.ALGORITHMID_AES,     // currently AES is the only supported key type 
                    clientLicensorCertificate,
                    signIssuanceLicenseCallbackHandler.CallbackDelegate,
                    null, // we are only supporting offline publishing no url needed
                    0); // no context required

                Errors.ThrowOnErrorCode(hr);

                signIssuanceLicenseCallbackHandler.WaitForCompletion();     // it will throw a proper exception in a failure case                

                // build publish License from th result 
                PublishLicense publishLicense = new PublishLicense(
                    signIssuanceLicenseCallbackHandler.CallbackData);

                // After Issuance license is signed we should build the Author's Use License 
                authorUseLicense = new UseLicense(GetOwnerLicense(issuanceLicense.Handle));

                return publishLicense;
            }
        }
 /// <summary>
 /// Save the publish license to the RM transform's instance data stream.
 /// </summary>
 /// <param name="publishLicense">
 /// The publish licence to be saved. The RM server returns a publish license as a string.
 /// </param>
 /// <remarks>
 /// The stream is rewritten from the beginning, so any existing publish license is
 /// overwritten.
 /// </remarks>
 /// <exception cref="ArgumentNullException">
 /// If <paramref name="publishLicense"/> is null.
 /// </exception>
 /// <exception cref="FileFormatException">
 /// If the existing RM instance data in this file cannot be updated by the current version
 /// of this class.
 /// </exception>
 /// <exception cref="InvalidOperationException">
 /// If the transform settings are fixed.
 /// </exception>
 public void SavePublishLicense(PublishLicense publishLicense)
 {
     _rmet.SavePublishLicense(publishLicense);
 }
        SavePublishLicense(
            PublishLicense publishLicense
            )
        {
            if (publishLicense == null)
            {
                throw new ArgumentNullException("publishLicense");
            }

            if (_fixedSettings)
            {
                throw new InvalidOperationException(SR.Get(SRID.CannotChangePublishLicense));
            }

            // We seek to position 0 but under the covers, the VersionedStream maintains a FormatVersion
            // structure before our logical position zero.
            _publishLicenseStream.Seek(0, SeekOrigin.Begin);

            //
            // Construct a BinaryWriter to write the rest of the instance data.
            //
            // Although BinaryWriter is IDisposable, we must not Close or Dispose it,
            // as that would close the underlying stream, which we do not own. Simply
            // allowing the BinaryWriter to be finalized after it goes out of scope
            // does -not- close the underlying stream.
            //

// Suppress 6518 Local IDisposable object not disposed: 
// Reason: The stream is not owned by the BlockManager, therefore we cannot 
// close the BinaryWriter, as that would Close the stream underneath.
#pragma warning disable 6518
            BinaryWriter utf8Writer = new BinaryWriter(_publishLicenseStream, Encoding.UTF8);
#pragma warning restore 6518

            //
            // There follows a variable-length header (not to be confused with the physical
            // stream header). This header allows future expansion, in case we want to store
            // something in addition to the publish license in the primary instance data stream
            // for this transform. In this version, there is no additional information, so the
            // header consists only of the headerLen field itself, so its length is 4 bytes.
            //
            //
            // If we have previously read in the publish license stream from a file whose format
            // included extra header bytes that we didn't interpret, write those bytes back out
            // (and include them in the header length).
            //
            Int32 headerLen = CU.Int32Size;
            if (_publishLicenseHeaderExtraBytes != null)
            {
                checked { headerLen += _publishLicenseHeaderExtraBytes.Length; }
            }
            utf8Writer.Write(headerLen);

            if (_publishLicenseHeaderExtraBytes != null)
            {
                _publishLicenseStream.Write(
                    _publishLicenseHeaderExtraBytes,
                    0, 
                    _publishLicenseHeaderExtraBytes.Length
                    );
            }

            // 
            // Write out the publish license as a length-prefixed, UTF-8 encoded string.
            //
            WriteByteLengthPrefixedDwordPaddedString(publishLicense.ToString(), utf8Writer, Encoding.UTF8);

            utf8Writer.Flush();

            _publishLicense = publishLicense;
        }
        LoadPublishLicense()
        {
            if (_publishLicenseStream.Length <= 0)
                return null;

            // We seek to position 0 but under the covers, the VersionedStream maintains a FormatVersion
            // structure before our logical position zero.
            _publishLicenseStream.Seek(0, SeekOrigin.Begin);

            //
            // Construct a BinaryReader to read the rest of the instance data.
            //
            // Although BinaryReader is IDisposable, we must not Close or Dispose it,
            // as that would close the underlying stream, which we do not own. Simply
            // allowing the BinaryReader to be finalized after it goes out of scope
            // does -not- close the underlying stream.
            //

// Suppress 6518 Local IDisposable object not disposed: 
// Reason: The stream is not owned by the BlockManager, therefore we cannot 
// close the BinaryWriter, as that would Close the stream underneath.
#pragma warning disable 6518
            BinaryReader utf8Reader = new BinaryReader(_publishLicenseStream, Encoding.UTF8);
#pragma warning restore 6518

            //
            // There follows a variable-length header (not to be confused with the physical
            // stream header). This header allows future expansion, in case we want to store
            // something in addition to the publish license in the primary instance data stream
            // for this transform. The first field in the header is the header length in bytes
            // (including the headerLen field itself).
            //
            Int32 headerLen = utf8Reader.ReadInt32();
            if (headerLen < CU.Int32Size)
            {
                throw new FileFormatException(SR.Get(SRID.PublishLicenseStreamCorrupt));
            }

            if (headerLen > MaxPublishLicenseHeaderLen)
            {
                throw new FileFormatException(
                                SR.Get(SRID.PublishLicenseStreamHeaderTooLong,
                                headerLen,
                                MaxPublishLicenseHeaderLen
                                ));
            }

            //
            // Save any additional bytes in the header that we don't recognize, so we can
            // write them back out later if necessary. We've already read the headerLen field,
            // so subtract the size of that field from the amount we have to save.
            //
            // No need to use checked{} here since we already made sure that header length is greater than Int32Size
            Int32 numPublishLicenseHeaderExtraBytes = headerLen - CU.Int32Size;
            if (numPublishLicenseHeaderExtraBytes > 0)
            {
                _publishLicenseHeaderExtraBytes = new byte [numPublishLicenseHeaderExtraBytes];
                if (PackagingUtilities.ReliableRead(_publishLicenseStream, _publishLicenseHeaderExtraBytes, 0, numPublishLicenseHeaderExtraBytes)
                        != numPublishLicenseHeaderExtraBytes)
                {
                    throw new FileFormatException(SR.Get(SRID.PublishLicenseStreamCorrupt));
                }
            }

            //
            // Read the publish license as a length-prefixed UTF-8 string. If the stream
            // is shorter than the length prefix implies, an exception will be thrown.
            //
            _publishLicense = new PublishLicense(
                                        ReadLengthPrefixedString(
                                            utf8Reader,
                                            Encoding.UTF8,
                                            PublishLicenseLengthMax
                                            )
                                        );

            return _publishLicense;
        }
 public static EncryptedPackageEnvelope CreateFromPackage(string envelopeFileName, Stream packageStream, PublishLicense publishLicense, CryptoProvider cryptoProvider)
 {
     throw new NotImplementedException ();
 }
 public static EncryptedPackageEnvelope Create(Stream envelopeStream, PublishLicense publishLicense, CryptoProvider cryptoProvider)
 {
     throw new NotImplementedException ();
 }
Ejemplo n.º 8
0
        EncryptedPackageEnvelope(
            Stream envelopeStream, 
            PublishLicense publishLicense,
            CryptoProvider cryptoProvider
            )
        { 
            if (envelopeStream == null)
                throw new ArgumentNullException("envelopeStream"); 
 
            ThrowIfRMEncryptionInfoInvalid(publishLicense, cryptoProvider);
 
            _root = StorageRoot.CreateOnStream(envelopeStream, _defaultFileModeForCreate);

            //
            // CreateOnStream opens the stream for read access if it's readable, and for 
            // read/write access if it's writable. We're going to need it to be writable,
            // so check that it is. 
            // 
            if (_root.OpenAccess != FileAccess.ReadWrite)
            { 
                throw new NotSupportedException(SR.Get(SRID.StreamNeedsReadWriteAccess));
            }

            InitializeRMForCreate(publishLicense, cryptoProvider); 
            EmbedPackage(null);
        } 
Ejemplo n.º 9
0
        InitializeRMForCreate(
            PublishLicense publishLicense, 
            CryptoProvider cryptoProvider
            )
        {
            // 
            // Define a data space consisting of a single transform, namely the
            // RightsManagementEncryptionTransform. 
            // 
            DataSpaceManager dsm = _root.GetDataSpaceManager();
 
            dsm.DefineTransform(
                    RightsManagementEncryptionTransform.ClassTransformIdentifier,
                    EncryptionTransformName
                    ); 

            string[] transformStack = new string[1]; 
            transformStack[0] = EncryptionTransformName; 
            _dataSpaceName = DataspaceLabelRMEncryptionNoCompression;
            dsm.DefineDataSpace(transformStack, _dataSpaceName); 

            //
            // The call to DefineTransform created a RightsManagementEncryptionTransform
            // object. Obtain this object from the DataSpaceManager, and wrap it in a 
            // RightsManagementInformation object. This makes the RM information in the
            // compound file available to the application (through the RightsManagementInformation 
            // property), without exposing to the application the implementation detail 
            // that there -is- such a thing as a "transform".
            // 
            RightsManagementEncryptionTransform rmet =
                dsm.GetTransformFromName(EncryptionTransformName) as RightsManagementEncryptionTransform;

            // 
            // We just defined this transform, so it must exist.
            // 
            Debug.Assert( 
                rmet != null,
                "RightsManagementEncryptionTransform not found" 
                );

            _rmi = new RightsManagementInformation(rmet);
 
            //
            // Prepare the transform object for use. 
            // 
            rmet.SavePublishLicense(publishLicense);
            rmet.CryptoProvider = cryptoProvider; 

            //
            // The transform object is now ready for use. When the data space manager
            // queries the transform's IsReady property, it will return true. So there 
            // is no need to sign up for the TransformInitializationEvent.
            // 
        } 
Ejemplo n.º 10
0
 CreateFromPackage(
     Stream envelopeStream, 
     Stream packageStream,
     PublishLicense publishLicense,
     CryptoProvider cryptoProvider
     ) 
 {
     return new EncryptedPackageEnvelope( 
                     envelopeStream, 
                     packageStream,
                     publishLicense, 
                     cryptoProvider
                     );
 }
Ejemplo n.º 11
0
 Create(
     string envelopeFileName, 
     PublishLicense publishLicense, 
     CryptoProvider cryptoProvider
     ) 
 {
     return new EncryptedPackageEnvelope(envelopeFileName, publishLicense, cryptoProvider);
 }
Ejemplo n.º 12
0
        private void ThrowIfRMEncryptionInfoInvalid( 
            PublishLicense publishLicense, 
            CryptoProvider cryptoProvider)
        { 
            if (publishLicense == null)
                throw new ArgumentNullException("publishLicense");

            if (cryptoProvider == null) 
                throw new ArgumentNullException("cryptoProvider");
 
        } 
 public void SavePublishLicense(PublishLicense publishLicense)
 {
     throw new NotImplementedException ();
 }