Skip to content
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1.0.0
* Initial Release

1.1.0
* Added support for renew/reissue
* Made CARequestID more consistent for pending/external validation certs
* Allow for the specifying of custom lifetimes for certs, and comparing the provided value to the allowed values
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,30 @@ The Nameshield AnyCA Gateway REST plugin extends the capabilities of Nameshield
The Nameshield AnyCA Gateway REST plugin is compatible with the Keyfactor AnyCA Gateway REST 24.2.0 and later.

## Support
The Nameshield AnyCA Gateway REST plugin is supported by Keyfactor for Keyfactor customers. If you have a support issue, please open a support ticket via the Keyfactor Support Portal at https://support.keyfactor.com.
The Nameshield AnyCA Gateway REST plugin is open source and community supported, meaning that there is **no SLA** applicable.

> To report a problem or suggest a new feature, use the **[Issues](../../issues)** tab. If you want to contribute actual bug fixes or proposed enhancements, use the **[Pull requests](../../pulls)** tab.

## Requirements

TODO Requirements is a required section


## Installation

1. Install the AnyCA Gateway REST per the [official Keyfactor documentation](https://software.keyfactor.com/Guides/AnyCAGatewayREST/Content/AnyCAGatewayREST/InstallIntroduction.htm).

2. On the server hosting the AnyCA Gateway REST, download and unzip the latest [Nameshield AnyCA Gateway REST plugin](https://github.com/Keyfactor/nameshield-caplugin/releases/latest) from GitHub.

3. Copy the unzipped directory (usually called `net6.0`) to the Extensions directory:
3. Copy the unzipped directory (usually called `net8.0` or `net10.0`) to the Extensions directory:


```shell
Depending on your AnyCA Gateway REST version, copy the unzipped directory to one of the following locations:
Program Files\Keyfactor\AnyCA Gateway\AnyGatewayREST\net6.0\Extensions
Program Files\Keyfactor\AnyCA Gateway\AnyGatewayREST\net8.0\Extensions
Program Files\Keyfactor\AnyCA Gateway\AnyGatewayREST\net10.0\Extensions
```

> The directory containing the Nameshield AnyCA Gateway REST plugin DLLs (`net6.0`) can be named anything, as long as it is unique within the `Extensions` directory.
> The directory containing the Nameshield AnyCA Gateway REST plugin DLLs (`net8.0` or `net10.0`) can be named anything, as long as it is unique within the `Extensions` directory.

4. Restart the AnyCA Gateway REST service.

Expand Down Expand Up @@ -97,6 +98,7 @@ TODO Requirements is a required section

* **OrganizationId** - If you know your organization's ID in nameshield, you can supply that here and that will be used as the organization regardless of what values exist in the request or the Organization field
* **Organization** - If OrganizationId is empty, and an organization name is provided here, the Nameshield gateway will use that name to do organization lookups when enrolling. If neither OrganizationId nor Organization is supplied, the gateway will use whatever is in the O= field of the request subject.
* **Lifetime** - REQUIRED:The lifetime of the certificate to be issued. This should be a string in the format recognized by Nameshield (e.g. 1 year, etc.)

## License

Expand Down
2 changes: 1 addition & 1 deletion docsource/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ When defining templates, use the product names as the ProductID (e.g. Digicert S

## Requirements

TODO Requirements is a required section


4 changes: 4 additions & 0 deletions integration-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
{
"name": "Organization",
"description": "If OrganizationId is empty, and an organization name is provided here, the Nameshield gateway will use that name to do organization lookups when enrolling. If neither OrganizationId nor Organization is supplied, the gateway will use whatever is in the O= field of the request subject."
},
{
"name": "Lifetime",
"description": "REQUIRED:The lifetime of the certificate to be issued. This should be a string in the format recognized by Nameshield (e.g. 1 year, etc.)"
}
]
}
Expand Down
5 changes: 5 additions & 0 deletions nameshield-caplugin/API/RequestCertificate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public CertificateRequest()
data.relationships.product.data = new RelationshipData();
data.relationships.organization = new CertRequestRel();
data.relationships.organization.data = new RelationshipData();
data.relationships.certificate = new CertRequestRel();
data.relationships.certificate.data = new RelationshipData();
}

public string GetOrderData()
Expand Down Expand Up @@ -81,6 +83,9 @@ public class CertificateRequestRelationships
[JsonPropertyName("organization")]
[JsonProperty("organization")]
public CertRequestRel organization { get; set; }
[JsonPropertyName("certificate")]
[JsonProperty("certificate")]
public CertRequestRel certificate { get; set; }
}

public class CertRequestRel
Expand Down
1 change: 1 addition & 0 deletions nameshield-caplugin/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Template
{
public const string ORGANIZATION_ID = "OrganizationId";
public const string ORGANIZATION = "Organization";
public const string LIFETIME = "Lifetime";
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions nameshield-caplugin/Models/Certificate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class Certificate
[JsonPropertyName("attributes")]
[JsonProperty("attributes")]
public CertificateAttributes Attributes { get; set; }
[JsonPropertyName("relationships")]
[JsonProperty("relationships")]
public CertificateRelationships Relationships { get; set; }
}

public class CertificateAttributes
Expand All @@ -60,4 +63,18 @@ public class CertificateAttributes
[JsonProperty("pem")]
public string Pem { get; set; }
}

public class CertificateRelationships
{
[JsonPropertyName("current-order")]
[JsonProperty("current-order")]
public CertOrderRelationship Order { get; set; }
}

public class CertOrderRelationship
{
[JsonPropertyName("data")]
[JsonProperty("data")]
public RelationshipData Data { get; set; }
}
}
3 changes: 3 additions & 0 deletions nameshield-caplugin/Models/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public class OrderAttributes
[JsonPropertyName("status")]
[JsonProperty("status")]
public string Status { get; set; }
[JsonPropertyName("can_renew_at")]
[JsonProperty("can_renew_at")]
public string CanRenewAt { get; set; }
}

public class OrderRelationships
Expand Down
116 changes: 99 additions & 17 deletions nameshield-caplugin/NameshieldCAPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Keyfactor.AnyGateway.Extensions;
using Keyfactor.Extensions.CAPlugin.Nameshield.API;
using Keyfactor.Extensions.CAPlugin.Nameshield.Client;
using Keyfactor.Extensions.CAPlugin.Nameshield.Models;
using Keyfactor.Logging;
using Keyfactor.PKI.Enums.EJBCA;

Expand Down Expand Up @@ -36,7 +37,7 @@ public void Initialize(IAnyCAPluginConfigProvider configProvider, ICertificateDa
_config = JsonConvert.DeserializeObject<NameshieldConfig>(rawConfig);
}

