| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# EdelStamp (C) 2000-2016, ON-X, All rights reserved. |
| 4 |
# Author: Peter Sylvester <[email protected]> |
| 5 |
# This program is free software; you can redistribute it and/or |
| 6 |
# modify it under the same terms as Perl itself. |
| 7 |
# |
| 8 |
# Function: Create RFC 3161 time stamps |
| 9 |
# Runs as CGI when REQUEST_METHOD is present. |
| 10 |
# |
| 11 |
# Input: The program needs three environment variables: |
| 12 |
# TSAKeyFile name of a pem encoded RS private key |
| 13 |
# TSACertificateFile name of a der encoded certficate for the key |
| 14 |
# TSAPolicy a list of dot separated numbers |
| 15 |
# |
| 16 |
# If the program finds REQUEST_METHOD, httpd context is assumed |
| 17 |
# program reads a time stamp request from STDIN |
| 18 |
# The REQUEST_METHOD must be POST |
| 19 |
# |
| 20 |
# Output: A time stamp response (if http response is 200) to STDOUT. |
| 21 |
# if under http context headers are prepended. |
| 22 |
# Not details of errors are provided when status=2=rejected |
| 23 |
# |
| 24 |
# Errors: Error details are written to STDERR and can be written to Syslog in |
| 25 |
# and httpd context: |
| 26 |
# Environement varibles SyslogOptions and SyslogFacility are used for parameterization |
| 27 |
# See perl module Sys::Syslog fpr details |
| 28 |
# Notes: Policy on input is ignored, hash algorithms are not checked and copied as is. |
| 29 |
# Since the maximim size of a request is limited, it is unlikely that a request |
| 30 |
# contains the data instead of a hash. |
| 31 |
# Example: The following example are for the apache httpd server configuration |
| 32 |
# |
| 33 |
# ScriptAlias "/tsa/" "/var/www/cgi-bin/" |
| 34 |
# <Directory "/var/www/cgi-bin"> |
| 35 |
# AllowOverride None |
| 36 |
# Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch |
| 37 |
# Require all granted |
| 38 |
# SetEnv TSACertificateFile /etc/apache2/pki/TSA.der |
| 39 |
# SetEnv TSAKeyFile /etc/apache2/pki/TSA.key |
| 40 |
# SetEnv TSAPolicy 1.2.3.4.1 |
| 41 |
# SetEnv TSATimeout 15 |
| 42 |
# SetEnv TSARequestLimit 500 |
| 43 |
# SetEnv SyslogOptions "nofatal,ndelay,pid" |
| 44 |
# SetEnv SyslogFacility "user" |
| 45 |
# </Directory> |
| 46 |
|
| 47 |
use strict; |
| 48 |
use warnings; |
| 49 |
|
| 50 |
use Convert::ASN1; |
| 51 |
use Time::HiRes; |
| 52 |
use Digest::SHA qw(sha256 sha1); |
| 53 |
use Crypt::OpenSSL::RSA; |
| 54 |
|
| 55 |
#{status=> {status=>2}} hard coded. |
| 56 |
my $rejected = "\x30\x05\x30\x03\x02\x01\x02"; |
| 57 |
|
| 58 |
binmode(STDOUT); |
| 59 |
binmode(STDIN); |
| 60 |
binmode(STDERR,':utf8'); |
| 61 |
|
| 62 |
# we like to test out a web server. |
| 63 |
my $HTTP = defined $ENV{'REQUEST_METHOD'} ; |
| 64 |
|
| 65 |
# and we can do syslog |
| 66 |
my $SYSLOG = ($HTTP && defined $ENV{'SyslogOptions'}); |
| 67 |
if ($SYSLOG) { |
| 68 |
use Sys::Syslog ; |
| 69 |
$ENV{'SyslogFacility'} = 'user' unless $ENV{'SyslogFacility'} ; |
| 70 |
openlog('EdelStamp', $ENV{'SyslogOptions'}, $ENV{'SyslogFacility'}) or dieif(1,'Cannot open Syslog'); |
| 71 |
} |
| 72 |
|
| 73 |
print "Content-Type: application/timestamp-response\n" |
| 74 |
."Content-Transfer-Encoding: binary\n" if $HTTP; |
| 75 |
|
| 76 |
# To be or not to be sorry |
| 77 |
sub dieif { |
| 78 |
my ($cond,$text) = @_; |
| 79 |
return unless $cond; |
| 80 |
print "Content-Length: ".length($rejected)."\n\n" if $HTTP; |
| 81 |
print $rejected; |
| 82 |
print STDERR "$text\n"; |
| 83 |
syslog("info", $ENV{'REMOTE_ADDR'} . " $text") if $SYSLOG; |
| 84 |
exit ; |
| 85 |
} |
| 86 |
# To leave some traces |
| 87 |
sub log { |
| 88 |
my ($text) = @_; |
| 89 |
print STDERR "$text\n"; |
| 90 |
syslog("info", $ENV{'REMOTE_ADDR'} . " $text") if $SYSLOG; |
| 91 |
} |
| 92 |
|
| 93 |
# Ok, start parse the ASN. It would be nice to have them directly. |
| 94 |
|
| 95 |
my $asn = Convert::ASN1->new; |
| 96 |
$asn->prepare(q< |
| 97 |
TimeStampReq ::= SEQUENCE { |
| 98 |
version INTEGER , |
| 99 |
messageImprint MessageImprint, |
| 100 |
reqPolicy TSAPolicyId OPTIONAL, |
| 101 |
nonce INTEGER OPTIONAL, |
| 102 |
certReq BOOLEAN OPTIONAL, |
| 103 |
extensions [0] IMPLICIT Extensions OPTIONAL |
| 104 |
} |
| 105 |
|
| 106 |
MessageImprint ::= SEQUENCE { |
| 107 |
hashAlgorithm AlgorithmIdentifier, |
| 108 |
hashedMessage OCTET STRING } |
| 109 |
|
| 110 |
AlgorithmIdentifier ::= SEQUENCE { |
| 111 |
algorithm OBJECT IDENTIFIER, |
| 112 |
parameters NULL } |
| 113 |
|
| 114 |
TSAPolicyId ::= OBJECT IDENTIFIER |
| 115 |
|
| 116 |
Extensions ::= SEQUENCE OF Extension |
| 117 |
|
| 118 |
Extension ::= SEQUENCE { |
| 119 |
extnID OBJECT IDENTIFIER, |
| 120 |
critical BOOLEAN OPTIONAL, |
| 121 |
extnValue OCTET STRING } |
| 122 |
|
| 123 |
TimeStampResp ::= SEQUENCE { |
| 124 |
status PKIStatusInfo, |
| 125 |
timeStampToken TimeStampToken OPTIONAL } |
| 126 |
|
| 127 |
PKIStatusInfo ::= SEQUENCE { |
| 128 |
status PKIStatus, |
| 129 |
statusString PKIFreeText OPTIONAL, |
| 130 |
failInfo PKIFailureInfo OPTIONAL } |
| 131 |
|
| 132 |
PKIStatus ::= INTEGER |
| 133 |
|
| 134 |
PKIFailureInfo ::= BIT STRING |
| 135 |
|
| 136 |
PKIFreeText ::= SEQUENCE OF UTF8String |
| 137 |
|
| 138 |
TimeStampToken ::= SEQUENCE { |
| 139 |
contentType ContentType, |
| 140 |
content [0] EXPLICIT SignedData } |
| 141 |
ContentType ::= OBJECT IDENTIFIER |
| 142 |
|
| 143 |
TSTInfo ::= SEQUENCE { |
| 144 |
version INTEGER , |
| 145 |
policy TSAPolicyId, |
| 146 |
messageImprint MessageImprint, |
| 147 |
serialNumber INTEGER, |
| 148 |
genTime GeneralizedTime, |
| 149 |
accuracy Accuracy OPTIONAL, |
| 150 |
ordering BOOLEAN OPTIONAL, |
| 151 |
nonce INTEGER OPTIONAL, |
| 152 |
tsa [0] GeneralName OPTIONAL, |
| 153 |
extensions [1] IMPLICIT Extensions OPTIONAL |
| 154 |
} |
| 155 |
|
| 156 |
Accuracy ::= SEQUENCE { |
| 157 |
seconds INTEGER OPTIONAL, |
| 158 |
millis [0] INTEGER OPTIONAL, |
| 159 |
micros [1] INTEGER OPTIONAL |
| 160 |
} |
| 161 |
|
| 162 |
Attribute ::= SEQUENCE { |
| 163 |
type AttributeType, |
| 164 |
values SET OF AttributeValue |
| 165 |
} |
| 166 |
|
| 167 |
AttributeType ::= OBJECT IDENTIFIER |
| 168 |
|
| 169 |
AttributeValue ::= ANY |
| 170 |
|
| 171 |
AttributeTypeAndValue ::= SEQUENCE { |
| 172 |
type AttributeType, |
| 173 |
value AttributeValue |
| 174 |
} |
| 175 |
|
| 176 |
Name ::= CHOICE { -- only one possibility for now |
| 177 |
rdnSequence RDNSequence |
| 178 |
} |
| 179 |
|
| 180 |
RDNSequence ::= SEQUENCE OF RelativeDistinguishedName |
| 181 |
|
| 182 |
DistinguishedName ::= RDNSequence |
| 183 |
|
| 184 |
RelativeDistinguishedName ::= |
| 185 |
SET OF AttributeTypeAndValue --SET SIZE (1 .. MAX) OF |
| 186 |
|
| 187 |
DirectoryString ::= CHOICE { |
| 188 |
teletexString TeletexString, --(SIZE (1..MAX)), |
| 189 |
printableString PrintableString, --(SIZE (1..MAX)), |
| 190 |
bmpString BMPString, --(SIZE (1..MAX)), |
| 191 |
universalString UniversalString, --(SIZE (1..MAX)), |
| 192 |
utf8String UTF8String, --(SIZE (1..MAX)), |
| 193 |
ia5String IA5String --added for EmailAddress |
| 194 |
} |
| 195 |
|
| 196 |
Certificate ::= SEQUENCE { |
| 197 |
tbsCertificate TBSCertificate, |
| 198 |
signatureAlgorithm AlgorithmIdentifier, |
| 199 |
signature BIT STRING |
| 200 |
} |
| 201 |
|
| 202 |
TBSCertificate ::= SEQUENCE { |
| 203 |
version [0] EXPLICIT Version OPTIONAL, --DEFAULT v1 |
| 204 |
serialNumber CertificateSerialNumber, |
| 205 |
signature AlgorithmIdentifier, |
| 206 |
issuer Name, |
| 207 |
validity Validity, |
| 208 |
subject Name, |
| 209 |
subjectPublicKeyInfo SubjectPublicKeyInfo, |
| 210 |
issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, |
| 211 |
-- If present, version shall be v2 or v3 |
| 212 |
subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, |
| 213 |
-- If present, version shall be v2 or v3 |
| 214 |
extensions [3] EXPLICIT Extensions OPTIONAL |
| 215 |
-- If present, version shall be v3 |
| 216 |
} |
| 217 |
|
| 218 |
Version ::= INTEGER --{ v1(0), v2(1), v3(2) } |
| 219 |
CertificateSerialNumber ::= INTEGER |
| 220 |
|
| 221 |
Validity ::= SEQUENCE { |
| 222 |
notBefore Time, |
| 223 |
notAfter Time |
| 224 |
} |
| 225 |
|
| 226 |
UniqueIdentifier ::= BIT STRING |
| 227 |
|
| 228 |
SubjectPublicKeyInfo ::= SEQUENCE { |
| 229 |
algorithm AlgorithmIdentifier, |
| 230 |
subjectPublicKey BIT STRING |
| 231 |
} |
| 232 |
|
| 233 |
AlgorithmIdentifier ::= SEQUENCE { |
| 234 |
algorithm OBJECT IDENTIFIER, |
| 235 |
parameters ANY |
| 236 |
} |
| 237 |
|
| 238 |
GeneralNames ::= SEQUENCE OF GeneralName |
| 239 |
GeneralName ::= CHOICE { |
| 240 |
otherName [0] AnotherName, |
| 241 |
rfc822Name [1] IA5String, |
| 242 |
dNSName [2] IA5String, |
| 243 |
x400Address [3] ANY, --ORAddress, |
| 244 |
directoryName [4] Name, |
| 245 |
ediPartyName [5] EDIPartyName, |
| 246 |
uniformResourceIdentifier [6] IA5String, |
| 247 |
iPAddress [7] OCTET STRING, |
| 248 |
registeredID [8] OBJECT IDENTIFIER } |
| 249 |
|
| 250 |
AnotherName ::= SEQUENCE { |
| 251 |
type OBJECT IDENTIFIER, |
| 252 |
value [0] EXPLICIT ANY } --DEFINED BY type-id } |
| 253 |
|
| 254 |
EDIPartyName ::= SEQUENCE { |
| 255 |
nameAssigner [0] DirectoryString OPTIONAL, |
| 256 |
partyName [1] DirectoryString } |
| 257 |
|
| 258 |
IssuerAltName ::= GeneralNames |
| 259 |
|
| 260 |
SubjectDirectoryAttributes ::= SEQUENCE OF Attribute |
| 261 |
|
| 262 |
EncapsulatedContentInfo ::= SEQUENCE { |
| 263 |
eContentType ContentType, |
| 264 |
eContent [0] EXPLICIT OCTET STRING OPTIONAL } |
| 265 |
|
| 266 |
ContentType ::= OBJECT IDENTIFIER |
| 267 |
|
| 268 |
CMSVersion ::= INTEGER |
| 269 |
|
| 270 |
DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier |
| 271 |
|
| 272 |
DigestAlgorithmIdentifier ::= AlgorithmIdentifier |
| 273 |
|
| 274 |
SignerInfo ::= SEQUENCE { |
| 275 |
version CMSVersion, |
| 276 |
sid SignerIdentifier, |
| 277 |
digestAlgorithm DigestAlgorithmIdentifier, |
| 278 |
signedAttrs [0] IMPLICIT SignedAttributes OPTIONAL, |
| 279 |
signatureAlgorithm SignatureAlgorithmIdentifier, |
| 280 |
signature SignatureValue, |
| 281 |
unsignedAttrs [1] IMPLICIT UnsignedAttributes OPTIONAL } |
| 282 |
SignerIdentifier ::= CHOICE { |
| 283 |
issuerAndSerialNumber IssuerAndSerialNumber, |
| 284 |
subjectKeyIdentifier [0] OCTET STRING } |
| 285 |
|
| 286 |
SignatureAlgorithmIdentifier ::= AlgorithmIdentifier |
| 287 |
|
| 288 |
SignedAttributes ::= SET OF ANY |
| 289 |
UnsignedAttributes ::= SET OF ANY |
| 290 |
CMSAttribute ::= SEQUENCE { |
| 291 |
attrType OBJECT IDENTIFIER, |
| 292 |
attrValues SET OF CMSAttributeValue } |
| 293 |
|
| 294 |
CMSAttributeValue ::= ANY |
| 295 |
SignatureValue ::= OCTET STRING |
| 296 |
IssuerAndSerialNumber ::= SEQUENCE { |
| 297 |
issuer Name, |
| 298 |
serialNumber CertificateSerialNumber } |
| 299 |
CertificateSerialNumber ::= INTEGER |
| 300 |
|
| 301 |
SigningTime ::= Time |
| 302 |
Time ::= CHOICE { |
| 303 |
utcTime UTCTime, |
| 304 |
generalTime GeneralizedTime } |
| 305 |
MessageDigest ::= OCTET STRING |
| 306 |
SignedData ::= SEQUENCE { |
| 307 |
version CMSVersion, |
| 308 |
digestAlgorithms DigestAlgorithmIdentifiers, |
| 309 |
encapContentInfo EncapsulatedContentInfo, |
| 310 |
certificates [0] IMPLICIT CertificateSet OPTIONAL, |
| 311 |
-- crls [1] IMPLICIT RevocationInfoChoices OPTIONAL, |
| 312 |
signerInfos SignerInfos } |
| 313 |
DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier |
| 314 |
SignerInfos ::= SET OF SignerInfo |
| 315 |
CertificateSet ::= SET OF CertificateChoices |
| 316 |
SigningCertificate ::= SEQUENCE { |
| 317 |
certs SEQUENCE OF ESSCertID, |
| 318 |
policies SEQUENCE OF PolicyInformation OPTIONAL } |
| 319 |
|
| 320 |
PolicyInformation ::= SEQUENCE { |
| 321 |
policyIdentifier CertPolicyId, |
| 322 |
policyQualifiers SEQUENCE OF |
| 323 |
PolicyQualifierInfo } --OPTIONAL } |
| 324 |
CertPolicyId ::= OBJECT IDENTIFIER |
| 325 |
PolicyQualifierInfo ::= SEQUENCE { |
| 326 |
policyQualifierId PolicyQualifierId, |
| 327 |
qualifier ANY } --DEFINED BY policyQualifierId } |
| 328 |
PolicyQualifierId ::= |
| 329 |
OBJECT IDENTIFIER --( id-qt-cps | id-qt-unotice ) |
| 330 |
-- CPS pointer qualifier |
| 331 |
CPSuri ::= IA5String |
| 332 |
-- user notice qualifier |
| 333 |
UserNotice ::= SEQUENCE { |
| 334 |
noticeRef NoticeReference OPTIONAL, |
| 335 |
explicitText DisplayText OPTIONAL} |
| 336 |
|
| 337 |
NoticeReference ::= SEQUENCE { |
| 338 |
organization DisplayText, |
| 339 |
noticeNumbers SEQUENCE OF INTEGER } |
| 340 |
|
| 341 |
DisplayText ::= CHOICE { |
| 342 |
visibleString VisibleString , |
| 343 |
bmpString BMPString , |
| 344 |
utf8String UTF8String } |
| 345 |
|
| 346 |
ESSCertID ::= SEQUENCE { |
| 347 |
certHash OCTET STRING, -- SHA1 hash of entire certificate |
| 348 |
issuerSerial IssuerSerial OPTIONAL } |
| 349 |
IssuerSerial ::= SEQUENCE { |
| 350 |
issuer GeneralNames, |
| 351 |
serialNumber CertificateSerialNumber } |
| 352 |
CertificateChoices ::= CHOICE { |
| 353 |
certificate ANY -- we already have it encoded |
| 354 |
-- v2AttrCert [2] IMPLICIT AttributeCertificateV2, |
| 355 |
-- other [3] IMPLICIT OtherCertificateFormat |
| 356 |
} |
| 357 |
>) or &dieif(1, "Bad ASN1 definitions: " . $asn->error) ; |
| 358 |
|
| 359 |
# a little helper |
| 360 |
sub asnfind { |
| 361 |
my ($macro) = @_; |
| 362 |
my $asn_macro = $asn->find($macro) or &dieif(1,"No ASN1 syntax for '$macro'"); |
| 363 |
return $asn_macro; |
| 364 |
} |
| 365 |
# can we give a response? |
| 366 |
my $asn_resp = &asnfind('TimeStampResp'); |
| 367 |
$asn_resp->configure('encoding','DER'); |
| 368 |
|
| 369 |
# can we parse? |
| 370 |
my $asn_tspreq = &asnfind('TimeStampReq'); |
| 371 |
&dieif(!$HTTP || $ENV{CONTENT_TYPE} ne 'application/timestamp-query',"Invalid content type received"); |
| 372 |
&dieif(!$HTTP || $ENV{'REQUEST_METHOD'} ne 'POST',"Request Method is not POST"); |
| 373 |
|
| 374 |
# requests are small, we don't want large files here and we timeout, |
| 375 |
# we don't care about contentlength |
| 376 |
|
| 377 |
my $cnt=$ENV{'TSATimeout'}+0; $cnt=15 unless $cnt>2 && $cnt<120; |
| 378 |
my $limit=$ENV{'TSARequestLimit'}+0; $limit=300 unless $limit>5 && $cnt<20000; |
| 379 |
|
| 380 |
# normally a request comes in one packet, but we never know. |
| 381 |
my $pdu=''; |
| 382 |
while ($cnt-- >0) { |
| 383 |
my $asn_tspreq = &asnfind('TimeStampReq'); |
| 384 |
my $next; |
| 385 |
read STDIN, $next, $limit; |
| 386 |
if ($next eq '') { |
| 387 |
sleep(1); |
| 388 |
} else { |
| 389 |
$pdu .= $next; |
| 390 |
&dieif(($cnt <= 0),"Timeout"); |
| 391 |
&dieif((length($pdu)>$limit),"Request too long"); |
| 392 |
my $tspreq = $asn_tspreq->decode($pdu); |
| 393 |
} |
| 394 |
last unless $asn_tspreq->error() ; |
| 395 |
} |
| 396 |
|
| 397 |
my $tspreq = $asn_tspreq->decode($pdu); |
| 398 |
&dieif($asn_tspreq->error(),'Invalid request'); |
| 399 |
&dieif($tspreq->{'version'} != 1,'Invalid version'); |
| 400 |
|
| 401 |
# get policy, cert and key |
| 402 |
{ |
| 403 |
&dieif(!$ENV{'TSAPolicy'}); |
| 404 |
my $asn_policyid=&asnfind('TSAPolicyId'); |
| 405 |
$asn_policyid->encode($ENV{'TSAPolicy'}); |
| 406 |
&dieif($asn_policyid->error(),"Invalid TSAPolicy syntax"); |
| 407 |
} |
| 408 |
my $tsa_cert; |
| 409 |
my $tsa_cert_asn; |
| 410 |
my $certDigest; |
| 411 |
{ # get certificate |
| 412 |
my $filename = $ENV{'TSACertificateFile'} or &dieif(1, "Missing environment variable 'TSACertificateFile'"); |
| 413 |
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, |
| 414 |
$atime,$mtime,$ctime,$blksize,$blocks) = stat $filename; |
| 415 |
open TSACERT, "<$filename" or &dieif(1, "cannot open TSACertificateFile '$filename'"); |
| 416 |
binmode TSACERT; |
| 417 |
read TSACERT, $tsa_cert_asn, $size; |
| 418 |
close TSACERT; |
| 419 |
$certDigest=sha1($tsa_cert_asn); |
| 420 |
my $asn_cert=&asnfind('Certificate'); |
| 421 |
$tsa_cert = $asn_cert->decode( $tsa_cert_asn) or &dieif(1, $asn_cert->error()); |
| 422 |
} |
| 423 |
my $tsa_key; |
| 424 |
{ # get key |
| 425 |
my $filename = $ENV{'TSAKeyFile'} or &dieif(1, "Missing environment variable 'TSAKeyFile'");; |
| 426 |
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, |
| 427 |
$atime,$mtime,$ctime,$blksize,$blocks) = stat $filename; |
| 428 |
open TSAKEY, "<$filename" or &dieif(1,"cannot open TSAKeyFile '$filename'"); |
| 429 |
binmode TSAKEY; |
| 430 |
my $tsa_key_pem; |
| 431 |
read TSAKEY, $tsa_key_pem, $size; |
| 432 |
close TSAKEY; |
| 433 |
$tsa_key = Crypt::OpenSSL::RSA->new_private_key($tsa_key_pem) or &dieif(1,"TSAKeyFile '$filename' cannot be decoded"); |
| 434 |
} |
| 435 |
|
| 436 |
# some magic |
| 437 |
my $time = Time::HiRes::gettimeofday() ; |
| 438 |
my $now = int($time); |
| 439 |
my $serial = ($time-1288070000)*1000000*100000 +$$; |
| 440 |
my $TSTInfo_asn = &asnfind('TSTInfo'); |
| 441 |
$TSTInfo_asn->configure('encoding','DER'); |
| 442 |
$TSTInfo_asn->configure('encode',{time=>'withzone'}); |
| 443 |
$TSTInfo_asn->configure('encode',{timezone=>0}); |
| 444 |
|
| 445 |
# TBD: Add whatever logic you want to fill the TSTInfo, e.g. accurancy, take policy from input. |
| 446 |
# check the validity of the digest, OIDs + length. |
| 447 |
|
| 448 |
my $tstinfo = { |
| 449 |
version=>1, |
| 450 |
policy=>$ENV{'TSAPolicy'}, |
| 451 |
messageImprint=> $tspreq->{'messageImprint'}, |
| 452 |
genTime=>$now, |
| 453 |
serialNumber=>$serial, |
| 454 |
tsa=>{directoryName=>$tsa_cert->{'tbsCertificate'}->{'subject'}} |
| 455 |
}; |
| 456 |
$tstinfo->{'nonce'} = $tspreq->{'nonce'} if defined $tspreq->{'nonce'}; |
| 457 |
|
| 458 |
# encode the content |
| 459 |
my $tstinfostr=$TSTInfo_asn->encode($tstinfo) || &dieif(1,"Cannot encode TSTINFO:" .$TSTInfo_asn->error()); |
| 460 |
|
| 461 |
# and hash it with sha256 |
| 462 |
|
| 463 |
my $DigestAlgorithmIdentifiers=[]; |
| 464 |
$DigestAlgorithmIdentifiers->[0]={algorithm=>'2 16 840 1 101 3 4 2 1',parameters=>"\x05\x00"}; |
| 465 |
my $DigestAlgorithmIdentifiers_asn = &asnfind('DigestAlgorithmIdentifiers') ; |
| 466 |
my $contentDigest=sha256($tstinfostr); |
| 467 |
|
| 468 |
# encode message attributes |
| 469 |
|
| 470 |
my @CMSAttributeList; |
| 471 |
my $CMSAttribute_asn = &asnfind('CMSAttribute'); |
| 472 |
{ |
| 473 |
my $CMSAttributevalue_asn = &asnfind('ContentType'); |
| 474 |
my $l = []; $l->[0] = $CMSAttributevalue_asn->encode('1.2.840.113549.1.9.16.1.4'); |
| 475 |
my $CMSAttribute={attrType=>'1.2.840.113549.1.9.3', attrValues=>$l}; |
| 476 |
push @CMSAttributeList,$CMSAttribute_asn->encode($CMSAttribute); |
| 477 |
} |
| 478 |
{ |
| 479 |
my $CMSAttributevalue_asn = &asnfind('SigningTime') ; |
| 480 |
my $l = []; $l->[0] = $CMSAttributevalue_asn->encode(generalTime=>$now); |
| 481 |
my $CMSAttribute={attrType=>'1.2.840.113549.1.9.5', attrValues=>$l}; |
| 482 |
push @CMSAttributeList,$CMSAttribute_asn->encode($CMSAttribute); |
| 483 |
} |
| 484 |
{ |
| 485 |
my $CMSAttributevalue_asn = &asnfind('MessageDigest') ; |
| 486 |
my $l = []; $l->[0] = $CMSAttributevalue_asn->encode($contentDigest); |
| 487 |
my $CMSAttribute={attrType=>'1.2.840.113549.1.9.4', attrValues=>$l}; |
| 488 |
push @CMSAttributeList,$CMSAttribute_asn->encode($CMSAttribute); |
| 489 |
} |
| 490 |
{ |
| 491 |
my $CMSAttributevalue_asn = &asnfind('SigningCertificate') ; |
| 492 |
my $SC=[]; $SC->[0] = {certHash=>$certDigest}; |
| 493 |
my $l = []; $l->[0] = $CMSAttributevalue_asn->encode({certs=>$SC}); |
| 494 |
my $CMSAttribute={attrType=>'1.2.840.113549.1 9.16.2.12', attrValues=>$l}; |
| 495 |
push @CMSAttributeList,$CMSAttribute_asn->encode($CMSAttribute); |
| 496 |
} |
| 497 |
my @SortedAttributes = sort @CMSAttributeList; # needed for DER, just to be sure. |
| 498 |
my $CMSAttributes_asn = &asnfind('SignedAttributes') ; |
| 499 |
my $TBSattrs=$CMSAttributes_asn->encode(\@SortedAttributes) or &dieif(1,$CMSAttributes_asn->error()); |
| 500 |
|
| 501 |
# create and sign a signerinfo |
| 502 |
$tsa_key->use_sha256_hash(); |
| 503 |
my $SignerInfos=[]; $SignerInfos->[0] = { |
| 504 |
version =>1, |
| 505 |
digestAlgorithm=>$DigestAlgorithmIdentifiers->[0], |
| 506 |
sid=>{issuerAndSerialNumber=>{issuer=>$tsa_cert->{'tbsCertificate'}->{'issuer'}, |
| 507 |
serialNumber=>$tsa_cert->{'tbsCertificate'}->{'serialNumber'},}}, |
| 508 |
signedAttrs=>\@SortedAttributes, |
| 509 |
signatureAlgorithm=>$DigestAlgorithmIdentifiers->[0], |
| 510 |
signature=>$tsa_key->sign($TBSattrs), |
| 511 |
}; |
| 512 |
# finish the token and response |
| 513 |
my $CertificateSet=[]; $CertificateSet->[0]={certificate=>$tsa_cert_asn}; |
| 514 |
my $TimeStampToken={ |
| 515 |
contentType=>'1 2 840 113549 1 7 2', |
| 516 |
content=>{ |
| 517 |
version=>3, |
| 518 |
digestAlgorithms=>$DigestAlgorithmIdentifiers, |
| 519 |
encapContentInfo=>{ |
| 520 |
eContentType=>'1.2.840.113549.1.9.16.1.4', |
| 521 |
eContent=>$tstinfostr |
| 522 |
}, |
| 523 |
certificates=>$CertificateSet, |
| 524 |
signerInfos=>$SignerInfos, |
| 525 |
}}; |
| 526 |
my $response = $asn_resp->encode({status=> {status=>0},timeStampToken=>$TimeStampToken}) or &dieif(1,"Cannot create Timestampresponse"); |
| 527 |
print STDOUT "Content-Disposition: Attachment; filename=$now-$$.tsr\n"; |
| 528 |
print STDOUT "Content-Length:" .length($response) . "\n\n" if $HTTP; print STDOUT $response; |
| 529 |
|
| 530 |
my $messageImprint_asn = &asnfind('MessageImprint'); |
| 531 |
my $messageImprint= $messageImprint_asn->encode($tspreq->{'messageImprint'}) or &dieif(1,$messageImprint_asn->error()); |
| 532 |
|
| 533 |
&log('ReceivedHash ' . unpack('H*', $messageImprint) . ' SignedAtributes ' . unpack("H*",$TBSattrs) ) ; |
| 534 |
# This is the end (for now) |
| 535 |
|