Wednesday, August 27, 2008

Code Review Vol. 5 - DRM Profile Generator in VB.Net

Here is a follow up to the Windows Media Encoder and DRM script I posted previously. I mentioned in the article that I had to export a DRM profile from the DRM server, but I never really elaborated on that. Here is the script that I wrote that I ran on the server which generated the DRM profile.


Imports WMEncoderLib
Imports WMRMOBJSLib
Imports System.IO
Imports System.Xml

Public Class Generate
Shared Sub Main()
Dim profValue
CreateProfile()
End Sub

Shared Sub CreateProfile()
' Create a WMEncoder object.
Dim Encoder As WMEncoder
Encoder = New WMEncoder

' Create an IWMDRMContentAuthor object.
Dim DRM As IWMDRMContentAuthor
DRM = Encoder.EncoderDRMContentAuthor

' Retrieve the collection of DRM profiles.
Dim DRMProColl As IWMDRMProfileCollection
Dim KeyObj = New WMRMKeys


DRMProColl = DRM.DRMProfileCollection

' Declare variables. Specify the provider's Web site, and certificate and
' signing values. The other variables are returned.
Dim sPublicKey As String
Dim sWebURL As String
Dim vProfileID As Object
Dim vSeed As Object
Dim sPrivateKey As String
Dim sSignedPublicKey As String
Dim sSigLSCert As String
Dim sSigRootCert As String
Dim sProfilePublicKey As String

Dim sContentID As String

sContentID = KeyObj.GenerateKeyID()

KeyObj.GenerateSigningKeysEx(sPrivateKey, sPublicKey, sSignedPublicKey)

sWebURL = "http://license3.musicchoice.com/XXXX.asp"

sSigLSCert = KeyObj.GetCertificate("LSLicenseSigningCert")
sSigRootCert = KeyObj.GetCertificate("LSRootCert")

' Create the DRM profile. The public key, DRM profile ID, and
' license key seed are returned.
sProfilePublicKey = DRM.CreateDRMProfile(sWebURL, sPrivateKey, sSignedPublicKey, sSigLSCert, sSigRootCert, vProfileID, vSeed)

' Create an IWMDRMProfile object and retrieve a profile.
Dim DRMPro As IWMDRMProfile
DRMPro = DRM.GetDRMProfile(vProfileID)

' Set the rest of the properties for the DRM profile.
DRMPro.Description = "DRM for MC portable content"
DRMPro.LicenseAcquisitionURL = "http://license3.musicchoice.com/XXXX.asp"
DRMPro.Name = "MC Portable DRM"
DRMPro.V1LicenseAcquisitionURL = "http://go.microsoft.com/fwlink/?LinkId=10141"

' Add the required individualization version as an attribute.
Dim ProAttr As IWMDRMAttributes
ProAttr = DRMPro.Attributes
ProAttr.Add("SECURITYVERSION", "2.2")

ProAttr.Add("ContentID", sContentID)

Dim sDetails As String

Dim oXMLWriter As New XmlTextWriter("C:\Inetpub\wwwroot\wm\IssuedDetails.xml", System.Text.Encoding.UTF8)

oXMLWriter.WriteStartElement("Details")
oXMLWriter.WriteStartElement("Detail")

oXMLWriter.WriteAttributeString("ContentID", sContentID)
oXMLWriter.WriteAttributeString("Seed", vSeed)
oXMLWriter.WriteAttributeString("Key", sProfilePublicKey)
oXMLWriter.WriteAttributeString("PrivateKey", sPrivateKey)


oXMLWriter.WriteEndElement()
oXMLWriter.WriteEndElement()

oXMLWriter.Close()

DRMPro.Update()

DRM.ExportDRMProfile(DRMPro.ID, "password", "C:\Inetpub\wwwroot\wm\ExportProfile.drm")

End Sub

Shared Sub SaveInfo(ByVal nSeed As String, ByVal nPath As String)
Dim objStreamWriter As StreamWriter
objStreamWriter = New StreamWriter(nPath)
objStreamWriter.WriteLine(nSeed)
objStreamWriter.Close()


End Sub

End Class

No comments: