Class: PqcAsn1::DER::EncryptedKeyInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/pqc_asn1.rb

Overview

Result returned by parse_encrypted_pkcs8.

An immutable value object holding the two opaque fields of an EncryptedPrivateKeyInfo (RFC 5958) structure. This gem is a codec — it does not perform the actual encryption or decryption. Callers are responsible for:

1. Encrypting a PKCS#8 DER blob (e.g. via OpenSSL or libsodium)
 and providing +encryption_algorithm_der+ + +encrypted_data+ to
 {DER.build_encrypted_pkcs8}.
2. Decrypting +encrypted_data+ (using +encryption_algorithm+ as a
 hint for algorithm + parameters) and passing the result to
 {DER.parse_pkcs8}.

Examples:

Round-trip

info = PqcAsn1::DER.parse_encrypted_pkcs8(der)
info.format                          # => :encrypted_pkcs8
info.encryption_algorithm.bytesize   # => AlgorithmIdentifier DER size
info.encrypted_data.bytesize         # => ciphertext size
info.to_pem                          # => "-----BEGIN ENCRYPTED PRIVATE KEY..."

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(encryption_algorithm, encrypted_data) ⇒ EncryptedKeyInfo

Returns a new instance of EncryptedKeyInfo.



456
457
458
459
460
461
# File 'lib/pqc_asn1.rb', line 456

def initialize(encryption_algorithm, encrypted_data)
  @encryption_algorithm = encryption_algorithm
  @encrypted_data = encrypted_data
  @format = :encrypted_pkcs8
  freeze
end

Instance Attribute Details

#encrypted_data ⇒ String (readonly)

Returns raw ciphertext bytes (frozen ASCII-8BIT). These are the contents of the encryptedData OCTET STRING; the outer OCTET STRING TLV is stripped.

Returns:

  • (String) —

    raw ciphertext bytes (frozen ASCII-8BIT). These are the contents of the encryptedData OCTET STRING; the outer OCTET STRING TLV is stripped.



450
451
452
# File 'lib/pqc_asn1.rb', line 450

def encrypted_data
  @encrypted_data
end

#encryption_algorithm ⇒ String (readonly)

Returns full AlgorithmIdentifier DER TLV (frozen ASCII-8BIT). The tag, length, OID, and any algorithm parameters are all included so callers can pass this directly to their cipher implementation.

Returns:

  • (String) —

    full AlgorithmIdentifier DER TLV (frozen ASCII-8BIT). The tag, length, OID, and any algorithm parameters are all included so callers can pass this directly to their cipher implementation.



445
446
447
# File 'lib/pqc_asn1.rb', line 445

def encryption_algorithm
  @encryption_algorithm
end

#format ⇒ Symbol (readonly)

Returns always :encrypted_pkcs8.

Returns:

  • (Symbol) —

    always :encrypted_pkcs8



453
454
455
# File 'lib/pqc_asn1.rb', line 453

def format
  @format
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


499
500
501
502
503
# File 'lib/pqc_asn1.rb', line 499

def ==(other)
  other.is_a?(EncryptedKeyInfo) &&
    @encryption_algorithm == other.encryption_algorithm &&
    @encrypted_data == other.encrypted_data
end

#deconstruct_keys(keys) ⇒ Hash{Symbol => Object}

Pattern-matching support (Ruby 2.7+).

Parameters:

  • keys (Array<Symbol>, nil)

Returns:

  • (Hash{Symbol => Object})


485
486
487
488
489
490
491
492
493
494
495
# File 'lib/pqc_asn1.rb', line 485

def deconstruct_keys(keys)
  return to_h if keys.nil?

  keys.each_with_object({}) do |k, h|
    case k
    when :encryption_algorithm then h[:encryption_algorithm] = @encryption_algorithm
    when :encrypted_data then h[:encrypted_data] = @encrypted_data
    when :format then h[:format] = @format
    end
  end
end

#hash ⇒ Integer

Returns:

  • (Integer)


508
509
510
# File 'lib/pqc_asn1.rb', line 508

def hash
  [@encryption_algorithm, @encrypted_data].hash
end

#inspect ⇒ String

Returns:

  • (String)


513
514
515
516
517
# File 'lib/pqc_asn1.rb', line 513

def inspect
  "#<PqcAsn1::DER::EncryptedKeyInfo " \
    "algo=#{@encryption_algorithm.bytesize}B " \
    "encrypted=#{@encrypted_data.bytesize}B>"
end

#to_der ⇒ String

Re-encode to EncryptedPrivateKeyInfo DER.

Returns:

  • (String) —

    frozen binary DER bytes (ASCII-8BIT)



465
466
467
# File 'lib/pqc_asn1.rb', line 465

def to_der
  PqcAsn1::DER.build_encrypted_pkcs8(@encryption_algorithm, @encrypted_data)
end

#to_h ⇒ Hash{Symbol => Object}

Returns:

  • (Hash{Symbol => Object})


476
477
478
479
480
# File 'lib/pqc_asn1.rb', line 476

def to_h
  {encryption_algorithm: @encryption_algorithm,
   encrypted_data: @encrypted_data,
   format: @format}
end

#to_pem ⇒ String

Re-encode to PEM with label "ENCRYPTED PRIVATE KEY".

Returns:

  • (String) —

    frozen US-ASCII PEM string



471
472
473
# File 'lib/pqc_asn1.rb', line 471

def to_pem
  PqcAsn1::PEM.encode(to_der, "ENCRYPTED PRIVATE KEY")
end