Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of OCSP issuer DN hash creation #136

Closed
alexey-pelykh opened this issue Nov 24, 2017 · 19 comments
Closed

Example of OCSP issuer DN hash creation #136

alexey-pelykh opened this issue Nov 24, 2017 · 19 comments

Comments

@alexey-pelykh
Copy link

It would be great to have that as part of OCSP creation example

@alexey-pelykh
Copy link
Author

(or if there's an i2d_X509_NAME () implementation in PKI.js)

@rmhrisk
Copy link
Contributor

rmhrisk commented Nov 24, 2017

@YuryStrozhevsky I can't find a good example, mind handling this request.

@alexey-pelykh
Copy link
Author

alexey-pelykh commented Nov 24, 2017

Here's what I've got:

const publicCertificateContent =
  '----BEGIN CERTIFICATE----\n' +
  ...
  '----END CERTIFICATE----';
const publicCertificate = publicCertificateContent
  .replace(/-{5}([^-]+)-{5}/g, '')
  .replace(/\n|\r/g, '')
const sha1 = xmldsigjs.CryptoConfig.GetHashAlgorithm('SHA-1')
const asn1 = asn1js.fromBER(new Uint8Array(Buffer.from(publicCertificate, 'base64')).buffer)
const publicCertificateInfo = new pkijs.Certificate({
  schema: asn1.result
})
// d2i_X509_NAME() and i2d_X509_NAME() decode and encode an ASN.1 Name structure defined in RFC 5280 section 4.1.2.4.
const issuerNameHash = await sha1.Digest(publicCertificateInfo.issuer.toSchema().toBER()) // TODO: this gives wrong results
const authorityKeyIdentifier = _.find(publicCertificateInfo.extensions, (extension) => extension.extnID === '2.5.29.35')
const authorityKeyIdentifierValue = authorityKeyIdentifier.parsedValue.keyIdentifier.valueBlock.valueHex
const authorityInformationExtension = _.find(publicCertificateInfo.extensions, (extension) => extension.extnID === '1.3.6.1.5.5.7.1.1')
const parsedOcspValue = _.find(authorityInformationExtension.parsedValue.accessDescriptions, (parsedValue) => parsedValue.accessMethod === '1.3.6.1.5.5.7.48.1')
const ocspUrl = parsedOcspValue.accessLocation.value
const ocspRequest = new pkijs.OCSPRequest()
const serialNumberValue = publicCertificateInfo.serialNumber.valueBlock.valueHex
ocspRequest.tbsRequest.requestList = [new pkijs.Request({
  reqCert: new pkijs.CertID({
    hashAlgorithm: new pkijs.AlgorithmIdentifier({
      algorithmId: '1.3.14.3.2.26' // SHA-1
    }),
    issuerNameHash: new asn1js.OctetString({ valueHex: issuerNameHash }),
    issuerKeyHash: new asn1js.OctetString({ valueHex: authorityKeyIdentifierValue }),
    serialNumber: new asn1js.Integer({ valueHex: serialNumberValue })
  })
})]

const ocspNonceValue = crypto.randomBytes(18)
console.log('ocspNonceValue', Buffer.from(new Uint8Array(ocspNonceValue)).toString('hex'))
ocspRequest.tbsRequest.requestExtensions = [
  new pkijs.Extension({
    extnID: '1.3.6.1.5.5.7.48.1.2', // ocspNonce
    extnValue: (new asn1js.OctetString({ valueHex: ocspNonceValue })).toBER(false)
  })
]

const ocspRequestData = ocspRequest.toSchema(true).toBER(false)
console.log('ocspRequestData', Buffer.from(new Uint8Array(ocspRequestData)).toString('base64'))

@alexey-pelykh
Copy link
Author

The results I get looks good, except for DN hash, it's not the same as OpenSSL creates, so I must be doing something wrong

@YuryStrozhevsky
Copy link
Collaborator

@alexey-pelykh How did you find the hash is wrong? Could you describe your test procedure and input data for such testing?

@alexey-pelykh
Copy link
Author

https://sk.ee/upload/files/KLASS3-SK_2016_EECCRCA_SHA384.pem.crt
and

-----BEGIN CERTIFICATE-----
MIIFxzCCA6+gAwIBAgIQAwnFwvSSyDNZylYfimMGkDANBgkqhkiG9w0BAQsFADCB
hjELMAkGA1UEBhMCRUUxIjAgBgNVBAoMGUFTIFNlcnRpZml0c2VlcmltaXNrZXNr
dXMxITAfBgNVBAsMGFNlcnRpZml0c2VlcmltaXN0ZWVudXNlZDEXMBUGA1UEYQwO
TlRSRUUtMTA3NDcwMTMxFzAVBgNVBAMMDktMQVNTMy1TSyAyMDE2MB4XDTE3MDky
NjEzMjgzMFoXDTIyMTAyNTEzM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duE1Op/wQqQp1tacV
HXIXPtBV0K1vviIt7YSaaxXg0S0gqaCRKqU/u1hSwToQfxvP2yx6hLKeA+rlwLiY
tRXrBLYglu9j5tVM020aVptd8F91Em1ue5HP4uiOL8Cbj0d++H9ti+6429+M4iBz
EXgP8ujnQ9DY+HkdWxQIh9dMJSdbBn6zzblpuxIMNsSedKAxoOodNvjX9opdpX+/
PHYDGXy4qdpFeQD4ZzBurLe4sC4EG16rZIKqhLHeQnhN10n/bdGvAvLRhMLuchl9
8B4aJPh0AumnNfgbVWxGJ6xktUSW4fOBg2KWHG9wU5sIZqc2Vrup9+It86/bQmss
jtTprsukWPT+H27CZGy9nSLQZAxlJzd0l4pHU68YRJE0bEGUqdqj+1QXq5Xu+iNm
Z1FKDWJ9ghlDx7Fcla5podJOEaa7TesTFuuT9R+WY52cMRNwXRVjQhatue2hinvk
Wc5YPIMGyraSW3wBO6nk0gnia1Vigi1F+w2rwY6LurKSNYgzIwmxxUbtT7bamZnW
yPB0QBZmJO50DWjzTmGBFplKRIKJY8fAoMWZ5GtakVVa5v0fxDU7sS7rC66NrC1d
DxX71jecdzNHqNyrqbMfvJ7PvvqkD+TV23vwKReZGFLFlF6wsKGartl60A==
-----END CERTIFICATE-----
issuerNameHash 16c8f8ac7b57bd5b58b41327228e3fa21201db68
authorityKeyIdentifierValue ae5e58f5f2f2d9c18ed9ef4e07db75ca50e28700
serialNumberValue 0309c5c2f492c83359ca561f8a630690
ocspNonceValue a4ac993cff3ca4ca9a8b3a62a7d8a035c2b6
$ openssl ocsp -issuer res/KLASS3-SK_2016_EECCRCA_SHA384.pem.crt -cert identity.crt -text
OCSP Request Data:
    Version: 1 (0x0)
    Requestor List:
        Certificate ID:
          Hash Algorithm: sha1
          Issuer Name Hash: 797CF08C18F544B716136397E74594EAD5BE9398
          Issuer Key Hash: AE5E58F5F2F2D9C18ED9EF4E07DB75CA50E28700
          Serial Number: 0309C5C2F492C83359CA561F8A630690
    Request Extensions:
        OCSP Nonce: 
            0410E9F1C559A1959173F204DF59F13953C5
$ openssl x509 -in res/KLASS3-SK_2016_EECCRCA_SHA384.pem.crt -ocspid 
        Subject OCSP hash: 797CF08C18F544B716136397E74594EAD5BE9398
        Public key OCSP hash: AE5E58F5F2F2D9C18ED9EF4E07DB75CA50E28700

@alexey-pelykh
Copy link
Author

@YuryStrozhevsky ^

@YuryStrozhevsky
Copy link
Collaborator

@alexey-pelykh I have a suspicious regarding "why hash is wrong". Try to use this code:

const issuerNameHash = await sha1.Digest(publicCertificateInfo.issuer.valueBeforeDecode)

@alexey-pelykh
Copy link
Author

@YuryStrozhevsky that was the very first thing to test, does the same due to https://github.com/PeculiarVentures/PKI.js/blob/master/src/RelativeDistinguishedNames.js#L152

@YuryStrozhevsky
Copy link
Collaborator

@alexey-pelykh Just made a simple test using your certificate as a source:

function test()
{
	//region Initial variables
	let sequence = Promise.resolve();
	//endregion
	
	const asn1 = asn1js.fromBER(stringToArrayBuffer(fromBase64(certBase64)));
	if(asn1.offset === (-1))
	{
		console.log("Something wrong");
		return;
	}
	
	const crypto = getCrypto();
	
	const certificate = new Certificate({ schema: asn1.result });
	
	sequence = sequence.then(() => crypto.digest({ name: "SHA-1" }, certificate.issuer.valueBeforeDecode));
	
	sequence = sequence.then(result =>
	{
		// result = 797CF08C18F544B716136397E74594EAD5BE9398
		console.log(`Issuer hash: ${(bufferToHexCodes(result)).toUpperCase()}`);
	});
	
	sequence = sequence.then(() => crypto.digest({ name: "SHA-1" }, certificate.subject.valueBeforeDecode));
	
	sequence = sequence.then(result =>
	{
		// result = 1CC2E7AF4B76D25234E12415B453B0EB7BA6A1A5
		console.log(`Subject hash: ${(bufferToHexCodes(result)).toUpperCase()}`);
	});
	
	return sequence;
}

So, I do not understand how did you get the wrong

issuerNameHash 16c8f8ac7b57bd5b58b41327228e3fa21201db68

@alexey-pelykh
Copy link
Author

@YuryStrozhevsky Frankly to say, neither do I. Just checked again, same result.

console.log('issuerName', Buffer.from(new Uint8Array(publicCertificateInfo.issuer.valueBeforeDecode)).toString('hex'))

gives

issuerName 308186310b300906035504061302454531223020060355040a0c19415320536572746966697473656572696d69736b65736b75733121301f060355040b0c18536572746966697473656572696d69737465656e757365643117301506035504610c0e4e545245452d31303734373031333117301506035504030c0e4b4c415353332d534b2032303136

same on your side? Since if it's the same, the hashing did something wrong

@YuryStrozhevsky
Copy link
Collaborator

@alexey-pelykh Not really sure I want to check 126 bytes of the buffer :) Yes, probably the "sha1.Digest" function did something wrong.

