pki(n)
pki(n) β public key encryption, but make it β¨iconicβ¨
okay so bestie, welcome to pki(n) β the public key encryption manual, but make it β¨iconicβ¨. no cap.
πΈ NAME πΈ
pki β it's the implementation of the public key cipher. periodt. π
β¨ SYNOPSIS β¨
first things first, you gotta have these lines, bestie:
package require Tcl 8.5 9
package require pki ?0.22?
::pki::encrypt ?-binary? ?-hex? ?-pad? ?-nopad? ?-priv? ?-pub? ?--?
input key
::pki::decrypt ?-binary? ?-hex? ?-unpad? ?-nounpad? ?-priv? ?-pub? ?--?
input key
::pki::sign input key ?algo?
::pki::verify signedmessage plaintext key ?algo?
::pki::key key ?password? ?encodePem?
::pki::pkcs::parse_key key ?password?
::pki::x509::parse_cert cert
::pki::rsa::generate bitlength ?exponent?
::pki::x509::verify_cert cert trustedcerts ?intermediatecerts?
::pki::x509::validate_cert cert ?-sign_message dn_of_signer?
?-encrypt_message dn_of_signer? ?-sign_cert dn_to_be_signed ca_depth?
?-ssl dn?
::pki::pkcs::create_csr keylist namelist ?encodePem? ?algo?
::pki::pkcs::parse_csr csr
::pki::x509::create_cert signreqlist cakeylist serial_number notBefore
notAfter isCA extensions ?encodePem? ?algo?
π DESCRIPTION π
okay so this is where the description would spill the tea, but bestie, that part is coming in the next chunks β don't go anywhere, we're just getting started π β¨
π the actual commands π
Bestie. Sit down. Hydrate. We're doing the commands now and there are a lot of them, but every single one is gonna make sense by the end, I promise. π«Ά
::pki::encrypt
::pki::encrypt ?-binary? ?-hex? ?-pad? ?-nopad? ?-priv? ?-pub? ?--? input key
Encrypt a message using PKI (probably RSA, let's be honest). β¨
The way this works: you have to tell it which key you're using β -priv to encrypt with the private key, or -pub to encrypt with the public key. There's no "figure it out yourself" mode. One of -pub or -priv must be specified. The default behaviour is to pad and hand you back hex.
-hexβ the data comes back encoded as a hexidecimal string. π-binaryβ the data comes back as a binary string.-padβ the data gets padded per PKCS#1 before it's encrypted. π-nopadβ blocks the padding, no padding for you.
Heads up: if you pass-hex/-binarymore than once, or-pad/-nopadmore than once, the last one you said wins. No merging, no arguing, last one slays. π
The thing you're encrypting goes in the input argument.
The key argument β that's the key you're using, and it's a return value from one of these three:
::pki::pkcs::parse_key::pki::x509::parse_cert::pki::rsa::generate
Mapping to OpenSSL's openssl application:
"openssl rsautl -encrypt" == "::pki::encrypt -binary -pub""openssl rsautl -sign" == "::pki::encrypt -binary -priv"
::pki::decrypt
::pki::decrypt ?-binary? ?-hex? ?-unpad? ?-nounpad? ?-priv? ?-pub? ?--? input key
Decrypt a message using PKI (again, probably RSA). For how all the options behave, go read ::pki::encrypt β same energy, same handling. π¦
Mapping to OpenSSL's openssl application:
"openssl rsautl -decrypt" == "::pki::decrypt -binary -priv""openssl rsautl -verify" == "::pki::decrypt -binary -pub"
::pki::sign
::pki::sign input key ?algo?
Digitally sign the message input using the private key. βοΈ
If algo is ommited, "sha1" is assumed. The possible values for algo include "md5", "sha1", "sha256", and "raw". π
Attention, this one actually matters: if you specify"raw"foralgo, it inhibits the building of an ASN.1 structure that would encode which hashing algorithm was chosen. In that case, the correspondingpkgi::verifymust be called with algorithm information. And conversely β if you specify a non-"raw"algorithm here, then the correspondingpkgi::verifyinvokation has to be made without algorithm information. It's a matched set, bestie. Don't split them up. π
The input should be the plain text β hashing happens on it.
The key should include the private key. π
::pki::verify
::pki::verify signedmessage plaintext key ?algo?
Verify a digital signature</purl> using a public key. Returns true or false. That's it, that's the whole vibe check. β
Attention: the algorithm informationalgohas to be specified if and only if thepki::signthat generated thesignedmessagewas called with the algorithm"raw"β because that's what inhibited the building of the ASN.1 structure encoding the chosen hashing algorithm. And conversely, if a proper algorithm was specified during signing, then you must not specify an algorithm here. Matching game, fr fr. π§
::pki::key
::pki::key key ?password? ?encodePem?
Convert a key structure into a serialized PEM (that's the default) or DER encoded private key, suitable for other applications. π« For RSA keys, this means PKCS#1.
::pki::pkcs::parse_key
::pki::pkcs::parse_key key ?password?
Convert a PKCS#1 private key into a usable key β and by usable I mean one you can actually hand to ::pki::encrypt, ::pki::decrypt, ::pki::sign, and ::pki::verify. π₯
::pki::x509::parse_cert
::pki::x509::parse_cert cert
Convert an X.509 certificate into a usable (public) key. The dictionary you get back can be used as an argument for ::pki:encrypt, ::pki::decrypt, and ::pki::verify. π
The cert argument can be either PEM or DER encoded.
On top of all the public keying info, the dictionary also contains these keys, holding certificate content as defined in RFC5280 (https://www.rfc-editor.org/rfc/rfc5280#section-4.1):
subjectholds the name of the subject from the certificate. πissuerholds the name of the issuing CA.serial_numberholds the serial number of the certificate.notBeforeholds the starting date for certificate validity.notAfterholds the ending date for certificate validity.versionholds the X.509 version format.extensionsholds a dictionary containing the extensions included in the certificate (more on that below π).
The dictionary holds additional entries related to keying, too. Those are intended for use with the commands mentioned above for cryptographic operations. π
The extensions key in the returned dictionary holds a nested dictionary whose keys correspond to the names (with the same exact case π) in Certificate Extensions (https://www.rfc-editor.org/rfc/rfc5280#section-4.2) in RFC5280. The format of each value is also based on the ASN.1 structures defined there. See the Examples (#section3) for an illustration. β¨
::pki::rsa::generate
::pki::rsa::generate bitlength ?exponent?
Generate a brand new RSA key pair, the parts of which can be used as arguments for ::pki::encrypt, ::pki::decrypt, ::pki::sign, and ::pki::verify. ποΈβ¨
The bitlength argument is the length of the public key modulus.
The exponent argument should generally not be specified unless you really know what you are doing. Like genuinely. Not "I read a blog post" know. Actually know. π
::pki::x509::verify_cert
::pki::x509::verify_cert cert trustedcerts ?intermediatecerts?
Verify that a trust can be found between the certificate specified in the cert argument and one of the certificates specified in the list of certificates in the trustedcerts argument. π«Ά
Note: eventually the chain can be through untrusted certificates listed in the intermediatecerts argument, but this is currently unimplemented. π
The certificates specified in the cert and trustedcerts option should be parsed β from ::pki::x509::parse_cert. π
::pki::x509::validate_cert
::pki::x509::validate_cert cert ?-sign_message dn_of_signer? ?-encrypt_message dn_of_signer? ?-sign_cert dn_to_be_signed ca_depth? ?-ssl dn?
Validate that a certificate is valid to be used in some capacity. If you specify multiple options, they all have to be met for this procedure to return "true". It's giving all-or-nothing. π
Currently, only the -sign_cert option is functional. The rest are just... there. Manifesting, bestie. π―οΈ
The -sign_cert arguments are dn_to_be_signed and ca_depth.
dn_to_be_signedis the distinguished from the subject of a certificate, used to verify that the certificate specified in thecertargument can sign. βοΈca_depthis used to indicate at which depth the verification should be done at. Some certificates are limited to how far down the chain they can be used to verify a given certificate. πͺ
::pki::pkcs::create_csr
::pki::pkcs::create_csr keylist namelist ?encodePem? ?algo?
Generate a certificate signing request from a key pair specified in the keylist argument. π
The namelist argument is a list of "name" followed by "value" pairs, to encoding as the requested distinguished name in the CSR.
The encodePem option specifies whether the result should be PEM encoded or DER encoded. A "true" value means the result is PEM encoded, while any other value results in the result being DER encoded. DER encoding is the default. π
The algo argument specifies the hashing algorithm we should use to sign this certificate signing request with. The default is "sha1". Other possible values include "md5" and "sha256". π
::pki::pkcs::parse_csr
::pki::pkcs::parse_csr csr
Parse a Certificate Signing Request. The csr argument can be either PEM or DER encoded. π
The command returns a dictionary that includes the following keys:
subjectβ contains the subject name from the CSR. πtypeβ contains the public key algorithm name. Currently onlyrsais supported.extensionRequestβ contains a dictionary with the contents of theextensionRequest(https://datatracker.ietf.org/doc/html/rfc2986#page-5) information in the CSR. This has the same form as described for theextensionsdictionary in the documentation forparse_cert. π¦
There may be other keys in the dictionary related to the public key algorithm in use. iykyk. β¨
::pki::x509::create_cert
::pki::x509::create_cert signreqlist cakeylist serial_number notBefore notAfter isCA extensions ?encodePem? ?algo?
Sign a signing request (usually from ::pki::pkcs::create_csr or ::pki::pkcs::parse_csr) with a Certificate Authority (CA) certificate. π
signreqlistβ should be the parsed signing request. πcakeylistβ should be the parsed CA certificate. π° (sorry, the name is right there, I had to)serial_numberβ should be a serial number unique to this certificate from this certificate authority. π’notBeforeandnotAfterβ should contain the time before and after which (respectively) the certificate should be considered invalid. The time should be encoded as somethingclock formatwill accept β i.e., the results ofclock secondsandclock add. β°isCAβ a boolean argument describing whether or not the signed certificate should be a a CA certificate. If specified astrue, the"id-ce-basicConstraints"extension is added, with the arguments of"critical"beingtrue,"allowCA"beingtrue, andcaDepthbeing-1(infinite). βΎοΈextensionsβ a list of extensions and their parameters that should be encoded into the created certificate. β¨
Currently only one extension is understood:"id-ce-basicConstraints". It accepts three arguments:critical,allowCA,caDepth.
criticalβ for this extension (and any extension, tbh) it says whether or not the validator should reject the certificate as invalid if it does not understand the extension (if set to"true") or should ignore the extension (if set to"false"). π¨allowCAβ a boolean value specifying whether or not we can be used as a certificate authority (CA). πcaDepthβ indicates how many children CAs can be children of this CA, in a depth-wise fashion. A value of"0"means this CA cannot sign a CA certificate and have the result be valid. A value of"-1"indicates infinite depth. βΎοΈ
and bestie we are SO not done yet β keep scrolling, the rest is coming π
π EXAMPLES π
Okay bestie, this is the glow-up moment β¨ β here's how you actually pull a cert off a real live server, using the TLS extension, and then dump everything inside it. Watch and learn π
% set so [tls::socket www.example.com 443]
sock00000229EB84E710
% tls::handshake $so
1
% set status [tls::status $so]
...output not shown...
% set cert_pem [dict get $status certificate]
...output not shown...
% set cert [::pki::x509::parse_cert $cert_pem]
...output not shown...
% dict get $cert subject
C=US, ST=California, L=Los Angeles, O=Internet Corporation for Assigned Names and Numbers, CN=www.example.org
% dict get $cert issuer
C=US, O=DigiCert Inc, CN=DigiCert TLS RSA SHA256 2020 CA1
% clock format [dict get $cert notAfter]
Sun Dec 26 05:29:59 +0530 2021
% set extensions [dict get $cert extensions]
...output not shown...
% dict keys $extensions
authorityKeyIdentifier subjectKeyIdentifier subjectAltName keyUsage extKeyUsage cRLDistributionPoints certificatePolicies authorityInfoAccess id-ce-basicConstraints basicConstraints 1.3.6.1.4.1.11129.2.4.2
dict get $extensions basicConstraints
1 {0 -1}
% dict get $extensions keyUsage
1 {5 digitalSignature keyEncipherment}
% dict get $extensions extKeyUsage
0 {serverAuth clientAuth}
% dict get $extensions subjectAltName
0 {dNSName www.example.org dNSName example.com dNSName example.edu dNSName example.net dNSName example.org dNSName www.example.com dNSName www.example.edu dNSName www.example.net}
% dict get $extensions basicConstraints
1 {0 -1}
% dict get $extensions keyUsage
1 {5 digitalSignature keyEncipherment}
% dict get $extensions extKeyUsage
0 {serverAuth clientAuth}
Likeβ¦ the way tls::handshake just returns 1 and then you've got the whole tls::status dict sitting there with a certificate key in it? iconic. You grab the PEM, hand it to ::pki::x509::parse_cert, and suddenly you're reading the subject, the issuer, the notAfter (formatted all pretty with clock format), and the whole extensions dict. no cap, it ate π
π REFERENCES π
The lore, the canon, the required reading π§ β all the specs this whole thing is built on:
- Internet X.509 Public Key Infrastructure Certificate and Certificate Revocation List (CRL) Profile [https://www.rfc-editor.org/rfc/rfc5280]
- New ASN.1 Modules for the Public Key Infrastructure Using X.509 (PKIX) [https://www.rfc-editor.org/rfc/rfc5912]
- PKCS #10: Certification Request Syntax Specification [https://www.rfc-editor.org/rfc/rfc2986]
π AUTHORS π
The main characters, fr fr:
- Roy Keene, Ashok P. Nadkarni
π«Ά BUGS, IDEAS, FEEDBACK π«Ά
Okay so real talk π β this document, and the package it describes, will undoubtedly contain bugs and other problems. Not even being dramatic, that's just how it is. Please report such in the category rsa of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. And if you've got ideas for enhancements for either the package and/or the documentation, please report those too β we want them π₯Ί
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. β¨
πΈ SEE ALSO πΈ
Go read these too, bestie, they're part of the family:
β KEYWORDS β
- cipher, data integrity, encryption, public key cipher, rsa, security
π CATEGORY π
- Hashes, checksums, and encryption
π COPYRIGHT π
Copyright (c) 2010, 2011, 2012, 2013, 2021 - 2024 Roy Keene, Andreas Kupries, Ashok P. Nadkarni
And that's the whole page, bestie π¦ β brought to you by tcllib, version 0.22, the pki(n) page itself π Stay encrypted, stay iconic, mwah π
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2010, 2011, 2012, 2013, 2021 - 2024 Roy Keene, Andreas Kupries, Ashok P\&. Nadkarni
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms