Skip to content

fix: add tests for raw RSA keyring node #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 182 additions & 0 deletions modules/raw-rsa-keyring-node/test/raw_rsa_keyring_node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing permissions and
* limitations under the License.
*/

/* eslint-env mocha */

import * as chai from 'chai'
import chaiAsPromised from 'chai-as-promised'
import 'mocha'
import {
RawRsaKeyringNode
} from '../src/index'
import {
KeyringNode,
NodeEncryptionMaterial,
NodeAlgorithmSuite,
AlgorithmSuiteIdentifier,
EncryptedDataKey, // eslint-disable-line no-unused-vars
NodeDecryptionMaterial
} from '@aws-crypto/material-management-node'

chai.use(chaiAsPromised)
const { expect } = chai

/* RSA Keys to test.
* These keys are *Public*!
* *DO NOT USE*
*
* to recrate
crypto.generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem'
}
}, (e, p, v) => {
public = p
private = v
})
*/
const publicPem = '-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAu3eAVbBR//sN05yszjSh\n09D9L9e5q6W7z5Sqdn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kLsqbBa+4tsjfmlJY3udFgkCggEATU9p2DEYm3uVT6E/wsyK\nPKWI13dcMANdfZtLH5nI4B/Ers8bfRmhpO3q73QRbqa40zJmKtXdsuE+wq5+rBvw\n6L7oD7cwNYr/Wt51SUAUPCYZuxRCLjWHR+wHZuMr3Yv/9XLFrFlqo54uwnb9w+vt\nHkFUeJuX14KThGtbo/bW7sPYTp4UDG0guQQD3JQG1JaJJBJlu/MZMGWdKkQOsobr\nVUlJ6aamxXBP+gqz9FNWHnAF0F8VYUbHpS7yOjQM4qWgVB24CyzUoyUN7SyPUgiI\n8XAKlGw0uoIDrJAtJiYV0oYicfuqhUiX5I3PKFnCK0cIRY+VIRXi02+49q9wBsFh\nUQKCAQB190+U+7Z7YsvKWIbFxnSIGRpzae2HlVJzRI/aoo79ij5qNfisaZvxhvfY\nUTkz6FzHIJoxq/mAJXKQgQ9ofW+XMR/xWKaFRkiGE2aPy3JecoVOx1OL6TWV7KDX\n2prwflF4WEzkZA25BPmg2GM8sTXXln9xSVwNLe24lF6UNj0KG+6zioUM+oepf70v\nUB63Bn/qQwBGgh72oBni4HxqskNG1bNkKt6emgyr/tRNZGqrKCxjqq3Oa8YMrfbr\nP4goDm0LICzbeLlFtVUT7xpAGuItkK26JGypoAfdU5SybjuKaXDygTcq1EEMyduV\nnUwWh19LU+597M0VXIKm2/H3v4up\n-----END PRIVATE KEY-----\n'

describe('RawRsaKeyringWebCrypto::constructor', () => {
const keyName = 'keyName'
const keyNamespace = 'keyNamespace'

it('constructor decorates', async () => {
const test = new RawRsaKeyringNode({
rsaKey: {
privateKey: privatePem,
publicKey: publicPem
},
keyName,
keyNamespace
})

expect(test.keyName).to.equal(keyName)
expect(test.keyNamespace).to.equal(keyNamespace)
expect(test._wrapKey).to.be.a('function')
expect(test._unwrapKey).to.be.a('function')
expect(test).to.be.instanceOf(KeyringNode)
})

it('can construct with only public key', () => {
const testPublicOnly = new RawRsaKeyringNode({
rsaKey: {
publicKey: publicPem
},
keyName,
keyNamespace
})
expect(testPublicOnly).to.be.instanceOf(RawRsaKeyringNode)
})

it('can construct with only private key', () => {
const testPrivateOnly = new RawRsaKeyringNode({
rsaKey: {
privateKey: privatePem
},
keyName,
keyNamespace
})
expect(testPrivateOnly).to.be.instanceOf(RawRsaKeyringNode)
})

it('Precondition: RsaKeyringNode needs either a public or a private key to operate.', () => {
expect(() => new RawRsaKeyringNode({
keyName,
keyNamespace,
rsaKey: {}
})).to.throw()
})

it('Precondition: RsaKeyringNode needs identifying information for encrypt and decrypt.', () => {
// @ts-ignore Typescript is trying to save us.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is typescript saving us from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typescript knows that this function should be called with a keyNamespace and a keyName, so it will complain. But JS does not have this protection... So to test that this will indeed throw, I need TypeScript to be quite and not complain about calling the function incorrectly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

expect(() => new RawRsaKeyringNode({
rsaKey: { privateKey: privatePem, publicKey: publicPem }
})).to.throw()
// @ts-ignore Typescript is trying to save us.
expect(() => new RawRsaKeyringNode({
rsaKey: { privateKey: privatePem, publicKey: publicPem },
keyNamespace
})).to.throw()
// @ts-ignore Typescript is trying to save us.
expect(() => new RawRsaKeyringNode({
rsaKey: { privateKey: privatePem, publicKey: publicPem },
keyName
})).to.throw()
})
})

describe('RawRsaKeyringWebCrypto encrypt/decrypt', () => {
const keyNamespace = 'keyNamespace'
const keyName = 'keyName'
const keyring = new RawRsaKeyringNode({
rsaKey: { privateKey: privatePem, publicKey: publicPem },
keyName,
keyNamespace
})
let encryptedDataKey: EncryptedDataKey

it('can encrypt and create unencrypted data key', async () => {
const suite = new NodeAlgorithmSuite(AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256)
const material = new NodeEncryptionMaterial(suite)
const test = await keyring.onEncrypt(material)
expect(test.hasValidKey()).to.equal(true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this compare the type that is returned to be true? Or will any true value returned from this function pass this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.chaijs.com/api/bdd/#method_equal

it is === . The issue is that the udk should be random, so there is not too much to test. Given that I pull the udk below maybe this has no value.
Want me to pull it out?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No i was trying to understand JS a little better.

const udk = test.getUnencryptedDataKey()
expect(udk).to.have.lengthOf(suite.keyLengthBytes)
expect(test.encryptedDataKeys).to.have.lengthOf(1)
const [edk] = test.encryptedDataKeys
expect(edk.providerId).to.equal(keyNamespace)
encryptedDataKey = edk
})

it('can decrypt an EncryptedDataKey', async () => {
const suite = new NodeAlgorithmSuite(AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256)
const material = new NodeDecryptionMaterial(suite)
const test = await keyring.onDecrypt(material, [encryptedDataKey])
expect(test.hasValidKey()).to.equal(true)
})

it('Precondition: Public key must be defined to support encrypt.', async () => {
const keyring = new RawRsaKeyringNode({
rsaKey: { privateKey: privatePem },
keyName,
keyNamespace
})

const suite = new NodeAlgorithmSuite(AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256)
const material = new NodeEncryptionMaterial(suite)
expect(keyring.onEncrypt(material)).to.rejectedWith(Error)
})

it('Precondition: Private key must be defined to support decrypt.', async () => {
const keyring = new RawRsaKeyringNode({
rsaKey: { publicKey: publicPem },
keyName,
keyNamespace
})

const suite = new NodeAlgorithmSuite(AlgorithmSuiteIdentifier.ALG_AES256_GCM_IV12_TAG16_HKDF_SHA256)
const material = new NodeDecryptionMaterial(suite)
await keyring.onDecrypt(material, [encryptedDataKey])
expect(keyring.onDecrypt(material, [encryptedDataKey])).to.rejectedWith(Error)
})
})