@alexey-pelykh
Copy link
Author

It did, actually, just figured it out

const issuerNameHash = crypto.createHash('sha1')
      .update(Buffer.from(new Uint8Array(publicCertificateInfo.issuer.valueBeforeDecode)))
      .digest()

gave

issuerNameHash 797cf08c18f544b716136397e74594ead5be9398

while

const sha1 = xmldsigjs.CryptoConfig.GetHashAlgorithm('SHA-1')
const issuerNameHash = await sha1.Digest(publicCertificateInfo.issuer.valueBeforeDecode)

gave

issuerNameHash 16c8f8ac7b57bd5b58b41327228e3fa21201db68

@YuryStrozhevsky
Copy link
Collaborator

@alexey-pelykh Great, I can close the issue then.

@alexey-pelykh
Copy link
Author

Heh,

const sha1 = xmldsigjs.CryptoConfig.GetHashAlgorithm('SHA-1')
const issuerNameHash = await sha1.Digest(Buffer.from(new Uint8Array(publicCertificateInfo.issuer.valueBeforeDecode)))

also gave

issuerNameHash 797cf08c18f544b716136397e74594ead5be9398

@YuryStrozhevsky
Copy link
Collaborator

@alexey-pelykh But no - will keep it open util example update

@alexey-pelykh
Copy link
Author

@microshine any insights on what I did wrong?

@YuryStrozhevsky
Copy link
Collaborator

@alexey-pelykh Would be better to make a new issue in @microshine repos.

@alexey-pelykh
Copy link
Author

Consider that done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants