playready/
xml_utils.rs

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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use xml_builder::{XMLElement, XMLError};

pub fn render(element: &XMLElement) -> Result<Vec<u8>, XMLError> {
    let mut ret = Vec::<u8>::new();
    element.render(&mut ret, false, false, false, true)?;
    Ok(ret)
}

pub fn build_cipher_data(b64_chain: String) -> Result<XMLElement, XMLError> {
    let mut data = XMLElement::new("Data");

    let certificate_chains = {
        let mut certificate_chains = XMLElement::new("CertificateChains");

        let certificate_chain = {
            let mut certificate_chain = XMLElement::new("CertificateChain");
            certificate_chain.add_text(b64_chain)?;
            certificate_chain
        };
        certificate_chains.add_child(certificate_chain)?;

        certificate_chains
    };
    data.add_child(certificate_chains)?;

    let features = {
        let mut features = XMLElement::new("Features");

        let feature = {
            let mut feature = XMLElement::new("Feature");
            feature.add_attribute("Name", "AESCBC");
            feature.add_text(String::from("\"\""))?;

            feature
        };
        features.add_child(feature)?;

        let ree = {
            let mut ree = XMLElement::new("REE");
            let aescbcs = XMLElement::new("AESCBCS");

            ree.add_child(aescbcs)?;
            ree
        };
        features.add_child(ree)?;

        features
    };
    data.add_child(features)?;

    Ok(data)
}

pub fn build_signed_info(digest_value: String) -> Result<XMLElement, XMLError> {
    let mut signed_info = XMLElement::new("SignedInfo");
    signed_info.add_attribute("xmlns", "http://www.w3.org/2000/09/xmldsig#");

    let mut canonicalization_method = XMLElement::new("CanonicalizationMethod");
    canonicalization_method.add_attribute(
        "Algorithm",
        "http://www.w3.org/TR/2001/REC-xml-c14n-20010315",
    );

    let mut signature_method = XMLElement::new("SignatureMethod");
    signature_method.add_attribute(
        "Algorithm",
        "http://schemas.microsoft.com/DRM/2007/03/protocols#ecdsa-sha256",
    );

    let reference = {
        let mut reference = XMLElement::new("Reference");
        reference.add_attribute("URI", "#SignedData");

        let digest_method = {
            let mut digest_method = XMLElement::new("DigestMethod");
            digest_method.add_attribute(
                "Algorithm",
                "http://schemas.microsoft.com/DRM/2007/03/protocols#sha256",
            );
            digest_method
        };
        reference.add_child(digest_method)?;

        let digest_value_tag = {
            let mut digest_value_tag = XMLElement::new("DigestValue");
            digest_value_tag.add_text(digest_value)?;
            digest_value_tag
        };
        reference.add_child(digest_value_tag)?;

        reference
    };

    signed_info.add_child(canonicalization_method)?;
    signed_info.add_child(signature_method)?;
    signed_info.add_child(reference)?;

    Ok(signed_info)
}

