forked from ruby/rbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenssl.rbs
12021 lines (10629 loc) · 386 KB
/
openssl.rbs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# <!-- rdoc-file=ext/openssl/ossl.c -->
# OpenSSL provides SSL, TLS and general purpose cryptography. It wraps the
# [OpenSSL](https://www.openssl.org/) library.
#
# # Examples
#
# All examples assume you have loaded OpenSSL with:
#
# require 'openssl'
#
# These examples build atop each other. For example the key created in the next
# is used in throughout these examples.
#
# ## Keys
#
# ### Creating a Key
#
# This example creates a 2048 bit RSA keypair and writes it to the current
# directory.
#
# key = OpenSSL::PKey::RSA.new 2048
#
# File.write 'private_key.pem', key.private_to_pem
# File.write 'public_key.pem', key.public_to_pem
#
# ### Exporting a Key
#
# Keys saved to disk without encryption are not secure as anyone who gets ahold
# of the key may use it unless it is encrypted. In order to securely export a
# key you may export it with a password.
#
# cipher = OpenSSL::Cipher.new 'aes-256-cbc'
# password = 'my secure password goes here'
#
# key_secure = key.private_to_pem cipher, password
#
# File.write 'private.secure.pem', key_secure
#
# OpenSSL::Cipher.ciphers returns a list of available ciphers.
#
# ### Loading a Key
#
# A key can also be loaded from a file.
#
# key2 = OpenSSL::PKey.read File.read 'private_key.pem'
# key2.public? # => true
# key2.private? # => true
#
# or
#
# key3 = OpenSSL::PKey.read File.read 'public_key.pem'
# key3.public? # => true
# key3.private? # => false
#
# ### Loading an Encrypted Key
#
# OpenSSL will prompt you for your password when loading an encrypted key. If
# you will not be able to type in the password you may provide it when loading
# the key:
#
# key4_pem = File.read 'private.secure.pem'
# password = 'my secure password goes here'
# key4 = OpenSSL::PKey.read key4_pem, password
#
# ## RSA Encryption
#
# RSA provides encryption and decryption using the public and private keys. You
# can use a variety of padding methods depending upon the intended use of
# encrypted data.
#
# ### Encryption & Decryption
#
# Asymmetric public/private key encryption is slow and victim to attack in cases
# where it is used without padding or directly to encrypt larger chunks of data.
# Typical use cases for RSA encryption involve "wrapping" a symmetric key with
# the public key of the recipient who would "unwrap" that symmetric key again
# using their private key. The following illustrates a simplified example of
# such a key transport scheme. It shouldn't be used in practice, though,
# standardized protocols should always be preferred.
#
# wrapped_key = key.public_encrypt key
#
# A symmetric key encrypted with the public key can only be decrypted with the
# corresponding private key of the recipient.
#
# original_key = key.private_decrypt wrapped_key
#
# By default PKCS#1 padding will be used, but it is also possible to use other
# forms of padding, see PKey::RSA for further details.
#
# ### Signatures
#
# Using "private_encrypt" to encrypt some data with the private key is
# equivalent to applying a digital signature to the data. A verifying party may
# validate the signature by comparing the result of decrypting the signature
# with "public_decrypt" to the original data. However, OpenSSL::PKey already has
# methods "sign" and "verify" that handle digital signatures in a standardized
# way - "private_encrypt" and "public_decrypt" shouldn't be used in practice.
#
# To sign a document, a cryptographically secure hash of the document is
# computed first, which is then signed using the private key.
#
# signature = key.sign 'SHA256', document
#
# To validate the signature, again a hash of the document is computed and the
# signature is decrypted using the public key. The result is then compared to
# the hash just computed, if they are equal the signature was valid.
#
# if key.verify 'SHA256', signature, document
# puts 'Valid'
# else
# puts 'Invalid'
# end
#
# ## PBKDF2 Password-based Encryption
#
# If supported by the underlying OpenSSL version used, Password-based Encryption
# should use the features of PKCS5. If not supported or if required by legacy
# applications, the older, less secure methods specified in RFC 2898 are also
# supported (see below).
#
# PKCS5 supports PBKDF2 as it was specified in PKCS#5
# [v2.0](http://www.rsa.com/rsalabs/node.asp?id=2127). It still uses a password,
# a salt, and additionally a number of iterations that will slow the key
# derivation process down. The slower this is, the more work it requires being
# able to brute-force the resulting key.
#
# ### Encryption
#
# The strategy is to first instantiate a Cipher for encryption, and then to
# generate a random IV plus a key derived from the password using PBKDF2. PKCS
# #5 v2.0 recommends at least 8 bytes for the salt, the number of iterations
# largely depends on the hardware being used.
#
# cipher = OpenSSL::Cipher.new 'aes-256-cbc'
# cipher.encrypt
# iv = cipher.random_iv
#
# pwd = 'some hopefully not to easily guessable password'
# salt = OpenSSL::Random.random_bytes 16
# iter = 20000
# key_len = cipher.key_len
# digest = OpenSSL::Digest.new('SHA256')
#
# key = OpenSSL::PKCS5.pbkdf2_hmac(pwd, salt, iter, key_len, digest)
# cipher.key = key
#
# Now encrypt the data:
#
# encrypted = cipher.update document
# encrypted << cipher.final
#
# ### Decryption
#
# Use the same steps as before to derive the symmetric AES key, this time
# setting the Cipher up for decryption.
#
# cipher = OpenSSL::Cipher.new 'aes-256-cbc'
# cipher.decrypt
# cipher.iv = iv # the one generated with #random_iv
#
# pwd = 'some hopefully not to easily guessable password'
# salt = ... # the one generated above
# iter = 20000
# key_len = cipher.key_len
# digest = OpenSSL::Digest.new('SHA256')
#
# key = OpenSSL::PKCS5.pbkdf2_hmac(pwd, salt, iter, key_len, digest)
# cipher.key = key
#
# Now decrypt the data:
#
# decrypted = cipher.update encrypted
# decrypted << cipher.final
#
# ## X509 Certificates
#
# ### Creating a Certificate
#
# This example creates a self-signed certificate using an RSA key and a SHA1
# signature.
#
# key = OpenSSL::PKey::RSA.new 2048
# name = OpenSSL::X509::Name.parse '/CN=nobody/DC=example'
#
# cert = OpenSSL::X509::Certificate.new
# cert.version = 2
# cert.serial = 0
# cert.not_before = Time.now
# cert.not_after = Time.now + 3600
#
# cert.public_key = key.public_key
# cert.subject = name
#
# ### Certificate Extensions
#
# You can add extensions to the certificate with OpenSSL::SSL::ExtensionFactory
# to indicate the purpose of the certificate.
#
# extension_factory = OpenSSL::X509::ExtensionFactory.new nil, cert
#
# cert.add_extension \
# extension_factory.create_extension('basicConstraints', 'CA:FALSE', true)
#
# cert.add_extension \
# extension_factory.create_extension(
# 'keyUsage', 'keyEncipherment,dataEncipherment,digitalSignature')
#
# cert.add_extension \
# extension_factory.create_extension('subjectKeyIdentifier', 'hash')
#
# The list of supported extensions (and in some cases their possible values) can
# be derived from the "objects.h" file in the OpenSSL source code.
#
# ### Signing a Certificate
#
# To sign a certificate set the issuer and use OpenSSL::X509::Certificate#sign
# with a digest algorithm. This creates a self-signed cert because we're using
# the same name and key to sign the certificate as was used to create the
# certificate.
#
# cert.issuer = name
# cert.sign key, OpenSSL::Digest.new('SHA1')
#
# open 'certificate.pem', 'w' do |io| io.write cert.to_pem end
#
# ### Loading a Certificate
#
# Like a key, a cert can also be loaded from a file.
#
# cert2 = OpenSSL::X509::Certificate.new File.read 'certificate.pem'
#
# ### Verifying a Certificate
#
# Certificate#verify will return true when a certificate was signed with the
# given public key.
#
# raise 'certificate can not be verified' unless cert2.verify key
#
# ## Certificate Authority
#
# A certificate authority (CA) is a trusted third party that allows you to
# verify the ownership of unknown certificates. The CA issues key signatures
# that indicate it trusts the user of that key. A user encountering the key can
# verify the signature by using the CA's public key.
#
# ### CA Key
#
# CA keys are valuable, so we encrypt and save it to disk and make sure it is
# not readable by other users.
#
# ca_key = OpenSSL::PKey::RSA.new 2048
# password = 'my secure password goes here'
#
# cipher = 'aes-256-cbc'
#
# open 'ca_key.pem', 'w', 0400 do |io|
# io.write ca_key.private_to_pem(cipher, password)
# end
#
# ### CA Certificate
#
# A CA certificate is created the same way we created a certificate above, but
# with different extensions.
#
# ca_name = OpenSSL::X509::Name.parse '/CN=ca/DC=example'
#
# ca_cert = OpenSSL::X509::Certificate.new
# ca_cert.serial = 0
# ca_cert.version = 2
# ca_cert.not_before = Time.now
# ca_cert.not_after = Time.now + 86400
#
# ca_cert.public_key = ca_key.public_key
# ca_cert.subject = ca_name
# ca_cert.issuer = ca_name
#
# extension_factory = OpenSSL::X509::ExtensionFactory.new
# extension_factory.subject_certificate = ca_cert
# extension_factory.issuer_certificate = ca_cert
#
# ca_cert.add_extension \
# extension_factory.create_extension('subjectKeyIdentifier', 'hash')
#
# This extension indicates the CA's key may be used as a CA.
#
# ca_cert.add_extension \
# extension_factory.create_extension('basicConstraints', 'CA:TRUE', true)
#
# This extension indicates the CA's key may be used to verify signatures on both
# certificates and certificate revocations.
#
# ca_cert.add_extension \
# extension_factory.create_extension(
# 'keyUsage', 'cRLSign,keyCertSign', true)
#
# Root CA certificates are self-signed.
#
# ca_cert.sign ca_key, OpenSSL::Digest.new('SHA1')
#
# The CA certificate is saved to disk so it may be distributed to all the users
# of the keys this CA will sign.
#
# open 'ca_cert.pem', 'w' do |io|
# io.write ca_cert.to_pem
# end
#
# ### Certificate Signing Request
#
# The CA signs keys through a Certificate Signing Request (CSR). The CSR
# contains the information necessary to identify the key.
#
# csr = OpenSSL::X509::Request.new
# csr.version = 0
# csr.subject = name
# csr.public_key = key.public_key
# csr.sign key, OpenSSL::Digest.new('SHA1')
#
# A CSR is saved to disk and sent to the CA for signing.
#
# open 'csr.pem', 'w' do |io|
# io.write csr.to_pem
# end
#
# ### Creating a Certificate from a CSR
#
# Upon receiving a CSR the CA will verify it before signing it. A minimal
# verification would be to check the CSR's signature.
#
# csr = OpenSSL::X509::Request.new File.read 'csr.pem'
#
# raise 'CSR can not be verified' unless csr.verify csr.public_key
#
# After verification a certificate is created, marked for various usages, signed
# with the CA key and returned to the requester.
#
# csr_cert = OpenSSL::X509::Certificate.new
# csr_cert.serial = 0
# csr_cert.version = 2
# csr_cert.not_before = Time.now
# csr_cert.not_after = Time.now + 600
#
# csr_cert.subject = csr.subject
# csr_cert.public_key = csr.public_key
# csr_cert.issuer = ca_cert.subject
#
# extension_factory = OpenSSL::X509::ExtensionFactory.new
# extension_factory.subject_certificate = csr_cert
# extension_factory.issuer_certificate = ca_cert
#
# csr_cert.add_extension \
# extension_factory.create_extension('basicConstraints', 'CA:FALSE')
#
# csr_cert.add_extension \
# extension_factory.create_extension(
# 'keyUsage', 'keyEncipherment,dataEncipherment,digitalSignature')
#
# csr_cert.add_extension \
# extension_factory.create_extension('subjectKeyIdentifier', 'hash')
#
# csr_cert.sign ca_key, OpenSSL::Digest.new('SHA1')
#
# open 'csr_cert.pem', 'w' do |io|
# io.write csr_cert.to_pem
# end
#
# ## SSL and TLS Connections
#
# Using our created key and certificate we can create an SSL or TLS connection.
# An SSLContext is used to set up an SSL session.
#
# context = OpenSSL::SSL::SSLContext.new
#
# ### SSL Server
#
# An SSL server requires the certificate and private key to communicate securely
# with its clients:
#
# context.cert = cert
# context.key = key
#
# Then create an SSLServer with a TCP server socket and the context. Use the
# SSLServer like an ordinary TCP server.
#
# require 'socket'
#
# tcp_server = TCPServer.new 5000
# ssl_server = OpenSSL::SSL::SSLServer.new tcp_server, context
#
# loop do
# ssl_connection = ssl_server.accept
#
# data = ssl_connection.gets
#
# response = "I got #{data.dump}"
# puts response
#
# ssl_connection.puts "I got #{data.dump}"
# ssl_connection.close
# end
#
# ### SSL client
#
# An SSL client is created with a TCP socket and the context. SSLSocket#connect
# must be called to initiate the SSL handshake and start encryption. A key and
# certificate are not required for the client socket.
#
# Note that SSLSocket#close doesn't close the underlying socket by default. Set
# SSLSocket#sync_close to true if you want.
#
# require 'socket'
#
# tcp_socket = TCPSocket.new 'localhost', 5000
# ssl_client = OpenSSL::SSL::SSLSocket.new tcp_socket, context
# ssl_client.sync_close = true
# ssl_client.connect
#
# ssl_client.puts "hello server!"
# puts ssl_client.gets
#
# ssl_client.close # shutdown the TLS connection and close tcp_socket
#
# ### Peer Verification
#
# An unverified SSL connection does not provide much security. For enhanced
# security the client or server can verify the certificate of its peer.
#
# The client can be modified to verify the server's certificate against the
# certificate authority's certificate:
#
# context.ca_file = 'ca_cert.pem'
# context.verify_mode = OpenSSL::SSL::VERIFY_PEER
#
# require 'socket'
#
# tcp_socket = TCPSocket.new 'localhost', 5000
# ssl_client = OpenSSL::SSL::SSLSocket.new tcp_socket, context
# ssl_client.connect
#
# ssl_client.puts "hello server!"
# puts ssl_client.gets
#
# If the server certificate is invalid or `context.ca_file` is not set when
# verifying peers an OpenSSL::SSL::SSLError will be raised.
#
module OpenSSL
# <!--
# rdoc-file=ext/openssl/lib/openssl/digest.rb
# - Digest(name)
# -->
# Returns a Digest subclass by *name*
#
# require 'openssl'
#
# OpenSSL::Digest("MD5")
# # => OpenSSL::Digest::MD5
#
# Digest("Foo")
# # => NameError: wrong constant name Foo
#
def self.Digest: (String name) -> singleton(Digest)
# <!--
# rdoc-file=ext/openssl/ossl.c
# - OpenSSL.debug -> true | false
# -->
#
def self.debug: () -> bool
# <!--
# rdoc-file=ext/openssl/ossl.c
# - OpenSSL.debug = boolean -> boolean
# -->
# Turns on or off debug mode. With debug mode, all errors added to the OpenSSL
# error queue will be printed to stderr.
#
def self.debug=: [U] (boolish) -> U
# <!--
# rdoc-file=ext/openssl/ossl.c
# - OpenSSL.errors -> [String...]
# -->
# See any remaining errors held in queue.
#
# Any errors you see here are probably due to a bug in Ruby's OpenSSL
# implementation.
#
def self.errors: () -> Array[String]
# <!--
# rdoc-file=ext/openssl/ossl.c
# - OpenSSL.fips_mode -> true | false
# -->
#
def self.fips_mode: () -> bool
# <!--
# rdoc-file=ext/openssl/ossl.c
# - OpenSSL.fips_mode = boolean -> boolean
# -->
# Turns FIPS mode on or off. Turning on FIPS mode will obviously only have an
# effect for FIPS-capable installations of the OpenSSL library. Trying to do so
# otherwise will result in an error.
#
# ### Examples
# OpenSSL.fips_mode = true # turn FIPS mode on
# OpenSSL.fips_mode = false # and off again
#
def self.fips_mode=: [U] (boolish) -> U
# <!--
# rdoc-file=ext/openssl/ossl.c
# - OpenSSL.fixed_length_secure_compare(string, string) -> boolean
# -->
# Constant time memory comparison for fixed length strings, such as results of
# HMAC calculations.
#
# Returns `true` if the strings are identical, `false` if they are of the same
# length but not identical. If the length is different, `ArgumentError` is
# raised.
#
def self.fixed_length_secure_compare: (String, String) -> bool
# <!--
# rdoc-file=ext/openssl/lib/openssl.rb
# - OpenSSL.secure_compare(string, string) -> boolean
# -->
# Constant time memory comparison. Inputs are hashed using SHA-256 to mask the
# length of the secret. Returns `true` if the strings are identical, `false`
# otherwise.
#
def self.secure_compare: (String a, String b) -> bool
# <!-- rdoc-file=ext/openssl/ossl.c -->
# Boolean indicating whether OpenSSL is FIPS-capable or not
#
OPENSSL_FIPS: bool
OPENSSL_LIBRARY_VERSION: String
# <!-- rdoc-file=ext/openssl/ossl.c -->
# Version of OpenSSL the ruby OpenSSL extension was built with
#
OPENSSL_VERSION: String
# <!-- rdoc-file=ext/openssl/ossl.c -->
# Version number of OpenSSL the ruby OpenSSL extension was built with (base 16).
# The formats are below.
#
# OpenSSL 3
# : `0xMNN00PP0 (major minor 00 patch 0)`
# OpenSSL before 3
# : `0xMNNFFPPS (major minor fix patch status)`
# LibreSSL
# : `0x20000000 (fixed value)`
#
#
# See also the man page OPENSSL_VERSION_NUMBER(3).
#
OPENSSL_VERSION_NUMBER: Integer
VERSION: String
# <!-- rdoc-file=ext/openssl/ossl_asn1.c -->
# Abstract Syntax Notation One (or ASN.1) is a notation syntax to describe data
# structures and is defined in ITU-T X.680. ASN.1 itself does not mandate any
# encoding or parsing rules, but usually ASN.1 data structures are encoded using
# the Distinguished Encoding Rules (DER) or less often the Basic Encoding Rules
# (BER) described in ITU-T X.690. DER and BER encodings are binary
# Tag-Length-Value (TLV) encodings that are quite concise compared to other
# popular data description formats such as XML, JSON etc. ASN.1 data structures
# are very common in cryptographic applications, e.g. X.509 public key
# certificates or certificate revocation lists (CRLs) are all defined in ASN.1
# and DER-encoded. ASN.1, DER and BER are the building blocks of applied
# cryptography. The ASN1 module provides the necessary classes that allow
# generation of ASN.1 data structures and the methods to encode them using a DER
# encoding. The decode method allows parsing arbitrary BER-/DER-encoded data to
# a Ruby object that can then be modified and re-encoded at will.
#
# ## ASN.1 class hierarchy
#
# The base class representing ASN.1 structures is ASN1Data. ASN1Data offers
# attributes to read and set the *tag*, the *tag_class* and finally the *value*
# of a particular ASN.1 item. Upon parsing, any tagged values (implicit or
# explicit) will be represented by ASN1Data instances because their "real type"
# can only be determined using out-of-band information from the ASN.1 type
# declaration. Since this information is normally known when encoding a type,
# all sub-classes of ASN1Data offer an additional attribute *tagging* that
# allows to encode a value implicitly (`:IMPLICIT`) or explicitly (`:EXPLICIT`).
#
# ### Constructive
#
# Constructive is, as its name implies, the base class for all constructed
# encodings, i.e. those that consist of several values, opposed to "primitive"
# encodings with just one single value. The value of an Constructive is always
# an Array.
#
# #### ASN1::Set and ASN1::Sequence
#
# The most common constructive encodings are SETs and SEQUENCEs, which is why
# there are two sub-classes of Constructive representing each of them.
#
# ### Primitive
#
# This is the super class of all primitive values. Primitive itself is not used
# when parsing ASN.1 data, all values are either instances of a corresponding
# sub-class of Primitive or they are instances of ASN1Data if the value was
# tagged implicitly or explicitly. Please cf. Primitive documentation for
# details on sub-classes and their respective mappings of ASN.1 data types to
# Ruby objects.
#
# ## Possible values for *tagging*
#
# When constructing an ASN1Data object the ASN.1 type definition may require
# certain elements to be either implicitly or explicitly tagged. This can be
# achieved by setting the *tagging* attribute manually for sub-classes of
# ASN1Data. Use the symbol `:IMPLICIT` for implicit tagging and `:EXPLICIT` if
# the element requires explicit tagging.
#
# ## Possible values for *tag_class*
#
# It is possible to create arbitrary ASN1Data objects that also support a
# PRIVATE or APPLICATION tag class. Possible values for the *tag_class*
# attribute are:
# * `:UNIVERSAL` (the default for untagged values)
# * `:CONTEXT_SPECIFIC` (the default for tagged values)
# * `:APPLICATION`
# * `:PRIVATE`
#
#
# ## Tag constants
#
# There is a constant defined for each universal tag:
# * OpenSSL::ASN1::EOC (0)
# * OpenSSL::ASN1::BOOLEAN (1)
# * OpenSSL::ASN1::INTEGER (2)
# * OpenSSL::ASN1::BIT_STRING (3)
# * OpenSSL::ASN1::OCTET_STRING (4)
# * OpenSSL::ASN1::NULL (5)
# * OpenSSL::ASN1::OBJECT (6)
# * OpenSSL::ASN1::ENUMERATED (10)
# * OpenSSL::ASN1::UTF8STRING (12)
# * OpenSSL::ASN1::SEQUENCE (16)
# * OpenSSL::ASN1::SET (17)
# * OpenSSL::ASN1::NUMERICSTRING (18)
# * OpenSSL::ASN1::PRINTABLESTRING (19)
# * OpenSSL::ASN1::T61STRING (20)
# * OpenSSL::ASN1::VIDEOTEXSTRING (21)
# * OpenSSL::ASN1::IA5STRING (22)
# * OpenSSL::ASN1::UTCTIME (23)
# * OpenSSL::ASN1::GENERALIZEDTIME (24)
# * OpenSSL::ASN1::GRAPHICSTRING (25)
# * OpenSSL::ASN1::ISO64STRING (26)
# * OpenSSL::ASN1::GENERALSTRING (27)
# * OpenSSL::ASN1::UNIVERSALSTRING (28)
# * OpenSSL::ASN1::BMPSTRING (30)
#
#
# ## UNIVERSAL_TAG_NAME constant
#
# An Array that stores the name of a given tag number. These names are the same
# as the name of the tag constant that is additionally defined, e.g.
# `UNIVERSAL_TAG_NAME[2] = "INTEGER"` and `OpenSSL::ASN1::INTEGER = 2`.
#
# ## Example usage
#
# ### Decoding and viewing a DER-encoded file
# require 'openssl'
# require 'pp'
# der = File.binread('data.der')
# asn1 = OpenSSL::ASN1.decode(der)
# pp der
#
# ### Creating an ASN.1 structure and DER-encoding it
# require 'openssl'
# version = OpenSSL::ASN1::Integer.new(1)
# # Explicitly 0-tagged implies context-specific tag class
# serial = OpenSSL::ASN1::Integer.new(12345, 0, :EXPLICIT, :CONTEXT_SPECIFIC)
# name = OpenSSL::ASN1::PrintableString.new('Data 1')
# sequence = OpenSSL::ASN1::Sequence.new( [ version, serial, name ] )
# der = sequence.to_der
#
module ASN1
type tagging = :IMPLICIT | :EXPLICIT
type tag_class = :UNIVERSAL | :CONTEXT_SPECIFIC | :APPLICATION | :PRIVATE
def self.BMPString: (String value, ?bn tag, ?tagging tagging) -> BMPString
def self.BitString: (String value, ?bn tag, ?tagging tagging) -> BitString
def self.Boolean: (boolish value, ?bn tag, ?tagging tagging) -> Boolean
def self.EndOfContent: () -> EndOfContent
def self.Enumerated: (bn value, ?bn tag, ?tagging tagging) -> Enumerated
def self.GeneralString: (String value, ?bn tag, ?tagging tagging) -> GeneralString
def self.GeneralizedTime: (::Time value, ?bn tag, ?tagging tagging) -> GeneralizedTime
def self.GraphicString: (String value, ?bn tag, ?tagging tagging) -> GraphicString
def self.IA5String: (String value, ?bn tag, ?tagging tagging) -> IA5String
def self.ISO64String: (String value, ?bn tag, ?tagging tagging) -> ISO64String
def self.Integer: (bn value, ?bn tag, ?tagging tagging) -> Integer
def self.Null: (nil) -> Null
def self.NumericString: (String value, ?bn tag, ?tagging tagging) -> NumericString
def self.ObjectId: (String value, ?bn tag, ?tagging tagging) -> ObjectId
def self.OctetString: (String value, ?bn tag, ?tagging tagging) -> OctetString
def self.PrintableString: (String value, ?bn tag, ?tagging tagging) -> PrintableString
def self.Sequence: (Array[ASN1Data] value, ?bn tag, ?tagging tagging) -> Sequence
def self.Set: (Array[ASN1Data] value, ?bn tag, ?tagging tagging) -> Set
def self.T61String: (String value, ?bn tag, ?tagging tagging) -> T61String
def self.UTCTime: (::Time value, ?bn tag, ?tagging tagging) -> UTCTime
def self.UTF8String: (String value, ?bn tag, ?tagging tagging) -> UTF8String
def self.UniversalString: (String value, ?bn tag, ?tagging tagging) -> UniversalString
def self.VideotexString: (String value, ?bn tag, ?tagging tagging) -> VideotexString
# <!--
# rdoc-file=ext/openssl/ossl_asn1.c
# - OpenSSL::ASN1.decode(der) -> ASN1Data
# -->
# Decodes a BER- or DER-encoded value and creates an ASN1Data instance. *der*
# may be a String or any object that features a `.to_der` method transforming it
# into a BER-/DER-encoded String+
#
# ## Example
# der = File.binread('asn1data')
# asn1 = OpenSSL::ASN1.decode(der)
#
def self.decode: (String | _ToDer der) -> ASN1Data
# <!--
# rdoc-file=ext/openssl/ossl_asn1.c
# - OpenSSL::ASN1.decode_all(der) -> Array of ASN1Data
# -->
# Similar to #decode with the difference that #decode expects one distinct value
# represented in *der*. #decode_all on the contrary decodes a sequence of
# sequential BER/DER values lined up in *der* and returns them as an array.
#
# ## Example
# ders = File.binread('asn1data_seq')
# asn1_ary = OpenSSL::ASN1.decode_all(ders)
#
def self.decode_all: (String | _ToDer der) -> Array[ASN1Data]
# <!--
# rdoc-file=ext/openssl/ossl_asn1.c
# - OpenSSL::ASN1.traverse(asn1) -> nil
# -->
# If a block is given, it prints out each of the elements encountered. Block
# parameters are (in that order):
# * depth: The recursion depth, plus one with each constructed value being
# encountered (Integer)
# * offset: Current byte offset (Integer)
# * header length: Combined length in bytes of the Tag and Length headers.
# (Integer)
# * length: The overall remaining length of the entire data (Integer)
# * constructed: Whether this value is constructed or not (Boolean)
# * tag_class: Current tag class (Symbol)
# * tag: The current tag number (Integer)
#
#
# ## Example
# der = File.binread('asn1data.der')
# OpenSSL::ASN1.traverse(der) do | depth, offset, header_len, length, constructed, tag_class, tag|
# puts "Depth: #{depth} Offset: #{offset} Length: #{length}"
# puts "Header length: #{header_len} Tag: #{tag} Tag class: #{tag_class} Constructed: #{constructed}"
# end
#
def self.traverse: (String | _ToDer der) { ([::Integer, ::Integer, ::Integer, ::Integer, bool, tag_class, ::Integer]) -> void } -> void
BIT_STRING: Integer
BMPSTRING: Integer
BOOLEAN: Integer
CHARACTER_STRING: Integer
EMBEDDED_PDV: Integer
ENUMERATED: Integer
EOC: Integer
EXTERNAL: Integer
GENERALIZEDTIME: Integer
GENERALSTRING: Integer
GRAPHICSTRING: Integer
IA5STRING: Integer
INTEGER: Integer
ISO64STRING: Integer
NULL: Integer
NUMERICSTRING: Integer
OBJECT: Integer
OBJECT_DESCRIPTOR: Integer
OCTET_STRING: Integer
PRINTABLESTRING: Integer
REAL: Integer
RELATIVE_OID: Integer
SEQUENCE: Integer
SET: Integer
T61STRING: Integer
UNIVERSALSTRING: Integer
# <!-- rdoc-file=ext/openssl/ossl_asn1.c -->
# Array storing tag names at the tag's index.
#
UNIVERSAL_TAG_NAME: Array[untyped]
UTCTIME: Integer
UTF8STRING: Integer
VIDEOTEXSTRING: Integer
interface _ToDer
def to_der: () -> String
end
# <!-- rdoc-file=ext/openssl/ossl_asn1.c -->
# The top-level class representing any ASN.1 object. When parsed by ASN1.decode,
# tagged values are always represented by an instance of ASN1Data.
#
# ## The role of ASN1Data for parsing tagged values
#
# When encoding an ASN.1 type it is inherently clear what original type (e.g.
# INTEGER, OCTET STRING etc.) this value has, regardless of its tagging. But
# opposed to the time an ASN.1 type is to be encoded, when parsing them it is
# not possible to deduce the "real type" of tagged values. This is why tagged
# values are generally parsed into ASN1Data instances, but with a different
# outcome for implicit and explicit tagging.
#
# ### Example of a parsed implicitly tagged value
#
# An implicitly 1-tagged INTEGER value will be parsed as an ASN1Data with
# * *tag* equal to 1
# * *tag_class* equal to `:CONTEXT_SPECIFIC`
# * *value* equal to a String that carries the raw encoding of the INTEGER.
#
# This implies that a subsequent decoding step is required to completely decode
# implicitly tagged values.
#
# ### Example of a parsed explicitly tagged value
#
# An explicitly 1-tagged INTEGER value will be parsed as an ASN1Data with
# * *tag* equal to 1
# * *tag_class* equal to `:CONTEXT_SPECIFIC`
# * *value* equal to an Array with one single element, an instance of
# OpenSSL::ASN1::Integer, i.e. the inner element is the non-tagged primitive
# value, and the tagging is represented in the outer ASN1Data
#
#
# ## Example - Decoding an implicitly tagged INTEGER
# int = OpenSSL::ASN1::Integer.new(1, 0, :IMPLICIT) # implicit 0-tagged
# seq = OpenSSL::ASN1::Sequence.new( [int] )
# der = seq.to_der
# asn1 = OpenSSL::ASN1.decode(der)
# # pp asn1 => #<OpenSSL::ASN1::Sequence:0x87326e0
# # @indefinite_length=false,
# # @tag=16,
# # @tag_class=:UNIVERSAL,
# # @tagging=nil,
# # @value=
# # [#<OpenSSL::ASN1::ASN1Data:0x87326f4
# # @indefinite_length=false,
# # @tag=0,
# # @tag_class=:CONTEXT_SPECIFIC,
# # @value="\x01">]>
# raw_int = asn1.value[0]
# # manually rewrite tag and tag class to make it an UNIVERSAL value
# raw_int.tag = OpenSSL::ASN1::INTEGER
# raw_int.tag_class = :UNIVERSAL
# int2 = OpenSSL::ASN1.decode(raw_int)
# puts int2.value # => 1
#
# ## Example - Decoding an explicitly tagged INTEGER
# int = OpenSSL::ASN1::Integer.new(1, 0, :EXPLICIT) # explicit 0-tagged
# seq = OpenSSL::ASN1::Sequence.new( [int] )
# der = seq.to_der
# asn1 = OpenSSL::ASN1.decode(der)
# # pp asn1 => #<OpenSSL::ASN1::Sequence:0x87326e0
# # @indefinite_length=false,
# # @tag=16,
# # @tag_class=:UNIVERSAL,
# # @tagging=nil,
# # @value=
# # [#<OpenSSL::ASN1::ASN1Data:0x87326f4
# # @indefinite_length=false,
# # @tag=0,
# # @tag_class=:CONTEXT_SPECIFIC,
# # @value=
# # [#<OpenSSL::ASN1::Integer:0x85bf308
# # @indefinite_length=false,
# # @tag=2,
# # @tag_class=:UNIVERSAL
# # @tagging=nil,
# # @value=1>]>]>
# int2 = asn1.value[0].value[0]
# puts int2.value # => 1
#
class ASN1Data
# <!-- rdoc-file=ext/openssl/ossl_asn1.c -->
# Never `nil`. A boolean value indicating whether the encoding uses indefinite
# length (in the case of parsing) or whether an indefinite length form shall be
# used (in the encoding case). In DER, every value uses definite length form.
# But in scenarios where large amounts of data need to be transferred it might
# be desirable to have some kind of streaming support available. For example,
# huge OCTET STRINGs are preferably sent in smaller-sized chunks, each at a
# time. This is possible in BER by setting the length bytes of an encoding to
# zero and by this indicating that the following value will be sent in chunks.
# Indefinite length encodings are always constructed. The end of such a stream
# of chunks is indicated by sending a EOC (End of Content) tag. SETs and
# SEQUENCEs may use an indefinite length encoding, but also primitive types such
# as e.g. OCTET STRINGS or BIT STRINGS may leverage this functionality (cf.
# ITU-T X.690).
#
def indefinite_length: () -> bool
# <!-- rdoc-file=ext/openssl/ossl_asn1.c -->
# Never `nil`. A boolean value indicating whether the encoding uses indefinite
# length (in the case of parsing) or whether an indefinite length form shall be
# used (in the encoding case). In DER, every value uses definite length form.
# But in scenarios where large amounts of data need to be transferred it might
# be desirable to have some kind of streaming support available. For example,
# huge OCTET STRINGs are preferably sent in smaller-sized chunks, each at a
# time. This is possible in BER by setting the length bytes of an encoding to
# zero and by this indicating that the following value will be sent in chunks.
# Indefinite length encodings are always constructed. The end of such a stream
# of chunks is indicated by sending a EOC (End of Content) tag. SETs and
# SEQUENCEs may use an indefinite length encoding, but also primitive types such
# as e.g. OCTET STRINGS or BIT STRINGS may leverage this functionality (cf.
# ITU-T X.690).
#
def indefinite_length=: [U] (boolish) -> U
# <!-- rdoc-file=ext/openssl/ossl_asn1.c -->
# Never `nil`. A boolean value indicating whether the encoding uses indefinite
# length (in the case of parsing) or whether an indefinite length form shall be
# used (in the encoding case). In DER, every value uses definite length form.
# But in scenarios where large amounts of data need to be transferred it might
# be desirable to have some kind of streaming support available. For example,
# huge OCTET STRINGs are preferably sent in smaller-sized chunks, each at a
# time. This is possible in BER by setting the length bytes of an encoding to
# zero and by this indicating that the following value will be sent in chunks.
# Indefinite length encodings are always constructed. The end of such a stream
# of chunks is indicated by sending a EOC (End of Content) tag. SETs and
# SEQUENCEs may use an indefinite length encoding, but also primitive types such
# as e.g. OCTET STRINGS or BIT STRINGS may leverage this functionality (cf.
# ITU-T X.690).
#
alias infinite_length indefinite_length
# <!-- rdoc-file=ext/openssl/ossl_asn1.c -->
# Never `nil`. A boolean value indicating whether the encoding uses indefinite
# length (in the case of parsing) or whether an indefinite length form shall be
# used (in the encoding case). In DER, every value uses definite length form.
# But in scenarios where large amounts of data need to be transferred it might
# be desirable to have some kind of streaming support available. For example,
# huge OCTET STRINGs are preferably sent in smaller-sized chunks, each at a
# time. This is possible in BER by setting the length bytes of an encoding to
# zero and by this indicating that the following value will be sent in chunks.
# Indefinite length encodings are always constructed. The end of such a stream
# of chunks is indicated by sending a EOC (End of Content) tag. SETs and
# SEQUENCEs may use an indefinite length encoding, but also primitive types such
# as e.g. OCTET STRINGS or BIT STRINGS may leverage this functionality (cf.