Skip to content

Commit

Permalink
add protection key id to content key implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert Cheng committed Dec 17, 2012
1 parent d5dde55 commit c192e96
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.microsoft.windowsazure.services.media.implementation.entities.EntityOperationSingleResultBase;
import com.sun.jersey.api.client.GenericType;

// TODO: Auto-generated Javadoc
/**
* Class for creating operations to manipulate content key entities.
*
Expand Down Expand Up @@ -78,6 +79,9 @@ public static class Creator extends EntityOperationSingleResultBase<ContentKeyIn
/** The checksum. */
private String checksum;

/** The protection key id. */
private String protectionKeyId;

/**
* Instantiates a new creator.
*
Expand All @@ -89,7 +93,6 @@ public static class Creator extends EntityOperationSingleResultBase<ContentKeyIn
* the encrypted content key
*/
public Creator(String id, ContentKeyType contentKeyType, String encryptedContentKey) {

super(ENTITY_SET, ContentKeyInfo.class);

this.id = id;
Expand All @@ -110,6 +113,7 @@ public Object getRequestContents() {
contentKeyRestType.setEncryptedContentKey(encryptedContentKey);
contentKeyRestType.setName(name);
contentKeyRestType.setChecksum(checksum);
contentKeyRestType.setProtectionKeyId(protectionKeyId);
return contentKeyRestType;
}

Expand All @@ -136,6 +140,18 @@ public Creator setChecksum(String checksum) {
this.checksum = checksum;
return this;
}

/**
* Sets the protection key id.
*
* @param protectionKeyId
* the protection key id
* @return the creator
*/
public Creator setProtectionKeyId(String protectionKeyId) {
this.protectionKeyId = protectionKeyId;
return this;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,12 @@ public void uploadAesProtectedAssetAndDownloadSuccess() throws Exception {

// gets the public key for storage encryption.

String protectionKey = getProtectionKey();
String protectionKeyId = getProtectionKeyId();
String protectionKey = getProtectionKey(protectionKeyId);

// creates the content key with encrypted
ContentKeyInfo contentKeyInfo = createContentKey(aesKey, ContentKeyType.StorageEncryption, protectionKey);
ContentKeyInfo contentKeyInfo = createContentKey(aesKey, ContentKeyType.StorageEncryption, protectionKeyId,
protectionKey);

// link the content key with the asset.
linkContentKey(assetInfo, contentKeyInfo);
Expand All @@ -158,6 +160,12 @@ public void uploadAesProtectedAssetAndDownloadSuccess() throws Exception {

}

private String getProtectionKeyId() throws ServiceException {
String protectionKeyId = (String) service.action(ProtectionKey
.getProtectionKeyId(ContentKeyType.StorageEncryption));
return protectionKeyId;
}

private JobInfo decodeAsset(String name, AssetInfo assetInfo) throws ServiceException, InterruptedException {
MediaProcessorInfo mediaProcessorInfo = GetMediaProcessor(strorageDecryptionProcessor);
JobInfo jobInfo = createJob(name, assetInfo, mediaProcessorInfo);
Expand Down Expand Up @@ -200,9 +208,7 @@ private void linkContentKey(AssetInfo assetInfo, ContentKeyInfo contentKeyInfo)
service.action(Asset.linkContentKey(assetInfo.getId(), contentKeyUri));
}

private String getProtectionKey() throws ServiceException {
String protectionKeyId = (String) service.action(ProtectionKey
.getProtectionKeyId(ContentKeyType.StorageEncryption));
private String getProtectionKey(String protectionKeyId) throws ServiceException {
String protectionKey = (String) service.action(ProtectionKey.getProtectionKey(protectionKeyId));
return protectionKey;
}
Expand Down Expand Up @@ -247,17 +253,18 @@ private byte[] EncryptFile(InputStream inputStream, byte[] aesKey, byte[] initia
return cipherText;
}

private ContentKeyInfo createContentKey(byte[] aesKey, ContentKeyType contentKeyType, String protectionKey)
throws InvalidKeyException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException,
NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, ServiceException,
CertificateException {
private ContentKeyInfo createContentKey(byte[] aesKey, ContentKeyType contentKeyType, String protectionKeyId,
String protectionKey) throws InvalidKeyException, InvalidKeySpecException, NoSuchAlgorithmException,
NoSuchProviderException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException,
ServiceException, CertificateException {
UUID contentKeyIdUuid = UUID.randomUUID();
String contentKeyId = createContentKeyId(contentKeyIdUuid);
byte[] encryptedContentKey = EncryptionHelper.EncryptSymmetricKey(protectionKey, aesKey);
String encryptedContentKeyString = Base64.encode(encryptedContentKey);
String checksum = EncryptionHelper.calculateChecksum(contentKeyIdUuid, aesKey);
ContentKeyInfo contentKeyInfo = service.create(ContentKey.create(contentKeyId, contentKeyType,
encryptedContentKeyString).setChecksum(checksum));
ContentKeyInfo contentKeyInfo = service.create(ContentKey
.create(contentKeyId, contentKeyType, encryptedContentKeyString).setChecksum(checksum)
.setProtectionKeyId(protectionKeyId));
return contentKeyInfo;
}

Expand Down

0 comments on commit c192e96

Please sign in to comment.