1 1.1 christos =pod 2 1.1 christos 3 1.1 christos =head1 NAME 4 1.1 christos 5 1.1 christos DERlib - internal OpenSSL DER library 6 1.1 christos 7 1.1 christos =head1 DESCRIPTION 8 1.1 christos 9 1.1 christos OpenSSL contains an internal small DER reading and writing library, 10 1.1 christos as an alternative to the publicly known i2d and d2i functions. It's 11 1.1 christos solely constituted of functions that work as building blocks to create 12 1.1 christos more similar functions to encode and decode larger structures. 13 1.1 christos 14 1.1 christos All these functions have similar function signatures (C<something> 15 1.1 christos will vary depending on what the function will encode): 16 1.1 christos 17 1.1 christos int DER_w_something(WPACKET *pkt, int tag, ...); 18 1.1 christos 19 1.1 christos =begin comment 20 1.1 christos 21 1.1 christos When readers are added, add this: 22 1.1 christos 23 1.1 christos int DER_r_something(PACKET *pkt, int tag, ...); 24 1.1 christos 25 1.1 christos =end comment 26 1.1 christos 27 1.1 christos I<pkt> is the packet context used, and I<tag> should be the 28 1.1 christos context-specific tag value of the element being handled, or -1 if there 29 1.1 christos is no tag number for that element (you may use the convenience macro 30 1.1 christos B<DER_NO_CONTEXT> instead of -1). Any argument following is the C 31 1.1 christos variable that's being encoded or decoded. 32 1.1 christos 33 1.1 christos =head2 DER writers / encoders 34 1.1 christos 35 1.1 christos DER writers are based in L<WPACKET(3)>, a generic packet writing 36 1.1 christos library, so before using any of them, I<pkt> must be initialized 37 1.1 christos using L<WPACKET_init_der(3)> or L<WPACKET_init_null_der(3)> 38 1.1 christos 39 1.1 christos DER writers must be used in reverse order, except for the wrapping 40 1.1 christos functions that implement a constructed element. The latter are easily 41 1.1 christos recognised by their function name including the words C<begin> and 42 1.1 christos C<end>. As an example, we can look at the DSA signature structure, 43 1.1 christos which is defined like this in ASN.1 terms: 44 1.1 christos 45 1.1 christos -- Copied from RFC 3279, section 2.2.2 46 1.1 christos Dss-Sig-Value ::= SEQUENCE { 47 1.1 christos r INTEGER, 48 1.1 christos s INTEGER } 49 1.1 christos 50 1.1 christos With the DER library, this is the corresponding code, given two OpenSSL 51 1.1 christos B<BIGNUM>s I<r> and I<s>: 52 1.1 christos 53 1.1 christos int ok = ossl_DER_w_begin_sequence(pkt, -1) 54 1.1 christos && ossl_DER_w_bn(pkg, -1, s) 55 1.1 christos && ossl_DER_w_bn(pkg, -1, r) 56 1.1 christos && ossl_DER_w_end_sequence(pkt, -1); 57 1.1 christos 58 1.1 christos As an example of the use of I<tag>, an ASN.1 element like this: 59 1.1 christos 60 1.1 christos v [1] INTEGER OPTIONAL 61 1.1 christos 62 1.1 christos Would be encoded like this: 63 1.1 christos 64 1.1 christos ossl_DER_w_bn(pkt, 1, v) 65 1.1 christos 66 1.1 christos =begin comment 67 1.1 christos 68 1.1 christos =head2 DER readers / decoders 69 1.1 christos 70 1.1 christos TBA 71 1.1 christos 72 1.1 christos =end comment 73 1.1 christos 74 1.1 christos =head1 EXAMPLES 75 1.1 christos 76 1.1 christos A more complex example, encoding the AlgorithmIdentifier with 77 1.1 christos RSASSA-PSS values. 78 1.1 christos 79 1.1 christos As a reminder, the AlgorithmIdentifier is specified like this: 80 1.1 christos 81 1.1 christos -- From RFC 3280, section 4.1.1.2 82 1.1 christos AlgorithmIdentifier ::= SEQUENCE { 83 1.1 christos algorithm OBJECT IDENTIFIER, 84 1.1 christos parameters ANY DEFINED BY algorithm OPTIONAL } 85 1.1 christos 86 1.1 christos And the RSASSA-PSS OID and parameters are specified like this: 87 1.1 christos 88 1.1 christos -- From RFC 3279, section 3.1 89 1.1 christos id-RSASSA-PSS OBJECT IDENTIFIER ::= { pkcs-1 10 } 90 1.1 christos 91 1.1 christos RSASSA-PSS-params ::= SEQUENCE { 92 1.1 christos hashAlgorithm [0] HashAlgorithm DEFAULT 93 1.1 christos sha1Identifier, 94 1.1 christos maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT 95 1.1 christos mgf1SHA1Identifier, 96 1.1 christos saltLength [2] INTEGER DEFAULT 20, 97 1.1 christos trailerField [3] INTEGER DEFAULT 1 } 98 1.1 christos 99 1.1 christos The value we want to encode, written in ASN.1 syntax: 100 1.1 christos 101 1.1 christos { 102 1.1 christos algorithm id-RSASSA-PSS, 103 1.1 christos parameters { 104 1.1 christos hashAlgorithm sha256Identifier, 105 1.1 christos maskGenAlgorithm mgf1SHA256Identifier, 106 1.1 christos saltLength 20 -- unnecessarily explicit 107 1.1 christos } 108 1.1 christos } 109 1.1 christos 110 1.1 christos Assuming that we have precompiled constants for C<id-RSASSA-PSS>, 111 1.1 christos C<sha256Identifier> and C<mgf1SHA256Identifier>, the DER writing code 112 1.1 christos looks as follows. This is a complete function to write that specific 113 1.1 christos value: 114 1.1 christos 115 1.1 christos int DER_w_AlgorithmIdentifier_RSASSA_PSS_special(WPACKET *pkt, 116 1.1 christos int tag, 117 1.1 christos RSA *rsa) 118 1.1 christos { 119 1.1 christos return ossl_DER_w_begin_sequence(pkt, tag) 120 1.1 christos && (ossl_DER_w_begin_sequence(pkt, DER_NO_CONTEXT) 121 1.1 christos && ossl_DER_w_uint32(pkt, 2, 20) 122 1.1 christos && ossl_DER_w_precompiled(pkt, 1, 123 1.1 christos der_mgf1SHA256Identifier, 124 1.1 christos sizeof(der_mgf1SHA256Identifier)) 125 1.1 christos && ossl_DER_w_precompiled(pkt, 0, 126 1.1 christos der_sha256Identifier, 127 1.1 christos sizeof(der_sha256Identifier)) 128 1.1 christos && ossl_DER_w_end_sequence(pkt, DER_NO_CONTEXT)) 129 1.1 christos && ossl_DER_w_precompiled(pkt, DER_NO_CONTEXT, 130 1.1 christos der_id_RSASSA_PSS, 131 1.1 christos sizeof(der_id_RSASSA_PSS)) 132 1.1 christos && ossl_DER_w_end_sequence(pkt, tag); 133 1.1 christos } 134 1.1 christos 135 1.1 christos =head1 SEE ALSO 136 1.1 christos 137 1.1 christos L<ossl_DER_w_bn(3)>, L<ossl_DER_w_begin_sequence(3)>, 138 1.1 christos L<ossl_DER_w_precompiled(3)> 139 1.1 christos 140 1.1 christos =head1 COPYRIGHT 141 1.1 christos 142 1.1 christos Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved. 143 1.1 christos 144 1.1 christos Licensed under the Apache License 2.0 (the "License"). You may not use 145 1.1 christos this file except in compliance with the License. You can obtain a copy 146 1.1 christos in the file LICENSE in the source distribution or at 147 1.1 christos L<https://www.openssl.org/source/license.html>. 148 1.1 christos 149 1.1 christos =cut 150