pub fn build_digest_content(
    protocol_version: String,
    client_version: String,
    client_time: String,
    wrm_header: String,
    nonce: String,
    wmrm_cipher: String,
    cert_cipher: String,
) -> Result<XMLElement, XMLError> {
    let mut la = XMLElement::new("LA");

    la.add_attribute(
        "xmlns",
        "http://schemas.microsoft.com/DRM/2007/03/protocols",
    );
    la.add_attribute("Id", "SignedData");
    la.add_attribute("xml:space", "preserve");

    let mut version = XMLElement::new("Version");
    version.add_text(protocol_version)?;

    let mut content_header = XMLElement::new("ContentHeader");
    content_header.add_text(wrm_header)?;

    let client_info = {
        let mut client_info = XMLElement::new("CLIENTINFO");
        let client_version_tag = {
            let mut client_version_tag = XMLElement::new("CLIENTVERSION");
            client_version_tag.add_text(client_version)?;
            client_version_tag
        };
        client_info.add_child(client_version_tag)?;
        client_info
    };

    let mut license_nonce = XMLElement::new("LicenseNonce");
    license_nonce.add_text(nonce)?;

    let mut client_time_tag = XMLElement::new("ClientTime");
    client_time_tag.add_text(client_time)?;

    let encrypted_data = {
        let mut encrypted_data = XMLElement::new("EncryptedData");
        encrypted_data.add_attribute("xmlns", "http://www.w3.org/2001/04/xmlenc#");
        encrypted_data.add_attribute("Type", "http://www.w3.org/2001/04/xmlenc#Element");

        let encryption_method = {
            let mut encryption_method = XMLElement::new("EncryptionMethod");
            encryption_method
                .add_attribute("Algorithm", "http://www.w3.org/2001/04/xmlenc#aes128-cbc");
            encryption_method
        };
        encrypted_data.add_child(encryption_method)?;

        let key_info = {
            let mut key_info = XMLElement::new("KeyInfo");
            key_info.add_attribute("xmlns", "http://www.w3.org/2000/09/xmldsig#");

            let encrypted_key = {
                let mut encrypted_key = XMLElement::new("EncryptedKey");
                encrypted_key.add_attribute("xmlns", "http://www.w3.org/2001/04/xmlenc#");

                let encryption_method = {
                    let mut encryption_method = XMLElement::new("EncryptionMethod");
                    encryption_method.add_attribute(
                        "Algorithm",
                        "http://schemas.microsoft.com/DRM/2007/03/protocols#ecc256",
                    );
                    encryption_method
                };
                encrypted_key.add_child(encryption_method)?;

                let key_info = {
                    let mut key_info = XMLElement::new("KeyInfo");
                    key_info.add_attribute("xmlns", "http://www.w3.org/2000/09/xmldsig#");

                    let key_name = {
                        let mut key_name = XMLElement::new("KeyName");
                        key_name.add_text(String::from("WMRMServer"))?;
                        key_name
                    };
                    key_info.add_child(key_name)?;

                    key_info
                };
                encrypted_key.add_child(key_info)?;

                let cipher_data = {
                    let mut cipher_data = XMLElement::new("CipherData");
                    let cipher_value = {
                        let mut cipher_value = XMLElement::new("CipherValue");
                        cipher_value.add_text(wmrm_cipher)?;
                        cipher_value
                    };
                    cipher_data.add_child(cipher_value)?;
                    cipher_data
                };

                encrypted_key.add_child(cipher_data)?;
                encrypted_key
            };

            key_info.add_child(encrypted_key)?;
            key_info
        };
        encrypted_data.add_child(key_info)?;

        let cipher_data = {
            let mut cipher_data = XMLElement::new("CipherData");
            let cipher_value = {
                let mut cipher_value = XMLElement::new("CipherValue");
                cipher_value.add_text(cert_cipher)?;
                cipher_value
            };
            cipher_data.add_child(cipher_value)?;
            cipher_data
        };

        encrypted_data.add_child(cipher_data)?;
        encrypted_data
    };

    la.add_child(version)?;
    la.add_child(content_header)?;
    la.add_child(client_info)?;
    la.add_child(license_nonce)?;
    la.add_child(client_time_tag)?;
    la.add_child(encrypted_data)?;

    Ok(la)
}

pub fn build_license_challenge(
    la_content: XMLElement,
    signed_info: XMLElement,
    signature: String,
    public_key: String,
) -> Result<XMLElement, XMLError> {
    let mut envelope = XMLElement::new("soap:Envelope");
    envelope.add_attribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
    envelope.add_attribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
    envelope.add_attribute("xmlns:soap", "http://schemas.xmlsoap.org/soap/envelope/");

    let mut body = XMLElement::new("soap:Body");
    let mut acquire_license = XMLElement::new("AcquireLicense");
    let mut challenge1 = XMLElement::new("challenge");
    let mut challenge2 = XMLElement::new("Challenge");

    acquire_license.add_attribute(
        "xmlns",
        "http://schemas.microsoft.com/DRM/2007/03/protocols",
    );
    challenge2.add_attribute(
        "xmlns",
        "http://schemas.microsoft.com/DRM/2007/03/protocols/messages",
    );

    let signature_tag = {
        let mut signature_tag = XMLElement::new("Signature");
        signature_tag.add_attribute("xmlns", "http://www.w3.org/2000/09/xmldsig#");
        signature_tag.add_child(signed_info)?;

        let signature_value = {
            let mut signature_value = XMLElement::new("SignatureValue");
            signature_value.add_text(signature)?;

            signature_value
        };
        signature_tag.add_child(signature_value)?;

        let key_info = {
            let mut key_info = XMLElement::new("KeyInfo");
            key_info.add_attribute("xmlns", "http://www.w3.org/2000/09/xmldsig#");

            let key_value = {
                let mut key_value = XMLElement::new("KeyValue");

                let ecc_key_value = {
                    let mut ecc_key_value = XMLElement::new("ECCKeyValue");

                    let public_key_tag = {
                        let mut public_key_tag = XMLElement::new("PublicKey");
                        public_key_tag.add_text(public_key)?;
                        public_key_tag
                    };
                    ecc_key_value.add_child(public_key_tag)?;

                    ecc_key_value
                };
                key_value.add_child(ecc_key_value)?;

                key_value
            };
            key_info.add_child(key_value)?;

            key_info
        };
        signature_tag.add_child(key_info)?;

        signature_tag
    };

    challenge2.add_child(la_content)?;
    challenge2.add_child(signature_tag)?;

    challenge1.add_child(challenge2)?;
    acquire_license.add_child(challenge1)?;
    body.add_child(acquire_license)?;
    envelope.add_child(body)?;

    Ok(envelope)
}

pub fn parse_challenge_response(text: &str) -> Result<Vec<String>, roxmltree::Error> {
    let doc = roxmltree::Document::parse(text)?;

    Ok(doc
        .descendants()
        .filter(|n| n.has_tag_name("License"))
        .filter_map(|n| n.text().map(String::from))
        .collect())
}