forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support key less certificates in Key Vault. (Azure#23002)
- Loading branch information
1 parent
75e17d8
commit c859ce8
Showing
24 changed files
with
1,053 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+136 KB
sdk/keyvault/azure-security-keyvault-jca/resources/SignToPrincipal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions
67
...e-security-keyvault-jca/src/main/java/com/azure/security/keyvault/jca/KeyVaultEncode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.security.keyvault.jca; | ||
|
||
import java.math.BigInteger; | ||
|
||
/** | ||
* encode signature | ||
* Ref: https://docs.microsoft.com/en-us/windows/win32/seccertenroll/about-der-encoding-of-asn-1-types | ||
*/ | ||
public class KeyVaultEncode { | ||
|
||
private static final byte TAG_INTEGER = 0x02; | ||
private static final byte TAG_SEQUENCE = 0x30; | ||
|
||
/** | ||
* Decode signatures imitating ECUtil | ||
* @param signature signature get by keyvault | ||
* @return decoded signatures | ||
*/ | ||
public static byte[] encodeByte(byte[] signature) { | ||
int halfLength = signature.length >> 1; | ||
byte[] leftResult = toBigIntegerBytesWithLengthPrefix(signature, 0, halfLength); | ||
byte[] rightResult = toBigIntegerBytesWithLengthPrefix(signature, halfLength, halfLength); | ||
byte[] resultLengthBytes = buildLengthBytes(TAG_SEQUENCE, leftResult.length + rightResult.length); | ||
return concatBytes(resultLengthBytes, leftResult, rightResult); | ||
} | ||
|
||
static byte[] toBigIntegerBytesWithLengthPrefix(byte[] bytes, int offset, int length) { | ||
byte[] magnitude = new byte[length]; | ||
System.arraycopy(bytes, offset, magnitude, 0, length); | ||
BigInteger bigInteger = new BigInteger(1, magnitude); | ||
byte[] bigIntegerArray = bigInteger.toByteArray(); | ||
return concatBytes(buildLengthBytes(TAG_INTEGER, bigIntegerArray.length), bigIntegerArray); | ||
} | ||
|
||
static byte[] concatBytes(byte[] bytes1, byte[] bytes2) { | ||
byte[] result = new byte[bytes1.length + bytes2.length]; | ||
System.arraycopy(bytes1, 0, result, 0, bytes1.length); | ||
System.arraycopy(bytes2, 0, result, bytes1.length, bytes2.length); | ||
return result; | ||
} | ||
|
||
static byte[] concatBytes(byte[] bytes1, byte[] bytes2, byte[] bytes3) { | ||
byte[] result = new byte[bytes1.length + bytes2.length + bytes3.length]; | ||
System.arraycopy(bytes1, 0, result, 0, bytes1.length); | ||
System.arraycopy(bytes2, 0, result, bytes1.length, bytes2.length); | ||
System.arraycopy(bytes3, 0, result, bytes1.length + bytes2.length, bytes3.length); | ||
return result; | ||
} | ||
|
||
static byte[] buildLengthBytes(byte tag, int len) { | ||
if (len < 128) { | ||
return new byte[] {tag, ((byte) len)}; | ||
} else if (len < (1 << 8)) { | ||
return new byte[] {tag, (byte) 0x081, (byte) len}; | ||
} else if (len < (1 << 16)) { | ||
return new byte[] {tag, (byte) 0x082, (byte) (len >> 8), (byte) len}; | ||
} else if (len < (1 << 24)) { | ||
return new byte[] {tag, (byte) 0x083, (byte) (len >> 16), (byte) (len >> 8), (byte) len}; | ||
} else { | ||
return new byte[] {tag, (byte) 0x084, (byte) (len >> 24), (byte) (len >> 16), (byte) (len >> 8), (byte) len}; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...curity-keyvault-jca/src/main/java/com/azure/security/keyvault/jca/KeyVaultPrivateKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.security.keyvault.jca; | ||
|
||
import java.security.PrivateKey; | ||
|
||
/** | ||
* KeyVault fake private which work when key less | ||
*/ | ||
public class KeyVaultPrivateKey implements PrivateKey { | ||
|
||
/** | ||
* Stores the serial version UID. | ||
*/ | ||
private static final long serialVersionUID = 30_10_00; | ||
|
||
private String kid; | ||
|
||
private String algorithm; | ||
|
||
/** | ||
* Builder for key vault private key | ||
* @param algorithm algorithm | ||
* @param kid The key id | ||
*/ | ||
public KeyVaultPrivateKey(String algorithm, String kid) { | ||
this.algorithm = algorithm; | ||
this.kid = kid; | ||
} | ||
|
||
/** | ||
* Get the KeyId | ||
* @return the KeyId | ||
*/ | ||
public String getKid() { | ||
return kid; | ||
} | ||
|
||
/** | ||
* Store the KeyId | ||
* @param kid the KeyId | ||
*/ | ||
public void setKid(String kid) { | ||
this.kid = kid; | ||
} | ||
|
||
/** | ||
* Store key vault certificate algorithm | ||
* @param algorithm algorithm | ||
*/ | ||
public void setAlgorithm(String algorithm) { | ||
this.algorithm = algorithm; | ||
} | ||
|
||
@Override | ||
public String getAlgorithm() { | ||
return algorithm; | ||
} | ||
|
||
@Override | ||
public String getFormat() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public byte[] getEncoded() { | ||
return new byte[0]; | ||
} | ||
} |
Oops, something went wrong.