DERlib.pod 4.9 KB

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