public async Task<EnrollmentResult> Enroll(string csr, string subject, Dictionary<string, string[]> san, EnrollmentProductInfo productInfo, RequestFormat requestFormat, EnrollmentType enrollmnentType)
public async Task<EnrollmentResult> Enroll(string csr, string subject, Dictionary<string, string[]> san, EnrollmentProductInfo productInfo, RequestFormat requestFormat, EnrollmentType enrollmentType)
{
_logger.MethodEntry(LogLevel.Debug);
NameshieldClient client = NameshieldClient.InitializeClient(_config);
Expand Down Expand Up @@ -93,17 +94,51 @@ public async Task<EnrollmentResult> Enroll(string csr, string subject, Dictionar
{
throw new Exception($"No organization provided in attributes or subject that matches an organization in the Nameshield account");
}
var lifetime = productInfo.ProductParameters[Constants.Config.Template.LIFETIME];

CertificateRequest request = new CertificateRequest();
request.data.type = "order";
request.data.attributes.action = "create";

request.data.attributes.csr = csr;
request.data.attributes.duration = "1 year";
request.data.attributes.duration = lifetime;
request.data.relationships.product.data.type = "product";
request.data.relationships.product.data.id = productId;
request.data.relationships.organization.data.type = "organization";
request.data.relationships.organization.data.id = orgId;

string orderAction = "create";
// Determine if we should do a renew or a reissue
if (enrollmentType == EnrollmentType.RenewOrReissue)
{
string priorCertSnString = productInfo.ProductParameters["PriorCertSN"];
var priorCertReqID = await _certificateDataReader.GetRequestIDBySerialNumber(priorCertSnString);
if (string.IsNullOrEmpty(priorCertReqID))
{
throw new Exception($"Cannot find prior certificate with serial number {priorCertSnString}.");
}

string orderId = priorCertReqID.Split('-')[0];

var orderResponse = Task.Run(async () => await client.GetOrderDetails(orderId)).Result;
if (orderResponse.Order.Attributes.CanRenewAt != null)
{
DateTime canRenewDT = DateTime.Parse(orderResponse.Order.Attributes.CanRenewAt);
if (canRenewDT < DateTime.UtcNow)
{
orderAction = "renew";
}
else
{
orderAction = "reissue";
}
}

request.data.relationships.certificate.data.type = "certificate";
request.data.relationships.certificate.data.id = priorCertReqID.Split('-')[1];
}

request.data.attributes.action = orderAction;

var response = Task.Run(async () => await client.RequestCertificate(request)).Result;

var status = response.Order.Attributes.Status;
Expand All @@ -128,15 +163,17 @@ public async Task<EnrollmentResult> Enroll(string csr, string subject, Dictionar
_logger.LogTrace($"Cert request submitted successfully but not delivered, will be picked up by a fugure sync once it is issued.");
return new EnrollmentResult
{
CARequestID = response.Order.Id,
CARequestID = response.Order.Relationships.Certificate.Data.id,
Status = (int)EndEntityStatus.EXTERNALVALIDATION,
StatusMessage = "Certificate is pending issuance and will be picked up by a future sync."
};
}
_logger.LogTrace($"Certificate enrolled successfully");

var cert = GetSingleRecord(response.Order.Relationships.Certificate.Data.id).Result;

//Use placeholder for the serial number part of the req ID, which will be retrieved by GetSingleRecord.
var reqId = $"{response.Order.Relationships.Certificate.Data.id}-XXXXXXXX";
var cert = GetSingleRecord(reqId).Result;

return new EnrollmentResult
{
CARequestID = cert.CARequestID,
Expand Down Expand Up @@ -191,15 +228,21 @@ public List<string> GetProductIds()
public async Task<AnyCAPluginCertificate> GetSingleRecord(string caRequestID)
{
NameshieldClient client = NameshieldClient.InitializeClient(_config);
var idParts = caRequestID.Split('-').ToList();
if (idParts.Count == 1)
{
_logger.LogWarning($"Order {caRequestID} is still awaiting delivery.");
throw new Exception($"Unable to retrieve certificate for order {caRequestID} as it is still awaiting delivery. The certificate will be picked up by a future sync once it is issued.");
}
string certId = idParts[0];
var cert = Task.Run(async () => await client.GetCertificate(certId)).Result;
var certProduct = Task.Run(async () => await client.GetCertificateProduct(certId)).Result;

var cert = Task.Run(async () => await client.GetCertificate(caRequestID)).Result;
var certProduct = Task.Run(async () => await client.GetCertificateProduct(caRequestID)).Result;

int status = ConvertToKeyfactorStatus(cert.Certificate.Attributes.Status, caRequestID);
int status = ConvertToKeyfactorStatus(cert.Certificate.Attributes.Status, certId);

return new AnyCAPluginCertificate()
{
CARequestID = caRequestID,
CARequestID = $"{cert.Certificate.Id}-{cert.Certificate.Attributes.Serial}",
ProductID = certProduct.CertificateProduct.Attributes.Name,
Status = status,
Certificate = cert.Certificate.Attributes.Pem,
Expand All @@ -224,6 +267,13 @@ public Dictionary<string, PropertyConfigInfo> GetTemplateParameterAnnotations()
Hidden = false,
DefaultValue = "",
Type = "String"
},
[Constants.Config.Template.LIFETIME] = new PropertyConfigInfo()
{
Comments = "REQUIRED:The lifetime of the certificate to be issued. This should be a string in the format recognized by Nameshield (e.g. 1 year, etc.)",
Hidden = false,
DefaultValue = "1 year",
Type = "String"
}
};
}
Expand Down Expand Up @@ -271,11 +321,24 @@ public async Task Synchronize(BlockingCollection<AnyCAPluginCertificate> blockin

foreach (var cert in certs.Certificates)
{
var certObj = Task.Run(async () => await client.GetCertificate(cert.Id)).Result;
var certProduct = Task.Run(async () => await client.GetCertificateProduct(cert.Id)).Result;
int status = ConvertToKeyfactorStatus(cert.Attributes.Status, cert.Id);
if (status == (int)EndEntityStatus.EXTERNALVALIDATION)
{
_logger.LogInformation($"Cert request with cert ID {cert.Id} is still waiting for issuance.");
continue;
}
if (string.IsNullOrEmpty(cert.Attributes.Pem))
{
_logger.LogInformation($"Cert request with cert ID {cert.Id} has no PEM data. Skipping...");
continue;
}

var newCert = new AnyCAPluginCertificate()
{
CARequestID = cert.Id,
CARequestID = $"{cert.Id}-{cert.Attributes.Serial}",
OldCARequestID = $"{cert.Id}",
ProductID = certProduct.CertificateProduct.Attributes.Name,
Status = status,
Certificate = cert.Attributes.Pem,
Expand Down Expand Up @@ -310,20 +373,39 @@ public async Task ValidateProductInfo(EnrollmentProductInfo productInfo, Diction

var productList = Task.Run(async () => await localClient.ListProducts()).Result;

bool foundProduct = false;
Product foundProduct = null;
foreach (var product in productList.Products)
{
if (string.Equals(product.Attributes.Name, productInfo.ProductID, StringComparison.OrdinalIgnoreCase))
{
foundProduct = true;
foundProduct = product;
break;
}
}

if (!foundProduct)
if (foundProduct == null)
{
throw new Exception($"Product ID {productInfo.ProductID} was not found in the list of valid products.");
}
string lifetime = productInfo.ProductParameters[Constants.Config.Template.LIFETIME];
if (string.IsNullOrEmpty(lifetime))
{
throw new Exception($"Product ID {productInfo.ProductID} is missing a required lifetime value");
}

bool allowedDuration = false;
foreach (var duration in foundProduct.Attributes.Durations)
{
if (string.Equals(duration, lifetime, StringComparison.OrdinalIgnoreCase))
{
allowedDuration = true;
break;
}
}
if (!allowedDuration)
{
throw new Exception($"Product ID {productInfo.ProductID} has an invalid lifetime value {lifetime}. Valid values for this product are: {string.Join(',', foundProduct.Attributes.Durations)}");
}
}

private static string ValidateConfigurationKey(Dictionary<string, object> connectionInfo, string key)
Expand All @@ -336,7 +418,7 @@ private static string ValidateConfigurationKey(Dictionary<string, object> connec
return string.Empty;
}

private static int ConvertToKeyfactorStatus(string status, string sslId)
private static int ConvertToKeyfactorStatus(string status, string certId)
{
switch (status.ToUpper())
{
Expand All @@ -353,7 +435,7 @@ private static int ConvertToKeyfactorStatus(string status, string sslId)
return (int)EndEntityStatus.REVOKED;

default:
throw new Exception($"Request ID {sslId} has unknown status {status}");
throw new Exception($"Request ID {certId} has unknown status {status}");
}
}

Expand Down
4 changes: 2 additions & 2 deletions nameshield-caplugin/nameshield-caplugin.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
<RootNamespace>Keyfactor.Extensions.CAPlugin.Nameshield</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
Expand All @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Keyfactor.AnyGateway.IAnyCAPlugin" Version="3.0.0" />
<PackageReference Include="Keyfactor.AnyGateway.IAnyCAPlugin" Version="3.2.0" />
<PackageReference Include="Keyfactor.Common" Version="2.5.0" />
<PackageReference Include="Keyfactor.Logging" Version="1.1.1" />
<PackageReference Include="Keyfactor.PKI" Version="5.5.0" />
Expand Down