Skip to content

Commit

Permalink
[APPSEC-26] Add Kotlin specification (#1045)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-oliveira-sonarsource authored Jun 16, 2022
1 parent 4fb47c2 commit 6fd8b5b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
8 changes: 8 additions & 0 deletions rules/S6432/kotlin/highlighting.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== Highlighting

* Primary location
** javax.crypto.Cipher.init call
* Secondary locations
** javax.crypto.spec.GCMParameterSpec constructor
** nonce variable declaration
42 changes: 34 additions & 8 deletions rules/S6432/kotlin/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
FIXME: add a description

// If you want to factorize the description uncomment the following line and create the file.
//include::../description.adoc[]
include::../description.adoc[]

== Noncompliant Code Example

[source,kotlin]
----
FIXME
fun encrypt(key: ByteArray, ptxt: ByteArray) {
val nonce: ByteArray = "7cVgr5cbdCZV".toByteArray() // The initialization vector is a static value
val gcmSpec = GCMParameterSpec(128, nonce) // The initialization vector is configured here
val skeySpec = SecretKeySpec(key, "AES")
val cipher: Cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, gcmSpec) // Noncompliant
}
----

== Compliant Solution

[source,kotlin]
----
FIXME
fun encrypt(key: ByteArray, ptxt: ByteArray) {
val random: SecureRandom = SecureRandom()
val nonce: ByteArray = ByteArray(12)
random.nextBytes(nonce) // Random 96 bit IV
val gcmSpec = GCMParameterSpec(128, nonce)
val skeySpec = SecretKeySpec(key, "AES")
val cipher: Cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, gcmSpec)
}
----

== See
include::../see.adoc[]


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::../message.adoc[]

include::./highlighting.adoc[]

FIXME: A list of links
endif::env-github,rspecator-view[]

0 comments on commit 6fd8b5b

Please sign in to comment.