Skip to content

Commit

Permalink
feat: Expose function to calculate private address from public keys
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarea committed Aug 6, 2020
1 parent bf26960 commit 90ed9fe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/main/kotlin/tech/relaycorp/relaynet/pki.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package tech.relaycorp.relaynet

import tech.relaycorp.relaynet.wrappers.privateAddress
import tech.relaycorp.relaynet.wrappers.x509.Certificate
import java.security.PrivateKey
import java.security.PublicKey
Expand All @@ -25,7 +26,7 @@ fun issueGatewayCertificate(
): Certificate {
val isSelfIssued = issuerCertificate == null
return Certificate.issue(
computePrivateAddress(subjectPublicKey),
subjectPublicKey.privateAddress,
subjectPublicKey,
issuerPrivateKey,
validityEndDate,
Expand Down Expand Up @@ -53,7 +54,7 @@ fun issueEndpointCertificate(
validityStartDate: ZonedDateTime = ZonedDateTime.now()
): Certificate {
return Certificate.issue(
computePrivateAddress(subjectPublicKey),
subjectPublicKey.privateAddress,
subjectPublicKey,
issuerPrivateKey,
validityEndDate,
Expand All @@ -80,7 +81,7 @@ fun issueParcelDeliveryAuthorization(
issuerCertificate: Certificate,
validityStartDate: ZonedDateTime = ZonedDateTime.now()
): Certificate = Certificate.issue(
computePrivateAddress(subjectPublicKey),
subjectPublicKey.privateAddress,
subjectPublicKey,
issuerPrivateKey,
validityEndDate,
Expand All @@ -89,6 +90,3 @@ fun issueParcelDeliveryAuthorization(
0,
validityStartDate
)

private fun computePrivateAddress(subjectPublicKey: PublicKey) =
"0${getSHA256DigestHex(subjectPublicKey.encoded)}"
7 changes: 7 additions & 0 deletions src/main/kotlin/tech/relaycorp/relaynet/wrappers/Keys.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.relaycorp.relaynet.wrappers

import tech.relaycorp.relaynet.getSHA256DigestHex
import java.security.KeyFactory
import java.security.KeyPair
import java.security.KeyPairGenerator
Expand Down Expand Up @@ -35,3 +36,9 @@ fun ByteArray.deserializeRSAPublicKey(): PublicKey {
throw KeyException("Value is not a valid RSA public key", exc)
}
}

/**
* Derive private address for Relaynet node from its public key.
*/
val PublicKey.privateAddress: String
get() = "0${getSHA256DigestHex(this.encoded)}"
13 changes: 13 additions & 0 deletions src/test/kotlin/tech/relaycorp/relaynet/wrappers/KeysTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tech.relaycorp.relaynet.wrappers
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import tech.relaycorp.relaynet.sha256Hex
import java.security.interfaces.RSAPrivateKey
import java.security.interfaces.RSAPublicKey
import java.security.spec.InvalidKeySpecException
Expand Down Expand Up @@ -65,4 +66,16 @@ class KeysTest {
assertEquals(publicKeySerialized.asList(), publicKeyDeserialized.encoded.asList())
}
}

@Nested
inner class PrivateAddress {
@Test
fun `Private node address should be calculated`() {
val keyPair = generateRSAKeyPair()

val privateAddress = keyPair.public.privateAddress

assertEquals("0${sha256Hex(keyPair.public.encoded)}", privateAddress)
}
}
}

0 comments on commit 90ed9fe

Please sign in to comment.